blob: c8a2b23a4c2f867269d9ec95f3ac00798e0e3dcb [file] [log] [blame]
Theodore Ts'o44339bd1997-10-15 02:47:20 +00001/*
2 * subst.c --- substitution program
3 *
4 * Subst is used as a quicky program to do @ substitutions
5 *
6 */
7
8#include <stdio.h>
9#include <errno.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <string.h>
13#include <ctype.h>
14
15#ifdef HAVE_GETOPT_H
16#include <getopt.h>
17#endif
18
19
20struct subst_entry {
21 char *name;
22 char *value;
23 struct subst_entry *next;
24};
25
26struct subst_entry *subst_table = 0;
27
28static int add_subst(char *name, char *value)
29{
30 struct subst_entry *ent = 0;
31 int retval;
32
33 retval = ENOMEM;
Theodore Ts'o45d21611998-01-19 14:40:24 +000034 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
Theodore Ts'o44339bd1997-10-15 02:47:20 +000035 if (!ent)
36 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000037 ent->name = (char *) malloc(strlen(name)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000038 if (!ent->name)
39 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000040 ent->value = (char *) malloc(strlen(value)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000041 if (!ent->value)
42 goto fail;
43 strcpy(ent->name, name);
44 strcpy(ent->value, value);
45 ent->next = subst_table;
46 subst_table = ent;
47 return 0;
48fail:
49 if (ent) {
50 if (ent->name)
51 free(ent->name);
52 if (ent->value)
53 free(ent->value);
54 free(ent);
55 }
56 return retval;
57}
58
59static struct subst_entry *fetch_subst_entry(char *name)
60{
61 struct subst_entry *ent;
62
63 for (ent = subst_table; ent; ent = ent->next) {
64 if (strcmp(name, ent->name) == 0)
65 break;
66 }
67 return ent;
68}
69
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000070/*
71 * Given the starting and ending position of the replacement name,
72 * check to see if it is valid, and pull it out if it is.
73 */
74static char *get_subst_symbol(const char *begin, int len, char prefix)
75{
76 static char replace_name[128];
77 char *cp, *start;
78
79 start = replace_name;
80 if (prefix)
81 *start++ = prefix;
82
83 if (len > sizeof(replace_name)-2)
84 return NULL;
85 memcpy(start, begin, len);
86 start[len] = 0;
87
88 /*
Theodore Ts'o7822c1d1998-12-19 08:08:43 +000089 * The substitution variable must all be in the of [0-9A-Za-z_].
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000090 * If it isn't, this must be an invalid symbol name.
91 */
92 for (cp = start; *cp; cp++) {
93 if (!(*cp >= 'a' && *cp <= 'z') &&
94 !(*cp >= 'A' && *cp <= 'Z') &&
Theodore Ts'o7822c1d1998-12-19 08:08:43 +000095 !(*cp >= '0' && *cp <= '9') &&
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000096 !(*cp == '_'))
97 return NULL;
98 }
99 return (replace_name);
100}
101
102static void replace_string(char *begin, char *end, char *newstr)
103{
104 int replace_len, len;
105
106 replace_len = strlen(newstr);
107 len = end - begin;
108 if (replace_len != len+1)
109 memmove(end+(replace_len-len-1), end,
110 strlen(end)+1);
111 memcpy(begin, newstr, replace_len);
112}
113
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000114static void substitute_line(char *line)
115{
Theodore Ts'ocdceb041999-10-26 16:28:59 +0000116 char *ptr, *name_ptr, *end_ptr;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000117 struct subst_entry *ent;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000118 char *replace_name;
Theodore Ts'ocdceb041999-10-26 16:28:59 +0000119 int len;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000120
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000121 /*
122 * Expand all @FOO@ substitutions
123 */
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000124 ptr = line;
125 while (ptr) {
126 name_ptr = strchr(ptr, '@');
127 if (!name_ptr)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000128 break; /* No more */
129 if (*(++name_ptr) == '@') {
130 /*
131 * Handle tytso@@mit.edu --> tytso@mit.edu
132 */
133 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
134 ptr = name_ptr+1;
135 continue;
136 }
137 end_ptr = strchr(name_ptr, '@');
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000138 if (!end_ptr)
139 break;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000140 len = end_ptr - name_ptr;
141 replace_name = get_subst_symbol(name_ptr, len, 0);
142 if (!replace_name) {
143 ptr = name_ptr;
144 continue;
145 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000146 ent = fetch_subst_entry(replace_name);
147 if (!ent) {
148 fprintf(stderr, "Unfound expansion: '%s'\n",
149 replace_name);
150 ptr = end_ptr + 1;
151 continue;
152 }
153#if 0
154 fprintf(stderr, "Replace name = '%s' with '%s'\n",
155 replace_name, ent->value);
156#endif
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000157 ptr = name_ptr-1;
158 replace_string(ptr, end_ptr, ent->value);
159 }
160 /*
161 * Now do a second pass to expand ${FOO}
162 */
163 ptr = line;
164 while (ptr) {
165 name_ptr = strchr(ptr, '$');
166 if (!name_ptr)
167 break; /* No more */
168 if (*(++name_ptr) != '{') {
169 ptr = name_ptr;
170 continue;
171 }
172 name_ptr++;
173 end_ptr = strchr(name_ptr, '}');
174 if (!end_ptr)
175 break;
176 len = end_ptr - name_ptr;
177 replace_name = get_subst_symbol(name_ptr, len, '$');
178 if (!replace_name) {
179 ptr = name_ptr;
180 continue;
181 }
182 ent = fetch_subst_entry(replace_name);
183 if (!ent) {
184 ptr = end_ptr + 1;
185 continue;
186 }
187#if 0
188 fprintf(stderr, "Replace name = '%s' with '%s'\n",
189 replace_name, ent->value);
190#endif
191 ptr = name_ptr-2;
192 replace_string(ptr, end_ptr, ent->value);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000193 }
194}
195
196static void parse_config_file(FILE *f)
197{
198 char line[2048];
199 char *cp, *ptr;
200
201 while (!feof(f)) {
202 memset(line, 0, sizeof(line));
203 if (fgets(line, sizeof(line), f) == NULL)
204 break;
205 /*
206 * Strip newlines and comments.
207 */
208 cp = strchr(line, '\n');
209 if (cp)
210 *cp = 0;
211 cp = strchr(line, '#');
212 if (cp)
213 *cp = 0;
214 /*
215 * Skip trailing and leading whitespace
216 */
217 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
218 if (*cp == ' ' || *cp == '\t')
219 *cp = 0;
220 else
221 break;
222 }
223 cp = line;
224 while (*cp && isspace(*cp))
225 cp++;
226 ptr = cp;
227 /*
228 * Skip empty lines
229 */
230 if (*ptr == 0)
231 continue;
232 /*
233 * Ignore future extensions
234 */
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000235 if (*ptr == '@')
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000236 continue;
237 /*
238 * Parse substitutions
239 */
240 for (cp = ptr; *cp; cp++)
241 if (isspace(*cp))
242 break;
243 *cp = 0;
244 for (cp++; *cp; cp++)
245 if (!isspace(*cp))
246 break;
247#if 0
248 printf("Substitute: '%s' for '%s'\n", ptr, cp);
249#endif
250 add_subst(ptr, cp);
251 }
252}
253
254/*
255 * Return 0 if the files are different, 1 if the files are the same.
256 */
257static int compare_file(const char *outfn, const char *newfn)
258{
Theodore Ts'o45d21611998-01-19 14:40:24 +0000259 FILE *old_f, *new_f;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000260 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
261 int retval;
262
Theodore Ts'o45d21611998-01-19 14:40:24 +0000263 old_f = fopen(outfn, "r");
264 if (!old_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000265 return 0;
Theodore Ts'o45d21611998-01-19 14:40:24 +0000266 new_f = fopen(newfn, "r");
267 if (!new_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000268 return 0;
269
270 while (1) {
Theodore Ts'o45d21611998-01-19 14:40:24 +0000271 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
272 newcp = fgets(newbuf, sizeof(newbuf), new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000273 if (!oldcp && !newcp) {
274 retval = 1;
275 break;
276 }
277 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
278 retval = 0;
279 break;
280 }
281 }
Theodore Ts'o45d21611998-01-19 14:40:24 +0000282 fclose(old_f);
283 fclose(new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000284 return retval;
285}
286
287
288
289
290int main(int argc, char **argv)
291{
292 char line[2048];
293 int c;
294 FILE *in, *out;
295 char *outfn = NULL, *newfn = NULL;
296 int verbose = 0;
297
298 while ((c = getopt (argc, argv, "f:v")) != EOF) {
299 switch (c) {
300 case 'f':
301 in = fopen(optarg, "r");
302 if (!in) {
303 perror(optarg);
304 exit(1);
305 }
306 parse_config_file(in);
307 fclose(in);
308 break;
309 case 'v':
310 verbose++;
311 break;
312 default:
313 fprintf(stderr, "%s: [-f config-file] [file]\n",
314 argv[0]);
315 break;
316 }
317 }
318 if (optind < argc) {
319 in = fopen(argv[optind], "r");
320 if (!in) {
321 perror(argv[optind]);
322 exit(1);
323 }
324 optind++;
325 } else
326 in = stdin;
327
328 if (optind < argc) {
329 outfn = argv[optind];
Theodore Ts'o45d21611998-01-19 14:40:24 +0000330 newfn = (char *) malloc(strlen(outfn)+20);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000331 if (!newfn) {
332 fprintf(stderr, "Memory error! Exiting.\n");
333 exit(1);
334 }
335 strcpy(newfn, outfn);
336 strcat(newfn, ".new");
337 out = fopen(newfn, "w");
338 if (!out) {
339 perror(newfn);
340 exit(1);
341 }
342 } else {
343 out = stdout;
344 outfn = 0;
345 }
346
347 while (!feof(in)) {
348 if (fgets(line, sizeof(line), in) == NULL)
349 break;
350 substitute_line(line);
351 fputs(line, out);
352 }
353 fclose(in);
354 fclose(out);
355 if (outfn) {
356 if (compare_file(outfn, newfn)) {
357 if (verbose)
358 printf("No change, keeping %s.\n", outfn);
359 unlink(newfn);
360 } else {
361 if (verbose)
362 printf("Creating or replacing %s.\n", outfn);
363 rename(newfn, outfn);
364 }
365 }
366 return (0);
367}
368
369