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