
# line 2 "print.y"
typedef union  {
	int integer;
	char *caddr;
	float fl;
	struct type *type;
	struct var *var;
	struct array *array;
	struct pat *pat;
	int (*function)();
} YYSTYPE;

# line 14 "print.y"
#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <signal.h>
#include "symtab.h"
	extern	char *strcpy();
	extern	struct type *st_fetch();
	extern	struct type *st_enter();
	extern	struct var *sv_fetch();
	extern	struct var *sv_enter();
	extern	struct array *makearray();
	extern	struct var *makevar();
	extern	struct pat *makepa();
	extern	char *malloc();
	extern	char *gcalloc();
	extern	struct member *ismember();
	extern	char *gcsavestr();
	/*
	 * commands
	 */
	extern	int doprint();
	extern	int whatis();
	extern	int delete();
	extern	int list();

	extern	char *progname;
	extern	char *yy_instr;
	extern	int linenumber;

	static	struct	var *vp;
	static	struct	type *tp;
	static	struct	member *mp;
	static	struct	pat *pp;
	static	struct	array *ap;
	static	struct	str str;
	static	struct	member mlist[80];	/* temporary member list */
	static	char	pathname[120];
	static	char	refname[120];
	static	char	buf[80];
	static	char	*pathp = &pathname[1];
	static	int	mindex = 0;
	static	YYSTYPE	data[100];
	static	int	dindex = 0;
	static	int	mode;
	static	u_long	curoff = 0;
	static	int	arg1;
	static	int	arg2;
	static	int	arg3;
	static	struct	array ar;		/* temporary array def */
# define TYPE 257
# define PRINT 258
# define WHATIS 259
# define DELETE 260
# define LIST 261
# define PTR 262
# define ARROW 263
# define IDENT 264
# define NUM 265
#define yyclearin yychar = -1
#define yyerrok yyerrflag = 0
extern int yychar;
extern short yyerrflag;
#ifndef YYMAXDEPTH
#define YYMAXDEPTH 150
#endif
YYSTYPE yylval, yyval;
# define YYERRCODE 256

# line 402 "print.y"


/*
 * this is the entry point to this whole mess
 */
#include <setjmp.h>
jmp_buf	onerror;

static int
catch() {
	fprintf(stderr, "Interrupt\n");
	longjmp(onerror, 1);
}

int
parseprint(p)
	char	*p;
{
	int	i;
	int	val;
	static	int init = 0;

	yy_instr = p;
	if (init == 0) {
		parseinit();
		init++;
	}
	val = 1;
	linenumber = 1;
	signal(SIGINT, catch);
	if (setjmp(onerror)) {
		signal(SIGINT, SIG_IGN);
		val = 1;
	} else if (yyparse() != 0)
		val = 0;
	reopen();	/* fix stdout if needed */
	gc();
	return val;
}

static
parseinit() {
	struct	type *tp;

	namelist(progname);
	tp = st_enter("float", 0);
	tp->t_fmt = " %5.2f ";
	tp->t_size = sizeof(float);
	tp = st_enter("int", 0);
	tp->t_fmt = " %d ";
	tp->t_size = sizeof(int);
	tp = st_enter("char", 0);
	tp->t_fmt = " %c ";
	tp->t_size = sizeof(char);
	tp = st_enter("string", 0);
	tp->t_fmt = " %s ";
	tp->t_size = sizeof(char *);
	tp = st_enter("ptr", 0);
	tp->t_fmt = "(nil)";
	tp->t_size = sizeof(char *);
}

int
yyerror(p, n)
	char	*p;
	int	n;
{
	extern	int errno;
	extern	char *sys_errlist[];
	extern	int sys_nerr;
	char	buf[255];

	sprintf(buf, p, n);
	if (errno < sys_nerr && errno > 0)
		fprintf(stderr, "print: %s (%s)\n", buf, sys_errlist[errno]);
	else
		fprintf(stderr, "print: %s near line %d\n", buf, linenumber);
	errno = 0;
	longjmp(onerror, 1);
}
short yyexca[] ={
-1, 1,
	0, -1,
	-2, 0,
-1, 23,
	264, 60,
	-2, 61,
-1, 89,
	91, 71,
	-2, 73,
	};
