xref: /netbsd-src/games/quiz/quiz.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
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.6 1994/04/08 08:33:13 pk 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 <err.h>
56 #include "quiz.h"
57 #include "pathnames.h"
58 
59 static QE qlist;
60 static int catone, cattwo, tflag;
61 static u_int qsize;
62 
63 char	*appdstr __P((char *, char *));
64 void	 downcase __P((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(1, "%s", file);
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 = fgetln(fp, &len)) != NULL) {
133 		if (qp->q_text && qp->q_text[strlen(qp->q_text) - 1] == '\\') {
134 			lp[len - 1] = '\0';
135 			qp->q_text = appdstr(qp->q_text, lp);
136 		} else {
137 			if ((qp->q_next = malloc(sizeof(QE))) == NULL)
138 				err(1, NULL);
139 			qp = qp->q_next;
140 			lp[len - 1] = '\0';
141 			if ((qp->q_text = strdup(lp)) == NULL)
142 				err(1, NULL);
143 			qp->q_asked = qp->q_answered = FALSE;
144 			qp->q_next = NULL;
145 			++qsize;
146 		}
147 	}
148 	(void)fclose(fp);
149 }
150 
151 void
152 show_index()
153 {
154 	register QE *qp;
155 	register char *p, *s;
156 	FILE *pf;
157 
158 	if ((pf = popen(_PATH_PAGER, "w")) == NULL)
159 		err(1, "%s", _PATH_PAGER);
160 	(void)fprintf(pf, "Subjects:\n\n");
161 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
162 		for (s = next_cat(qp->q_text); s; s = next_cat(s)) {
163 			if (!rxp_compile(s))
164 				errx(1, "%s", rxperr);
165 			if (p = rxp_expand())
166 				(void)fprintf(pf, "%s ", p);
167 		}
168 		(void)fprintf(pf, "\n");
169 	}
170 	(void)fprintf(pf, "\n%s\n%s\n%s\n",
171 "For example, \"quiz victim killer\" prints a victim's name and you reply",
172 "with the killer, and \"quiz killer victim\" works the other way around.",
173 "Type an empty line to get the correct answer.");
174 	(void)pclose(pf);
175 }
176 
177 void
178 get_cats(cat1, cat2)
179 	char *cat1, *cat2;
180 {
181 	register QE *qp;
182 	int i;
183 	char *s;
184 
185 	downcase(cat1);
186 	downcase(cat2);
187 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
188 		s = next_cat(qp->q_text);
189 		catone = cattwo = i = 0;
190 		while (s) {
191 			if (!rxp_compile(s))
192 				errx(1, "%s", rxperr);
193 			i++;
194 			if (rxp_match(cat1))
195 				catone = i;
196 			if (rxp_match(cat2))
197 				cattwo = i;
198 			s = next_cat(s);
199 		}
200 		if (catone && cattwo && catone != cattwo) {
201 			if (!rxp_compile(qp->q_text))
202 				errx(1, "%s", rxperr);
203 			get_file(rxp_expand());
204 			return;
205 		}
206 	}
207 	errx(1, "invalid categories");
208 }
209 
210 void
211 quiz()
212 {
213 	register QE *qp;
214 	register int i;
215 	u_int guesses, rights, wrongs;
216 	int len, next;
217 	char *s, *t, question[LINE_SZ];
218 	char *answer;
219 
220 	srandom(time(NULL));
221 	guesses = rights = wrongs = 0;
222 	for (;;) {
223 		if (qsize == 0)
224 			break;
225 		next = random() % qsize;
226 		qp = qlist.q_next;
227 		for (i = 0; i < next; i++)
228 			qp = qp->q_next;
229 		while (qp && qp->q_answered)
230 			qp = qp->q_next;
231 		if (!qp) {
232 			qsize = next;
233 			continue;
234 		}
235 		if (tflag && random() % 100 > 20) {
236 			/* repeat questions in tutorial mode */
237 			while (qp && (!qp->q_asked || qp->q_answered))
238 				qp = qp->q_next;
239 			if (!qp)
240 				continue;
241 		}
242 		s = qp->q_text;
243 		for (i = 0; i < catone - 1; i++)
244 			s = next_cat(s);
245 		if (!rxp_compile(s))
246 			errx(1, "%s", rxperr);
247 		t = rxp_expand();
248 		if (!t || *t == '\0') {
249 			qp->q_answered = TRUE;
250 			continue;
251 		}
252 		(void)strcpy(question, t);
253 		s = qp->q_text;
254 		for (i = 0; i < cattwo - 1; i++)
255 			s = next_cat(s);
256 		if (!rxp_compile(s))
257 			errx(1, "%s", rxperr);
258 		t = rxp_expand();
259 		if (!t || *t == '\0') {
260 			qp->q_answered = TRUE;
261 			continue;
262 		}
263 		qp->q_asked = TRUE;
264 		(void)printf("%s?\n", question);
265 		for (;; ++guesses) {
266 			if ((answer = fgetln(stdin, &len)) == NULL) {
267 				score(rights, wrongs, guesses);
268 				exit(0);
269 			}
270 			answer[len - 1] = '\0';
271 			downcase(answer);
272 			if (rxp_match(answer)) {
273 				(void)printf("Right!\n");
274 				++rights;
275 				qp->q_answered = TRUE;
276 				break;
277 			}
278 			if (*answer == '\0') {
279 				(void)printf("%s\n", t);
280 				++wrongs;
281 				if (!tflag)
282 					qp->q_answered = TRUE;
283 				break;
284 			}
285 			(void)printf("What?\n");
286 		}
287 	}
288 	score(rights, wrongs, guesses);
289 }
290 
291 char *
292 next_cat(s)
293 	register char *	s;
294 {
295 	for (;;)
296 		switch (*s++) {
297 		case '\0':
298 			return (NULL);
299 		case '\\':
300 			break;
301 		case ':':
302 			return (s);
303 		}
304 	/* NOTREACHED */
305 }
306 
307 char *
308 appdstr(s, tp)
309 	char *s;
310 	register char *tp;
311 {
312 	register char *mp, *sp;
313 	register int ch;
314 	char *m;
315 
316 	if ((m = malloc(strlen(s) + strlen(tp) + 1)) == NULL)
317 		err(1, NULL);
318 	for (mp = m, sp = s; *mp++ = *sp++;);
319 	--mp;
320 
321 	if (*(mp - 1) == '\\')
322 		--mp;
323 	while ((ch = *mp++ = *tp++) && ch != '\n');
324 	*mp = '\0';
325 
326 	free(s);
327 	return (m);
328 }
329 
330 void
331 score(r, w, g)
332 	u_int r, w, g;
333 {
334 	(void)printf("Rights %d, wrongs %d,", r, w);
335 	if (g)
336 		(void)printf(" extra guesses %d,", g);
337 	(void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0);
338 }
339 
340 void
341 downcase(p)
342 	register char *p;
343 {
344 	register int ch;
345 
346 	for (; ch = *p; ++p)
347 		if (isascii(ch) && isupper(ch))
348 			*p = tolower(ch);
349 }
350 
351 void
352 usage()
353 {
354 	(void)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n");
355 	exit(1);
356 }
357