2 * subst.c --- substitution program
4 * Subst is used as a quickie program to do @ substitutions
11 #define HAVE_SYS_STAT_H
12 #define HAVE_SYS_TIME_H
20 #ifdef HAVE_SYS_TIME_H
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
26 #ifdef HAVE_SYS_STAT_H
32 #ifdef HAVE_SYS_TIME_H
47 struct subst_entry *next;
50 static struct subst_entry *subst_table = 0;
52 static int add_subst(char *name, char *value)
54 struct subst_entry *ent = 0;
56 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
59 ent->name = (char *) malloc(strlen(name)+1);
62 ent->value = (char *) malloc(strlen(value)+1);
65 strcpy(ent->name, name);
66 strcpy(ent->value, value);
67 ent->next = subst_table;
78 static struct subst_entry *fetch_subst_entry(char *name)
80 struct subst_entry *ent;
82 for (ent = subst_table; ent; ent = ent->next) {
83 if (strcmp(name, ent->name) == 0)
90 * Given the starting and ending position of the replacement name,
91 * check to see if it is valid, and pull it out if it is.
93 static char *get_subst_symbol(const char *begin, size_t len, char prefix)
95 static char replace_name[128];
102 if (len > sizeof(replace_name)-2)
104 memcpy(start, begin, len);
108 * The substitution variable must all be in the of [0-9A-Za-z_].
109 * If it isn't, this must be an invalid symbol name.
111 for (cp = start; *cp; cp++) {
112 if (!(*cp >= 'a' && *cp <= 'z') &&
113 !(*cp >= 'A' && *cp <= 'Z') &&
114 !(*cp >= '0' && *cp <= '9') &&
118 return (replace_name);
121 static void replace_string(char *begin, char *end, char *newstr)
123 int replace_len, len;
125 replace_len = strlen(newstr);
127 if (replace_len == 0)
128 memmove(begin, end+1, strlen(end)+1);
129 else if (replace_len != len+1)
130 memmove(end+(replace_len-len-1), end,
132 memcpy(begin, newstr, replace_len);
135 static void substitute_line(char *line)
137 char *ptr, *name_ptr, *end_ptr;
138 struct subst_entry *ent;
143 * Expand all @FOO@ substitutions
147 name_ptr = strchr(ptr, '@');
150 if (*(++name_ptr) == '@') {
152 * Handle tytso@@mit.edu --> tytso@mit.edu
154 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
158 end_ptr = strchr(name_ptr, '@');
161 len = end_ptr - name_ptr;
162 replace_name = get_subst_symbol(name_ptr, len, 0);
167 ent = fetch_subst_entry(replace_name);
169 fprintf(stderr, "Unfound expansion: '%s'\n",
175 fprintf(stderr, "Replace name = '%s' with '%s'\n",
176 replace_name, ent->value);
179 replace_string(ptr, end_ptr, ent->value);
180 if ((ent->value[0] == '@') &&
181 (strlen(replace_name) == strlen(ent->value)-2) &&
182 !strncmp(replace_name, ent->value+1,
183 strlen(ent->value)-2))
184 /* avoid an infinite loop */
185 ptr += strlen(ent->value);
188 * Now do a second pass to expand ${FOO}
192 name_ptr = strchr(ptr, '$');
195 if (*(++name_ptr) != '{') {
200 end_ptr = strchr(name_ptr, '}');
203 len = end_ptr - name_ptr;
204 replace_name = get_subst_symbol(name_ptr, len, '$');
209 ent = fetch_subst_entry(replace_name);
215 fprintf(stderr, "Replace name = '%s' with '%s'\n",
216 replace_name, ent->value);
219 replace_string(ptr, end_ptr, ent->value);
223 static void parse_config_file(FILE *f)
229 memset(line, 0, sizeof(line));
230 if (fgets(line, sizeof(line), f) == NULL)
233 * Strip newlines and comments.
235 cp = strchr(line, '\n');
238 cp = strchr(line, '#');
242 * Skip trailing and leading whitespace
244 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
245 if (*cp == ' ' || *cp == '\t')
251 while (*cp && isspace(*cp))
260 * Ignore future extensions
265 * Parse substitutions
267 for (cp = ptr; *cp; cp++)
271 for (cp++; *cp; cp++)
275 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
282 * Return 0 if the files are different, 1 if the files are the same.
284 static int compare_file(FILE *old_f, FILE *new_f)
286 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
290 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
291 newcp = fgets(newbuf, sizeof(newbuf), new_f);
292 if (!oldcp && !newcp) {
296 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
304 void set_utimes(const char *filename, int fd, const struct timeval times[2])
307 if (futimes(fd, times) < 0)
310 if (utimes(filename, times) < 0)
315 ut.actime = times[0].tv_sec;
316 ut.modtime = times[1].tv_sec;
317 if (utime(filename, &ut) < 0)
323 int main(int argc, char **argv)
328 FILE *in, *out, *old = NULL;
329 char *outfn = NULL, *newfn = NULL;
331 int adjust_timestamp = 0;
334 struct timeval tv[2];
336 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
339 in = fopen(optarg, "r");
344 parse_config_file(in);
354 fprintf(stderr, "%s: [-f config-file] [file]\n",
360 in = fopen(argv[optind], "r");
362 perror(argv[optind]);
370 outfn = argv[optind];
371 newfn = (char *) malloc(strlen(outfn)+20);
373 fprintf(stderr, "Memory error! Exiting.\n");
376 strcpy(newfn, outfn);
377 strcat(newfn, ".new");
378 ofd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0644);
383 out = fdopen(ofd, "w+");
389 fd = open(outfn, O_RDONLY);
391 /* save the original atime, if possible */
392 if (fstat(fd, &stbuf) == 0) {
393 #if HAVE_STRUCT_STAT_ST_ATIM
394 tv[0].tv_sec = stbuf.st_atim.tv_sec;
395 tv[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
397 tv[0].tv_sec = stbuf.st_atime;
402 old = fdopen(fd, "r");
412 if (fgets(line, sizeof(line), in) == NULL)
414 substitute_line(line);
421 if (old && compare_file(old, out)) {
423 printf("No change, keeping %s.\n", outfn);
424 if (adjust_timestamp) {
426 printf("Updating modtime for %s\n", outfn);
427 if (gettimeofday(&tv[1], NULL) < 0) {
428 perror("gettimeofday");
434 printf("Using original atime\n");
435 set_utimes(outfn, fileno(old), tv);
439 (void) fchmod(ofd, 0444);
442 if (unlink(newfn) < 0)
446 printf("Creating or replacing %s.\n", outfn);
449 (void) fchmod(ofd, 0444);
455 if (rename(newfn, outfn) < 0) {