2 * subst.c --- substitution program
4 * Subst is used as a quicky program to do @ substitutions
17 #include <sys/types.h>
34 struct subst_entry *next;
37 static struct subst_entry *subst_table = 0;
39 static int add_subst(char *name, char *value)
41 struct subst_entry *ent = 0;
43 ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
46 ent->name = (char *) malloc(strlen(name)+1);
49 ent->value = (char *) malloc(strlen(value)+1);
52 strcpy(ent->name, name);
53 strcpy(ent->value, value);
54 ent->next = subst_table;
65 static struct subst_entry *fetch_subst_entry(char *name)
67 struct subst_entry *ent;
69 for (ent = subst_table; ent; ent = ent->next) {
70 if (strcmp(name, ent->name) == 0)
77 * Given the starting and ending position of the replacement name,
78 * check to see if it is valid, and pull it out if it is.
80 static char *get_subst_symbol(const char *begin, size_t len, char prefix)
82 static char replace_name[128];
89 if (len > sizeof(replace_name)-2)
91 memcpy(start, begin, len);
95 * The substitution variable must all be in the of [0-9A-Za-z_].
96 * If it isn't, this must be an invalid symbol name.
98 for (cp = start; *cp; cp++) {
99 if (!(*cp >= 'a' && *cp <= 'z') &&
100 !(*cp >= 'A' && *cp <= 'Z') &&
101 !(*cp >= '0' && *cp <= '9') &&
105 return (replace_name);
108 static void replace_string(char *begin, char *end, char *newstr)
110 int replace_len, len;
112 replace_len = strlen(newstr);
114 if (replace_len == 0)
115 memmove(begin, end+1, strlen(end)+1);
116 else if (replace_len != len+1)
117 memmove(end+(replace_len-len-1), end,
119 memcpy(begin, newstr, replace_len);
122 static void substitute_line(char *line)
124 char *ptr, *name_ptr, *end_ptr;
125 struct subst_entry *ent;
130 * Expand all @FOO@ substitutions
134 name_ptr = strchr(ptr, '@');
137 if (*(++name_ptr) == '@') {
139 * Handle tytso@@mit.edu --> tytso@mit.edu
141 memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1);
145 end_ptr = strchr(name_ptr, '@');
148 len = end_ptr - name_ptr;
149 replace_name = get_subst_symbol(name_ptr, len, 0);
154 ent = fetch_subst_entry(replace_name);
156 fprintf(stderr, "Unfound expansion: '%s'\n",
162 fprintf(stderr, "Replace name = '%s' with '%s'\n",
163 replace_name, ent->value);
166 replace_string(ptr, end_ptr, ent->value);
167 if ((ent->value[0] == '@') &&
168 (strlen(replace_name) == strlen(ent->value)-2) &&
169 !strncmp(replace_name, ent->value+1,
170 strlen(ent->value)-2))
171 /* avoid an infinite loop */
172 ptr += strlen(ent->value);
175 * Now do a second pass to expand ${FOO}
179 name_ptr = strchr(ptr, '$');
182 if (*(++name_ptr) != '{') {
187 end_ptr = strchr(name_ptr, '}');
190 len = end_ptr - name_ptr;
191 replace_name = get_subst_symbol(name_ptr, len, '$');
196 ent = fetch_subst_entry(replace_name);
202 fprintf(stderr, "Replace name = '%s' with '%s'\n",
203 replace_name, ent->value);
206 replace_string(ptr, end_ptr, ent->value);
210 static void parse_config_file(FILE *f)
216 memset(line, 0, sizeof(line));
217 if (fgets(line, sizeof(line), f) == NULL)
220 * Strip newlines and comments.
222 cp = strchr(line, '\n');
225 cp = strchr(line, '#');
229 * Skip trailing and leading whitespace
231 for (cp = line + strlen(line) - 1; cp >= line; cp--) {
232 if (*cp == ' ' || *cp == '\t')
238 while (*cp && isspace(*cp))
247 * Ignore future extensions
252 * Parse substitutions
254 for (cp = ptr; *cp; cp++)
258 for (cp++; *cp; cp++)
262 printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>");
269 * Return 0 if the files are different, 1 if the files are the same.
271 static int compare_file(FILE *old_f, FILE *new_f)
273 char oldbuf[2048], newbuf[2048], *oldcp, *newcp;
277 oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
278 newcp = fgets(newbuf, sizeof(newbuf), new_f);
279 if (!oldcp && !newcp) {
283 if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) {
293 int main(int argc, char **argv)
298 FILE *in, *out, *old = NULL;
299 char *outfn = NULL, *newfn = NULL;
301 int adjust_timestamp = 0;
304 struct timeval tv[2];
306 while ((c = getopt (argc, argv, "f:tv")) != EOF) {
309 in = fopen(optarg, "r");
314 parse_config_file(in);
324 fprintf(stderr, "%s: [-f config-file] [file]\n",
330 in = fopen(argv[optind], "r");
332 perror(argv[optind]);
340 outfn = argv[optind];
341 newfn = (char *) malloc(strlen(outfn)+20);
343 fprintf(stderr, "Memory error! Exiting.\n");
346 strcpy(newfn, outfn);
347 strcat(newfn, ".new");
348 fd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0444);
353 out = fdopen(fd, "w+");
359 fd = open(outfn, O_RDONLY);
361 /* save the original atime, if possible */
362 if (fstat(fd, &stbuf) == 0) {
363 #if HAVE_STRUCT_STAT_ST_ATIM
364 tv[0].tv_sec = stbuf.st_atim.tv_sec;
365 tv[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
367 tv[0].tv_sec = stbuf.st_atime;
372 old = fdopen(fd, "r");
382 if (fgets(line, sizeof(line), in) == NULL)
384 substitute_line(line);
391 if (old && compare_file(old, out)) {
393 printf("No change, keeping %s.\n", outfn);
394 if (adjust_timestamp) {
396 printf("Updating modtime for %s\n", outfn);
397 if (gettimeofday(&tv[1], NULL) < 0) {
398 perror("gettimeofday");
404 printf("Using original atime\n");
406 if (futimes(fileno(old), tv) < 0)
409 if (utimes(outfn, tv) < 0)
414 if (unlink(newfn) < 0)
418 printf("Creating or replacing %s.\n", outfn);
423 if (rename(newfn, outfn) < 0) {