blob: 0c618a868f03b572056c221dc2c04d92ea6378da [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>
Theodore Ts'odd607052004-04-03 13:53:46 -050014#include <sys/types.h>
15#include <sys/stat.h>
16#include <utime.h>
Theodore Ts'o44339bd1997-10-15 02:47:20 +000017
18#ifdef HAVE_GETOPT_H
19#include <getopt.h>
Theodore Ts'o691d3352000-04-03 16:16:46 +000020#else
21extern char *optarg;
22extern int optind;
Theodore Ts'o44339bd1997-10-15 02:47:20 +000023#endif
24
25
26struct subst_entry {
27 char *name;
28 char *value;
29 struct subst_entry *next;
30};
31
32struct subst_entry *subst_table = 0;
33
34static int add_subst(char *name, char *value)
35{
36 struct subst_entry *ent = 0;
37 int retval;
38
39 retval = ENOMEM;
Theodore Ts'o45d21611998-01-19 14:40:24 +000040 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
Theodore Ts'o44339bd1997-10-15 02:47:20 +000041 if (!ent)
42 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000043 ent->name = (char *) malloc(strlen(name)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000044 if (!ent->name)
45 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000046 ent->value = (char *) malloc(strlen(value)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000047 if (!ent->value)
48 goto fail;
49 strcpy(ent->name, name);
50 strcpy(ent->value, value);
51 ent->next = subst_table;
52 subst_table = ent;
53 return 0;
54fail:
55 if (ent) {
56 if (ent->name)
57 free(ent->name);
58 if (ent->value)
59 free(ent->value);
60 free(ent);
61 }
62 return retval;
63}
64
65static struct subst_entry *fetch_subst_entry(char *name)
66{
67 struct subst_entry *ent;
68
69 for (ent = subst_table; ent; ent = ent->next) {
70 if (strcmp(name, ent->name) == 0)
71 break;
72 }
73 return ent;
74}
75
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000076/*
77 * Given the starting and ending position of the replacement name,
78 * check to see if it is valid, and pull it out if it is.
79 */
Theodore Ts'o54434922003-12-07 01:28:50 -050080static char *get_subst_symbol(const char *begin, size_t len, char prefix)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000081{
82 static char replace_name[128];
83 char *cp, *start;
84
85 start = replace_name;
86 if (prefix)
87 *start++ = prefix;
88
89 if (len > sizeof(replace_name)-2)
90 return NULL;
91 memcpy(start, begin, len);
92 start[len] = 0;
93
94 /*
Theodore Ts'o7822c1d1998-12-19 08:08:43 +000095 * The substitution variable must all be in the of [0-9A-Za-z_].
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000096 * If it isn't, this must be an invalid symbol name.
97 */
98 for (cp = start; *cp; cp++) {
99 if (!(*cp >= 'a' && *cp <= 'z') &&
100 !(*cp >= 'A' && *cp <= 'Z') &&
Theodore Ts'o7822c1d1998-12-19 08:08:43 +0000101 !(*cp >= '0' && *cp <= '9') &&
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000102 !(*cp == '_'))
103 return NULL;
104 }
105 return (replace_name);
106}
107
108static void replace_string(char *begin, char *end, char *newstr)
109{
110 int replace_len, len;
111
112 replace_len = strlen(newstr);
113 len = end - begin;
Theodore Ts'o913c4e92001-04-17 05:05:37 +0000114 if (replace_len == 0)
115 memmove(begin, end+1, strlen(end)+1);
116 else if (replace_len != len+1)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000117 memmove(end+(replace_len-len-1), end,
118 strlen(end)+1);
119 memcpy(begin, newstr, replace_len);
120}
121
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000122static void substitute_line(char *line)
123{
Theodore Ts'ocdceb041999-10-26 16:28:59 +0000124 char *ptr, *name_ptr, *end_ptr;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000125 struct subst_entry *ent;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000126 char *replace_name;
Theodore Ts'o54434922003-12-07 01:28:50 -0500127 size_t len;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000128
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000129 /*
130 * Expand all @FOO@ substitutions
131 */
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000132 ptr = line;
133 while (ptr) {
134 name_ptr = strchr(ptr, '@');
135 if (!name_ptr)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000136 break; /* No more */
137 if (*(++name_ptr) == '@') {
138 /*
139 * Handle tytso@@mit.edu --> tytso@mit.edu
140 */
141 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
142 ptr = name_ptr+1;
143 continue;
144 }
145 end_ptr = strchr(name_ptr, '@');
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000146 if (!end_ptr)
147 break;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000148 len = end_ptr - name_ptr;
149 replace_name = get_subst_symbol(name_ptr, len, 0);
150 if (!replace_name) {
151 ptr = name_ptr;
152 continue;
153 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000154 ent = fetch_subst_entry(replace_name);
155 if (!ent) {
156 fprintf(stderr, "Unfound expansion: '%s'\n",
157 replace_name);
158 ptr = end_ptr + 1;
159 continue;
160 }
161#if 0
162 fprintf(stderr, "Replace name = '%s' with '%s'\n",
163 replace_name, ent->value);
164#endif
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000165 ptr = name_ptr-1;
166 replace_string(ptr, end_ptr, ent->value);
167 }
168 /*
169 * Now do a second pass to expand ${FOO}
170 */
171 ptr = line;
172 while (ptr) {
173 name_ptr = strchr(ptr, '$');
174 if (!name_ptr)
175 break; /* No more */
176 if (*(++name_ptr) != '{') {
177 ptr = name_ptr;
178 continue;
179 }
180 name_ptr++;
181 end_ptr = strchr(name_ptr, '}');
182 if (!end_ptr)
183 break;
184 len = end_ptr - name_ptr;
185 replace_name = get_subst_symbol(name_ptr, len, '$');
186 if (!replace_name) {
187 ptr = name_ptr;
188 continue;
189 }
190 ent = fetch_subst_entry(replace_name);
191 if (!ent) {
192 ptr = end_ptr + 1;
193 continue;
194 }
195#if 0
196 fprintf(stderr, "Replace name = '%s' with '%s'\n",
197 replace_name, ent->value);
198#endif
199 ptr = name_ptr-2;
200 replace_string(ptr, end_ptr, ent->value);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000201 }
202}
203
204static void parse_config_file(FILE *f)
205{
206 char line[2048];
207 char *cp, *ptr;
208
209 while (!feof(f)) {
210 memset(line, 0, sizeof(line));
211 if (fgets(line, sizeof(line), f) == NULL)
212 break;
213 /*
214 * Strip newlines and comments.
215 */
216 cp = strchr(line, '\n');
217 if (cp)
218 *cp = 0;
219 cp = strchr(line, '#');
220 if (cp)
221 *cp = 0;
222 /*
223 * Skip trailing and leading whitespace
224 */
225 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
226 if (*cp == ' ' || *cp == '\t')
227 *cp = 0;
228 else
229 break;
230 }
231 cp = line;
232 while (*cp && isspace(*cp))
233 cp++;
234 ptr = cp;
235 /*
236 * Skip empty lines
237 */
238 if (*ptr == 0)
239 continue;
240 /*
241 * Ignore future extensions
242 */
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000243 if (*ptr == '@')
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000244 continue;
245 /*
246 * Parse substitutions
247 */
248 for (cp = ptr; *cp; cp++)
249 if (isspace(*cp))
250 break;
251 *cp = 0;
252 for (cp++; *cp; cp++)
253 if (!isspace(*cp))
254 break;
255#if 0
Theodore Ts'o913c4e92001-04-17 05:05:37 +0000256 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000257#endif
258 add_subst(ptr, cp);
259 }
260}
261
262/*
263 * Return 0 if the files are different, 1 if the files are the same.
264 */
265static int compare_file(const char *outfn, const char *newfn)
266{
Theodore Ts'o45d21611998-01-19 14:40:24 +0000267 FILE *old_f, *new_f;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000268 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
269 int retval;
270
Theodore Ts'o45d21611998-01-19 14:40:24 +0000271 old_f = fopen(outfn, "r");
272 if (!old_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000273 return 0;
Theodore Ts'o45d21611998-01-19 14:40:24 +0000274 new_f = fopen(newfn, "r");
275 if (!new_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000276 return 0;
277
278 while (1) {
Theodore Ts'o45d21611998-01-19 14:40:24 +0000279 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
280 newcp = fgets(newbuf, sizeof(newbuf), new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000281 if (!oldcp && !newcp) {
282 retval = 1;
283 break;
284 }
285 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
286 retval = 0;
287 break;
288 }
289 }
Theodore Ts'o45d21611998-01-19 14:40:24 +0000290 fclose(old_f);
291 fclose(new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000292 return retval;
293}
294
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000295
296
297int main(int argc, char **argv)
298{
299 char line[2048];
300 int c;
301 FILE *in, *out;
302 char *outfn = NULL, *newfn = NULL;
303 int verbose = 0;
Theodore Ts'odd607052004-04-03 13:53:46 -0500304 int adjust_timestamp = 0;
305 struct stat stbuf;
306 struct utimbuf ut;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000307
Theodore Ts'odd607052004-04-03 13:53:46 -0500308 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000309 switch (c) {
310 case 'f':
311 in = fopen(optarg, "r");
312 if (!in) {
313 perror(optarg);
314 exit(1);
315 }
316 parse_config_file(in);
317 fclose(in);
318 break;
Theodore Ts'odd607052004-04-03 13:53:46 -0500319 case 't':
320 adjust_timestamp++;
321 break;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000322 case 'v':
323 verbose++;
324 break;
325 default:
326 fprintf(stderr, "%s: [-f config-file] [file]\n",
327 argv[0]);
328 break;
329 }
330 }
331 if (optind < argc) {
332 in = fopen(argv[optind], "r");
333 if (!in) {
334 perror(argv[optind]);
335 exit(1);
336 }
337 optind++;
338 } else
339 in = stdin;
340
341 if (optind < argc) {
342 outfn = argv[optind];
Theodore Ts'o45d21611998-01-19 14:40:24 +0000343 newfn = (char *) malloc(strlen(outfn)+20);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000344 if (!newfn) {
345 fprintf(stderr, "Memory error! Exiting.\n");
346 exit(1);
347 }
348 strcpy(newfn, outfn);
349 strcat(newfn, ".new");
350 out = fopen(newfn, "w");
351 if (!out) {
352 perror(newfn);
353 exit(1);
354 }
355 } else {
356 out = stdout;
357 outfn = 0;
358 }
359
360 while (!feof(in)) {
361 if (fgets(line, sizeof(line), in) == NULL)
362 break;
363 substitute_line(line);
364 fputs(line, out);
365 }
366 fclose(in);
367 fclose(out);
368 if (outfn) {
369 if (compare_file(outfn, newfn)) {
370 if (verbose)
371 printf("No change, keeping %s.\n", outfn);
Theodore Ts'odd607052004-04-03 13:53:46 -0500372 if (adjust_timestamp) {
373 if (stat(outfn, &stbuf) == 0) {
374 if (verbose)
375 printf("Updating modtime for %s\n", outfn);
376 ut.actime = stbuf.st_atime;
377 ut.modtime = time(0);
378 if (utime(outfn, &ut) < 0)
379 perror("utime");
380 }
381 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000382 unlink(newfn);
383 } else {
384 if (verbose)
385 printf("Creating or replacing %s.\n", outfn);
386 rename(newfn, outfn);
387 }
388 }
389 return (0);
390}
391
392