Home | History | Annotate | Line # | Download | only in quiz
quiz.c revision 1.16
      1 /*	$NetBSD: quiz.c,v 1.16 1999/09/17 17:07:11 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.16 1999/09/17 17:07:11 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 	/* Revoke setgid privileges */
     92 	setregid(getgid(), getgid());
     93 
     94 	indexfile = _PATH_QUIZIDX;
     95 	while ((ch = getopt(argc, argv, "i:t")) != -1)
     96 		switch(ch) {
     97 		case 'i':
     98 			indexfile = optarg;
     99 			break;
    100 		case 't':
    101 			tflag = 1;
    102 			break;
    103 		case '?':
    104 		default:
    105 			usage();
    106 		}
    107 	argc -= optind;
    108 	argv += optind;
    109 
    110 	switch(argc) {
    111 	case 0:
    112 		get_file(indexfile);
    113 		show_index();
    114 		break;
    115 	case 2:
    116 		get_file(indexfile);
    117 		get_cats(argv[0], argv[1]);
    118 		quiz();
    119 		break;
    120 	default:
    121 		usage();
    122 	}
    123 	exit(0);
    124 }
    125 
    126 void
    127 get_file(file)
    128 	const char *file;
    129 {
    130 	FILE *fp;
    131 	QE *qp;
    132 	size_t len;
    133 	char *lp;
    134 
    135 	if ((fp = fopen(file, "r")) == NULL)
    136 		err(1, "%s", file);
    137 
    138 	/*
    139 	 * XXX
    140 	 * Should really free up space from any earlier read list
    141 	 * but there are no reverse pointers to do so with.
    142 	 */
    143 	qp = &qlist;
    144 	qsize = 0;
    145 	while ((lp = fgetln(fp, &len)) != NULL) {
    146 		if (lp[len - 1] == '\n')
    147 			lp[--len] = '\0';
    148 		if (qp->q_text && qp->q_text[strlen(qp->q_text) - 1] == '\\')
    149 			qp->q_text = appdstr(qp->q_text, lp, len);
    150 		else {
    151 			if ((qp->q_next = malloc(sizeof(QE))) == NULL)
    152 				errx(1, "malloc");
    153 			qp = qp->q_next;
    154 			if ((qp->q_text = malloc(len + 1)) == NULL)
    155 				errx(1, "malloc");
    156 			strncpy(qp->q_text, lp, len);
    157 			qp->q_text[len] = '\0';
    158 			qp->q_asked = qp->q_answered = FALSE;
    159 			qp->q_next = NULL;
    160 			++qsize;
    161 		}
    162 	}
    163 	(void)fclose(fp);
    164 }
    165 
    166 void
    167 show_index()
    168 {
    169 	QE *qp;
    170 	const char *p, *s;
    171 	FILE *pf;
    172 
    173 	if ((pf = popen(_PATH_PAGER, "w")) == NULL)
    174 		err(1, "%s", _PATH_PAGER);
    175 	(void)fprintf(pf, "Subjects:\n\n");
    176 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
    177 		for (s = next_cat(qp->q_text); s; s = next_cat(s)) {
    178 			if (!rxp_compile(s))
    179 				errx(1, "%s", rxperr);
    180 			if ((p = rxp_expand()) != NULL)
    181 				(void)fprintf(pf, "%s ", p);
    182 		}
    183 		(void)fprintf(pf, "\n");
    184 	}
    185 	(void)fprintf(pf, "\n%s\n%s\n%s\n",
    186 "For example, \"quiz victim killer\" prints a victim's name and you reply",
    187 "with the killer, and \"quiz killer victim\" works the other way around.",
    188 "Type an empty line to get the correct answer.");
    189 	(void)pclose(pf);
    190 }
    191 
    192 void
    193 get_cats(cat1, cat2)
    194 	char *cat1, *cat2;
    195 {
    196 	QE *qp;
    197 	int i;
    198 	const char *s;
    199 
    200 	downcase(cat1);
    201 	downcase(cat2);
    202 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
    203 		s = next_cat(qp->q_text);
    204 		catone = cattwo = i = 0;
    205 		while (s) {
    206 			if (!rxp_compile(s))
    207 				errx(1, "%s", rxperr);
    208 			i++;
    209 			if (rxp_match(cat1))
    210 				catone = i;
    211 			if (rxp_match(cat2))
    212 				cattwo = i;
    213 			s = next_cat(s);
    214 		}
    215 		if (catone && cattwo && catone != cattwo) {
    216 			if (!rxp_compile(qp->q_text))
    217 				errx(1, "%s", rxperr);
    218 			get_file(rxp_expand());
    219 			return;
    220 		}
    221 	}
    222 	errx(1, "invalid categories");
    223 }
    224 
    225 void
    226 quiz()
    227 {
    228 	QE *qp;
    229 	int i;
    230 	size_t len;
    231 	u_int guesses, rights, wrongs;
    232 	int next;
    233 	char *answer, *t, question[LINE_SZ];
    234 	const char *s;
    235 
    236 	srandom(time(NULL));
    237 	guesses = rights = wrongs = 0;
    238 	for (;;) {
    239 		if (qsize == 0)
    240 			break;
    241 		next = random() % qsize;
    242 		qp = qlist.q_next;
    243 		for (i = 0; i < next; i++)
    244 			qp = qp->q_next;
    245 		while (qp && qp->q_answered)
    246 			qp = qp->q_next;
    247 		if (!qp) {
    248 			qsize = next;
    249 			continue;
    250 		}
    251 		if (tflag && random() % 100 > 20) {
    252 			/* repeat questions in tutorial mode */
    253 			while (qp && (!qp->q_asked || qp->q_answered))
    254 				qp = qp->q_next;
    255 			if (!qp)
    256 				continue;
    257 		}
    258 		s = qp->q_text;
    259 		for (i = 0; i < catone - 1; i++)
    260 			s = next_cat(s);
    261 		if (!rxp_compile(s))
    262 			errx(1, "%s", rxperr);
    263 		t = rxp_expand();
    264 		if (!t || *t == '\0') {
    265 			qp->q_answered = TRUE;
    266 			continue;
    267 		}
    268 		(void)strcpy(question, t);
    269 		s = qp->q_text;
    270 		for (i = 0; i < cattwo - 1; i++)
    271 			s = next_cat(s);
    272 		if (!rxp_compile(s))
    273 			errx(1, "%s", rxperr);
    274 		t = rxp_expand();
    275 		if (!t || *t == '\0') {
    276 			qp->q_answered = TRUE;
    277 			continue;
    278 		}
    279 		qp->q_asked = TRUE;
    280 		(void)printf("%s?\n", question);
    281 		for (;; ++guesses) {
    282 			if ((answer = fgetln(stdin, &len)) == NULL ||
    283 			    answer[len - 1] != '\n') {
    284 				score(rights, wrongs, guesses);
    285 				exit(0);
    286 			}
    287 			answer[len - 1] = '\0';
    288 			downcase(answer);
    289 			if (rxp_match(answer)) {
    290 				(void)printf("Right!\n");
    291 				++rights;
    292 				qp->q_answered = TRUE;
    293 				break;
    294 			}
    295 			if (*answer == '\0') {
    296 				(void)printf("%s\n", t);
    297 				++wrongs;
    298 				if (!tflag)
    299 					qp->q_answered = TRUE;
    300 				break;
    301 			}
    302 			(void)printf("What?\n");
    303 		}
    304 	}
    305 	score(rights, wrongs, guesses);
    306 }
    307 
    308 const char *
    309 next_cat(s)
    310 	const char *	s;
    311 {
    312 	int esc;
    313 
    314 	esc = 0;
    315 	for (;;)
    316 		switch (*s++) {
    317 		case '\0':
    318 			return (NULL);
    319 		case '\\':
    320 			esc = 1;
    321 			break;
    322 		case ':':
    323 			if (!esc)
    324 				return (s);
    325 		default:
    326 			esc = 0;
    327 			break;
    328 		}
    329 	/* NOTREACHED */
    330 }
    331 
    332 char *
    333 appdstr(s, tp, len)
    334 	char *s;
    335 	const char *tp;
    336 	size_t len;
    337 {
    338 	char *mp;
    339 	const char *sp;
    340 	int ch;
    341 	char *m;
    342 
    343 	if ((m = malloc(strlen(s) + len + 1)) == NULL)
    344 		errx(1, "malloc");
    345 	for (mp = m, sp = s; (*mp++ = *sp++) != '\0'; )
    346 		;
    347 	--mp;
    348 	if (*(mp - 1) == '\\')
    349 		--mp;
    350 
    351 	while ((ch = *mp++ = *tp++) && ch != '\n')
    352 		;
    353 	*mp = '\0';
    354 
    355 	free(s);
    356 	return (m);
    357 }
    358 
    359 void
    360 score(r, w, g)
    361 	u_int r, w, g;
    362 {
    363 	(void)printf("Rights %d, wrongs %d,", r, w);
    364 	if (g)
    365 		(void)printf(" extra guesses %d,", g);
    366 	(void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0);
    367 }
    368 
    369 void
    370 downcase(p)
    371 	char *p;
    372 {
    373 	int ch;
    374 
    375 	for (; (ch = *p) != '\0'; ++p)
    376 		if (isascii(ch) && isupper(ch))
    377 			*p = tolower(ch);
    378 }
    379 
    380 void
    381 usage()
    382 {
    383 	(void)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n");
    384 	exit(1);
    385 }
    386