void sub_file(char *t) /****************************************************************** * Do our substitutions on a single file. ******************************************************************/ { Name *ll, *n; FILE *sfile; sfile = must_open(t, "r"); printf("Subbing %s\n", t); ll = NULL; llcount = 0; ltitle = t; dirty = FALSE; for (;;) { if (fgets(b1, sizeof(b1)-1, sfile) == NULL) break; if (embedded) embedded_subline(); else csym_subline(); n = begmem(sizeof(*n)); n->name = clone_string(b2); n->next = ll; ll = n; llcount += 1; } fclose(sfile); if (dirty && writeit) { printf("saving new %s\n", t); ll = reverse_list(ll); n = ll; if (backup) backitup(t); sfile = must_open(t, "w"); while (n != NULL) { fputs(n->name, sfile); n = n->next; } fclose(sfile); } free_names(ll); }
void sub_param_file(char *n) /****************************************************************** * Read in list of files to substitute on from a parameter file. ******************************************************************/ { FILE *pfile; char buf[100]; pfile = must_open(n, "r"); while (next_word(pfile, buf, sizeof(buf))) add_sub_file(buf); fclose(pfile); }
void redirect_file (const char *filename) { must_redirect (1, must_open (filename, O_WRONLY | O_CREAT | O_TRUNC, /* I guess */ 0666)); /* I guess */ }
void build_sub_file(char *sname, Boolean embed) /****************************************************************** * go look for lines of format : * in|out * to tell us what to substitute * in| * is ok if embed is false, and will delete all ins. * * if embed is true, we'll skip all white space. * ******************************************************************/ { FILE *sfile; int line; char buf[256]; char *s; char word1[256]; char word2[256]; char *d; char c; Sub *sub; sfile = must_open(sname, "r"); line = 0; for (;;) { if (fgets(buf, sizeof(buf)-1, sfile) == NULL) break; line++; s = buf; if (!embed) s = skip_space(s); d = word1; for (;;) { c = *s++; if (c == 0) { fatal("%s %d - Line with no separator", sname, line); } if (!embed) { if (isspace(c)) break; } else if (c == separator) break; *d++ = c; } *d++ = 0; d = word2; if (!embed) s = skip_space(s); for (;;) { c = *s++; if (!embed) { if (isspace(c) || c == 0) break; } else { if (c == 0 || c == '\r' || c == '\n') break; } *d++ = c; } *d++ = 0; if (!embed && (word2[0] == 0)) fatal("%s %d - Line with no substitution", sname, line); sub = begmem(sizeof(*sub)); sub->in = clone_string(word1); sub->insize = strlen(word1); sub->out = clone_string(word2); sub->outsize = strlen(word2); add_hash(global_hash, sub); } fclose(sfile); }