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;
66 static struct subst_entry *fetch_subst_entry(char *name)
68 struct subst_entry *ent;
70 for (ent = subst_table; ent; ent = ent->next) {
71 if (strcmp(name, ent->name) == 0)
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.
81 static char *get_subst_symbol(const char *begin, size_t len, char prefix)
83 static char replace_name[128];
90 if (len > sizeof(replace_name)-2)
92 memcpy(start, begin, len);
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.
99 for (cp = start; *cp; cp++) {
100 if (!(*cp >= 'a' && *cp <= 'z') &&
101 !(*cp >= 'A' && *cp <= 'Z') &&
102 !(*cp >= '0' && *cp <= '9') &&
106 return (replace_name);
109 static void replace_string(char *begin, char *end, char *newstr)
111 int replace_len, len;
113 replace_len = strlen(newstr);
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,
120 memcpy(begin, newstr, replace_len);
123 static void substitute_line(char *line)
125 char *ptr, *name_ptr, *end_ptr;
126 struct subst_entry *ent;
131 * Expand all @FOO@ substitutions
135 name_ptr = strchr(ptr, '@');
138 if (*(++name_ptr) == '@') {
140 * Handle tytso@@mit.edu --> tytso@mit.edu
142 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
146 end_ptr = strchr(name_ptr, '@');
149 len = end_ptr - name_ptr;
150 replace_name = get_subst_symbol(name_ptr, len, 0);
155 ent = fetch_subst_entry(replace_name);
157 fprintf(stderr, "Unfound expansion: '%s'\n",
163 fprintf(stderr, "Replace name = '%s' with '%s'\n",
164 replace_name, ent->value);
167 replace_string(ptr, end_ptr, ent->value);
168 if ((ent->value[0] == '@') &&
169 (strlen(replace_name) == strlen(ent->value)-2) &&
170 !strncmp(replace_name, ent->value+1,
171 strlen(ent->value)-2))
172 /* avoid an infinite loop */
173 ptr += strlen(ent->value);
176 * Now do a second pass to expand ${FOO}
180 name_ptr = strchr(ptr, '$');
183 if (*(++name_ptr) != '{') {
188 end_ptr = strchr(name_ptr, '}');
191 len = end_ptr - name_ptr;
192 replace_name = get_subst_symbol(name_ptr, len, '$');
197 ent = fetch_subst_entry(replace_name);
203 fprintf(stderr, "Replace name = '%s' with '%s'\n",
204 replace_name, ent->value);
207 replace_string(ptr, end_ptr, ent->value);
211 static void parse_config_file(FILE *f)
217 memset(line, 0, sizeof(line));
218 if (fgets(line, sizeof(line), f) == NULL)
221 * Strip newlines and comments.
223 cp = strchr(line, '\n');
226 cp = strchr(line, '#');
230 * Skip trailing and leading whitespace
232 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
233 if (*cp == ' ' || *cp == '\t')
239 while (*cp && isspace(*cp))
248 * Ignore future extensions
253 * Parse substitutions
255 for (cp = ptr; *cp; cp++)
259 for (cp++; *cp; cp++)
263 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
270 * Return 0 if the files are different, 1 if the files are the same.
272 static int compare_file(const char *outfn, const char *newfn)
275 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
278 old_f = fopen(outfn, "r");
281 new_f = fopen(newfn, "r");
288 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
289 newcp = fgets(newbuf, sizeof(newbuf), new_f);
290 if (!oldcp && !newcp) {
294 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
306 int main(int argc, char **argv)
311 char *outfn = NULL, *newfn = NULL;
313 int adjust_timestamp = 0;
317 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
320 in = fopen(optarg, "r");
325 parse_config_file(in);
335 fprintf(stderr, "%s: [-f config-file] [file]\n",
341 in = fopen(argv[optind], "r");
343 perror(argv[optind]);
351 outfn = argv[optind];
352 newfn = (char *) malloc(strlen(outfn)+20);
354 fprintf(stderr, "Memory error! Exiting.\n");
357 strcpy(newfn, outfn);
358 strcat(newfn, ".new");
359 out = fopen(newfn, "w");
370 if (fgets(line, sizeof(line), in) == NULL)
372 substitute_line(line);
379 if (compare_file(outfn, newfn)) {
381 printf("No change, keeping %s.\n", outfn);
382 if (adjust_timestamp) {
383 if (stat(outfn, &stbuf) == 0) {
385 printf("Updating modtime for %s\n", outfn);
386 ut.actime = stbuf.st_atime;
387 ut.modtime = time(0);
388 if (utime(outfn, &ut) < 0)
395 printf("Creating or replacing %s.\n", outfn);
396 rename(newfn, outfn);
398 /* set read-only to alert user it is a generated file */
399 if (stat(outfn, &st) == 0)
400 chmod(outfn, st.st_mode & ~0222);