[net.sources] Reposting of stdmain.c, with enhancements and bug fixes

ken@turtlevax.UUCP (Ken Turkowski) (03/11/85)

Due to popular demand, here is a reposting of stdmain.c, a program that
parses command line arguments, opens standard input and output, and
other things that every main program tends to have.  you may want to
modify this for special things that you do at your site, such as the
use of RCS rather than SCCS ID's.

-----------------------------------------------------------------

# This is a shell archive.  Remove anything before this line, then
# unpack it by saving it in a file and typing "sh file".  (Files
# unpacked will be owned by you and have default permissions.)
#
# This archive contains:
# stdmain.c

echo x - stdmain.c
cat > "stdmain.c" << '//E*O*F stdmain.c//'
# include <stdio.h>

extern int errno;
int verbose = { 0 };
int exitcode = { 0 };
char *progname;
Usage() {
	fprintf(stderr,
"Usage: %s\n", progname);
}

static char SccsId[] = "%W%	%G%";

# define ARGVAL() (*++(*argv) || (--argc && *++argv))

main(argc, argv)
int argc;
char **argv;
{
	char *rindex();
	char *ofname = { NULL };
#ifndef MANYFILES
	char *ifname = { NULL };
#else MANYFILES
	char **filelist, **fileptr;
	char *malloc();
	filelist = fileptr = (char **)(malloc(argc * sizeof(*argv)));
	*filelist = NULL;
#endif MANYFILES


	/* Initialization */
	if ((progname = rindex(*argv, '/')) == NULL)	progname = *argv;
	else						progname++;

	/* Argument Processing */
	for (argc--, argv++; argc > 0; argc--, argv++) {
	    if (**argv == '-') {	/* A flag argument */
		while (*++(*argv)) {	/* Process all flags in this arg */
		    switch (**argv) {
			case 'v':
			    verbose = 1;
			    break;
			case 'o':	/* Output file name */
			    if (!ARGVAL())
				exeunt("Missing output file name\n");
			    ofname = *argv;
			    goto nextarg;
			default:
			    fprintf(stderr, "Unknown flag: '%c'; ", **argv);
			    Usage();
		    }
		}
	    }
	    else {		/* Input file name */
#ifndef MANYFILES
		if (ifname == NULL) {
		    ifname = *argv;
		}
		else {
		    fprintf(stderr,
			"Duplicate input filenames: keeping %s, ignoring %s\n",
			ifname, *argv);
		}
#else MANYFILES
		*fileptr++ = *argv;	/* Build input file list */
		*fileptr = NULL;
#endif MANYFILES
		/* goto nextarg; */
	    }
	    nextarg: continue;
	}

	/* Open output file */
	if (ofname && (freopen(ofname, "w", stdout) == NULL)) {
	    perror(ofname); exit(errno);
	}

#ifndef MANYFILES
	if (ifname && (freopen(ifname, "r", stdin) == NULL)) {
	    perror(ifname); exit(errno);
	}
	/* ACTION HERE */
#else MANYFILES
	/* Process each input file */
	if (*filelist != NULL) {
	    for (fileptr = filelist; *fileptr; fileptr++) {
		/* Open input file */
		if ((freopen(*fileptr, "r", stdin)) == NULL) {
			perror(*fileptr); continue;
		}
		/* ACTION HERE */
	    }
	}
	else {		/* Standard input */
	    /* ACTION HERE */
	}
#endif MANYFILES

	exit(exitcode);
}

/* Exeunt -- a graceful exit for abnormal conditions */
exeunt(mess, arg1, arg2, arg3, arg4)
char *mess;
int arg1, arg2, arg3, arg4;
{
	fprintf(stderr, "%s: ", progname);
	fprintf(stderr, mess, arg1, arg2, arg3, arg4);
	putc('\n', stderr);
	exit((exitcode == 0) ? -1 : exitcode);
}
//E*O*F stdmain.c//

exit 0
-- 

Ken Turkowski @ CADLINC, Menlo Park, CA
UUCP: {amd,decwrl,nsc,seismo,spar}!turtlevax!ken
ARPA: turtlevax!ken@DECWRL.ARPA