# define YYNPROD 81
# define YYLAST 242
short yyact[]={

  20,  13,  14,  15,  16,  24,  71,  23,  55, 118,
  82,  70,  60,  20, 125,  51,  96,  24,  24,  31,
  31,  44,  51,  86,  51, 122, 121,  89, 106,  53,
  47,  46,  45,  36,  34,  77,  74,  56,  41, 109,
  27,  28,  28,  72,  39,  67,  22, 112,  21,  91,
  99,  42,  44,  64,  88,  76,  27,  66,  65,  32,
 127, 120,  84,  83,  52,  38,  37,  79,  94,  92,
  94,  93,  50,  95, 117,  95,  62,  42,  40,  25,
  12,  26,  11,  43, 111,  73,  75,  87,  30,  63,
  98,  18,  17,  85,  33,  35,  44,  58,   6,   5,
   2,  54,  57,  29,  29,   1,  49,  48,   3,  10,
   9,   8, 102,   7,   4,  19,  78,   0,  61, 101,
 108, 100,   0,  90, 105,   0, 104,   0,  80,  81,
  97,   0, 103,   0,   0,   0,   0,   0,   0,   0,
 115, 116, 110, 123, 124, 101,  24, 100,  31, 119,
 113, 114,   0,   0,  59, 107, 126,   0,   0, 129,
 128, 130,   0,   0,   0,   0,   0, 131,   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,  77,   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,
  68,  69,  53,   0,   0,   0,   0,   0,   0,  53,
   0,  53 };
short yypact[]={

-1000,-1000,-257,-1000, -20,-244,-1000,-1000,-1000,-1000,
-1000,-1000,  -2,-230,-230,-231,-1000,   7,   6,   5,
-232,-233,-234,-1000,-1000, -21,  -4, -32, -25, -32,
-1000,-1000,-111,-1000,-1000,-1000,-1000,-1000,-1000,  31,
  30,-1000,-1000, -38, -34, -80, -39, -70,-1000,-1000,
-1000,-235,  20,-1000,-1000, -32, -23,-1000,   4,   3,
-242,  30,-1000,-237, -34, -44,  26,-1000,-1000,-1000,
-249, -34,-245,-1000,-1000,-228,-228,-245,  20,-236,
-1000,-1000, -32,-1000,-1000,  -5,-1000,-237,-1000,-1000,
 -46,-1000, -34, -34, -34, -34,-1000,  33,-116,   2,
-238,-239,-1000,-1000, -39, -39,-1000,-1000,-1000,-251,
-1000, -39,-1000,  28,  28,-1000,-1000,-1000,-1000,   1,
-1000, -39, -70,-1000,-1000,-1000,-1000,-1000,-1000,-1000,
-228,-1000 };
short yypgo[]={

   0,  48,  46,  38, 115,  80,  36,  55,  58,  57,
  45,  79,  81,  72, 114, 113, 111, 110, 109, 108,
 105, 100,  99,  98,  64,  97,  93,  82,  92,  91,
  90,  50,  44,  78,  89,  54,  87,  84,  83 };
short yyr1[]={

   0,  20,  20,  20,  19,  19,  19,  19,  19,  12,
  12,  12,  12,  12,  11,  14,  14,  14,  14,  21,
  15,  16,  17,  18,  13,  13,  24,  24,  23,  23,
  25,  26,  26,  22,  22,  27,  27,   2,  29,  29,
  29,  29,   6,   6,   7,   7,   7,  28,  30,  30,
  31,  31,  31,  31,   8,   8,   8,   9,   9,   9,
   1,   4,   5,   5,   5,   5,  32,  34,  33,  36,
  33,  37,  35,  35,  10,  10,  10,  10,   3,  38,
  38 };
