Whamcloud - gitweb
Update Debian packaging for 1.39+1.40-WIP-2006.10.02+dfsg-1
[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 #include <stdio.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <time.h>
17 #include <utime.h>
18
19 #ifdef HAVE_GETOPT_H
20 #include <getopt.h>
21 #else
22 extern char *optarg;
23 extern int optind;
24 #endif
25
26
27 struct subst_entry {
28         char *name;
29         char *value;
30         struct subst_entry *next;
31 };
32
33 struct subst_entry *subst_table = 0;
34
35 static int add_subst(char *name, char *value)
36 {
37         struct subst_entry      *ent = 0;
38         int     retval;
39         
40         retval = ENOMEM;
41         ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
42         if (!ent)
43                 goto fail;
44         ent->name = (char *) malloc(strlen(name)+1);
45         if (!ent->name)
46                 goto fail;
47         ent->value = (char *) malloc(strlen(value)+1);
48         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;
55 fail:
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
66 static 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
77 /*
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  */
81 static char *get_subst_symbol(const char *begin, size_t len, char prefix)
82 {
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         /*
96          * The substitution variable must all be in the of [0-9A-Za-z_].
97          * 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') &&
102                     !(*cp >= '0' && *cp <= '9') &&
103                     !(*cp == '_'))
104                         return NULL;
105         }
106         return (replace_name);
107 }
108
109 static 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;
115         if (replace_len == 0)
116                 memmove(begin, end+1, strlen(end)+1);
117         else if (replace_len != len+1)
118                 memmove(end+(replace_len-len-1), end,
119                         strlen(end)+1);
120         memcpy(begin, newstr, replace_len);
121 }
122
123 static void substitute_line(char *line)
124 {
125         char    *ptr, *name_ptr, *end_ptr;
126         struct subst_entry *ent;
127         char    *replace_name;
128         size_t  len;
129
130         /*
131          * Expand all @FOO@ substitutions
132          */
133         ptr = line;
134         while (ptr) {
135                 name_ptr = strchr(ptr, '@');
136                 if (!name_ptr)
137                         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, '@');
147                 if (!end_ptr)
148                         break;
149                 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                 }
155                 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
166                 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);
202         }
203 }
204
205 static 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                  */
244                 if (*ptr == '@')
245                         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
257                 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
258 #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  */
266 static int compare_file(const char *outfn, const char *newfn)
267 {
268         FILE    *old_f, *new_f;
269         char    oldbuf[2048], newbuf[2048], *oldcp, *newcp;
270         int     retval;
271
272         old_f = fopen(outfn, "r");
273         if (!old_f)
274                 return 0;
275         new_f = fopen(newfn, "r");
276         if (!new_f)
277                 return 0;
278
279         while (1) {
280                 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
281                 newcp = fgets(newbuf, sizeof(newbuf), new_f);
282                 if (!oldcp && !newcp) {
283                         retval = 1;
284                         break;
285                 }
286                 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
287                         retval = 0;
288                         break;
289                 }
290         }
291         fclose(old_f);
292         fclose(new_f);
293         return retval;
294 }
295
296
297
298 int 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;
305         int     adjust_timestamp = 0;
306         struct stat stbuf;
307         struct utimbuf ut;
308         
309         while ((c = getopt (argc, argv, "f:tv")) != EOF) {
310                 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;
320                 case 't':
321                         adjust_timestamp++;
322                         break;
323                 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];
344                 newfn = (char *) malloc(strlen(outfn)+20);
345                 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                 struct stat st;
371                 if (compare_file(outfn, newfn)) {
372                         if (verbose)
373                                 printf("No change, keeping %s.\n", outfn);
374                         if (adjust_timestamp) {
375                                 if (stat(outfn, &stbuf) == 0) {
376                                         if (verbose)
377                                                 printf("Updating modtime for %s\n", outfn);
378                                         ut.actime = stbuf.st_atime;
379                                         ut.modtime = time(0);
380                                         if (utime(outfn, &ut) < 0)
381                                                 perror("utime");
382                                 }
383                         }
384                         unlink(newfn);
385                 } else {
386                         if (verbose)
387                                 printf("Creating or replacing %s.\n", outfn);
388                         rename(newfn, outfn);
389                 }
390                 /* set read-only to alert user it is a generated file */
391                 if (stat(outfn, &st) == 0)
392                         chmod(outfn, st.st_mode & ~0222);
393         }
394         return (0);
395 }
396
397