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);
170 * Now do a second pass to expand ${FOO}
174 name_ptr = strchr(ptr, '$');
177 if (*(++name_ptr) != '{') {
182 end_ptr = strchr(name_ptr, '}');
185 len = end_ptr - name_ptr;
186 replace_name = get_subst_symbol(name_ptr, len, '$');
191 ent = fetch_subst_entry(replace_name);
197 fprintf(stderr, "Replace name = '%s' with '%s'\n",
198 replace_name, ent->value);
201 replace_string(ptr, end_ptr, ent->value);
205 static void parse_config_file(FILE *f)
211 memset(line, 0, sizeof(line));
212 if (fgets(line, sizeof(line), f) == NULL)
215 * Strip newlines and comments.
217 cp = strchr(line, '\n');
220 cp = strchr(line, '#');
224 * Skip trailing and leading whitespace
226 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
227 if (*cp == ' ' || *cp == '\t')
233 while (*cp && isspace(*cp))
242 * Ignore future extensions
247 * Parse substitutions
249 for (cp = ptr; *cp; cp++)
253 for (cp++; *cp; cp++)
257 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
264 * Return 0 if the files are different, 1 if the files are the same.
266 static int compare_file(const char *outfn, const char *newfn)
269 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
272 old_f = fopen(outfn, "r");
275 new_f = fopen(newfn, "r");
280 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
281 newcp = fgets(newbuf, sizeof(newbuf), new_f);
282 if (!oldcp && !newcp) {
286 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
298 int main(int argc, char **argv)
303 char *outfn = NULL, *newfn = NULL;
305 int adjust_timestamp = 0;
309 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
312 in = fopen(optarg, "r");
317 parse_config_file(in);
327 fprintf(stderr, "%s: [-f config-file] [file]\n",
333 in = fopen(argv[optind], "r");
335 perror(argv[optind]);
343 outfn = argv[optind];
344 newfn = (char *) malloc(strlen(outfn)+20);
346 fprintf(stderr, "Memory error! Exiting.\n");
349 strcpy(newfn, outfn);
350 strcat(newfn, ".new");
351 out = fopen(newfn, "w");
362 if (fgets(line, sizeof(line), in) == NULL)
364 substitute_line(line);
371 if (compare_file(outfn, newfn)) {
373 printf("No change, keeping %s.\n", outfn);
374 if (adjust_timestamp) {
375 if (stat(outfn, &stbuf) == 0) {
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)
387 printf("Creating or replacing %s.\n", outfn);
388 rename(newfn, outfn);
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);