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