blob: dff1848bc70535dee780891a5d6b599b48f06068 [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>
Theodore Ts'o691d3352000-04-03 16:16:46 +000017#else
18extern char *optarg;
19extern int optind;
Theodore Ts'o44339bd1997-10-15 02:47:20 +000020#endif
21
22
23struct subst_entry {
24 char *name;
25 char *value;
26 struct subst_entry *next;
27};
28
29struct subst_entry *subst_table = 0;
30
31static int add_subst(char *name, char *value)
32{
33 struct subst_entry *ent = 0;
34 int retval;
35
36 retval = ENOMEM;
Theodore Ts'o45d21611998-01-19 14:40:24 +000037 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
Theodore Ts'o44339bd1997-10-15 02:47:20 +000038 if (!ent)
39 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000040 ent->name = (char *) malloc(strlen(name)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000041 if (!ent->name)
42 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000043 ent->value = (char *) malloc(strlen(value)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000044 if (!ent->value)
45 goto fail;
46 strcpy(ent->name, name);
47 strcpy(ent->value, value);
48 ent->next = subst_table;
49 subst_table = ent;
50 return 0;
51fail:
52 if (ent) {
53 if (ent->name)
54 free(ent->name);
55 if (ent->value)
56 free(ent->value);
57 free(ent);
58 }
59 return retval;
60}
61
62static struct subst_entry *fetch_subst_entry(char *name)
63{
64 struct subst_entry *ent;
65
66 for (ent = subst_table; ent; ent = ent->next) {
67 if (strcmp(name, ent->name) == 0)
68 break;
69 }
70 return ent;
71}
72
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000073/*
74 * Given the starting and ending position of the replacement name,
75 * check to see if it is valid, and pull it out if it is.
76 */
Theodore Ts'o54434922003-12-07 01:28:50 -050077static char *get_subst_symbol(const char *begin, size_t len, char prefix)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000078{
79 static char replace_name[128];
80 char *cp, *start;
81
82 start = replace_name;
83 if (prefix)
84 *start++ = prefix;
85
86 if (len > sizeof(replace_name)-2)
87 return NULL;
88 memcpy(start, begin, len);
89 start[len] = 0;
90
91 /*
Theodore Ts'o7822c1d1998-12-19 08:08:43 +000092 * The substitution variable must all be in the of [0-9A-Za-z_].
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000093 * If it isn't, this must be an invalid symbol name.
94 */
95 for (cp = start; *cp; cp++) {
96 if (!(*cp >= 'a' && *cp <= 'z') &&
97 !(*cp >= 'A' && *cp <= 'Z') &&
Theodore Ts'o7822c1d1998-12-19 08:08:43 +000098 !(*cp >= '0' && *cp <= '9') &&
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000099 !(*cp == '_'))
100 return NULL;
101 }
102 return (replace_name);
103}
104
105static void replace_string(char *begin, char *end, char *newstr)
106{
107 int replace_len, len;
108
109 replace_len = strlen(newstr);
110 len = end - begin;
Theodore Ts'o913c4e92001-04-17 05:05:37 +0000111 if (replace_len == 0)
112 memmove(begin, end+1, strlen(end)+1);
113 else if (replace_len != len+1)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000114 memmove(end+(replace_len-len-1), end,
115 strlen(end)+1);
116 memcpy(begin, newstr, replace_len);
117}
118
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000119static void substitute_line(char *line)
120{
Theodore Ts'ocdceb041999-10-26 16:28:59 +0000121 char *ptr, *name_ptr, *end_ptr;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000122 struct subst_entry *ent;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000123 char *replace_name;
Theodore Ts'o54434922003-12-07 01:28:50 -0500124 size_t len;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000125
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000126 /*
127 * Expand all @FOO@ substitutions
128 */
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000129 ptr = line;
130 while (ptr) {
131 name_ptr = strchr(ptr, '@');
132 if (!name_ptr)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000133 break; /* No more */
134 if (*(++name_ptr) == '@') {
135 /*
136 * Handle tytso@@mit.edu --> tytso@mit.edu
137 */
138 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
139 ptr = name_ptr+1;
140 continue;
141 }
142 end_ptr = strchr(name_ptr, '@');
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000143 if (!end_ptr)
144 break;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000145 len = end_ptr - name_ptr;
146 replace_name = get_subst_symbol(name_ptr, len, 0);
147 if (!replace_name) {
148 ptr = name_ptr;
149 continue;
150 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000151 ent = fetch_subst_entry(replace_name);
152 if (!ent) {
153 fprintf(stderr, "Unfound expansion: '%s'\n",
154 replace_name);
155 ptr = end_ptr + 1;
156 continue;
157 }
158#if 0
159 fprintf(stderr, "Replace name = '%s' with '%s'\n",
160 replace_name, ent->value);
161#endif
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000162 ptr = name_ptr-1;
163 replace_string(ptr, end_ptr, ent->value);
164 }
165 /*
166 * Now do a second pass to expand ${FOO}
167 */
168 ptr = line;
169 while (ptr) {
170 name_ptr = strchr(ptr, '$');
171 if (!name_ptr)
172 break; /* No more */
173 if (*(++name_ptr) != '{') {
174 ptr = name_ptr;
175 continue;
176 }
177 name_ptr++;
178 end_ptr = strchr(name_ptr, '}');
179 if (!end_ptr)
180 break;
181 len = end_ptr - name_ptr;
182 replace_name = get_subst_symbol(name_ptr, len, '$');
183 if (!replace_name) {
184 ptr = name_ptr;
185 continue;
186 }
187 ent = fetch_subst_entry(replace_name);
188 if (!ent) {
189 ptr = end_ptr + 1;
190 continue;
191 }
192#if 0
193 fprintf(stderr, "Replace name = '%s' with '%s'\n",
194 replace_name, ent->value);
195#endif
196 ptr = name_ptr-2;
197 replace_string(ptr, end_ptr, ent->value);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000198 }
199}
200
201static void parse_config_file(FILE *f)
202{
203 char line[2048];
204 char *cp, *ptr;
205
206 while (!feof(f)) {
207 memset(line, 0, sizeof(line));
208 if (fgets(line, sizeof(line), f) == NULL)
209 break;
210 /*
211 * Strip newlines and comments.
212 */
213 cp = strchr(line, '\n');
214 if (cp)
215 *cp = 0;
216 cp = strchr(line, '#');
217 if (cp)
218 *cp = 0;
219 /*
220 * Skip trailing and leading whitespace
221 */
222 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
223 if (*cp == ' ' || *cp == '\t')
224 *cp = 0;
225 else
226 break;
227 }
228 cp = line;
229 while (*cp && isspace(*cp))
230 cp++;
231 ptr = cp;
232 /*
233 * Skip empty lines
234 */
235 if (*ptr == 0)
236 continue;
237 /*
238 * Ignore future extensions
239 */
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000240 if (*ptr == '@')
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000241 continue;
242 /*
243 * Parse substitutions
244 */
245 for (cp = ptr; *cp; cp++)
246 if (isspace(*cp))
247 break;
248 *cp = 0;
249 for (cp++; *cp; cp++)
250 if (!isspace(*cp))
251 break;
252#if 0
Theodore Ts'o913c4e92001-04-17 05:05:37 +0000253 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000254#endif
255 add_subst(ptr, cp);
256 }
257}
258
259/*
260 * Return 0 if the files are different, 1 if the files are the same.
261 */
262static int compare_file(const char *outfn, const char *newfn)
263{
Theodore Ts'o45d21611998-01-19 14:40:24 +0000264 FILE *old_f, *new_f;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000265 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
266 int retval;
267
Theodore Ts'o45d21611998-01-19 14:40:24 +0000268 old_f = fopen(outfn, "r");
269 if (!old_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000270 return 0;
Theodore Ts'o45d21611998-01-19 14:40:24 +0000271 new_f = fopen(newfn, "r");
272 if (!new_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000273 return 0;
274
275 while (1) {
Theodore Ts'o45d21611998-01-19 14:40:24 +0000276 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
277 newcp = fgets(newbuf, sizeof(newbuf), new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000278 if (!oldcp && !newcp) {
279 retval = 1;
280 break;
281 }
282 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
283 retval = 0;
284 break;
285 }
286 }
Theodore Ts'o45d21611998-01-19 14:40:24 +0000287 fclose(old_f);
288 fclose(new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000289 return retval;
290}
291
292
293
294
295int main(int argc, char **argv)
296{
297 char line[2048];
298 int c;
299 FILE *in, *out;
300 char *outfn = NULL, *newfn = NULL;
301 int verbose = 0;
302
303 while ((c = getopt (argc, argv, "f:v")) != EOF) {
304 switch (c) {
305 case 'f':
306 in = fopen(optarg, "r");
307 if (!in) {
308 perror(optarg);
309 exit(1);
310 }
311 parse_config_file(in);
312 fclose(in);
313 break;
314 case 'v':
315 verbose++;
316 break;
317 default:
318 fprintf(stderr, "%s: [-f config-file] [file]\n",
319 argv[0]);
320 break;
321 }
322 }
323 if (optind < argc) {
324 in = fopen(argv[optind], "r");
325 if (!in) {
326 perror(argv[optind]);
327 exit(1);
328 }
329 optind++;
330 } else
331 in = stdin;
332
333 if (optind < argc) {
334 outfn = argv[optind];
Theodore Ts'o45d21611998-01-19 14:40:24 +0000335 newfn = (char *) malloc(strlen(outfn)+20);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000336 if (!newfn) {
337 fprintf(stderr, "Memory error! Exiting.\n");
338 exit(1);
339 }
340 strcpy(newfn, outfn);
341 strcat(newfn, ".new");
342 out = fopen(newfn, "w");
343 if (!out) {
344 perror(newfn);
345 exit(1);
346 }
347 } else {
348 out = stdout;
349 outfn = 0;
350 }
351
352 while (!feof(in)) {
353 if (fgets(line, sizeof(line), in) == NULL)
354 break;
355 substitute_line(line);
356 fputs(line, out);
357 }
358 fclose(in);
359 fclose(out);
360 if (outfn) {
361 if (compare_file(outfn, newfn)) {
362 if (verbose)
363 printf("No change, keeping %s.\n", outfn);
364 unlink(newfn);
365 } else {
366 if (verbose)
367 printf("Creating or replacing %s.\n", outfn);
368 rename(newfn, outfn);
369 }
370 }
371 return (0);
372}
373
374