2 * subst.c --- substitution program
4 * Subst is used as a quicky program to do @ substitutions
15 #include <sys/types.h>
20 #ifdef HAVE_SYS_TIME_H
35 struct subst_entry *next;
38 static struct subst_entry *subst_table = 0;
40 static int add_subst(char *name, char *value)
42 struct subst_entry *ent = 0;
44 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
47 ent->name = (char *) malloc(strlen(name)+1);
50 ent->value = (char *) malloc(strlen(value)+1);
53 strcpy(ent->name, name);
54 strcpy(ent->value, value);
55 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);
168 if ((ent->value[0] == '@') &&
169 (strlen(replace_name) == strlen(ent->value)-2) &&
170 !strncmp(replace_name, ent->value+1,
171 strlen(ent->value)-2))
172 /* avoid an infinite loop */
173 ptr += strlen(ent->value);
176 * Now do a second pass to expand ${FOO}
180 name_ptr = strchr(ptr, '$');
183 if (*(++name_ptr) != '{') {
188 end_ptr = strchr(name_ptr, '}');
191 len = end_ptr - name_ptr;
192 replace_name = get_subst_symbol(name_ptr, len, '$');
197 ent = fetch_subst_entry(replace_name);
203 fprintf(stderr, "Replace name = '%s' with '%s'\n",
204 replace_name, ent->value);
207 replace_string(ptr, end_ptr, ent->value);
211 static void parse_config_file(FILE *f)
217 memset(line, 0, sizeof(line));
218 if (fgets(line, sizeof(line), f) == NULL)
221 * Strip newlines and comments.
223 cp = strchr(line, '\n');
226 cp = strchr(line, '#');
230 * Skip trailing and leading whitespace
232 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
233 if (*cp == ' ' || *cp == '\t')
239 while (*cp && isspace(*cp))
248 * Ignore future extensions
253 * Parse substitutions
255 for (cp = ptr; *cp; cp++)
259 for (cp++; *cp; cp++)
263 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
270 * Return 0 if the files are different, 1 if the files are the same.
272 static int compare_file(FILE *old_f, FILE *new_f)
274 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
278 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
279 newcp = fgets(newbuf, sizeof(newbuf), new_f);
280 if (!oldcp && !newcp) {
284 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
294 int main(int argc, char **argv)
299 FILE *in, *out, *old = NULL;
300 char *outfn = NULL, *newfn = NULL;
302 int adjust_timestamp = 0;
305 struct timeval tv[2];
307 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
310 in = fopen(optarg, "r");
315 parse_config_file(in);
325 fprintf(stderr, "%s: [-f config-file] [file]\n",
331 in = fopen(argv[optind], "r");
333 perror(argv[optind]);
341 outfn = argv[optind];
342 newfn = (char *) malloc(strlen(outfn)+20);
344 fprintf(stderr, "Memory error! Exiting.\n");
347 strcpy(newfn, outfn);
348 strcat(newfn, ".new");
349 fd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0444);
354 out = fdopen(fd, "w+");
360 fd = open(outfn, O_RDONLY);
362 /* save the original atime, if possible */
363 if (fstat(fd, &stbuf) == 0) {
364 #if HAVE_STRUCT_STAT_ST_ATIM
365 tv[0].tv_sec = stbuf.st_atim.tv_sec;
366 tv[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
368 tv[0].tv_sec = stbuf.st_atime;
373 old = fdopen(fd, "r");
383 if (fgets(line, sizeof(line), in) == NULL)
385 substitute_line(line);
392 if (old && compare_file(old, out)) {
394 printf("No change, keeping %s.\n", outfn);
395 if (adjust_timestamp) {
397 printf("Updating modtime for %s\n", outfn);
398 if (gettimeofday(&tv[1], NULL) < 0) {
399 perror("gettimeofday");
405 printf("Using original atime\n");
407 if (futimes(fileno(old), tv) < 0)
410 if (utimes(outfn, tv) < 0)
415 if (unlink(newfn) < 0)
419 printf("Creating or replacing %s.\n", outfn);
424 if (rename(newfn, outfn) < 0) {