/* TECO for DOS  Copyright (C) 1986-1991 Matt Fichtenbaum					*/
/* Based on Ultrix TECO (C) 1986-1990 GenRad Inc., Concord, MA 01742		*/
/* These programs may be copied if this copyright notice is included and	*/
/* only on a "not for sale or profit" basis									*/

/* te_data.c global variables  01/23/91  21.05 */
/* 8-bit version  08/15/91  22.27		*/
/* add directory command  03/30/92  21.49 */
/* current version 09/02/92  22.24  */
/* four buffers  03/10/93  21.46 */

#include "te_defs.h"

/* error message text */
unsigned char *errors[] =
{
	"> not in iteration",
	"Can't pop Q register",
	"Can't open output file ",
	"File not found ",
	"Invalid E character",
	"Invalid F character",
	"Invalid insert arg",
	"Invalid command",
	"Invalid number",
	"Invalid P arg",
	"Invalid \" character",
	"Invalid Q-reg name",
	"Invalid radix arg",
	"Invalid search arg",
	"Invalid search string",
	"Invalid ^ character",
	"Insufficient memory available",
	"Missing )",
	"No arg before ^_",
	"No arg before ,",
	"No arg before =",
	"No arg before )",
	"No arg before \"",
	"No arg before ;",
	"No arg before U",
	"No file for input",
	"No file for output",
	"Numeric arg with Y",
	"Output file already open",
	"Pushdown list overflow",
	"Pointer off page",
	"; not in iteration",
	"Search failure ",
	"String too long",
	"Unterminated command",
	"Unterminated macro",
	"Execution interrupted",
	"Y command suppressed",
	"Invalid W arg",
	"Numeric arg with FR",
	"Internal error",
	"EOF read from std input\r\n",
	"Invalid A arg",
	"Ambiguous file specification ",
	"System call or temp file error",
	"Missing command string",
	"Invalid buffer specification",
	"File I/O switching is implicit",
	"File open or buffer not empty",
	"Insufficient memory to execute program",
	"No system mouse support",
	"Invalid mouse parameter",
	"Internal mouse error",
	"Can't change to directory "
} ;


/* declare global variables */
	BUFFPTR freebuff = NULL;				/* buffcell free-list pointer */
	BUFFPTR dly_freebuff = NULL;			/* delayed free-list pointer */
	struct is *freedcell = NULL;			/* cell free-list pointer */

/* the text buffer headers */
	struct bh buffs[4] =
{
		{ NULL, NULL, 0, 0, 0, 0 },
		{ NULL, NULL, 0, 0, 0, 0 },
		{ NULL, NULL, 0, 0, 0, 0 },
		{ NULL, NULL, 0, 0, 0, 0 }
} ;
	struct bh *pbuff = buffs;

/* the q-register headers point to the start of the buffer and registers */
	struct qh qreg[NQREGS+9+NEXTQREGS];		/* for q regs, command, search, files, sys-command, time/date, extended */

/* the q-register stack contains temporary copies of q-register contents */
	struct qh qstack[QSTACKSIZE];	/* q-reg stack */
	struct qh *qsp;					/* q-reg stack pointer */

/* the macro stack contains pointers to the currently active macros. */
/* the top of the stack is the command pointer */
	struct qp mstack[MSTACKSIZE];	/* macro stack */
	struct qp *msp;					/* macro stack pointer */

/* the expression stack */
	struct exp_entry estack[ESTACKSIZE];	/* expression stack */
	struct exp_entry *esp;					/* expression stack pointer */

/* global variables, etc. */

short char_count = 0;				/* char count for tab typer						*/
unsigned char lastc = ' ';			/* last char read								*/
short ttyerr;						/* error return from ioctl						*/
jmp_buf xxx;						/* preserved environment for error restart		*/
short err;							/* local error code								*/
struct qp t_qp;						/* temporary buffer pointer						*/
struct qp aa, bb, cc;				/* more temporaries								*/
struct buffcell t_bcell;			/* temporary bcell								*/
short tabmask = 7;					/* mask for typing tabs							*/
unsigned char char_mask = 0x7f;		/* 7- or 8-bit char mask */
short exitflag;						/* flag for ending command str exec				*/
unsigned char term_char = ESC;		/* terminator for insert, search, etc.  		*/
unsigned char cmdc;					/* current command character					*/
unsigned char skipc;				/* char found by "skipto()"						*/
long dot, z, tdot;					/* current, last, temp buffer position  		*/
long ins_count;						/* count of chars inserted						*/
long ll, mm, nn;					/* general temps								*/
long ctrl_r = 10;					/* numeric radix (8, 10, 16)					*/
long ctrl_s = 0;					/* string length for S, I, G					*/
long ctrl_x = 0;					/* search mode flag								*/
long ed_val = 0;					/* ED value										*/
long es_val = 0;					/* ES value										*/
long et_val = 518;					/* ET value										*/
long eu_val = -1;					/* EU value										*/
long ev_val = 0;					/* EV value										*/
long ez_val = 0;					/* EZ value										*/
short srch_result = 0;				/* result of last :S executed					*/
short atflag = 0;					/* flag for @ char typed						*/
short colonflag = 0;				/* flag for : char typed						*/
short trace_sw = 0;					/* nonzero if tracing command exec  			*/

