2 * subst.c --- substitution program
4 * Subst is used as a quicky program to do @ substitutions
26 struct subst_entry *next;
29 struct subst_entry *subst_table = 0;
31 static int add_subst(char *name, char *value)
33 struct subst_entry *ent = 0;
37 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
40 ent->name = (char *) malloc(strlen(name)+1);
43 ent->value = (char *) malloc(strlen(value)+1);
46 strcpy(ent->name, name);
47 strcpy(ent->value, value);
48 ent->next = subst_table;
62 static struct subst_entry *fetch_subst_entry(char *name)
64 struct subst_entry *ent;
66 for (ent = subst_table; ent; ent = ent->next) {
67 if (strcmp(name, ent->name) == 0)
74 * Given the starting and ending position of the replacement name,
75 * check to see if it is valid, and pull it out if it is.
77 static char *get_subst_symbol(const char *begin, int len, char prefix)
79 static char replace_name[128];
86 if (len > sizeof(replace_name)-2)
88 memcpy(start, begin, len);
92 * The substitution variable must all be in the of [0-9A-Za-z_].
93 * If it isn't, this must be an invalid symbol name.
95 for (cp = start; *cp; cp++) {
96 if (!(*cp >= 'a' && *cp <= 'z') &&
97 !(*cp >= 'A' && *cp <= 'Z') &&
98 !(*cp >= '0' && *cp <= '9') &&
102 return (replace_name);
105 static void replace_string(char *begin, char *end, char *newstr)
107 int replace_len, len;
109 replace_len = strlen(newstr);
111 if (replace_len != len+1)
112 memmove(end+(replace_len-len-1), end,
114 memcpy(begin, newstr, replace_len);
117 static void substitute_line(char *line)
119 char *ptr, *name_ptr, *end_ptr;
120 struct subst_entry *ent;
125 * Expand all @FOO@ substitutions
129 name_ptr = strchr(ptr, '@');
132 if (*(++name_ptr) == '@') {
134 * Handle tytso@@mit.edu --> tytso@mit.edu
136 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
140 end_ptr = strchr(name_ptr, '@');
143 len = end_ptr - name_ptr;
144 replace_name = get_subst_symbol(name_ptr, len, 0);
149 ent = fetch_subst_entry(replace_name);
151 fprintf(stderr, "Unfound expansion: '%s'\n",
157 fprintf(stderr, "Replace name = '%s' with '%s'\n",
158 replace_name, ent->value);
161 replace_string(ptr, end_ptr, ent->value);
164 * Now do a second pass to expand ${FOO}
168 name_ptr = strchr(ptr, '$');
171 if (*(++name_ptr) != '{') {
176 end_ptr = strchr(name_ptr, '}');
179 len = end_ptr - name_ptr;
180 replace_name = get_subst_symbol(name_ptr, len, '$');
185 ent = fetch_subst_entry(replace_name);
191 fprintf(stderr, "Replace name = '%s' with '%s'\n",
192 replace_name, ent->value);
195 replace_string(ptr, end_ptr, ent->value);
199 static void parse_config_file(FILE *f)
205 memset(line, 0, sizeof(line));
206 if (fgets(line, sizeof(line), f) == NULL)
209 * Strip newlines and comments.
211 cp = strchr(line, '\n');
214 cp = strchr(line, '#');
218 * Skip trailing and leading whitespace
220 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
221 if (*cp == ' ' || *cp == '\t')
227 while (*cp && isspace(*cp))
236 * Ignore future extensions
241 * Parse substitutions
243 for (cp = ptr; *cp; cp++)
247 for (cp++; *cp; cp++)
251 printf("Substitute: '%s' for '%s'\n", ptr, cp);
258 * Return 0 if the files are different, 1 if the files are the same.
260 static int compare_file(const char *outfn, const char *newfn)
263 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
266 old_f = fopen(outfn, "r");
269 new_f = fopen(newfn, "r");
274 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
275 newcp = fgets(newbuf, sizeof(newbuf), new_f);
276 if (!oldcp && !newcp) {
280 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
293 int main(int argc, char **argv)
298 char *outfn = NULL, *newfn = NULL;
301 while ((c = getopt (argc, argv, "f:v")) != EOF) {
304 in = fopen(optarg, "r");
309 parse_config_file(in);
316 fprintf(stderr, "%s: [-f config-file] [file]\n",
322 in = fopen(argv[optind], "r");
324 perror(argv[optind]);
332 outfn = argv[optind];
333 newfn = (char *) malloc(strlen(outfn)+20);
335 fprintf(stderr, "Memory error! Exiting.\n");
338 strcpy(newfn, outfn);
339 strcat(newfn, ".new");
340 out = fopen(newfn, "w");
351 if (fgets(line, sizeof(line), in) == NULL)
353 substitute_line(line);
359 if (compare_file(outfn, newfn)) {
361 printf("No change, keeping %s.\n", outfn);
365 printf("Creating or replacing %s.\n", outfn);
366 rename(newfn, outfn);