aboutsummaryrefslogtreecommitdiff
path: root/src/deprecation.c
blob: 3c48db75786d0090fa6d202f2ca3c1b999aa0385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* This file is part of Mailfromd.
   Copyright (C) 2005-2011, 2015 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "mailfromd.h"

static int deprecated_features;

int
enable_deprecation_warnings()
{
	static int retval = -1;

	if (retval == -1) {
		char *p = getenv("MAILFROMD_DEPRECATION");
		if (!p)
			retval = 1;
		else if (strcmp(p, "no") == 0)
			retval = 0;
	}
	return retval;
}

void
deprecation_warning_locus(const struct locus *locus, const char *fmt, ...)
{
	if (enable_deprecation_warnings()) {
		va_list ap;

		va_start(ap, fmt);
		print_parse_message(locus, _("warning"), fmt, ap);
		va_end(ap);
	}
	deprecated_features = 1;
}

void
deprecation_warning(const char *fmt, ...)
{
	if (enable_deprecation_warnings()) {
		va_list ap;

		va_start(ap, fmt);
		print_parse_message(get_locus(), _("warning"), fmt, ap);
		va_end(ap);
	}
	deprecated_features = 1;
}

void
final_deprecation_warning()
{
	if (deprecated_features && !enable_deprecation_warnings()) {
		mu_diag_output(MU_DIAG_WARNING,
			       _("deprecated features used"));
		mu_diag_output(MU_DIAG_WARNING,
			       _("unset environment variable MAILFROMD_DEPRECATION,"
				 " or set it to `yes' to see detailed diagnostics"));
	}
}



static int fixup_opweight[] = {
	0,    /* fixup_noop */
	90,   /* fixup_delete_line */
	91,   /* fixup_insert_line */
	92,   /* fixup_append_line */
	93,   /* fixup_replace_line */
	0,    /* fixup_delete_chars */
	1,    /* fixup_insert_chars */
	2,    /* fixup_replace_chars */
	100,   /* fixup_rename_file */
	1000  /* fixup_end */
};

struct fixup_cmd {
	enum fixup_op opcode;
	mf_yyltype_t locus;
	char *arg;
};

static struct obstack fixup_stk;
static struct obstack fixup_literal_stk;
static size_t fixup_cnt;

static void
init_fixup_stk()
{
	if (fixup_cnt == 0) {
		obstack_init(&fixup_stk);
		obstack_init(&fixup_literal_stk);
	}
}

void
add_fixup_command(enum fixup_op opcode, mf_yyltype_t *locus, char *arg)
{
	struct fixup_cmd cmd;

	if (!enable_deprecation_warnings())
		return;
	if (opcode < 0 || opcode > fixup_end)
		abort();
	init_fixup_stk();
	cmd.opcode = opcode;
	if (locus)
		cmd.locus = *locus;
	else
		memset(&cmd.locus, 0, sizeof(cmd.locus));
	cmd.arg = arg;
	obstack_grow(&fixup_stk, &cmd, sizeof(cmd));
	fixup_cnt++;
}

void
add_fixup_command_fmt(enum fixup_op opcode, mf_yyltype_t *locus,
		      const char *fmt, ...)
{
	va_list ap;
	char *str;
	
	va_start(ap, fmt);
	vasprintf(&str, fmt, ap);
	va_end(ap);

	init_fixup_stk();
	obstack_grow(&fixup_literal_stk, str, strlen(str));
	obstack_1grow(&fixup_literal_stk, 0);
	free(str);
	add_fixup_command(opcode, locus, obstack_finish(&fixup_literal_stk));
}
			  
void
add_fixup_command_locus(enum fixup_op opcode, const struct locus *beg,
			char *arg)
{
	mf_yyltype_t locus;
	locus.beg = *beg;
	add_fixup_command(opcode, &locus, arg);
}

