/*  routine to get command line options for tek2ps

    aug, 97 - name changed from getopt.c to tekopt.c to avoid conflict
  	with routine in DECC run-time library; also optind -> optidx
*/

#include <stdio.h>

/*
 * get option letter from argument vector
 */
int	optidx = 1;		/* index into parent argv vector */
int	optopt;			/* character checked for validity */
char	*optarg;		/* argument associated with option */

#define BADCH	(int)'?'
#define EMSG	""
#define tell(s)	fputs(*nargv,stderr);fputs(s,stderr); \
		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);

int tekopt (int nargc, char *nargv[], char *ostr)
{
static char	*place = EMSG;	/* option letter processing */
register char	*oli;		/* option letter list index */
char	*index();

	if(!*place) {			/* update scanning pointer */
		if(optidx >= nargc || *(place = nargv[optidx]) != '-'
			|| !*++place) return(EOF);
		if (*place == '-') {	/* found "--" */
		    ++optidx;
		    return(EOF);
		}
	}				/* option letter okay? */
	if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
		if(!*place) ++optidx;
		tell(": illegal option -- ");
	}
	if (*++oli != ':') {		/* don't need argument */
		optarg = NULL;
		if (!*place) ++optidx;
	} else {			/* need an argument */
		if (*place) optarg = place;	/* no white space */
		else if (nargc <= ++optidx) {	/* no arg */
			place = EMSG;
			tell(": option requires an argument -- ");
		}
	 	else optarg = nargv[optidx];	/* white space */
		place = EMSG;
		++optidx;
	}
	return(optopt);			/* dump back option letter */
}
