xref: /freebsd/usr.sbin/crunch/crunchgen/crunchgen.c (revision 63f537551380d2dab29fa402ad1269feae17e594)
1 /*
2  * Copyright (c) 1994 University of Maryland
3  * All Rights Reserved.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of U.M. not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  U.M. makes no representations about the
12  * suitability of this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  *
15  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
17  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  *
22  * Author: James da Silva, Systems Design and Analysis Group
23  *			   Computer Science Department
24  *			   University of Maryland at College Park
25  */
26 /*
27  * ========================================================================
28  * crunchgen.c
29  *
30  * Generates a Makefile and main C file for a crunched executable,
31  * from specs given in a .conf file.
32  */
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 
38 #include <ctype.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <paths.h>
42 #include <stdbool.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sysexits.h>
47 #include <unistd.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;	/* link field */
68 	char *name;		/* program name */
69 	char *ident;		/* C identifier for the program name */
70 	char *srcdir;
71 	char *realsrcdir;
72 	char *objdir;
73 	char *objvar;		/* Makefile variable to replace OBJS */
74 	strlst_t *objs, *objpaths;
75 	strlst_t *buildopts;
76 	strlst_t *keeplist;
77 	strlst_t *links;
78 	strlst_t *libs;
79 	strlst_t *libs_so;
80 	int goterror;
81 } prog_t;
82 
83 
84 /* global state */
85 
86 static strlst_t *buildopts = NULL;
87 static strlst_t *srcdirs   = NULL;
88 static strlst_t *libs      = NULL;
89 static strlst_t *libs_so   = NULL;
90 static prog_t   *progs     = NULL;
91 
92 static char confname[MAXPATHLEN], infilename[MAXPATHLEN];
93 static char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
94 static char tempfname[MAXPATHLEN], cachename[MAXPATHLEN];
95 static char curfilename[MAXPATHLEN];
96 static bool tempfname_initialized = false;
97 static char outhdrname[MAXPATHLEN] ;	/* user-supplied header for *.mk */
98 static const char *objprefix;		/* where are the objects ? */
99 static const char *path_make;
100 static int linenum = -1;
101 static int goterror = 0;
102 
103 static int verbose, readcache;	/* options */
104 static int reading_cache;
105 static int makeobj = 0;		/* add 'make obj' rules to the makefile */
106 
107 static int list_mode;
108 
109 /* general library routines */
110 
111 void status(const char *str);
112 void out_of_memory(void);
113 void add_string(strlst_t **listp, char *str);
114 int is_dir(const char *pathname);
115 int is_nonempty_file(const char *pathname);
116 int subtract_strlst(strlst_t **lista, strlst_t **listb);
117 int in_list(strlst_t **listp, char *str);
118 
119 /* helper routines for main() */
120 
121 void usage(void);
122 void parse_conf_file(void);
123 void gen_outputs(void);
124 
125 extern const char *crunched_skel[];
126 
127 
128 int
129 main(int argc, char **argv)
130 {
131 	char *p;
132 	int optc;
133 
134 	verbose = 1;
135 	readcache = 1;
136 	*outmkname = *outcfname = *execfname = '\0';
137 
138 	path_make = getenv("MAKE");
139 	if (path_make == NULL || *path_make == '\0')
140 		path_make = "make";
141 
142 	p = getenv("MAKEOBJDIRPREFIX");
143 	if (p == NULL || *p == '\0')
144 		objprefix = "/usr/obj"; /* default */
145 	else
146 		if ((objprefix = strdup(p)) == NULL)
147 			out_of_memory();
148 
149 	while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) {
150 		switch(optc) {
151 		case 'f':
152 			readcache = 0;
153 			break;
154 		case 'o':
155 			makeobj = 1;
156 			break;
157 		case 'q':
158 			verbose = 0;
159 			break;
160 
161 		case 'm':
162 			strlcpy(outmkname, optarg, sizeof(outmkname));
163 			break;
164 		case 'p':
165 			if ((objprefix = strdup(optarg)) == NULL)
166 				out_of_memory();
167 			break;
168 
169 		case 'h':
170 			strlcpy(outhdrname, optarg, sizeof(outhdrname));
171 			break;
172 		case 'c':
173 			strlcpy(outcfname, optarg, sizeof(outcfname));
174 			break;
175 		case 'e':
176 			strlcpy(execfname, optarg, sizeof(execfname));
177 			break;
178 
179 		case 'l':
180 			list_mode++;
181 			verbose = 0;
182 			break;
183 
184 		case '?':
185 		default:
186 			usage();
187 		}
188 	}
189 
190 	argc -= optind;
191 	argv += optind;
192 
193 	if (argc != 1)
194 		usage();
195 
196 	/*
197 	 * generate filenames
198 	 */
199 
200 	strlcpy(infilename, argv[0], sizeof(infilename));
201 
202 	/* confname = `basename infilename .conf` */
203 
204 	if ((p=strrchr(infilename, '/')) != NULL)
205 		strlcpy(confname, p + 1, sizeof(confname));
206 	else
207 		strlcpy(confname, infilename, sizeof(confname));
208 
209 	if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
210 		*p = '\0';
211 
212 	if (!*outmkname)
213 		snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
214 	if (!*outcfname)
215 		snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
216 	if (!*execfname)
217 		snprintf(execfname, sizeof(execfname), "%s", confname);
218 
219 	snprintf(cachename, sizeof(cachename), "%s.cache", confname);
220 	snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX",
221 	getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname);
222 	tempfname_initialized = false;
223 
224 	parse_conf_file();
225 	if (list_mode)
226 		exit(goterror);
227 
228 	gen_outputs();
229 
230 	exit(goterror);
231 }
232 
233 
234 void
235 usage(void)
236 {
237 	fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ",
238 	    "[-h <makefile-header-name>] [-m <makefile>]",
239 	    "[-p <obj-prefix>] [-c <c-file-name>] [-e <exec-file>] ",
240 	    "<conffile>");
241 	exit(1);
242 }
243 
244 
245 /*
246  * ========================================================================
247  * parse_conf_file subsystem
248  *
249  */
250 
251 /* helper routines for parse_conf_file */
252 
253 void parse_one_file(char *filename);
254 void parse_line(char *pline, int *fc, char **fv, int nf);
255 void add_srcdirs(int argc, char **argv);
256 void add_progs(int argc, char **argv);
257 void add_link(int argc, char **argv);
258 void add_libs(int argc, char **argv);
259 void add_libs_so(int argc, char **argv);
260 void add_buildopts(int argc, char **argv);
261 void add_special(int argc, char **argv);
262 
263 prog_t *find_prog(char *str);
264 void add_prog(char *progname);
265 
266 
267 void
268 parse_conf_file(void)
269 {
270 	if (!is_nonempty_file(infilename))
271 		errx(1, "fatal: input file \"%s\" not found", infilename);
272 
273 	parse_one_file(infilename);
274 	if (readcache && is_nonempty_file(cachename)) {
275 		reading_cache = 1;
276 		parse_one_file(cachename);
277 	}
278 }
279 
280 
281 void
282 parse_one_file(char *filename)
283 {
284 	char *fieldv[MAXFIELDS];
285 	int fieldc;
286 	void (*f)(int c, char **v);
287 	FILE *cf;
288 	char line[MAXLINELEN];
289 
290 	snprintf(line, sizeof(line), "reading %s", filename);
291 	status(line);
292 	strlcpy(curfilename, filename, sizeof(curfilename));
293 
294 	if ((cf = fopen(curfilename, "r")) == NULL) {
295 		warn("%s", curfilename);
296 		goterror = 1;
297 		return;
298 	}
299 
300 	linenum = 0;
301 	while (fgets(line, MAXLINELEN, cf) != NULL) {
302 		linenum++;
303 		parse_line(line, &fieldc, fieldv, MAXFIELDS);
304 
305 		if (fieldc < 1)
306 			continue;
307 
308 		if (!strcmp(fieldv[0], "srcdirs"))
309 			f = add_srcdirs;
310 		else if(!strcmp(fieldv[0], "progs"))
311 			f = add_progs;
312 		else if(!strcmp(fieldv[0], "ln"))
313 			f = add_link;
314 		else if(!strcmp(fieldv[0], "libs"))
315 			f = add_libs;
316 		else if(!strcmp(fieldv[0], "libs_so"))
317 			f = add_libs_so;
318 		else if(!strcmp(fieldv[0], "buildopts"))
319 			f = add_buildopts;
320 		else if(!strcmp(fieldv[0], "special"))
321 			f = add_special;
322 		else {
323 			warnx("%s:%d: skipping unknown command `%s'",
324 			    curfilename, linenum, fieldv[0]);
325 			goterror = 1;
326 			continue;
327 		}
328 
329 		if (fieldc < 2) {
330 			warnx("%s:%d: %s %s",
331 			    curfilename, linenum, fieldv[0],
332 			    "command needs at least 1 argument, skipping");
333 			goterror = 1;
334 			continue;
335 		}
336 
337 		f(fieldc, fieldv);
338 	}
339 
340 	if (ferror(cf)) {
341 		warn("%s", curfilename);
342 		goterror = 1;
343 	}
344 	fclose(cf);
345 }
346 
347 
348 void
349 parse_line(char *pline, int *fc, char **fv, int nf)
350 {
351 	char *p;
352 
353 	p = pline;
354 	*fc = 0;
355 
356 	while (1) {
357 		while (isspace((unsigned char)*p))
358 			p++;
359 
360 		if (*p == '\0' || *p == '#')
361 			break;
362 
363 		if (*fc < nf)
364 			fv[(*fc)++] = p;
365 
366 		while (*p && !isspace((unsigned char)*p) && *p != '#')
367 			p++;
368 
369 		if (*p == '\0' || *p == '#')
370 			break;
371 
372 		*p++ = '\0';
373 	}
374 
375 	if (*p)
376 		*p = '\0';		/* needed for '#' case */
377 }
378 
379 
380 void
381 add_srcdirs(int argc, char **argv)
382 {
383 	int i;
384 
385 	for (i = 1; i < argc; i++) {
386 		if (is_dir(argv[i]))
387 			add_string(&srcdirs, argv[i]);
388 		else {
389 			warnx("%s:%d: `%s' is not a directory, skipping it",
390 			    curfilename, linenum, argv[i]);
391 			goterror = 1;
392 		}
393 	}
394 }
395 
396 
397 void
398 add_progs(int argc, char **argv)
399 {
400 	int i;
401 
402 	for (i = 1; i < argc; i++)
403 		add_prog(argv[i]);
404 }
405 
406 
407 void
408 add_prog(char *progname)
409 {
410 	prog_t *p1, *p2;
411 
412 	/* add to end, but be smart about dups */
413 
414 	for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
415 		if (!strcmp(p2->name, progname))
416 			return;
417 
418 	p2 = malloc(sizeof(prog_t));
419 	if(p2) {
420 		memset(p2, 0, sizeof(prog_t));
421 		p2->name = strdup(progname);
422 	}
423 	if (!p2 || !p2->name)
424 		out_of_memory();
425 
426 	p2->next = NULL;
427 	if (p1 == NULL)
428 		progs = p2;
429 	else
430 		p1->next = p2;
431 
432 	p2->ident = NULL;
433 	p2->srcdir = NULL;
434 	p2->realsrcdir = NULL;
435 	p2->objdir = NULL;
436 	p2->links = NULL;
437 	p2->libs = NULL;
438 	p2->libs_so = NULL;
439 	p2->objs = NULL;
440 	p2->keeplist = NULL;
441 	p2->buildopts = NULL;
442 	p2->goterror = 0;
443 
444 	if (list_mode)
445 		printf("%s\n",progname);
446 }
447 
448 
449 void
450 add_link(int argc, char **argv)
451 {
452 	int i;
453 	prog_t *p = find_prog(argv[1]);
454 
455 	if (p == NULL) {
456 		warnx("%s:%d: no prog %s previously declared, skipping link",
457 		    curfilename, linenum, argv[1]);
458 		goterror = 1;
459 		return;
460 	}
461 
462 	for (i = 2; i < argc; i++) {
463 		if (list_mode)
464 			printf("%s\n",argv[i]);
465 
466 		add_string(&p->links, argv[i]);
467 	}
468 }
469 
470 
471 void
472 add_libs(int argc, char **argv)
473 {
474 	int i;
475 
476 	for(i = 1; i < argc; i++) {
477 		add_string(&libs, argv[i]);
478 		if ( in_list(&libs_so, argv[i]) )
479 			warnx("%s:%d: "
480 				"library `%s' specified as dynamic earlier",
481 				curfilename, linenum, argv[i]);
482 	}
483 }
484 
485 
486 void
487 add_libs_so(int argc, char **argv)
488 {
489 	int i;
490 
491 	for(i = 1; i < argc; i++) {
492 		add_string(&libs_so, argv[i]);
493 		if ( in_list(&libs, argv[i]) )
494 			warnx("%s:%d: "
495 				"library `%s' specified as static earlier",
496 				curfilename, linenum, argv[i]);
497 	}
498 }
499 
500 
501 void
502 add_buildopts(int argc, char **argv)
503 {
504 	int i;
505 
506 	for (i = 1; i < argc; i++)
507 		add_string(&buildopts, argv[i]);
508 }
509 
510 
511 void
512 add_special(int argc, char **argv)
513 {
514 	int i;
515 	prog_t *p = find_prog(argv[1]);
516 
517 	if (p == NULL) {
518 		if (reading_cache)
519 			return;
520 
521 		warnx("%s:%d: no prog %s previously declared, skipping special",
522 		    curfilename, linenum, argv[1]);
523 		goterror = 1;
524 		return;
525 	}
526 
527 	if (!strcmp(argv[2], "ident")) {
528 		if (argc != 4)
529 			goto argcount;
530 		if ((p->ident = strdup(argv[3])) == NULL)
531 			out_of_memory();
532 	} else if (!strcmp(argv[2], "srcdir")) {
533 		if (argc != 4)
534 			goto argcount;
535 		if ((p->srcdir = strdup(argv[3])) == NULL)
536 			out_of_memory();
537 	} else if (!strcmp(argv[2], "objdir")) {
538 		if(argc != 4)
539 			goto argcount;
540 		if((p->objdir = strdup(argv[3])) == NULL)
541 			out_of_memory();
542 	} else if (!strcmp(argv[2], "objs")) {
543 		p->objs = NULL;
544 		for (i = 3; i < argc; i++)
545 			add_string(&p->objs, argv[i]);
546 	} else if (!strcmp(argv[2], "objpaths")) {
547 		p->objpaths = NULL;
548 		for (i = 3; i < argc; i++)
549 			add_string(&p->objpaths, argv[i]);
550 	} else if (!strcmp(argv[2], "keep")) {
551 		p->keeplist = NULL;
552 		for(i = 3; i < argc; i++)
553 			add_string(&p->keeplist, argv[i]);
554 	} else if (!strcmp(argv[2], "objvar")) {
555 		if(argc != 4)
556 			goto argcount;
557 		if ((p->objvar = strdup(argv[3])) == NULL)
558 			out_of_memory();
559 	} else if (!strcmp(argv[2], "buildopts")) {
560 		p->buildopts = NULL;
561 		for (i = 3; i < argc; i++)
562 			add_string(&p->buildopts, argv[i]);
563 	} else if (!strcmp(argv[2], "lib")) {
564 		for (i = 3; i < argc; i++)
565 			add_string(&p->libs, argv[i]);
566 	} else {
567 		warnx("%s:%d: bad parameter name `%s', skipping line",
568 		    curfilename, linenum, argv[2]);
569 		goterror = 1;
570 	}
571 	return;
572 
573  argcount:
574 	warnx("%s:%d: too %s arguments, expected \"special %s %s <string>\"",
575 	    curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
576 	goterror = 1;
577 }
578 
579 
580 prog_t *find_prog(char *str)
581 {
582 	prog_t *p;
583 
584 	for (p = progs; p != NULL; p = p->next)
585 		if (!strcmp(p->name, str))
586 			return p;
587 
588 	return NULL;
589 }
590 
591 
592 /*
593  * ========================================================================
594  * gen_outputs subsystem
595  *
596  */
597 
598 /* helper subroutines */
599 
600 void remove_error_progs(void);
601 void fillin_program(prog_t *p);
602 void gen_specials_cache(void);
603 void gen_output_makefile(void);
604 void gen_output_cfile(void);
605 
606 void fillin_program_objs(prog_t *p, char *path);
607 void top_makefile_rules(FILE *outmk);
608 void prog_makefile_rules(FILE *outmk, prog_t *p);
609 void output_strlst(FILE *outf, strlst_t *lst);
610 char *genident(char *str);
611 char *dir_search(char *progname);
612 
613 
614 void
615 gen_outputs(void)
616 {
617 	prog_t *p;
618 
619 	for (p = progs; p != NULL; p = p->next)
620 		fillin_program(p);
621 
622 	remove_error_progs();
623 	gen_specials_cache();
624 	gen_output_cfile();
625 	gen_output_makefile();
626 	status("");
627 	fprintf(stderr,
628 	    "Run \"%s -f %s\" to build crunched binary.\n",
629 	    path_make, outmkname);
630 }
631 
632 /*
633  * run the makefile for the program to find which objects are necessary
634  */
635 void
636 fillin_program(prog_t *p)
637 {
638 	char path[MAXPATHLEN];
639 	char line[MAXLINELEN];
640 
641 	snprintf(line, MAXLINELEN, "filling in parms for %s", p->name);
642 	status(line);
643 
644 	if (!p->ident)
645 		p->ident = genident(p->name);
646 
647 	/* look for the source directory if one wasn't specified by a special */
648 	if (!p->srcdir) {
649 		p->srcdir = dir_search(p->name);
650 	}
651 
652 	/* Determine the actual srcdir (maybe symlinked). */
653 	if (p->srcdir) {
654 		p->realsrcdir = realpath(p->srcdir, NULL);
655 		if (p->realsrcdir == NULL)
656 			errx(1, "Can't resolve path: %s\n", p->srcdir);
657 	}
658 
659 	/* Unless the option to make object files was specified the
660 	* the objects will be built in the source directory unless
661 	* an object directory already exists.
662 	*/
663 	if (!makeobj && !p->objdir && p->srcdir) {
664 		char *auto_obj;
665 
666 		auto_obj = NULL;
667 		snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
668 		if (is_dir(line) ||
669 		    ((auto_obj = getenv("MK_AUTO_OBJ")) != NULL &&
670 		    strcmp(auto_obj, "yes") == 0)) {
671 			if ((p->objdir = strdup(line)) == NULL)
672 			out_of_memory();
673 		} else
674 			p->objdir = p->realsrcdir;
675 	}
676 
677 	/*
678 	* XXX look for a Makefile.{name} in local directory first.
679 	* This lets us override the original Makefile.
680 	*/
681 	snprintf(path, sizeof(path), "Makefile.%s", p->name);
682 	if (is_nonempty_file(path)) {
683 		snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
684 		status(line);
685 	} else
686 		if (p->srcdir)
687 			snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
688 	if (!p->objs && p->srcdir && is_nonempty_file(path))
689 		fillin_program_objs(p, path);
690 
691 	if (!p->srcdir && !p->objdir && verbose)
692 		warnx("%s: %s: %s",
693 		    "warning: could not find source directory",
694 		    infilename, p->name);
695 	if (!p->objs && verbose)
696 		warnx("%s: %s: warning: could not find any .o files",
697 		    infilename, p->name);
698 
699 	if ((!p->srcdir || !p->objdir) && !p->objs)
700 		p->goterror = 1;
701 }
702 
703 void
704 fillin_program_objs(prog_t *p, char *path)
705 {
706 	char *obj, *cp;
707 	int fd, rc;
708 	FILE *f;
709 	const char *objvar="OBJS";
710 	strlst_t *s;
711 	char line[MAXLINELEN];
712 
713 	/* discover the objs from the srcdir Makefile */
714 
715 	/*
716 	 * We reuse the same temporary file name for multiple objects. However,
717 	 * some libc implementations (such as glibc) return EINVAL if there
718 	 * are no XXXXX characters in the template. This happens after the
719 	 * first call to mkstemp since the argument is modified in-place.
720 	 * To avoid this error we use open() instead of mkstemp() after the
721 	 * call to mkstemp().
722 	 */
723 	if (tempfname_initialized) {
724 		if ((fd = open(tempfname, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) {
725 			err(EX_OSERR, "open(%s)", tempfname);
726 		}
727 	} else if ((fd = mkstemp(tempfname)) == -1) {
728 		err(EX_OSERR, "mkstemp(%s)", tempfname);
729 	}
730 	tempfname_initialized = true;
731 	if ((f = fdopen(fd, "w")) == NULL) {
732 		warn("fdopen(%s)", tempfname);
733 		goterror = 1;
734 		goto out;
735 	}
736 	if (p->objvar)
737 		objvar = p->objvar;
738 
739 	/*
740 	* XXX include outhdrname (e.g. to contain Make variables)
741 	*/
742 	if (outhdrname[0] != '\0')
743 		fprintf(f, ".include \"%s\"\n", outhdrname);
744 	fprintf(f, ".include \"%s\"\n", path);
745 	fprintf(f, ".POSIX:\n");
746 	if (buildopts) {
747 		fprintf(f, "BUILDOPTS+=");
748 		output_strlst(f, buildopts);
749 	}
750 	fprintf(f, ".if defined(PROG)\n");
751 	fprintf(f, "%s?=${PROG}.o\n", objvar);
752 	fprintf(f, ".endif\n");
753 	fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
754 
755 	fprintf(f, "crunchgen_objs:\n"
756 	    "\t@cd %s && %s -f %s $(BUILDOPTS) $(%s_OPTS)",
757 	    p->srcdir, path_make, tempfname, p->ident);
758 	for (s = p->buildopts; s != NULL; s = s->next)
759 		fprintf(f, " %s", s->str);
760 	fprintf(f, " loop\n");
761 
762 	fclose(f);
763 
764 	snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs",
765 	     p->srcdir, path_make, tempfname);
766 	if ((f = popen(line, "r")) == NULL) {
767 		warn("submake pipe");
768 		goterror = 1;
769 		goto out;
770 	}
771 
772 	while(fgets(line, MAXLINELEN, f)) {
773 		if (strncmp(line, "OBJS= ", 6)) {
774 			warnx("make error: %s", line);
775 			goterror = 1;
776 			goto out;
777 		}
778 
779 		cp = line + 6;
780 		while (isspace((unsigned char)*cp))
781 			cp++;
782 
783 		while(*cp) {
784 			obj = cp;
785 			while (*cp && !isspace((unsigned char)*cp))
786 				cp++;
787 			if (*cp)
788 				*cp++ = '\0';
789 			add_string(&p->objs, obj);
790 			while (isspace((unsigned char)*cp))
791 				cp++;
792 		}
793 	}
794 
795 	if ((rc=pclose(f)) != 0) {
796 		warnx("make error: make returned %d", rc);
797 		goterror = 1;
798 	}
799 out:
800 	unlink(tempfname);
801 }
802 
803 void
804 remove_error_progs(void)
805 {
806 	prog_t *p1, *p2;
807 
808 	p1 = NULL; p2 = progs;
809 	while (p2 != NULL) {
810 		if (!p2->goterror)
811 			p1 = p2, p2 = p2->next;
812 		else {
813 			/* delete it from linked list */
814 			warnx("%s: %s: ignoring program because of errors",
815 			    infilename, p2->name);
816 			if (p1)
817 				p1->next = p2->next;
818 			else
819 				progs = p2->next;
820 			p2 = p2->next;
821 		}
822 	}
823 }
824 
825 void
826 gen_specials_cache(void)
827 {
828 	FILE *cachef;
829 	prog_t *p;
830 	char line[MAXLINELEN];
831 
832 	snprintf(line, MAXLINELEN, "generating %s", cachename);
833 	status(line);
834 
835 	if ((cachef = fopen(cachename, "w")) == NULL) {
836 		warn("%s", cachename);
837 		goterror = 1;
838 		return;
839 	}
840 
841 	fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
842 	    " %s\n\n",
843 	    cachename, infilename, CRUNCH_VERSION);
844 
845 	for (p = progs; p != NULL; p = p->next) {
846 		fprintf(cachef, "\n");
847 		if (p->srcdir)
848 			fprintf(cachef, "special %s srcdir %s\n",
849 			    p->name, p->srcdir);
850 		if (p->objdir)
851 			fprintf(cachef, "special %s objdir %s\n",
852 			    p->name, p->objdir);
853 		if (p->objs) {
854 			fprintf(cachef, "special %s objs", p->name);
855 			output_strlst(cachef, p->objs);
856 		}
857 		if (p->objpaths) {
858 			fprintf(cachef, "special %s objpaths", p->name);
859 			output_strlst(cachef, p->objpaths);
860 		}
861 	}
862 	fclose(cachef);
863 }
864 
865 
866 void
867 gen_output_makefile(void)
868 {
869 	prog_t *p;
870 	FILE *outmk;
871 	char line[MAXLINELEN];
872 
873 	snprintf(line, MAXLINELEN, "generating %s", outmkname);
874 	status(line);
875 
876 	if ((outmk = fopen(outmkname, "w")) == NULL) {
877 		warn("%s", outmkname);
878 		goterror = 1;
879 		return;
880 	}
881 
882 	fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
883 	    outmkname, infilename, CRUNCH_VERSION);
884 
885 	if (outhdrname[0] != '\0')
886 		fprintf(outmk, ".include \"%s\"\n", outhdrname);
887 
888 	top_makefile_rules(outmk);
889 	for (p = progs; p != NULL; p = p->next)
890 		prog_makefile_rules(outmk, p);
891 
892 	fprintf(outmk, "\n# ========\n");
893 	fclose(outmk);
894 }
895 
896 
897 void
898 gen_output_cfile(void)
899 {
900 	const char **cp;
901 	FILE *outcf;
902 	prog_t *p;
903 	strlst_t *s;
904 	char line[MAXLINELEN];
905 
906 	snprintf(line, MAXLINELEN, "generating %s", outcfname);
907 	status(line);
908 
909 	if((outcf = fopen(outcfname, "w")) == NULL) {
910 		warn("%s", outcfname);
911 		goterror = 1;
912 		return;
913 	}
914 
915 	fprintf(outcf,
916 	    "/* %s - generated from %s by crunchgen %s */\n",
917 	    outcfname, infilename, CRUNCH_VERSION);
918 
919 	fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
920 	for (cp = crunched_skel; *cp != NULL; cp++)
921 		fprintf(outcf, "%s\n", *cp);
922 
923 	for (p = progs; p != NULL; p = p->next)
924 		fprintf(outcf,
925 		    "extern crunched_stub_t _crunched_%s_stub;\n",
926 		    p->ident);
927 
928 	fprintf(outcf, "\nstruct stub entry_points[] = {\n");
929 	for (p = progs; p != NULL; p = p->next) {
930 		fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
931 		    p->name, p->ident);
932 		for (s = p->links; s != NULL; s = s->next)
933 			fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
934 			    s->str, p->ident);
935 	}
936 
937 	fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
938 	fprintf(outcf, "\t{ NULL, NULL }\n};\n");
939 	fclose(outcf);
940 }
941 
942 
943 char *genident(char *str)
944 {
945 	char *n, *s, *d;
946 
947 	/*
948 	 * generates a Makefile/C identifier from a program name,
949 	 * mapping '-' to '_' and ignoring all other non-identifier
950 	 * characters.  This leads to programs named "foo.bar" and
951 	 * "foobar" to map to the same identifier.
952 	 */
953 
954 	if ((n = strdup(str)) == NULL)
955 		return NULL;
956 	for (d = s = n; *s != '\0'; s++) {
957 		if (*s == '-')
958 			*d++ = '_';
959 		else if (*s == '_' || isalnum((unsigned char)*s))
960 			*d++ = *s;
961 	}
962 	*d = '\0';
963 	return n;
964 }
965 
966 
967 char *dir_search(char *progname)
968 {
969 	char path[MAXPATHLEN];
970 	strlst_t *dir;
971 	char *srcdir;
972 
973 	for (dir = srcdirs; dir != NULL; dir = dir->next) {
974 		snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
975 		if (!is_dir(path))
976 			continue;
977 
978 		if ((srcdir = strdup(path)) == NULL)
979 			out_of_memory();
980 
981 		return srcdir;
982 	}
983 	return NULL;
984 }
985 
986 
987 void
988 top_makefile_rules(FILE *outmk)
989 {
990 	prog_t *p;
991 
992 	fprintf(outmk, "LD?= ld\n");
993 	if ( subtract_strlst(&libs, &libs_so) )
994 		fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n");
995 
996 	fprintf(outmk, "LIBS+=");
997 	output_strlst(outmk, libs);
998 
999 	fprintf(outmk, "LIBS_SO+=");
1000 	output_strlst(outmk, libs_so);
1001 
1002 	if (makeobj) {
1003 		fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
1004 		fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n");
1005 		fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n");
1006 	} else {
1007 		fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n");
1008 	}
1009 
1010 	if (buildopts) {
1011 		fprintf(outmk, "BUILDOPTS+=");
1012 		output_strlst(outmk, buildopts);
1013 	}
1014 
1015 	fprintf(outmk, "CRUNCHED_OBJS=");
1016 	for (p = progs; p != NULL; p = p->next)
1017 		fprintf(outmk, " %s.lo", p->name);
1018 	fprintf(outmk, "\n");
1019 
1020 	fprintf(outmk, "SUBMAKE_TARGETS=");
1021 	for (p = progs; p != NULL; p = p->next)
1022 		fprintf(outmk, " %s_make", p->ident);
1023 	fprintf(outmk, "\nSUBCLEAN_TARGETS=");
1024 	for (p = progs; p != NULL; p = p->next)
1025 		fprintf(outmk, " %s_clean", p->ident);
1026 	fprintf(outmk, "\n\n");
1027 
1028 	fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
1029 	fprintf(outmk, "exe: %s\n", execfname);
1030 	fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname);
1031 	fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n");
1032 	fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n",
1033 	    execfname, execfname);
1034 	fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n");
1035 	fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n");
1036 	fprintf(outmk, ".else\n");
1037 	fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
1038 	    execfname, execfname);
1039 	fprintf(outmk, ".endif\n");
1040 	fprintf(outmk, "realclean: clean subclean\n");
1041 	fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
1042 	fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
1043 }
1044 
1045 
1046 void
1047 prog_makefile_rules(FILE *outmk, prog_t *p)
1048 {
1049 	strlst_t *lst;
1050 
1051 	fprintf(outmk, "\n# -------- %s\n\n", p->name);
1052 
1053 	fprintf(outmk, "%s_OBJDIR=", p->ident);
1054 	if (p->objdir)
1055 		fprintf(outmk, "%s", p->objdir);
1056 	else
1057 		fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
1058 		    p->ident);
1059 	fprintf(outmk, "\n");
1060 
1061 	fprintf(outmk, "%s_OBJPATHS=", p->ident);
1062 	if (p->objpaths)
1063 		output_strlst(outmk, p->objpaths);
1064 	else {
1065 		for (lst = p->objs; lst != NULL; lst = lst->next) {
1066 			fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1067 		}
1068 		fprintf(outmk, "\n");
1069 	}
1070 	fprintf(outmk, "$(%s_OBJPATHS): .NOMETA\n", p->ident);
1071 
1072 	if (p->srcdir && p->objs) {
1073 		fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1074 		fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
1075 
1076 		fprintf(outmk, "%s_OBJS=", p->ident);
1077 		output_strlst(outmk, p->objs);
1078 		if (p->buildopts != NULL) {
1079 			fprintf(outmk, "%s_OPTS+=", p->ident);
1080 			output_strlst(outmk, p->buildopts);
1081 		}
1082 #if 0
1083 		fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident);
1084 #endif
1085 		fprintf(outmk, "%s_make:\n", p->ident);
1086 		fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
1087 		if (makeobj)
1088 			fprintf(outmk, "$(CRUNCHMAKE) obj && ");
1089 		fprintf(outmk, "\\\n");
1090 		fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
1091 		    p->ident);
1092 		fprintf(outmk, "\\\n");
1093 		fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) "
1094 		    "$(%s_OBJS))",
1095 		    p->ident, p->ident);
1096 		fprintf(outmk, "\n");
1097 		fprintf(outmk, "%s_clean:\n", p->ident);
1098 		fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1099 		    p->ident);
1100 	} else {
1101 		fprintf(outmk, "%s_make:\n", p->ident);
1102 		fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1103 		    p->name);
1104 	}
1105 
1106 	if (p->libs) {
1107 		fprintf(outmk, "%s_LIBS=", p->ident);
1108 		output_strlst(outmk, p->libs);
1109 	}
1110 
1111 	fprintf(outmk, "%s_stub.c:\n", p->name);
1112 	fprintf(outmk, "\techo \""
1113 	    "extern int main(int argc, char **argv, char **envp); "
1114 	    "int _crunched_%s_stub(int argc, char **argv, char **envp);"
1115 	    "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1116 	    "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1117 	    p->ident, p->ident, p->name);
1118 	fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1119 	    p->name, p->name, p->ident);
1120 	if (p->libs)
1121 		fprintf(outmk, " $(%s_LIBS)", p->ident);
1122 
1123 	fprintf(outmk, "\n");
1124 	fprintf(outmk, "\t$(CC) -nostdlib -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1125 	    p->name, p->name, p->ident);
1126 	if (p->libs)
1127 		fprintf(outmk, " $(%s_LIBS)", p->ident);
1128 	fprintf(outmk, "\n");
1129 	fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1130 	for (lst = p->keeplist; lst != NULL; lst = lst->next)
1131 		fprintf(outmk, "-k _%s ", lst->str);
1132 	fprintf(outmk, "%s.lo\n", p->name);
1133 }
1134 
1135 void
1136 output_strlst(FILE *outf, strlst_t *lst)
1137 {
1138 	for (; lst != NULL; lst = lst->next)
1139 		if ( strlen(lst->str) )
1140 			fprintf(outf, " %s", lst->str);
1141 	fprintf(outf, "\n");
1142 }
1143 
1144 
1145 /*
1146  * ========================================================================
1147  * general library routines
1148  *
1149  */
1150 
1151 void
1152 status(const char *str)
1153 {
1154 	static int lastlen = 0;
1155 	int len, spaces;
1156 
1157 	if (!verbose)
1158 		return;
1159 
1160 	len = strlen(str);
1161 	spaces = lastlen - len;
1162 	if (spaces < 1)
1163 		spaces = 1;
1164 
1165 	fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1166 	fflush(stderr);
1167 	lastlen = len;
1168 }
1169 
1170 
1171 void
1172 out_of_memory(void)
1173 {
1174 	err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1175 }
1176 
1177 
1178 void
1179 add_string(strlst_t **listp, char *str)
1180 {
1181 	strlst_t *p1, *p2;
1182 
1183 	/* add to end, but be smart about dups */
1184 
1185 	for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1186 		if (!strcmp(p2->str, str))
1187 			return;
1188 
1189 	p2 = malloc(sizeof(strlst_t));
1190 	if (p2) {
1191 		p2->next = NULL;
1192 		p2->str = strdup(str);
1193     	}
1194 	if (!p2 || !p2->str)
1195 		out_of_memory();
1196 
1197 	if (p1 == NULL)
1198 		*listp = p2;
1199 	else
1200 		p1->next = p2;
1201 }
1202 
1203 int
1204 subtract_strlst(strlst_t **lista, strlst_t **listb)
1205 {
1206 	int subtract_count = 0;
1207 	strlst_t *p1;
1208 	for (p1 = *listb; p1 != NULL; p1 = p1->next)
1209 		if ( in_list(lista, p1->str) ) {
1210 			warnx("Will compile library `%s' dynamically", p1->str);
1211 			strcat(p1->str, "");
1212 			subtract_count++;
1213 		}
1214 	return subtract_count;
1215 }
1216 
1217 int
1218 in_list(strlst_t **listp, char *str)
1219 {
1220 	strlst_t *p1;
1221 	for (p1 = *listp; p1 != NULL; p1 = p1->next)
1222 		if (!strcmp(p1->str, str))
1223 			return 1;
1224 	return 0;
1225 }
1226 
1227 int
1228 is_dir(const char *pathname)
1229 {
1230 	struct stat buf;
1231 
1232 	if (stat(pathname, &buf) == -1)
1233 		return 0;
1234 
1235 	return S_ISDIR(buf.st_mode);
1236 }
1237 
1238 int
1239 is_nonempty_file(const char *pathname)
1240 {
1241 	struct stat buf;
1242 
1243 	if (stat(pathname, &buf) == -1)
1244 		return 0;
1245 
1246 	return S_ISREG(buf.st_mode) && buf.st_size > 0;
1247 }
1248