Changeset View
Changeset View
Standalone View
Standalone View
head/usr.sbin/crunch/crunchgen/crunchgen.c
Show All 33 Lines | |||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <fcntl.h> | |||||
#include <paths.h> | #include <paths.h> | ||||
#include <stdbool.h> | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <sysexits.h> | |||||
#include <unistd.h> | #include <unistd.h> | ||||
#define CRUNCH_VERSION "0.2" | #define CRUNCH_VERSION "0.2" | ||||
#define MAXLINELEN 16384 | #define MAXLINELEN 16384 | ||||
#define MAXFIELDS 2048 | #define MAXFIELDS 2048 | ||||
Show All 32 Lines | |||||
strlst_t *srcdirs = NULL; | strlst_t *srcdirs = NULL; | ||||
strlst_t *libs = NULL; | strlst_t *libs = NULL; | ||||
strlst_t *libs_so = NULL; | strlst_t *libs_so = NULL; | ||||
prog_t *progs = NULL; | prog_t *progs = NULL; | ||||
char confname[MAXPATHLEN], infilename[MAXPATHLEN]; | char confname[MAXPATHLEN], infilename[MAXPATHLEN]; | ||||
char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN]; | char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN]; | ||||
char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN]; | char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN]; | ||||
bool tempfname_initialized = false; | |||||
char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */ | char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */ | ||||
char *objprefix; /* where are the objects ? */ | char *objprefix; /* where are the objects ? */ | ||||
char *path_make; | char *path_make; | ||||
int linenum = -1; | int linenum = -1; | ||||
int goterror = 0; | int goterror = 0; | ||||
int verbose, readcache; /* options */ | int verbose, readcache; /* options */ | ||||
int reading_cache; | int reading_cache; | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | main(int argc, char **argv) | ||||
if (!*outcfname) | if (!*outcfname) | ||||
snprintf(outcfname, sizeof(outcfname), "%s.c", confname); | snprintf(outcfname, sizeof(outcfname), "%s.c", confname); | ||||
if (!*execfname) | if (!*execfname) | ||||
snprintf(execfname, sizeof(execfname), "%s", confname); | snprintf(execfname, sizeof(execfname), "%s", confname); | ||||
snprintf(cachename, sizeof(cachename), "%s.cache", confname); | snprintf(cachename, sizeof(cachename), "%s.cache", confname); | ||||
snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX", | snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX", | ||||
getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname); | getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname); | ||||
tempfname_initialized = false; | |||||
parse_conf_file(); | parse_conf_file(); | ||||
if (list_mode) | if (list_mode) | ||||
exit(goterror); | exit(goterror); | ||||
gen_outputs(); | gen_outputs(); | ||||
exit(goterror); | exit(goterror); | ||||
▲ Show 20 Lines • Show All 416 Lines • ▼ Show 20 Lines | fillin_program(prog_t *p) | ||||
/* look for the source directory if one wasn't specified by a special */ | /* look for the source directory if one wasn't specified by a special */ | ||||
if (!p->srcdir) { | if (!p->srcdir) { | ||||
p->srcdir = dir_search(p->name); | p->srcdir = dir_search(p->name); | ||||
} | } | ||||
/* Determine the actual srcdir (maybe symlinked). */ | /* Determine the actual srcdir (maybe symlinked). */ | ||||
if (p->srcdir) { | if (p->srcdir) { | ||||
snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`", | snprintf(line, MAXLINELEN, "cd %s && pwd", p->srcdir); | ||||
p->srcdir); | |||||
f = popen(line,"r"); | f = popen(line,"r"); | ||||
if (!f) | if (!f) | ||||
errx(1, "Can't execute: %s\n", line); | errx(1, "Can't execute: %s\n", line); | ||||
path[0] = '\0'; | path[0] = '\0'; | ||||
fgets(path, sizeof path, f); | fgets(path, sizeof path, f); | ||||
if (pclose(f)) | if (pclose(f)) | ||||
errx(1, "Can't execute: %s\n", line); | errx(1, "Can't execute: %s\n", line); | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | fillin_program_objs(prog_t *p, char *path) | ||||
int fd, rc; | int fd, rc; | ||||
FILE *f; | FILE *f; | ||||
char *objvar="OBJS"; | char *objvar="OBJS"; | ||||
strlst_t *s; | strlst_t *s; | ||||
char line[MAXLINELEN]; | char line[MAXLINELEN]; | ||||
/* discover the objs from the srcdir Makefile */ | /* discover the objs from the srcdir Makefile */ | ||||
if ((fd = mkstemp(tempfname)) == -1) { | /* | ||||
perror(tempfname); | * We reuse the same temporary file name for multiple objects. However, | ||||
exit(1); | * some libc implementations (such as glibc) return EINVAL if there | ||||
* are no XXXXX characters in the template. This happens after the | |||||
* first call to mkstemp since the argument is modified in-place. | |||||
* To avoid this error we use open() instead of mkstemp() after the | |||||
* call to mkstemp(). | |||||
*/ | |||||
if (tempfname_initialized) { | |||||
if ((fd = open(tempfname, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) { | |||||
err(EX_OSERR, "open(%s)", tempfname); | |||||
} | } | ||||
} else if ((fd = mkstemp(tempfname)) == -1) { | |||||
err(EX_OSERR, "mkstemp(%s)", tempfname); | |||||
} | |||||
tempfname_initialized = true; | |||||
if ((f = fdopen(fd, "w")) == NULL) { | if ((f = fdopen(fd, "w")) == NULL) { | ||||
warn("%s", tempfname); | warn("fdopen(%s)", tempfname); | ||||
goterror = 1; | goterror = 1; | ||||
return; | goto out; | ||||
} | } | ||||
if (p->objvar) | if (p->objvar) | ||||
objvar = p->objvar; | objvar = p->objvar; | ||||
/* | /* | ||||
* XXX include outhdrname (e.g. to contain Make variables) | * XXX include outhdrname (e.g. to contain Make variables) | ||||
*/ | */ | ||||
if (outhdrname[0] != '\0') | if (outhdrname[0] != '\0') | ||||
Show All 18 Lines | fillin_program_objs(prog_t *p, char *path) | ||||
fclose(f); | fclose(f); | ||||
snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs", | snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs", | ||||
p->srcdir, path_make, tempfname); | p->srcdir, path_make, tempfname); | ||||
if ((f = popen(line, "r")) == NULL) { | if ((f = popen(line, "r")) == NULL) { | ||||
warn("submake pipe"); | warn("submake pipe"); | ||||
goterror = 1; | goterror = 1; | ||||
return; | goto out; | ||||
} | } | ||||
while(fgets(line, MAXLINELEN, f)) { | while(fgets(line, MAXLINELEN, f)) { | ||||
if (strncmp(line, "OBJS= ", 6)) { | if (strncmp(line, "OBJS= ", 6)) { | ||||
warnx("make error: %s", line); | warnx("make error: %s", line); | ||||
goterror = 1; | goterror = 1; | ||||
continue; | goto out; | ||||
} | } | ||||
cp = line + 6; | cp = line + 6; | ||||
while (isspace((unsigned char)*cp)) | while (isspace((unsigned char)*cp)) | ||||
cp++; | cp++; | ||||
while(*cp) { | while(*cp) { | ||||
obj = cp; | obj = cp; | ||||
while (*cp && !isspace((unsigned char)*cp)) | while (*cp && !isspace((unsigned char)*cp)) | ||||
cp++; | cp++; | ||||
if (*cp) | if (*cp) | ||||
*cp++ = '\0'; | *cp++ = '\0'; | ||||
add_string(&p->objs, obj); | add_string(&p->objs, obj); | ||||
while (isspace((unsigned char)*cp)) | while (isspace((unsigned char)*cp)) | ||||
cp++; | cp++; | ||||
} | } | ||||
} | } | ||||
if ((rc=pclose(f)) != 0) { | if ((rc=pclose(f)) != 0) { | ||||
warnx("make error: make returned %d", rc); | warnx("make error: make returned %d", rc); | ||||
goterror = 1; | goterror = 1; | ||||
} | } | ||||
out: | |||||
unlink(tempfname); | unlink(tempfname); | ||||
} | } | ||||
void | void | ||||
remove_error_progs(void) | remove_error_progs(void) | ||||
{ | { | ||||
prog_t *p1, *p2; | prog_t *p1, *p2; | ||||
▲ Show 20 Lines • Show All 437 Lines • Show Last 20 Lines |