2 * subst.c --- substitution program
4 * Subst is used as a quicky program to do @ substitutions
14 #include <sys/types.h>
30 struct subst_entry *next;
33 struct subst_entry *subst_table = 0;
35 static int add_subst(char *name, char *value)
37 struct subst_entry *ent = 0;
39 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
42 ent->name = (char *) malloc(strlen(name)+1);
45 ent->value = (char *) malloc(strlen(value)+1);
48 strcpy(ent->name, name);
49 strcpy(ent->value, value);
50 ent->next = subst_table;
61 static struct subst_entry *fetch_subst_entry(char *name)
63 struct subst_entry *ent;
65 for (ent = subst_table; ent; ent = ent->next) {
66 if (strcmp(name, ent->name) == 0)
73 * Given the starting and ending position of the replacement name,
74 * check to see if it is valid, and pull it out if it is.
76 static char *get_subst_symbol(const char *begin, size_t len, char prefix)
78 static char replace_name[128];
85 if (len > sizeof(replace_name)-2)
87 memcpy(start, begin, len);
91 * The substitution variable must all be in the of [0-9A-Za-z_].
92 * If it isn't, this must be an invalid symbol name.
94 for (cp = start; *cp; cp++) {
95 if (!(*cp >= 'a' && *cp <= 'z') &&
96 !(*cp >= 'A' && *cp <= 'Z') &&
97 !(*cp >= '0' && *cp <= '9') &&
101 return (replace_name);
104 static void replace_string(char *begin, char *end, char *newstr)
106 int replace_len, len;
108 replace_len = strlen(newstr);
110 if (replace_len == 0)
111 memmove(begin, end+1, strlen(end)+1);
112 else if (replace_len != len+1)
113 memmove(end+(replace_len-len-1), end,
115 memcpy(begin, newstr, replace_len);
118 static void substitute_line(char *line)
120 char *ptr, *name_ptr, *end_ptr;
121 struct subst_entry *ent;
126 * Expand all @FOO@ substitutions
130 name_ptr = strchr(ptr, '@');
133 if (*(++name_ptr) == '@') {
135 * Handle tytso@@mit.edu --> tytso@mit.edu
137 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
141 end_ptr = strchr(name_ptr, '@');
144 len = end_ptr - name_ptr;
145 replace_name = get_subst_symbol(name_ptr, len, 0);
150 ent = fetch_subst_entry(replace_name);
152 fprintf(stderr, "Unfound expansion: '%s'\n",
158 fprintf(stderr, "Replace name = '%s' with '%s'\n",
159 replace_name, ent->value);
162 replace_string(ptr, end_ptr, ent->value);
163 if ((ent->value[0] == '@') &&
164 (strlen(replace_name) == strlen(ent->value)-2) &&
165 !strncmp(replace_name, ent->value+1,
166 strlen(ent->value)-2))
167 /* avoid an infinite loop */
168 ptr += strlen(ent->value);
171 * Now do a second pass to expand ${FOO}
175 name_ptr = strchr(ptr, '$');
178 if (*(++name_ptr) != '{') {
183 end_ptr = strchr(name_ptr, '}');
186 len = end_ptr - name_ptr;
187 replace_name = get_subst_symbol(name_ptr, len, '$');
192 ent = fetch_subst_entry(replace_name);
198 fprintf(stderr, "Replace name = '%s' with '%s'\n",
199 replace_name, ent->value);
202 replace_string(ptr, end_ptr, ent->value);
206 static void parse_config_file(FILE *f)
212 memset(line, 0, sizeof(line));
213 if (fgets(line, sizeof(line), f) == NULL)
216 * Strip newlines and comments.
218 cp = strchr(line, '\n');
221 cp = strchr(line, '#');
225 * Skip trailing and leading whitespace
227 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
228 if (*cp == ' ' || *cp == '\t')
234 while (*cp && isspace(*cp))
243 * Ignore future extensions
248 * Parse substitutions
250 for (cp = ptr; *cp; cp++)
254 for (cp++; *cp; cp++)
258 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
265 * Return 0 if the files are different, 1 if the files are the same.
267 static int compare_file(const char *outfn, const char *newfn)
270 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
273 old_f = fopen(outfn, "r");
276 new_f = fopen(newfn, "r");
283 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
284 newcp = fgets(newbuf, sizeof(newbuf), new_f);
285 if (!oldcp && !newcp) {
289 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
301 int main(int argc, char **argv)
306 char *outfn = NULL, *newfn = NULL;
308 int adjust_timestamp = 0;
312 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
315 in = fopen(optarg, "r");
320 parse_config_file(in);
330 fprintf(stderr, "%s: [-f config-file] [file]\n",
336 in = fopen(argv[optind], "r");
338 perror(argv[optind]);
346 outfn = argv[optind];
347 newfn = (char *) malloc(strlen(outfn)+20);
349 fprintf(stderr, "Memory error! Exiting.\n");
352 strcpy(newfn, outfn);
353 strcat(newfn, ".new");
354 out = fopen(newfn, "w");
365 if (fgets(line, sizeof(line), in) == NULL)
367 substitute_line(line);
374 if (compare_file(outfn, newfn)) {
376 printf("No change, keeping %s.\n", outfn);
377 if (adjust_timestamp) {
378 if (stat(outfn, &stbuf) == 0) {
380 printf("Updating modtime for %s\n", outfn);
381 ut.actime = stbuf.st_atime;
382 ut.modtime = time(0);
383 if (utime(outfn, &ut) < 0)
390 printf("Creating or replacing %s.\n", outfn);
391 rename(newfn, outfn);
393 /* set read-only to alert user it is a generated file */
394 if (stat(outfn, &st) == 0)
395 chmod(outfn, st.st_mode & ~0222);