static char rcsid[] = "$Header: /usr/jjc/dvitops/RCS/main.c,v 1.8 90/08/14 13:10:37 jjc Rel $";

#include "dvitops.h"
 
char *texfonts = TEXFONTS;
char *texinputs = TEXINPUTS;
char *texpk = TEXPK;
int dpi = DPI;
char *page_used[MAXFONTS];
long f_memory = 200*1024L;
/* each region has a number which indexes into the `region' vector */
/* the default region is `NO_REGION', and doesn't have an entry */
 
int current_region = NO_REGION;
int nregions = 0;
int quiet = FALSE;

#ifdef PROTO
static void usage(void);
#else
static void usage();
#endif

#ifdef PROTO
static char **newargv(char **argv, int *argcp);
#else 
static char **newargv();
#endif
 
char *program_name; 

void main(argc, argv)
int argc; 
char *argv[];
{
	int c;
	int i;
	char *ptr;
#ifndef HAVE_REDIRECTION
	FILE *ofp;
#endif
	FILE *fp;
	struct option_info option;
	int exit_code;
	program_name = argv[0];
	opterr = 0; /* disable message from getopt */
	option.mag = 0; /* specified by default in dvi file */
	option.voffset = option.hoffset = 72.0; /* 1in in PostScript points */
	option.reverse = 0;
	option.maxpages = INT_MAX;
	option.copies = 1;
	option.sort = FALSE;
	option.paper = PAPER;
	if ((ptr = getenv("TEXINPUTS")) != NULL)
		texinputs = ptr;
	if ((ptr = getenv("TEXFONTS")) != NULL)
		texfonts = ptr;	
	if ((ptr = getenv("TEXPK")) != NULL)
		texpk = ptr;
	for (i = 0; i < 10; i++)
		option.start_page[i] = WILD;
	argv = newargv(argv, &argc);
	while ((c = getopt(argc, argv, "qf:y:c:m:rp:n:sh:v:d:")) != EOF)
		switch (c) {
		case 'q':
			quiet = TRUE;
			break;
		case 'p':
			option.paper = optarg;
			break;
		case 'y':
			f_memory = atol(optarg)*1024L;
			break;
		case 'c':
			option.copies = atoi(optarg);
			if (option.copies == 0)
				option.copies = 1;
			break;
		case 'm':
			option.mag = atoi(optarg);
			if (option.mag <= 0) {
				message(ERROR, "bad magnification: using 1000 instead");
				option.mag = 1000;
			}
			break;
		case 'r':
			option.reverse = !option.reverse;
			break;
		case 'f':
			ptr = optarg;
			for (i = 0; i < 10 && *ptr != '\0'; i++) {
				if (*ptr == '*') {
					option.start_page[i] = WILD;
					++ptr;
				}
				else {
					option.start_page[i] = atol(ptr);
					if (*ptr == '+' || *ptr == '-')
						++ptr;
					while (isdigit(*ptr))
						ptr++;
				}
				if (*ptr == '.')
					++ptr;
				else if (*ptr == '\0')
					break;
				else
					usage();
			}
			break;
		case 'n':
			option.maxpages = atoi(optarg);
			break;
		case 's':
			option.sort = TRUE;
			break;
		case 'h':
			if (bigpoint(optarg, (char **)NULL,&option.hoffset) == 0)
				usage();
			break;
		case 'v':
			if (bigpoint(optarg, (char **)NULL, &option.voffset) == 0)
				usage();
			break;
		case 'd':
			dpi = atoi(optarg);
			break;
		case '?':
			usage();
		default:
			cant_happen();
		}
	if (argc - optind == 1) {
#ifndef HAVE_REDIRECTION
		int len;
		char ofilename[FILENAME_MAX+1];
#endif
		if ((fp = xfopen(argv[optind], TRUE, (char *)NULL, ".dvi")) == NULL)
			message(FATAL_ERROR, "can't find %s", argv[optind]);
#ifndef HAVE_REDIRECTION
		len = strlen(argv[optind]);
		if (len >= 4 && 
#ifdef CASE_INSENSITIVE_FILENAMES
			stricmp(argv[optind] - 4, ".dvi") == 0
#else
			strcmp(argv[optind] - 4, ".dvi") == 0
#endif
			) {
			if (len < FILENAME_MAX) {
				strcpy(ofilename, argv[optind]);
				strcpy(ofilename + len - 3, "ps");
			}
			else
				message(FATAL_ERROR, "input filename `%s' too long", argv[optind]);
		}
		else if (len <= FILENAME_MAX - 3) {
			strcpy(ofilename, argv[optind]);
			strcpy(ofilename + len, ".ps");
		}
		else
			message(FATAL_ERROR, "input filename `%s' too long", argv[optind]);
		if ((ofp = fopen(ofilename, "w")) == NULL)
			message(FATAL_ERROR, "can't open `%s' for output", ofilename);
#endif
	}
#ifdef HAVE_REDIRECTION
	else if (argc == optind) {
#ifdef HAVE_ISATTY
		if (isatty(fileno(stdin)))
			message(FATAL_ERROR, "can't seek on standard input");
#endif
		fp = stdin;
	}
#endif
	else
		usage();
#ifdef HAVE_SETVBUF
	setvbuf(fp, (char *)NULL, _IOFBF, 16384); /* ignore any error */
#endif
	read_dvi_file(fp,
#ifdef HAVE_REDIRECTION
				  stdout,
#else
				  ofp, 
#endif
				  &option);
	switch (history) {
	case INFORMATION:
		exit_code = NORMAL_EXIT_CODE;
		break;
	case WARNING:
		exit_code = WARNING_EXIT_CODE;
		break;
	case ERROR:
		exit_code = ERROR_EXIT_CODE;
		break;
	case FATAL_ERROR:
		exit_code = FATAL_ERROR_EXIT_CODE;
		break;
	default:
		cant_happen();
		break;
	}
	exit(exit_code);
}
 
