Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * subst.c --- substitution program |
| 3 | * |
| 4 | * Subst is used as a quicky program to do @ substitutions |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <errno.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <unistd.h> |
| 12 | #include <string.h> |
| 13 | #include <ctype.h> |
Theodore Ts'o | dd60705 | 2004-04-03 13:53:46 -0500 | [diff] [blame] | 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
Matthias Andree | f932ed9 | 2004-04-12 20:16:04 +0200 | [diff] [blame] | 16 | #include <time.h> |
Theodore Ts'o | dd60705 | 2004-04-03 13:53:46 -0500 | [diff] [blame] | 17 | #include <utime.h> |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 18 | |
| 19 | #ifdef HAVE_GETOPT_H |
| 20 | #include <getopt.h> |
Theodore Ts'o | 691d335 | 2000-04-03 16:16:46 +0000 | [diff] [blame] | 21 | #else |
| 22 | extern char *optarg; |
| 23 | extern int optind; |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | |
| 27 | struct subst_entry { |
| 28 | char *name; |
| 29 | char *value; |
| 30 | struct subst_entry *next; |
| 31 | }; |
| 32 | |
| 33 | struct subst_entry *subst_table = 0; |
| 34 | |
| 35 | static int add_subst(char *name, char *value) |
| 36 | { |
| 37 | struct subst_entry *ent = 0; |
| 38 | int retval; |
| 39 | |
| 40 | retval = ENOMEM; |
Theodore Ts'o | 45d2161 | 1998-01-19 14:40:24 +0000 | [diff] [blame] | 41 | ent = (struct subst_entry *) malloc(sizeof(struct subst_entry)); |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 42 | if (!ent) |
| 43 | goto fail; |
Theodore Ts'o | 45d2161 | 1998-01-19 14:40:24 +0000 | [diff] [blame] | 44 | ent->name = (char *) malloc(strlen(name)+1); |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 45 | if (!ent->name) |
| 46 | goto fail; |
Theodore Ts'o | 45d2161 | 1998-01-19 14:40:24 +0000 | [diff] [blame] | 47 | ent->value = (char *) malloc(strlen(value)+1); |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 48 | if (!ent->value) |
| 49 | goto fail; |
| 50 | strcpy(ent->name, name); |
| 51 | strcpy(ent->value, value); |
| 52 | ent->next = subst_table; |
| 53 | subst_table = ent; |
| 54 | return 0; |
| 55 | fail: |
| 56 | if (ent) { |
| 57 | if (ent->name) |
| 58 | free(ent->name); |
| 59 | if (ent->value) |
| 60 | free(ent->value); |
| 61 | free(ent); |
| 62 | } |
| 63 | return retval; |
| 64 | } |
| 65 | |
| 66 | static struct subst_entry *fetch_subst_entry(char *name) |
| 67 | { |
| 68 | struct subst_entry *ent; |
| 69 | |
| 70 | for (ent = subst_table; ent; ent = ent->next) { |
| 71 | if (strcmp(name, ent->name) == 0) |
| 72 | break; |
| 73 | } |
| 74 | return ent; |
| 75 | } |
| 76 | |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 77 | /* |
| 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. |
| 80 | */ |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 81 | static char *get_subst_symbol(const char *begin, size_t len, char prefix) |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 82 | { |
| 83 | static char replace_name[128]; |
| 84 | char *cp, *start; |
| 85 | |
| 86 | start = replace_name; |
| 87 | if (prefix) |
| 88 | *start++ = prefix; |
| 89 | |
| 90 | if (len > sizeof(replace_name)-2) |
| 91 | return NULL; |
| 92 | memcpy(start, begin, len); |
| 93 | start[len] = 0; |
| 94 | |
| 95 | /* |
Theodore Ts'o | 7822c1d | 1998-12-19 08:08:43 +0000 | [diff] [blame] | 96 | * The substitution variable must all be in the of [0-9A-Za-z_]. |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 97 | * If it isn't, this must be an invalid symbol name. |
| 98 | */ |
| 99 | for (cp = start; *cp; cp++) { |
| 100 | if (!(*cp >= 'a' && *cp <= 'z') && |
| 101 | !(*cp >= 'A' && *cp <= 'Z') && |
Theodore Ts'o | 7822c1d | 1998-12-19 08:08:43 +0000 | [diff] [blame] | 102 | !(*cp >= '0' && *cp <= '9') && |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 103 | !(*cp == '_')) |
| 104 | return NULL; |
| 105 | } |
| 106 | return (replace_name); |
| 107 | } |
| 108 | |
| 109 | static void replace_string(char *begin, char *end, char *newstr) |
| 110 | { |
| 111 | int replace_len, len; |
| 112 | |
| 113 | replace_len = strlen(newstr); |
| 114 | len = end - begin; |
Theodore Ts'o | 913c4e9 | 2001-04-17 05:05:37 +0000 | [diff] [blame] | 115 | if (replace_len == 0) |
| 116 | memmove(begin, end+1, strlen(end)+1); |
| 117 | else if (replace_len != len+1) |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 118 | memmove(end+(replace_len-len-1), end, |
| 119 | strlen(end)+1); |
| 120 | memcpy(begin, newstr, replace_len); |
| 121 | } |
| 122 | |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 123 | static void substitute_line(char *line) |
| 124 | { |
Theodore Ts'o | cdceb04 | 1999-10-26 16:28:59 +0000 | [diff] [blame] | 125 | char *ptr, *name_ptr, *end_ptr; |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 126 | struct subst_entry *ent; |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 127 | char *replace_name; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 128 | size_t len; |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 129 | |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 130 | /* |
| 131 | * Expand all @FOO@ substitutions |
| 132 | */ |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 133 | ptr = line; |
| 134 | while (ptr) { |
| 135 | name_ptr = strchr(ptr, '@'); |
| 136 | if (!name_ptr) |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 137 | break; /* No more */ |
| 138 | if (*(++name_ptr) == '@') { |
| 139 | /* |
| 140 | * Handle tytso@@mit.edu --> tytso@mit.edu |
| 141 | */ |
| 142 | memmove(name_ptr-1, name_ptr, strlen(name_ptr)+1); |
| 143 | ptr = name_ptr+1; |
| 144 | continue; |
| 145 | } |
| 146 | end_ptr = strchr(name_ptr, '@'); |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 147 | if (!end_ptr) |
| 148 | break; |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 149 | len = end_ptr - name_ptr; |
| 150 | replace_name = get_subst_symbol(name_ptr, len, 0); |
| 151 | if (!replace_name) { |
| 152 | ptr = name_ptr; |
| 153 | continue; |
| 154 | } |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 155 | ent = fetch_subst_entry(replace_name); |
| 156 | if (!ent) { |
| 157 | fprintf(stderr, "Unfound expansion: '%s'\n", |
| 158 | replace_name); |
| 159 | ptr = end_ptr + 1; |
| 160 | continue; |
| 161 | } |
| 162 | #if 0 |
| 163 | fprintf(stderr, "Replace name = '%s' with '%s'\n", |
| 164 | replace_name, ent->value); |
| 165 | #endif |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 166 | ptr = name_ptr-1; |
| 167 | replace_string(ptr, end_ptr, ent->value); |
| 168 | } |
| 169 | /* |
| 170 | * Now do a second pass to expand ${FOO} |
| 171 | */ |
| 172 | ptr = line; |
| 173 | while (ptr) { |
| 174 | name_ptr = strchr(ptr, '$'); |
| 175 | if (!name_ptr) |
| 176 | break; /* No more */ |
| 177 | if (*(++name_ptr) != '{') { |
| 178 | ptr = name_ptr; |
| 179 | continue; |
| 180 | } |
| 181 | name_ptr++; |
| 182 | end_ptr = strchr(name_ptr, '}'); |
| 183 | if (!end_ptr) |
| 184 | break; |
| 185 | len = end_ptr - name_ptr; |
| 186 | replace_name = get_subst_symbol(name_ptr, len, '$'); |
| 187 | if (!replace_name) { |
| 188 | ptr = name_ptr; |
| 189 | continue; |
| 190 | } |
| 191 | ent = fetch_subst_entry(replace_name); |
| 192 | if (!ent) { |
| 193 | ptr = end_ptr + 1; |
| 194 | continue; |
| 195 | } |
| 196 | #if 0 |
| 197 | fprintf(stderr, "Replace name = '%s' with '%s'\n", |
| 198 | replace_name, ent->value); |
| 199 | #endif |
| 200 | ptr = name_ptr-2; |
| 201 | replace_string(ptr, end_ptr, ent->value); |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | static void parse_config_file(FILE *f) |
| 206 | { |
| 207 | char line[2048]; |
| 208 | char *cp, *ptr; |
| 209 | |
| 210 | while (!feof(f)) { |
| 211 | memset(line, 0, sizeof(line)); |
| 212 | if (fgets(line, sizeof(line), f) == NULL) |
| 213 | break; |
| 214 | /* |
| 215 | * Strip newlines and comments. |
| 216 | */ |
| 217 | cp = strchr(line, '\n'); |
| 218 | if (cp) |
| 219 | *cp = 0; |
| 220 | cp = strchr(line, '#'); |
| 221 | if (cp) |
| 222 | *cp = 0; |
| 223 | /* |
| 224 | * Skip trailing and leading whitespace |
| 225 | */ |
| 226 | for (cp = line + strlen(line) - 1; cp >= line; cp--) { |
| 227 | if (*cp == ' ' || *cp == '\t') |
| 228 | *cp = 0; |
| 229 | else |
| 230 | break; |
| 231 | } |
| 232 | cp = line; |
| 233 | while (*cp && isspace(*cp)) |
| 234 | cp++; |
| 235 | ptr = cp; |
| 236 | /* |
| 237 | * Skip empty lines |
| 238 | */ |
| 239 | if (*ptr == 0) |
| 240 | continue; |
| 241 | /* |
| 242 | * Ignore future extensions |
| 243 | */ |
Theodore Ts'o | e7549ca | 1998-08-01 04:35:39 +0000 | [diff] [blame] | 244 | if (*ptr == '@') |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 245 | continue; |
| 246 | /* |
| 247 | * Parse substitutions |
| 248 | */ |
| 249 | for (cp = ptr; *cp; cp++) |
| 250 | if (isspace(*cp)) |
| 251 | break; |
| 252 | *cp = 0; |
| 253 | for (cp++; *cp; cp++) |
| 254 | if (!isspace(*cp)) |
| 255 | break; |
| 256 | #if 0 |
Theodore Ts'o | 913c4e9 | 2001-04-17 05:05:37 +0000 | [diff] [blame] | 257 | printf("Substitute: '%s' for '%s'\n", ptr, cp ? cp : "<NULL>"); |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 258 | #endif |
| 259 | add_subst(ptr, cp); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Return 0 if the files are different, 1 if the files are the same. |
| 265 | */ |
| 266 | static int compare_file(const char *outfn, const char *newfn) |
| 267 | { |
Theodore Ts'o | 45d2161 | 1998-01-19 14:40:24 +0000 | [diff] [blame] | 268 | FILE *old_f, *new_f; |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 269 | char oldbuf[2048], newbuf[2048], *oldcp, *newcp; |
| 270 | int retval; |
| 271 | |
Theodore Ts'o | 45d2161 | 1998-01-19 14:40:24 +0000 | [diff] [blame] | 272 | old_f = fopen(outfn, "r"); |
| 273 | if (!old_f) |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 274 | return 0; |
Theodore Ts'o | 45d2161 | 1998-01-19 14:40:24 +0000 | [diff] [blame] | 275 | new_f = fopen(newfn, "r"); |
| 276 | if (!new_f) |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 277 | return 0; |
| 278 | |
| 279 | while (1) { |
Theodore Ts'o | 45d2161 | 1998-01-19 14:40:24 +0000 | [diff] [blame] | 280 | oldcp = fgets(oldbuf, sizeof(oldbuf), old_f); |
| 281 | newcp = fgets(newbuf, sizeof(newbuf), new_f); |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 282 | if (!oldcp && !newcp) { |
| 283 | retval = 1; |
| 284 | break; |
| 285 | } |
| 286 | if (!oldcp || !newcp || strcmp(oldbuf, newbuf)) { |
| 287 | retval = 0; |
| 288 | break; |
| 289 | } |
| 290 | } |
Theodore Ts'o | 45d2161 | 1998-01-19 14:40:24 +0000 | [diff] [blame] | 291 | fclose(old_f); |
| 292 | fclose(new_f); |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 293 | return retval; |
| 294 | } |
| 295 | |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 296 | |
| 297 | |
| 298 | int main(int argc, char **argv) |
| 299 | { |
| 300 | char line[2048]; |
| 301 | int c; |
| 302 | FILE *in, *out; |
| 303 | char *outfn = NULL, *newfn = NULL; |
| 304 | int verbose = 0; |
Theodore Ts'o | dd60705 | 2004-04-03 13:53:46 -0500 | [diff] [blame] | 305 | int adjust_timestamp = 0; |
| 306 | struct stat stbuf; |
| 307 | struct utimbuf ut; |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 308 | |
Theodore Ts'o | dd60705 | 2004-04-03 13:53:46 -0500 | [diff] [blame] | 309 | while ((c = getopt (argc, argv, "f:tv")) != EOF) { |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 310 | switch (c) { |
| 311 | case 'f': |
| 312 | in = fopen(optarg, "r"); |
| 313 | if (!in) { |
| 314 | perror(optarg); |
| 315 | exit(1); |
| 316 | } |
| 317 | parse_config_file(in); |
| 318 | fclose(in); |
| 319 | break; |
Theodore Ts'o | dd60705 | 2004-04-03 13:53:46 -0500 | [diff] [blame] | 320 | case 't': |
| 321 | adjust_timestamp++; |
| 322 | break; |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 323 | case 'v': |
| 324 | verbose++; |
| 325 | break; |
| 326 | default: |
| 327 | fprintf(stderr, "%s: [-f config-file] [file]\n", |
| 328 | argv[0]); |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | if (optind < argc) { |
| 333 | in = fopen(argv[optind], "r"); |
| 334 | if (!in) { |
| 335 | perror(argv[optind]); |
| 336 | exit(1); |
| 337 | } |
| 338 | optind++; |
| 339 | } else |
| 340 | in = stdin; |
| 341 | |
| 342 | if (optind < argc) { |
| 343 | outfn = argv[optind]; |
Theodore Ts'o | 45d2161 | 1998-01-19 14:40:24 +0000 | [diff] [blame] | 344 | newfn = (char *) malloc(strlen(outfn)+20); |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 345 | if (!newfn) { |
| 346 | fprintf(stderr, "Memory error! Exiting.\n"); |
| 347 | exit(1); |
| 348 | } |
| 349 | strcpy(newfn, outfn); |
| 350 | strcat(newfn, ".new"); |
| 351 | out = fopen(newfn, "w"); |
| 352 | if (!out) { |
| 353 | perror(newfn); |
| 354 | exit(1); |
| 355 | } |
| 356 | } else { |
| 357 | out = stdout; |
| 358 | outfn = 0; |
| 359 | } |
| 360 | |
| 361 | while (!feof(in)) { |
| 362 | if (fgets(line, sizeof(line), in) == NULL) |
| 363 | break; |
| 364 | substitute_line(line); |
| 365 | fputs(line, out); |
| 366 | } |
| 367 | fclose(in); |
| 368 | fclose(out); |
| 369 | if (outfn) { |
| 370 | if (compare_file(outfn, newfn)) { |
| 371 | if (verbose) |
| 372 | printf("No change, keeping %s.\n", outfn); |
Theodore Ts'o | dd60705 | 2004-04-03 13:53:46 -0500 | [diff] [blame] | 373 | if (adjust_timestamp) { |
| 374 | if (stat(outfn, &stbuf) == 0) { |
| 375 | if (verbose) |
| 376 | printf("Updating modtime for %s\n", outfn); |
| 377 | ut.actime = stbuf.st_atime; |
| 378 | ut.modtime = time(0); |
| 379 | if (utime(outfn, &ut) < 0) |
| 380 | perror("utime"); |
| 381 | } |
| 382 | } |
Theodore Ts'o | 44339bd | 1997-10-15 02:47:20 +0000 | [diff] [blame] | 383 | unlink(newfn); |
| 384 | } else { |
| 385 | if (verbose) |
| 386 | printf("Creating or replacing %s.\n", outfn); |
| 387 | rename(newfn, outfn); |
| 388 | } |
| 389 | } |
| 390 | return (0); |
| 391 | } |
| 392 | |
| 393 | |