Whamcloud - gitweb
e2fsck: track errors/badness found for each inode
[tools/e2fsprogs.git] / util / subst.c
index 82779f9..8544b6d 100644 (file)
@@ -2,7 +2,7 @@
  * subst.c --- substitution program
  *
  * Subst is used as a quicky program to do @ substitutions
- * 
+ *
  */
 
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
 #include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <utime.h>
 
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
+#else
+extern char *optarg;
+extern int optind;
 #endif
 
 
@@ -28,16 +35,14 @@ struct subst_entry *subst_table = 0;
 static int add_subst(char *name, char *value)
 {
        struct subst_entry      *ent = 0;
-       int     retval;
-       
-       retval = ENOMEM;
-       ent = malloc(sizeof(struct subst_entry));
+
+       ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
        if (!ent)
                goto fail;
-       ent->name = malloc(strlen(name)+1);
+       ent->name = (char *) malloc(strlen(name)+1);
        if (!ent->name)
                goto fail;
-       ent->value = malloc(strlen(value)+1);
+       ent->value = (char *) malloc(strlen(value)+1);
        if (!ent->value)
                goto fail;
        strcpy(ent->name, name);
@@ -47,13 +52,10 @@ static int add_subst(char *name, char *value)
        return 0;
 fail:
        if (ent) {
-               if (ent->name)
-                       free(ent->name);
-               if (ent->value)
-                       free(ent->value);
+               free(ent->name);
                free(ent);
        }
-       return retval;
+       return ENOMEM;
 }
 
 static struct subst_entry *fetch_subst_entry(char *name)
@@ -67,27 +69,87 @@ static struct subst_entry *fetch_subst_entry(char *name)
        return ent;
 }
 
