xref: /csrg-svn/old/boggle/boggle.c (revision 46734)
121499Smckusick /*
221499Smckusick  * Copyright (c) 1980 Regents of the University of California.
321499Smckusick  * All rights reserved.  The Berkeley software License Agreement
421499Smckusick  * specifies the terms and conditions for redistribution.
521499Smckusick  */
621499Smckusick 
79895Ssam #ifndef lint
821499Smckusick char copyright[] =
921499Smckusick "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1021499Smckusick  All rights reserved.\n";
1121499Smckusick #endif not lint
129895Ssam 
1321499Smckusick #ifndef lint
14*46734Sbostic static char sccsid[] = "@(#)boggle.c	5.5 (Berkeley) 02/27/91";
1521499Smckusick #endif not lint
1621499Smckusick 
179895Ssam #include <ctype.h>
189895Ssam #include <errno.h>
199895Ssam #include <setjmp.h>
209895Ssam #include <sgtty.h>
219895Ssam #include <signal.h>
229895Ssam #include <stdio.h>
2341169Sbostic #include "pathnames.h"
249895Ssam 
259895Ssam /* basic parameters */
269895Ssam #define N 4
279895Ssam #define SSIZE 200
289895Ssam #define MAXWORDS 1000
299895Ssam #define CWIDTH 10
309895Ssam #define LWIDTH 80
319895Ssam 
329895Ssam /* parameters defined in terms of above */
339895Ssam #define BSIZE (N*N)
349895Ssam #define row(x) (x/N)
359895Ssam #define col(x) (x%N)
369895Ssam 
379895Ssam /* word being searched for */
389895Ssam int wlength;
399895Ssam int numsame;
409895Ssam char wbuff [BSIZE+1];
419895Ssam 
429895Ssam /* tty and process control */
439895Ssam extern int errno;
449895Ssam int status;
459895Ssam int pipefd[2];
469895Ssam int super = 0;
479895Ssam int delct = 1;
489895Ssam int zero = 0;
499895Ssam int master = 1;
509895Ssam int column;
519895Ssam int *timept;
529895Ssam int timeint[] = {60,60,50,7,1,1,1,0};
539895Ssam long timein;
549895Ssam extern long int time();
559895Ssam struct sgttyb origttyb, tempttyb;
569895Ssam int ctlecho = 0;
579895Ssam int lctlech = LCTLECH;
589895Ssam jmp_buf env;
599895Ssam 
609895Ssam /* monitoring variables */
619895Ssam int games;
629895Ssam int logfile = -1;
639895Ssam long logloc;
649895Ssam char logbuff[100] = {"inst\t"};
659895Ssam extern char *ctime(), *getlogin();
669895Ssam extern long lseek();
679895Ssam 
689895Ssam /* dictionary interface */
699895Ssam FILE *dict;
709895Ssam 
719895Ssam /* structures for doing matching */
729895Ssam struct frame {
739895Ssam 	struct frame *parent;
749895Ssam 	int place;
759895Ssam };
769895Ssam struct frame stack [SSIZE];
779895Ssam struct frame *level [BSIZE+1];
789895Ssam 
799895Ssam /* the board and subsidiary structures */
809895Ssam char present [BSIZE+1];
819895Ssam char board [BSIZE];
829895Ssam char olink [BSIZE];
839895Ssam char adj [BSIZE+1][BSIZE];
849895Ssam char occurs [26];
859895Ssam 
869895Ssam /* the boggle cubes */
879895Ssam char *cube [BSIZE] = {
889895Ssam 	"forixb", "moqabj", "gurilw", "setupl",
899895Ssam 	"cmpdae", "acitao", "slcrae", "romash",
909895Ssam 	"nodesw", "hefiye", "onudtk", "tevign",
919895Ssam 	"anedvz", "pinesh", "abilyt", "gkyleu"
929895Ssam };
939895Ssam 
949895Ssam 
959895Ssam /* storage for words found */
969895Ssam int ubotch, ustart, wcount;
979895Ssam char *word [MAXWORDS];
989895Ssam char *freesp;
999895Ssam char space[10000];
1009895Ssam 
1019895Ssam endline ()
1029895Ssam {
1039895Ssam 	if (column != 0) {
1049895Ssam 		putchar('\n');
1059895Ssam 		column = 0;
1069895Ssam 	}
1079895Ssam }
1089895Ssam 
109*46734Sbostic void
1109895Ssam timeout ()
1119895Ssam {
1129895Ssam 	if (*timept > 0) {
1139895Ssam 		signal (SIGALRM, timeout);
1149895Ssam 		alarm(*timept++);
1159895Ssam 	}
1169895Ssam 	putchar('\007');
1179895Ssam }
1189895Ssam 
119*46734Sbostic void
1209895Ssam interrupt ()
1219895Ssam {
1229895Ssam 	signal(SIGINT, interrupt);
1239895Ssam 	if (delct++ >= 1)
1249895Ssam 		longjmp(env, 1);
1259895Ssam 	timept = &zero;
1269895Ssam }
1279895Ssam 
1289895Ssam goodbye (stat)
1299895Ssam int stat;
1309895Ssam {
1319895Ssam 	if (master != 0) {
1329895Ssam 		wait(&status);
1339895Ssam 		if ( ctlecho & LCTLECH ) {
1349895Ssam 			ioctl( fileno(stdin), TIOCLBIS, &lctlech );
1359895Ssam 		}
1369895Ssam 		stty(fileno(stdin), &origttyb);
1379895Ssam 	}
1389895Ssam 	exit(stat);
1399895Ssam }
1409895Ssam 
1419895Ssam clearscreen ()
1429895Ssam {
1439895Ssam 	stty (fileno(stdin), &tempttyb);
1449895Ssam 	printf("\n\033\f\r");
1459895Ssam }
1469895Ssam 
1479895Ssam compare (a, b)
1489895Ssam char **a, **b;
1499895Ssam {
1509895Ssam 	return(wordcomp(*a, *b));
1519895Ssam }
1529895Ssam 
1539895Ssam wordcomp (p, q)
1549895Ssam register char *p, *q;
1559895Ssam {
1569895Ssam 	if (*p=='0' && *q!='0')
1579895Ssam 		return(-1);
1589895Ssam 	if (*p!='0' && *q=='0')
1599895Ssam 		return(1);
1609895Ssam 	while (*++p == *++q && isalpha(*p)) ;
1619895Ssam 	if (!isalpha(*p))
1629895Ssam 		return(-isalpha(*q));
1639895Ssam 	if (!isalpha(*q))
1649895Ssam 		return(1);
1659895Ssam 	return(*p-*q);
1669895Ssam }
1679895Ssam 
1689895Ssam printinst ()
1699895Ssam {
1709895Ssam 	stty (fileno(stdin), &tempttyb);
1719895Ssam 	printf("instructions?");
1729895Ssam 	if (getchar() == 'y') {
1739895Ssam 		clearscreen();
1749895Ssam 		printf("     The object of Boggle (TM  Parker  Bros.)  is  to  find,  within  3\n");
1759895Ssam 		printf("minutes,  as many words as possible in a 4 by 4 grid of letters.  Words\n");
1769895Ssam 		printf("may be formed from any sequence of 3 or more adjacent  letters  in  the\n");
1779895Ssam 		printf("grid.   The  letters  may join horizontally, vertically, or diagonally.\n");
1789895Ssam 		printf("However, no position in the grid may be used more than once within  any\n");
1799895Ssam 		printf("one  word.   In  competitive  play amongst humans, each player is given\n");
1809895Ssam 		printf("credit for those of his words which no other player has found.\n");
1819895Ssam 		printf("     This program is intended  for  people  wishing  to  sharpen  their\n");
1829895Ssam 		printf("skills  at  Boggle.   If  you  invoke the program with 4 arguments of 4\n");
1839895Ssam 		printf("letters each, (e.g. \"boggle appl epie moth erhd\") the program forms the\n");
1849895Ssam 		printf("obvious  Boggle grid and lists all the words from /usr/dict/words found\n");
1859895Ssam 		printf("therein.  If you invoke the program without arguments, it will generate\n");
1869895Ssam 		printf("a  board  for you, let you enter words for 3 minutes, and then tell you\n");
1879895Ssam 		printf("how well you did relative to /usr/dict/words.\n");
1889895Ssam 		printf("     In interactive play, enter your words separated by  spaces,  tabs,\n");
1899895Ssam 		printf("or  newlines.   A  bell will ring when there is 2:00, 1:00, 0:10, 0:02,\n");
1909895Ssam 		printf("0:01, and 0:00 time left.  You may complete any word started before the\n");
1919895Ssam 		printf("expiration  of  time.   You  can surrender before time is up by hitting\n");
1929895Ssam 		printf("'break'.  While entering words, your erase character is only  effective\n");
1939895Ssam 		printf("within the current word and your line kill character is ignored.\n");
1949895Ssam 		printf("     Advanced players may wish to invoke the program with 1 or 2 +'s as\n");
19520195Smckusick 		printf("the  first argument.  The first + removes the restriction that positions\n");
1969895Ssam 		printf("can only be used once in each word.  The second + causes a position  to\n");
1979895Ssam 		printf("be  considered  adjacent  to itself as well as its (up to) 8 neighbors.\n");
1989895Ssam 		printf("Hit any key to begin.\n");
1999895Ssam 		stty (fileno(stdin), &tempttyb);
2009895Ssam 		getchar();
2019895Ssam 	}
2029895Ssam 	stty (fileno(stdin), &tempttyb);
2039895Ssam }
2049895Ssam 
2059895Ssam setup ()
2069895Ssam {
2079895Ssam 	register int i, j;
2089895Ssam 	int rd, cd, k;
2099895Ssam 	for (i=0; i<BSIZE; i++) {
2109895Ssam 		adj[i][i] = super>=2 ? 1 : 0;
2119895Ssam 		adj[BSIZE][i] = 1;
2129895Ssam 		for (j=0; j<i; j++) {
2139895Ssam 			rd = row(i)-row(j);
2149895Ssam 			cd = col(i)-col(j);
2159895Ssam 			k = 0;
2169895Ssam 			switch (rd) {
2179895Ssam 			case -1:
2189895Ssam 			case 1:
2199895Ssam 				if (-1<=cd && cd<=1)
2209895Ssam 					k = 1;
2219895Ssam 				break;
2229895Ssam 			case 0:
2239895Ssam 				if (cd==-1 || cd==1)
2249895Ssam 					k = 1;
2259895Ssam 				break;
2269895Ssam 			}
2279895Ssam 			adj[i][j] = adj[j][i] = k;
2289895Ssam 		}
2299895Ssam 	}
2309895Ssam 	stack[0].parent = &stack[0];
2319895Ssam 	stack[0].place = BSIZE;
2329895Ssam 	level[0] = &stack[0];
2339895Ssam 	level[1] = &stack[1];
2349895Ssam }
2359895Ssam 
2369895Ssam makelists ()
2379895Ssam {
2389895Ssam 	register int i, c;
2399895Ssam 	for (i=0; i<26; i++)
2409895Ssam 		occurs[i] = BSIZE;
2419895Ssam 	for (i=0; i<BSIZE; i++) {
2429895Ssam 		c = board[i];
2439895Ssam 		olink[i] = occurs[c-'a'];
2449895Ssam 		occurs[c-'a'] = i;
2459895Ssam 	}
2469895Ssam }
2479895Ssam 
2489895Ssam genboard ()
2499895Ssam {
2509895Ssam 	register int i, j;
2519895Ssam 	for (i=0; i<BSIZE; i++)
2529895Ssam 		board[i] = 0;
2539895Ssam 	for (i=0; i<BSIZE; i++) {
2549895Ssam 		j = rand()%BSIZE;
2559895Ssam 		while (board[j] != 0)
2569895Ssam 			j = (j+1)%BSIZE;
2579895Ssam 		board[j] = cube[i][rand()%6];
2589895Ssam 	}
2599895Ssam }
2609895Ssam 
2619895Ssam printboard ()
2629895Ssam {
2639895Ssam 	register int i, j;
2649895Ssam 	for (i=0; i<N; i++) {
2659895Ssam 		printf("\t\t\t\t\b\b");
2669895Ssam 		for (j=0; j<N; j++) {
2679895Ssam 			putchar ((putchar(board[i*N+j]) == 'q') ? 'u' : ' ');
2689895Ssam 			putchar(' ');
2699895Ssam 		}
2709895Ssam 		putchar('\n');
2719895Ssam 	}
2729895Ssam 	putchar('\n');
2739895Ssam }
2749895Ssam 
2759895Ssam getdword ()
2769895Ssam {
2779895Ssam 	/* input:  numsame = # chars same as last word   */
2789895Ssam 	/* output: numsame = # same chars for next word  */
2799895Ssam 	/*        word in wbuff[0]...wbuff[wlength-1]    */
2809895Ssam 	register int c;
2819895Ssam 	register char *p;
2829895Ssam 	if (numsame == EOF)
2839895Ssam 		return (0);
2849895Ssam 	p = &wbuff[numsame]+1;
2859895Ssam 	while ((*p++ = c = getc(dict)) != EOF && isalpha(c)) ;
2869895Ssam 	numsame = c;
2879895Ssam 	wlength = p - &wbuff[2];
2889895Ssam 	return (1);
2899895Ssam }
2909895Ssam 
2919895Ssam getuword ()
2929895Ssam {
2939895Ssam 	int c;
2949895Ssam 	register char *p, *q, *r;
2959895Ssam 	numsame = 0;
2969895Ssam 	while (*timept>0 && (isspace(c=getchar()) || c==EOF));
2979895Ssam 	if (*timept == 0)
2989895Ssam 		return(0);
2999895Ssam 	word[wcount++] = freesp;
3009895Ssam 	*freesp++ = '0';
3019895Ssam 	r = &wbuff[1];
3029895Ssam 	q = p = freesp;
3039895Ssam 	*p++ = c;
3049895Ssam 	while (!isspace(c = getchar())) {
3059895Ssam 		if (c == EOF)
3069895Ssam 			continue;
3079895Ssam 		if (c == origttyb.sg_erase) {
3089895Ssam 			if (p > q)
3099895Ssam 				p--;
3109895Ssam 			continue;
3119895Ssam 		}
3129895Ssam 		*p++ = c;
3139895Ssam 	}
3149895Ssam 	freesp = p;
3159895Ssam 	for (p=q; p<freesp && r<&wbuff[BSIZE]; )
3169895Ssam 		if (islower(c = *p++) && (*r++ = *q++ = c) == 'q' && *p == 'u')
3179895Ssam 			p++;
3189895Ssam 	*(freesp = q) = '0';
3199895Ssam 	wlength = r-&wbuff[1];
3209895Ssam 	return(1);
3219895Ssam }
3229895Ssam 
3239895Ssam aputuword (ways)
3249895Ssam int ways;
3259895Ssam {
3269895Ssam 	*word[wcount-1] = ways>=10 ? '*' : '0'+ways;
3279895Ssam }
3289895Ssam 
3299895Ssam aputword (ways)
3309895Ssam int ways;
3319895Ssam {
3329895Ssam 	/* store (wbuff, ways) in next slot in space */
3339895Ssam 	register int i;
3349895Ssam 	*freesp++ = ways>=10 ? '*' : '0'+ways;
3359895Ssam 	for (i=1; i<= wlength; i++)
3369895Ssam 		*freesp++ = wbuff[i];
3379895Ssam 	word[++wcount] = freesp;
3389895Ssam }
3399895Ssam 
3409895Ssam tputword (ways)
3419895Ssam int ways;
3429895Ssam {
3439895Ssam 	/* print (wbuff, ways) on terminal */
3449895Ssam 	wbuff[wlength+1] = '0';
3459895Ssam 	wbuff[0] = ways>=10 ? '*' : '0'+ways;
3469895Ssam 	outword(&wbuff[0]);
3479895Ssam }
3489895Ssam 
3499895Ssam outword (p)
3509895Ssam register char *p;
3519895Ssam {
3529895Ssam 	register int newcol;
3539895Ssam 	register char *q;
3549895Ssam 	for (q=p+1; isalpha(*q); ) {
3559895Ssam 		putchar(*q);
3569895Ssam 		if (*q++ == 'q') {
3579895Ssam 			putchar('u');
3589895Ssam 			column++;
3599895Ssam 		}
3609895Ssam 	}
3619895Ssam 	column += q-p-1;
3629895Ssam 	if (column > LWIDTH-CWIDTH) {
3639895Ssam 		putchar('\n');
3649895Ssam 		column = 0;
3659895Ssam 		return;
3669895Ssam 	}
3679895Ssam 	newcol = ((column+CWIDTH)/CWIDTH)*CWIDTH;
3689895Ssam 	while (((column+8)/8)*8 <= newcol) {
3699895Ssam 		putchar('\t');
3709895Ssam 		column = ((column+8)/8)*8;
3719895Ssam 	}
3729895Ssam 	while (column < newcol) {
3739895Ssam 		putchar(' ');
3749895Ssam 		column++;
3759895Ssam 	}
3769895Ssam }
3779895Ssam 
3789895Ssam printdiff ()
3799895Ssam {
3809895Ssam 	register int c, d, u;
3819895Ssam 	char both, donly, uonly;
3829895Ssam 	word[wcount] = freesp;
3839895Ssam 	*freesp = '0';
3849895Ssam 	both = donly = uonly = column = d = 0;
3859895Ssam 	u = ustart;
3869895Ssam 	while (d < ubotch) {
3879895Ssam 		c = u<wcount ? wordcomp (word[d], word[u]) : -1;
3889895Ssam 		if (c == 0) {
3899895Ssam 			/* dict and user found same word */
3909895Ssam 			if (both == 0) {
3919895Ssam 				both = 1;
3929895Ssam 				printf("\t\t\t   we both found:\n");
3939895Ssam 			}
3949895Ssam 			outword(word[d]);
3959895Ssam 			word[d++] = NULL;
3969895Ssam 			word[u++] = NULL;
3979895Ssam 		} else if (c < 0) {
3989895Ssam 			/* dict found it, user didn't */
3999895Ssam 			donly = 1;
4009895Ssam 			d++;
4019895Ssam 		} else {
4029895Ssam 			/* user found it, dict didn't */
4039895Ssam 			uonly = 1;
4049895Ssam 			u++;
4059895Ssam 		}
4069895Ssam 	}
4079895Ssam 	endline();
4089895Ssam 	if (donly) {
4099895Ssam 		printf("\n\t\t\tI alone found these:\n");
4109895Ssam 		for (d=0; d<ubotch; d++)
4119895Ssam 			if (word[d] != NULL)
4129895Ssam 				outword(word[d]);
4139895Ssam 		endline();
4149895Ssam 	}
4159895Ssam 	if (uonly) {
4169895Ssam 		printf("\n\t\t\tyou alone found these:\n");
4179895Ssam 		for (u=ustart; u<wcount; u++)
4189895Ssam 			if (word[u] != NULL)
4199895Ssam 				outword(word[u]);
4209895Ssam 		endline();
4219895Ssam 	}
4229895Ssam 	if (ubotch < ustart) {
4239895Ssam 		printf("\n\t\t\t  you botched these:\n");
4249895Ssam 		for (u=ubotch; u<ustart; u++)
4259895Ssam 			outword(word[u]);
4269895Ssam 		endline();
4279895Ssam 	}
4289895Ssam }
4299895Ssam 
4309895Ssam numways (leaf, last)
4319895Ssam register struct frame *leaf;
4329895Ssam struct frame *last;
4339895Ssam {
4349895Ssam 	int count;
4359895Ssam 	register char *p;
4369895Ssam 	register struct frame *node;
4379895Ssam 	if (super > 0)
4389895Ssam 		return(last-leaf);
4399895Ssam 	count = 0;
4409895Ssam 	present[BSIZE] = 1;
4419895Ssam 	while (leaf < last) {
4429895Ssam 		for (p = &present[0]; p < &present[BSIZE]; *p++ = 0);
4439895Ssam 		for (node = leaf; present[node->place]++ == 0; node = node->parent);
4449895Ssam 		if (node == &stack[0])
4459895Ssam 			count++;
4469895Ssam 		leaf++;
4479895Ssam 	}
4489895Ssam 	return(count);
4499895Ssam }
4509895Ssam 
4519895Ssam evalboard (getword, putword)
4529895Ssam int (*getword)(), (*putword)();
4539895Ssam {
4549895Ssam 	register struct frame *top;
4559895Ssam 	register int l, q;
4569895Ssam 	int fo, found;
4579895Ssam 	struct frame *parent, *lastparent;
4589895Ssam 	char *padj;
4599895Ssam 
4609895Ssam 	numsame = found = 0;
4619895Ssam 	makelists ();
4629895Ssam 
4639895Ssam 	while (1) {
4649895Ssam 		l = numsame;
4659895Ssam 		if (!(*getword) ())
4669895Ssam 			break;
4679895Ssam 		top = level[l+1];
4689895Ssam 
4699895Ssam 		while (1) {
4709895Ssam 			level[l+1] = lastparent = top;
4719895Ssam 			/* wbuff[1]...wbuff[l] have been matched */
4729895Ssam 			/* level[0],...,level[l] of tree built */
4739895Ssam 			if (l == wlength) {
4749895Ssam 				if (wlength >= 3 && (q = numways(level[l], top)) != 0) {
4759895Ssam 					(*putword) (q);
4769895Ssam 					found++;
4779895Ssam 				}
4789895Ssam 				l = BSIZE+1;
4799895Ssam 				break;
4809895Ssam 			}
4819895Ssam 			if ((fo = occurs[wbuff[++l]-'a']) == BSIZE)
4829895Ssam 				break;
4839895Ssam 			/* wbuff[1]...wbuff[l-1] have been matched */
4849895Ssam 			/* level[0],...,level[l-1] of tree built */
4859895Ssam 			for (parent=level[l-1]; parent<lastparent; parent++) {
4869895Ssam 				padj = &adj[parent->place][0];
4879895Ssam 				for (q=fo; q!=BSIZE; q=olink[q])
4889895Ssam 					if (padj[q]) {
4899895Ssam 						top->parent = parent;
4909895Ssam 						top->place = q;
4919895Ssam 						if (++top >= &stack[SSIZE]) {
4929895Ssam 							printf("stack overflow\n");
4939895Ssam 							goodbye(1);
4949895Ssam 						}
4959895Ssam 					}
4969895Ssam 			}
4979895Ssam 			/* were any nodes added? */
4989895Ssam 			if (top == lastparent)
4999895Ssam 				break;
5009895Ssam 		}
5019895Ssam 
5029895Ssam 		/* advance until first l characters of next word are different */
5039895Ssam 		while (numsame >= l && (*getword)()) ;
5049895Ssam 	}
5059895Ssam 	return(found);
5069895Ssam }
5079895Ssam 
5089895Ssam main (argc, argv)
5099895Ssam int argc;
5109895Ssam char **argv;
5119895Ssam {
5129895Ssam 	char *q;
5139895Ssam 	register char *p;
5149895Ssam 	register int i, c;
5159895Ssam 
5169895Ssam 	gtty (fileno(stdin), &origttyb);
5179895Ssam 	setbuf(stdin, NULL);
5189895Ssam 	tempttyb = origttyb;
5199895Ssam 	if (setjmp(env) != 0)
5209895Ssam 		goodbye(0);
5219895Ssam 	signal (SIGINT, interrupt);
522*46734Sbostic 	timein = time((time_t *)NULL);
523*46734Sbostic 	if (argv[0][0] != 'a' && (logfile = open(_PATH_BOGLOG, 1)) >= 0) {
5249895Ssam 		p = &logbuff[5];
5259895Ssam 		q = getlogin();
5269895Ssam 		while (*p++ = *q++);
5279895Ssam 		p[-1] = '\t';
5289895Ssam 		q = ctime(&timein);
5299895Ssam 		while (*p++ = *q++);
5309895Ssam 		logloc = lseek(logfile, 0L, 2);
5319895Ssam 		write(logfile, &logbuff[0], p-&logbuff[1]);
5329895Ssam 	}
53341169Sbostic 	if ((dict = fopen(_PATH_DICTIONARY, "r")) == NULL) {
53441169Sbostic 		printf("can't open %s\n", _PATH_DICTIONARY);
5359895Ssam 		goodbye (2);
5369895Ssam 	}
5379895Ssam 	while ( argc > 1 && ( argv[1][0] == '+' || argv[1][0] == '-' ) ) {
5389895Ssam 		if (argv[1][0]=='+') {
5399895Ssam 			while(*(argv[1]++) == '+')
5409895Ssam 				super++;
5419895Ssam 			argc--;
5429895Ssam 			argv++;
5439895Ssam 		}
5449895Ssam 		if ( argv[1][0] == '-' ) {
5459895Ssam 			timeint[0] = 60 * ( atol( &argv[1][1] ) - 2 );
5469895Ssam 			if ( timeint[0] <= 0 ) {
5479895Ssam 				timeint[0] = 60;
5489895Ssam 			}
5499895Ssam 			argc--;
5509895Ssam 			argv++;
5519895Ssam 		}
5529895Ssam 	}
5539895Ssam 	setup ();
5549895Ssam 	switch (argc) {
5559895Ssam 	default:  punt:
5569895Ssam 		printf("usage: boggle [+[+]] [row1 row2 row3 row4]\n");
5579895Ssam 		goodbye (3);
5589895Ssam 	case 5:
5599895Ssam 		for (i=0; i<BSIZE; i++) {
5609895Ssam 			board[i] = c = argv[row(i)+1][col(i)];
5619895Ssam 			if (!islower(c)) {
5629895Ssam 				printf("bad board\n");
5639895Ssam 				goto punt;
5649895Ssam 			}
5659895Ssam 		}
5669895Ssam 		printboard();
5679895Ssam 		column = 0;
5689895Ssam 		evalboard(getdword, tputword);
5699895Ssam 		endline();
5709895Ssam 		if (logfile >= 0) {
5719895Ssam 			strncpy(&logbuff[0], "eval", 4);
5729895Ssam 			lseek(logfile, logloc, 0);
5739895Ssam 			write(logfile, &logbuff[0], 4);
5749895Ssam 		}
5759895Ssam 		goodbye(0);
5769895Ssam 	case 1:
5779895Ssam 		tempttyb.sg_flags |= CBREAK;
5789895Ssam 		if ( ioctl( fileno(stdin), TIOCLGET, &ctlecho ) == 0 ) {
5799895Ssam 			if ( ctlecho & LCTLECH ) {
5809895Ssam 				ioctl( fileno(stdin), TIOCLBIC, &lctlech );
5819895Ssam 			}
5829895Ssam 		}
5839895Ssam 		printinst();
5849895Ssam 		srand((int) timein);
5859895Ssam 		while (setjmp(env) == 0) {
5869895Ssam 			errno = 0;
5879895Ssam 			if (pipe(&pipefd[0]) != 0) {
5889895Ssam 				printf("can't create pipe\n");
5899895Ssam 				goodbye(4);
5909895Ssam 			}
5919895Ssam 			genboard();
5929895Ssam 			delct = wcount = 0;
5939895Ssam 			word[0] = freesp = &space[0];
5949895Ssam 			if ((master = fork()) == 0) {
5959895Ssam 				close(pipefd[0]);
5969895Ssam 				clearscreen();
5979895Ssam 				printboard();
5989895Ssam 				signal(SIGALRM, timeout);
5999895Ssam 				timept = &timeint[0];
6009895Ssam 				alarm(*timept++);
6019895Ssam 				evalboard(getuword, aputuword);
6029895Ssam 				clearscreen();
6039895Ssam 				qsort(&word[0], wcount, sizeof (int), compare);
6049895Ssam 				for (i=0; i<wcount; i++)
6059895Ssam 					if (i==0 || wordcomp(word[i], word[i-1])!=0) {
6069895Ssam 						p = word[i];
6079895Ssam 						while (isalpha(*++p)) ;
6089895Ssam 						write (pipefd[1], word[i], p-word[i]);
6099895Ssam 					}
6109895Ssam 				close(pipefd[1]);
6119895Ssam 				goodbye(0);
6129895Ssam 			}
6139895Ssam 			close(pipefd[1]);
6149895Ssam 			rewind(dict);
6159895Ssam 			getc(dict);
6169895Ssam 			evalboard(getdword, aputword);
6179895Ssam 			p = freesp;
6189895Ssam 			while ((i = read(pipefd[0], freesp, 512)) != 0) {
6199895Ssam 				if (i < 0)
6209895Ssam 					if (errno != EINTR)
6219895Ssam 						break;
6229895Ssam 					else
6239895Ssam 						i = 0;
6249895Ssam 				freesp += i;
6259895Ssam 			}
6269895Ssam 			close(pipefd[0]);
6279895Ssam 			ustart = ubotch = wcount;
6289895Ssam 			while (p < freesp) {
6299895Ssam 				word[wcount++] = p;
6309895Ssam 				if (*p == '0')
6319895Ssam 					ustart = wcount;
6329895Ssam 				while (isalpha(*++p));
6339895Ssam 			}
6349895Ssam 			wait(&status);
6359895Ssam 			if (status != 0)
6369895Ssam 				goodbye (5);
6379895Ssam 			delct = 1;
6389895Ssam 			printdiff();
6399895Ssam 			printboard();
6409895Ssam 			games++;
6419895Ssam 			if (logfile >= 0) {
64232490Sbostic 				(void)sprintf(&logbuff[0], "%4d", games);
6439895Ssam 				lseek(logfile, logloc, 0);
6449895Ssam 				write(logfile, &logbuff[0], 4);
6459895Ssam 			}
6469895Ssam 			stty (fileno(stdin), &tempttyb);
6479895Ssam 			printf("\nanother game?");
6489895Ssam 			if (getchar() != 'y') {
6499895Ssam 				putchar('\n');
6509895Ssam 				break;
6519895Ssam 			}
6529895Ssam 			stty (fileno(stdin), &tempttyb);
6539895Ssam 		}
6549895Ssam 		goodbye(0);
6559895Ssam 	}
6569895Ssam }
657