File: subst.c

package info (click to toggle)
ipac 0.99-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 180 kB
  • ctags: 64
  • sloc: perl: 878; ansic: 451; sh: 169; makefile: 69
file content (596 lines) | stat: -rw-r--r-- 12,106 bytes parent folder | download | duplicates (3)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*  $Id: subst.c,v 1.4 1998/05/26 09:25:07 moritz Exp $
**
**  A C version of Henry Spencer's "subst" script.
*/
#include <stdio.h>
#include <signal.h>
#include <errno.h>

#define LINESIZE		1024
#define FNAMESIZE		1024
#define PARAMSIZE		128
#define WHITE(c)		((c) == ' ' || (c) == '\t')


/*
**  AFS doesn't support hard links, so enable this #define.
#define USE_RENAME
*/

/*
**  If you don't have getopt in your C library, enable this #define.
#define NEED_GETOPT
*/

/* Linux and FreeBSD have sys_errlist defined in their stdio.h files. */
#if !defined(__FreeBSD__) && !defined(__linux__)
extern char	*sys_errlist[];
#endif

 

typedef struct _PAIR {
    char	*Name;
    int		Length;
    char	*Value;
} PAIR;

static char	*argv0;
extern char	*optarg;
extern int	optind;

extern void	exit();
extern char	*malloc();
extern char	*strcpy();



/*
**  Local implementations of common C library functions.
*/

/*
**  Return string represtation of errno.
*/
static char *
xstrerror()
{
    extern int	sys_nerr;
    extern int	errno;
    static char	buff[30];

    if (errno >= 0 && errno < sys_nerr)
	return (char *) (sys_errlist[errno]);
    (void)sprintf(buff, "Error code %d\n", errno);
    return buff;
}


/*
**  Return the first occurrence of 'c' in 'p' or NULL if not there.
*/
static char *
xstrchr(p, c)
    register char	*p;
    register char	c;
{
    for ( ; *p; p++)
	if (*p == c)
	    return p;
    return NULL;
}


/*
**  Return the last occurrence of 'c' in 'p' or NULL if not there.
*/
static char *
xstrrchr(p, c)
    register char	*p;
    register char	c;
{
    register char	*ret;

    for (ret = NULL; *p; p++)
	if (*p == c)
	    ret = p;
    return ret;
}


/*
**  Copy a string to malloc'd memory or exit.
*/
static char *
xstrdup(p)
    char	*p;
{
    char	*new;

    if ((new = malloc(strlen(p) + 1)) == NULL) {
	(void)fprintf(stderr, "%s: Can't copy \"%s\", %s\n",
		argv0, p, xstrerror());
	exit(1);
    }
    return strcpy(new, p);
}

#if	defined(NEED_GETOPT)

#define TYPE	int

#define ERR(s, c)					\
    if (opterr) {					\
	char buff[2];					\
	buff[0] = c; buff[1] = '\n';			\
	(void)write(2, av[0], (TYPE)strlen(av[0]));	\
	(void)write(2, s, (TYPE)strlen(s));		\
	(void)write(2, buff, 2);			\
    }

int	 opterr = 1;
int	 optind = 1;
int	 optopt;
char	*optarg;

/*
**  Return options and their values from the command line.
**  This comes from the AT&T public-domain getopt published in mod.sources
**  (i.e., comp.sources.unix before the great Usenet renaming).
*/
int
getopt(ac, av, opts)
    int		ac;
    char	*av[];
    char	*opts;
{
    static int	i = 1;
    char	*p;

    /* Move to next value from argv? */
    if (i == 1) {
	if (optind >= ac || av[optind][0] != '-' || av[optind][1] == '\0')
	    return EOF;
	if (strcmp(av[optind], "--") == 0) {
	    optind++;
	    return EOF;
	}
    }

    /* Get next option character. */
    if ((optopt = av[optind][i]) == ':' || (p = IDX(opts,  optopt)) == NULL) {
	ERR(": illegal option -- ", optopt);
	if (av[optind][++i] == '\0') {
	    optind++;
	    i = 1;
	}
	return '?';
    }

    /* Snarf argument? */
    if (*++p == ':') {
	if (av[optind][i + 1] != '\0')
	    optarg = &av[optind++][i + 1];
	else {
	    if (++optind >= ac) {
		ERR(": option requires an argument -- ", optopt);
		i = 1;
		return '?';
	    }
	    optarg = av[optind++];
	}
	i = 1;
    }
    else {
	if (av[optind][++i] == '\0') {
	    i = 1;
	    optind++;
	}
	optarg = NULL;
    }

    return optopt;
}
#endif	/* defined(NEED_GETOPT) */



