# usage:  awk -f mainpars.awk mainpars.c
# where mainpars.c is produced from mainpars.y by yacc (see Makefile)
# This produces two files, macMainpars.r and macMainpars.c
# These are used in making MacAnova under MPW on the Macintosh.
# macMainpars.r is used by rez in MPW on the Macintosh to create
# resources consisting of the various tables produced by yacc
# macMainpars.c is mainpars.c without the tables, and with a number of
# redundent cases deleted
BEGIN	{
			rFile = "macMainpars.r"
			cFile = "macMainpars.c"
			ndelete = 0
			for(i=1;i<=8;i++){
				code[i] = 1000+i
			}
			name[code[1]] = "yyexca"
			name[code[2]] = "yyact"
			name[code[3]] = "yypact"
			name[code[4]] = "yypgo"
			name[code[5]] = "yyr1"
			name[code[6]] = "yyr2"
			name[code[7]] = "yychk"
			name[code[8]] = "yydef"
			id[name[code[1]]] = "YYEXCA"
			id[name[code[2]]] = "YYACT"
			id[name[code[3]]] = "YYPACT"
			id[name[code[4]]] = "YYPGO"
			id[name[code[5]]] = "YYR1"
			id[name[code[6]]] = "YYR2"
			id[name[code[7]]] = "YYCHK"
			id[name[code[8]]] = "YYDEF"
		}
/^\*\(C\)\*/	{
				if(!copyright) 	print "/*" > rFile
				copyright = 1
			}
copyright == 1	{
					print > rFile
					if($0 ~ /^[ 	]*\*\//) copyright = 0
				}
/^VERSION:/	{
				version = $2
				print "/* resource version of mainpar2.c */" > rFile
				print "/*" > rFile
				printf "  Tables for Macintosh version of mainpars.c dated %s\n", version > rFile
				print "*/" > rFile
				print "" > rFile
				print "type 'YTAB'  {" > rFile
				print "	wide  array { integer; };" > rFile
				print "};" > rFile
				print "" > rFile
				for(i=1;i<=8;i++){
					printf "#define %-6s %4d\n", id[name[code[i]]], code[i] > rFile
				}
				print "" > rFile
				print "/*" > rFile
				print "	each resource must be of the form " > rFile
				print "resource 'YTAB' (YYxxxx)" > rFile
				print "{" > rFile
				print "	{" > rFile
				print "	    n1, n2, ..." > rFile
				print "	};" > rFile
				print "};" > rFile
				print " */" > rFile
				print "" > rFile
				print "" > rFile
			}
/^short yyexca/  {which = "yyexca";process = 1; next}
/^short yyact/  {which = "yyact";process = 1; next}
/^short yypact/  {which = "yypact";process = 1; next}
/^short yypgo/  {which = "yypgo";process = 1; next}
/^short yyr1/  {which = "yyr1";process = 1; next}
/^short yyr2/  {which = "yyr2";process = 1; next}
/^short yychk/  {which = "yychk";process = 1; next}
/^short yydef/  {which = "yydef";process = 1; next}
process == 1	{
					process = 2
					printf "/* Deleted definition of short %s[] */\n",which > cFile
					printf "resource 'YTAB' (%s,\"%s\")\n{\n\t{\n",id[which],which > rFile
				}
process == 2	{
					printf "\t\t%s\n", $0 > rFile
					if($0 ~ /\}/){
						printf "};\n\n" > rFile
						process = 0
					}
					next
				}
	
/\/\*[ 	]*Mac: *fold/	{
							for(i=1;i<=NF;i++){if($i == "fold")break}
							i++
							printf "\t\t/*Folding %d cases into 1*/\n",$i>cFile
							ndelete = $i - 1
						}
/case.*[0-9]:/	{
					if(ndelete > 0)ndelete--
					print > cFile
					next
				}
process == 0 && ndelete <= 0	{print > cFile}

END	{printf "%s and %s are the new files for Macintosh\n",cFile,rFile>"/dev/tty"}
