Use err(3).

This commit is contained in:
Philippe Charnier 1997-09-15 06:41:09 +00:00
parent 401e64688b
commit b17e90a9c9
4 changed files with 53 additions and 74 deletions

View File

@ -96,7 +96,7 @@ int crunched_usage()
int columns, len;
struct stub *ep;
fprintf(stderr, "Usage: %s <prog> <args> ..., where <prog> is one of:\n",
fprintf(stderr, "usage: %s <prog> <args> ..., where <prog> is one of:\n",
EXECNAME);
columns = 0;
for(ep=entry_points; ep->name != NULL; ep++) {

View File

@ -64,7 +64,7 @@ hiding all unnecessary symbols.
.Pp
After
.Nm crunchgen
.Nm
is run, the crunched binary can be built by running ``make -f
<conf-name>.mk''. The component programs' object files must already
be built. A ``objs'' target, included in the output makefile, will
@ -107,7 +107,7 @@ top-level source directories in which their sources can be found.
then calculates (via the source makefiles) and caches the
list of object files and their locations. For more specialized
situations, the user can specify by hand all the parameters that
.Nm crunchgen
.Nm
needs.
.Pp
The
@ -143,7 +143,7 @@ To handle specialized situations, such as when the source is not
available or not built via a conventional Makefile, the following
.Nm special
commands can be used to set
.Nm crunchgen
.Nm
parameters for a component program.
.Bl -tag -width indent
.It Nm special Ar progname Nm srcdir Ar pathname
@ -192,12 +192,12 @@ and
which are in turn calculated from
.Nm srcdir ,
so is sometimes convenient to specify the earlier parameters and let
.Nm crunchgen
.Nm
calculate forward from there if it can.
.Pp
The makefile produced by
.Nm crunchgen
.Nm
contains an optional
.Ar objs
target that will build the object files for each component program by
@ -211,7 +211,7 @@ program is skipped in the
target.
.Sh EXAMPLE
Here is an example
.Nm crunchgen
.Nm
input conf file, named
.Dq Pa kcopy.conf :
.Pp

View File

@ -30,11 +30,12 @@
* Generates a Makefile and main C file for a crunched executable,
* from specs given in a .conf file.
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
@ -81,8 +82,6 @@ char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
int linenum = -1;
int goterror = 0;
char *pname = "crunchgen";
int verbose, readcache; /* options */
int reading_cache;
@ -107,15 +106,11 @@ int main(int argc, char **argv)
{
char *p;
int optc;
extern int optind;
extern char *optarg;
verbose = 1;
readcache = 1;
*outmkname = *outcfname = *execfname = '\0';
if(argc > 0) pname = argv[0];
while((optc = getopt(argc, argv, "lm:c:e:fq")) != -1) {
switch(optc) {
case 'f': readcache = 0; break;
@ -171,9 +166,9 @@ int main(int argc, char **argv)
void usage(void)
{
fprintf(stderr,
"%s [-fq] [-m <makefile>] [-c <c file>] [-e <exec file>] <conffile>\n",
pname);
fprintf(stderr, "%s\n%s\n",
"usage: crunchgen [-fq] [-m <makefile>] [-c <c file>]",
" [-e <exec file>] <conffile>");
exit(1);
}
@ -200,11 +195,8 @@ void add_prog(char *progname);
void parse_conf_file(void)
{
if(!is_nonempty_file(infilename)) {
fprintf(stderr, "%s: fatal: input file \"%s\" not found.\n",
pname, infilename);
exit(1);
}
if(!is_nonempty_file(infilename))
errx(1, "fatal: input file \"%s\" not found", infilename);
parse_one_file(infilename);
if(readcache && is_nonempty_file(cachename)) {
reading_cache = 1;
@ -225,7 +217,7 @@ void parse_one_file(char *filename)
strcpy(curfilename, filename);
if((cf = fopen(curfilename, "r")) == NULL) {
perror(curfilename);
warn("%s", curfilename);
goterror = 1;
return;
}
@ -241,14 +233,13 @@ void parse_one_file(char *filename)
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",
warnx("%s:%d: skipping unknown command `%s'",
curfilename, linenum, fieldv[0]);
goterror = 1;
continue;
}
if(fieldc < 2) {
fprintf(stderr,
"%s:%d: %s command needs at least 1 argument, skipping.\n",
warnx("%s:%d: %s command needs at least 1 argument, skipping",
curfilename, linenum, fieldv[0]);
goterror = 1;
continue;
@ -257,7 +248,7 @@ void parse_one_file(char *filename)
}
if(ferror(cf)) {
perror(curfilename);
warn("%s", curfilename);
goterror = 1;
}
fclose(cf);
@ -291,7 +282,7 @@ void add_srcdirs(int argc, char **argv)
if(is_dir(argv[i]))
add_string(&srcdirs, argv[i]);
else {
fprintf(stderr, "%s:%d: `%s' is not a directory, skipping it.\n",
warnx("%s:%d: `%s' is not a directory, skipping it",
curfilename, linenum, argv[i]);
goterror = 1;
}
@ -343,8 +334,7 @@ void add_link(int argc, char **argv)
prog_t *p = find_prog(argv[1]);
if(p == NULL) {
fprintf(stderr,
"%s:%d: no prog %s previously declared, skipping link.\n",
warnx("%s:%d: no prog %s previously declared, skipping link",
curfilename, linenum, argv[1]);
goterror = 1;
return;
@ -373,8 +363,7 @@ void add_special(int argc, char **argv)
if(p == NULL) {
if(reading_cache) return;
fprintf(stderr,
"%s:%d: no prog %s previously declared, skipping special.\n",
warnx("%s:%d: no prog %s previously declared, skipping special",
curfilename, linenum, argv[1]);
goterror = 1;
return;
@ -406,7 +395,7 @@ void add_special(int argc, char **argv)
add_string(&p->objpaths, argv[i]);
}
else {
fprintf(stderr, "%s:%d: bad parameter name `%s', skipping line.\n",
warnx("%s:%d: bad parameter name `%s', skipping line",
curfilename, linenum, argv[2]);
goterror = 1;
}
@ -414,8 +403,7 @@ void add_special(int argc, char **argv)
argcount:
fprintf(stderr,
"%s:%d: too %s arguments, expected \"special %s %s <string>\".\n",
warnx("%s:%d: too %s arguments, expected \"special %s %s <string>\"",
curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
goterror = 1;
}
@ -518,15 +506,14 @@ void fillin_program(prog_t *p)
}
if(!p->srcdir && verbose)
fprintf(stderr, "%s: %s: warning: could not find source directory.\n",
warnx("%s: %s: warning: could not find source directory",
infilename, p->name);
if(!p->objs && verbose)
fprintf(stderr, "%s: %s: warning: could not find any .o files.\n",
warnx("%s: %s: warning: could not find any .o files",
infilename, p->name);
if(!p->objpaths) {
fprintf(stderr,
"%s: %s: error: no objpaths specified or calculated.\n",
warnx("%s: %s: error: no objpaths specified or calculated",
infilename, p->name);
p->goterror = goterror = 1;
}
@ -541,7 +528,7 @@ void fillin_program_objs(prog_t *p, char *path)
/* discover the objs from the srcdir Makefile */
if((f = fopen(tempfname, "w")) == NULL) {
perror(tempfname);
warn("%s", tempfname);
goterror = 1;
return;
}
@ -555,14 +542,14 @@ void fillin_program_objs(prog_t *p, char *path)
sprintf(line, "make -f %s crunchgen_objs 2>&1", tempfname);
if((f = popen(line, "r")) == NULL) {
perror("submake pipe");
warn("submake pipe");
goterror = 1;
return;
}
while(fgets(line, MAXLINELEN, f)) {
if(strncmp(line, "OBJS= ", 6)) {
fprintf(stderr, "make error: %s", line);
warnx("make error: %s", line);
goterror = 1;
continue;
}
@ -577,7 +564,7 @@ void fillin_program_objs(prog_t *p, char *path)
}
}
if((rc=pclose(f)) != 0) {
fprintf(stderr, "make error: make returned %d\n", rc);
warnx("make error: make returned %d", rc);
goterror = 1;
}
unlink(tempfname);
@ -593,7 +580,7 @@ void remove_error_progs(void)
p1 = p2, p2 = p2->next;
else {
/* delete it from linked list */
fprintf(stderr, "%s: %s: ignoring program because of errors.\n",
warnx("%s: %s: ignoring program because of errors",
infilename, p2->name);
if(p1) p1->next = p2->next;
else progs = p2->next;
@ -611,7 +598,7 @@ void gen_specials_cache(void)
status(line);
if((cachef = fopen(cachename, "w")) == NULL) {
perror(cachename);
warn("%s", cachename);
goterror = 1;
return;
}
@ -645,7 +632,7 @@ void gen_output_makefile(void)
status(line);
if((outmk = fopen(outmkname, "w")) == NULL) {
perror(outmkname);
warn("%s", outmkname);
goterror = 1;
return;
}
@ -675,7 +662,7 @@ void gen_output_cfile(void)
status(line);
if((outcf = fopen(outcfname, "w")) == NULL) {
perror(outcfname);
warn("%s", outcfname);
goterror = 1;
return;
}
@ -834,8 +821,7 @@ void status(char *str)
void out_of_memory(void)
{
fprintf(stderr, "%s: %d: out of memory, stopping.\n", infilename, linenum);
exit(1);
errx(1, "%s: %d: out of memory, stopping", infilename, linenum);
}

View File

@ -56,18 +56,17 @@
* that the final crunched binary BSS size is the max of all the
* component programs' BSS sizes, rather than their sum.
*/
#include <unistd.h>
#include <a.out.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <a.out.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
char *pname = "crunchide";
void usage(void);
void add_to_keep_list(char *symbol);
@ -82,8 +81,6 @@ char **argv;
{
int ch;
if(argc > 0) pname = argv[0];
while ((ch = getopt(argc, argv, "k:f:")) != -1)
switch(ch) {
case 'k':
@ -112,8 +109,7 @@ char **argv;
void usage(void)
{
fprintf(stderr,
"Usage: %s [-k <symbol-name>] [-f <keep-list-file>] <files> ...\n",
pname);
"usage: crunchide [-k <symbol-name>] [-f <keep-list-file>] <files> ...\n");
exit(1);
}
@ -138,8 +134,7 @@ void add_to_keep_list(char *symbol)
newp = (struct keep *) malloc(sizeof(struct keep));
if(newp) newp->sym = strdup(symbol);
if(newp == NULL || newp->sym == NULL) {
fprintf(stderr, "%s: out of memory for keep list\n", pname);
exit(1);
errx(1, "out of memory for keep list");
}
newp->next = curp;
@ -165,7 +160,7 @@ void add_file_to_keep_list(char *filename)
int len;
if((keepf = fopen(filename, "r")) == NULL) {
perror(filename);
warn("%s", filename);
usage();
}
@ -202,7 +197,7 @@ void check_reloc(char *filename, struct relocation_info *relp);
void hide_syms(char *filename)
{
int inf, outf, rc;
int inf, rc;
struct stat infstat;
struct relocation_info *relp;
struct nlist *symp;
@ -212,18 +207,18 @@ void hide_syms(char *filename)
*/
if((inf = open(filename, O_RDWR)) == -1) {
perror(filename);
warn("%s", filename);
return;
}
if(fstat(inf, &infstat) == -1) {
perror(filename);
warn("%s", filename);
close(inf);
return;
}
if(infstat.st_size < sizeof(struct exec)) {
fprintf(stderr, "%s: short file\n", filename);
warnx("%s: short file", filename);
close(inf);
return;
}
@ -234,13 +229,13 @@ void hide_syms(char *filename)
*/
if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
fprintf(stderr, "%s: too big to read into memory\n", filename);
warnx("%s: too big to read into memory", filename);
close(inf);
return;
}
if((rc = read(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
fprintf(stderr, "%s: read error: %s\n", filename,
warnx("%s: read error: %s", filename,
rc == -1? strerror(errno) : "short read");
close(inf);
return;
@ -253,7 +248,7 @@ void hide_syms(char *filename)
hdrp = (struct exec *) aoutdata;
if(N_BADMAG(*hdrp)) {
fprintf(stderr, "%s: bad magic: not an a.out file\n", filename);
warnx("%s: bad magic: not an a.out file", filename);
close(inf);
return;
}
@ -301,7 +296,7 @@ void hide_syms(char *filename)
*/
lseek(inf, 0, SEEK_SET);
if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
fprintf(stderr, "%s: write error: %s\n", filename,
warnx("%s: write error: %s", filename,
rc == -1? strerror(errno) : "short write");
}
@ -313,9 +308,7 @@ void check_reloc(char *filename, struct relocation_info *relp)
{
/* bail out if we zapped a symbol that is needed */
if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
fprintf(stderr,
"%s: oops, have hanging relocation for %s: bailing out!\n",
errx(1, "%s: oops, have hanging relocation for %s: bailing out!",
filename, SYMSTR(&symbase[relp->r_symbolnum]));
exit(1);
}
}