/*
**  Simulate "mv $from $to" -- return no useful status.  We know that
**  the $from and $to are on the same filesystem.
*/
static void
mv(from, to)
    char	*from;
    char	*to;
{
    if (unlink(to) < 0 && errno != ENOENT) {
	(void)fprintf(stderr, "%s: Can't unlink %s, %s\n",
	    argv0, to, xstrerror());
	return;
    }
#if	defined(USE_RENAME)
    if (rename(from, to) < 0) {
	(void)fprintf(stderr, "%s: Can't rename %s to %s, %s\n",
		argv0, from, to, xstrerror());
	return;
    }
#else
    if (link(from, to) < 0) {
	(void)fprintf(stderr, "%s: Can't link %s to %s, %s\n",
		argv0, from, to, xstrerror());
	return;
    }
    if (unlink(from) < 0)
	(void)fprintf(stderr, "%s: Can't unlink %s, %s\n",
	    argv0, from, xstrerror());
#endif	/* defined(USE_RENAME) */
}


/*
**  Simulate "cmp -s $n1 $n2" -- return 0 if files are the same.
*/
static int
cmp(n1, n2)
    char	*n1;
    char	*n2;
{
    FILE	*f1;
    FILE	*f2;
    int		c;

    if ((f1 = fopen(n1, "r")) == NULL)
	return 1;
    if ((f2 = fopen(n2, "r")) == NULL) {
	(void)fclose(f1);
	return 1;
    }
    while ((c = getc(f1)) != EOF)
	if (getc(f2) != c) {
	    (void)fclose(f1);
	    (void)fclose(f2);
	    return 1;
	}
    if (getc(f2) != EOF) {
	(void)fclose(f1);
	(void)fclose(f2);
	return 1;
    }
    (void)fclose(f1);
    (void)fclose(f2);
    return 0;
}


/*
**  If line does not look like a template, return NULL, otherwise modify
**  it to delete the trailing gunk and return the start of the template.
*/
static char *
istemplate(line)
    char	*line;
{
    char	*p;
    char	*start;

    /* Find "=()<" and remember where it starts. */
    for (p = line; (p = xstrchr(p, '=')) != NULL; p++)
	if (p[1] == '(' && p[2] == ')' && p[3] == '<')
	    break;
    if (p == NULL)
	return NULL;
    start = &p[4];

    /* Now find ">()=" and nip it off. */
    for (p = start; (p = xstrchr(p, '>')) != NULL; p++)
	if (p[1] == '(' && p[2] == ')' && p[3] == '=') {
	    *p++ = '\n';
	    *p = '\0';
	    return start;
	}
    return NULL;
}


/*
**  Splice three strings together, returning an allocated copy.
*/
static char *
splice(s1, s2, s3)
    char	*s1;
    char	*s2;
    char	*s3;
{
    int		i;
    char	*new;

    i = strlen(s1) + strlen(s2) + strlen(s3) + 1;
    if ((new = malloc(i)) == NULL) {
	(void)fprintf(stderr, "%s: Can't splice %s+%s+%s, %s\n",
	    argv0, s1, s2, s3, xstrerror());
	exit(1);
    }
    (void)sprintf(new, "%s%s%s", s1, s2, s3);
    return new;
}