short yyr2[]={

   0,   2,   2,   1,   1,   2,   2,   3,   3,   2,
   3,   3,   4,   2,   2,   2,   2,   2,   2,   0,
   2,   2,   2,   1,   2,   1,   3,   1,   4,   4,
   3,   1,   3,   1,   2,   2,   2,   1,   2,   3,
   3,   4,   1,   2,   2,   3,   3,   5,   2,   3,
   2,   3,   3,   4,   3,   3,   1,   3,   3,   1,
   1,   1,   1,   2,   3,   2,   1,   0,   3,   0,
   4,   0,   3,   1,   1,   1,   2,   3,   1,   3,
   4 };
short yychk[]={

-1000, -20, -21, -19, -14, -22, -23, -15, -16, -17,
 -18, -27,  -5, 258, 259, 260, 261, -28, -29,  -4,
 257,  -1,  -2, 264, 262, -11, -12,  60,  62, 124,
 -27, 264,  61,  -5, 264,  -5, 264,  59,  59, -32,
 -33,  -3,  46, -38,  91, 264, 264, 264, -12, -11,
 -13,  47, -24, 264, -13,  33,  62, -13, -25, 265,
 123, -33,  46, -34,  91,  -8,  -9, -10, 264, 265,
  45,  40, 123,  -3,  -6,  -3,  -7, 263, -24,  47,
 -13, -13,  33,  59,  59, -26, 265, -36, -35, 264,
  -8,  93,  43,  45,  42,  47, 265,  -8, -30, -31,
  -1,  -2,  -6,  -7,  -1,  -2, 264, -13, 125,  44,
 -35, -37,  93,  -9,  -9, -10, -10,  41, 125, -31,
  59, 264, 264,  -3,  -3, 265, -32,  59,  -3,  -6,
  -3,  -6 };
short yydef[]={

  19,  -2,   0,   3,   4,   1,   2,  15,  16,  17,
  18,  33,   0,   0,   0,   0,  23,   0,   0,  62,
   0,   0,   0,  -2,  37,   5,   6,   0,   0,   0,
  34,  60,   0,  20,  61,  21,  22,  35,  36,  63,
  65,  66,  67,  78,   0,   0,  38,   0,   7,   8,
  14,   0,  25,  27,   9,   0,   0,  13,   0,   0,
   0,  64,  69,   0,   0,   0,  56,  59,  74,  75,
   0,   0,   0,  39,  40,   0,  42,   0,  24,   0,
  10,  11,   0,  28,  29,   0,  31,   0,  68,  -2,
   0,  79,   0,   0,   0,   0,  76,   0,   0,   0,
   0,   0,  41,  43,  44,   0,  26,  12,  30,   0,
  70,   0,  80,  54,  55,  57,  58,  77,  47,   0,
  48,  50,   0,  45,  46,  32,  72,  49,  51,  52,
   0,  53 };
#ifndef lint
static char yaccpar_sccsid[] = "@(#)yaccpar	4.1	(Berkeley)	2/11/83";
#endif not lint

#
# define YYFLAG -1000
# define YYERROR goto yyerrlab
# define YYACCEPT return(0)
# define YYABORT return(1)

/*	parser for yacc output	*/

#ifdef YYDEBUG
int yydebug = 0; /* 1 for debugging */
#endif
YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
int yychar = -1; /* current input token number */
int yynerrs = 0;  /* number of errors */
short yyerrflag = 0;  /* error recovery flag */

