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