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;
41 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
44 ent->name = (char *) malloc(strlen(name)+1);
47 ent->value = (char *) malloc(strlen(value)+1);
50 strcpy(ent->name, name);
51 strcpy(ent->value, value);
52 ent->next = subst_table;
64 static struct subst_entry *fetch_subst_entry(char *name)
66 struct subst_entry *ent;
68 for (ent = subst_table; ent; ent = ent->next) {
69 if (strcmp(name, ent->name) == 0)
76 * Given the starting and ending position of the replacement name,
77 * check to see if it is valid, and pull it out if it is.
79 static char *get_subst_symbol(const char *begin, size_t len, char prefix)
81 static char replace_name[128];
88 if (len > sizeof(replace_name)-2)
90 memcpy(start, begin, len);
94 * The substitution variable must all be in the of [0-9A-Za-z_].
95 * If it isn't, this must be an invalid symbol name.
97 for (cp = start; *cp; cp++) {
98 if (!(*cp >= 'a' && *cp <= 'z') &&
99 !(*cp >= 'A' && *cp <= 'Z') &&
100 !(*cp >= '0' && *cp <= '9') &&
104 return (replace_name);
107 static void replace_string(char *begin, char *end, char *newstr)
109 int replace_len, len;
111 replace_len = strlen(newstr);
113 if (replace_len == 0)
114 memmove(begin, end+1, strlen(end)+1);
115 else if (replace_len != len+1)
116 memmove(end+(replace_len-len-1), end,
118 memcpy(begin, newstr, replace_len);
121 static void substitute_line(char *line)
123 char *ptr, *name_ptr, *end_ptr;
124 struct subst_entry *ent;
129 * Expand all @FOO@ substitutions
133 name_ptr = strchr(ptr, '@');
136 if (*(++name_ptr) == '@') {
138 * Handle tytso@@mit.edu --> tytso@mit.edu
140 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
144 end_ptr = strchr(name_ptr, '@');
147 len = end_ptr - name_ptr;
148 replace_name = get_subst_symbol(name_ptr, len, 0);
153 ent = fetch_subst_entry(replace_name);
155 fprintf(stderr, "Unfound expansion: '%s'\n",
161 fprintf(stderr, "Replace name = '%s' with '%s'\n",
162 replace_name, ent->value);
165 replace_string(ptr, end_ptr, ent->value);
166 if ((ent->value[0] == '@') &&
167 (strlen(replace_name) == strlen(ent->value)-2) &&
168 !strncmp(replace_name, ent->value+1,
169 strlen(ent->value)-2))
170 /* avoid an infinite loop */
171 ptr += strlen(ent->value);
174 * Now do a second pass to expand ${FOO}
178 name_ptr = strchr(ptr, '$');
181 if (*(++name_ptr) != '{') {
186 end_ptr = strchr(name_ptr, '}');
189 len = end_ptr - name_ptr;
190 replace_name = get_subst_symbol(name_ptr, len, '$');
195 ent = fetch_subst_entry(replace_name);
201 fprintf(stderr, "Replace name = '%s' with '%s'\n",
202 replace_name, ent->value);
205 replace_string(ptr, end_ptr, ent->value);
209 static void parse_config_file(FILE *f)
215 memset(line, 0, sizeof(line));
216 if (fgets(line, sizeof(line), f) == NULL)
219 * Strip newlines and comments.
221 cp = strchr(line, '\n');
224 cp = strchr(line, '#');
228 * Skip trailing and leading whitespace
230 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
231 if (*cp == ' ' || *cp == '\t')
237 while (*cp && isspace(*cp))
246 * Ignore future extensions
251 * Parse substitutions
253 for (cp = ptr; *cp; cp++)
257 for (cp++; *cp; cp++)
261 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
268 * Return 0 if the files are different, 1 if the files are the same.
270 static int compare_file(const char *outfn, const char *newfn)
273 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
276 old_f = fopen(outfn, "r");
279 new_f = fopen(newfn, "r");
286 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
287 newcp = fgets(newbuf, sizeof(newbuf), new_f);
288 if (!oldcp && !newcp) {
292 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
304 int main(int argc, char **argv)
309 char *outfn = NULL, *newfn = NULL;
311 int adjust_timestamp = 0;
315 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
318 in = fopen(optarg, "r");
323 parse_config_file(in);
333 fprintf(stderr, "%s: [-f config-file] [file]\n",
339 in = fopen(argv[optind], "r");
341 perror(argv[optind]);
349 outfn = argv[optind];
350 newfn = (char *) malloc(strlen(outfn)+20);
352 fprintf(stderr, "Memory error! Exiting.\n");
355 strcpy(newfn, outfn);
356 strcat(newfn, ".new");
357 out = fopen(newfn, "w");
368 if (fgets(line, sizeof(line), in) == NULL)
370 substitute_line(line);
377 if (compare_file(outfn, newfn)) {
379 printf("No change, keeping %s.\n", outfn);
380 if (adjust_timestamp) {
381 if (stat(outfn, &stbuf) == 0) {
383 printf("Updating modtime for %s\n", outfn);
384 ut.actime = stbuf.st_atime;
385 ut.modtime = time(0);
386 if (utime(outfn, &ut) < 0)
393 printf("Creating or replacing %s.\n", outfn);
394 rename(newfn, outfn);
396 /* set read-only to alert user it is a generated file */
397 if (stat(outfn, &st) == 0)
398 chmod(outfn, st.st_mode & ~0222);