/* this looks for an environmental variable called DVITOPS
   and types it into the command line */

#define MAXARGS 256
 
static char **newargv(argv, argcp)
char **argv;
int *argcp;
{
	char **p;
	int i;
	int argc = 0;
	char *s, *env;
	if ((s = getenv("DVITOPS")) == NULL)
		return argv;
	if ((env = (char *)malloc(strlen(s) + 1)) == NULL)
		out_of_memory();
	if ((p = (char **)malloc(sizeof(char *)*MAXARGS)) == NULL)
		out_of_memory();
	strcpy(env, s);
	p[argc++] = argv[0];
	s = strtok(env, " \t");
	while (s != NULL && argc < MAXARGS) {
		p[argc++] = s;
		s = strtok((char *)NULL, " \t");
	}
	for (i = 1; i < *argcp && argc < MAXARGS; i++)
		p[argc++] = argv[i];
	if (argc == MAXARGS) {
		message(ERROR, "too many arguments in environment variable DVITOPS");
		return argv;
	}
	p[argc] = NULL;
	*argcp = argc;
	return p;
}
 
 
static void usage()
{
	message(FATAL_ERROR, 
#ifdef HAVE_REDIRECTION
	"command line error: usage:\n\
dvitops [-qrs] [-c ncopies] [-f firstpage] [-n npages] [-m magnification]\n\
        [-y kmem] [-h hoffset] [-v voffset] [-p papersize] [-d dpi] [dvifile]"
#else
	"command line error: usage:\n\
dvitops [-Vqrs] [-c ncopies] [-f firstpage] [-n npages] [-m magnification]\n\
        [-y kmem] [-h hoffset] [-v voffset] [-p papersize] [-d dpi] dvifile"
#endif
			);
}

/*
Local Variables:
tab-width: 4
c-indent-level: 4
c-continued-statement-offset: 4
c-brace-offset: -4
c-argdecl-indent: 0
c-label-offset: -4
End:
*/