yyparse() {

	short yys[YYMAXDEPTH];
	short yyj, yym;
	register YYSTYPE *yypvt;
	register short yystate, *yyps, yyn;
	register YYSTYPE *yypv;
	register short *yyxi;

	yystate = 0;
	yychar = -1;
	yynerrs = 0;
	yyerrflag = 0;
	yyps= &yys[-1];
	yypv= &yyv[-1];

 yystack:    /* put a state and value onto the stack */

#ifdef YYDEBUG
	if( yydebug  ) printf( "state %d, char 0%o\n", yystate, yychar );
#endif
		if( ++yyps> &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); }
		*yyps = yystate;
		++yypv;
		*yypv = yyval;

 yynewstate:

	yyn = yypact[yystate];

	if( yyn<= YYFLAG ) goto yydefault; /* simple state */

	if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
	if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;

	if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
		yychar = -1;
		yyval = yylval;
		yystate = yyn;
		if( yyerrflag > 0 ) --yyerrflag;
		goto yystack;
		}

 yydefault:
	/* default state action */

	if( (yyn=yydef[yystate]) == -2 ) {
		if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
		/* look through exception table */

		for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */

		while( *(yyxi+=2) >= 0 ){
			if( *yyxi == yychar ) break;
			}
		if( (yyn = yyxi[1]) < 0 ) return(0);   /* accept */
		}

	if( yyn == 0 ){ /* error */
		/* error ... attempt to resume parsing */

		switch( yyerrflag ){

		case 0:   /* brand new error */

			yyerror( "syntax error" );
		yyerrlab:
			++yynerrs;

		case 1:
		case 2: /* incompletely recovered error ... try again */

			yyerrflag = 3;

			/* find a state where "error" is a legal shift action */

			while ( yyps >= yys ) {
			   yyn = yypact[*yyps] + YYERRCODE;
			   if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
			      yystate = yyact[yyn];  /* simulate a shift of "error" */
			      goto yystack;
			      }
			   yyn = yypact[*yyps];

			   /* the current yyps has no shift onn "error", pop stack */

#ifdef YYDEBUG
			   if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
#endif
			   --yyps;
			   --yypv;
			   }

			/* there is no state on the stack with an error shift ... abort */

	yyabort:
			return(1);


		case 3:  /* no shift yet; clobber input char */

#ifdef YYDEBUG
			if( yydebug ) printf( "error recovery discards char %d\n", yychar );
#endif

			if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
			yychar = -1;
			goto yynewstate;   /* try again in the same state */

			}

		}

	/* reduction by production yyn */

#ifdef YYDEBUG
		if( yydebug ) printf("reduce %d\n",yyn);
