blob: 4c58b91b618444b1663f70414d52a9864ddf9728 [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>
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;
39
40 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;
94
95 /*
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) {
157 fprintf(stderr, "Unfound expansion: '%s'\n",
158 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);
168 }
169 /*
170 * Now do a second pass to expand ${FOO}
171 */
172 ptr = line;
173 while (ptr) {
174 name_ptr = strchr(ptr, '$');
175 if (!name_ptr)
176 break; /* No more */
177 if (*(++name_ptr) != '{') {
178 ptr = name_ptr;
179 continue;
180 }
181 name_ptr++;
182 end_ptr = strchr(name_ptr, '}');
183 if (!end_ptr)
184 break;
185 len = end_ptr - name_ptr;
186 replace_name = get_subst_symbol(name_ptr, len, '$');
187 if (!replace_name) {
188 ptr = name_ptr;
189 continue;
190 }
191 ent = fetch_subst_entry(replace_name);
192 if (!ent) {
193 ptr = end_ptr + 1;
194 continue;
195 }
196#if 0
197 fprintf(stderr, "Replace name = '%s' with '%s'\n",
198 replace_name, ent->value);
199#endif
200 ptr = name_ptr-2;
201 replace_string(ptr, end_ptr, ent->value);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000202 }
203}
204
205static void parse_config_file(FILE *f)
206{
207 char line[2048];
208 char *cp, *ptr;
209
210 while (!feof(f)) {
211 memset(line, 0, sizeof(line));
212 if (fgets(line, sizeof(line), f) == NULL)
213 break;
214 /*
215 * Strip newlines and comments.
216 */
217 cp = strchr(line, '\n');
218 if (cp)
219 *cp = 0;
220 cp = strchr(line, '#');
221 if (cp)
222 *cp = 0;
223 /*
224 * Skip trailing and leading whitespace
225 */
226 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
227 if (*cp == ' ' || *cp == '\t')
228 *cp = 0;
229 else
230 break;
231 }
232 cp = line;
233 while (*cp && isspace(*cp))
234 cp++;
235 ptr = cp;
236 /*
237 * Skip empty lines
238 */
239 if (*ptr == 0)
240 continue;
241 /*
242 * Ignore future extensions
243 */
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000244 if (*ptr == '@')
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000245 continue;
246 /*
247 * Parse substitutions
248 */
249 for (cp = ptr; *cp; cp++)
250 if (isspace(*cp))
251 break;
252 *cp = 0;
253 for (cp++; *cp; cp++)
254 if (!isspace(*cp))
255 break;
256#if 0
Theodore Ts'o913c4e92001-04-17 05:05:37 +0000257 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000258#endif
259 add_subst(ptr, cp);
260 }
261}
262
263/*
264 * Return 0 if the files are different, 1 if the files are the same.
265 */
266static int compare_file(const char *outfn, const char *newfn)
267{
Theodore Ts'o45d21611998-01-19 14:40:24 +0000268 FILE *old_f, *new_f;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000269 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
270 int retval;
271
Theodore Ts'o45d21611998-01-19 14:40:24 +0000272 old_f = fopen(outfn, "r");
273 if (!old_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000274 return 0;
Theodore Ts'o45d21611998-01-19 14:40:24 +0000275 new_f = fopen(newfn, "r");
276 if (!new_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000277 return 0;
278
279 while (1) {
Theodore Ts'o45d21611998-01-19 14:40:24 +0000280 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
281 newcp = fgets(newbuf, sizeof(newbuf), new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000282 if (!oldcp && !newcp) {
283 retval = 1;
284 break;
285 }
286 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
287 retval = 0;
288 break;
289 }
290 }
Theodore Ts'o45d21611998-01-19 14:40:24 +0000291 fclose(old_f);
292 fclose(new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000293 return retval;
294}
295
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000296
297
298int main(int argc, char **argv)
299{
300 char line[2048];
301 int c;
302 FILE *in, *out;
303 char *outfn = NULL, *newfn = NULL;
304 int verbose = 0;
Theodore Ts'odd607052004-04-03 13:53:46 -0500305 int adjust_timestamp = 0;
306 struct stat stbuf;
307 struct utimbuf ut;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000308
Theodore Ts'odd607052004-04-03 13:53:46 -0500309 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000310 switch (c) {
311 case 'f':
312 in = fopen(optarg, "r");
313 if (!in) {
314 perror(optarg);
315 exit(1);
316 }
317 parse_config_file(in);
318 fclose(in);
319 break;
Theodore Ts'odd607052004-04-03 13:53:46 -0500320 case 't':
321 adjust_timestamp++;
322 break;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000323 case 'v':
324 verbose++;
325 break;
326 default:
327 fprintf(stderr, "%s: [-f config-file] [file]\n",
328 argv[0]);
329 break;
330 }
331 }
332 if (optind < argc) {
333 in = fopen(argv[optind], "r");
334 if (!in) {
335 perror(argv[optind]);
336 exit(1);
337 }
338 optind++;
339 } else
340 in = stdin;
341
342 if (optind < argc) {
343 outfn = argv[optind];
Theodore Ts'o45d21611998-01-19 14:40:24 +0000344 newfn = (char *) malloc(strlen(outfn)+20);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000345 if (!newfn) {
346 fprintf(stderr, "Memory error! Exiting.\n");
347 exit(1);
348 }
349 strcpy(newfn, outfn);
350 strcat(newfn, ".new");
351 out = fopen(newfn, "w");
352 if (!out) {
353 perror(newfn);
354 exit(1);
355 }
356 } else {
357 out = stdout;
358 outfn = 0;
359 }
360
361 while (!feof(in)) {
362 if (fgets(line, sizeof(line), in) == NULL)
363 break;
364 substitute_line(line);
365 fputs(line, out);
366 }
367 fclose(in);
368 fclose(out);
369 if (outfn) {
370 if (compare_file(outfn, newfn)) {
371 if (verbose)
372 printf("No change, keeping %s.\n", outfn);
Theodore Ts'odd607052004-04-03 13:53:46 -0500373 if (adjust_timestamp) {
374 if (stat(outfn, &stbuf) == 0) {
375 if (verbose)
376 printf("Updating modtime for %s\n", outfn);
377 ut.actime = stbuf.st_atime;
378 ut.modtime = time(0);
379 if (utime(outfn, &ut) < 0)
380 perror("utime");
381 }
382 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000383 unlink(newfn);
384 } else {
385 if (verbose)
386 printf("Creating or replacing %s.\n", outfn);
387 rename(newfn, outfn);
388 }
389 }
390 return (0);
391}
392
393