BUFFPTR insert_p;					/* pointer to temp buffer segment during insert */
short search_flag;					/* set nonzero by search						*/
/* character mapping table (direct) */
unsigned char mapch[] =
{
 0,	  1,   2,	3,	 4,	  5,   6,	7,	 8,	  9,  10,  11,  12,  13,  14,  15,		/* ^@ - ^M */
16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,		/* ^N - ^_ */
' ', '!', '"', '#', '$', '%', '&', '\'','(', ')', '*', '+', ',', '-', '.', '/',		/* sp - /  */
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',		/* 0 - ?   */
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',		/* @ - O   */
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\',']', '^', '_',		/* P - _   */
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',		/* ` - o   */
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|','}', '~', 0177,		/* p - del */
128,	129,	130,	131,	132,	133,	134,	135,
136,	137,	138,	139,	140,	141,	142,	143,
144,	145,	146,	147,	148,	149,	150,	151,
152,	153,	154,	155,	156,	157,	158,	159,
160,	161,	162,	163,	164,	165,	166,	167,
168,	169,	170,	171,	172,	173,	174,	175,
176,	177,	178,	179,	180,	181,	182,	183,
184,	185,	186,	187,	188,	189,	190,	191,
192,	193,	194,	195,	196,	197,	198,	199,
200,	201,	202,	203,	204,	205,	206,	207,
208,	209,	210,	211,	212,	213,	214,	215,
216,	217,	218,	219,	220,	221,	222,	223,
224,	225,	226,	227,	228,	229,	230,	231,
232,	233,	234,	235,	236,	237,	238,	239,
240,	241,	242,	243,	244,	245,	246,	247,
248,	249,	250,	251,	252,	253,	254,	255
} ;

/* character table (mapped to lower case) */
unsigned char mapch_l[] =
{
 0,	  1,   2,	3,	 4,	  5,   6,	7,	 8,	  9,  10,  11,  12,  13,  14,  15,		/* ^@ - ^M */
16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,		/* ^N - ^_ */
' ', '!', '"', '#', '$', '%', '&', '\'','(', ')', '*', '+', ',', '-', '.', '/',		/* sp - /  */
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',		/* 0 - ?   */
'@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',		/* @ - O   */
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[', '\\',']', '^', '_',		/* P - _   */
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',		/* ` - o   */
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|','}', '~', 0177,		/* p - del */
135,	129,	130,	131,	132,	133,	134,	135,
136,	137,	138,	139,	140,	141,	132,	134,
130,	145,	145,	147,	148,	149,	150,	151,
152,	148,	129,	155,	156,	157,	158,	159,
160,	161,	162,	163,	164,	164,	166,	167,
168,	169,	170,	171,	172,	173,	174,	175,
176,	177,	178,	179,	180,	181,	182,	183,
184,	185,	186,	187,	188,	189,	190,	191,
192,	193,	194,	195,	196,	197,	198,	199,
200,	201,	202,	203,	204,	205,	206,	207,
208,	209,	210,	211,	212,	213,	214,	215,
216,	217,	218,	219,	220,	221,	222,	223,
224,	225,	226,	227,	228,	229,	230,	231,
232,	233,	234,	235,	236,	237,	238,	239,
240,	241,	242,	243,	244,	245,	246,	247,
248,	249,	250,	251,	252,	253,	254,	255
} ;
/* table of special characters for "search," "skipto()," and "lines()" */
/* see "te_defs.h for meaning of bits */

unsigned char spec_chars[] =
{
0,			A_S,		0,			0,				/* ^@ ^A ^B ^C */
0,			A_A,		0,			0,				/* ^D ^E ^F ^G */
0,			A_T,		A_L,		A_L,			/* ^H ^I ^J ^K */
A_L,		0,			A_A,		0,				/* ^L ^M ^N ^O */
0,			A_A,		A_A,		A_A,			/* ^P ^Q ^R ^S */
0,			A_T|A_Q,	0,			0,				/* ^T ^U ^V ^W */
A_A,		0,			0,			0,				/* ^X ^Y ^Z ^[ */
0,			0,			A_S,		0,				/* ^\ ^] ^^ ^_ */
0,			A_S,		A_X,		0,				/*	  !  "  #  */
0,			0,			0,			A_X,			/* $  %  &  '  */
0,			0,			0,			0,				/* (  )  *  +  */
0,			0,			0,			0,				/* ,  -  .  /  */
0,			0,			0,			0,				/* 0  1  2  3  */
0,			0,			0,			0,				/* 4  5  6  7  */
0,			0,			0,			0,				/* 8  9  :  ;  */
A_X,		0,			A_X,		0,				/* <  =  >  ?  */
A_S,		0,			A_E|A_F,	0,				/* @  A  B  C  */
0,			A_S,		A_S,		A_Q,			/* D  E  F  G  */
0,			A_T|A_E,	0,			0,				/* H  I  J  K  */
0,			A_Q,		A_T|A_F,	A_T,			/* L  M  N  O  */
0,			A_Q,		A_E,		A_T|A_F,		/* P  Q  R  S  */
0,			A_Q,		0,			A_E,			/* T  U  V  W  */
A_Q,		0,			0,			A_Q,			/* X  Y  Z  [  */
0,			A_Q,		A_S,		A_T|A_E,		/* \  ]  ^  _  */
0,			0,			A_E|A_F,	0,				/* `  a  b  c  */
0,			A_S,		A_S,		A_Q,			/* d  e  f  g  */
0,			A_T|A_E,	0,			0,				/* h  i  j  k  */
0,			A_Q,		A_T|A_F,	A_T,			/* l  m  n  o  */
0,			A_Q,		A_E,		A_T|A_F,		/* p  q  r  s  */
0,			A_Q,		0,			A_E,			/* t  u  v  w  */
A_Q,		0,			0,			0,				/* x  y  z  {  */
A_X,		0,			0,			0,				/* |  }  ~  del */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,		/* chars with 8th bit set */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
} ;

struct buffcell id_cell =
{
	(BUFFPTR) 0, (BUFFPTR) &qreg[1], 0,
	"DOS TECO 1.50  03/10/93  Copyright (c) 1993 Matt Fichtenbaum\r\n"
};
