Whitespace police:

if, for and while are not functions,
put controlled statements of if and while onto separate lines
put function names into column 1
This commit is contained in:
dsl 2004-09-25 19:21:07 +00:00
parent e039c124c4
commit 8e4e69acab
1 changed files with 251 additions and 176 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: crunchgen.c,v 1.59 2004/09/25 18:32:37 dsl Exp $ */ /* $NetBSD: crunchgen.c,v 1.60 2004/09/25 19:21:07 dsl Exp $ */
/* /*
* Copyright (c) 1994 University of Maryland * Copyright (c) 1994 University of Maryland
* All Rights Reserved. * All Rights Reserved.
@ -38,7 +38,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if !defined(lint) #if !defined(lint)
__RCSID("$NetBSD: crunchgen.c,v 1.59 2004/09/25 18:32:37 dsl Exp $"); __RCSID("$NetBSD: crunchgen.c,v 1.60 2004/09/25 19:21:07 dsl Exp $");
#endif #endif
#include <stdlib.h> #include <stdlib.h>
@ -122,7 +122,8 @@ void gen_outputs(void);
extern char *crunched_skel[]; extern char *crunched_skel[];
int main(int argc, char **argv) int
main(int argc, char **argv)
{ {
char *p; char *p;
int optc; int optc;
@ -148,9 +149,10 @@ int main(int argc, char **argv)
useobjs = 0; useobjs = 0;
*outmkname = *outcfname = *execfname = '\0'; *outmkname = *outcfname = *execfname = '\0';
if(argc > 0) pname = argv[0]; if (argc > 0)
pname = argv[0];
while((optc = getopt(argc, argv, "m:c:d:e:foqD:L:v:")) != -1) { while ((optc = getopt(argc, argv, "m:c:d:e:foqD:L:v:")) != -1) {
switch(optc) { switch(optc) {
case 'f': readcache = 0; break; case 'f': readcache = 0; break;
case 'q': verbose = 0; break; case 'q': verbose = 0; break;
@ -173,7 +175,8 @@ int main(int argc, char **argv)
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if(argc != 1) usage(); if (argc != 1)
usage();
/* /*
* generate filenames * generate filenames
@ -184,9 +187,12 @@ int main(int argc, char **argv)
/* confname = `basename infilename .conf` */ /* confname = `basename infilename .conf` */
if((p=strrchr(infilename, '/')) != NULL) strcpy(confname, p+1); if ((p = strrchr(infilename, '/')) != NULL)
else strcpy(confname, infilename); strcpy(confname, p + 1);
if((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) *p = '\0'; else
strcpy(confname, infilename);
if ((p = strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
*p = '\0';
if (!*outmkname) if (!*outmkname)
(void)snprintf(outmkname, sizeof(outmkname), "%s.mk", confname); (void)snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
@ -204,7 +210,8 @@ int main(int argc, char **argv)
} }
void usage(void) void
usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"%s [-foq] [-m makefile-name] [-c c-file-name] [-e exec-file-name]\n" "%s [-foq] [-m makefile-name] [-c c-file-name] [-e exec-file-name]\n"
@ -234,22 +241,24 @@ prog_t *find_prog(char *str);
void add_prog(char *progname); void add_prog(char *progname);
void parse_conf_file(void) void
parse_conf_file(void)
{ {
if(!is_nonempty_file(infilename)) { if (!is_nonempty_file(infilename)) {
fprintf(stderr, "%s: fatal: input file \"%s\" not found.\n", fprintf(stderr, "%s: fatal: input file \"%s\" not found.\n",
pname, infilename); pname, infilename);
exit(1); exit(1);
} }
parse_one_file(infilename); parse_one_file(infilename);
if(readcache && is_nonempty_file(cachename)) { if (readcache && is_nonempty_file(cachename)) {
reading_cache = 1; reading_cache = 1;
parse_one_file(cachename); parse_one_file(cachename);
} }
} }
void parse_one_file(char *filename) void
parse_one_file(char *filename)
{ {
char *fieldv[MAXFIELDS]; char *fieldv[MAXFIELDS];
int fieldc; int fieldc;
@ -260,29 +269,30 @@ void parse_one_file(char *filename)
status(line); status(line);
strcpy(curfilename, filename); strcpy(curfilename, filename);
if((cf = fopen(curfilename, "r")) == NULL) { if ((cf = fopen(curfilename, "r")) == NULL) {
perror(curfilename); perror(curfilename);
goterror = 1; goterror = 1;
return; return;
} }
linenum = 0; linenum = 0;
while(fgets(line, MAXLINELEN, cf) != NULL) { while (fgets(line, MAXLINELEN, cf) != NULL) {
linenum++; linenum++;
parse_line(line, &fieldc, fieldv, MAXFIELDS); parse_line(line, &fieldc, fieldv, MAXFIELDS);
if(fieldc < 1) continue; if (fieldc < 1)
if(!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs; continue;
else if(!strcmp(fieldv[0], "progs")) f = add_progs; if (!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs;
else if(!strcmp(fieldv[0], "ln")) f = add_link; else if (!strcmp(fieldv[0], "progs")) f = add_progs;
else if(!strcmp(fieldv[0], "libs")) f = add_libs; else if (!strcmp(fieldv[0], "ln")) f = add_link;
else if(!strcmp(fieldv[0], "special")) f = add_special; else if (!strcmp(fieldv[0], "libs")) f = add_libs;
else if (!strcmp(fieldv[0], "special")) f = add_special;
else { else {
fprintf(stderr, "%s:%d: skipping unknown command `%s'.\n", fprintf(stderr, "%s:%d: skipping unknown command `%s'.\n",
curfilename, linenum, fieldv[0]); curfilename, linenum, fieldv[0]);
goterror = 1; goterror = 1;
continue; continue;
} }
if(fieldc < 2) { if (fieldc < 2) {
fprintf(stderr, fprintf(stderr,
"%s:%d: %s command needs at least 1 argument, skipping.\n", "%s:%d: %s command needs at least 1 argument, skipping.\n",
curfilename, linenum, fieldv[0]); curfilename, linenum, fieldv[0]);
@ -292,7 +302,7 @@ void parse_one_file(char *filename)
f(fieldc, fieldv); f(fieldc, fieldv);
} }
if(ferror(cf)) { if (ferror(cf)) {
perror(curfilename); perror(curfilename);
goterror = 1; goterror = 1;
} }
@ -300,31 +310,39 @@ void parse_one_file(char *filename)
} }
void parse_line(char *line, int *fc, char **fv, int nf) void
parse_line(char *line, int *fc, char **fv, int nf)
{ {
char *p; char *p;
p = line; p = line;
*fc = 0; *fc = 0;
while(1) { for (;;) {
while(isspace(*p)) p++; while (isspace(*p))
if(*p == '\0' || *p == '#') break; p++;
if (*p == '\0' || *p == '#')
break;
if(*fc < nf) fv[(*fc)++] = p; if (*fc < nf)
while(*p && !isspace(*p) && *p != '#') p++; fv[(*fc)++] = p;
if(*p == '\0' || *p == '#') break; while (*p && !isspace(*p) && *p != '#')
p++;
if (*p == '\0' || *p == '#')
break;
*p++ = '\0'; *p++ = '\0';
} }
if(*p) *p = '\0'; /* needed for '#' case */ if (*p)
*p = '\0'; /* needed for '#' case */
} }
void add_srcdirs(int argc, char **argv) void
add_srcdirs(int argc, char **argv)
{ {
int i; int i;
char tmppath[MAXPATHLEN]; char tmppath[MAXPATHLEN];
for(i=1;i<argc;i++) { for (i = 1; i < argc; i++) {
if (argv[i][0] == '/') if (argv[i][0] == '/')
strcpy(tmppath, argv[i]); strcpy(tmppath, argv[i]);
else { else {
@ -335,7 +353,7 @@ void add_srcdirs(int argc, char **argv)
strcat(tmppath, "/"); strcat(tmppath, "/");
strcat(tmppath, argv[i]); strcat(tmppath, argv[i]);
} }
if(is_dir(tmppath)) if (is_dir(tmppath))
add_string(&srcdirs, tmppath); add_string(&srcdirs, tmppath);
else { else {
fprintf(stderr, "%s:%d: `%s' is not a directory, skipping it.\n", fprintf(stderr, "%s:%d: `%s' is not a directory, skipping it.\n",
@ -346,32 +364,38 @@ void add_srcdirs(int argc, char **argv)
} }
void add_progs(int argc, char **argv) void
add_progs(int argc, char **argv)
{ {
int i; int i;
for(i=1;i<argc;i++) for (i = 1; i < argc; i++)
add_prog(argv[i]); add_prog(argv[i]);
} }
void add_prog(char *progname) void
add_prog(char *progname)
{ {
prog_t *p1, *p2; prog_t *p1, *p2;
/* add to end, but be smart about dups */ /* add to end, but be smart about dups */
for(p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next) for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
if(!strcmp(p2->name, progname)) return; if (!strcmp(p2->name, progname))
return;
p2 = malloc(sizeof(prog_t)); p2 = malloc(sizeof(prog_t));
if(p2) p2->name = strdup(progname); if (p2)
if(!p2 || !p2->name) p2->name = strdup(progname);
if (!p2 || !p2->name)
out_of_memory(); out_of_memory();
p2->next = NULL; p2->next = NULL;
if(p1 == NULL) progs = p2; if (p1 == NULL)
else p1->next = p2; progs = p2;
else
p1->next = p2;
p2->ident = p2->srcdir = p2->objdir = NULL; p2->ident = p2->srcdir = p2->objdir = NULL;
p2->objs = p2->objpaths = p2->links = p2->keepsymbols = NULL; p2->objs = p2->objpaths = p2->links = p2->keepsymbols = NULL;
@ -379,39 +403,43 @@ void add_prog(char *progname)
} }
void add_link(int argc, char **argv) void
add_link(int argc, char **argv)
{ {
int i; int i;
prog_t *p = find_prog(argv[1]); prog_t *p = find_prog(argv[1]);
if(p == NULL) { if (p == NULL) {
fprintf(stderr, fprintf(stderr,
"%s:%d: no prog %s previously declared, skipping link.\n", "%s:%d: no prog %s previously declared, skipping link.\n",
curfilename, linenum, argv[1]); curfilename, linenum, argv[1]);
goterror = 1; goterror = 1;
return; return;
} }
for(i=2;i<argc;i++) for (i = 2; i < argc; i++)
add_string(&p->links, argv[i]); add_string(&p->links, argv[i]);
} }
void add_libs(int argc, char **argv) void
add_libs(int argc, char **argv)
{ {
int i; int i;
for(i=1;i<argc;i++) for (i = 1; i < argc; i++)
add_string(&libs, argv[i]); add_string(&libs, argv[i]);
} }
void add_special(int argc, char **argv) void
add_special(int argc, char **argv)
{ {
int i; int i;
prog_t *p = find_prog(argv[1]); prog_t *p = find_prog(argv[1]);
if(p == NULL) { if (p == NULL) {
if(reading_cache) return; if (reading_cache)
return;
fprintf(stderr, fprintf(stderr,
"%s:%d: no prog %s previously declared, skipping special.\n", "%s:%d: no prog %s previously declared, skipping special.\n",
curfilename, linenum, argv[1]); curfilename, linenum, argv[1]);
@ -419,15 +447,19 @@ void add_special(int argc, char **argv)
return; return;
} }
if(!strcmp(argv[2], "ident")) { if (!strcmp(argv[2], "ident")) {
if(argc != 4) goto argcount; if (argc != 4)
if((p->ident = strdup(argv[3])) == NULL) goto argcount;
if ((p->ident = strdup(argv[3])) == NULL)
out_of_memory(); out_of_memory();
return;
} }
else if(!strcmp(argv[2], "srcdir")) {
if(argc != 4) goto argcount; if (!strcmp(argv[2], "srcdir")) {
if (argc != 4)
goto argcount;
if (argv[3][0] == '/') { if (argv[3][0] == '/') {
if((p->srcdir = strdup(argv[3])) == NULL) if ((p->srcdir = strdup(argv[3])) == NULL)
out_of_memory(); out_of_memory();
} else { } else {
char tmppath[MAXPATHLEN]; char tmppath[MAXPATHLEN];
@ -437,37 +469,45 @@ void add_special(int argc, char **argv)
strcpy(tmppath, topdir); strcpy(tmppath, topdir);
strcat(tmppath, "/"); strcat(tmppath, "/");
strcat(tmppath, argv[3]); strcat(tmppath, argv[3]);
if((p->srcdir = strdup(tmppath)) == NULL) if ((p->srcdir = strdup(tmppath)) == NULL)
out_of_memory(); out_of_memory();
} }
return;
} }
else if(!strcmp(argv[2], "objdir")) {
if(argc != 4) goto argcount;
if((p->objdir = strdup(argv[3])) == NULL)
out_of_memory();
}
else if(!strcmp(argv[2], "objs")) {
p->objs = NULL;
for(i=3;i<argc;i++)
add_string(&p->objs, argv[i]);
}
else if(!strcmp(argv[2], "objpaths")) {
p->objpaths = NULL;
for(i=3;i<argc;i++)
add_string(&p->objpaths, argv[i]);
}
else if(!strcmp(argv[2], "keepsymbols")) {
p->keepsymbols = NULL;
for (i=3;i<argc;i++)
add_string(&p->keepsymbols, argv[i]);
}
else {
fprintf(stderr, "%s:%d: bad parameter name `%s', skipping line.\n",
curfilename, linenum, argv[2]);
goterror = 1;
}
return;
if (!strcmp(argv[2], "objdir")) {
if (argc != 4)
goto argcount;
if ((p->objdir = strdup(argv[3])) == NULL)
out_of_memory();
return;
}
if (!strcmp(argv[2], "objs")) {
p->objs = NULL;
for (i = 3; i < argc; i++)
add_string(&p->objs, argv[i]);
return;
}
if (!strcmp(argv[2], "objpaths")) {
p->objpaths = NULL;
for (i = 3; i < argc; i++)
add_string(&p->objpaths, argv[i]);
return;
}
if (!strcmp(argv[2], "keepsymbols")) {
p->keepsymbols = NULL;
for (i = 3; i < argc; i++)
add_string(&p->keepsymbols, argv[i]);
return;
}
fprintf(stderr, "%s:%d: bad parameter name `%s', skipping line.\n",
curfilename, linenum, argv[2]);
goterror = 1;
return;
argcount: argcount:
fprintf(stderr, fprintf(stderr,
@ -477,12 +517,14 @@ void add_special(int argc, char **argv)
} }
prog_t *find_prog(char *str) prog_t *
find_prog(char *str)
{ {
prog_t *p; prog_t *p;
for(p = progs; p != NULL; p = p->next) for (p = progs; p != NULL; p = p->next)
if(!strcmp(p->name, str)) return p; if (!strcmp(p->name, str))
return p;
return NULL; return NULL;
} }
@ -511,11 +553,12 @@ char *genident(char *str);
char *dir_search(char *progname); char *dir_search(char *progname);
void gen_outputs(void) void
gen_outputs(void)
{ {
prog_t *p; prog_t *p;
for(p = progs; p != NULL; p = p->next) for (p = progs; p != NULL; p = p->next)
fillin_program(p); fillin_program(p);
remove_error_progs(); remove_error_progs();
@ -529,7 +572,8 @@ void gen_outputs(void)
} }
void fillin_program(prog_t *p) void
fillin_program(prog_t *p)
{ {
char path[MAXPATHLEN]; char path[MAXPATHLEN];
char *srcparent; char *srcparent;
@ -538,15 +582,15 @@ void fillin_program(prog_t *p)
(void)snprintf(line, sizeof(line), "filling in parms for %s", p->name); (void)snprintf(line, sizeof(line), "filling in parms for %s", p->name);
status(line); status(line);
if(!p->ident) if (!p->ident)
p->ident = genident(p->name); p->ident = genident(p->name);
if(!p->srcdir) { if (!p->srcdir) {
srcparent = dir_search(p->name); srcparent = dir_search(p->name);
if(srcparent) { if (srcparent) {
(void)snprintf(path, sizeof(path), "%s/%s", srcparent, p->name); (void)snprintf(path, sizeof(path), "%s/%s", srcparent, p->name);
if(is_dir(path)) { if (is_dir(path)) {
if (path[0] == '/') { if (path[0] == '/') {
if((p->srcdir = strdup(path)) == NULL) if ((p->srcdir = strdup(path)) == NULL)
out_of_memory(); out_of_memory();
} else { } else {
char tmppath[MAXPATHLEN]; char tmppath[MAXPATHLEN];
@ -556,13 +600,14 @@ void fillin_program(prog_t *p)
strcpy(tmppath, topdir); strcpy(tmppath, topdir);
strcat(tmppath, "/"); strcat(tmppath, "/");
strcat(tmppath, path); strcat(tmppath, path);
if((p->srcdir = strdup(tmppath)) == NULL) if ((p->srcdir = strdup(tmppath)) == NULL)
out_of_memory(); out_of_memory();
} }
} }
} }
} }
if(!p->objdir && p->srcdir && useobjs) {
if (!p->objdir && p->srcdir && useobjs) {
if (makeobjdirprefix) { if (makeobjdirprefix) {
(void)snprintf(path, sizeof(path), "%s/%s", makeobjdirprefix, p->srcdir); (void)snprintf(path, sizeof(path), "%s/%s", makeobjdirprefix, p->srcdir);
if (is_dir(path)) if (is_dir(path))
@ -575,7 +620,7 @@ void fillin_program(prog_t *p)
} }
if (!p->objdir) { if (!p->objdir) {
(void)snprintf(path, sizeof(path), "%s/obj", p->srcdir); (void)snprintf(path, sizeof(path), "%s/obj", p->srcdir);
if(is_dir(path)) if (is_dir(path))
p->objdir = strdup(path); p->objdir = strdup(path);
} }
if (!p->objdir) { if (!p->objdir) {
@ -583,33 +628,33 @@ void fillin_program(prog_t *p)
} }
} }
if(p->srcdir) if (p->srcdir)
(void)snprintf(path, sizeof(path), "%s/Makefile", p->srcdir); (void)snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
if(!p->objs && p->srcdir && is_nonempty_file(path)) if (!p->objs && p->srcdir && is_nonempty_file(path))
fillin_program_objs(p, p->srcdir); fillin_program_objs(p, p->srcdir);
if(!p->objpaths && p->objs) { if (!p->objpaths && p->objs) {
if (p->objdir && useobjs) { if (p->objdir && useobjs) {
for(s = p->objs; s != NULL; s = s->next) { for (s = p->objs; s != NULL; s = s->next) {
(void)snprintf(line, sizeof(line), "%s/%s", p->objdir, s->str); (void)snprintf(line, sizeof(line), "%s/%s", p->objdir, s->str);
add_string(&p->objpaths, line); add_string(&p->objpaths, line);
} }
} else { } else {
for(s = p->objs; s != NULL; s = s->next) { for (s = p->objs; s != NULL; s = s->next) {
(void)snprintf(line, sizeof(line), "%s/%s", p->ident, s->str); (void)snprintf(line, sizeof(line), "%s/%s", p->ident, s->str);
add_string(&p->objpaths, line); add_string(&p->objpaths, line);
} }
} }
} }
if(!p->srcdir && verbose) if (!p->srcdir && verbose)
fprintf(stderr, "%s: %s: warning: could not find source directory.\n", fprintf(stderr, "%s: %s: warning: could not find source directory.\n",
infilename, p->name); infilename, p->name);
if(!p->objs && verbose) if (!p->objs && verbose)
fprintf(stderr, "%s: %s: warning: could not find any .o files.\n", fprintf(stderr, "%s: %s: warning: could not find any .o files.\n",
infilename, p->name); infilename, p->name);
if(!p->objpaths) { if (!p->objpaths) {
fprintf(stderr, fprintf(stderr,
"%s: %s: error: no objpaths specified or calculated.\n", "%s: %s: error: no objpaths specified or calculated.\n",
infilename, p->name); infilename, p->name);
@ -617,7 +662,8 @@ void fillin_program(prog_t *p)
} }
} }
void fillin_program_objs(prog_t *p, char *dirpath) void
fillin_program_objs(prog_t *p, char *dirpath)
{ {
char *obj, *cp; char *obj, *cp;
int rc; int rc;
@ -628,12 +674,12 @@ void fillin_program_objs(prog_t *p, char *dirpath)
/* discover the objs from the srcdir Makefile */ /* discover the objs from the srcdir Makefile */
(void)snprintf(tempfname, sizeof(tempfname), "/tmp/%sXXXXXX", confname); (void)snprintf(tempfname, sizeof(tempfname), "/tmp/%sXXXXXX", confname);
if((fd = mkstemp(tempfname)) < 0) { if ((fd = mkstemp(tempfname)) < 0) {
perror(tempfname); perror(tempfname);
exit(1); exit(1);
} }
if((f = fdopen(fd, "w")) == NULL) { if ((f = fdopen(fd, "w")) == NULL) {
perror(tempfname); perror(tempfname);
goterror = 1; goterror = 1;
return; return;
@ -649,15 +695,15 @@ void fillin_program_objs(prog_t *p, char *dirpath)
(void)snprintf(line, sizeof(line), (void)snprintf(line, sizeof(line),
"cd %s && %s -B -f %s %s CRUNCHEDPROG=1 crunchgen_objs 2>&1", dirpath, "cd %s && %s -B -f %s %s CRUNCHEDPROG=1 crunchgen_objs 2>&1", dirpath,
makebin, tempfname, makeflags); makebin, tempfname, makeflags);
if((f = popen(line, "r")) == NULL) { if ((f = popen(line, "r")) == NULL) {
perror("submake pipe"); perror("submake pipe");
goterror = 1; goterror = 1;
unlink(tempfname); unlink(tempfname);
return; return;
} }
while(fgets(line, MAXLINELEN, f)) { while (fgets(line, MAXLINELEN, f)) {
if(strncmp(line, "OBJS= ", 6)) { if (strncmp(line, "OBJS= ", 6)) {
if (strcmp(line, if (strcmp(line,
"sh: warning: running as root with dot in PATH\n") == 0) "sh: warning: running as root with dot in PATH\n") == 0)
continue; continue;
@ -666,42 +712,50 @@ void fillin_program_objs(prog_t *p, char *dirpath)
continue; continue;
} }
cp = line + 6; cp = line + 6;
while(isspace(*cp)) cp++; while (isspace(*cp))
while(*cp) { cp++;
while (*cp) {
obj = cp; obj = cp;
while(*cp && !isspace(*cp)) cp++; while (*cp && !isspace(*cp))
if(*cp) *cp++ = '\0'; cp++;
if (*cp)
*cp++ = '\0';
add_string(&p->objs, obj); add_string(&p->objs, obj);
while(isspace(*cp)) cp++; while (isspace(*cp))
cp++;
} }
} }
if((rc=pclose(f)) != 0) { if ((rc=pclose(f)) != 0) {
fprintf(stderr, "make error: make returned %d\n", rc); fprintf(stderr, "make error: make returned %d\n", rc);
goterror = 1; goterror = 1;
} }
unlink(tempfname); unlink(tempfname);
} }
void remove_error_progs(void) void
remove_error_progs(void)
{ {
prog_t *p1, *p2; prog_t *p1, *p2;
p1 = NULL; p2 = progs; p1 = NULL; p2 = progs;
while(p2 != NULL) { while (p2 != NULL) {
if(!p2->goterror) if (!p2->goterror)
p1 = p2, p2 = p2->next; p1 = p2, p2 = p2->next;
else { else {
/* delete it from linked list */ /* delete it from linked list */
fprintf(stderr, "%s: %s: ignoring program because of errors.\n", fprintf(stderr, "%s: %s: ignoring program because of errors.\n",
infilename, p2->name); infilename, p2->name);
if(p1) p1->next = p2->next; if (p1)
else progs = p2->next; p1->next = p2->next;
else
progs = p2->next;
p2 = p2->next; p2 = p2->next;
} }
} }
} }
void gen_specials_cache(void) void
gen_specials_cache(void)
{ {
FILE *cachef; FILE *cachef;
prog_t *p; prog_t *p;
@ -709,7 +763,7 @@ void gen_specials_cache(void)
(void)snprintf(line, sizeof(line), "generating %s", cachename); (void)snprintf(line, sizeof(line), "generating %s", cachename);
status(line); status(line);
if((cachef = fopen(cachename, "w")) == NULL) { if ((cachef = fopen(cachename, "w")) == NULL) {
perror(cachename); perror(cachename);
goterror = 1; goterror = 1;
return; return;
@ -718,13 +772,13 @@ void gen_specials_cache(void)
fprintf(cachef, "# %s - parm cache generated from %s by crunchgen %s\n\n", fprintf(cachef, "# %s - parm cache generated from %s by crunchgen %s\n\n",
cachename, infilename, CRUNCH_VERSION); cachename, infilename, CRUNCH_VERSION);
for(p = progs; p != NULL; p = p->next) { for (p = progs; p != NULL; p = p->next) {
fprintf(cachef, "\n"); fprintf(cachef, "\n");
if(p->srcdir) if (p->srcdir)
fprintf(cachef, "special %s srcdir %s\n", p->name, p->srcdir); fprintf(cachef, "special %s srcdir %s\n", p->name, p->srcdir);
if(p->objdir && useobjs) if (p->objdir && useobjs)
fprintf(cachef, "special %s objdir %s\n", p->name, p->objdir); fprintf(cachef, "special %s objdir %s\n", p->name, p->objdir);
if(p->objs) { if (p->objs) {
fprintf(cachef, "special %s objs", p->name); fprintf(cachef, "special %s objs", p->name);
output_strlst(cachef, p->objs); output_strlst(cachef, p->objs);
} }
@ -735,7 +789,8 @@ void gen_specials_cache(void)
} }
void gen_output_makefile(void) void
gen_output_makefile(void)
{ {
prog_t *p; prog_t *p;
FILE *outmk; FILE *outmk;
@ -743,7 +798,7 @@ void gen_output_makefile(void)
(void)snprintf(line, sizeof(line), "generating %s", outmkname); (void)snprintf(line, sizeof(line), "generating %s", outmkname);
status(line); status(line);
if((outmk = fopen(outmkname, "w")) == NULL) { if ((outmk = fopen(outmkname, "w")) == NULL) {
perror(outmkname); perror(outmkname);
goterror = 1; goterror = 1;
return; return;
@ -754,7 +809,7 @@ void gen_output_makefile(void)
top_makefile_rules(outmk); top_makefile_rules(outmk);
for(p = progs; p != NULL; p = p->next) for (p = progs; p != NULL; p = p->next)
prog_makefile_rules(outmk, p); prog_makefile_rules(outmk, p);
fprintf(outmk, "\n.include <bsd.prog.mk>\n"); fprintf(outmk, "\n.include <bsd.prog.mk>\n");
@ -766,7 +821,8 @@ void gen_output_makefile(void)
} }
void gen_output_cfile(void) void
gen_output_cfile(void)
{ {
char **cp; char **cp;
FILE *outcf; FILE *outcf;
@ -776,7 +832,7 @@ void gen_output_cfile(void)
(void)snprintf(line, sizeof(line), "generating %s", outcfname); (void)snprintf(line, sizeof(line), "generating %s", outcfname);
status(line); status(line);
if((outcf = fopen(outcfname, "w")) == NULL) { if ((outcf = fopen(outcfname, "w")) == NULL) {
perror(outcfname); perror(outcfname);
goterror = 1; goterror = 1;
return; return;
@ -787,17 +843,17 @@ void gen_output_cfile(void)
outcfname, infilename, CRUNCH_VERSION); outcfname, infilename, CRUNCH_VERSION);
fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname); fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
for(cp = crunched_skel; *cp != NULL; cp++) for (cp = crunched_skel; *cp != NULL; cp++)
fprintf(outcf, "%s\n", *cp); fprintf(outcf, "%s\n", *cp);
for(p = progs; p != NULL; p = p->next) for (p = progs; p != NULL; p = p->next)
fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident); fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
fprintf(outcf, "\nstruct stub entry_points[] = {\n"); fprintf(outcf, "\nstruct stub entry_points[] = {\n");
for(p = progs; p != NULL; p = p->next) { for (p = progs; p != NULL; p = p->next) {
fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
p->name, p->ident); p->name, p->ident);
for(s = p->links; s != NULL; s = s->next) for (s = p->links; s != NULL; s = s->next)
fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
s->str, p->ident); s->str, p->ident);
} }
@ -808,7 +864,8 @@ void gen_output_cfile(void)
} }
char *genident(char *str) char *
genident(char *str)
{ {
char *n,*s,*d; char *n,*s,*d;
@ -818,31 +875,37 @@ char *genident(char *str)
* programs named "foo.bar" and "foobar" to map to the same identifier. * programs named "foo.bar" and "foobar" to map to the same identifier.
*/ */
if((n = strdup(str)) == NULL) if ((n = strdup(str)) == NULL)
return NULL; return NULL;
for(d = s = n; *s != '\0'; s++) { for (d = s = n; *s != '\0'; s++) {
if(*s == '-') *d++ = '_'; if (*s == '-')
else if(*s == '_' || isalnum(*s)) *d++ = *s; *d++ = '_';
else
if (*s == '_' || isalnum(*s))
*d++ = *s;
} }
*d = '\0'; *d = '\0';
return n; return n;
} }
char *dir_search(char *progname) char *
dir_search(char *progname)
{ {
char path[MAXPATHLEN]; char path[MAXPATHLEN];
strlst_t *dir; strlst_t *dir;
for(dir=srcdirs; dir != NULL; dir=dir->next) { for (dir=srcdirs; dir != NULL; dir=dir->next) {
(void)snprintf(path, sizeof(path), "%s/%s", dir->str, progname); (void)snprintf(path, sizeof(path), "%s/%s", dir->str, progname);
if(is_dir(path)) return dir->str; if (is_dir(path))
return dir->str;
} }
return NULL; return NULL;
} }
void top_makefile_rules(FILE *outmk) void
top_makefile_rules(FILE *outmk)
{ {
prog_t *p; prog_t *p;
@ -858,19 +921,19 @@ void top_makefile_rules(FILE *outmk)
#endif #endif
fprintf(outmk, "CRUNCHED_OBJS="); fprintf(outmk, "CRUNCHED_OBJS=");
for(p = progs; p != NULL; p = p->next) for (p = progs; p != NULL; p = p->next)
fprintf(outmk, " %s.cro", p->name); fprintf(outmk, " %s.cro", p->name);
fprintf(outmk, "\n"); fprintf(outmk, "\n");
fprintf(outmk, "DPADD+= ${CRUNCHED_OBJS}\n"); fprintf(outmk, "DPADD+= ${CRUNCHED_OBJS}\n");
fprintf(outmk, "LDADD+= ${CRUNCHED_OBJS} "); fprintf(outmk, "LDADD+= ${CRUNCHED_OBJS} ");
output_strlst(outmk, libs); output_strlst(outmk, libs);
fprintf(outmk, "CRUNCHEDOBJSDIRS="); fprintf(outmk, "CRUNCHEDOBJSDIRS=");
for(p = progs; p != NULL; p = p->next) for (p = progs; p != NULL; p = p->next)
fprintf(outmk, " %s", p->ident); fprintf(outmk, " %s", p->ident);
fprintf(outmk, "\n\n"); fprintf(outmk, "\n\n");
fprintf(outmk, "SUBMAKE_TARGETS="); fprintf(outmk, "SUBMAKE_TARGETS=");
for(p = progs; p != NULL; p = p->next) for (p = progs; p != NULL; p = p->next)
fprintf(outmk, " %s_make", p->ident); fprintf(outmk, " %s_make", p->ident);
fprintf(outmk, "\n\n"); fprintf(outmk, "\n\n");
@ -892,22 +955,24 @@ void top_makefile_rules(FILE *outmk)
execfname); execfname);
} }
void bottom_makefile_rules(FILE *outmk) void
bottom_makefile_rules(FILE *outmk)
{ {
fprintf(outmk, "LDSTATIC=-static\n"); fprintf(outmk, "LDSTATIC=-static\n");
} }
void prog_makefile_rules(FILE *outmk, prog_t *p) void
prog_makefile_rules(FILE *outmk, prog_t *p)
{ {
strlst_t *lst; strlst_t *lst;
fprintf(outmk, "\n# -------- %s\n\n", p->name); fprintf(outmk, "\n# -------- %s\n\n", p->name);
fprintf(outmk, "%s_OBJPATHS=", p->ident); fprintf(outmk, "%s_OBJPATHS=", p->ident);
output_strlst(outmk, p->objpaths); output_strlst(outmk, p->objpaths);
if(p->srcdir && p->objs && !useobjs) { if (p->srcdir && p->objs && !useobjs) {
fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir); fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
fprintf(outmk, "%s_OBJS=", p->ident); fprintf(outmk, "%s_OBJS=", p->ident);
output_strlst(outmk, p->objs); output_strlst(outmk, p->objs);
@ -917,17 +982,15 @@ void prog_makefile_rules(FILE *outmk, prog_t *p)
fprintf(outmk, "\tprintf '.PATH: ${%s_SRCDIR}\\n" fprintf(outmk, "\tprintf '.PATH: ${%s_SRCDIR}\\n"
".CURDIR:= ${%s_SRCDIR}\\n" ".CURDIR:= ${%s_SRCDIR}\\n"
".include \"$${.CURDIR}/Makefile\"\\n", p->ident, p->ident); ".include \"$${.CURDIR}/Makefile\"\\n", p->ident, p->ident);
for(lst = vars; lst != NULL; lst = lst->next) for (lst = vars; lst != NULL; lst = lst->next)
fprintf(outmk, "%s\\n", lst->str); fprintf(outmk, "%s\\n", lst->str);
fprintf(outmk, "'\\\n"); fprintf(outmk, "'\\\n");
fprintf(outmk, "\t| ${MAKE} -f- CRUNCHEDPROG=1 DBG=\"${DBG}\" depend ${%s_OBJS}\n\n", fprintf(outmk, "\t| ${MAKE} -f- CRUNCHEDPROG=1 DBG=\"${DBG}\" depend ${%s_OBJS}\n\n",
p->ident); p->ident);
} } else
else
fprintf(outmk, "%s_make:\n\t@echo \"** Using existing objs for %s\"\n\n", fprintf(outmk, "%s_make:\n\t@echo \"** Using existing objs for %s\"\n\n",
p->ident, p->name); p->ident, p->name);
fprintf(outmk, "%s_stub.c:\n", p->name); fprintf(outmk, "%s_stub.c:\n", p->name);
fprintf(outmk, "\techo \"" fprintf(outmk, "\techo \""
"int _crunched_%s_stub(int argc, char **argv, char **envp)" "int _crunched_%s_stub(int argc, char **argv, char **envp)"
@ -959,9 +1022,10 @@ void prog_makefile_rules(FILE *outmk, prog_t *p)
fprintf(outmk, "%s.cro\n", p->name); fprintf(outmk, "%s.cro\n", p->name);
} }
void output_strlst(FILE *outf, strlst_t *lst) void
output_strlst(FILE *outf, strlst_t *lst)
{ {
for(; lst != NULL; lst = lst->next) for (; lst != NULL; lst = lst->next)
fprintf(outf, " %s", lst->str); fprintf(outf, " %s", lst->str);
fprintf(outf, "\n"); fprintf(outf, "\n");
} }
@ -973,16 +1037,19 @@ void output_strlst(FILE *outf, strlst_t *lst)
* *
*/ */
void status(char *str) void
status(char *str)
{ {
static int lastlen = 0; static int lastlen = 0;
int len, spaces; int len, spaces;
if(!verbose) return; if (!verbose)
return;
len = strlen(str); len = strlen(str);
spaces = lastlen - len; spaces = lastlen - len;
if(spaces < 1) spaces = 1; if (spaces < 1)
spaces = 1;
fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " "); fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
fflush(stderr); fflush(stderr);
@ -990,47 +1057,55 @@ void status(char *str)
} }
void out_of_memory(void) void
out_of_memory(void)
{ {
fprintf(stderr, "%s: %d: out of memory, stopping.\n", infilename, linenum); fprintf(stderr, "%s: %d: out of memory, stopping.\n", infilename, linenum);
exit(1); exit(1);
} }
void add_string(strlst_t **listp, char *str) void
add_string(strlst_t **listp, char *str)
{ {
strlst_t *p1, *p2; strlst_t *p1, *p2;
/* add to end, but be smart about dups */ /* add to end, but be smart about dups */
for(p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next) for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
if(!strcmp(p2->str, str)) return; if (!strcmp(p2->str, str))
return;
p2 = malloc(sizeof(strlst_t)); p2 = malloc(sizeof(strlst_t));
if(p2) p2->str = strdup(str); if (p2)
if(!p2 || !p2->str) p2->str = strdup(str);
if (!p2 || !p2->str)
out_of_memory(); out_of_memory();
p2->next = NULL; p2->next = NULL;
if(p1 == NULL) *listp = p2; if (p1 == NULL)
else p1->next = p2; *listp = p2;
else
p1->next = p2;
} }
int is_dir(char *pathname) int
is_dir(char *pathname)
{ {
struct stat buf; struct stat buf;
if(stat(pathname, &buf) == -1) if (stat(pathname, &buf) == -1)
return 0; return 0;
return S_ISDIR(buf.st_mode); return S_ISDIR(buf.st_mode);
} }
int is_nonempty_file(char *pathname) int
is_nonempty_file(char *pathname)
{ {
struct stat buf; struct stat buf;
if(stat(pathname, &buf) == -1) if (stat(pathname, &buf) == -1)
return 0; return 0;
return S_ISREG(buf.st_mode) && buf.st_size > 0; return S_ISREG(buf.st_mode) && buf.st_size > 0;