Home | History | Annotate | Line # | Download | only in crunchgen
crunchgen.c revision 1.14
      1 /*	$NetBSD: crunchgen.c,v 1.14 2000/01/24 18:07:54 mycroft Exp $	*/
      2 /*
      3  * Copyright (c) 1994 University of Maryland
      4  * All Rights Reserved.
      5  *
      6  * Permission to use, copy, modify, distribute, and sell this software and its
      7  * documentation for any purpose is hereby granted without fee, provided that
      8  * the above copyright notice appear in all copies and that both that
      9  * copyright notice and this permission notice appear in supporting
     10  * documentation, and that the name of U.M. not be used in advertising or
     11  * publicity pertaining to distribution of the software without specific,
     12  * written prior permission.  U.M. makes no representations about the
     13  * suitability of this software for any purpose.  It is provided "as is"
     14  * without express or implied warranty.
     15  *
     16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
     18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
     20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     22  *
     23  * Author: James da Silva, Systems Design and Analysis Group
     24  *			   Computer Science Department
     25  *			   University of Maryland at College Park
     26  */
     27 /*
     28  * ========================================================================
     29  * crunchgen.c
     30  *
     31  * Generates a Makefile and main C file for a crunched executable,
     32  * from specs given in a .conf file.
     33  */
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: crunchgen.c,v 1.14 2000/01/24 18:07:54 mycroft Exp $");
     37 #endif
     38 
     39 #include <stdlib.h>
     40 #include <unistd.h>
     41 #include <stdio.h>
     42 #include <ctype.h>
     43 #include <string.h>
     44 
     45 #include <sys/types.h>
     46 #include <sys/stat.h>
     47 #include <sys/param.h>
     48 #include <sys/utsname.h>
     49 
     50 #define CRUNCH_VERSION	"0.2"
     51 
     52 #define MAXLINELEN	16384
     53 #define MAXFIELDS 	 2048
     54 
     55 
     56 /* internal representation of conf file: */
     57 
     58 /* simple lists of strings suffice for most parms */
     59 
     60 typedef struct strlst {
     61     struct strlst *next;
     62     char *str;
     63 } strlst_t;
     64 
     65 /* progs have structure, each field can be set with "special" or calculated */
     66 
     67 typedef struct prog {
     68     struct prog *next;
     69     char *name, *ident;
     70     char *srcdir, *objdir;
     71     strlst_t *objs, *objpaths;
     72     strlst_t *links;
     73     int goterror;
     74 } prog_t;
     75 
     76 
     77 /* global state */
     78 
     79 strlst_t *srcdirs = NULL;
     80 strlst_t *libs    = NULL;
     81 prog_t   *progs   = NULL;
     82 
     83 char line[MAXLINELEN];
     84 
     85 char confname[MAXPATHLEN], infilename[MAXPATHLEN];
     86 char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
     87 char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
     88 char topdir[MAXPATHLEN];
     89 char libdir[MAXPATHLEN] = "/usr/lib";
     90 int linenum = -1;
     91 int goterror = 0;
     92 
     93 char *pname = "crunchgen";
     94 
     95 int verbose, readcache;	/* options */
     96 int reading_cache;
     97 char *machine;
     98 
     99 /* general library routines */
    100 
    101 void status(char *str);
    102 void out_of_memory(void);
    103 void add_string(strlst_t **listp, char *str);
    104 int is_dir(char *pathname);
    105 int is_nonempty_file(char *pathname);
    106 
    107 /* helper routines for main() */
    108 
    109 void usage(void);
    110 void parse_conf_file(void);
    111 void gen_outputs(void);
    112 
    113 
    114 int main(int argc, char **argv)
    115 {
    116     char *p;
    117     int optc;
    118     extern int optind;
    119     extern char *optarg;
    120 
    121     if ((machine = getenv("MACHINE")) == NULL) {
    122 	struct utsname utsname;
    123 
    124 	if (uname(&utsname) == -1) {
    125 	    perror("uname");
    126 	    exit(1);
    127 	}
    128 	machine = utsname.machine;
    129     }
    130     verbose = 1;
    131     readcache = 1;
    132     *outmkname = *outcfname = *execfname = '\0';
    133 
    134     if(argc > 0) pname = argv[0];
    135 
    136     while((optc = getopt(argc, argv, "m:c:e:fqD:L:")) != -1) {
    137 	switch(optc) {
    138 	case 'f':	readcache = 0; break;
    139 	case 'q':	verbose = 0; break;
    140 
    141 	case 'm':	strcpy(outmkname, optarg); break;
    142 	case 'c':	strcpy(outcfname, optarg); break;
    143 	case 'e':	strcpy(execfname, optarg); break;
    144 
    145 	case 'D':	strcpy(topdir, optarg); break;
    146 	case 'L':	strcpy(libdir, optarg); break;
    147 
    148 	case '?':
    149 	default:	usage();
    150 	}
    151     }
    152 
    153     argc -= optind;
    154     argv += optind;
    155 
    156     if(argc != 1) usage();
    157 
    158     /*
    159      * generate filenames
    160      */
    161 
    162     strcpy(infilename, argv[0]);
    163 
    164     /* confname = `basename infilename .conf` */
    165 
    166     if((p=strrchr(infilename, '/')) != NULL) strcpy(confname, p+1);
    167     else strcpy(confname, infilename);
    168     if((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) *p = '\0';
    169 
    170     if (!*outmkname)
    171 	(void)snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
    172     if (!*outcfname)
    173 	(void)snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
    174     if (!*execfname)
    175 	(void)snprintf(execfname, sizeof(execfname), "%s", confname);
    176 
    177     (void)snprintf(cachename, sizeof(cachename), "%s.cache", confname);
    178     (void)snprintf(tempfname, sizeof(tempfname), "/tmp/%sXXXXXX", confname);
    179 
    180     parse_conf_file();
    181     gen_outputs();
    182 
    183     exit(goterror);
    184 }
    185 
    186 
    187 void usage(void)
    188 {
    189     fprintf(stderr,
    190 	"%s [-fq] [-m <makefile>] [-c <c file>] [-e <exec file>] <conffile>\n",
    191 	    pname);
    192     exit(1);
    193 }
    194 
    195 
    196 /*
    197  * ========================================================================
    198  * parse_conf_file subsystem
    199  *
    200  */
    201 
    202 /* helper routines for parse_conf_file */
    203 
    204 void parse_one_file(char *filename);
    205 void parse_line(char *line, int *fc, char **fv, int nf);
    206 void add_srcdirs(int argc, char **argv);
    207 void add_progs(int argc, char **argv);
    208 void add_link(int argc, char **argv);
    209 void add_libs(int argc, char **argv);
    210 void add_special(int argc, char **argv);
    211 
    212 prog_t *find_prog(char *str);
    213 void add_prog(char *progname);
    214 
    215 
    216 void parse_conf_file(void)
    217 {
    218     if(!is_nonempty_file(infilename)) {
    219 	fprintf(stderr, "%s: fatal: input file \"%s\" not found.\n",
    220 		pname, infilename);
    221 	exit(1);
    222     }
    223     parse_one_file(infilename);
    224     if(readcache && is_nonempty_file(cachename)) {
    225 	reading_cache = 1;
    226 	parse_one_file(cachename);
    227     }
    228 }
    229 
    230 
    231 void parse_one_file(char *filename)
    232 {
    233     char *fieldv[MAXFIELDS];
    234     int fieldc;
    235     void (*f)(int c, char **v);
    236     FILE *cf;
    237 
    238     (void)snprintf(line, sizeof(line), "reading %s", filename);
    239     status(line);
    240     strcpy(curfilename, filename);
    241 
    242     if((cf = fopen(curfilename, "r")) == NULL) {
    243 	perror(curfilename);
    244 	goterror = 1;
    245 	return;
    246     }
    247 
    248     linenum = 0;
    249     while(fgets(line, MAXLINELEN, cf) != NULL) {
    250 	linenum++;
    251 	parse_line(line, &fieldc, fieldv, MAXFIELDS);
    252 	if(fieldc < 1) continue;
    253 	if(!strcmp(fieldv[0], "srcdirs"))	f = add_srcdirs;
    254 	else if(!strcmp(fieldv[0], "progs"))    f = add_progs;
    255 	else if(!strcmp(fieldv[0], "ln"))	f = add_link;
    256 	else if(!strcmp(fieldv[0], "libs"))	f = add_libs;
    257 	else if(!strcmp(fieldv[0], "special"))	f = add_special;
    258 	else {
    259 	    fprintf(stderr, "%s:%d: skipping unknown command `%s'.\n",
    260 		    curfilename, linenum, fieldv[0]);
    261 	    goterror = 1;
    262 	    continue;
    263 	}
    264 	if(fieldc < 2) {
    265 	    fprintf(stderr,
    266 		    "%s:%d: %s command needs at least 1 argument, skipping.\n",
    267 		    curfilename, linenum, fieldv[0]);
    268 	    goterror = 1;
    269 	    continue;
    270 	}
    271 	f(fieldc, fieldv);
    272     }
    273 
    274     if(ferror(cf)) {
    275 	perror(curfilename);
    276 	goterror = 1;
    277     }
    278     fclose(cf);
    279 }
    280 
    281 
    282 void parse_line(char *line, int *fc, char **fv, int nf)
    283 {
    284     char *p;
    285 
    286     p = line;
    287     *fc = 0;
    288     while(1) {
    289 	while(isspace(*p)) p++;
    290 	if(*p == '\0' || *p == '#') break;
    291 
    292 	if(*fc < nf) fv[(*fc)++] = p;
    293 	while(*p && !isspace(*p) && *p != '#') p++;
    294 	if(*p == '\0' || *p == '#') break;
    295 	*p++ = '\0';
    296     }
    297     if(*p) *p = '\0';		/* needed for '#' case */
    298 }
    299 
    300 
    301 void add_srcdirs(int argc, char **argv)
    302 {
    303     int i;
    304     char tmppath[MAXPATHLEN];
    305 
    306     for(i=1;i<argc;i++) {
    307 	if (argv[i][0] == '/' || topdir[0] == '\0')
    308 		strcpy(tmppath, argv[i]);
    309 	else {
    310 		strcpy(tmppath, topdir);
    311 		strcat(tmppath, "/");
    312 		strcat(tmppath, argv[i]);
    313 	}
    314 	if(is_dir(tmppath))
    315 	    add_string(&srcdirs, tmppath);
    316 	else {
    317 	    fprintf(stderr, "%s:%d: `%s' is not a directory, skipping it.\n",
    318 		    curfilename, linenum, tmppath);
    319 	    goterror = 1;
    320 	}
    321     }
    322 }
    323 
    324 
    325 void add_progs(int argc, char **argv)
    326 {
    327     int i;
    328 
    329     for(i=1;i<argc;i++)
    330 	add_prog(argv[i]);
    331 }
    332 
    333 
    334 void add_prog(char *progname)
    335 {
    336     prog_t *p1, *p2;
    337 
    338     /* add to end, but be smart about dups */
    339 
    340     for(p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
    341 	if(!strcmp(p2->name, progname)) return;
    342 
    343     p2 = malloc(sizeof(prog_t));
    344     if(p2) p2->name = strdup(progname);
    345     if(!p2 || !p2->name)
    346 	out_of_memory();
    347 
    348     p2->next = NULL;
    349     if(p1 == NULL) progs = p2;
    350     else p1->next = p2;
    351 
    352     p2->ident = p2->srcdir = p2->objdir = NULL;
    353     p2->links = p2->objs = NULL;
    354     p2->goterror = 0;
    355 }
    356 
    357 
    358 void add_link(int argc, char **argv)
    359 {
    360     int i;
    361     prog_t *p = find_prog(argv[1]);
    362 
    363     if(p == NULL) {
    364 	fprintf(stderr,
    365 		"%s:%d: no prog %s previously declared, skipping link.\n",
    366 		curfilename, linenum, argv[1]);
    367 	goterror = 1;
    368 	return;
    369     }
    370     for(i=2;i<argc;i++)
    371 	add_string(&p->links, argv[i]);
    372 }
    373 
    374 
    375 void add_libs(int argc, char **argv)
    376 {
    377     int i;
    378 
    379     for(i=1;i<argc;i++)
    380 	add_string(&libs, argv[i]);
    381 }
    382 
    383 
    384 void add_special(int argc, char **argv)
    385 {
    386     int i;
    387     prog_t *p = find_prog(argv[1]);
    388 
    389     if(p == NULL) {
    390 	if(reading_cache) return;
    391 	fprintf(stderr,
    392 		"%s:%d: no prog %s previously declared, skipping special.\n",
    393 		curfilename, linenum, argv[1]);
    394 	goterror = 1;
    395 	return;
    396     }
    397 
    398     if(!strcmp(argv[2], "ident")) {
    399 	if(argc != 4) goto argcount;
    400 	if((p->ident = strdup(argv[3])) == NULL)
    401 	    out_of_memory();
    402     }
    403     else if(!strcmp(argv[2], "srcdir")) {
    404 	if(argc != 4) goto argcount;
    405 	if (argv[3][0] == '/' || topdir[0] == '\0') {
    406 	    if((p->srcdir = strdup(argv[3])) == NULL)
    407 		out_of_memory();
    408 	} else {
    409 	    char tmppath[MAXPATHLEN];
    410 	    strcpy(tmppath, topdir);
    411 	    strcat(tmppath, "/");
    412 	    strcat(tmppath, argv[3]);
    413 	    if((p->srcdir = strdup(tmppath)) == NULL)
    414 		out_of_memory();
    415 	}
    416     }
    417     else if(!strcmp(argv[2], "objdir")) {
    418 	if(argc != 4) goto argcount;
    419 	if((p->objdir = strdup(argv[3])) == NULL)
    420 	    out_of_memory();
    421     }
    422     else if(!strcmp(argv[2], "objs")) {
    423 	p->objs = NULL;
    424 	for(i=3;i<argc;i++)
    425 	    add_string(&p->objs, argv[i]);
    426     }
    427     else if(!strcmp(argv[2], "objpaths")) {
    428 	p->objpaths = NULL;
    429 	for(i=3;i<argc;i++)
    430 	    add_string(&p->objpaths, argv[i]);
    431     }
    432     else {
    433 	fprintf(stderr, "%s:%d: bad parameter name `%s', skipping line.\n",
    434 		curfilename, linenum, argv[2]);
    435 	goterror = 1;
    436     }
    437     return;
    438 
    439 
    440  argcount:
    441     fprintf(stderr,
    442 	    "%s:%d: too %s arguments, expected \"special %s %s <string>\".\n",
    443 	    curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
    444     goterror = 1;
    445 }
    446 
    447 
    448 prog_t *find_prog(char *str)
    449 {
    450     prog_t *p;
    451 
    452     for(p = progs; p != NULL; p = p->next)
    453 	if(!strcmp(p->name, str)) return p;
    454 
    455     return NULL;
    456 }
    457 
    458 
    459 /*
    460  * ========================================================================
    461  * gen_outputs subsystem
    462  *
    463  */
    464 
    465 /* helper subroutines */
    466 
    467 void remove_error_progs(void);
    468 void fillin_program(prog_t *p);
    469 void gen_specials_cache(void);
    470 void gen_output_makefile(void);
    471 void gen_output_cfile(void);
    472 
    473 void fillin_program_objs(prog_t *p, char *path);
    474 void top_makefile_rules(FILE *outmk);
    475 void prog_makefile_rules(FILE *outmk, prog_t *p);
    476 void output_strlst(FILE *outf, strlst_t *lst);
    477 char *genident(char *str);
    478 char *dir_search(char *progname);
    479 
    480 
    481 void gen_outputs(void)
    482 {
    483     prog_t *p;
    484 
    485     for(p = progs; p != NULL; p = p->next)
    486 	fillin_program(p);
    487 
    488     remove_error_progs();
    489     gen_specials_cache();
    490     gen_output_cfile();
    491     gen_output_makefile();
    492     status("");
    493     fprintf(stderr,
    494 	    "Run \"make -f %s objs exe\" to build crunched binary.\n",
    495 	    outmkname);
    496 }
    497 
    498 
    499 void fillin_program(prog_t *p)
    500 {
    501     char path[MAXPATHLEN];
    502     char *srcparent;
    503     strlst_t *s;
    504 
    505     (void)snprintf(line, sizeof(line), "filling in parms for %s", p->name);
    506     status(line);
    507 
    508     if(!p->ident)
    509 	p->ident = genident(p->name);
    510     if(!p->srcdir) {
    511 	srcparent = dir_search(p->name);
    512 	if(srcparent) {
    513 	    (void)snprintf(path, sizeof(path), "%s/%s", srcparent, p->name);
    514 	    if(is_dir(path))
    515 		p->srcdir = strdup(path);
    516 	}
    517     }
    518     if(!p->objdir && p->srcdir) {
    519 	(void)snprintf(path, sizeof(path), "%s/obj.%s", p->srcdir, machine);
    520 	if(is_dir(path))
    521 	    p->objdir = strdup(path);
    522 	else {
    523 	    (void)snprintf(path, sizeof(path), "%s/obj", p->srcdir);
    524 	    if(is_dir(path))
    525 		p->objdir = strdup(path);
    526 	    else
    527 	        p->objdir = p->srcdir;
    528         }
    529     }
    530 
    531     if(p->srcdir)
    532 	(void)snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
    533     if(!p->objs && p->srcdir && is_nonempty_file(path))
    534 	fillin_program_objs(p, p->srcdir);
    535 
    536     if(!p->objpaths && p->objdir && p->objs)
    537 	for(s = p->objs; s != NULL; s = s->next) {
    538 	    (void)snprintf(line, sizeof(line), "%s/%s", p->objdir, s->str);
    539 	    add_string(&p->objpaths, line);
    540 	}
    541 
    542     if(!p->srcdir && verbose)
    543 	fprintf(stderr, "%s: %s: warning: could not find source directory.\n",
    544 		infilename, p->name);
    545     if(!p->objs && verbose)
    546 	fprintf(stderr, "%s: %s: warning: could not find any .o files.\n",
    547 		infilename, p->name);
    548 
    549     if(!p->objpaths) {
    550 	fprintf(stderr,
    551 		"%s: %s: error: no objpaths specified or calculated.\n",
    552 		infilename, p->name);
    553 	p->goterror = goterror = 1;
    554     }
    555 }
    556 
    557 void fillin_program_objs(prog_t *p, char *dirpath)
    558 {
    559     char *obj, *cp;
    560     int rc;
    561     int fd;
    562     FILE *f;
    563 
    564     /* discover the objs from the srcdir Makefile */
    565 
    566     if((fd = mkstemp(tempfname)) < 0) {
    567 	perror(tempfname);
    568 	exit(1);
    569     }
    570 
    571     if((f = fdopen(fd, "w")) == NULL) {
    572 	perror(tempfname);
    573 	goterror = 1;
    574 	return;
    575     }
    576 
    577     fprintf(f, ".include \"${.CURDIR}/Makefile\"\n");
    578     fprintf(f, ".if defined(PROG)\n");
    579     fprintf(f, "OBJS?= ${PROG}.o\n");
    580     fprintf(f, ".endif\n");
    581     fprintf(f, "crunchgen_objs:\n\t@echo 'OBJS= '${OBJS}\n");
    582     fclose(f);
    583 
    584     (void)snprintf(line, sizeof(line),
    585 	"cd %s && make -f %s crunchgen_objs 2>&1", dirpath, tempfname);
    586     if((f = popen(line, "r+")) == NULL) {
    587 	perror("submake pipe");
    588 	goterror = 1;
    589 	return;
    590     }
    591 
    592     while(fgets(line, MAXLINELEN, f)) {
    593 	if(strncmp(line, "OBJS= ", 6)) {
    594 	    if (strcmp(line,
    595 	   	"sh: warning: running as root with dot in PATH\n") == 0)
    596 		    continue;
    597 	    fprintf(stderr, "make error: %s", line);
    598 	    goterror = 1;
    599 	    continue;
    600 	}
    601 	cp = line + 6;
    602 	while(isspace(*cp)) cp++;
    603 	while(*cp) {
    604 	    obj = cp;
    605 	    while(*cp && !isspace(*cp)) cp++;
    606 	    if(*cp) *cp++ = '\0';
    607 	    add_string(&p->objs, obj);
    608 	    while(isspace(*cp)) cp++;
    609 	}
    610     }
    611     if((rc=pclose(f)) != 0) {
    612 	fprintf(stderr, "make error: make returned %d\n", rc);
    613 	goterror = 1;
    614     }
    615     unlink(tempfname);
    616 }
    617 
    618 void remove_error_progs(void)
    619 {
    620     prog_t *p1, *p2;
    621 
    622     p1 = NULL; p2 = progs;
    623     while(p2 != NULL) {
    624 	if(!p2->goterror)
    625 	    p1 = p2, p2 = p2->next;
    626 	else {
    627 	    /* delete it from linked list */
    628 	    fprintf(stderr, "%s: %s: ignoring program because of errors.\n",
    629 		    infilename, p2->name);
    630 	    if(p1) p1->next = p2->next;
    631 	    else progs = p2->next;
    632 	    p2 = p2->next;
    633 	}
    634     }
    635 }
    636 
    637 void gen_specials_cache(void)
    638 {
    639     FILE *cachef;
    640     prog_t *p;
    641 
    642     (void)snprintf(line, sizeof(line), "generating %s", cachename);
    643     status(line);
    644 
    645     if((cachef = fopen(cachename, "w")) == NULL) {
    646 	perror(cachename);
    647 	goterror = 1;
    648 	return;
    649     }
    650 
    651     fprintf(cachef, "# %s - parm cache generated from %s by crunchgen %s\n\n",
    652 	    cachename, infilename, CRUNCH_VERSION);
    653 
    654     for(p = progs; p != NULL; p = p->next) {
    655 	fprintf(cachef, "\n");
    656 	if(p->srcdir)
    657 	    fprintf(cachef, "special %s srcdir %s\n", p->name, p->srcdir);
    658 	if(p->objdir)
    659 	    fprintf(cachef, "special %s objdir %s\n", p->name, p->objdir);
    660 	if(p->objs) {
    661 	    fprintf(cachef, "special %s objs", p->name);
    662 	    output_strlst(cachef, p->objs);
    663 	}
    664 	fprintf(cachef, "special %s objpaths", p->name);
    665 	output_strlst(cachef, p->objpaths);
    666     }
    667     fclose(cachef);
    668 }
    669 
    670 
    671 void gen_output_makefile(void)
    672 {
    673     prog_t *p;
    674     FILE *outmk;
    675 
    676     (void)snprintf(line, sizeof(line), "generating %s", outmkname);
    677     status(line);
    678 
    679     if((outmk = fopen(outmkname, "w")) == NULL) {
    680 	perror(outmkname);
    681 	goterror = 1;
    682 	return;
    683     }
    684 
    685     fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
    686 	    outmkname, infilename, CRUNCH_VERSION);
    687 
    688     top_makefile_rules(outmk);
    689 
    690     for(p = progs; p != NULL; p = p->next)
    691 	prog_makefile_rules(outmk, p);
    692 
    693     fprintf(outmk, "\n.include <bsd.sys.mk>\n");
    694     fprintf(outmk, "\n# ========\n");
    695     fclose(outmk);
    696 }
    697 
    698 
    699 void gen_output_cfile(void)
    700 {
    701     extern char *crunched_skel[];
    702     char **cp;
    703     FILE *outcf;
    704     prog_t *p;
    705     strlst_t *s;
    706 
    707     (void)snprintf(line, sizeof(line), "generating %s", outcfname);
    708     status(line);
    709 
    710     if((outcf = fopen(outcfname, "w")) == NULL) {
    711 	perror(outcfname);
    712 	goterror = 1;
    713 	return;
    714     }
    715 
    716     fprintf(outcf,
    717 	  "/* %s - generated from %s by crunchgen %s */\n",
    718 	    outcfname, infilename, CRUNCH_VERSION);
    719 
    720     fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
    721     for(cp = crunched_skel; *cp != NULL; cp++)
    722 	fprintf(outcf, "%s\n", *cp);
    723 
    724     for(p = progs; p != NULL; p = p->next)
    725 	fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
    726 
    727     fprintf(outcf, "\nstruct stub entry_points[] = {\n");
    728     for(p = progs; p != NULL; p = p->next) {
    729 	fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
    730 		p->name, p->ident);
    731 	for(s = p->links; s != NULL; s = s->next)
    732 	    fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
    733 		    s->str, p->ident);
    734     }
    735 
    736     fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
    737     fprintf(outcf, "\t{ NULL, NULL }\n};\n");
    738     fclose(outcf);
    739 }
    740 
    741 
    742 char *genident(char *str)
    743 {
    744     char *n,*s,*d;
    745 
    746     /*
    747      * generates a Makefile/C identifier from a program name, mapping '-' to
    748      * '_' and ignoring all other non-identifier characters.  This leads to
    749      * programs named "foo.bar" and "foobar" to map to the same identifier.
    750      */
    751 
    752     if((n = strdup(str)) == NULL)
    753 	return NULL;
    754     for(d = s = n; *s != '\0'; s++) {
    755 	if(*s == '-') *d++ = '_';
    756 	else if(*s == '_' || isalnum(*s)) *d++ = *s;
    757     }
    758     *d = '\0';
    759     return n;
    760 }
    761 
    762 
    763 char *dir_search(char *progname)
    764 {
    765     char path[MAXPATHLEN];
    766     strlst_t *dir;
    767 
    768     for(dir=srcdirs; dir != NULL; dir=dir->next) {
    769 	(void)snprintf(path, sizeof(path), "%s/%s", dir->str, progname);
    770 	if(is_dir(path)) return dir->str;
    771     }
    772     return NULL;
    773 }
    774 
    775 
    776 void top_makefile_rules(FILE *outmk)
    777 {
    778     prog_t *p;
    779 
    780     fprintf(outmk, "STRIP?=strip\n");
    781     fprintf(outmk, "LIBS=");
    782     fprintf(outmk, "-L%s ", libdir);
    783     output_strlst(outmk, libs);
    784 
    785     fprintf(outmk, "CRUNCHED_OBJS=");
    786     for(p = progs; p != NULL; p = p->next)
    787 	fprintf(outmk, " %s.cro", p->name);
    788     fprintf(outmk, "\n");
    789 
    790     fprintf(outmk, "SUBMAKE_TARGETS=");
    791     for(p = progs; p != NULL; p = p->next)
    792 	fprintf(outmk, " %s_make", p->ident);
    793     fprintf(outmk, "\n\n");
    794 
    795     fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS)\n",
    796 	    execfname, execfname);
    797     fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
    798 	    execfname, execfname);
    799     fprintf(outmk, "\t$(STRIP) %s\n", execfname);
    800     fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
    801     fprintf(outmk, "exe: %s\n", execfname);
    802     fprintf(outmk, "clean:\n\trm -f %s *.cro *.o *_stub.c\n",
    803 	    execfname);
    804 }
    805 
    806 
    807 void prog_makefile_rules(FILE *outmk, prog_t *p)
    808 {
    809     fprintf(outmk, "\n# -------- %s\n\n", p->name);
    810 
    811     if(p->srcdir && p->objs) {
    812 	fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
    813 	fprintf(outmk, "%s_OBJS=", p->ident);
    814 	output_strlst(outmk, p->objs);
    815 	fprintf(outmk, "%s_make:\n", p->ident);
    816 	fprintf(outmk, "\t(cd $(%s_SRCDIR); make $(%s_OBJS))\n\n",
    817 		p->ident, p->ident);
    818     }
    819     else
    820 	fprintf(outmk, "%s_make:\n\t@echo \"** cannot make objs for %s\"\n\n",
    821 		p->ident, p->name);
    822 
    823     fprintf(outmk,   "%s_OBJPATHS=", p->ident);
    824     output_strlst(outmk, p->objpaths);
    825 
    826     fprintf(outmk, "%s_stub.c:\n", p->name);
    827     fprintf(outmk, "\techo \""
    828 	           "int _crunched_%s_stub(int argc, char **argv, char **envp)"
    829 	           "{return main(argc,argv,envp);}\" >%s_stub.c\n",
    830 	    p->ident, p->name);
    831     fprintf(outmk, "%s.cro: %s_stub.o $(%s_OBJPATHS)\n",
    832 	    p->name, p->name, p->ident);
    833     fprintf(outmk, "\t${LD} -dc -r -o %s.cro %s_stub.o $(%s_OBJPATHS)\n",
    834 	    p->name, p->name, p->ident);
    835     fprintf(outmk, "\tcrunchide -k _crunched_%s_stub %s.cro\n",
    836 	    p->ident, p->name);
    837 }
    838 
    839 void output_strlst(FILE *outf, strlst_t *lst)
    840 {
    841     for(; lst != NULL; lst = lst->next)
    842 	fprintf(outf, " %s", lst->str);
    843     fprintf(outf, "\n");
    844 }
    845 
    846 
    847 /*
    848  * ========================================================================
    849  * general library routines
    850  *
    851  */
    852 
    853 void status(char *str)
    854 {
    855     static int lastlen = 0;
    856     int len, spaces;
    857 
    858     if(!verbose) return;
    859 
    860     len = strlen(str);
    861     spaces = lastlen - len;
    862     if(spaces < 1) spaces = 1;
    863 
    864     fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
    865     fflush(stderr);
    866     lastlen = len;
    867 }
    868 
    869 
    870 void out_of_memory(void)
    871 {
    872     fprintf(stderr, "%s: %d: out of memory, stopping.\n", infilename, linenum);
    873     exit(1);
    874 }
    875 
    876 
    877 void add_string(strlst_t **listp, char *str)
    878 {
    879     strlst_t *p1, *p2;
    880 
    881     /* add to end, but be smart about dups */
    882 
    883     for(p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
    884 	if(!strcmp(p2->str, str)) return;
    885 
    886     p2 = malloc(sizeof(strlst_t));
    887     if(p2) p2->str = strdup(str);
    888     if(!p2 || !p2->str)
    889 	out_of_memory();
    890 
    891     p2->next = NULL;
    892     if(p1 == NULL) *listp = p2;
    893     else p1->next = p2;
    894 }
    895 
    896 
    897 int is_dir(char *pathname)
    898 {
    899     struct stat buf;
    900 
    901     if(stat(pathname, &buf) == -1)
    902 	return 0;
    903     return S_ISDIR(buf.st_mode);
    904 }
    905 
    906 int is_nonempty_file(char *pathname)
    907 {
    908     struct stat buf;
    909 
    910     if(stat(pathname, &buf) == -1)
    911 	return 0;
    912 
    913     return S_ISREG(buf.st_mode) && buf.st_size > 0;
    914 }
    915