/*
**  Substitute all found patterns in the line and print it.  Using the goto
**  makes the code more clear than using do/while.
*/
static int
doline(f, out, line, tp, end)
    char	*f;
    FILE	*out;
    char	*line;
    PAIR	*tp;
    PAIR	*end;
{
    char	*p;
    char	*new;
    char	save;
    int		count;

    for (count = 0, line = xstrdup(line); tp < end; tp++) {
Again:
	for (p = line; (p = xstrchr(p, tp->Name[0])) != NULL; p++)
	    if (strncmp(p, tp->Name, tp->Length) == 0) {
		save = *p;
		*p = '\0';
		count++;
		new = splice(line, tp->Value, p + tp->Length);
		*p = save;
		if (strcmp(new, line) == 0) {
		    (void)fprintf(stderr, "%s:  subst loop in %s:\n\t%s\n",
			    argv0, f, line);
		    free(new);
		    break;
		}
		free(line);
		line = new;
		goto Again;
	    }
    }
    if (count > 0 && fputs(line, out) == EOF) {
	(void)fprintf(stderr, "%s:  can't write %s, %s\n",
		argv0, f, xstrerror());
	free(line);
	return -1;
    }
    free(line);
    return count;
}


/*
**  Process one file, carefully substituting it in place.
*/
static void
Process(f, Table, end)
    char	*f;
    PAIR	*Table;
    PAIR	*end;
{
    char	new[FNAMESIZE];
    char	old[FNAMESIZE];
    char	line[LINESIZE];
    int		bad;
    int		i;
    int		count;
    FILE	*in;
    FILE	*out;
    FILE	*temp;
    char	*p;

    /* First, figure out temporary names. */
    if ((p = xstrrchr(f, '/')) == NULL) {
	(void)strcpy(new, "substtmp.new");
	(void)strcpy(old, "substtmp.old");
    }
    else {
	*p = '\0';
	(void)sprintf(new, "%s/substtmp.new", f);
	(void)sprintf(old, "%s/substtmp.old", f);
	*p = '/';
    }

    /* Test existences. */
    if ((in = fopen(f, "r")) == NULL) {
	(void)fprintf(stderr, "%s: can't open %s, %s\n",
		argv0, f, xstrerror());
	return;
    }
    if ((temp = fopen(new, "r")) != NULL) {
	(void)fclose(in);
	(void)fprintf(stderr, "%s: %s exists, cannot proceed\n",
		argv0, new);
	exit(1);
    }
    if ((temp = fopen(old, "r")) != NULL) {
	(void)fprintf(stderr, "%s: %s exists, cannot proceed\n",
		argv0, old);
	exit(1);
    }
    temp = fopen(old, "w");
    out = fopen(new, "w");
    if (out == NULL || temp == NULL) {
	if (temp != NULL)
	    (void)fclose(temp);
	(void)unlink(old);
	if (out != NULL)
	    (void)fclose(out);
	(void)unlink(new);
	(void)fprintf(stderr, "%s: cannot create temporaries %s and %s\n",
		argv0, old, new);
	exit(1);
    }
    (void)fclose(temp);

    /* Generate the new version. */
    for (i = 1, bad = 0; fgets(line, sizeof line, in) != NULL; i++) {
	if ((p = xstrchr(line, '\n')) == NULL) {
	    (void)fprintf(stderr,
	      "%s: Line %d of %s is too long (or doesn't end with a newline)\n",
		    argv0, i, f);
	    bad++;
	    break;
	}
	(void)fputs(line, out);
	if ((p = istemplate(line)) != NULL) {
	    if ((count = doline(f, out, p, Table, end)) < 0) {
		bad++;
		break;
	    }
	    if (count > 0) {
		(void)fgets(line, sizeof line, in);
		i++;
	    }
	    else
		(void)fprintf(stderr,
			"%s: %s:%d unknown parameter or bad line:\n\t%s",
			argv0, f, i, p);
	}
    }
    (void)fclose(in);
    if (fflush(out) == EOF || fclose(out) == EOF) {
	(void)fprintf(stderr, "%s: can't close %s, %s\n",
		argv0, f, xstrerror());
	bad++;
    }

    if (bad || cmp(new, f) == 0) {
	(void)unlink(old);
	(void)unlink(new);
	(void)printf("%s: unchanged\n", f);
	return;
    }

    /* Substitute new for old the only safe way -- ignore signals. */
    (void)signal(SIGHUP, SIG_IGN);
    (void)signal(SIGINT, SIG_IGN);
    (void)signal(SIGTERM, SIG_IGN);
    mv(f, old);
    mv(new, f);
    (void)signal(SIGHUP, SIG_DFL);
    (void)signal(SIGINT, SIG_DFL);
    (void)signal(SIGTERM, SIG_DFL);
    (void)printf("%s: updated\n", f);
    (void)unlink(old);
}


