2 * subst.c --- substitution program
4 * Subst is used as a quicky program to do @ substitutions
11 #define HAVE_SYS_TIME_H
19 #ifdef HAVE_SYS_TIME_H
22 #include <sys/types.h>
39 struct subst_entry *next;
42 static struct subst_entry *subst_table = 0;
44 static int add_subst(char *name, char *value)
46 struct subst_entry *ent = 0;
48 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
51 ent->name = (char *) malloc(strlen(name)+1);
54 ent->value = (char *) malloc(strlen(value)+1);
57 strcpy(ent->name, name);
58 strcpy(ent->value, value);
59 ent->next = subst_table;
70 static struct subst_entry *fetch_subst_entry(char *name)
72 struct subst_entry *ent;
74 for (ent = subst_table; ent; ent = ent->next) {
75 if (strcmp(name, ent->name) == 0)
82 * Given the starting and ending position of the replacement name,
83 * check to see if it is valid, and pull it out if it is.
85 static char *get_subst_symbol(const char *begin, size_t len, char prefix)
87 static char replace_name[128];
94 if (len > sizeof(replace_name)-2)
96 memcpy(start, begin, len);
100 * The substitution variable must all be in the of [0-9A-Za-z_].
101 * If it isn't, this must be an invalid symbol name.
103 for (cp = start; *cp; cp++) {
104 if (!(*cp >= 'a' && *cp <= 'z') &&
105 !(*cp >= 'A' && *cp <= 'Z') &&
106 !(*cp >= '0' && *cp <= '9') &&
110 return (replace_name);
113 static void replace_string(char *begin, char *end, char *newstr)
115 int replace_len, len;
117 replace_len = strlen(newstr);
119 if (replace_len == 0)
120 memmove(begin, end+1, strlen(end)+1);
121 else if (replace_len != len+1)
122 memmove(end+(replace_len-len-1), end,
124 memcpy(begin, newstr, replace_len);
127 static void substitute_line(char *line)
129 char *ptr, *name_ptr, *end_ptr;
130 struct subst_entry *ent;
135 * Expand all @FOO@ substitutions
139 name_ptr = strchr(ptr, '@');
142 if (*(++name_ptr) == '@') {
144 * Handle tytso@@mit.edu --> tytso@mit.edu
146 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
150 end_ptr = strchr(name_ptr, '@');
153 len = end_ptr - name_ptr;
154 replace_name = get_subst_symbol(name_ptr, len, 0);
159 ent = fetch_subst_entry(replace_name);
161 fprintf(stderr, "Unfound expansion: '%s'\n",
167 fprintf(stderr, "Replace name = '%s' with '%s'\n",
168 replace_name, ent->value);
171 replace_string(ptr, end_ptr, ent->value);
172 if ((ent->value[0] == '@') &&
173 (strlen(replace_name) == strlen(ent->value)-2) &&
174 !strncmp(replace_name, ent->value+1,
175 strlen(ent->value)-2))
176 /* avoid an infinite loop */
177 ptr += strlen(ent->value);
180 * Now do a second pass to expand ${FOO}
184 name_ptr = strchr(ptr, '$');
187 if (*(++name_ptr) != '{') {
192 end_ptr = strchr(name_ptr, '}');
195 len = end_ptr - name_ptr;
196 replace_name = get_subst_symbol(name_ptr, len, '$');
201 ent = fetch_subst_entry(replace_name);
207 fprintf(stderr, "Replace name = '%s' with '%s'\n",
208 replace_name, ent->value);
211 replace_string(ptr, end_ptr, ent->value);
215 static void parse_config_file(FILE *f)
221 memset(line, 0, sizeof(line));
222 if (fgets(line, sizeof(line), f) == NULL)
225 * Strip newlines and comments.
227 cp = strchr(line, '\n');
230 cp = strchr(line, '#');
234 * Skip trailing and leading whitespace
236 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
237 if (*cp == ' ' || *cp == '\t')
243 while (*cp && isspace(*cp))
252 * Ignore future extensions
257 * Parse substitutions
259 for (cp = ptr; *cp; cp++)
263 for (cp++; *cp; cp++)
267 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
274 * Return 0 if the files are different, 1 if the files are the same.
276 static int compare_file(FILE *old_f, FILE *new_f)
278 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
282 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
283 newcp = fgets(newbuf, sizeof(newbuf), new_f);
284 if (!oldcp && !newcp) {
288 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
296 void set_utimes(const char *filename, int fd, const struct timeval times[2])
299 if (futimes(fd, times) < 0)
302 if (utimes(filename, times) < 0)
307 ut.actime = times[0].tv_sec;
308 ut.modtime = times[1].tv_sec;
309 if (utime(filename, &ut) < 0)
315 int main(int argc, char **argv)
320 FILE *in, *out, *old = NULL;
321 char *outfn = NULL, *newfn = NULL;
323 int adjust_timestamp = 0;
326 struct timeval tv[2];
328 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
331 in = fopen(optarg, "r");
336 parse_config_file(in);
346 fprintf(stderr, "%s: [-f config-file] [file]\n",
352 in = fopen(argv[optind], "r");
354 perror(argv[optind]);
362 outfn = argv[optind];
363 newfn = (char *) malloc(strlen(outfn)+20);
365 fprintf(stderr, "Memory error! Exiting.\n");
368 strcpy(newfn, outfn);
369 strcat(newfn, ".new");
370 fd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0444);
375 out = fdopen(fd, "w+");
381 fd = open(outfn, O_RDONLY);
383 /* save the original atime, if possible */
384 if (fstat(fd, &stbuf) == 0) {
385 #if HAVE_STRUCT_STAT_ST_ATIM
386 tv[0].tv_sec = stbuf.st_atim.tv_sec;
387 tv[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
389 tv[0].tv_sec = stbuf.st_atime;
394 old = fdopen(fd, "r");
404 if (fgets(line, sizeof(line), in) == NULL)
406 substitute_line(line);
413 if (old && compare_file(old, out)) {
415 printf("No change, keeping %s.\n", outfn);
416 if (adjust_timestamp) {
418 printf("Updating modtime for %s\n", outfn);
419 if (gettimeofday(&tv[1], NULL) < 0) {
420 perror("gettimeofday");
426 printf("Using original atime\n");
427 set_utimes(outfn, fileno(old), tv);
430 if (unlink(newfn) < 0)
434 printf("Creating or replacing %s.\n", outfn);
439 if (rename(newfn, outfn) < 0) {