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