xref: /freebsd/usr.sbin/crunch/crunchgen/crunchgen.c (revision bc5304a006238115291e7568583632889dffbab9)
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 	FILE *f;
643 
644 	snprintf(line, MAXLINELEN, "filling in parms for %s", p->name);
645 	status(line);
646 
647 	if (!p->ident)
648 		p->ident = genident(p->name);
649 
650 	/* look for the source directory if one wasn't specified by a special */
651 	if (!p->srcdir) {
652 		p->srcdir = dir_search(p->name);
653 	}
654 
655 	/* Determine the actual srcdir (maybe symlinked). */
656 	if (p->srcdir) {
657 		snprintf(line, MAXLINELEN, "cd %s && pwd -P", p->srcdir);
658 		f = popen(line,"r");
659 		if (!f)
660 			errx(1, "Can't execute: %s\n", line);
661 
662 		path[0] = '\0';
663 		fgets(path, sizeof path, f);
664 		if (pclose(f))
665 			errx(1, "Can't execute: %s\n", line);
666 
667 		if (!*path)
668 			errx(1, "Can't perform pwd on: %s\n", p->srcdir);
669 
670 		/* Chop off trailing newline. */
671 		path[strlen(path) - 1] = '\0';
672 		p->realsrcdir = strdup(path);
673 	}
674 
675 	/* Unless the option to make object files was specified the
676 	* the objects will be built in the source directory unless
677 	* an object directory already exists.
678 	*/
679 	if (!makeobj && !p->objdir && p->srcdir) {
680 		char *auto_obj;
681 
682 		auto_obj = NULL;
683 		snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
684 		if (is_dir(line) ||
685 		    ((auto_obj = getenv("MK_AUTO_OBJ")) != NULL &&
686 		    strcmp(auto_obj, "yes") == 0)) {
687 			if ((p->objdir = strdup(line)) == NULL)
688 			out_of_memory();
689 		} else
690 			p->objdir = p->realsrcdir;
691 	}
692 
693 	/*
694 	* XXX look for a Makefile.{name} in local directory first.
695 	* This lets us override the original Makefile.
696 	*/
697 	snprintf(path, sizeof(path), "Makefile.%s", p->name);
698 	if (is_nonempty_file(path)) {
699 		snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
700 		status(line);
701 	} else
702 		if (p->srcdir)
703 			snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
704 	if (!p->objs && p->srcdir && is_nonempty_file(path))
705 		fillin_program_objs(p, path);
706 
707 	if (!p->srcdir && !p->objdir && verbose)
708 		warnx("%s: %s: %s",
709 		    "warning: could not find source directory",
710 		    infilename, p->name);
711 	if (!p->objs && verbose)
712 		warnx("%s: %s: warning: could not find any .o files",
713 		    infilename, p->name);
714 
715 	if ((!p->srcdir || !p->objdir) && !p->objs)
716 		p->goterror = 1;
717 }
718 
719 void
720 fillin_program_objs(prog_t *p, char *path)
721 {
722 	char *obj, *cp;
723 	int fd, rc;
724 	FILE *f;
725 	const char *objvar="OBJS";
726 	strlst_t *s;
727 	char line[MAXLINELEN];
728 
729 	/* discover the objs from the srcdir Makefile */
730 
731 	/*
732 	 * We reuse the same temporary file name for multiple objects. However,
733 	 * some libc implementations (such as glibc) return EINVAL if there
734 	 * are no XXXXX characters in the template. This happens after the
735 	 * first call to mkstemp since the argument is modified in-place.
736 	 * To avoid this error we use open() instead of mkstemp() after the
737 	 * call to mkstemp().
738 	 */
739 	if (tempfname_initialized) {
740 		if ((fd = open(tempfname, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) {
741 			err(EX_OSERR, "open(%s)", tempfname);
742 		}
743 	} else if ((fd = mkstemp(tempfname)) == -1) {
744 		err(EX_OSERR, "mkstemp(%s)", tempfname);
745 	}
746 	tempfname_initialized = true;
747 	if ((f = fdopen(fd, "w")) == NULL) {
748 		warn("fdopen(%s)", tempfname);
749 		goterror = 1;
750 		goto out;
751 	}
752 	if (p->objvar)
753 		objvar = p->objvar;
754 
755 	/*
756 	* XXX include outhdrname (e.g. to contain Make variables)
757 	*/
758 	if (outhdrname[0] != '\0')
759 		fprintf(f, ".include \"%s\"\n", outhdrname);
760 	fprintf(f, ".include \"%s\"\n", path);
761 	fprintf(f, ".POSIX:\n");
762 	if (buildopts) {
763 		fprintf(f, "BUILDOPTS+=");
764 		output_strlst(f, buildopts);
765 	}
766 	fprintf(f, ".if defined(PROG)\n");
767 	fprintf(f, "%s?=${PROG}.o\n", objvar);
768 	fprintf(f, ".endif\n");
769 	fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
770 
771 	fprintf(f, "crunchgen_objs:\n"
772 	    "\t@cd %s && %s -f %s $(BUILDOPTS) $(%s_OPTS)",
773 	    p->srcdir, path_make, tempfname, p->ident);
774 	for (s = p->buildopts; s != NULL; s = s->next)
775 		fprintf(f, " %s", s->str);
776 	fprintf(f, " loop\n");
777 
778 	fclose(f);
779 
780 	snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs",
781 	     p->srcdir, path_make, tempfname);
782 	if ((f = popen(line, "r")) == NULL) {
783 		warn("submake pipe");
784 		goterror = 1;
785 		goto out;
786 	}
787 
788 	while(fgets(line, MAXLINELEN, f)) {
789 		if (strncmp(line, "OBJS= ", 6)) {
790 			warnx("make error: %s", line);
791 			goterror = 1;
792 			goto out;
793 		}
794 
795 		cp = line + 6;
796 		while (isspace((unsigned char)*cp))
797 			cp++;
798 
799 		while(*cp) {
800 			obj = cp;
801 			while (*cp && !isspace((unsigned char)*cp))
802 				cp++;
803 			if (*cp)
804 				*cp++ = '\0';
805 			add_string(&p->objs, obj);
806 			while (isspace((unsigned char)*cp))
807 				cp++;
808 		}
809 	}
810 
811 	if ((rc=pclose(f)) != 0) {
812 		warnx("make error: make returned %d", rc);
813 		goterror = 1;
814 	}
815 out:
816 	unlink(tempfname);
817 }
818 
819 void
820 remove_error_progs(void)
821 {
822 	prog_t *p1, *p2;
823 
824 	p1 = NULL; p2 = progs;
825 	while (p2 != NULL) {
826 		if (!p2->goterror)
827 			p1 = p2, p2 = p2->next;
828 		else {
829 			/* delete it from linked list */
830 			warnx("%s: %s: ignoring program because of errors",
831 			    infilename, p2->name);
832 			if (p1)
833 				p1->next = p2->next;
834 			else
835 				progs = p2->next;
836 			p2 = p2->next;
837 		}
838 	}
839 }
840 
841 void
842 gen_specials_cache(void)
843 {
844 	FILE *cachef;
845 	prog_t *p;
846 	char line[MAXLINELEN];
847 
848 	snprintf(line, MAXLINELEN, "generating %s", cachename);
849 	status(line);
850 
851 	if ((cachef = fopen(cachename, "w")) == NULL) {
852 		warn("%s", cachename);
853 		goterror = 1;
854 		return;
855 	}
856 
857 	fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
858 	    " %s\n\n",
859 	    cachename, infilename, CRUNCH_VERSION);
860 
861 	for (p = progs; p != NULL; p = p->next) {
862 		fprintf(cachef, "\n");
863 		if (p->srcdir)
864 			fprintf(cachef, "special %s srcdir %s\n",
865 			    p->name, p->srcdir);
866 		if (p->objdir)
867 			fprintf(cachef, "special %s objdir %s\n",
868 			    p->name, p->objdir);
869 		if (p->objs) {
870 			fprintf(cachef, "special %s objs", p->name);
871 			output_strlst(cachef, p->objs);
872 		}
873 		if (p->objpaths) {
874 			fprintf(cachef, "special %s objpaths", p->name);
875 			output_strlst(cachef, p->objpaths);
876 		}
877 	}
878 	fclose(cachef);
879 }
880 
881 
882 void
883 gen_output_makefile(void)
884 {
885 	prog_t *p;
886 	FILE *outmk;
887 	char line[MAXLINELEN];
888 
889 	snprintf(line, MAXLINELEN, "generating %s", outmkname);
890 	status(line);
891 
892 	if ((outmk = fopen(outmkname, "w")) == NULL) {
893 		warn("%s", outmkname);
894 		goterror = 1;
895 		return;
896 	}
897 
898 	fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
899 	    outmkname, infilename, CRUNCH_VERSION);
900 
901 	if (outhdrname[0] != '\0')
902 		fprintf(outmk, ".include \"%s\"\n", outhdrname);
903 
904 	top_makefile_rules(outmk);
905 	for (p = progs; p != NULL; p = p->next)
906 		prog_makefile_rules(outmk, p);
907 
908 	fprintf(outmk, "\n# ========\n");
909 	fclose(outmk);
910 }
911 
912 
913 void
914 gen_output_cfile(void)
915 {
916 	const char **cp;
917 	FILE *outcf;
918 	prog_t *p;
919 	strlst_t *s;
920 	char line[MAXLINELEN];
921 
922 	snprintf(line, MAXLINELEN, "generating %s", outcfname);
923 	status(line);
924 
925 	if((outcf = fopen(outcfname, "w")) == NULL) {
926 		warn("%s", outcfname);
927 		goterror = 1;
928 		return;
929 	}
930 
931 	fprintf(outcf,
932 	    "/* %s - generated from %s by crunchgen %s */\n",
933 	    outcfname, infilename, CRUNCH_VERSION);
934 
935 	fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
936 	for (cp = crunched_skel; *cp != NULL; cp++)
937 		fprintf(outcf, "%s\n", *cp);
938 
939 	for (p = progs; p != NULL; p = p->next)
940 		fprintf(outcf,
941 		    "extern crunched_stub_t _crunched_%s_stub;\n",
942 		    p->ident);
943 
944 	fprintf(outcf, "\nstruct stub entry_points[] = {\n");
945 	for (p = progs; p != NULL; p = p->next) {
946 		fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
947 		    p->name, p->ident);
948 		for (s = p->links; s != NULL; s = s->next)
949 			fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
950 			    s->str, p->ident);
951 	}
952 
953 	fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
954 	fprintf(outcf, "\t{ NULL, NULL }\n};\n");
955 	fclose(outcf);
956 }
957 
958 
959 char *genident(char *str)
960 {
961 	char *n, *s, *d;
962 
963 	/*
964 	 * generates a Makefile/C identifier from a program name,
965 	 * mapping '-' to '_' and ignoring all other non-identifier
966 	 * characters.  This leads to programs named "foo.bar" and
967 	 * "foobar" to map to the same identifier.
968 	 */
969 
970 	if ((n = strdup(str)) == NULL)
971 		return NULL;
972 	for (d = s = n; *s != '\0'; s++) {
973 		if (*s == '-')
974 			*d++ = '_';
975 		else if (*s == '_' || isalnum((unsigned char)*s))
976 			*d++ = *s;
977 	}
978 	*d = '\0';
979 	return n;
980 }
981 
982 
983 char *dir_search(char *progname)
984 {
985 	char path[MAXPATHLEN];
986 	strlst_t *dir;
987 	char *srcdir;
988 
989 	for (dir = srcdirs; dir != NULL; dir = dir->next) {
990 		snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
991 		if (!is_dir(path))
992 			continue;
993 
994 		if ((srcdir = strdup(path)) == NULL)
995 			out_of_memory();
996 
997 		return srcdir;
998 	}
999 	return NULL;
1000 }
1001 
1002 
1003 void
1004 top_makefile_rules(FILE *outmk)
1005 {
1006 	prog_t *p;
1007 
1008 	fprintf(outmk, "LD?= ld\n");
1009 	if ( subtract_strlst(&libs, &libs_so) )
1010 		fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n");
1011 
1012 	fprintf(outmk, "LIBS+=");
1013 	output_strlst(outmk, libs);
1014 
1015 	fprintf(outmk, "LIBS_SO+=");
1016 	output_strlst(outmk, libs_so);
1017 
1018 	if (makeobj) {
1019 		fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
1020 		fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n");
1021 		fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n");
1022 	} else {
1023 		fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n");
1024 	}
1025 
1026 	if (buildopts) {
1027 		fprintf(outmk, "BUILDOPTS+=");
1028 		output_strlst(outmk, buildopts);
1029 	}
1030 
1031 	fprintf(outmk, "CRUNCHED_OBJS=");
1032 	for (p = progs; p != NULL; p = p->next)
1033 		fprintf(outmk, " %s.lo", p->name);
1034 	fprintf(outmk, "\n");
1035 
1036 	fprintf(outmk, "SUBMAKE_TARGETS=");
1037 	for (p = progs; p != NULL; p = p->next)
1038 		fprintf(outmk, " %s_make", p->ident);
1039 	fprintf(outmk, "\nSUBCLEAN_TARGETS=");
1040 	for (p = progs; p != NULL; p = p->next)
1041 		fprintf(outmk, " %s_clean", p->ident);
1042 	fprintf(outmk, "\n\n");
1043 
1044 	fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
1045 	fprintf(outmk, "exe: %s\n", execfname);
1046 	fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname);
1047 	fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n");
1048 	fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n",
1049 	    execfname, execfname);
1050 	fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n");
1051 	fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n");
1052 	fprintf(outmk, ".else\n");
1053 	fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
1054 	    execfname, execfname);
1055 	fprintf(outmk, ".endif\n");
1056 	fprintf(outmk, "realclean: clean subclean\n");
1057 	fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
1058 	fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
1059 }
1060 
1061 
1062 void
1063 prog_makefile_rules(FILE *outmk, prog_t *p)
1064 {
1065 	strlst_t *lst;
1066 
1067 	fprintf(outmk, "\n# -------- %s\n\n", p->name);
1068 
1069 	fprintf(outmk, "%s_OBJDIR=", p->ident);
1070 	if (p->objdir)
1071 		fprintf(outmk, "%s", p->objdir);
1072 	else
1073 		fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
1074 		    p->ident);
1075 	fprintf(outmk, "\n");
1076 
1077 	fprintf(outmk, "%s_OBJPATHS=", p->ident);
1078 	if (p->objpaths)
1079 		output_strlst(outmk, p->objpaths);
1080 	else {
1081 		for (lst = p->objs; lst != NULL; lst = lst->next) {
1082 			fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1083 		}
1084 		fprintf(outmk, "\n");
1085 	}
1086 	fprintf(outmk, "$(%s_OBJPATHS): .NOMETA\n", p->ident);
1087 
1088 	if (p->srcdir && p->objs) {
1089 		fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1090 		fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
1091 
1092 		fprintf(outmk, "%s_OBJS=", p->ident);
1093 		output_strlst(outmk, p->objs);
1094 		if (p->buildopts != NULL) {
1095 			fprintf(outmk, "%s_OPTS+=", p->ident);
1096 			output_strlst(outmk, p->buildopts);
1097 		}
1098 #if 0
1099 		fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident);
1100 #endif
1101 		fprintf(outmk, "%s_make:\n", p->ident);
1102 		fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
1103 		if (makeobj)
1104 			fprintf(outmk, "$(CRUNCHMAKE) obj && ");
1105 		fprintf(outmk, "\\\n");
1106 		fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
1107 		    p->ident);
1108 		fprintf(outmk, "\\\n");
1109 		fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) "
1110 		    "$(%s_OBJS))",
1111 		    p->ident, p->ident);
1112 		fprintf(outmk, "\n");
1113 		fprintf(outmk, "%s_clean:\n", p->ident);
1114 		fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1115 		    p->ident);
1116 	} else {
1117 		fprintf(outmk, "%s_make:\n", p->ident);
1118 		fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1119 		    p->name);
1120 	}
1121 
1122 	if (p->libs) {
1123 		fprintf(outmk, "%s_LIBS=", p->ident);
1124 		output_strlst(outmk, p->libs);
1125 	}
1126 
1127 	fprintf(outmk, "%s_stub.c:\n", p->name);
1128 	fprintf(outmk, "\techo \""
1129 	    "extern int main(int argc, char **argv, char **envp); "
1130 	    "int _crunched_%s_stub(int argc, char **argv, char **envp);"
1131 	    "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1132 	    "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1133 	    p->ident, p->ident, p->name);
1134 	fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1135 	    p->name, p->name, p->ident);
1136 	if (p->libs)
1137 		fprintf(outmk, " $(%s_LIBS)", p->ident);
1138 
1139 	fprintf(outmk, "\n");
1140 	fprintf(outmk, "\t$(CC) -nostdlib -Wl,-dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1141 	    p->name, p->name, p->ident);
1142 	if (p->libs)
1143 		fprintf(outmk, " $(%s_LIBS)", p->ident);
1144 	fprintf(outmk, "\n");
1145 	fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1146 	for (lst = p->keeplist; lst != NULL; lst = lst->next)
1147 		fprintf(outmk, "-k _%s ", lst->str);
1148 	fprintf(outmk, "%s.lo\n", p->name);
1149 }
1150 
1151 void
1152 output_strlst(FILE *outf, strlst_t *lst)
1153 {
1154 	for (; lst != NULL; lst = lst->next)
1155 		if ( strlen(lst->str) )
1156 			fprintf(outf, " %s", lst->str);
1157 	fprintf(outf, "\n");
1158 }
1159 
1160 
1161 /*
1162  * ========================================================================
1163  * general library routines
1164  *
1165  */
1166 
1167 void
1168 status(const char *str)
1169 {
1170 	static int lastlen = 0;
1171 	int len, spaces;
1172 
1173 	if (!verbose)
1174 		return;
1175 
1176 	len = strlen(str);
1177 	spaces = lastlen - len;
1178 	if (spaces < 1)
1179 		spaces = 1;
1180 
1181 	fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1182 	fflush(stderr);
1183 	lastlen = len;
1184 }
1185 
1186 
1187 void
1188 out_of_memory(void)
1189 {
1190 	err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1191 }
1192 
1193 
1194 void
1195 add_string(strlst_t **listp, char *str)
1196 {
1197 	strlst_t *p1, *p2;
1198 
1199 	/* add to end, but be smart about dups */
1200 
1201 	for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1202 		if (!strcmp(p2->str, str))
1203 			return;
1204 
1205 	p2 = malloc(sizeof(strlst_t));
1206 	if (p2) {
1207 		p2->next = NULL;
1208 		p2->str = strdup(str);
1209     	}
1210 	if (!p2 || !p2->str)
1211 		out_of_memory();
1212 
1213 	if (p1 == NULL)
1214 		*listp = p2;
1215 	else
1216 		p1->next = p2;
1217 }
1218 
1219 int
1220 subtract_strlst(strlst_t **lista, strlst_t **listb)
1221 {
1222 	int subtract_count = 0;
1223 	strlst_t *p1;
1224 	for (p1 = *listb; p1 != NULL; p1 = p1->next)
1225 		if ( in_list(lista, p1->str) ) {
1226 			warnx("Will compile library `%s' dynamically", p1->str);
1227 			strcat(p1->str, "");
1228 			subtract_count++;
1229 		}
1230 	return subtract_count;
1231 }
1232 
1233 int
1234 in_list(strlst_t **listp, char *str)
1235 {
1236 	strlst_t *p1;
1237 	for (p1 = *listp; p1 != NULL; p1 = p1->next)
1238 		if (!strcmp(p1->str, str))
1239 			return 1;
1240 	return 0;
1241 }
1242 
1243 int
1244 is_dir(const char *pathname)
1245 {
1246 	struct stat buf;
1247 
1248 	if (stat(pathname, &buf) == -1)
1249 		return 0;
1250 
1251 	return S_ISDIR(buf.st_mode);
1252 }
1253 
1254 int
1255 is_nonempty_file(const char *pathname)
1256 {
1257 	struct stat buf;
1258 
1259 	if (stat(pathname, &buf) == -1)
1260 		return 0;
1261 
1262 	return S_ISREG(buf.st_mode) && buf.st_size > 0;
1263 }
1264