+/*
+ * Given the starting and ending position of the replacement name,
+ * check to see if it is valid, and pull it out if it is.
+ */
+static char *get_subst_symbol(const char *begin, size_t len, char prefix)
+{
+       static char replace_name[128];
+       char *cp, *start;
+
+       start = replace_name;
+       if (prefix)
+               *start++ = prefix;
+
+       if (len > sizeof(replace_name)-2)
+               return NULL;
+       memcpy(start, begin, len);
+       start[len] = 0;
+
+       /*
+        * The substitution variable must all be in the of [0-9A-Za-z_].
+        * If it isn't, this must be an invalid symbol name.
+        */
+       for (cp = start; *cp; cp++) {
+               if (!(*cp >= 'a' && *cp <= 'z') &&
+                   !(*cp >= 'A' && *cp <= 'Z') &&
+                   !(*cp >= '0' && *cp <= '9') &&
+                   !(*cp == '_'))
+                       return NULL;
+       }
+       return (replace_name);
+}
+
+static void replace_string(char *begin, char *end, char *newstr)
+{
+       int     replace_len, len;
+
+       replace_len = strlen(newstr);
+       len = end - begin;
+       if (replace_len == 0)
+               memmove(begin, end+1, strlen(end)+1);
+       else if (replace_len != len+1)
+               memmove(end+(replace_len-len-1), end,
+                       strlen(end)+1);
+       memcpy(begin, newstr, replace_len);
+}
+
 static void substitute_line(char *line)
 {
        char    *ptr, *name_ptr, *end_ptr;
        struct subst_entry *ent;
-       char    replace_name[128];
-       int     len, replace_len;
+       char    *replace_name;
+       size_t  len;
 
+       /*
+        * Expand all @FOO@ substitutions
+        */
        ptr = line;
        while (ptr) {
                name_ptr = strchr(ptr, '@');
                if (!name_ptr)
-                       break;
-               end_ptr = strchr(name_ptr+1, '@');
+                       break;  /* No more */
+               if (*(++name_ptr) == '@') {
+                       /*
+                        * Handle tytso@@mit.edu --> tytso@mit.edu
+                        */
+                       memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
+                       ptr = name_ptr+1;
+                       continue;
+               }
+               end_ptr = strchr(name_ptr, '@');
                if (!end_ptr)
                        break;
-               len = end_ptr - name_ptr - 1;
-               memcpy(replace_name, name_ptr+1, len);
-               replace_name[len] = 0;
+               len = end_ptr - name_ptr;
+               replace_name = get_subst_symbol(name_ptr, len, 0);
+               if (!replace_name) {
+                       ptr = name_ptr;
+                       continue;
+               }
                ent = fetch_subst_entry(replace_name);
                if (!ent) {
-                       fprintf(stderr, "Unfound expansion: '%s'\n", 
+                       fprintf(stderr, "Unfound expansion: '%s'\n",
                                replace_name);
                        ptr = end_ptr + 1;
                        continue;
@@ -96,12 +158,48 @@ static void substitute_line(char *line)
                fprintf(stderr, "Replace name = '%s' with '%s'\n",
                       replace_name, ent->value);
 #endif
-               replace_len = strlen(ent->value);
-               if (replace_len != len+2)
-                       memmove(end_ptr+(replace_len-len-2), end_ptr,
-                               strlen(end_ptr)+1);
-               memcpy(name_ptr, ent->value, replace_len);
-               ptr = name_ptr;
+               ptr = name_ptr-1;
+               replace_string(ptr, end_ptr, ent->value);
+               if ((ent->value[0] == '@') &&
+                   (strlen(replace_name) == strlen(ent->value)-2) &&
+                   !strncmp(replace_name, ent->value+1,
+                            strlen(ent->value)-2))
+                       /* avoid an infinite loop */
+                       ptr += strlen(ent->value);
+       }
+       /*
+        * Now do a second pass to expand ${FOO}
+        */
+       ptr = line;
+       while (ptr) {
+               name_ptr = strchr(ptr, '$');
+               if (!name_ptr)
+                       break;  /* No more */
+               if (*(++name_ptr) != '{') {
+                       ptr = name_ptr;
+                       continue;
+               }
+               name_ptr++;
+               end_ptr = strchr(name_ptr, '}');
+               if (!end_ptr)
+                       break;
+               len = end_ptr - name_ptr;
+               replace_name = get_subst_symbol(name_ptr, len, '$');
+               if (!replace_name) {
+                       ptr = name_ptr;
+                       continue;
+               }
+               ent = fetch_subst_entry(replace_name);
+               if (!ent) {
+                       ptr = end_ptr + 1;
+                       continue;
+               }
+#if 0
+               fprintf(stderr, "Replace name = '%s' with '%s'\n",
+                      replace_name, ent->value);
+#endif
+               ptr = name_ptr-2;
+               replace_string(ptr, end_ptr, ent->value);
        }
 }
 
@@ -144,7 +242,7 @@ static void parse_config_file(FILE *f)
                /*
                 * Ignore future extensions
                 */
-               if (*ptr == '$')
+               if (*ptr == '@')
                        continue;
                /*
                 * Parse substitutions
@@ -157,7 +255,7 @@ static void parse_config_file(FILE *f)
                        if (!isspace(*cp))
                                break;
 #if 0
-               printf("Substitute: '%s' for '%s'\n", ptr, cp);
+               printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
 #endif
                add_subst(ptr, cp);
        }
@@ -168,20 +266,22 @@ static void parse_config_file(FILE *f)
  */
 static int compare_file(const char *outfn, const char *newfn)
 {
-       FILE    *old, *new;
+       FILE    *old_f, *new_f;
        char    oldbuf[2048], newbuf[2048], *oldcp, *newcp;
        int     retval;
 
-       old = fopen(outfn, "r");
-       if (!old)
+       old_f = fopen(outfn, "r");
+       if (!old_f)
                return 0;
-       new = fopen(newfn, "r");
-       if (!new)
+       new_f = fopen(newfn, "r");
+       if (!new_f) {
+               fclose(old_f);
                return 0;
+       }
 
        while (1) {
-               oldcp = fgets(oldbuf, sizeof(oldbuf), old);
-               newcp = fgets(newbuf, sizeof(newbuf), new);
+               oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
+               newcp = fgets(newbuf, sizeof(newbuf), new_f);
                if (!oldcp && !newcp) {
                        retval = 1;
                        break;
@@ -191,10 +291,11 @@ static int compare_file(const char *outfn, const char *newfn)
                        break;
                }
        }
+       fclose(old_f);
+       fclose(new_f);
        return retval;
 }
 
-       
 
 
 int main(int argc, char **argv)
@@ -204,8 +305,11 @@ int main(int argc, char **argv)
        FILE    *in, *out;
        char    *outfn = NULL, *newfn = NULL;
        int     verbose = 0;
-       
-       while ((c = getopt (argc, argv, "f:v")) != EOF) {
+       int     adjust_timestamp = 0;
+       struct stat stbuf;
+       struct utimbuf ut;
+
+       while ((c = getopt (argc, argv, "f:tv")) != EOF) {
                switch (c) {
                case 'f':
                        in = fopen(optarg, "r");
@@ -216,11 +320,14 @@ int main(int argc, char **argv)
                        parse_config_file(in);
                        fclose(in);
                        break;
+               case 't':
+                       adjust_timestamp++;
+                       break;
                case 'v':
                        verbose++;
                        break;
                default:
-                       fprintf(stderr, "%s: [-f config-file] [file]\n", 
+                       fprintf(stderr, "%s: [-f config-file] [file]\n",
                                argv[0]);
                        break;
                }
@@ -234,10 +341,10 @@ int main(int argc, char **argv)
                optind++;
        } else
                in = stdin;
-       
+
        if (optind < argc) {
                outfn = argv[optind];
-               newfn = malloc(strlen(outfn)+20);
+               newfn = (char *) malloc(strlen(outfn)+20);
                if (!newfn) {
                        fprintf(stderr, "Memory error!  Exiting.\n");
                        exit(1);
@@ -253,7 +360,7 @@ int main(int argc, char **argv)
                out = stdout;
                outfn = 0;
        }
-                       
+
        while (!feof(in)) {
                if (fgets(line, sizeof(line), in) == NULL)
                        break;
@@ -263,15 +370,29 @@ int main(int argc, char **argv)
        fclose(in);
        fclose(out);
        if (outfn) {
+               struct stat st;
                if (compare_file(outfn, newfn)) {
                        if (verbose)
                                printf("No change, keeping %s.\n", outfn);
+                       if (adjust_timestamp) {
+                               if (stat(outfn, &stbuf) == 0) {
+                                       if (verbose)
+                                               printf("Updating modtime for %s\n", outfn);
+                                       ut.actime = stbuf.st_atime;
+                                       ut.modtime = time(0);
+                                       if (utime(outfn, &ut) < 0)
+                                               perror("utime");
+                               }
+                       }
                        unlink(newfn);
                } else {
                        if (verbose)
                                printf("Creating or replacing %s.\n", outfn);
                        rename(newfn, outfn);
                }
+               /* set read-only to alert user it is a generated file */
+               if (stat(outfn, &st) == 0)
+                       chmod(outfn, st.st_mode & ~0222);
        }
        return (0);
 }