/*
**  Print usage message and exit.
*/
static void
Usage()
{
    (void)fprintf(stderr, "Usage: %s -f file victims...\n", argv0);
    exit(1);
}


int
main(ac, av)
    int		ac;
    char	*av[];
{
    static char	NIL[] = "";
    char	*ctlfile;
    char	*p;
    char	*dest;
    FILE	*F;
    int		i;
    char	buff[LINESIZE];
    char	name[PARAMSIZE];
    PAIR	*Table;
    PAIR	*tp;

    /* Set defaults. */
    ctlfile = NULL;
    argv0 = av[0];

    /* Parse JCL. */
    while ((i = getopt(ac, av, "f:")) != EOF)
	switch (i) {
	default:
	    Usage();
	    /* NOTREACHED */
	case 'f':
	    if (ctlfile != NULL)
		Usage();
	    ctlfile = optarg;
	    break;
	}
    ac -= optind;
    av += optind;

    /* Open control file, count lines, allocate table. */
    if ((F = fopen(ctlfile, "r")) == NULL) {
	(void)fprintf(stderr, "%s: Can't open %s to read it, %s\n",
		argv0, ctlfile, xstrerror());
	exit(1);
    }
    for (i = 0; fgets(buff, sizeof buff, F) != NULL; i++)
	continue;
    if ((Table = (PAIR *)malloc(i * sizeof *Table)) == NULL) {
	(void)fprintf(stderr, "%s: Can't allocate %d table elements, %s\n",
		argv0, i, xstrerror());
	exit(1);
    }

    /* Now parse the table. */
    (void)fseek(F, 0L, 0);
    for (i = 1, tp = Table; fgets(buff, sizeof buff, F) != NULL; i++) {
	if ((p = xstrchr(buff, '\n')) == NULL) {
	    (void)fprintf(stderr, "%s: Line %d of %s is too long\n",
		    argv0, i, ctlfile);
	    exit(1);
	}
	*p = '\0';

	/* Skip empty lines, comment lines, and all-blank lines. */
	if (buff[0] == '\0' || buff[0] == '#')
	    continue;
	for (p = buff; WHITE(*p); p++)
	    continue;
	if (*p == '\0')
	    continue;

	/* Find end of first word, copy second word (or empty string) */
	for (p = buff; *p && !WHITE(*p); p++)
	    continue;
	if (*p == '\0')
	    tp->Value = NIL;
	else {
	    for (*p++ = '\0'; *p && WHITE(*p); p++)
		continue;
	    tp->Value = xstrdup(p);

	    /* Turn things like \& into &. */
	    for (p = dest = tp->Value; *p; p++)
		*dest++ = (*p == '\\' && p[1] != '\0') ? *++p : *p;
	    *dest = '\0';
	}

	/* Turn first word into something directly searchable. */
	if (strlen(buff) > sizeof name - 4) {
	    (void)fprintf(stderr, "%s: Parameter %s is too long\n",
		    argv0, buff);
	    exit(1);
	}
	(void)sprintf(name, "@<%s>@", buff);
	tp->Name = xstrdup(name);
	tp->Length = strlen(tp->Name);
	tp++;
    }
    (void)fclose(F);

    while (*av != NULL)
	Process(*av++, Table, tp);

    exit(0);
    /* NOTREACHED */
}