blob: 66d7d9a9e1096b727502fb6a4b39ec6974c0faaa [file] [log] [blame]
Theodore Ts'o44339bd1997-10-15 02:47:20 +00001/*
2 * subst.c --- substitution program
3 *
Sebastian Rasmussen055866d2017-10-08 13:30:08 +02004 * Subst is used as a quickie program to do @ substitutions
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'o44339bd1997-10-15 02:47:20 +00006 */
7
Theodore Ts'oe7822c12014-07-05 20:23:23 -04008#ifdef HAVE_CONFIG_H
Theodore Ts'o28739272014-01-03 00:26:43 -05009#include "config.h"
Theodore Ts'o441eb332014-10-19 22:02:48 -040010#else
Robert Schiele68192a82018-03-20 00:38:48 -040011#define HAVE_SYS_STAT_H
Theodore Ts'o441eb332014-10-19 22:02:48 -040012#define HAVE_SYS_TIME_H
Theodore Ts'oe7822c12014-07-05 20:23:23 -040013#endif
Theodore Ts'o44339bd1997-10-15 02:47:20 +000014#include <stdio.h>
15#include <errno.h>
16#include <stdlib.h>
17#include <unistd.h>
18#include <string.h>
19#include <ctype.h>
Andreas Dilger8b90ab22014-07-25 21:43:08 -040020#ifdef HAVE_SYS_TIME_H
21#include <sys/time.h>
22#endif
Andreas Dilger7fd53742016-03-05 17:38:33 -070023#ifdef HAVE_SYS_TYPES_H
Theodore Ts'odd607052004-04-03 13:53:46 -050024#include <sys/types.h>
Andreas Dilger7fd53742016-03-05 17:38:33 -070025#endif
26#ifdef HAVE_SYS_STAT_H
Theodore Ts'odd607052004-04-03 13:53:46 -050027#include <sys/stat.h>
Andreas Dilger7fd53742016-03-05 17:38:33 -070028#endif
Theodore Ts'o28739272014-01-03 00:26:43 -050029#include <fcntl.h>
Matthias Andreef932ed92004-04-12 20:16:04 +020030#include <time.h>
Theodore Ts'odd607052004-04-03 13:53:46 -050031#include <utime.h>
Darrick J. Wong8a6cc1a2014-03-14 09:43:11 -040032#ifdef HAVE_SYS_TIME_H
33#include <sys/time.h>
34#endif
Theodore Ts'o44339bd1997-10-15 02:47:20 +000035
36#ifdef HAVE_GETOPT_H
37#include <getopt.h>
Theodore Ts'o691d3352000-04-03 16:16:46 +000038#else
39extern char *optarg;
40extern int optind;
Theodore Ts'o44339bd1997-10-15 02:47:20 +000041#endif
42
43
44struct subst_entry {
45 char *name;
46 char *value;
47 struct subst_entry *next;
48};
49
Theodore Ts'of4041672013-12-16 18:56:36 -050050static struct subst_entry *subst_table = 0;
Theodore Ts'o44339bd1997-10-15 02:47:20 +000051
52static int add_subst(char *name, char *value)
53{
54 struct subst_entry *ent = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040055
Theodore Ts'o45d21611998-01-19 14:40:24 +000056 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
Theodore Ts'o44339bd1997-10-15 02:47:20 +000057 if (!ent)
58 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000059 ent->name = (char *) malloc(strlen(name)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000060 if (!ent->name)
61 goto fail;
Theodore Ts'o45d21611998-01-19 14:40:24 +000062 ent->value = (char *) malloc(strlen(value)+1);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000063 if (!ent->value)
64 goto fail;
65 strcpy(ent->name, name);
66 strcpy(ent->value, value);
67 ent->next = subst_table;
68 subst_table = ent;
69 return 0;
70fail:
71 if (ent) {
Jim Meyering45e338f2009-02-23 18:07:50 +010072 free(ent->name);
Theodore Ts'o44339bd1997-10-15 02:47:20 +000073 free(ent);
74 }
Eric Sandeend5c1d822011-09-16 15:49:29 -050075 return ENOMEM;
Theodore Ts'o44339bd1997-10-15 02:47:20 +000076}
77
78static struct subst_entry *fetch_subst_entry(char *name)
79{
80 struct subst_entry *ent;
81
82 for (ent = subst_table; ent; ent = ent->next) {
83 if (strcmp(name, ent->name) == 0)
84 break;
85 }
86 return ent;
87}
88
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000089/*
90 * Given the starting and ending position of the replacement name,
91 * check to see if it is valid, and pull it out if it is.
92 */
Theodore Ts'o54434922003-12-07 01:28:50 -050093static char *get_subst_symbol(const char *begin, size_t len, char prefix)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +000094{
95 static char replace_name[128];
96 char *cp, *start;
97
98 start = replace_name;
99 if (prefix)
100 *start++ = prefix;
101
102 if (len > sizeof(replace_name)-2)
103 return NULL;
104 memcpy(start, begin, len);
105 start[len] = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400106
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000107 /*
Theodore Ts'o7822c1d1998-12-19 08:08:43 +0000108 * The substitution variable must all be in the of [0-9A-Za-z_].
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000109 * If it isn't, this must be an invalid symbol name.
110 */
111 for (cp = start; *cp; cp++) {
112 if (!(*cp >= 'a' && *cp <= 'z') &&
113 !(*cp >= 'A' && *cp <= 'Z') &&
Theodore Ts'o7822c1d1998-12-19 08:08:43 +0000114 !(*cp >= '0' && *cp <= '9') &&
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000115 !(*cp == '_'))
116 return NULL;
117 }
118 return (replace_name);
119}
120
121static void replace_string(char *begin, char *end, char *newstr)
122{
123 int replace_len, len;
124
125 replace_len = strlen(newstr);
126 len = end - begin;
Theodore Ts'o913c4e92001-04-17 05:05:37 +0000127 if (replace_len == 0)
128 memmove(begin, end+1, strlen(end)+1);
129 else if (replace_len != len+1)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000130 memmove(end+(replace_len-len-1), end,
131 strlen(end)+1);
132 memcpy(begin, newstr, replace_len);
133}
134
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000135static void substitute_line(char *line)
136{
Theodore Ts'ocdceb041999-10-26 16:28:59 +0000137 char *ptr, *name_ptr, *end_ptr;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000138 struct subst_entry *ent;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000139 char *replace_name;
Theodore Ts'o54434922003-12-07 01:28:50 -0500140 size_t len;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000141
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000142 /*
143 * Expand all @FOO@ substitutions
144 */
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000145 ptr = line;
146 while (ptr) {
147 name_ptr = strchr(ptr, '@');
148 if (!name_ptr)
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000149 break; /* No more */
150 if (*(++name_ptr) == '@') {
151 /*
152 * Handle tytso@@mit.edu --> tytso@mit.edu
153 */
154 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
155 ptr = name_ptr+1;
156 continue;
157 }
158 end_ptr = strchr(name_ptr, '@');
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000159 if (!end_ptr)
160 break;
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000161 len = end_ptr - name_ptr;
162 replace_name = get_subst_symbol(name_ptr, len, 0);
163 if (!replace_name) {
164 ptr = name_ptr;
165 continue;
166 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000167 ent = fetch_subst_entry(replace_name);
168 if (!ent) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400169 fprintf(stderr, "Unfound expansion: '%s'\n",
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000170 replace_name);
171 ptr = end_ptr + 1;
172 continue;
173 }
174#if 0
175 fprintf(stderr, "Replace name = '%s' with '%s'\n",
176 replace_name, ent->value);
177#endif
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000178 ptr = name_ptr-1;
179 replace_string(ptr, end_ptr, ent->value);
Theodore Ts'o98224fb2006-11-12 17:43:50 -0500180 if ((ent->value[0] == '@') &&
181 (strlen(replace_name) == strlen(ent->value)-2) &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400182 !strncmp(replace_name, ent->value+1,
Theodore Ts'o98224fb2006-11-12 17:43:50 -0500183 strlen(ent->value)-2))
184 /* avoid an infinite loop */
185 ptr += strlen(ent->value);
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000186 }
187 /*
188 * Now do a second pass to expand ${FOO}
189 */
190 ptr = line;
191 while (ptr) {
192 name_ptr = strchr(ptr, '$');
193 if (!name_ptr)
194 break; /* No more */
195 if (*(++name_ptr) != '{') {
196 ptr = name_ptr;
197 continue;
198 }
199 name_ptr++;
200 end_ptr = strchr(name_ptr, '}');
201 if (!end_ptr)
202 break;
203 len = end_ptr - name_ptr;
204 replace_name = get_subst_symbol(name_ptr, len, '$');
205 if (!replace_name) {
206 ptr = name_ptr;
207 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400208 }
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000209 ent = fetch_subst_entry(replace_name);
210 if (!ent) {
211 ptr = end_ptr + 1;
212 continue;
213 }
214#if 0
215 fprintf(stderr, "Replace name = '%s' with '%s'\n",
216 replace_name, ent->value);
217#endif
218 ptr = name_ptr-2;
219 replace_string(ptr, end_ptr, ent->value);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000220 }
221}
222
223static void parse_config_file(FILE *f)
224{
225 char line[2048];
226 char *cp, *ptr;
227
228 while (!feof(f)) {
229 memset(line, 0, sizeof(line));
230 if (fgets(line, sizeof(line), f) == NULL)
231 break;
232 /*
233 * Strip newlines and comments.
234 */
235 cp = strchr(line, '\n');
236 if (cp)
237 *cp = 0;
238 cp = strchr(line, '#');
239 if (cp)
240 *cp = 0;
241 /*
242 * Skip trailing and leading whitespace
243 */
244 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
245 if (*cp == ' ' || *cp == '\t')
246 *cp = 0;
247 else
248 break;
249 }
250 cp = line;
251 while (*cp && isspace(*cp))
252 cp++;
253 ptr = cp;
254 /*
255 * Skip empty lines
256 */
257 if (*ptr == 0)
258 continue;
259 /*
260 * Ignore future extensions
261 */
Theodore Ts'oe7549ca1998-08-01 04:35:39 +0000262 if (*ptr == '@')
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000263 continue;
264 /*
265 * Parse substitutions
266 */
267 for (cp = ptr; *cp; cp++)
268 if (isspace(*cp))
269 break;
270 *cp = 0;
271 for (cp++; *cp; cp++)
272 if (!isspace(*cp))
273 break;
274#if 0
Theodore Ts'o913c4e92001-04-17 05:05:37 +0000275 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000276#endif
277 add_subst(ptr, cp);
278 }
279}
280
281/*
282 * Return 0 if the files are different, 1 if the files are the same.
283 */
Theodore Ts'o28739272014-01-03 00:26:43 -0500284static int compare_file(FILE *old_f, FILE *new_f)
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000285{
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000286 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
287 int retval;
288
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000289 while (1) {
Theodore Ts'o45d21611998-01-19 14:40:24 +0000290 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
291 newcp = fgets(newbuf, sizeof(newbuf), new_f);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000292 if (!oldcp && !newcp) {
293 retval = 1;
294 break;
295 }
296 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
297 retval = 0;
298 break;
299 }
300 }
301 return retval;
302}
303
Theodore Ts'o441eb332014-10-19 22:02:48 -0400304void set_utimes(const char *filename, int fd, const struct timeval times[2])
305{
306#ifdef HAVE_FUTIMES
307 if (futimes(fd, times) < 0)
308 perror("futimes");
309#elif HAVE_UTIMES
310 if (utimes(filename, times) < 0)
311 perror("utimes");
312#else
313 struct utimbuf ut;
314
315 ut.actime = times[0].tv_sec;
316 ut.modtime = times[1].tv_sec;
317 if (utime(filename, &ut) < 0)
318 perror("utime");
319#endif
320}
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000321
322
323int main(int argc, char **argv)
324{
325 char line[2048];
326 int c;
Theodore Ts'oe5a82002015-09-18 21:37:53 -0400327 int fd, ofd = -1;
Theodore Ts'o28739272014-01-03 00:26:43 -0500328 FILE *in, *out, *old = NULL;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000329 char *outfn = NULL, *newfn = NULL;
330 int verbose = 0;
Theodore Ts'odd607052004-04-03 13:53:46 -0500331 int adjust_timestamp = 0;
Theodore Ts'o28739272014-01-03 00:26:43 -0500332 int got_atime = 0;
Theodore Ts'odd607052004-04-03 13:53:46 -0500333 struct stat stbuf;
Theodore Ts'o28739272014-01-03 00:26:43 -0500334 struct timeval tv[2];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400335
Theodore Ts'odd607052004-04-03 13:53:46 -0500336 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000337 switch (c) {
338 case 'f':
339 in = fopen(optarg, "r");
340 if (!in) {
341 perror(optarg);
342 exit(1);
343 }
344 parse_config_file(in);
345 fclose(in);
346 break;
Theodore Ts'odd607052004-04-03 13:53:46 -0500347 case 't':
348 adjust_timestamp++;
349 break;
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000350 case 'v':
351 verbose++;
352 break;
353 default:
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400354 fprintf(stderr, "%s: [-f config-file] [file]\n",
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000355 argv[0]);
356 break;
357 }
358 }
359 if (optind < argc) {
360 in = fopen(argv[optind], "r");
361 if (!in) {
362 perror(argv[optind]);
363 exit(1);
364 }
365 optind++;
366 } else
367 in = stdin;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400368
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000369 if (optind < argc) {
370 outfn = argv[optind];
Theodore Ts'o45d21611998-01-19 14:40:24 +0000371 newfn = (char *) malloc(strlen(outfn)+20);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000372 if (!newfn) {
373 fprintf(stderr, "Memory error! Exiting.\n");
374 exit(1);
375 }
376 strcpy(newfn, outfn);
377 strcat(newfn, ".new");
Theodore Ts'oe5a82002015-09-18 21:37:53 -0400378 ofd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0644);
379 if (ofd < 0) {
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000380 perror(newfn);
381 exit(1);
382 }
Theodore Ts'oe5a82002015-09-18 21:37:53 -0400383 out = fdopen(ofd, "w+");
Theodore Ts'o28739272014-01-03 00:26:43 -0500384 if (!out) {
385 perror("fdopen");
386 exit(1);
387 }
388
389 fd = open(outfn, O_RDONLY);
390 if (fd > 0) {
391 /* save the original atime, if possible */
392 if (fstat(fd, &stbuf) == 0) {
393#if HAVE_STRUCT_STAT_ST_ATIM
394 tv[0].tv_sec = stbuf.st_atim.tv_sec;
395 tv[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
396#else
397 tv[0].tv_sec = stbuf.st_atime;
398 tv[0].tv_usec = 0;
399#endif
400 got_atime = 1;
401 }
402 old = fdopen(fd, "r");
403 if (!old)
404 close(fd);
405 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000406 } else {
407 out = stdout;
408 outfn = 0;
409 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400410
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000411 while (!feof(in)) {
412 if (fgets(line, sizeof(line), in) == NULL)
413 break;
414 substitute_line(line);
415 fputs(line, out);
416 }
417 fclose(in);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000418 if (outfn) {
Theodore Ts'o28739272014-01-03 00:26:43 -0500419 fflush(out);
420 rewind(out);
421 if (old && compare_file(old, out)) {
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000422 if (verbose)
423 printf("No change, keeping %s.\n", outfn);
Theodore Ts'odd607052004-04-03 13:53:46 -0500424 if (adjust_timestamp) {
Theodore Ts'o28739272014-01-03 00:26:43 -0500425 if (verbose)
426 printf("Updating modtime for %s\n", outfn);
427 if (gettimeofday(&tv[1], NULL) < 0) {
428 perror("gettimeofday");
429 exit(1);
Theodore Ts'odd607052004-04-03 13:53:46 -0500430 }
Theodore Ts'o28739272014-01-03 00:26:43 -0500431 if (got_atime == 0)
432 tv[0] = tv[1];
433 else if (verbose)
434 printf("Using original atime\n");
Theodore Ts'o441eb332014-10-19 22:02:48 -0400435 set_utimes(outfn, fileno(old), tv);
Theodore Ts'odd607052004-04-03 13:53:46 -0500436 }
Theodore Ts'oe5a82002015-09-18 21:37:53 -0400437 if (ofd >= 0)
438 (void) fchmod(ofd, 0444);
Theodore Ts'o28739272014-01-03 00:26:43 -0500439 fclose(out);
440 if (unlink(newfn) < 0)
441 perror("unlink");
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000442 } else {
443 if (verbose)
444 printf("Creating or replacing %s.\n", outfn);
Theodore Ts'oe5a82002015-09-18 21:37:53 -0400445 if (ofd >= 0)
446 (void) fchmod(ofd, 0444);
Theodore Ts'o28739272014-01-03 00:26:43 -0500447 fclose(out);
448 if (old)
449 fclose(old);
450 old = NULL;
451 if (rename(newfn, outfn) < 0) {
452 perror("rename");
453 exit(1);
454 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000455 }
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000456 }
Theodore Ts'o28739272014-01-03 00:26:43 -0500457 if (old)
458 fclose(old);
Darrick J. Wong24997f12014-03-14 09:34:10 -0400459 if (newfn)
460 free(newfn);
Theodore Ts'o44339bd1997-10-15 02:47:20 +0000461 return (0);
462}
463
464