Home | History | Annotate | Line # | Download | only in quiz
quiz.c revision 1.14
      1 /*	$NetBSD: quiz.c,v 1.14 1999/09/08 21:17:56 jsm Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
      9  * Commodore Business Machines.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     43 	The Regents of the University of California.  All rights reserved.\n");
     44 #endif /* not lint */
     45 
     46 #ifndef lint
     47 #if 0
     48 static char sccsid[] = "@(#)quiz.c	8.3 (Berkeley) 5/4/95";
     49 #else
     50 __RCSID("$NetBSD: quiz.c,v 1.14 1999/09/08 21:17:56 jsm Exp $");
     51 #endif
     52 #endif /* not lint */
     53 
     54 #include <sys/types.h>
     55 
     56 #include <ctype.h>
     57 #include <errno.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <ctype.h>
     62 #include <err.h>
     63 #include <time.h>
     64 #include <unistd.h>
     65 #include "quiz.h"
     66 #include "pathnames.h"
     67 
     68 static QE qlist;
     69 static int catone, cattwo, tflag;
     70 static u_int qsize;
     71 
     72 char	*appdstr __P((char *, const char *, size_t));
     73 void	 downcase __P((char *));
     74 void	 get_cats __P((char *, char *));
     75 void	 get_file __P((const char *));
     76 int	 main __P((int, char *[]));
     77 const char	*next_cat __P((const char *));
     78 void	 quiz __P((void));
     79 void	 score __P((u_int, u_int, u_int));
     80 void	 show_index __P((void));
     81 void	 usage __P((void)) __attribute__((__noreturn__));
     82 
     83 int
     84 main(argc, argv)
     85 	int argc;
     86 	char *argv[];
     87 {
     88 	int ch;
     89 	const char *indexfile;
     90 
     91 	indexfile = _PATH_QUIZIDX;
     92 	while ((ch = getopt(argc, argv, "i:t")) != -1)
     93 		switch(ch) {
     94 		case 'i':
     95 			indexfile = optarg;
     96 			break;
     97 		case 't':
     98 			tflag = 1;
     99 			break;
    100 		case '?':
    101 		default:
    102 			usage();
    103 		}
    104 	argc -= optind;
    105 	argv += optind;
    106 
    107 	switch(argc) {
    108 	case 0:
    109 		get_file(indexfile);
    110 		show_index();
    111 		break;
    112 	case 2:
    113 		get_file(indexfile);
    114 		get_cats(argv[0], argv[1]);
    115 		quiz();
    116 		break;
    117 	default:
    118 		usage();
    119 	}
    120 	exit(0);
    121 }
    122 
    123 void
    124 get_file(file)
    125 	const char *file;
    126 {
    127 	FILE *fp;
    128 	QE *qp;
    129 	size_t len;
    130 	char *lp;
    131 
    132 	if ((fp = fopen(file, "r")) == NULL)
    133 		err(1, "%s", file);
    134 
    135 	/*
    136 	 * XXX
    137 	 * Should really free up space from any earlier read list
    138 	 * but there are no reverse pointers to do so with.
    139 	 */
    140 	qp = &qlist;
    141 	qsize = 0;
    142 	while ((lp = fgetln(fp, &len)) != NULL) {
    143 		if (lp[len - 1] == '\n')
    144 			lp[--len] = '\0';
    145 		if (qp->q_text && qp->q_text[strlen(qp->q_text) - 1] == '\\')
    146 			qp->q_text = appdstr(qp->q_text, lp, len);
    147 		else {
    148 			if ((qp->q_next = malloc(sizeof(QE))) == NULL)
    149 				errx(1, "malloc");
    150 			qp = qp->q_next;
    151 			if ((qp->q_text = malloc(len + 1)) == NULL)
    152 				errx(1, "malloc");
    153 			strncpy(qp->q_text, lp, len);
    154 			qp->q_text[len] = '\0';
    155 			qp->q_asked = qp->q_answered = FALSE;
    156 			qp->q_next = NULL;
    157 			++qsize;
    158 		}
    159 	}
    160 	(void)fclose(fp);
    161 }
    162 
    163 void
    164 show_index()
    165 {
    166 	QE *qp;
    167 	const char *p, *s;
    168 	FILE *pf;
    169 
    170 	if ((pf = popen(_PATH_PAGER, "w")) == NULL)
    171 		err(1, "%s", _PATH_PAGER);
    172 	(void)fprintf(pf, "Subjects:\n\n");
    173 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
    174 		for (s = next_cat(qp->q_text); s; s = next_cat(s)) {
    175 			if (!rxp_compile(s))
    176 				errx(1, "%s", rxperr);
    177 			if ((p = rxp_expand()) != NULL)
    178 				(void)fprintf(pf, "%s ", p);
    179 		}
    180 		(void)fprintf(pf, "\n");
    181 	}
    182 	(void)fprintf(pf, "\n%s\n%s\n%s\n",
    183 "For example, \"quiz victim killer\" prints a victim's name and you reply",
    184 "with the killer, and \"quiz killer victim\" works the other way around.",
    185 "Type an empty line to get the correct answer.");
    186 	(void)pclose(pf);
    187 }
    188 
    189 void
    190 get_cats(cat1, cat2)
    191 	char *cat1, *cat2;
    192 {
    193 	QE *qp;
    194 	int i;
    195 	const char *s;
    196 
    197 	downcase(cat1);
    198 	downcase(cat2);
    199 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
    200 		s = next_cat(qp->q_text);
    201 		catone = cattwo = i = 0;
    202 		while (s) {
    203 			if (!rxp_compile(s))
    204 				errx(1, "%s", rxperr);
    205 			i++;
    206 			if (rxp_match(cat1))
    207 				catone = i;
    208 			if (rxp_match(cat2))
    209 				cattwo = i;
    210 			s = next_cat(s);
    211 		}
    212 		if (catone && cattwo && catone != cattwo) {
    213 			if (!rxp_compile(qp->q_text))
    214 				errx(1, "%s", rxperr);
    215 			get_file(rxp_expand());
    216 			return;
    217 		}
    218 	}
    219 	errx(1, "invalid categories");
    220 }
    221 
    222 void
    223 quiz()
    224 {
    225 	QE *qp;
    226 	int i;
    227 	size_t len;
    228 	u_int guesses, rights, wrongs;
    229 	int next;
    230 	char *answer, *t, question[LINE_SZ];
    231 	const char *s;
    232 
    233 	srandom(time(NULL));
    234 	guesses = rights = wrongs = 0;
    235 	for (;;) {
    236 		if (qsize == 0)
    237 			break;
    238 		next = random() % qsize;
    239 		qp = qlist.q_next;
    240 		for (i = 0; i < next; i++)
    241 			qp = qp->q_next;
    242 		while (qp && qp->q_answered)
    243 			qp = qp->q_next;
    244 		if (!qp) {
    245 			qsize = next;
    246 			continue;
    247 		}
    248 		if (tflag && random() % 100 > 20) {
    249 			/* repeat questions in tutorial mode */
    250 			while (qp && (!qp->q_asked || qp->q_answered))
    251 				qp = qp->q_next;
    252 			if (!qp)
    253 				continue;
    254 		}
    255 		s = qp->q_text;
    256 		for (i = 0; i < catone - 1; i++)
    257 			s = next_cat(s);
    258 		if (!rxp_compile(s))
    259 			errx(1, "%s", rxperr);
    260 		t = rxp_expand();
    261 		if (!t || *t == '\0') {
    262 			qp->q_answered = TRUE;
    263 			continue;
    264 		}
    265 		(void)strcpy(question, t);
    266 		s = qp->q_text;
    267 		for (i = 0; i < cattwo - 1; i++)
    268 			s = next_cat(s);
    269 		if (!rxp_compile(s))
    270 			errx(1, "%s", rxperr);
    271 		t = rxp_expand();
    272 		if (!t || *t == '\0') {
    273 			qp->q_answered = TRUE;
    274 			continue;
    275 		}
    276 		qp->q_asked = TRUE;
    277 		(void)printf("%s?\n", question);
    278 		for (;; ++guesses) {
    279 			if ((answer = fgetln(stdin, &len)) == NULL ||
    280 			    answer[len - 1] != '\n') {
    281 				score(rights, wrongs, guesses);
    282 				exit(0);
    283 			}
    284 			answer[len - 1] = '\0';
    285 			downcase(answer);
    286 			if (rxp_match(answer)) {
    287 				(void)printf("Right!\n");
    288 				++rights;
    289 				qp->q_answered = TRUE;
    290 				break;
    291 			}
    292 			if (*answer == '\0') {
    293 				(void)printf("%s\n", t);
    294 				++wrongs;
    295 				if (!tflag)
    296 					qp->q_answered = TRUE;
    297 				break;
    298 			}
    299 			(void)printf("What?\n");
    300 		}
    301 	}
    302 	score(rights, wrongs, guesses);
    303 }
    304 
    305 const char *
    306 next_cat(s)
    307 	const char *	s;
    308 {
    309 	int esc;
    310 
    311 	esc = 0;
    312 	for (;;)
    313 		switch (*s++) {
    314 		case '\0':
    315 			return (NULL);
    316 		case '\\':
    317 			esc = 1;
    318 			break;
    319 		case ':':
    320 			if (!esc)
    321 				return (s);
    322 		default:
    323 			esc = 0;
    324 			break;
    325 		}
    326 	/* NOTREACHED */
    327 }
    328 
    329 char *
    330 appdstr(s, tp, len)
    331 	char *s;
    332 	const char *tp;
    333 	size_t len;
    334 {
    335 	char *mp;
    336 	const char *sp;
    337 	int ch;
    338 	char *m;
    339 
    340 	if ((m = malloc(strlen(s) + len + 1)) == NULL)
    341 		errx(1, "malloc");
    342 	for (mp = m, sp = s; (*mp++ = *sp++) != NULL; )
    343 		;
    344 	--mp;
    345 	if (*(mp - 1) == '\\')
    346 		--mp;
    347 
    348 	while ((ch = *mp++ = *tp++) && ch != '\n')
    349 		;
    350 	*mp = '\0';
    351 
    352 	free(s);
    353 	return (m);
    354 }
    355 
    356 void
    357 score(r, w, g)
    358 	u_int r, w, g;
    359 {
    360 	(void)printf("Rights %d, wrongs %d,", r, w);
    361 	if (g)
    362 		(void)printf(" extra guesses %d,", g);
    363 	(void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0);
    364 }
    365 
    366 void
    367 downcase(p)
    368 	char *p;
    369 {
    370 	int ch;
    371 
    372 	for (; (ch = *p) != '\0'; ++p)
    373 		if (isascii(ch) && isupper(ch))
    374 			*p = tolower(ch);
    375 }
    376 
    377 void
    378 usage()
    379 {
    380 	(void)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n");
    381 	exit(1);
    382 }
    383