Whamcloud - gitweb
e2fsck: insert a missing dirent tail for checksums if possible
[tools/e2fsprogs.git] / util / subst.c
1 /*
2  * subst.c --- substitution program
3  *
4  * Subst is used as a quicky program to do @ substitutions
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 #include <stdio.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <ctype.h>
17 #ifdef HAVE_SYS_TIME_H
18 #include <sys/time.h>
19 #endif
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <time.h>
24 #include <utime.h>
25 #ifdef HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28
29 #ifdef HAVE_GETOPT_H
30 #include <getopt.h>
31 #else
32 extern char *optarg;
33 extern int optind;
34 #endif
35
36
37 struct subst_entry {
38         char *name;
39         char *value;
40         struct subst_entry *next;
41 };
42
43 static struct subst_entry *subst_table = 0;
44
45 static int add_subst(char *name, char *value)
46 {
47         struct subst_entry      *ent = 0;
48
49         ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
50         if (!ent)
51                 goto fail;
52         ent->name = (char *) malloc(strlen(name)+1);
53         if (!ent->name)
54                 goto fail;
55         ent->value = (char *) malloc(strlen(value)+1);
56         if (!ent->value)
57                 goto fail;
58         strcpy(ent->name, name);
59         strcpy(ent->value, value);
60         ent->next = subst_table;
61         subst_table = ent;
62         return 0;
63 fail:
64         if (ent) {
65                 free(ent->name);
66                 free(ent);
67         }
68         return ENOMEM;
69 }
70
71 static struct subst_entry *fetch_subst_entry(char *name)
72 {
73         struct subst_entry *ent;
74
75         for (ent = subst_table; ent; ent = ent->next) {
76                 if (strcmp(name, ent->name) == 0)
77                         break;
78         }
79         return ent;
80 }
81
82 /*
83  * Given the starting and ending position of the replacement name,
84  * check to see if it is valid, and pull it out if it is.
85  */
86 static char *get_subst_symbol(const char *begin, size_t len, char prefix)
87 {
88         static char replace_name[128];
89         char *cp, *start;
90
91         start = replace_name;
92         if (prefix)
93                 *start++ = prefix;
94
95         if (len > sizeof(replace_name)-2)
96                 return NULL;
97         memcpy(start, begin, len);
98         start[len] = 0;
99
100         /*
101          * The substitution variable must all be in the of [0-9A-Za-z_].
102          * If it isn't, this must be an invalid symbol name.
103          */
104         for (cp = start; *cp; cp++) {
105                 if (!(*cp >= 'a' && *cp <= 'z') &&
106                     !(*cp >= 'A' && *cp <= 'Z') &&
107                     !(*cp >= '0' && *cp <= '9') &&
108                     !(*cp == '_'))
109                         return NULL;
110         }
111         return (replace_name);
112 }
113
114 static void replace_string(char *begin, char *end, char *newstr)
115 {
116         int     replace_len, len;
117
118         replace_len = strlen(newstr);
119         len = end - begin;
120         if (replace_len == 0)
121                 memmove(begin, end+1, strlen(end)+1);
122         else if (replace_len != len+1)
123                 memmove(end+(replace_len-len-1), end,
124                         strlen(end)+1);
125         memcpy(begin, newstr, replace_len);
126 }
127
128 static void substitute_line(char *line)
129 {
130         char    *ptr, *name_ptr, *end_ptr;
131         struct subst_entry *ent;
132         char    *replace_name;
133         size_t  len;
134
135         /*
136          * Expand all @FOO@ substitutions
137          */
138         ptr = line;
139         while (ptr) {
140                 name_ptr = strchr(ptr, '@');
141                 if (!name_ptr)
142                         break;  /* No more */
143                 if (*(++name_ptr) == '@') {
144                         /*
145                          * Handle tytso@@mit.edu --> tytso@mit.edu
146                          */
147                         memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
148                         ptr = name_ptr+1;
149                         continue;
150                 }
151                 end_ptr = strchr(name_ptr, '@');
152                 if (!end_ptr)
153                         break;
154                 len = end_ptr - name_ptr;
155                 replace_name = get_subst_symbol(name_ptr, len, 0);
156                 if (!replace_name) {
157                         ptr = name_ptr;
158                         continue;
159                 }
160                 ent = fetch_subst_entry(replace_name);
161                 if (!ent) {
162                         fprintf(stderr, "Unfound expansion: '%s'\n",
163                                 replace_name);
164                         ptr = end_ptr + 1;
165                         continue;
166                 }
167 #if 0
168                 fprintf(stderr, "Replace name = '%s' with '%s'\n",
169                        replace_name, ent->value);
170 #endif
171                 ptr = name_ptr-1;
172                 replace_string(ptr, end_ptr, ent->value);
173                 if ((ent->value[0] == '@') &&
174                     (strlen(replace_name) == strlen(ent->value)-2) &&
175                     !strncmp(replace_name, ent->value+1,
176                              strlen(ent->value)-2))
177                         /* avoid an infinite loop */
178                         ptr += strlen(ent->value);
179         }
180         /*
181          * Now do a second pass to expand ${FOO}
182          */
183         ptr = line;
184         while (ptr) {
185                 name_ptr = strchr(ptr, '$');
186                 if (!name_ptr)
187                         break;  /* No more */
188                 if (*(++name_ptr) != '{') {
189                         ptr = name_ptr;
190                         continue;
191                 }
192                 name_ptr++;
193                 end_ptr = strchr(name_ptr, '}');
194                 if (!end_ptr)
195                         break;
196                 len = end_ptr - name_ptr;
197                 replace_name = get_subst_symbol(name_ptr, len, '$');
198                 if (!replace_name) {
199                         ptr = name_ptr;
200                         continue;
201                 }
202                 ent = fetch_subst_entry(replace_name);
203                 if (!ent) {
204                         ptr = end_ptr + 1;
205                         continue;
206                 }
207 #if 0
208                 fprintf(stderr, "Replace name = '%s' with '%s'\n",
209                        replace_name, ent->value);
210 #endif
211                 ptr = name_ptr-2;
212                 replace_string(ptr, end_ptr, ent->value);
213         }
214 }
215
216 static void parse_config_file(FILE *f)
217 {
218         char    line[2048];
219         char    *cp, *ptr;
220
221         while (!feof(f)) {
222                 memset(line, 0, sizeof(line));
223                 if (fgets(line, sizeof(line), f) == NULL)
224                         break;
225                 /*
226                  * Strip newlines and comments.
227                  */
228                 cp = strchr(line, '\n');
229                 if (cp)
230                         *cp = 0;
231                 cp = strchr(line, '#');
232                 if (cp)
233                         *cp = 0;
234                 /*
235                  * Skip trailing and leading whitespace
236                  */
237                 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
238                         if (*cp == ' ' || *cp == '\t')
239                                 *cp = 0;
240                         else
241                                 break;
242                 }
243                 cp = line;
244                 while (*cp && isspace(*cp))
245                         cp++;
246                 ptr = cp;
247                 /*
248                  * Skip empty lines
249                  */
250                 if (*ptr == 0)
251                         continue;
252                 /*
253                  * Ignore future extensions
254                  */
255                 if (*ptr == '@')
256                         continue;
257                 /*
258                  * Parse substitutions
259                  */
260                 for (cp = ptr; *cp; cp++)
261                         if (isspace(*cp))
262                                 break;
263                 *cp = 0;
264                 for (cp++; *cp; cp++)
265                         if (!isspace(*cp))
266                                 break;
267 #if 0
268                 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
269 #endif
270                 add_subst(ptr, cp);
271         }
272 }
273
274 /*
275  * Return 0 if the files are different, 1 if the files are the same.
276  */
277 static int compare_file(FILE *old_f, FILE *new_f)
278 {
279         char    oldbuf[2048], newbuf[2048], *oldcp, *newcp;
280         int     retval;
281
282         while (1) {
283                 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
284                 newcp = fgets(newbuf, sizeof(newbuf), new_f);
285                 if (!oldcp && !newcp) {
286                         retval = 1;
287                         break;
288                 }
289                 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
290                         retval = 0;
291                         break;
292                 }
293         }
294         return retval;
295 }
296
297
298
299 int main(int argc, char **argv)
300 {
301         char    line[2048];
302         int     c;
303         int     fd;
304         FILE    *in, *out, *old = NULL;
305         char    *outfn = NULL, *newfn = NULL;
306         int     verbose = 0;
307         int     adjust_timestamp = 0;
308         int     got_atime = 0;
309         struct stat stbuf;
310         struct timeval tv[2];
311
312         while ((c = getopt (argc, argv, "f:tv")) != EOF) {
313                 switch (c) {
314                 case 'f':
315                         in = fopen(optarg, "r");
316                         if (!in) {
317                                 perror(optarg);
318                                 exit(1);
319                         }
320                         parse_config_file(in);
321                         fclose(in);
322                         break;
323                 case 't':
324                         adjust_timestamp++;
325                         break;
326                 case 'v':
327                         verbose++;
328                         break;
329                 default:
330                         fprintf(stderr, "%s: [-f config-file] [file]\n",
331                                 argv[0]);
332                         break;
333                 }
334         }
335         if (optind < argc) {
336                 in = fopen(argv[optind], "r");
337                 if (!in) {
338                         perror(argv[optind]);
339                         exit(1);
340                 }
341                 optind++;
342         } else
343                 in = stdin;
344
345         if (optind < argc) {
346                 outfn = argv[optind];
347                 newfn = (char *) malloc(strlen(outfn)+20);
348                 if (!newfn) {
349                         fprintf(stderr, "Memory error!  Exiting.\n");
350                         exit(1);
351                 }
352                 strcpy(newfn, outfn);
353                 strcat(newfn, ".new");
354                 fd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0444);
355                 if (fd < 0) {
356                         perror(newfn);
357                         exit(1);
358                 }
359                 out = fdopen(fd, "w+");
360                 if (!out) {
361                         perror("fdopen");
362                         exit(1);
363                 }
364
365                 fd = open(outfn, O_RDONLY);
366                 if (fd > 0) {
367                         /* save the original atime, if possible */
368                         if (fstat(fd, &stbuf) == 0) {
369 #if HAVE_STRUCT_STAT_ST_ATIM
370                                 tv[0].tv_sec = stbuf.st_atim.tv_sec;
371                                 tv[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
372 #else
373                                 tv[0].tv_sec = stbuf.st_atime;
374                                 tv[0].tv_usec = 0;
375 #endif
376                                 got_atime = 1;
377                         }
378                         old = fdopen(fd, "r");
379                         if (!old)
380                                 close(fd);
381                 }
382         } else {
383                 out = stdout;
384                 outfn = 0;
385         }
386
387         while (!feof(in)) {
388                 if (fgets(line, sizeof(line), in) == NULL)
389                         break;
390                 substitute_line(line);
391                 fputs(line, out);
392         }
393         fclose(in);
394         if (outfn) {
395                 fflush(out);
396                 rewind(out);
397                 if (old && compare_file(old, out)) {
398                         if (verbose)
399                                 printf("No change, keeping %s.\n", outfn);
400                         if (adjust_timestamp) {
401                                 if (verbose)
402                                         printf("Updating modtime for %s\n", outfn);
403                                 if (gettimeofday(&tv[1], NULL) < 0) {
404                                         perror("gettimeofday");
405                                         exit(1);
406                                 }
407                                 if (got_atime == 0)
408                                         tv[0] = tv[1];
409                                 else if (verbose)
410                                         printf("Using original atime\n");
411 #ifdef HAVE_FUTIMES
412                                 if (futimes(fileno(old), tv) < 0)
413                                         perror("futimes");
414 #else
415                                 if (utimes(outfn, tv) < 0)
416                                         perror("utimes");
417 #endif
418                         }
419                         fclose(out);
420                         if (unlink(newfn) < 0)
421                                 perror("unlink");
422                 } else {
423                         if (verbose)
424                                 printf("Creating or replacing %s.\n", outfn);
425                         fclose(out);
426                         if (old)
427                                 fclose(old);
428                         old = NULL;
429                         if (rename(newfn, outfn) < 0) {
430                                 perror("rename");
431                                 exit(1);
432                         }
433                 }
434         }
435         if (old)
436                 fclose(old);
437         if (newfn)
438                 free(newfn);
439         return (0);
440 }
441
442