2 * subst.c --- substitution program
4 * Subst is used as a quicky program to do @ substitutions
17 #ifdef HAVE_SYS_TIME_H
20 #include <sys/types.h>
37 struct subst_entry *next;
40 static struct subst_entry *subst_table = 0;
42 static int add_subst(char *name, char *value)
44 struct subst_entry *ent = 0;
46 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
49 ent->name = (char *) malloc(strlen(name)+1);
52 ent->value = (char *) malloc(strlen(value)+1);
55 strcpy(ent->name, name);
56 strcpy(ent->value, value);
57 ent->next = subst_table;
68 static struct subst_entry *fetch_subst_entry(char *name)
70 struct subst_entry *ent;
72 for (ent = subst_table; ent; ent = ent->next) {
73 if (strcmp(name, ent->name) == 0)
80 * Given the starting and ending position of the replacement name,
81 * check to see if it is valid, and pull it out if it is.
83 static char *get_subst_symbol(const char *begin, size_t len, char prefix)
85 static char replace_name[128];
92 if (len > sizeof(replace_name)-2)
94 memcpy(start, begin, len);
98 * The substitution variable must all be in the of [0-9A-Za-z_].
99 * If it isn't, this must be an invalid symbol name.
101 for (cp = start; *cp; cp++) {
102 if (!(*cp >= 'a' && *cp <= 'z') &&
103 !(*cp >= 'A' && *cp <= 'Z') &&
104 !(*cp >= '0' && *cp <= '9') &&
108 return (replace_name);
111 static void replace_string(char *begin, char *end, char *newstr)
113 int replace_len, len;
115 replace_len = strlen(newstr);
117 if (replace_len == 0)
118 memmove(begin, end+1, strlen(end)+1);
119 else if (replace_len != len+1)
120 memmove(end+(replace_len-len-1), end,
122 memcpy(begin, newstr, replace_len);
125 static void substitute_line(char *line)
127 char *ptr, *name_ptr, *end_ptr;
128 struct subst_entry *ent;
133 * Expand all @FOO@ substitutions
137 name_ptr = strchr(ptr, '@');
140 if (*(++name_ptr) == '@') {
142 * Handle tytso@@mit.edu --> tytso@mit.edu
144 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
148 end_ptr = strchr(name_ptr, '@');
151 len = end_ptr - name_ptr;
152 replace_name = get_subst_symbol(name_ptr, len, 0);
157 ent = fetch_subst_entry(replace_name);
159 fprintf(stderr, "Unfound expansion: '%s'\n",
165 fprintf(stderr, "Replace name = '%s' with '%s'\n",
166 replace_name, ent->value);
169 replace_string(ptr, end_ptr, ent->value);
170 if ((ent->value[0] == '@') &&
171 (strlen(replace_name) == strlen(ent->value)-2) &&
172 !strncmp(replace_name, ent->value+1,
173 strlen(ent->value)-2))
174 /* avoid an infinite loop */
175 ptr += strlen(ent->value);
178 * Now do a second pass to expand ${FOO}
182 name_ptr = strchr(ptr, '$');
185 if (*(++name_ptr) != '{') {
190 end_ptr = strchr(name_ptr, '}');
193 len = end_ptr - name_ptr;
194 replace_name = get_subst_symbol(name_ptr, len, '$');
199 ent = fetch_subst_entry(replace_name);
205 fprintf(stderr, "Replace name = '%s' with '%s'\n",
206 replace_name, ent->value);
209 replace_string(ptr, end_ptr, ent->value);
213 static void parse_config_file(FILE *f)
219 memset(line, 0, sizeof(line));
220 if (fgets(line, sizeof(line), f) == NULL)
223 * Strip newlines and comments.
225 cp = strchr(line, '\n');
228 cp = strchr(line, '#');
232 * Skip trailing and leading whitespace
234 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
235 if (*cp == ' ' || *cp == '\t')
241 while (*cp && isspace(*cp))
250 * Ignore future extensions
255 * Parse substitutions
257 for (cp = ptr; *cp; cp++)
261 for (cp++; *cp; cp++)
265 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
272 * Return 0 if the files are different, 1 if the files are the same.
274 static int compare_file(FILE *old_f, FILE *new_f)
276 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
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)) {
296 int main(int argc, char **argv)
301 FILE *in, *out, *old = NULL;
302 char *outfn = NULL, *newfn = NULL;
304 int adjust_timestamp = 0;
307 struct timeval tv[2];
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 fd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0444);
356 out = fdopen(fd, "w+");
362 fd = open(outfn, O_RDONLY);
364 /* save the original atime, if possible */
365 if (fstat(fd, &stbuf) == 0) {
366 #if HAVE_STRUCT_STAT_ST_ATIM
367 tv[0].tv_sec = stbuf.st_atim.tv_sec;
368 tv[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
370 tv[0].tv_sec = stbuf.st_atime;
375 old = fdopen(fd, "r");
385 if (fgets(line, sizeof(line), in) == NULL)
387 substitute_line(line);
394 if (old && compare_file(old, out)) {
396 printf("No change, keeping %s.\n", outfn);
397 if (adjust_timestamp) {
399 printf("Updating modtime for %s\n", outfn);
400 if (gettimeofday(&tv[1], NULL) < 0) {
401 perror("gettimeofday");
407 printf("Using original atime\n");
409 if (futimes(fileno(old), tv) < 0)
412 if (utimes(outfn, tv) < 0)
417 if (unlink(newfn) < 0)
421 printf("Creating or replacing %s.\n", outfn);
426 if (rename(newfn, outfn) < 0) {