static int
fixup_cmd_comp(const void *a, const void *b)
{
	const struct fixup_cmd *acmd = a;
	const struct fixup_cmd *bcmd = b;
	int rc;

	if (acmd->opcode == fixup_end)
		return 1;
	else if (bcmd->opcode == fixup_end)
		return -1;
	rc = strcmp(acmd->locus.beg.file, bcmd->locus.beg.file);
	if (rc)
		return rc;

	if (acmd->opcode != bcmd->opcode)
		return fixup_opweight[acmd->opcode] -
			fixup_opweight[bcmd->opcode];

	switch (acmd->opcode) {
	case fixup_delete_line:
	case fixup_replace_line:
		if (acmd->locus.beg.line > bcmd->locus.beg.line)
			rc = -1;
		else if (acmd->locus.beg.line < bcmd->locus.beg.line)
			rc = 1;
		else
			rc = 0;
		break;
		
	case fixup_insert_line:
	case fixup_delete_chars:
	case fixup_insert_chars:
	case fixup_replace_chars:
		if (acmd->locus.beg.line > bcmd->locus.beg.line)
			rc = 1;
		else if (acmd->locus.beg.line < bcmd->locus.beg.line)
			rc = -1;
		else if (acmd->locus.beg.point > bcmd->locus.beg.point)
			rc = -1;
		else if (acmd->locus.beg.point < bcmd->locus.beg.point)
			rc = 1;
		else
			rc = 0;
		break;
		
	case fixup_append_line:
	case fixup_rename_file:
	case fixup_end:
	case fixup_noop:
		rc = 0;
	}
	return rc;
}

static void
fprint_ml(FILE *fp, const char *arg)
{
	/* FIXME: Quote (escape) arg */
	while (*arg) {
		int len = strcspn(arg, "\n");
		fwrite(arg, len, 1, fp);
		arg += len;
		if (*arg) {
			fprintf(fp, "\\\n");
			arg++;
		}
	}
}

void
format_fixup_cmd(FILE *fp, const struct fixup_cmd *cmd)
{
	switch (cmd->opcode) {
	case fixup_delete_line:
		fprintf(fp, "%lud\n", (unsigned long) cmd->locus.beg.line);
		break;

	case fixup_insert_line:
		fprintf(fp, "%lui\\\n", (unsigned long) cmd->locus.beg.line);
		fprint_ml(fp, cmd->arg);
		fprintf(fp, "\n");		
		break;
		
	case fixup_append_line:
		fprintf(fp, "$a\\\n");
		fprint_ml(fp, cmd->arg);
		fprintf(fp, "\n");		
		break;

	case fixup_replace_line:
		fprintf(fp, "%luc\\\n", cmd->locus.beg.line);
		fprint_ml(fp, cmd->arg);
		fprintf(fp, "\n");
		break;
		
	case fixup_delete_chars:
		fprintf(fp, "%lus/\\(.\\{%lu\\}\\)\\(.\\{%lu\\}\\)\\(.*\\)/\\1\\3/\n",
			cmd->locus.beg.line,
			cmd->locus.beg.point,
			cmd->locus.end.point - cmd->locus.beg.point);
		break;
		
	case fixup_insert_chars:
		fprintf(fp, "%lus/\\(.\\{%lu\\}\\)\\(.*\\)/\\1%s\\2/\n",
			cmd->locus.beg.line,
			cmd->locus.beg.point,
			cmd->arg);
		break;
		
	case fixup_replace_chars:
		fprintf(fp, "%lus/\\(.\\{%lu\\}\\)\\(.\\{%lu\\}\\)\\(.*\\)/\\1%s\\3/\n",
			cmd->locus.beg.line,
			cmd->locus.beg.point,
			cmd->locus.end.point - cmd->locus.beg.point,
			cmd->arg);
		break;
		
	case fixup_noop:
	case fixup_rename_file:
	case fixup_end:
		break;
	}
}


static void
condense_append_line(struct fixup_cmd *cmd, size_t start, size_t *pend)
{
	size_t i;
	const char *last_file;
	
	if (cmd[start+1].opcode != fixup_append_line) {
		*pend = start+1;
		return;
	}
	last_file = cmd[start].locus.beg.file;
	for (i = start; i < fixup_cnt; i++) {
		if (cmd[i].opcode != fixup_append_line
		    || strcmp(cmd[i].locus.beg.file, last_file))
			break;
		if (i > start)
			obstack_1grow(&fixup_literal_stk, '\n');
		obstack_grow(&fixup_literal_stk, cmd[i].arg,
			     strlen(cmd[i].arg));
		cmd[i].opcode = fixup_noop;
	}
	obstack_1grow(&fixup_literal_stk, 0);
	cmd[start].opcode = fixup_append_line;
	cmd[start].arg = obstack_finish(&fixup_literal_stk);
	*pend = i;
}

