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