blob: 0015a9c45012852dd90fcfefe3e140614c9f4bda [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
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'o44339bd1997-10-15 02:47:20 +00006 */
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>
Matthias Andreef932ed92004-04-12 20:16:04 +020016#include <time.h>
Theodore Ts'odd607052004-04-03 13:53:46 -050017#include <utime.h>
Theodore Ts'o44339bd1997-10-15 02:47:20 +000018
19#ifdef HAVE_GETOPT_H
20#include <getopt.h>
Theodore Ts'o691d3352000-04-03 16:16:46 +000021#else
22extern char *optarg;
23extern int optind;
Theodore Ts'o44339bd1997-10-15 02:47:20 +000024#endif
25
26
27struct subst_entry {
28 char *name;
29 char *value;
30 struct subst_entry *next;
31};
32
33struct subst_entry *subst_table = 0;
34
35static int add_subst(char *name, char *value)
36{
37 struct subst_entry *ent = 0;
38 int retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040039
Theodore Ts'o44339bd1997-10-15 02:47:20 +000040 retval = ENOMEM;
Theodore Ts'o45d21611998-01-19 14:40:24 +000041 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
Theodore Ts'o44339bd1997-10-15 02:47:20 +000042 if (!ent)
43 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000044 ent->name = (char *) malloc(strlen(name)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000045 if (!ent->name)
46 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000047 ent->value = (char *) malloc(strlen(value)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000048 if (!ent->value)
49 goto fail;
50 strcpy(ent->name, name);
51 strcpy(ent->value, value);
52 ent->next = subst_table;
53 subst_table = ent;
54 return 0;
55fail:
56 if (ent) {
57 if (ent->name)
58 free(ent->name);
59 if (ent->value)
60 free(ent->value);
61 free(ent);
62 }
63 return retval;
64}
65
66static struct subst_entry *fetch_subst_entry(char *name)
67{
68 struct subst_entry *ent;
69
70 for (ent = subst_table; ent; ent = ent->next) {
71 if (strcmp(name, ent->name) == 0)
72 break;
73 }
74 return ent;
75}
76
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000077/*
78 * Given the starting and ending position of the replacement name,
79 * check to see if it is valid, and pull it out if it is.
80 */
Theodore Ts'o54434922003-12-07 01:28:50 -050081static char *get_subst_symbol(const char *begin, size_t len, char prefix)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000082{
83 static char replace_name[128];
84 char *cp, *start;
85
86 start = replace_name;
87 if (prefix)
88 *start++ = prefix;
89
90 if (len > sizeof(replace_name)-2)
91 return NULL;
92 memcpy(start, begin, len);
93 start[len] = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040094
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000095 /*
Theodore Ts'o7822c1d1998-12-19 08:08:43 +000096 * The substitution variable must all be in the of [0-9A-Za-z_].
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000097 * If it isn't, this must be an invalid symbol name.
98 */
99 for (cp = start; *cp; cp++) {
100 if (!(*cp >= 'a' && *cp <= 'z') &&
101 !(*cp >= 'A' && *cp <= 'Z') &&
Theodore Ts'o7822c1d1998-12-19 08:08:43 +0000102 !(*cp >= '0' && *cp <= '9') &&
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000103 !(*cp == '_'))
104 return NULL;
105 }
106 return (replace_name);
107}
108
109static void replace_string(char *begin, char *end, char *newstr)
110{
111 int replace_len, len;
112
113 replace_len = strlen(newstr);
114 len = end - begin;
Theodore Ts'o913c4e92001-04-17 05:05:37 +0000115 if (replace_len == 0)
116 memmove(begin, end+1, strlen(end)+1);
117 else if (replace_len != len+1)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000118 memmove(end+(replace_len-len-1), end,
119 strlen(end)+1);
120 memcpy(begin, newstr, replace_len);
121}
122
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000123static void substitute_line(char *line)
124{
Theodore Ts'ocdceb041999-10-26 16:28:59 +0000125 char *ptr, *name_ptr, *end_ptr;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000126 struct subst_entry *ent;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000127 char *replace_name;
Theodore Ts'o54434922003-12-07 01:28:50 -0500128 size_t len;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000129
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000130 /*
131 * Expand all @FOO@ substitutions
132 */
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000133 ptr = line;
134 while (ptr) {
135 name_ptr = strchr(ptr, '@');
136 if (!name_ptr)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000137 break; /* No more */
138 if (*(++name_ptr) == '@') {
139 /*
140 * Handle tytso@@mit.edu --> tytso@mit.edu
141 */
142 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
143 ptr = name_ptr+1;
144 continue;
145 }
146 end_ptr = strchr(name_ptr, '@');
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000147 if (!end_ptr)
148 break;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000149 len = end_ptr - name_ptr;
150 replace_name = get_subst_symbol(name_ptr, len, 0);
151 if (!replace_name) {
152 ptr = name_ptr;
153 continue;
154 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000155 ent = fetch_subst_entry(replace_name);
156 if (!ent) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400157 fprintf(stderr, "Unfound expansion: '%s'\n",
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000158 replace_name);
159 ptr = end_ptr + 1;
160 continue;
161 }
162#if 0
163 fprintf(stderr, "Replace name = '%s' with '%s'\n",
164 replace_name, ent->value);
165#endif
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000166 ptr = name_ptr-1;
167 replace_string(ptr, end_ptr, ent->value);
Theodore Ts'o98224fb2006-11-12 17:43:50 -0500168 if ((ent->value[0] == '@') &&
169 (strlen(replace_name) == strlen(ent->value)-2) &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400170 !strncmp(replace_name, ent->value+1,
Theodore Ts'o98224fb2006-11-12 17:43:50 -0500171 strlen(ent->value)-2))
172 /* avoid an infinite loop */
173 ptr += strlen(ent->value);
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000174 }
175 /*
176 * Now do a second pass to expand ${FOO}
177 */
178 ptr = line;
179 while (ptr) {
180 name_ptr = strchr(ptr, '$');
181 if (!name_ptr)
182 break; /* No more */
183 if (*(++name_ptr) != '{') {
184 ptr = name_ptr;
185 continue;
186 }
187 name_ptr++;
188 end_ptr = strchr(name_ptr, '}');
189 if (!end_ptr)
190 break;
191 len = end_ptr - name_ptr;
192 replace_name = get_subst_symbol(name_ptr, len, '$');
193 if (!replace_name) {
194 ptr = name_ptr;
195 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400196 }
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000197 ent = fetch_subst_entry(replace_name);
198 if (!ent) {
199 ptr = end_ptr + 1;
200 continue;
201 }
202#if 0
203 fprintf(stderr, "Replace name = '%s' with '%s'\n",
204 replace_name, ent->value);
205#endif
206 ptr = name_ptr-2;
207 replace_string(ptr, end_ptr, ent->value);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000208 }
209}
210
211static void parse_config_file(FILE *f)
212{
213 char line[2048];
214 char *cp, *ptr;
215
216 while (!feof(f)) {
217 memset(line, 0, sizeof(line));
218 if (fgets(line, sizeof(line), f) == NULL)
219 break;
220 /*
221 * Strip newlines and comments.
222 */
223 cp = strchr(line, '\n');
224 if (cp)
225 *cp = 0;
226 cp = strchr(line, '#');
227 if (cp)
228 *cp = 0;
229 /*
230 * Skip trailing and leading whitespace
231 */
232 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
233 if (*cp == ' ' || *cp == '\t')
234 *cp = 0;
235 else
236 break;
237 }
238 cp = line;
239 while (*cp && isspace(*cp))
240 cp++;
241 ptr = cp;
242 /*
243 * Skip empty lines
244 */
245 if (*ptr == 0)
246 continue;
247 /*
248 * Ignore future extensions
249 */
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000250 if (*ptr == '@')
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000251 continue;
252 /*
253 * Parse substitutions
254 */
255 for (cp = ptr; *cp; cp++)
256 if (isspace(*cp))
257 break;
258 *cp = 0;
259 for (cp++; *cp; cp++)
260 if (!isspace(*cp))
261 break;
262#if 0
Theodore Ts'o913c4e92001-04-17 05:05:37 +0000263 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000264#endif
265 add_subst(ptr, cp);
266 }
267}
268
269/*
270 * Return 0 if the files are different, 1 if the files are the same.
271 */
272static int compare_file(const char *outfn, const char *newfn)
273{
Theodore Ts'o45d21611998-01-19 14:40:24 +0000274 FILE *old_f, *new_f;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000275 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
276 int retval;
277
Theodore Ts'o45d21611998-01-19 14:40:24 +0000278 old_f = fopen(outfn, "r");
279 if (!old_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000280 return 0;
Theodore Ts'o45d21611998-01-19 14:40:24 +0000281 new_f = fopen(newfn, "r");
Brian Behlendorff1d6a072007-03-21 17:19:55 -0400282 if (!new_f) {
283 fclose(old_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000284 return 0;
Brian Behlendorff1d6a072007-03-21 17:19:55 -0400285 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000286
287 while (1) {
Theodore Ts'o45d21611998-01-19 14:40:24 +0000288 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
289 newcp = fgets(newbuf, sizeof(newbuf), new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000290 if (!oldcp && !newcp) {
291 retval = 1;
292 break;
293 }
294 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
295 retval = 0;
296 break;
297 }
298 }
Theodore Ts'o45d21611998-01-19 14:40:24 +0000299 fclose(old_f);
300 fclose(new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000301 return retval;
302}
303
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000304
305
306int main(int argc, char **argv)
307{
308 char line[2048];
309 int c;
310 FILE *in, *out;
311 char *outfn = NULL, *newfn = NULL;
312 int verbose = 0;
Theodore Ts'odd607052004-04-03 13:53:46 -0500313 int adjust_timestamp = 0;
314 struct stat stbuf;
315 struct utimbuf ut;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400316
Theodore Ts'odd607052004-04-03 13:53:46 -0500317 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000318 switch (c) {
319 case 'f':
320 in = fopen(optarg, "r");
321 if (!in) {
322 perror(optarg);
323 exit(1);
324 }
325 parse_config_file(in);
326 fclose(in);
327 break;
Theodore Ts'odd607052004-04-03 13:53:46 -0500328 case 't':
329 adjust_timestamp++;
330 break;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000331 case 'v':
332 verbose++;
333 break;
334 default:
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400335 fprintf(stderr, "%s: [-f config-file] [file]\n",
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000336 argv[0]);
337 break;
338 }
339 }
340 if (optind < argc) {
341 in = fopen(argv[optind], "r");
342 if (!in) {
343 perror(argv[optind]);
344 exit(1);
345 }
346 optind++;
347 } else
348 in = stdin;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400349
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000350 if (optind < argc) {
351 outfn = argv[optind];
Theodore Ts'o45d21611998-01-19 14:40:24 +0000352 newfn = (char *) malloc(strlen(outfn)+20);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000353 if (!newfn) {
354 fprintf(stderr, "Memory error! Exiting.\n");
355 exit(1);
356 }
357 strcpy(newfn, outfn);
358 strcat(newfn, ".new");
359 out = fopen(newfn, "w");
360 if (!out) {
361 perror(newfn);
362 exit(1);
363 }
364 } else {
365 out = stdout;
366 outfn = 0;
367 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400368
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000369 while (!feof(in)) {
370 if (fgets(line, sizeof(line), in) == NULL)
371 break;
372 substitute_line(line);
373 fputs(line, out);
374 }
375 fclose(in);
376 fclose(out);
377 if (outfn) {
Andreas Dilger3f5ef962006-08-05 14:41:00 -0400378 struct stat st;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000379 if (compare_file(outfn, newfn)) {
380 if (verbose)
381 printf("No change, keeping %s.\n", outfn);
Theodore Ts'odd607052004-04-03 13:53:46 -0500382 if (adjust_timestamp) {
383 if (stat(outfn, &stbuf) == 0) {
384 if (verbose)
385 printf("Updating modtime for %s\n", outfn);
386 ut.actime = stbuf.st_atime;
387 ut.modtime = time(0);
388 if (utime(outfn, &ut) < 0)
389 perror("utime");
390 }
391 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000392 unlink(newfn);
393 } else {
394 if (verbose)
395 printf("Creating or replacing %s.\n", outfn);
396 rename(newfn, outfn);
397 }
Andreas Dilger3f5ef962006-08-05 14:41:00 -0400398 /* set read-only to alert user it is a generated file */
399 if (stat(outfn, &st) == 0)
400 chmod(outfn, st.st_mode & ~0222);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000401 }
402 return (0);
403}
404
405