# HG changeset patch # User Mychaela Falconia # Date 1516216352 0 # Node ID b8b08c302acec4bdbc8eaef81c2e6038c33cfdcf # Parent a05086335d8e80fdb021c80c647cfc2a18f03710 cfg-hdr-gen C helper program written diff -r a05086335d8e -r b8b08c302ace .hgignore --- a/.hgignore Wed Jan 17 07:59:04 2018 +0000 +++ b/.hgignore Wed Jan 17 19:12:32 2018 +0000 @@ -2,6 +2,7 @@ ^build- ^helpers/build-date$ +^helpers/cfg-hdr-gen$ ^helpers/makeline$ ^helpers/mk-flash-script$ ^helpers/srec4ram$ diff -r a05086335d8e -r b8b08c302ace helpers/Makefile --- a/helpers/Makefile Wed Jan 17 07:59:04 2018 +0000 +++ b/helpers/Makefile Wed Jan 17 19:12:32 2018 +0000 @@ -1,12 +1,15 @@ CC= gcc CFLAGS= -O2 -PROGS= build-date makeline mk-flash-script srec4ram str2ind-ver +PROGS= build-date cfg-hdr-gen makeline mk-flash-script srec4ram str2ind-ver all: ${PROGS} build-date: build-date.c ${CC} ${CFLAGS} -o $@ $@.c +cfg-hdr-gen: cfg-hdr-gen.c + ${CC} ${CFLAGS} -o $@ $@.c + makeline: makeline.c ${CC} ${CFLAGS} -o $@ $@.c diff -r a05086335d8e -r b8b08c302ace helpers/cfg-hdr-gen.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/helpers/cfg-hdr-gen.c Wed Jan 17 19:12:32 2018 +0000 @@ -0,0 +1,141 @@ +/* + * This helper program generates the set of *.cfg header files, based on a + * template file and environment variables for the non-constant settings. + */ + +#include +#include +#include +#include +#include +#include + +char *infname; +FILE *inf, *outf; +char include_guard_symbol[32]; +char linebuf[256]; +int lineno; + +make_include_guard_symbol(fname) + char *fname; +{ + char *sp, *dp; + int c; + + sp = fname; + dp = include_guard_symbol; + *dp++ = '_'; + *dp++ = '_'; + while (c = *sp++) { + if (islower(c)) + c = toupper(c); + else if (c == '.') + c = '_'; + *dp++ = c; + } + *dp++ = '_'; + *dp++ = '_'; + *dp = '\0'; +} + +close_output() +{ + if (outf) { + fprintf(outf, "#endif /* %s */\n", include_guard_symbol); + fclose(outf); + outf = 0; + } +} + +bracket_line() +{ + char *cp; + + close_output(); + cp = index(linebuf+1, ']'); + if (!cp) { + fprintf(stderr, "%s line %d: unterminated bracket line\n", + infname, lineno); + exit(1); + } + *cp = '\0'; + outf = fopen(linebuf+1, "w"); + if (!outf) { + perror(linebuf+1); + exit(1); + } + make_include_guard_symbol(linebuf+1); + fprintf(outf, "#ifndef %s\n", include_guard_symbol); + fprintf(outf, "#define %s\n", include_guard_symbol); +} + +process_line() +{ + char *cp, *symbol, *value; + + if (linebuf[0] == '[') + return bracket_line(); + for (cp = linebuf; isspace(*cp); cp++) + ; + if (*cp == '\0' || *cp == '#') + return; + for (symbol = cp; *cp && !isspace(*cp); cp++) + ; + if (!*cp) { +inv: fprintf(stderr, "%s line %d: expected two fields\n", + infname, lineno); + exit(1); + } + *cp++ = '\0'; + while (isspace(*cp)) + cp++; + if (*cp == '\0' || *cp == '#') + goto inv; + for (value = cp; *cp && !isspace(*cp); cp++) + ; + if (*cp) + *cp++ = '\0'; + while (isspace(*cp)) + cp++; + if (*cp != '\0' && *cp != '#') + goto inv; + if (!strcmp(value, "var")) { + value = getenv(symbol); + if (!value) { + fprintf(stderr, + "%s line %d: no environment variable named %s\n", + infname, lineno, symbol); + exit(1); + } + } + if (!outf) { + fprintf(stderr, "%s line %d: no open output file\n", + infname, lineno); + exit(1); + } + fprintf(outf, "#define %s %s\n", symbol, value); +} + +main(argc, argv) + char **argv; +{ + if (argc != 3) { + fprintf(stderr, "usage: %s template-file output-dir\n", + argv[0]); + exit(1); + } + infname = argv[1]; + inf = fopen(infname, "r"); + if (!inf) { + perror(infname); + exit(1); + } + if (chdir(argv[2]) < 0) { + perror(argv[2]); + exit(1); + } + for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) + process_line(); + close_output(); + exit(0); +}