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
* All Rights Reserved.
@ -38,7 +38,7 @@
#include <sys/cdefs.h>
#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
#include <stdlib.h>
@ -122,7 +122,8 @@ void gen_outputs(void);
extern char *crunched_skel[];
int main(int argc, char **argv)
int
main(int argc, char **argv)
{
char *p;
int optc;
@ -148,9 +149,10 @@ int main(int argc, char **argv)
useobjs = 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) {
case 'f': readcache = 0; break;
case 'q': verbose = 0; break;
@ -173,7 +175,8 @@ int main(int argc, char **argv)
argc -= optind;
argv += optind;
if(argc != 1) usage();
if (argc != 1)
usage();
/*
* generate filenames
@ -184,9 +187,12 @@ int main(int argc, char **argv)
/* confname = `basename infilename .conf` */
if((p=strrchr(infilename, '/')) != NULL) strcpy(confname, p+1);
else strcpy(confname, infilename);
if((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) *p = '\0';
if ((p = strrchr(infilename, '/')) != NULL)
strcpy(confname, p + 1);
else
strcpy(confname, infilename);
if ((p = strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
*p = '\0';
if (!*outmkname)
(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,
"%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 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",
pname, infilename);
exit(1);
}
parse_one_file(infilename);
if(readcache && is_nonempty_file(cachename)) {
if (readcache && is_nonempty_file(cachename)) {
reading_cache = 1;
parse_one_file(cachename);
}
}
void parse_one_file(char *filename)
void
parse_one_file(char *filename)
{
char *fieldv[MAXFIELDS];
int fieldc;
@ -260,29 +269,30 @@ void parse_one_file(char *filename)
status(line);
strcpy(curfilename, filename);
if((cf = fopen(curfilename, "r")) == NULL) {
if ((cf = fopen(curfilename, "r")) == NULL) {
perror(curfilename);
goterror = 1;
return;
}
linenum = 0;
while(fgets(line, MAXLINELEN, cf) != NULL) {
while (fgets(line, MAXLINELEN, cf) != NULL) {
linenum++;
parse_line(line, &fieldc, fieldv, MAXFIELDS);
if(fieldc < 1) continue;
if(!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs;
else if(!strcmp(fieldv[0], "progs")) f = add_progs;
else if(!strcmp(fieldv[0], "ln")) f = add_link;
else if(!strcmp(fieldv[0], "libs")) f = add_libs;
else if(!strcmp(fieldv[0], "special")) f = add_special;
if (fieldc < 1)
continue;
if (!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs;
else if (!strcmp(fieldv[0], "progs")) f = add_progs;
else if (!strcmp(fieldv[0], "ln")) f = add_link;
else if (!strcmp(fieldv[0], "libs")) f = add_libs;
else if (!strcmp(fieldv[0], "special")) f = add_special;
else {
fprintf(stderr, "%s:%d: skipping unknown command `%s'.\n",
curfilename, linenum, fieldv[0]);
goterror = 1;
continue;
}
if(fieldc < 2) {
if (fieldc < 2) {
fprintf(stderr,
"%s:%d: %s command needs at least 1 argument, skipping.\n",
curfilename, linenum, fieldv[0]);
@ -292,7 +302,7 @@ void parse_one_file(char *filename)
f(fieldc, fieldv);
}
if(ferror(cf)) {
if (ferror(cf)) {
perror(curfilename);
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;
p = line;
*fc = 0;
while(1) {
while(isspace(*p)) p++;
if(*p == '\0' || *p == '#') break;
for (;;) {
while (isspace(*p))
p++;
if (*p == '\0' || *p == '#')
break;
if(*fc < nf) fv[(*fc)++] = p;
while(*p && !isspace(*p) && *p != '#') p++;
if(*p == '\0' || *p == '#') break;
if (*fc < nf)
fv[(*fc)++] = p;
while (*p && !isspace(*p) && *p != '#')
p++;
if (*p == '\0' || *p == '#')
break;
*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;
char tmppath[MAXPATHLEN];
for(i=1;i<argc;i++) {
for (i = 1; i < argc; i++) {
if (argv[i][0] == '/')
strcpy(tmppath, argv[i]);
else {
@ -335,7 +353,7 @@ void add_srcdirs(int argc, char **argv)
strcat(tmppath, "/");
strcat(tmppath, argv[i]);
}
if(is_dir(tmppath))
if (is_dir(tmppath))
add_string(&srcdirs, tmppath);
else {
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;
for(i=1;i<argc;i++)
for (i = 1; i < argc; i++)
add_prog(argv[i]);
}
void add_prog(char *progname)
void
add_prog(char *progname)
{
prog_t *p1, *p2;
/* add to end, but be smart about dups */
for(p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
if(!strcmp(p2->name, progname)) return;
for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
if (!strcmp(p2->name, progname))
return;
p2 = malloc(sizeof(prog_t));
if(p2) p2->name = strdup(progname);
if(!p2 || !p2->name)
if (p2)
p2->name = strdup(progname);
if (!p2 || !p2->name)
out_of_memory();
p2->next = NULL;
if(p1 == NULL) progs = p2;
else p1->next = p2;
if (p1 == NULL)
progs = p2;
else
p1->next = p2;
p2->ident = p2->srcdir = p2->objdir = 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;
prog_t *p = find_prog(argv[1]);
if(p == NULL) {
if (p == NULL) {
fprintf(stderr,
"%s:%d: no prog %s previously declared, skipping link.\n",
curfilename, linenum, argv[1]);
goterror = 1;
return;
}
for(i=2;i<argc;i++)
for (i = 2; i < argc; i++)
add_string(&p->links, argv[i]);
}
void add_libs(int argc, char **argv)
void
add_libs(int argc, char **argv)
{
int i;
for(i=1;i<argc;i++)
for (i = 1; i < argc; i++)
add_string(&libs, argv[i]);
}
void add_special(int argc, char **argv)
void
add_special(int argc, char **argv)
{
int i;
prog_t *p = find_prog(argv[1]);
if(p == NULL) {
if(reading_cache) return;
if (p == NULL) {
if (reading_cache)
return;
fprintf(stderr,
"%s:%d: no prog %s previously declared, skipping special.\n",
curfilename, linenum, argv[1]);
@ -419,15 +447,19 @@ void add_special(int argc, char **argv)
return;
}
if(!strcmp(argv[2], "ident")) {
if(argc != 4) goto argcount;
if((p->ident = strdup(argv[3])) == NULL)
if (!strcmp(argv[2], "ident")) {
if (argc != 4)
goto argcount;
if ((p->ident = strdup(argv[3])) == NULL)
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((p->srcdir = strdup(argv[3])) == NULL)
if ((p->srcdir = strdup(argv[3])) == NULL)
out_of_memory();
} else {
char tmppath[MAXPATHLEN];
@ -437,38 +469,46 @@ void add_special(int argc, char **argv)
strcpy(tmppath, topdir);
strcat(tmppath, "/");
strcat(tmppath, argv[3]);
if((p->srcdir = strdup(tmppath)) == NULL)
if ((p->srcdir = strdup(tmppath)) == NULL)
out_of_memory();
}
return;
}
else if(!strcmp(argv[2], "objdir")) {
if(argc != 4) goto argcount;
if((p->objdir = strdup(argv[3])) == NULL)
if (!strcmp(argv[2], "objdir")) {
if (argc != 4)
goto argcount;
if ((p->objdir = strdup(argv[3])) == NULL)
out_of_memory();
return;
}
else if(!strcmp(argv[2], "objs")) {
if (!strcmp(argv[2], "objs")) {
p->objs = NULL;
for(i=3;i<argc;i++)
for (i = 3; i < argc; i++)
add_string(&p->objs, argv[i]);
return;
}
else if(!strcmp(argv[2], "objpaths")) {
if (!strcmp(argv[2], "objpaths")) {
p->objpaths = NULL;
for(i=3;i<argc;i++)
for (i = 3; i < argc; i++)
add_string(&p->objpaths, argv[i]);
return;
}
else if(!strcmp(argv[2], "keepsymbols")) {
if (!strcmp(argv[2], "keepsymbols")) {
p->keepsymbols = NULL;
for (i=3;i<argc;i++)
for (i = 3; i < argc; i++)
add_string(&p->keepsymbols, argv[i]);
return;
}
else {
fprintf(stderr, "%s:%d: bad parameter name `%s', skipping line.\n",
curfilename, linenum, argv[2]);
goterror = 1;
}
return;
argcount:
fprintf(stderr,
"%s:%d: too %s arguments, expected \"special %s %s <string>\".\n",
@ -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;
for(p = progs; p != NULL; p = p->next)
if(!strcmp(p->name, str)) return p;
for (p = progs; p != NULL; p = p->next)
if (!strcmp(p->name, str))
return p;
return NULL;
}
@ -511,11 +553,12 @@ char *genident(char *str);
char *dir_search(char *progname);
void gen_outputs(void)
void
gen_outputs(void)
{
prog_t *p;
for(p = progs; p != NULL; p = p->next)
for (p = progs; p != NULL; p = p->next)
fillin_program(p);
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 *srcparent;
@ -538,15 +582,15 @@ void fillin_program(prog_t *p)
(void)snprintf(line, sizeof(line), "filling in parms for %s", p->name);
status(line);
if(!p->ident)
if (!p->ident)
p->ident = genident(p->name);
if(!p->srcdir) {
if (!p->srcdir) {
srcparent = dir_search(p->name);
if(srcparent) {
if (srcparent) {
(void)snprintf(path, sizeof(path), "%s/%s", srcparent, p->name);
if(is_dir(path)) {
if (is_dir(path)) {
if (path[0] == '/') {
if((p->srcdir = strdup(path)) == NULL)
if ((p->srcdir = strdup(path)) == NULL)
out_of_memory();
} else {
char tmppath[MAXPATHLEN];
@ -556,13 +600,14 @@ void fillin_program(prog_t *p)
strcpy(tmppath, topdir);
strcat(tmppath, "/");
strcat(tmppath, path);
if((p->srcdir = strdup(tmppath)) == NULL)
if ((p->srcdir = strdup(tmppath)) == NULL)
out_of_memory();
}
}
}
}
if(!p->objdir && p->srcdir && useobjs) {
if (!p->objdir && p->srcdir && useobjs) {
if (makeobjdirprefix) {
(void)snprintf(path, sizeof(path), "%s/%s", makeobjdirprefix, p->srcdir);
if (is_dir(path))
@ -575,7 +620,7 @@ void fillin_program(prog_t *p)
}
if (!p->objdir) {
(void)snprintf(path, sizeof(path), "%s/obj", p->srcdir);
if(is_dir(path))
if (is_dir(path))
p->objdir = strdup(path);
}
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);
if(!p->objs && p->srcdir && is_nonempty_file(path))
if (!p->objs && p->srcdir && is_nonempty_file(path))
fillin_program_objs(p, p->srcdir);
if(!p->objpaths && p->objs) {
if (!p->objpaths && p->objs) {
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);
add_string(&p->objpaths, line);
}
} 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);
add_string(&p->objpaths, line);
}
}
}
if(!p->srcdir && verbose)
if (!p->srcdir && verbose)
fprintf(stderr, "%s: %s: warning: could not find source directory.\n",
infilename, p->name);
if(!p->objs && verbose)
if (!p->objs && verbose)
fprintf(stderr, "%s: %s: warning: could not find any .o files.\n",
infilename, p->name);
if(!p->objpaths) {
if (!p->objpaths) {
fprintf(stderr,
"%s: %s: error: no objpaths specified or calculated.\n",
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;
int rc;
@ -628,12 +674,12 @@ void fillin_program_objs(prog_t *p, char *dirpath)
/* discover the objs from the srcdir Makefile */
(void)snprintf(tempfname, sizeof(tempfname), "/tmp/%sXXXXXX", confname);
if((fd = mkstemp(tempfname)) < 0) {
if ((fd = mkstemp(tempfname)) < 0) {
perror(tempfname);
exit(1);
}
if((f = fdopen(fd, "w")) == NULL) {
if ((f = fdopen(fd, "w")) == NULL) {
perror(tempfname);
goterror = 1;
return;
@ -649,15 +695,15 @@ void fillin_program_objs(prog_t *p, char *dirpath)
(void)snprintf(line, sizeof(line),
"cd %s && %s -B -f %s %s CRUNCHEDPROG=1 crunchgen_objs 2>&1", dirpath,
makebin, tempfname, makeflags);
if((f = popen(line, "r")) == NULL) {
if ((f = popen(line, "r")) == NULL) {
perror("submake pipe");
goterror = 1;
unlink(tempfname);
return;
}
while(fgets(line, MAXLINELEN, f)) {
if(strncmp(line, "OBJS= ", 6)) {
while (fgets(line, MAXLINELEN, f)) {
if (strncmp(line, "OBJS= ", 6)) {
if (strcmp(line,
"sh: warning: running as root with dot in PATH\n") == 0)
continue;
@ -666,42 +712,50 @@ void fillin_program_objs(prog_t *p, char *dirpath)
continue;
}
cp = line + 6;
while(isspace(*cp)) cp++;
while(*cp) {
while (isspace(*cp))
cp++;
while (*cp) {
obj = cp;
while(*cp && !isspace(*cp)) cp++;
if(*cp) *cp++ = '\0';
while (*cp && !isspace(*cp))
cp++;
if (*cp)
*cp++ = '\0';
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);
goterror = 1;
}
unlink(tempfname);
}
void remove_error_progs(void)
void
remove_error_progs(void)
{
prog_t *p1, *p2;
p1 = NULL; p2 = progs;
while(p2 != NULL) {
if(!p2->goterror)
while (p2 != NULL) {
if (!p2->goterror)
p1 = p2, p2 = p2->next;
else {
/* delete it from linked list */
fprintf(stderr, "%s: %s: ignoring program because of errors.\n",
infilename, p2->name);
if(p1) p1->next = p2->next;
else progs = p2->next;
if (p1)
p1->next = p2->next;
else
progs = p2->next;
p2 = p2->next;
}
}
}
void gen_specials_cache(void)
void
gen_specials_cache(void)
{
FILE *cachef;
prog_t *p;
@ -709,7 +763,7 @@ void gen_specials_cache(void)
(void)snprintf(line, sizeof(line), "generating %s", cachename);
status(line);
if((cachef = fopen(cachename, "w")) == NULL) {
if ((cachef = fopen(cachename, "w")) == NULL) {
perror(cachename);
goterror = 1;
return;
@ -718,13 +772,13 @@ void gen_specials_cache(void)
fprintf(cachef, "# %s - parm cache generated from %s by crunchgen %s\n\n",
cachename, infilename, CRUNCH_VERSION);
for(p = progs; p != NULL; p = p->next) {
for (p = progs; p != NULL; p = p->next) {
fprintf(cachef, "\n");
if(p->srcdir)
if (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);
if(p->objs) {
if (p->objs) {
fprintf(cachef, "special %s objs", p->name);
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;
FILE *outmk;
@ -743,7 +798,7 @@ void gen_output_makefile(void)
(void)snprintf(line, sizeof(line), "generating %s", outmkname);
status(line);
if((outmk = fopen(outmkname, "w")) == NULL) {
if ((outmk = fopen(outmkname, "w")) == NULL) {
perror(outmkname);
goterror = 1;
return;
@ -754,7 +809,7 @@ void gen_output_makefile(void)
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);
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;
FILE *outcf;
@ -776,7 +832,7 @@ void gen_output_cfile(void)
(void)snprintf(line, sizeof(line), "generating %s", outcfname);
status(line);
if((outcf = fopen(outcfname, "w")) == NULL) {
if ((outcf = fopen(outcfname, "w")) == NULL) {
perror(outcfname);
goterror = 1;
return;
@ -787,17 +843,17 @@ void gen_output_cfile(void)
outcfname, infilename, CRUNCH_VERSION);
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);
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, "\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",
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",
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;
@ -818,31 +875,37 @@ char *genident(char *str)
* programs named "foo.bar" and "foobar" to map to the same identifier.
*/
if((n = strdup(str)) == NULL)
if ((n = strdup(str)) == NULL)
return NULL;
for(d = s = n; *s != '\0'; s++) {
if(*s == '-') *d++ = '_';
else if(*s == '_' || isalnum(*s)) *d++ = *s;
for (d = s = n; *s != '\0'; s++) {
if (*s == '-')
*d++ = '_';
else
if (*s == '_' || isalnum(*s))
*d++ = *s;
}
*d = '\0';
return n;
}
char *dir_search(char *progname)
char *
dir_search(char *progname)
{
char path[MAXPATHLEN];
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);
if(is_dir(path)) return dir->str;
if (is_dir(path))
return dir->str;
}
return NULL;
}
void top_makefile_rules(FILE *outmk)
void
top_makefile_rules(FILE *outmk)
{
prog_t *p;
@ -858,19 +921,19 @@ void top_makefile_rules(FILE *outmk)
#endif
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, "\n");
fprintf(outmk, "DPADD+= ${CRUNCHED_OBJS}\n");
fprintf(outmk, "LDADD+= ${CRUNCHED_OBJS} ");
output_strlst(outmk, libs);
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, "\n\n");
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, "\n\n");
@ -892,13 +955,15 @@ void top_makefile_rules(FILE *outmk)
execfname);
}
void bottom_makefile_rules(FILE *outmk)
void
bottom_makefile_rules(FILE *outmk)
{
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;
@ -907,7 +972,7 @@ void prog_makefile_rules(FILE *outmk, prog_t *p)
fprintf(outmk, "%s_OBJPATHS=", p->ident);
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_OBJS=", p->ident);
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"
".CURDIR:= ${%s_SRCDIR}\\n"
".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, "'\\\n");
fprintf(outmk, "\t| ${MAKE} -f- CRUNCHEDPROG=1 DBG=\"${DBG}\" depend ${%s_OBJS}\n\n",
p->ident);
}
else
} else
fprintf(outmk, "%s_make:\n\t@echo \"** Using existing objs for %s\"\n\n",
p->ident, p->name);
fprintf(outmk, "%s_stub.c:\n", p->name);
fprintf(outmk, "\techo \""
"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);
}
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, "\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;
int len, spaces;
if(!verbose) return;
if (!verbose)
return;
len = strlen(str);
spaces = lastlen - len;
if(spaces < 1) spaces = 1;
if (spaces < 1)
spaces = 1;
fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
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);
exit(1);
}
void add_string(strlst_t **listp, char *str)
void
add_string(strlst_t **listp, char *str)
{
strlst_t *p1, *p2;
/* add to end, but be smart about dups */
for(p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
if(!strcmp(p2->str, str)) return;
for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
if (!strcmp(p2->str, str))
return;
p2 = malloc(sizeof(strlst_t));
if(p2) p2->str = strdup(str);
if(!p2 || !p2->str)
if (p2)
p2->str = strdup(str);
if (!p2 || !p2->str)
out_of_memory();
p2->next = NULL;
if(p1 == NULL) *listp = p2;
else p1->next = p2;
if (p1 == NULL)
*listp = p2;
else
p1->next = p2;
}
int is_dir(char *pathname)
int
is_dir(char *pathname)
{
struct stat buf;
if(stat(pathname, &buf) == -1)
if (stat(pathname, &buf) == -1)
return 0;
return S_ISDIR(buf.st_mode);
}
int is_nonempty_file(char *pathname)
int
is_nonempty_file(char *pathname)
{
struct stat buf;
if(stat(pathname, &buf) == -1)
if (stat(pathname, &buf) == -1)
return 0;
return S_ISREG(buf.st_mode) && buf.st_size > 0;