/* Condense fixup_append_lines */
static void
condense_all_appends(struct fixup_cmd *cmd)
{
	size_t i;
	for (i = 0; i < fixup_cnt; i++) {
		if (cmd[i].opcode == fixup_end)
			break;
		if (cmd[i].opcode == fixup_append_line)
			condense_append_line(cmd, i, &i);
		else
			i++;
	}
}


static void
script_prologue(FILE *fp)
{
	time_t now;
	fprintf(fp, "#! /bin/sh\n");
	fprintf(fp, "# Conversion script created by mailfromd on %s",
		 ctime (&now));
	fprintf(fp, "# Run this script to fix mailfromd deprecation warnings\n\n");
	fprintf(fp, "set -e\n\n");

	fprintf(fp,
		"modfiles=\n"
		"script=newconf.$$\n"
		"trap \"rm -f $script\" 1 2 13 15\n");
}

char script_epilogue[] = "\
rm -f $script\n\
set -- $modfiles\n\
while test $# -ne 0\n\
do\n\
  orig=$1\n\
  shift\n\
  file=$1\n\
  shift\n\
  mv $orig $orig.bak\n\
  mv $orig.tmp $file\n\
done\n";


void
fixup_create_script()
{
	struct fixup_cmd *cmd, *cmdbase;
	size_t i;
	FILE *fp;
	const char *file_name = "/tmp/mailfromd-newconf.sh";
	const char *last_file;
	const char *new_name;

	if (fixup_cnt == 0)
		return;

	fp = fopen(file_name, "w");
	if (fp) {
		mu_diag_output(MU_DIAG_INFO,
			       _("run script %s to fix the above warnings"),
			       file_name);
	} else
		fp = stdout;

	add_fixup_command(fixup_end, NULL, NULL);
	cmdbase = obstack_finish(&fixup_stk);
	qsort(cmdbase, fixup_cnt, sizeof(*cmd), fixup_cmd_comp);
	
	condense_all_appends(cmdbase);

	script_prologue(fp);
	
	new_name = NULL;
	last_file = cmdbase->locus.beg.file;
	fprintf(fp, "#\n# Fix %s\n#\n", last_file);
	fprintf(fp, "cat > $script <<'EOT'\n");
	for (i = 0, cmd = cmdbase; i < fixup_cnt; i++, cmd++) {
		if (cmd->opcode == fixup_end
		    || strcmp(cmd->locus.beg.file, last_file)) {

			/* Terminate current here-document */
			fprintf(fp, "EOT\n");

			/* Check if the last_file has not been modified
			   in between */
			fprintf(fp,
				"if test %s -nt $0; then\n"
				"  echo >&2 \"$0: %s is updated, re-run mailfromd --lint\"\n"
				"  exit 1\n"
				"fi\n",
				last_file, last_file);

			/* Modify the file */
			fprintf(fp, "test -r %s || "
				"echo \"# File generated by $0 on `date`\" > %s\n",
				last_file, last_file);
			fprintf(fp, "sed -f $script < %s > %s.tmp\n",
				last_file, last_file);
			fprintf(fp, "if cmp %s %s.tmp >/dev/null 2>&1; then\n",
				last_file, last_file);
			fprintf(fp, "  rm %s.tmp\n", last_file); 
			fprintf(fp, "else\n  modfiles=\"$modfiles %s %s\"\nfi\n",
				last_file, new_name ? new_name : last_file);

			if (cmd->opcode == fixup_end)
				break;
			new_name = NULL;
			last_file = cmd->locus.beg.file;
			fprintf(fp, "#\n# Fix %s\n#\n", last_file);
			fprintf(fp, "cat > $script <<'EOT'\n");
		}
		if (cmd->opcode == fixup_rename_file)
			new_name = cmd->arg;
		else
			format_fixup_cmd(fp, cmd);
	}

	fprintf(fp, "#\n# Flush changes\n#\n");
	fprintf(fp, "%s\n", script_epilogue);
	fprintf(fp, "# EOF\n");
	fclose(fp);		
	
	/* Free allocated memory */
	obstack_free(&fixup_stk, NULL);
	obstack_free(&fixup_literal_stk, NULL);
}

Return to:

Send suggestions and report system problems to the System administrator.