]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/crunch/crunchgen/crunchgen.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / crunch / crunchgen / crunchgen.c
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 <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
58 typedef 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
65 typedef 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
85 strlst_t *buildopts = NULL;
86 strlst_t *srcdirs   = NULL;
87 strlst_t *libs      = NULL;
88 strlst_t *libs_so   = NULL;
89 prog_t   *progs     = NULL;
90
91 char confname[MAXPATHLEN], infilename[MAXPATHLEN];
92 char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
93 char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
94 char outhdrname[MAXPATHLEN] ;   /* user-supplied header for *.mk */
95 char *objprefix;                /* where are the objects ? */
96 char *path_make;
97 int linenum = -1;
98 int goterror = 0;
99
100 int verbose, readcache;         /* options */
101 int reading_cache;
102 int makeobj = 0;                /* add 'make obj' rules to the makefile */
103
104 int list_mode;
105
106 /* general library routines */
107
108 void status(const char *str);
109 void out_of_memory(void);
110 void add_string(strlst_t **listp, char *str);
111 int is_dir(const char *pathname);
112 int is_nonempty_file(const char *pathname);
113 int subtract_strlst(strlst_t **lista, strlst_t **listb);
114 int in_list(strlst_t **listp, char *str);
115
116 /* helper routines for main() */
117
118 void usage(void);
119 void parse_conf_file(void);
120 void gen_outputs(void);
121
122 extern char *crunched_skel[];
123
124
125 int
126 main(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
230 void
231 usage(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
249 void parse_one_file(char *filename);
250 void parse_line(char *pline, int *fc, char **fv, int nf);
251 void add_srcdirs(int argc, char **argv);
252 void add_progs(int argc, char **argv);
253 void add_link(int argc, char **argv);
254 void add_libs(int argc, char **argv);
255 void add_libs_so(int argc, char **argv);
256 void add_buildopts(int argc, char **argv);
257 void add_special(int argc, char **argv);
258
259 prog_t *find_prog(char *str);
260 void add_prog(char *progname);
261
262
263 void
264 parse_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
277 void
278 parse_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
344 void
345 parse_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
376 void
377 add_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
393 void
394 add_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
403 void
404 add_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
445 void
446 add_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
467 void
468 add_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
482 void
483 add_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
497 void
498 add_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
507 void
508 add_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
576 prog_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
596 void remove_error_progs(void);
597 void fillin_program(prog_t *p);
598 void gen_specials_cache(void);
599 void gen_output_makefile(void);
600 void gen_output_cfile(void);
601
602 void fillin_program_objs(prog_t *p, char *path);
603 void top_makefile_rules(FILE *outmk);
604 void prog_makefile_rules(FILE *outmk, prog_t *p);
605 void output_strlst(FILE *outf, strlst_t *lst);
606 char *genident(char *str);
607 char *dir_search(char *progname);
608
609
610 void
611 gen_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  */
631 void
632 fillin_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                 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
674                 if (is_dir(line)) {
675                         if ((p->objdir = strdup(line)) == NULL)
676                         out_of_memory();
677                 } else
678                         p->objdir = p->realsrcdir;
679         }
680
681         /*
682         * XXX look for a Makefile.{name} in local directory first.
683         * This lets us override the original Makefile.
684         */
685         snprintf(path, sizeof(path), "Makefile.%s", p->name);
686         if (is_nonempty_file(path)) {
687                 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
688                 status(line);
689         } else
690                 if (p->srcdir)
691                         snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
692         if (!p->objs && p->srcdir && is_nonempty_file(path))
693                 fillin_program_objs(p, path);
694
695         if (!p->srcdir && !p->objdir && verbose)
696                 warnx("%s: %s: %s",
697                     "warning: could not find source directory",
698                     infilename, p->name);
699         if (!p->objs && verbose)
700                 warnx("%s: %s: warning: could not find any .o files",
701                     infilename, p->name);
702
703         if ((!p->srcdir || !p->objdir) && !p->objs)
704                 p->goterror = 1;
705 }
706
707 void
708 fillin_program_objs(prog_t *p, char *path)
709 {
710         char *obj, *cp;
711         int fd, rc;
712         FILE *f;
713         char *objvar="OBJS";
714         strlst_t *s;
715         char line[MAXLINELEN];
716
717         /* discover the objs from the srcdir Makefile */
718
719         if ((fd = mkstemp(tempfname)) == -1) {
720                 perror(tempfname);
721                 exit(1);
722         }
723         if ((f = fdopen(fd, "w")) == NULL) {
724                 warn("%s", tempfname);
725                 goterror = 1;
726                 return;
727         }
728         if (p->objvar)
729                 objvar = p->objvar;
730
731         /*
732         * XXX include outhdrname (e.g. to contain Make variables)
733         */
734         if (outhdrname[0] != '\0')
735                 fprintf(f, ".include \"%s\"\n", outhdrname);
736         fprintf(f, ".include \"%s\"\n", path);
737         fprintf(f, ".POSIX:\n");
738         if (buildopts) {
739                 fprintf(f, "BUILDOPTS+=");
740                 output_strlst(f, buildopts);
741         }
742         fprintf(f, ".if defined(PROG)\n");
743         fprintf(f, "%s?=${PROG}.o\n", objvar);
744         fprintf(f, ".endif\n");
745         fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
746
747         fprintf(f, "crunchgen_objs:\n"
748             "\t@cd %s && %s -f %s $(BUILDOPTS) $(%s_OPTS)",
749             p->srcdir, path_make, tempfname, p->ident);
750         for (s = p->buildopts; s != NULL; s = s->next)
751                 fprintf(f, " %s", s->str);
752         fprintf(f, " loop\n");
753
754         fclose(f);
755
756         snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs",
757              p->srcdir, path_make, tempfname);
758         if ((f = popen(line, "r")) == NULL) {
759                 warn("submake pipe");
760                 goterror = 1;
761                 return;
762         }
763
764         while(fgets(line, MAXLINELEN, f)) {
765                 if (strncmp(line, "OBJS= ", 6)) {
766                         warnx("make error: %s", line);
767                         goterror = 1;
768                         continue;
769                 }
770
771                 cp = line + 6;
772                 while (isspace((unsigned char)*cp))
773                         cp++;
774
775                 while(*cp) {
776                         obj = cp;
777                         while (*cp && !isspace((unsigned char)*cp))
778                                 cp++;
779                         if (*cp)
780                                 *cp++ = '\0';
781                         add_string(&p->objs, obj);
782                         while (isspace((unsigned char)*cp))
783                                 cp++;
784                 }
785         }
786
787         if ((rc=pclose(f)) != 0) {
788                 warnx("make error: make returned %d", rc);
789                 goterror = 1;
790         }
791
792         unlink(tempfname);
793 }
794
795 void
796 remove_error_progs(void)
797 {
798         prog_t *p1, *p2;
799
800         p1 = NULL; p2 = progs;
801         while (p2 != NULL) {
802                 if (!p2->goterror)
803                         p1 = p2, p2 = p2->next;
804                 else {
805                         /* delete it from linked list */
806                         warnx("%s: %s: ignoring program because of errors",
807                             infilename, p2->name);
808                         if (p1)
809                                 p1->next = p2->next;
810                         else
811                                 progs = p2->next;
812                         p2 = p2->next;
813                 }
814         }
815 }
816
817 void
818 gen_specials_cache(void)
819 {
820         FILE *cachef;
821         prog_t *p;
822         char line[MAXLINELEN];
823
824         snprintf(line, MAXLINELEN, "generating %s", cachename);
825         status(line);
826
827         if ((cachef = fopen(cachename, "w")) == NULL) {
828                 warn("%s", cachename);
829                 goterror = 1;
830                 return;
831         }
832
833         fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
834             " %s\n\n",
835             cachename, infilename, CRUNCH_VERSION);
836
837         for (p = progs; p != NULL; p = p->next) {
838                 fprintf(cachef, "\n");
839                 if (p->srcdir)
840                         fprintf(cachef, "special %s srcdir %s\n",
841                             p->name, p->srcdir);
842                 if (p->objdir)
843                         fprintf(cachef, "special %s objdir %s\n",
844                             p->name, p->objdir);
845                 if (p->objs) {
846                         fprintf(cachef, "special %s objs", p->name);
847                         output_strlst(cachef, p->objs);
848                 }
849                 if (p->objpaths) {
850                         fprintf(cachef, "special %s objpaths", p->name);
851                         output_strlst(cachef, p->objpaths);
852                 }
853         }
854         fclose(cachef);
855 }
856
857
858 void
859 gen_output_makefile(void)
860 {
861         prog_t *p;
862         FILE *outmk;
863         char line[MAXLINELEN];
864
865         snprintf(line, MAXLINELEN, "generating %s", outmkname);
866         status(line);
867
868         if ((outmk = fopen(outmkname, "w")) == NULL) {
869                 warn("%s", outmkname);
870                 goterror = 1;
871                 return;
872         }
873
874         fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
875             outmkname, infilename, CRUNCH_VERSION);
876
877         if (outhdrname[0] != '\0')
878                 fprintf(outmk, ".include \"%s\"\n", outhdrname);
879
880         top_makefile_rules(outmk);
881         for (p = progs; p != NULL; p = p->next)
882                 prog_makefile_rules(outmk, p);
883
884         fprintf(outmk, "\n# ========\n");
885         fclose(outmk);
886 }
887
888
889 void
890 gen_output_cfile(void)
891 {
892         char **cp;
893         FILE *outcf;
894         prog_t *p;
895         strlst_t *s;
896         char line[MAXLINELEN];
897
898         snprintf(line, MAXLINELEN, "generating %s", outcfname);
899         status(line);
900
901         if((outcf = fopen(outcfname, "w")) == NULL) {
902                 warn("%s", outcfname);
903                 goterror = 1;
904                 return;
905         }
906
907         fprintf(outcf,
908             "/* %s - generated from %s by crunchgen %s */\n",
909             outcfname, infilename, CRUNCH_VERSION);
910
911         fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
912         for (cp = crunched_skel; *cp != NULL; cp++)
913                 fprintf(outcf, "%s\n", *cp);
914
915         for (p = progs; p != NULL; p = p->next)
916                 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
917
918         fprintf(outcf, "\nstruct stub entry_points[] = {\n");
919         for (p = progs; p != NULL; p = p->next) {
920                 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
921                     p->name, p->ident);
922                 for (s = p->links; s != NULL; s = s->next)
923                         fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
924                             s->str, p->ident);
925         }
926
927         fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
928         fprintf(outcf, "\t{ NULL, NULL }\n};\n");
929         fclose(outcf);
930 }
931
932
933 char *genident(char *str)
934 {
935         char *n, *s, *d;
936
937         /*
938          * generates a Makefile/C identifier from a program name,
939          * mapping '-' to '_' and ignoring all other non-identifier
940          * characters.  This leads to programs named "foo.bar" and
941          * "foobar" to map to the same identifier.
942          */
943
944         if ((n = strdup(str)) == NULL)
945                 return NULL;
946         for (d = s = n; *s != '\0'; s++) {
947                 if (*s == '-')
948                         *d++ = '_';
949                 else if (*s == '_' || isalnum((unsigned char)*s))
950                         *d++ = *s;
951         }
952         *d = '\0';
953         return n;
954 }
955
956
957 char *dir_search(char *progname)
958 {
959         char path[MAXPATHLEN];
960         strlst_t *dir;
961         char *srcdir;
962
963         for (dir = srcdirs; dir != NULL; dir = dir->next) {
964                 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
965                 if (!is_dir(path))
966                         continue;
967
968                 if ((srcdir = strdup(path)) == NULL)
969                         out_of_memory();
970
971                 return srcdir;
972         }
973         return NULL;
974 }
975
976
977 void
978 top_makefile_rules(FILE *outmk)
979 {
980         prog_t *p;
981
982         fprintf(outmk, "LD?= ld\n");
983         if ( subtract_strlst(&libs, &libs_so) )
984                 fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n");
985
986         fprintf(outmk, "LIBS+=");
987         output_strlst(outmk, libs);
988
989         fprintf(outmk, "LIBS_SO+=");
990         output_strlst(outmk, libs_so);
991
992         if (makeobj) {
993                 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
994                 fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n");
995                 fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n");
996         } else {
997                 fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n");
998         }
999
1000         if (buildopts) {
1001                 fprintf(outmk, "BUILDOPTS+=");
1002                 output_strlst(outmk, buildopts);
1003         }
1004
1005         fprintf(outmk, "CRUNCHED_OBJS=");
1006         for (p = progs; p != NULL; p = p->next)
1007                 fprintf(outmk, " %s.lo", p->name);
1008         fprintf(outmk, "\n");
1009
1010         fprintf(outmk, "SUBMAKE_TARGETS=");
1011         for (p = progs; p != NULL; p = p->next)
1012                 fprintf(outmk, " %s_make", p->ident);
1013         fprintf(outmk, "\nSUBCLEAN_TARGETS=");
1014         for (p = progs; p != NULL; p = p->next)
1015                 fprintf(outmk, " %s_clean", p->ident);
1016         fprintf(outmk, "\n\n");
1017
1018         fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
1019         fprintf(outmk, "exe: %s\n", execfname);
1020         fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname);
1021         fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n");
1022         fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n",
1023             execfname, execfname);
1024         fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n");
1025         fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n");
1026         fprintf(outmk, ".else\n");
1027         fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
1028             execfname, execfname);
1029         fprintf(outmk, ".endif\n");
1030         fprintf(outmk, "\tstrip %s\n", execfname);
1031         fprintf(outmk, "realclean: clean subclean\n");
1032         fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
1033         fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
1034 }
1035
1036
1037 void
1038 prog_makefile_rules(FILE *outmk, prog_t *p)
1039 {
1040         strlst_t *lst;
1041
1042         fprintf(outmk, "\n# -------- %s\n\n", p->name);
1043
1044         fprintf(outmk, "%s_OBJDIR=", p->ident);
1045         if (p->objdir)
1046                 fprintf(outmk, "%s", p->objdir);
1047         else
1048                 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
1049                     p->ident);
1050         fprintf(outmk, "\n");
1051
1052         fprintf(outmk, "%s_OBJPATHS=", p->ident);
1053         if (p->objpaths)
1054                 output_strlst(outmk, p->objpaths);
1055         else {
1056                 for (lst = p->objs; lst != NULL; lst = lst->next) {
1057                         fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1058                 }
1059                 fprintf(outmk, "\n");
1060         }
1061
1062         if (p->srcdir && p->objs) {
1063                 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1064                 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
1065
1066                 fprintf(outmk, "%s_OBJS=", p->ident);
1067                 output_strlst(outmk, p->objs);
1068                 if (p->buildopts != NULL) {
1069                         fprintf(outmk, "%s_OPTS+=", p->ident);
1070                         output_strlst(outmk, p->buildopts);
1071                 }
1072 #if 0
1073                 fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident);
1074 #endif
1075                 fprintf(outmk, "%s_make:\n", p->ident);
1076                 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
1077                 if (makeobj)
1078                         fprintf(outmk, "$(CRUNCHMAKE) obj && ");
1079                 fprintf(outmk, "\\\n");
1080                 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
1081                     p->ident);
1082                 fprintf(outmk, "\\\n");
1083                 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) "
1084                     "$(%s_OBJS))",
1085                     p->ident, p->ident);
1086                 fprintf(outmk, "\n");
1087                 fprintf(outmk, "%s_clean:\n", p->ident);
1088                 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1089                     p->ident);
1090         } else {
1091                 fprintf(outmk, "%s_make:\n", p->ident);
1092                 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1093                     p->name);
1094         }
1095
1096         if (p->libs) {
1097                 fprintf(outmk, "%s_LIBS=", p->ident);
1098                 output_strlst(outmk, p->libs);
1099         }
1100
1101         fprintf(outmk, "%s_stub.c:\n", p->name);
1102         fprintf(outmk, "\techo \""
1103             "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1104             "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1105             p->ident, p->name);
1106         fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1107             p->name, p->name, p->ident);
1108         if (p->libs)
1109                 fprintf(outmk, " $(%s_LIBS)", p->ident);
1110
1111         fprintf(outmk, "\n");
1112         fprintf(outmk, "\t$(LD) -dc -r -o %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         fprintf(outmk, "\n");
1117         fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1118         for (lst = p->keeplist; lst != NULL; lst = lst->next)
1119                 fprintf(outmk, "-k _%s ", lst->str);
1120         fprintf(outmk, "%s.lo\n", p->name);
1121 }
1122
1123 void
1124 output_strlst(FILE *outf, strlst_t *lst)
1125 {
1126         for (; lst != NULL; lst = lst->next)
1127                 if ( strlen(lst->str) )
1128                         fprintf(outf, " %s", lst->str);
1129         fprintf(outf, "\n");
1130 }
1131
1132
1133 /*
1134  * ========================================================================
1135  * general library routines
1136  *
1137  */
1138
1139 void
1140 status(const char *str)
1141 {
1142         static int lastlen = 0;
1143         int len, spaces;
1144
1145         if (!verbose)
1146                 return;
1147
1148         len = strlen(str);
1149         spaces = lastlen - len;
1150         if (spaces < 1)
1151                 spaces = 1;
1152
1153         fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1154         fflush(stderr);
1155         lastlen = len;
1156 }
1157
1158
1159 void
1160 out_of_memory(void)
1161 {
1162         err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1163 }
1164
1165
1166 void
1167 add_string(strlst_t **listp, char *str)
1168 {
1169         strlst_t *p1, *p2;
1170
1171         /* add to end, but be smart about dups */
1172
1173         for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1174                 if (!strcmp(p2->str, str))
1175                         return;
1176
1177         p2 = malloc(sizeof(strlst_t));
1178         if (p2) {
1179                 p2->next = NULL;
1180                 p2->str = strdup(str);
1181         }
1182         if (!p2 || !p2->str)
1183                 out_of_memory();
1184
1185         if (p1 == NULL)
1186                 *listp = p2;
1187         else
1188                 p1->next = p2;
1189 }
1190
1191 int
1192 subtract_strlst(strlst_t **lista, strlst_t **listb)
1193 {
1194         int subtract_count = 0;
1195         strlst_t *p1;
1196         for (p1 = *listb; p1 != NULL; p1 = p1->next)
1197                 if ( in_list(lista, p1->str) ) {
1198                         warnx("Will compile library `%s' dynamically", p1->str);
1199                         strcat(p1->str, "");
1200                         subtract_count++;
1201                 }
1202         return subtract_count;
1203 }
1204
1205 int
1206 in_list(strlst_t **listp, char *str)
1207 {
1208         strlst_t *p1;
1209         for (p1 = *listp; p1 != NULL; p1 = p1->next)
1210                 if (!strcmp(p1->str, str))
1211                         return 1;
1212         return 0;
1213 }
1214
1215 int
1216 is_dir(const char *pathname)
1217 {
1218         struct stat buf;
1219
1220         if (stat(pathname, &buf) == -1)
1221                 return 0;
1222
1223         return S_ISDIR(buf.st_mode);
1224 }
1225
1226 int
1227 is_nonempty_file(const char *pathname)
1228 {
1229         struct stat buf;
1230
1231         if (stat(pathname, &buf) == -1)
1232                 return 0;
1233
1234         return S_ISREG(buf.st_mode) && buf.st_size > 0;
1235 }