-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtemplate.c
89 lines (79 loc) · 1.43 KB
/
template.c
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
/*
* Do template substitutions on an input file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
void badmarker(void)
{
fputs("Missing % marker\n", stderr);
exit(1);
}
void badsize(void)
{
fputs("Substitution out of range\n", stderr);
exit(1);
}
void badeof(void)
{
fputs("Unexpected EOF\n", stderr);
exit(1);
}
void badquote(char c)
{
fputs("Unexpected quoting '", stderr);
fputc(c, stderr);
fputs("'\n", stderr);
exit(1);
}
void process_sub(int argc, char *argv[])
{
int n;
char *p;
int c;
c = getchar();
if (c == EOF)
badeof();
while(isdigit(c)) {
n *= 10;
n += (c - '0');
c = getchar();
}
if (c != '%')
badmarker();
if (n < 1 || n >= argc)
badsize();
p = argv[n];
while(*p) {
if (*p != '\\')
putchar(*p);
else switch(*++p) {
case '\\':
putchar(*p);
break;
case 'n':
putchar('\n');
break;
case 'r':
putchar('\r');
break;
case 't':
putchar('\t');
break;
default:
badquote(*p);
}
p++;
}
}
int main(int argc, char *argv[])
{
int c;
while((c = getchar()) != EOF) {
if (c == '%')
process_sub(argc, argv);
else
putchar(c);
}
}