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