xref: /openbsd-src/games/quiz/quiz.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: quiz.c,v 1.19 2008/10/03 13:00:06 deraadt Exp $	*/
2 /*	$NetBSD: quiz.c,v 1.9 1995/04/22 10:16:58 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
10  * Commodore Business Machines.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. 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 static char copyright[] =
39 "@(#) Copyright (c) 1991, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)quiz.c	8.3 (Berkeley) 5/4/95";
46 #else
47 static char rcsid[] = "$OpenBSD: quiz.c,v 1.19 2008/10/03 13:00:06 deraadt Exp $";
48 #endif
49 #endif /* not lint */
50 
51 #include <sys/types.h>
52 #include <errno.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <ctype.h>
57 #include <err.h>
58 #include <time.h>
59 #include <unistd.h>
60 #include "quiz.h"
61 #include "pathnames.h"
62 
63 static QE qlist;
64 static int catone, cattwo, tflag;
65 static u_int qsize;
66 
67 char	*appdstr(char *, const char *, size_t);
68 void	 downcase(char *);
69 void	 get_cats(char *, char *);
70 void	 get_file(const char *);
71 const char	*next_cat(const char *);
72 void	 quiz(void);
73 void	 score(u_int, u_int, u_int);
74 void	 show_index(void);
75 void	 usage(void);
76 
77 int
78 main(int argc, char *argv[])
79 {
80 	int ch;
81 	const char *indexfile;
82 
83 	indexfile = _PATH_QUIZIDX;
84 	while ((ch = getopt(argc, argv, "i:t")) != -1)
85 		switch(ch) {
86 		case 'i':
87 			indexfile = optarg;
88 			break;
89 		case 't':
90 			tflag = 1;
91 			break;
92 		case '?':
93 		default:
94 			usage();
95 		}
96 	argc -= optind;
97 	argv += optind;
98 
99 	switch(argc) {
100 	case 0:
101 		get_file(indexfile);
102 		show_index();
103 		break;
104 	case 2:
105 		get_file(indexfile);
106 		get_cats(argv[0], argv[1]);
107 		quiz();
108 		break;
109 	default:
110 		usage();
111 	}
112 	exit(0);
113 }
114 
115 void
116 get_file(const char *file)
117 {
118 	FILE *fp;
119 	QE *qp;
120 	size_t len;
121 	char *lp;
122 
123 	if ((fp = fopen(file, "r")) == NULL)
124 		err(1, "%s", file);
125 
126 	/*
127 	 * XXX
128 	 * Should really free up space from any earlier read list
129 	 * but there are no reverse pointers to do so with.
130 	 */
131 	qp = &qlist;
132 	qsize = 0;
133 	while ((lp = fgetln(fp, &len)) != NULL) {
134 		if (lp[len - 1] == '\n')
135 			--len;
136 		if (qp->q_text && qp->q_text[0] != '\0' &&
137 		    qp->q_text[strlen(qp->q_text) - 1] == '\\')
138 			qp->q_text = appdstr(qp->q_text, lp, len);
139 		else {
140 			if ((qp->q_next = malloc(sizeof(QE))) == NULL)
141 				errx(1, "malloc");
142 			qp = qp->q_next;
143 			if ((qp->q_text = malloc(len + 1)) == NULL)
144 				errx(1, "malloc");
145 			/* lp may not be zero-terminated; cannot use strlcpy */
146 			strncpy(qp->q_text, lp, len);
147 			qp->q_text[len] = '\0';
148 			qp->q_asked = qp->q_answered = FALSE;
149 			qp->q_next = NULL;
150 			++qsize;
151 		}
152 	}
153 	(void)fclose(fp);
154 }
155 
156 void
157 show_index(void)
158 {
159 	QE *qp;
160 	const char *p, *s;
161 	FILE *pf;
162 	const char *pager;
163 
164 	if (!isatty(1))
165 		pager = "/bin/cat";
166 	else if (!(pager = getenv("PAGER")) || (*pager == 0))
167 			pager = _PATH_PAGER;
168 	if ((pf = popen(pager, "w")) == NULL)
169 		err(1, "%s", pager);
170 	(void)fprintf(pf, "Subjects:\n\n");
171 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
172 		for (s = next_cat(qp->q_text); s; s = next_cat(s)) {
173 			if (!rxp_compile(s))
174 				errx(1, "%s", rxperr);
175 			if ((p = rxp_expand()))
176 				(void)fprintf(pf, "%s ", p);
177 		}
178 		(void)fprintf(pf, "\n");
179 	}
180 	(void)fprintf(pf, "\n%s\n%s\n%s\n",
181 "For example, \"quiz victim killer\" prints a victim's name and you reply",
182 "with the killer, and \"quiz killer victim\" works the other way around.",
183 "Type an empty line to get the correct answer.");
184 	(void)pclose(pf);
185 }
186 
187 void
188 get_cats(char *cat1, char *cat2)
189 {
190 	QE *qp;
191 	int i;
192 	const char *s;
193 
194 	downcase(cat1);
195 	downcase(cat2);
196 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
197 		s = next_cat(qp->q_text);
198 		catone = cattwo = i = 0;
199 		while (s) {
200 			if (!rxp_compile(s))
201 				errx(1, "%s", rxperr);
202 			i++;
203 			if (rxp_match(cat1))
204 				catone = i;
205 			if (rxp_match(cat2))
206 				cattwo = i;
207 			s = next_cat(s);
208 		}
209 		if (catone && cattwo && catone != cattwo) {
210 			if (!rxp_compile(qp->q_text))
211 				errx(1, "%s", rxperr);
212 			get_file(rxp_expand());
213 			return;
214 		}
215 	}
216 	errx(1, "invalid categories");
217 }
218 
219 void
220 quiz(void)
221 {
222 	QE *qp;
223 	int i;
224 	size_t len;
225 	u_int guesses, rights, wrongs;
226 	int next;
227 	char *answer, *t, question[LINE_SZ];
228 	const char *s;
229 
230 	srandomdev();
231 	guesses = rights = wrongs = 0;
232 	for (;;) {
233 		if (qsize == 0)
234 			break;
235 		next = random() % qsize;
236 		qp = qlist.q_next;
237 		for (i = 0; i < next; i++)
238 			qp = qp->q_next;
239 		while (qp && qp->q_answered)
240 			qp = qp->q_next;
241 		if (!qp) {
242 			qsize = next;
243 			continue;
244 		}
245 		if (tflag && random() % 100 > 20) {
246 			/* repeat questions in tutorial mode */
247 			while (qp && (!qp->q_asked || qp->q_answered))
248 				qp = qp->q_next;
249 			if (!qp)
250 				continue;
251 		}
252 		s = qp->q_text;
253 		for (i = 0; i < catone - 1; i++)
254 			s = next_cat(s);
255 		if (!rxp_compile(s))
256 			errx(1, "%s", rxperr);
257 		t = rxp_expand();
258 		if (!t || *t == '\0') {
259 			qp->q_answered = TRUE;
260 			continue;
261 		}
262 		(void)strlcpy(question, t, sizeof question);
263 		s = qp->q_text;
264 		for (i = 0; i < cattwo - 1; i++)
265 			s = next_cat(s);
266 		if (s == NULL)
267 			errx(1, "too few fields in data file, line \"%s\"",
268 			    qp->q_text);
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(const char *s)
307 {
308 	int esc;
309 
310 	if (s == NULL)
311 		return (NULL);
312 	esc = 0;
313 	for (;;)
314 		switch (*s++) {
315 		case '\0':
316 			return (NULL);
317 		case '\\':
318 			esc = 1;
319 			break;
320 		case ':':
321 			if (!esc)
322 				return (s);
323 		default:
324 			esc = 0;
325 			break;
326 		}
327 	/* NOTREACHED */
328 }
329 
330 char *
331 appdstr(char *s, const char *tp, size_t len)
332 {
333 	char *mp;
334 	const char *sp;
335 	int ch;
336 	char *m;
337 
338 	if ((m = malloc(strlen(s) + len + 1)) == NULL)
339 		errx(1, "malloc");
340 	for (mp = m, sp = s; (*mp++ = *sp++) != '\0'; )
341 		;
342 	--mp;
343 	if (*(mp - 1) == '\\')
344 		--mp;
345 
346 	while ((ch = *mp++ = *tp++) && ch != '\n')
347 		;
348 	if (*(mp - 2) == '\\')
349 		mp--;
350 	*mp = '\0';
351 
352 	free(s);
353 	return (m);
354 }
355 
356 void
357 score(u_int r, u_int w, u_int g)
358 {
359 	(void)printf("Rights %d, wrongs %d,", r, w);
360 	if (g)
361 		(void)printf(" extra guesses %d,", g);
362 	(void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0);
363 }
364 
365 void
366 downcase(char *p)
367 {
368 	int ch;
369 
370 	for (; (ch = *p) != '\0'; ++p)
371 		if (isascii(ch) && isupper(ch))
372 			*p = tolower(ch);
373 }
374 
375 void
376 usage(void)
377 {
378 	(void)fprintf(stderr,
379 	    "usage: quiz [-t] [-i file] category1 category2\n");
380 	exit(1);
381 }
382