From 92c29403ab1106794a9b8735d962c2582869490b Mon Sep 17 00:00:00 2001 From: joe Date: Wed, 10 Jan 2001 14:08:48 +0000 Subject: Liberal application of style(9). --- usr.sbin/crunch/crunchgen/crunchgen.c | 1423 ++++++++++++++++++--------------- 1 file changed, 758 insertions(+), 665 deletions(-) (limited to 'usr.sbin/crunch') diff --git a/usr.sbin/crunch/crunchgen/crunchgen.c b/usr.sbin/crunch/crunchgen/crunchgen.c index 2d3974a..cf7066f 100644 --- a/usr.sbin/crunch/crunchgen/crunchgen.c +++ b/usr.sbin/crunch/crunchgen/crunchgen.c @@ -32,6 +32,10 @@ * Generates a Makefile and main C file for a crunched executable, * from specs given in a .conf file. */ +#include +#include +#include + #include #include #include @@ -40,10 +44,6 @@ #include #include -#include -#include -#include - #define CRUNCH_VERSION "0.2" #define MAXLINELEN 16384 @@ -55,46 +55,46 @@ /* simple lists of strings suffice for most parms */ typedef struct strlst { - struct strlst *next; - char *str; + struct strlst *next; + char *str; } strlst_t; /* progs have structure, each field can be set with "special" or calculated */ typedef struct prog { - struct prog *next; /* link field */ - char *name; /* program name */ - char *ident; /* C identifier for the program name */ - char *srcdir; - char *realsrcdir; - char *objdir; - char *objvar; /* Makefile variable to replace OBJS */ - strlst_t *objs, *objpaths; - strlst_t *buildopts; - strlst_t *keeplist; - strlst_t *links; - int goterror; + struct prog *next; /* link field */ + char *name; /* program name */ + char *ident; /* C identifier for the program name */ + char *srcdir; + char *realsrcdir; + char *objdir; + char *objvar; /* Makefile variable to replace OBJS */ + strlst_t *objs, *objpaths; + strlst_t *buildopts; + strlst_t *keeplist; + strlst_t *links; + int goterror; } prog_t; /* global state */ strlst_t *buildopts = NULL; -strlst_t *srcdirs = NULL; -strlst_t *libs = NULL; -prog_t *progs = NULL; +strlst_t *srcdirs = NULL; +strlst_t *libs = NULL; +prog_t *progs = NULL; char confname[MAXPATHLEN], infilename[MAXPATHLEN]; char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN]; char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN]; -char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */ -char *objprefix; /* where are the objects ? */ +char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */ +char *objprefix; /* where are the objects ? */ int linenum = -1; int goterror = 0; -int verbose, readcache; /* options */ +int verbose, readcache; /* options */ int reading_cache; -int makeobj = 0; /* add 'make obj' rules to the makefile */ +int makeobj = 0; /* add 'make obj' rules to the makefile */ int list_mode; @@ -115,83 +115,111 @@ void gen_outputs(void); int main(int argc, char **argv) { - char *p; - int optc; - - verbose = 1; - readcache = 1; - *outmkname = *outcfname = *execfname = '\0'; - - p = getenv("MAKEOBJDIRPREFIX"); - if (p == NULL || *p == '\0') - objprefix = "/usr/obj"; /* default */ - else - if ((objprefix = strdup(p)) == NULL) - out_of_memory(); - - while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) { - switch(optc) { - case 'f': readcache = 0; break; - case 'o': makeobj = 1; break; - case 'q': verbose = 0; break; - - case 'm': strlcpy(outmkname, optarg, sizeof(outmkname)); break; - case 'p': if ((objprefix = strdup(optarg)) == NULL) + char *p; + int optc; + + verbose = 1; + readcache = 1; + *outmkname = *outcfname = *execfname = '\0'; + + p = getenv("MAKEOBJDIRPREFIX"); + if (p == NULL || *p == '\0') + objprefix = "/usr/obj"; /* default */ + else + if ((objprefix = strdup(p)) == NULL) + out_of_memory(); + + while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) { + switch(optc) { + case 'f': + readcache = 0; + break; + case 'o': + makeobj = 1; + break; + case 'q': + verbose = 0; + break; + + case 'm': + strlcpy(outmkname, optarg, sizeof(outmkname)); + break; + case 'p': + if ((objprefix = strdup(optarg)) == NULL) out_of_memory(); break; - case 'h': strlcpy(outhdrname, optarg, sizeof(outhdrname)); break; - case 'c': strlcpy(outcfname, optarg, sizeof(outcfname)); break; - case 'e': strlcpy(execfname, optarg, sizeof(execfname)); break; - case 'l': list_mode++; verbose = 0; break; - case '?': - default: usage(); + case 'h': + strlcpy(outhdrname, optarg, sizeof(outhdrname)); + break; + case 'c': + strlcpy(outcfname, optarg, sizeof(outcfname)); + break; + case 'e': + strlcpy(execfname, optarg, sizeof(execfname)); + break; + + case 'l': + list_mode++; + verbose = 0; + break; + + case '?': + default: + usage(); + } } - } - argc -= optind; - argv += optind; + argc -= optind; + argv += optind; + + if (argc != 1) + usage(); - if(argc != 1) usage(); + /* + * generate filenames + */ - /* - * generate filenames - */ + strlcpy(infilename, argv[0], sizeof(infilename)); - strlcpy(infilename, argv[0], sizeof(infilename)); + /* confname = `basename infilename .conf` */ - /* confname = `basename infilename .conf` */ + if ((p=strrchr(infilename, '/')) != NULL) + strlcpy(confname, p + 1, sizeof(confname)); + else + strlcpy(confname, infilename, sizeof(confname)); - if((p=strrchr(infilename, '/')) != NULL) - strlcpy(confname, p+1, sizeof(confname)); - else - strlcpy(confname, infilename, sizeof(confname)); - if((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) *p = '\0'; + if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) + *p = '\0'; - if(!*outmkname) snprintf(outmkname, sizeof(outmkname), "%s.mk", confname); - if(!*outcfname) snprintf(outcfname, sizeof(outcfname), "%s.c", confname); - if(!*execfname) snprintf(execfname, sizeof(execfname), "%s", confname); + if (!*outmkname) + snprintf(outmkname, sizeof(outmkname), "%s.mk", confname); + if (!*outcfname) + snprintf(outcfname, sizeof(outcfname), "%s.c", confname); + if (!*execfname) + snprintf(execfname, sizeof(execfname), "%s", confname); - snprintf(cachename, sizeof(cachename), "%s.cache", confname); - snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX", + snprintf(cachename, sizeof(cachename), "%s.cache", confname); + snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname); - parse_conf_file(); - if (list_mode) - exit(goterror); + parse_conf_file(); + if (list_mode) + exit(goterror); - gen_outputs(); + gen_outputs(); - exit(goterror); + exit(goterror); } void usage(void) { - fprintf(stderr, "%s\n%s\n", - "usage: crunchgen [-foq] [-h ] [-m ]", - " [-p ] [-c ] [-e ] "); - exit(1); + fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ", + "[-h ] [-m ]", + "[-p ] [-c ] [-e ] ", + ""); + exit(1); } @@ -218,256 +246,289 @@ void add_prog(char *progname); void parse_conf_file(void) { - if(!is_nonempty_file(infilename)) + if (!is_nonempty_file(infilename)) errx(1, "fatal: input file \"%s\" not found", infilename); - parse_one_file(infilename); - if(readcache && is_nonempty_file(cachename)) { - reading_cache = 1; - parse_one_file(cachename); - } + + parse_one_file(infilename); + if (readcache && is_nonempty_file(cachename)) { + reading_cache = 1; + parse_one_file(cachename); + } } void parse_one_file(char *filename) { - char *fieldv[MAXFIELDS]; - int fieldc; - void (*f)(int c, char **v); - FILE *cf; - char line[MAXLINELEN]; - - snprintf(line, sizeof(line), "reading %s", filename); - status(line); - strlcpy(curfilename, filename, sizeof(curfilename)); - - if((cf = fopen(curfilename, "r")) == NULL) { - warn("%s", curfilename); - goterror = 1; - return; - } - - linenum = 0; - while(fgets(line, MAXLINELEN, cf) != NULL) { - linenum++; - parse_line(line, &fieldc, fieldv, MAXFIELDS); - if(fieldc < 1) continue; - if(!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs; - else if(!strcmp(fieldv[0], "progs")) f = add_progs; - else if(!strcmp(fieldv[0], "ln")) f = add_link; - else if(!strcmp(fieldv[0], "libs")) f = add_libs; - else if(!strcmp(fieldv[0], "buildopts")) f = add_buildopts; - else if(!strcmp(fieldv[0], "special")) f = add_special; - else { - warnx("%s:%d: skipping unknown command `%s'", - curfilename, linenum, fieldv[0]); - goterror = 1; - continue; - } - if(fieldc < 2) { - warnx("%s:%d: %s command needs at least 1 argument, skipping", - curfilename, linenum, fieldv[0]); - goterror = 1; - continue; - } - f(fieldc, fieldv); - } - - if(ferror(cf)) { - warn("%s", curfilename); - goterror = 1; - } - fclose(cf); + char *fieldv[MAXFIELDS]; + int fieldc; + void (*f)(int c, char **v); + FILE *cf; + char line[MAXLINELEN]; + + snprintf(line, sizeof(line), "reading %s", filename); + status(line); + strlcpy(curfilename, filename, sizeof(curfilename)); + + if ((cf = fopen(curfilename, "r")) == NULL) { + warn("%s", curfilename); + goterror = 1; + return; + } + + linenum = 0; + while (fgets(line, MAXLINELEN, cf) != NULL) { + linenum++; + parse_line(line, &fieldc, fieldv, MAXFIELDS); + + if (fieldc < 1) + continue; + + if (!strcmp(fieldv[0], "srcdirs")) + f = add_srcdirs; + else if(!strcmp(fieldv[0], "progs")) + f = add_progs; + else if(!strcmp(fieldv[0], "ln")) + f = add_link; + else if(!strcmp(fieldv[0], "libs")) + f = add_libs; + else if(!strcmp(fieldv[0], "buildopts")) + f = add_buildopts; + else if(!strcmp(fieldv[0], "special")) + f = add_special; + else { + warnx("%s:%d: skipping unknown command `%s'", + curfilename, linenum, fieldv[0]); + goterror = 1; + continue; + } + + if (fieldc < 2) { + warnx("%s:%d: %s %s", + curfilename, linenum, fieldv[0], + "command needs at least 1 argument, skipping"); + goterror = 1; + continue; + } + + f(fieldc, fieldv); + } + + if (ferror(cf)) { + warn("%s", curfilename); + goterror = 1; + } + fclose(cf); } void parse_line(char *line, int *fc, char **fv, int nf) { - char *p; - - p = line; - *fc = 0; - while(1) { - while(isspace(*p)) p++; - if(*p == '\0' || *p == '#') break; - - if(*fc < nf) fv[(*fc)++] = p; - while(*p && !isspace(*p) && *p != '#') p++; - if(*p == '\0' || *p == '#') break; - *p++ = '\0'; - } - if(*p) *p = '\0'; /* needed for '#' case */ + char *p; + + p = line; + *fc = 0; + + while (1) { + while (isspace(*p)) + p++; + + if (*p == '\0' || *p == '#') + break; + + if (*fc < nf) + fv[(*fc)++] = p; + + while (*p && !isspace(*p) && *p != '#') + p++; + + if (*p == '\0' || *p == '#') + break; + + *p++ = '\0'; + } + + if (*p) + *p = '\0'; /* needed for '#' case */ } void add_srcdirs(int argc, char **argv) { - int i; - - for(i=1;inext) - if(!strcmp(p2->name, progname)) return; - - p2 = malloc(sizeof(prog_t)); - if(p2) { - memset(p2, 0, sizeof(prog_t)); - p2->name = strdup(progname); - } - if(!p2 || !p2->name) - out_of_memory(); - - p2->next = NULL; - if(p1 == NULL) progs = p2; - else p1->next = p2; - - p2->ident = p2->srcdir = p2->realsrcdir = p2->objdir = NULL; - p2->links = p2->objs = p2->keeplist = NULL; - p2->buildopts = NULL; - p2->goterror = 0; - if (list_mode) - printf("%s\n",progname); + prog_t *p1, *p2; + + /* add to end, but be smart about dups */ + + for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next) + if (!strcmp(p2->name, progname)) + return; + + p2 = malloc(sizeof(prog_t)); + if(p2) { + memset(p2, 0, sizeof(prog_t)); + p2->name = strdup(progname); + } + if (!p2 || !p2->name) + out_of_memory(); + + p2->next = NULL; + if (p1 == NULL) + progs = p2; + else + p1->next = p2; + + p2->ident = NULL; + p2->srcdir = NULL; + p2->realsrcdir = NULL; + p2->objdir = NULL; + p2->links = NULL; + p2->objs = NULL; + p2->keeplist = NULL; + p2->buildopts = NULL; + p2->goterror = 0; + + if (list_mode) + printf("%s\n",progname); } void add_link(int argc, char **argv) { - int i; - prog_t *p = find_prog(argv[1]); + int i; + prog_t *p = find_prog(argv[1]); + + if (p == NULL) { + warnx("%s:%d: no prog %s previously declared, skipping link", + curfilename, linenum, argv[1]); + goterror = 1; + return; + } - if(p == NULL) { - warnx("%s:%d: no prog %s previously declared, skipping link", - curfilename, linenum, argv[1]); - goterror = 1; - return; - } - for(i=2;ilinks, argv[i]); - } + for (i = 2; i < argc; i++) { + if (list_mode) + printf("%s\n",argv[i]); + + add_string(&p->links, argv[i]); + } } void add_libs(int argc, char **argv) { - int i; + int i; - for(i=1;iident = strdup(argv[3])) == NULL) - out_of_memory(); - } - else if(!strcmp(argv[2], "srcdir")) { - if(argc != 4) goto argcount; - if((p->srcdir = strdup(argv[3])) == NULL) - out_of_memory(); - } - else if(!strcmp(argv[2], "objdir")) { - if(argc != 4) goto argcount; - if((p->objdir = strdup(argv[3])) == NULL) - out_of_memory(); - } - else if(!strcmp(argv[2], "objs")) { - p->objs = NULL; - for(i=3;iobjs, argv[i]); - } - else if(!strcmp(argv[2], "objpaths")) { - p->objpaths = NULL; - for(i=3;iobjpaths, argv[i]); - } - else if(!strcmp(argv[2], "keep")) { - p->keeplist = NULL; - for(i=3;ikeeplist, argv[i]); - } - else if(!strcmp(argv[2], "objvar")) { - if(argc != 4) - goto argcount; - if((p->objvar = strdup(argv[3])) == NULL) - out_of_memory(); - } - else if (!strcmp(argv[2], "buildopts")) { - p->buildopts = NULL; - for (i = 3; i < argc; i++) - add_string(&p->buildopts, argv[i]); - } - else { - warnx("%s:%d: bad parameter name `%s', skipping line", - curfilename, linenum, argv[2]); - goterror = 1; - } - return; + if (p == NULL) { + if (reading_cache) + return; + warnx("%s:%d: no prog %s previously declared, skipping special", + curfilename, linenum, argv[1]); + goterror = 1; + return; + } + + if (!strcmp(argv[2], "ident")) { + if (argc != 4) + goto argcount; + if ((p->ident = strdup(argv[3])) == NULL) + out_of_memory(); + } else if (!strcmp(argv[2], "srcdir")) { + if (argc != 4) + goto argcount; + if ((p->srcdir = strdup(argv[3])) == NULL) + out_of_memory(); + } else if (!strcmp(argv[2], "objdir")) { + if(argc != 4) + goto argcount; + if((p->objdir = strdup(argv[3])) == NULL) + out_of_memory(); + } else if (!strcmp(argv[2], "objs")) { + p->objs = NULL; + for (i = 3; i < argc; i++) + add_string(&p->objs, argv[i]); + } else if (!strcmp(argv[2], "objpaths")) { + p->objpaths = NULL; + for (i = 3; i < argc; i++) + add_string(&p->objpaths, argv[i]); + } else if (!strcmp(argv[2], "keep")) { + p->keeplist = NULL; + for(i = 3; i < argc; i++) + add_string(&p->keeplist, argv[i]); + } else if (!strcmp(argv[2], "objvar")) { + if(argc != 4) + goto argcount; + if ((p->objvar = strdup(argv[3])) == NULL) + out_of_memory(); + } else if (!strcmp(argv[2], "buildopts")) { + p->buildopts = NULL; + for (i = 3; i < argc; i++) + add_string(&p->buildopts, argv[i]); + } else { + warnx("%s:%d: bad parameter name `%s', skipping line", + curfilename, linenum, argv[2]); + goterror = 1; + } + return; argcount: - warnx("%s:%d: too %s arguments, expected \"special %s %s \"", + warnx("%s:%d: too %s arguments, expected \"special %s %s \"", curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]); - goterror = 1; + goterror = 1; } prog_t *find_prog(char *str) { - prog_t *p; + prog_t *p; - for(p = progs; p != NULL; p = p->next) - if(!strcmp(p->name, str)) return p; + for (p = progs; p != NULL; p = p->next) + if (!strcmp(p->name, str)) + return p; - return NULL; + return NULL; } @@ -495,17 +556,17 @@ char *dir_search(char *progname); void gen_outputs(void) { - prog_t *p; + prog_t *p; - for(p = progs; p != NULL; p = p->next) - fillin_program(p); + for (p = progs; p != NULL; p = p->next) + fillin_program(p); - remove_error_progs(); - gen_specials_cache(); - gen_output_cfile(); - gen_output_makefile(); - status(""); - fprintf(stderr, + remove_error_progs(); + gen_specials_cache(); + gen_output_cfile(); + gen_output_makefile(); + status(""); + fprintf(stderr, "Run \"make -f %s\" to build crunched binary.\n", outmkname); } @@ -514,436 +575,462 @@ void gen_outputs(void) */ void fillin_program(prog_t *p) { - char path[MAXPATHLEN]; - char line[MAXLINELEN]; - FILE *f; - - snprintf(line, MAXLINELEN, "filling in parms for %s", p->name); - status(line); - - if(!p->ident) - p->ident = genident(p->name); - - /* look for the source directory if one wasn't specified by a special */ - if(!p->srcdir) { - p->srcdir = dir_search(p->name); - } - - /* Determine the actual srcdir (maybe symlinked). */ - if (p->srcdir) { - snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`", p->srcdir); - f = popen(line,"r"); - if (!f) - errx(1, "Can't execute: %s\n", line); - - path[0] = '\0'; - fgets(path, sizeof path, f); - if (pclose(f)) - errx(1, "Can't execute: %s\n", line); - - if (!*path) - errx(1, "Can't perform pwd on: %s\n", p->srcdir); - - p->realsrcdir = strdup(path); - } - - /* Unless the option to make object files was specified the - * the objects will be built in the source directory unless - * an object directory already exists. - */ - if(!makeobj && !p->objdir && p->srcdir) { - snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir); - if (is_dir(line)) { - if ((p->objdir = strdup(line)) == NULL) - out_of_memory(); + char path[MAXPATHLEN]; + char line[MAXLINELEN]; + FILE *f; + + snprintf(line, MAXLINELEN, "filling in parms for %s", p->name); + status(line); + + if (!p->ident) + p->ident = genident(p->name); + + /* look for the source directory if one wasn't specified by a special */ + if (!p->srcdir) { + p->srcdir = dir_search(p->name); + } + + /* Determine the actual srcdir (maybe symlinked). */ + if (p->srcdir) { + snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`", + p->srcdir); + f = popen(line,"r"); + if (!f) + errx(1, "Can't execute: %s\n", line); + + path[0] = '\0'; + fgets(path, sizeof path, f); + if (pclose(f)) + errx(1, "Can't execute: %s\n", line); + + if (!*path) + errx(1, "Can't perform pwd on: %s\n", p->srcdir); + + p->realsrcdir = strdup(path); + } + + /* Unless the option to make object files was specified the + * the objects will be built in the source directory unless + * an object directory already exists. + */ + if (!makeobj && !p->objdir && p->srcdir) { + snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir); + if (is_dir(line)) { + if ((p->objdir = strdup(line)) == NULL) + out_of_memory(); + } else + p->objdir = p->realsrcdir; + } + + /* + * XXX look for a Makefile.{name} in local directory first. + * This lets us override the original Makefile. + */ + snprintf(path, sizeof(path), "Makefile.%s", p->name); + if (is_nonempty_file(path)) { + snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name); + status(line); } else - p->objdir = p->realsrcdir; - } -/* - * XXX look for a Makefile.{name} in local directory first. - * This lets us override the original Makefile. - */ - snprintf(path, sizeof(path), "Makefile.%s", p->name); - if (is_nonempty_file(path)) { - snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name); - status(line); - } else - if(p->srcdir) snprintf(path, sizeof(path), "%s/Makefile", p->srcdir); - if(!p->objs && p->srcdir && is_nonempty_file(path)) - fillin_program_objs(p, path); - - if(!p->srcdir && verbose) - warnx("%s: %s: warning: could not find source directory", - infilename, p->name); - if(!p->objs && verbose) - warnx("%s: %s: warning: could not find any .o files", - infilename, p->name); - - if(!p->srcdir || !p->objs) - p->goterror = 1; + if (p->srcdir) + snprintf(path, sizeof(path), "%s/Makefile", p->srcdir); + if (!p->objs && p->srcdir && is_nonempty_file(path)) + fillin_program_objs(p, path); + + if (!p->srcdir && verbose) + warnx("%s: %s: %s", + "warning: could not find source directory", + infilename, p->name); + if (!p->objs && verbose) + warnx("%s: %s: warning: could not find any .o files", + infilename, p->name); + + if (!p->srcdir || !p->objs) + p->goterror = 1; } void fillin_program_objs(prog_t *p, char *path) { - char *obj, *cp; - int fd, rc; - FILE *f; - char *objvar="OBJS"; - strlst_t *s; - char line[MAXLINELEN]; + char *obj, *cp; + int fd, rc; + FILE *f; + char *objvar="OBJS"; + strlst_t *s; + char line[MAXLINELEN]; + + /* discover the objs from the srcdir Makefile */ + + if ((fd = mkstemp(tempfname)) == -1) { + perror(tempfname); + exit(1); + } + if ((f = fdopen(fd, "w")) == NULL) { + warn("%s", tempfname); + goterror = 1; + return; + } + if (p->objvar) + objvar = p->objvar; + + /* + * XXX include outhdrname (e.g. to contain Make variables) + */ + if (outhdrname[0] != '\0') + fprintf(f, ".include \"%s\"\n", outhdrname); + fprintf(f, ".include \"%s\"\n", path); + if (buildopts) { + fprintf(f, "BUILDOPTS+="); + output_strlst(f, buildopts); + } + fprintf(f, ".if defined(PROG) && !defined(%s)\n", objvar); + fprintf(f, "%s=${PROG}.o\n", objvar); + fprintf(f, ".endif\n"); + fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar); + + fprintf(f, "crunchgen_objs:\n\t@make -f %s $(BUILDOPTS) $(%s_OPTS)", + tempfname, p->ident); + for (s = p->buildopts; s != NULL; s = s->next) + fprintf(f, " %s", s->str); + fprintf(f, " loop\n"); + + fclose(f); + + snprintf(line, MAXLINELEN, "make -f %s crunchgen_objs 2>&1", tempfname); + if ((f = popen(line, "r")) == NULL) { + warn("submake pipe"); + goterror = 1; + return; + } - /* discover the objs from the srcdir Makefile */ + while(fgets(line, MAXLINELEN, f)) { + if (strncmp(line, "OBJS= ", 6)) { + warnx("make error: %s", line); + goterror = 1; + continue; + } + + cp = line + 6; + while (isspace(*cp)) + cp++; + + while(*cp) { + obj = cp; + while (*cp && !isspace(*cp)) + cp++; + if (*cp) + *cp++ = '\0'; + add_string(&p->objs, obj); + while (isspace(*cp)) + cp++; + } + } - if((fd = mkstemp(tempfname)) == -1) { - perror(tempfname); - exit(1); - } - if((f = fdopen(fd, "w")) == NULL) { - warn("%s", tempfname); - goterror = 1; - return; - } - if (p->objvar) - objvar = p->objvar ; - - /* - * XXX include outhdrname (e.g. to contain Make variables) - */ - if (outhdrname[0] != '\0') - fprintf(f, ".include \"%s\"\n", outhdrname); - fprintf(f, ".include \"%s\"\n", path); - if (buildopts) { - fprintf(f, "BUILDOPTS+="); - output_strlst(f, buildopts); - } - fprintf(f, ".if defined(PROG) && !defined(%s)\n", objvar); - fprintf(f, "%s=${PROG}.o\n", objvar); - fprintf(f, ".endif\n"); - fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar); - - fprintf(f, "crunchgen_objs:\n\t@make -f %s $(BUILDOPTS) $(%s_OPTS)", - tempfname, p->ident); - for (s = p->buildopts; s != NULL; s = s->next) - fprintf(f, " %s", s->str); - fprintf(f, " loop\n"); - - fclose(f); - - snprintf(line, MAXLINELEN, "make -f %s crunchgen_objs 2>&1", tempfname); - if((f = popen(line, "r")) == NULL) { - warn("submake pipe"); - goterror = 1; - return; - } - - while(fgets(line, MAXLINELEN, f)) { - if(strncmp(line, "OBJS= ", 6)) { - warnx("make error: %s", line); - goterror = 1; - continue; - } - cp = line + 6; - while(isspace(*cp)) cp++; - while(*cp) { - obj = cp; - while(*cp && !isspace(*cp)) cp++; - if(*cp) *cp++ = '\0'; - add_string(&p->objs, obj); - while(isspace(*cp)) cp++; - } - } - if((rc=pclose(f)) != 0) { - warnx("make error: make returned %d", rc); - goterror = 1; - } - unlink(tempfname); + if ((rc=pclose(f)) != 0) { + warnx("make error: make returned %d", rc); + goterror = 1; + } + + unlink(tempfname); } void remove_error_progs(void) { - prog_t *p1, *p2; - - p1 = NULL; p2 = progs; - while(p2 != NULL) { - if(!p2->goterror) - p1 = p2, p2 = p2->next; - else { - /* delete it from linked list */ - warnx("%s: %s: ignoring program because of errors", - infilename, p2->name); - if(p1) p1->next = p2->next; - else progs = p2->next; - p2 = p2->next; - } - } + prog_t *p1, *p2; + + p1 = NULL; p2 = progs; + while (p2 != NULL) { + if (!p2->goterror) + p1 = p2, p2 = p2->next; + else { + /* delete it from linked list */ + warnx("%s: %s: ignoring program because of errors", + infilename, p2->name); + if (p1) + p1->next = p2->next; + else + progs = p2->next; + p2 = p2->next; + } + } } void gen_specials_cache(void) { - FILE *cachef; - prog_t *p; - char line[MAXLINELEN]; + FILE *cachef; + prog_t *p; + char line[MAXLINELEN]; - snprintf(line, MAXLINELEN, "generating %s", cachename); - status(line); + snprintf(line, MAXLINELEN, "generating %s", cachename); + status(line); - if((cachef = fopen(cachename, "w")) == NULL) { - warn("%s", cachename); - goterror = 1; - return; - } + if ((cachef = fopen(cachename, "w")) == NULL) { + warn("%s", cachename); + goterror = 1; + return; + } - fprintf(cachef, "# %s - parm cache generated from %s by crunchgen %s\n\n", + fprintf(cachef, "# %s - parm cache generated from %s by crunchgen " + " %s\n\n", cachename, infilename, CRUNCH_VERSION); - for(p = progs; p != NULL; p = p->next) { - fprintf(cachef, "\n"); - if(p->srcdir) - fprintf(cachef, "special %s srcdir %s\n", p->name, p->srcdir); - if(p->objdir) - fprintf(cachef, "special %s objdir %s\n", p->name, p->objdir); - if(p->objs) { - fprintf(cachef, "special %s objs", p->name); - output_strlst(cachef, p->objs); - } - if(p->objpaths) { - fprintf(cachef, "special %s objpaths", p->name); - output_strlst(cachef, p->objpaths); - } - } - fclose(cachef); + for (p = progs; p != NULL; p = p->next) { + fprintf(cachef, "\n"); + if (p->srcdir) + fprintf(cachef, "special %s srcdir %s\n", + p->name, p->srcdir); + if (p->objdir) + fprintf(cachef, "special %s objdir %s\n", + p->name, p->objdir); + if (p->objs) { + fprintf(cachef, "special %s objs", p->name); + output_strlst(cachef, p->objs); + } + if (p->objpaths) { + fprintf(cachef, "special %s objpaths", p->name); + output_strlst(cachef, p->objpaths); + } + } + fclose(cachef); } void gen_output_makefile(void) { - prog_t *p; - FILE *outmk; - char line[MAXLINELEN]; + prog_t *p; + FILE *outmk; + char line[MAXLINELEN]; - snprintf(line, MAXLINELEN, "generating %s", outmkname); - status(line); + snprintf(line, MAXLINELEN, "generating %s", outmkname); + status(line); - if((outmk = fopen(outmkname, "w")) == NULL) { - warn("%s", outmkname); - goterror = 1; - return; - } + if ((outmk = fopen(outmkname, "w")) == NULL) { + warn("%s", outmkname); + goterror = 1; + return; + } - fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n", + fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n", outmkname, infilename, CRUNCH_VERSION); - if (outhdrname[0] != '\0') - fprintf(outmk, ".include \"%s\"\n", outhdrname); - top_makefile_rules(outmk); - for(p = progs; p != NULL; p = p->next) - prog_makefile_rules(outmk, p); + if (outhdrname[0] != '\0') + fprintf(outmk, ".include \"%s\"\n", outhdrname); + + top_makefile_rules(outmk); + for (p = progs; p != NULL; p = p->next) + prog_makefile_rules(outmk, p); - fprintf(outmk, "\n# ========\n"); - fclose(outmk); + fprintf(outmk, "\n# ========\n"); + fclose(outmk); } void gen_output_cfile(void) { - extern char *crunched_skel[]; - char **cp; - FILE *outcf; - prog_t *p; - strlst_t *s; - char line[MAXLINELEN]; - - snprintf(line, MAXLINELEN, "generating %s", outcfname); - status(line); - - if((outcf = fopen(outcfname, "w")) == NULL) { - warn("%s", outcfname); - goterror = 1; - return; - } + extern char *crunched_skel[]; + char **cp; + FILE *outcf; + prog_t *p; + strlst_t *s; + char line[MAXLINELEN]; + + snprintf(line, MAXLINELEN, "generating %s", outcfname); + status(line); + + if((outcf = fopen(outcfname, "w")) == NULL) { + warn("%s", outcfname); + goterror = 1; + return; + } - fprintf(outcf, - "/* %s - generated from %s by crunchgen %s */\n", + fprintf(outcf, + "/* %s - generated from %s by crunchgen %s */\n", outcfname, infilename, CRUNCH_VERSION); - fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname); - for(cp = crunched_skel; *cp != NULL; cp++) - fprintf(outcf, "%s\n", *cp); - - for(p = progs; p != NULL; p = p->next) - fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident); - - fprintf(outcf, "\nstruct stub entry_points[] = {\n"); - for(p = progs; p != NULL; p = p->next) { - fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", - p->name, p->ident); - for(s = p->links; s != NULL; s = s->next) - fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", - s->str, p->ident); - } - - fprintf(outcf, "\t{ EXECNAME, crunched_main },\n"); - fprintf(outcf, "\t{ NULL, NULL }\n};\n"); - fclose(outcf); + fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname); + for (cp = crunched_skel; *cp != NULL; cp++) + fprintf(outcf, "%s\n", *cp); + + for (p = progs; p != NULL; p = p->next) + fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident); + + fprintf(outcf, "\nstruct stub entry_points[] = {\n"); + for (p = progs; p != NULL; p = p->next) { + fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", + p->name, p->ident); + for (s = p->links; s != NULL; s = s->next) + fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", + s->str, p->ident); + } + + fprintf(outcf, "\t{ EXECNAME, crunched_main },\n"); + fprintf(outcf, "\t{ NULL, NULL }\n};\n"); + fclose(outcf); } char *genident(char *str) { - char *n,*s,*d; - - /* - * generates a Makefile/C identifier from a program name, mapping '-' to - * '_' and ignoring all other non-identifier characters. This leads to - * programs named "foo.bar" and "foobar" to map to the same identifier. - */ - - if((n = strdup(str)) == NULL) - return NULL; - for(d = s = n; *s != '\0'; s++) { - if(*s == '-') *d++ = '_'; - else if(*s == '_' || isalnum(*s)) *d++ = *s; - } - *d = '\0'; - return n; + char *n, *s, *d; + + /* + * generates a Makefile/C identifier from a program name, + * mapping '-' to '_' and ignoring all other non-identifier + * characters. This leads to programs named "foo.bar" and + * "foobar" to map to the same identifier. + */ + + if ((n = strdup(str)) == NULL) + return NULL; + for (d = s = n; *s != '\0'; s++) { + if (*s == '-') + *d++ = '_'; + else if (*s == '_' || isalnum(*s)) + *d++ = *s; + } + *d = '\0'; + return n; } char *dir_search(char *progname) { - char path[MAXPATHLEN]; - strlst_t *dir; - char *srcdir; + char path[MAXPATHLEN]; + strlst_t *dir; + char *srcdir; - for(dir=srcdirs; dir != NULL; dir=dir->next) { - snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname); - if (!is_dir(path)) { - continue; - } + for (dir = srcdirs; dir != NULL; dir = dir->next) { + snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname); + if (!is_dir(path)) + continue; + + if ((srcdir = strdup(path)) == NULL) + out_of_memory(); - if ((srcdir = strdup(path)) == NULL) - out_of_memory(); - return srcdir; - } - return NULL; + return srcdir; + } + return NULL; } void top_makefile_rules(FILE *outmk) { - prog_t *p; - - fprintf(outmk, "LIBS="); - output_strlst(outmk, libs); - - if (makeobj) { - fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix); - fprintf(outmk, "MAKE=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX) make\n"); - } else { - fprintf(outmk, "MAKE=make\n"); - } - - if (buildopts) { - fprintf(outmk, "BUILDOPTS+="); - output_strlst(outmk, buildopts); - } - - fprintf(outmk, "CRUNCHED_OBJS="); - for(p = progs; p != NULL; p = p->next) - fprintf(outmk, " %s.lo", p->name); - fprintf(outmk, "\n"); - - fprintf(outmk, "SUBMAKE_TARGETS="); - for(p = progs; p != NULL; p = p->next) - fprintf(outmk, " %s_make", p->ident); - fprintf(outmk, "\nSUBCLEAN_TARGETS="); - for(p = progs; p != NULL; p = p->next) - fprintf(outmk, " %s_clean", p->ident); - fprintf(outmk, "\n\n"); - - fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n"); - fprintf(outmk, "exe: %s\n", execfname); - fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS)\n", - execfname, execfname); - fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n", + prog_t *p; + + fprintf(outmk, "LIBS="); + output_strlst(outmk, libs); + + if (makeobj) { + fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix); + fprintf(outmk, "MAKE=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX) " + "make\n"); + } else { + fprintf(outmk, "MAKE=make\n"); + } + + if (buildopts) { + fprintf(outmk, "BUILDOPTS+="); + output_strlst(outmk, buildopts); + } + + fprintf(outmk, "CRUNCHED_OBJS="); + for (p = progs; p != NULL; p = p->next) + fprintf(outmk, " %s.lo", p->name); + fprintf(outmk, "\n"); + + fprintf(outmk, "SUBMAKE_TARGETS="); + for (p = progs; p != NULL; p = p->next) + fprintf(outmk, " %s_make", p->ident); + fprintf(outmk, "\nSUBCLEAN_TARGETS="); + for (p = progs; p != NULL; p = p->next) + fprintf(outmk, " %s_clean", p->ident); + fprintf(outmk, "\n\n"); + + fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n"); + fprintf(outmk, "exe: %s\n", execfname); + fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS)\n", execfname, execfname); + fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n", execfname, execfname); - fprintf(outmk, "\tstrip %s\n", execfname); - fprintf(outmk, "realclean: clean subclean\n"); - fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", - execfname); - fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n"); + fprintf(outmk, "\tstrip %s\n", execfname); + fprintf(outmk, "realclean: clean subclean\n"); + fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname); + fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n"); } void prog_makefile_rules(FILE *outmk, prog_t *p) { - strlst_t *lst; - - fprintf(outmk, "\n# -------- %s\n\n", p->name); - - if(p->srcdir && p->objs) { - fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir); - fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir); - - fprintf(outmk, "%s_OBJDIR=", p->ident); - if (p->objdir) - fprintf(outmk, "%s", p->objdir); - else - fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n", + strlst_t *lst; + + fprintf(outmk, "\n# -------- %s\n\n", p->name); + + if (p->srcdir && p->objs) { + fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir); + fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir); + + fprintf(outmk, "%s_OBJDIR=", p->ident); + if (p->objdir) + fprintf(outmk, "%s", p->objdir); + else + fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n", + p->ident); + fprintf(outmk, "\n"); + + fprintf(outmk, "%s_OBJS=", p->ident); + output_strlst(outmk, p->objs); + if (p->buildopts != NULL) { + fprintf(outmk, "%s_OPTS+=", p->ident); + output_strlst(outmk, p->buildopts); + } + fprintf(outmk, "%s_make:\n", p->ident); + fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident); + if (makeobj) + fprintf(outmk, "$(MAKE) obj && "); + fprintf(outmk, "\\\n"); + fprintf(outmk, "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) depend &&", p->ident); - fprintf(outmk, "\n"); + fprintf(outmk, "\\\n"); + fprintf(outmk, "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) " + "$(%s_OBJS))", + p->ident, p->ident); + fprintf(outmk, "\n"); + fprintf(outmk, "%s_clean:\n", p->ident); + fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(MAKE) clean)\n\n", + p->ident); + } else { + fprintf(outmk, "%s_make:\n", p->ident); + fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n", + p->ident, p->name); + } - fprintf(outmk, "%s_OBJS=", p->ident); - output_strlst(outmk, p->objs); - if (p->buildopts != NULL) { - fprintf(outmk, "%s_OPTS+=", p->ident); - output_strlst(outmk, p->buildopts); - } - fprintf(outmk, "%s_make:\n", p->ident); - fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident); - if (makeobj) - fprintf(outmk, "$(MAKE) obj && "); - fprintf(outmk, "\\\n"); - fprintf(outmk, "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) depend && \\\n" - "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) $(%s_OBJS))\n", - p->ident, p->ident, p->ident); - fprintf(outmk, "%s_clean:\n", p->ident); - fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(MAKE) clean)\n\n", p->ident); - } - else - fprintf(outmk, "%s_make:\n\t@echo \"** cannot make objs for %s\"\n\n", - p->ident, p->name); - - fprintf(outmk, "%s_OBJPATHS=", p->ident); - if (p->objpaths) - output_strlst(outmk, p->objpaths); - else { - for (lst = p->objs; lst != NULL; lst = lst->next) { - fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str); + fprintf(outmk, "%s_OBJPATHS=", p->ident); + if (p->objpaths) + output_strlst(outmk, p->objpaths); + else { + for (lst = p->objs; lst != NULL; lst = lst->next) { + fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str); + } + fprintf(outmk, "\n"); } - fprintf(outmk, "\n"); - } - fprintf(outmk, "%s_stub.c:\n", p->name); - fprintf(outmk, "\techo \"" - "int _crunched_%s_stub(int argc, char **argv, char **envp)" - "{return main(argc,argv,envp);}\" >%s_stub.c\n", + fprintf(outmk, "%s_stub.c:\n", p->name); + fprintf(outmk, "\techo \"" + "int _crunched_%s_stub(int argc, char **argv, char **envp)" + "{return main(argc,argv,envp);}\" >%s_stub.c\n", p->ident, p->name); - fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)\n", + fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)\n", p->name, p->name, p->ident); - fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)\n", + fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)\n", p->name, p->name, p->ident); - fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident); - for(lst = p->keeplist; lst != NULL; lst = lst->next) - fprintf(outmk, "-k _%s ", lst->str); - fprintf(outmk, "%s.lo\n", p->name); + fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident); + for (lst = p->keeplist; lst != NULL; lst = lst->next) + fprintf(outmk, "-k _%s ", lst->str); + fprintf(outmk, "%s.lo\n", p->name); } void output_strlst(FILE *outf, strlst_t *lst) { - for(; lst != NULL; lst = lst->next) - fprintf(outf, " %s", lst->str); - fprintf(outf, "\n"); + for (; lst != NULL; lst = lst->next) + fprintf(outf, " %s", lst->str); + fprintf(outf, "\n"); } @@ -955,64 +1042,70 @@ void output_strlst(FILE *outf, strlst_t *lst) void status(char *str) { - static int lastlen = 0; - int len, spaces; + static int lastlen = 0; + int len, spaces; - if(!verbose) return; + if (!verbose) + return; - len = strlen(str); - spaces = lastlen - len; - if(spaces < 1) spaces = 1; + len = strlen(str); + spaces = lastlen - len; + if (spaces < 1) + spaces = 1; - fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " "); - fflush(stderr); - lastlen = len; + fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " "); + fflush(stderr); + lastlen = len; } void out_of_memory(void) { - err(1, "%s: %d: out of memory, stopping", infilename, linenum); + err(1, "%s: %d: out of memory, stopping", infilename, linenum); } void add_string(strlst_t **listp, char *str) { - strlst_t *p1, *p2; + strlst_t *p1, *p2; - /* add to end, but be smart about dups */ + /* add to end, but be smart about dups */ - for(p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next) - if(!strcmp(p2->str, str)) return; + for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next) + if (!strcmp(p2->str, str)) + return; - p2 = malloc(sizeof(strlst_t)); - if(p2) { - p2->next = NULL ; - p2->str = strdup(str); - } - if(!p2 || !p2->str) - out_of_memory(); + p2 = malloc(sizeof(strlst_t)); + if (p2) { + p2->next = NULL; + p2->str = strdup(str); + } + if (!p2 || !p2->str) + out_of_memory(); - if(p1 == NULL) *listp = p2; - else p1->next = p2; + if (p1 == NULL) + *listp = p2; + else + p1->next = p2; } int is_dir(char *pathname) { - struct stat buf; + struct stat buf; + + if (stat(pathname, &buf) == -1) + return 0; - if(stat(pathname, &buf) == -1) - return 0; - return S_ISDIR(buf.st_mode); + return S_ISDIR(buf.st_mode); } int is_nonempty_file(char *pathname) { - struct stat buf; + struct stat buf; - if(stat(pathname, &buf) == -1) - return 0; + if (stat(pathname, &buf) == -1) + return 0; - return S_ISREG(buf.st_mode) && buf.st_size > 0; + return S_ISREG(buf.st_mode) && buf.st_size > 0; } -- cgit v1.1