/* CRAYON.C		Top level of Scribble print driver

	Copyright (C) 1981 by Mark of the Unicorn, Inc.
	Created 81.3.12 Gyro

This code interprets the command line and the Scribble output file, and
calls the low-level abstraction to do the printing.  This might turn
into a generalized driver.  

Modifications Schedule:
08/03/81		-page option hack by Jeffrey D. Stone.

*/


#include "crayon.h"

int stpage, curpage;   /* jds */

main (argc, argv)
	int argc;
	char **argv;
{
	int ntimes, itime;
	
	puts ("Crayon v1.1, Copyright (C) 1981 by Mark of the Unicorn, Inc.\n");
	ntimes = 1;
	stflag = TRUE;  /* jds */
	curpage = 1;  /* jds */
	stpage = 0;  /* jds */
	char *cstpage;
	pausep = FALSE;
	remark = "";
	portname = NULL;
	quietp = FALSE;
	while (--argc > 0) {
		if (**++argv == '-') {
			if (isdigit ((*argv)[1])) ntimes = atoi (&(*argv)[1]);
			else if (match ((*argv) + 1, "Pause") 
				    ||  match ((*argv) + 1, "P")) {
				pausep = TRUE;
				if (!quietp) puts ("Pausing between pages.\n");
				}
			else if (match ((*argv) + 1, "Port")) {
				portname = *++argv;
				--argc;
				if (!quietp) 
					printf ("Using '%s' port(s).\n", portname);
				}
			else if (match ((*argv) + 1, "Page")) {
				cstpage = *++argv;
				--argc;
				stpage = atoi(cstpage);
				if (!quietp)
					printf("Starting at page %d - first page is # 1.\n",stpage);
				stflag = FALSE;
				
				}
			else if (match ((*argv) + 1, "Q")) quietp = TRUE;
			else if (match ((*argv) + 1, "R")) {
				remark = *++argv;
				--argc;
				if (!quietp) printf ("Remark: '%s'.\n", remark);
				}
			else printf ("Unknown command line option: '%s'\n", *argv);
			}
		else {
			if (PrintFile (*argv, ntimes) == ABORTALL) break;
			}
		}
	}


ReadLine ()
{
	FLAG done, vanilla;
	
	inchars = intokens = 0;
	superp = subp = FALSE;
	justp = FALSE;
	ForceTok();
	done = FALSE;
	while (!done) {
		switch (IChar()) {
		
		case LF:
			linelead += PrLineV();
			break;
		case FF:
			++curpage; /* jds */
			if (!stflag && curpage >= stpage) {  /* jds */ 
				if (PrVPos() != 0) PrFF();
				PrFlush();
				stflag = TRUE;
				linelead = 0;
				break;
				}
			if (pausep) {
				PagePause();
				linelead = 0;
				}
			else linelead += PrPageV() - (linelead + PrVPos()) % PrPageV();
			break;
		case VERTSPACE:
			linelead += PrUnMica ((INext() << 8) + INext(), VERTICAL);
			break;
		default:
			done = TRUE;
			break;
			}
		if (!done) INext();
		}
	done = FALSE;
	repeat {
		vanilla = FALSE;
		switch (IChar()) {
		
		case CR:
			INext();
		case LF:
		case FF:
			done = TRUE;
			break;
		case EOF:
			return (NULL);
			break;
		case VERTSPACE:
			done = TRUE;
			break;
		case HORIZSPACE:
			ForceTok();
			GetTok();
			itokens[intokens-1].toklead += 
				PrUnMica ((INext() << 8) + INext(), HORIZONTAL);
			break;
		case BOLDON:
			AttrPush();
			attrstack[attrsp].tokbold = TRUE;
			ForceTok();
			break;
		case ROMANON:
			AttrPush();
			attrstack[attrsp].tokbold = FALSE;
			attrstack[attrsp].tokital = FALSE;
			ForceTok();
			break;
		case UNNBON:
		case UNALLON:
		case UNANON:
			AttrPush();
			attrstack[attrsp].tokuscore = IChar();
			ForceTok();
			break;
		case ITALON:
			AttrPush();
			attrstack[attrsp].tokital = TRUE;
			ForceTok();
			break;
		case BITALON:
			AttrPush();
			attrstack[attrsp].tokbold = TRUE;
			attrstack[attrsp].tokital = TRUE;
			ForceTok();
			break;
		case SUPERON:
			superp = TRUE;
			AttrPush();
			attrstack[attrsp].tokscript = IChar();
			ForceTok();
			break;
		case SUBON:
			subp = TRUE;
			AttrPush();
			attrstack[attrsp].tokscript = IChar();
			ForceTok();
			break;
		case SUBOFF:
		case SUPEROFF:
		case BITALOFF:
		case ITALOFF:
		case UNANOFF:
		case UNALLOFF:
		case UNNBOFF:
		case ROMANOFF:
		case BOLDOFF:
			AttrPop();
			ForceTok();
			break;
		case JUSTIFY:
			justp = TRUE;
			just.jlefttok = intokens;
			just.jspace = (INext() << 8) + INext();
			break;
		case TOKENBREAK:
			ForceTok();
			GetTok();
			itokens[intokens-1].tokjust = TRUE;
			itokens[intokens-1].toklead += PrSpaceH();
			break;
		default:
			vanilla = TRUE;
			break;
			}
		if (done) break;
		if (vanilla) {
			GetTok();
			iline[inchars++] = IChar();
			itokens[intokens-1].tokwidth += PrWidChar (IChar());
			}
		if (!done) INext();
		}
	itokens[intokens].tokbegin = inchars;
	return (TRUE);
	}


/* End of CRAYON.C  --  Scribble print driver top level */