#endif
		yyps -= yyr2[yyn];
		yypvt = yypv;
		yypv -= yyr2[yyn];
		yyval = yypv[1];
		yym=yyn;
			/* consult goto table to find next state */
		yyn = yyr1[yyn];
		yyj = yypgo[yyn] + *yyps + 1;
		if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
		switch(yym){
			
case 3:
# line 122 "print.y"
{ yypvt[-0].function(arg1, arg2, arg3); } break;
case 9:
# line 135 "print.y"
{ redir_out(yypvt[-0].caddr, O_WRONLY|O_EXCL|O_CREAT); } break;
case 10:
# line 137 "print.y"
{ redir_out(yypvt[-0].caddr, O_WRONLY|O_TRUNC|O_CREAT); } break;
case 11:
# line 139 "print.y"
{ redir_out(yypvt[-0].caddr, O_WRONLY|O_APPEND); } break;
case 12:
# line 141 "print.y"
{ redir_out(yypvt[-0].caddr, O_WRONLY|O_CREAT|O_APPEND); } break;
case 13:
# line 143 "print.y"
{ dopipe(yypvt[-0].caddr); } break;
case 14:
# line 148 "print.y"
{ redir_in(yypvt[-0].caddr); } break;
case 15:
# line 153 "print.y"
{ yyval.function = yypvt[-0].function; } break;
case 16:
# line 155 "print.y"
{ yyval.function = yypvt[-0].function; } break;
case 17:
# line 157 "print.y"
{ yyval.function = yypvt[-0].function; } break;
case 18:
# line 159 "print.y"
{ yyval.function = yypvt[-0].function; } break;
case 19:
# line 163 "print.y"
{ bzero(&str, sizeof(str));
	*pathp = 0, *pathname = 0, ar.a_nelem = 0;
	mindex = 0; tp = 0, dindex = 0;
	bzero(&ar, sizeof(ar));
	arg1 = 0, arg2 = 0, arg3 = 0, *refname = 0; } break;
case 20:
# line 175 "print.y"
{ yyval.function = doprint;
		arg1 = (int) yypvt[-0].var; } break;
case 21:
# line 181 "print.y"
{ yyval.function = whatis;
		arg1 = (int) yypvt[-0].var; } break;
case 22:
# line 187 "print.y"
{ yyval.function = delete;
		arg1 = (int) yypvt[-0].caddr; } break;
case 23:
# line 193 "print.y"
{ yyval.function = list; } break;
case 24:
# line 198 "print.y"
{ *pathname = '/';
		yyval.caddr = pathname; } break;
case 25:
# line 201 "print.y"
{ yyval.caddr = pathp; } break;
case 26:
# line 206 "print.y"
{ strcat(pathp, "/");
		strcat(pathp, yypvt[-0].caddr); } break;
case 27:
# line 209 "print.y"
{ strcpy(pathp, yypvt[-0].caddr); } break;
case 28:
# line 214 "print.y"
{ setvar(yypvt[-3].var, data, dindex); } break;
case 29:
# line 216 "print.y"
{ data[0].integer = yypvt[-1].integer;
		setvar(yypvt[-3].var, data, 1); } break;
case 31:
# line 226 "print.y"
{ data[dindex++].integer = yypvt[-0].integer; } break;
case 32:
# line 228 "print.y"
{ data[dindex++].integer = yypvt[-0].integer; } break;
case 37:
# line 243 "print.y"
{ yyval.type = st_fetch("ptr"); } break;
case 38:
# line 249 "print.y"
{ vp = sv_enter(yypvt[-0].caddr, yypvt[-1].type, 0, 0); } break;
case 39:
# line 251 "print.y"
{ vp = sv_enter(yypvt[-1].caddr, yypvt[-2].type, yypvt[-0].array, 0); } break;
case 40:
# line 253 "print.y"
{ sv_enter(yypvt[-1].caddr, yypvt[-2].type, 0, yypvt[-0].pat); } break;
case 41:
# line 255 "print.y"
{ sv_enter(yypvt[-2].caddr, yypvt[-3].type, yypvt[-1].array, yypvt[-0].pat); } break;
case 42:
# line 260 "print.y"
{ yyval.pat = yypvt[-0].pat; } break;
case 43:
# line 262 "print.y"
{ yypvt[-1].pat->p_pat = yypvt[-0].pat;
		yyval.pat = yypvt[-1].pat; } break;
case 44:
# line 268 "print.y"
{ yyval.pat = makepa(yypvt[-0].type, 0, 0, malloc); } break;
case 45:
# line 270 "print.y"
{ yyval.pat = makepa(yypvt[-1].type, yypvt[-0].array, 0, malloc); } break;
case 46:
# line 272 "print.y"
{ yyval.pat = makepa(yypvt[-1].type, yypvt[-0].array, 0, malloc); } break;
case 47:
# line 277 "print.y"
{ str.s_members = mindex;
		str.s_size = curoff;
		str.s_mlist = mlist;
		tp = st_enter(yypvt[-3].caddr, &str);
		mindex = 0, curoff = 0, bzero(&str, sizeof(str));} break;
case 50:
# line 292 "print.y"
{ makemlist(&mlist[mindex++], yypvt[-0].caddr, yypvt[-1].type, 0, 0, &curoff); } break;
case 51:
# line 294 "print.y"
{ makemlist(&mlist[mindex++], yypvt[-1].caddr, yypvt[-2].type, yypvt[-0].array, 0, &curoff); } break;
case 52:
# line 296 "print.y"
{ makemlist(&mlist[mindex++], yypvt[-1].caddr, yypvt[-2].type, 0, yypvt[-0].pat, &curoff); } break;
case 53:
# line 298 "print.y"
{ makemlist(&mlist[mindex++], yypvt[-2].caddr, yypvt[-3].type, yypvt[-1].array, yypvt[-0].pat, &curoff); } break;
case 54:
# line 303 "print.y"
{ yyval.integer = yypvt[-2].integer + yypvt[-0].integer; } break;
case 55:
# line 305 "print.y"
{ yyval.integer = yypvt[-2].integer - yypvt[-0].integer; } break;
case 56:
# line 307 "print.y"
{ yyval.integer = yypvt[-0].integer; } break;
case 57:
# line 312 "print.y"
{ yyval.integer = yypvt[-2].integer * yypvt[-0].integer; } break;
case 58:
# line 314 "print.y"
{ yyval.integer = yypvt[-2].integer / yypvt[-0].integer; } break;
case 59:
# line 316 "print.y"
{ yyval.integer = yypvt[-0].integer; } break;
case 60:
# line 321 "print.y"
{ if ((yyval.type = st_fetch(yypvt[-0].caddr)) == 0)
			yyerror("non-existent type %s", yypvt[-0].caddr); } break;
case 61:
# line 327 "print.y"
{ if ((vp = sv_fetch(yypvt[-0].caddr)) == 0)
			yyerror("non-existent variable %s", yypvt[-0].caddr);
	  	yyval.var = vp;
		curoff = vp->v_addr;
		strcpy(refname, vp->v_name);
		ap = makearray(vp->v_array, gcalloc);
		tp = vp->v_type;
		pp = vp->v_pat; } break;
case 62:
# line 339 "print.y"
{ yyval.var = yypvt[-0].var; } break;
case 63:
# line 341 "print.y"
{ yyval.var = makevar(tp, ap, refname, pp, curoff); } break;
case 64:
# line 343 "print.y"
{ yyval.var = makevar(tp, ap, refname, pp, curoff); } break;
case 65:
# line 345 "print.y"
{ yyval.var = makevar(tp, ap, refname, pp, curoff); } break;
case 66:
# line 350 "print.y"
{ arrayadd(tp, ap, yypvt[-0].array, pp, &curoff); ap = yypvt[-0].array; } break;
case 67:
# line 354 "print.y"
{ strcat(refname, "."); } break;
case 69:
# line 355 "print.y"
{ strcat(refname, "."); } break;
case 71:
# line 359 "print.y"
{ strcat(refname, yypvt[-0].caddr);
		if ((mp = ismember(tp, yypvt[-0].caddr)) == 0)
			yyerror("no such member %s", yypvt[-0].caddr);
		tp = mp->m_type;
		ap = makearray(mp->m_array, gcalloc);
		pp = mp->m_pat; } break;
case 73:
# line 367 "print.y"
{ strcat(refname, yypvt[-0].caddr);
		if ((mp = ismember(tp, yypvt[-0].caddr)) == 0)
			yyerror("no such member %s", yypvt[-0].caddr);
		tp = mp->m_type;
		ap = makearray(mp->m_array, gcalloc);
		pp = mp->m_pat;
		curoff += mp->m_offs; } break;
case 74:
# line 378 "print.y"
{ yyval.integer = simple_eval(yypvt[-0].caddr); } break;
case 75:
# line 380 "print.y"
{ yyval.integer = yypvt[-0].integer; } break;
case 76:
# line 382 "print.y"
{ yyval.integer = -yypvt[-0].integer; } break;
case 77:
# line 384 "print.y"
{ yyval.integer = yypvt[-1].integer; } break;
case 78:
# line 389 "print.y"
{ yyval.array = makearray(&ar, gcalloc);
		ar.a_nelem = 0, ar.a_size = 0; } break;
case 79:
# line 395 "print.y"
{ ar.a_bound[ar.a_nelem++] = yypvt[-1].integer;
		strcat(refname, sprintf(buf, "[%d]", yypvt[-1].integer)); } break;
case 80:
# line 398 "print.y"
{ ar.a_bound[ar.a_nelem++] = yypvt[-1].integer;
		strcat(refname, sprintf(buf, "[%d]", yypvt[-1].integer)); } break;
		}
		goto yystack;  /* stack new state and value */

	}
