/*****************************************************************************

	ParsCL()

	This functions parses the command line that was used to invoke
TECOC.  A UNIX-style command line is expected to be represented by the
argc and argv arguments to this function.  If the command line syntax is
bad,  it prints an error message and aborts the program.  If the syntax is
good,  it sets global variables and returns to its caller.  The global
variables set by this function are the ones define with EXTERN below.

*****************************************************************************/

#include "ZPort.h"		/* define portability identifiers */
#include "DefTeco.h"		/* define general identifiers */
#include "ChrMacs.h"		/* define character processing macros */
	
extern	ZDspBf();		/* display a buffer on the terminal */
extern	ZAlloc();		/* cleanup and exit */

EXTERN	char	*DArg;		/* argument part of -d command line switch */
EXTERN	char	*FBfPtr;	/* pointer into file specification buffer */
EXTERN	BOOLEAN	FileDf;		/* was a filespec on the command line? */
EXTERN	BOOLEAN	Swit_C;		/* was there a -c on the command line? */
EXTERN	BOOLEAN	Swit_D;		/* was there a -d on the command line? */
EXTERN	BOOLEAN	Swit_M;		/* was there a -m on the command line? */
EXTERN	BOOLEAN	Swit_P;		/* was there a -p on the command line? */
EXTERN	BOOLEAN	Swit_R;		/* was there a -r on the command line? */


VOID UDumbo()
{
ZDspBf("\015\012", 2);
ZDspBf("\tInvalid format.  The format is\015\012", 33);
ZDspBf("\015\012", 2);
ZDspBf("\t\ttecoc [-c] [-d data] [-m] [-p] [-r] [filespec]\015\012", 50);
ZDspBf("\015\012", 2);
ZDspBf("\twhere the switches have the following meanings:\015\012", 50);
ZDspBf("\015\012", 2);
ZDspBf("\t\t-c\tdo not create the file if it doesn't exist\015\012", 49);
ZDspBf("\t\t-d\tpreceeds data when the -p switch is used\015\012", 47);
ZDspBf("\t\t-m\tdo not remember the last-file-edited\015\012", 43);
ZDspBf("\t\t-p\texecute a file containing TECO commands\015\012", 46);
ZDspBf("\t\t-r\topen the file read-only\015\012", 30);
ZAbort();
}


VOID ParsCL(argc, argv)		/* parse a TECOC command line */
int argc;
char *argv[];
{
	int i;
	char *TmpPtr;

#if DEBUGGING
DbgInd+=2;
if(DbgLvl>=1){DbgMsg();DbgDBf("ParsCL: called\015\012");DbgROf();}
#endif

	FileDf =	/* if YES, a filespec exists */
	Swit_C =	/* if YES, don't create on file not found */
	Swit_D =	/* if YES, data exists for TECO macro (mung) */
	Swit_M =	/* if YES, don't use last-file-edited memory */
	Swit_P =	/* if YES, execute TECO macro (mung) */
	Swit_R = NO;	/* if YES, open file for reading only */

	for (i=1; i<argc; ++i)				/* for all args */
		{
		TmpPtr = argv[i];
		if (*TmpPtr == '-')			/* if it's a switch */
			{
			++TmpPtr;
			while (*TmpPtr != '\0')
				{
				switch (To_Upper(*TmpPtr))
					{
					case 'C':	Swit_C = YES;
							break;
					case 'D':	Swit_D = YES;
							++i;
							if (i == argc)
								UDumbo();
							DArg = argv[i];
							break;
					case 'M':	Swit_M = YES;
							break;
					case 'P':	Swit_P = YES;
							break;
					case 'R':	Swit_R = YES;
							break;
					default:
							UDumbo();
					}
				++TmpPtr;
				}
			}
		else
			{
			if (FileDf)		/* file already defined? */
				UDumbo();
			FileDf = YES;
			while (*FBfPtr = *TmpPtr++)
				FBfPtr++;
			}
		}

	if (Swit_P)
		{
		if (Swit_R)
			{
ZDspBf("The P and R command line switches are incompatible\015\012", 52);
			UDumbo();
			}
		}
	else
		if (Swit_D)
			{
ZDspBf("The D command line switch requires the P switch\015\012", 49);
			UDumbo();
			}
#if DEBUGGING
if (DbgLvl>=1)
{DbgMsg();DbgDBf("ParsCL: returning\015\012");DbgROf();}DbgInd-=2;
#endif
}
