xref: /openbsd/games/quiz/quiz.c (revision 3d8817e4)
1 /*	$OpenBSD: quiz.c,v 1.20 2009/10/27 23:59:26 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 #include <sys/types.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <err.h>
44 #include <time.h>
45 #include <unistd.h>
46 #include "quiz.h"
47 #include "pathnames.h"
48 
49 static QE qlist;
50 static int catone, cattwo, tflag;
51 static u_int qsize;
52 
53 char	*appdstr(char *, const char *, size_t);
54 void	 downcase(char *);
55 void	 get_cats(char *, char *);
56 void	 get_file(const char *);
57 const char	*next_cat(const char *);
58 void	 quiz(void);
59 void	 score(u_int, u_int, u_int);
60 void	 show_index(void);
61 void	 usage(void);
62 
63 int
64 main(int argc, char *argv[])
65 {
66 	int ch;
67 	const char *indexfile;
68 
69 	indexfile = _PATH_QUIZIDX;
70 	while ((ch = getopt(argc, argv, "i:t")) != -1)
71 		switch(ch) {
72 		case 'i':
73 			indexfile = optarg;
74 			break;
75 		case 't':
76 			tflag = 1;
77 			break;
78 		case '?':
79 		default:
80 			usage();
81 		}
82 	argc -= optind;
83 	argv += optind;
84 
85 	switch(argc) {
86 	case 0:
87 		get_file(indexfile);
88 		show_index();
89 		break;
90 	case 2:
91 		get_file(indexfile);
92 		get_cats(argv[0], argv[1]);
93 		quiz();
94 		break;
95 	default:
96 		usage();
97 	}
98 	exit(0);
99 }
100 
101 void
102 get_file(const char *file)
103 {
104 	FILE *fp;
105 	QE *qp;
106 	size_t len;
107 	char *lp;
108 
109 	if ((fp = fopen(file, "r")) == NULL)
110 		err(1, "%s", file);
111 
112 	/*
113 	 * XXX
114 	 * Should really free up space from any earlier read list
115 	 * but there are no reverse pointers to do so with.
116 	 */
117 	qp = &qlist;
118 	qsize = 0;
119 	while ((lp = fgetln(fp, &len)) != NULL) {
120 		if (lp[len - 1] == '\n')
121 			--len;
122 		if (qp->q_text && qp->q_text[0] != '\0' &&
123 		    qp->q_text[strlen(qp->q_text) - 1] == '\\')
124 			qp->q_text = appdstr(qp->q_text, lp, len);
125 		else {
126 			if ((qp->q_next = malloc(sizeof(QE))) == NULL)
127 				errx(1, "malloc");
128 			qp = qp->q_next;
129 			if ((qp->q_text = malloc(len + 1)) == NULL)
130 				errx(1, "malloc");
131 			/* lp may not be zero-terminated; cannot use strlcpy */
132 			strncpy(qp->q_text, lp, len);
133 			qp->q_text[len] = '\0';
134 			qp->q_asked = qp->q_answered = FALSE;
135 			qp->q_next = NULL;
136 			++qsize;
137 		}
138 	}
139 	(void)fclose(fp);
140 }
141 
142 void
143 show_index(void)
144 {
145 	QE *qp;
146 	const char *p, *s;
147 	FILE *pf;
148 	const char *pager;
149 
150 	if (!isatty(1))
151 		pager = "/bin/cat";
152 	else if (!(pager = getenv("PAGER")) || (*pager == 0))
153 			pager = _PATH_PAGER;
154 	if ((pf = popen(pager, "w")) == NULL)
155 		err(1, "%s", pager);
156 	(void)fprintf(pf, "Subjects:\n\n");
157 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
158 		for (s = next_cat(qp->q_text); s; s = next_cat(s)) {
159 			if (!rxp_compile(s))
160 				errx(1, "%s", rxperr);
161 			if ((p = rxp_expand()))
162 				(void)fprintf(pf, "%s ", p);
163 		}
164 		(void)fprintf(pf, "\n");
165 	}
166 	(void)fprintf(pf, "\n%s\n%s\n%s\n",
167 "For example, \"quiz victim killer\" prints a victim's name and you reply",
168 "with the killer, and \"quiz killer victim\" works the other way around.",
169 "Type an empty line to get the correct answer.");
170 	(void)pclose(pf);
171 }
172 
173 void
174 get_cats(char *cat1, char *cat2)
175 {
176 	QE *qp;
177 	int i;
178 	const char *s;
179 
180 	downcase(cat1);
181 	downcase(cat2);
182 	for (qp = qlist.q_next; qp; qp = qp->q_next) {
183 		s = next_cat(qp->q_text);
184 		catone = cattwo = i = 0;
185 		while (s) {
186 			if (!rxp_compile(s))
187 				errx(1, "%s", rxperr);
188 			i++;
189 			if (rxp_match(cat1))
190 				catone = i;
191 			if (rxp_match(cat2))
192 				cattwo = i;
193 			s = next_cat(s);
194 		}
195 		if (catone && cattwo && catone != cattwo) {
196 			if (!rxp_compile(qp->q_text))
197 				errx(1, "%s", rxperr);
198 			get_file(rxp_expand());
199 			return;
200 		}
201 	}
202 	errx(1, "invalid categories");
203 }
204 
205 void
206 quiz(void)
207 {
208 	QE *qp;
209 	int i;
210 	size_t len;
211 	u_int guesses, rights, wrongs;
212 	int next;
213 	char *answer, *t, question[LINE_SZ];
214 	const char *s;
215 
216 	srandomdev();
217 	guesses = rights = wrongs = 0;
218 	for (;;) {
219 		if (qsize == 0)
220 			break;
221 		next = random() % qsize;
222 		qp = qlist.q_next;
223 		for (i = 0; i < next; i++)
224 			qp = qp->q_next;
225 		while (qp && qp->q_answered)
226 			qp = qp->q_next;
227 		if (!qp) {
228 			qsize = next;
229 			continue;
230 		}
231 		if (tflag && random() % 100 > 20) {
232 			/* repeat questions in tutorial mode */
233 			while (qp && (!qp->q_asked || qp->q_answered))
234 				qp = qp->q_next;
235 			if (!qp)
236 				continue;
237 		}
238 		s = qp->q_text;
239 		for (i = 0; i < catone - 1; i++)
240 			s = next_cat(s);
241 		if (!rxp_compile(s))
242 			errx(1, "%s", rxperr);
243 		t = rxp_expand();
244 		if (!t || *t == '\0') {
245 			qp->q_answered = TRUE;
246 			continue;
247 		}
248 		(void)strlcpy(question, t, sizeof question);
249 		s = qp->q_text;
250 		for (i = 0; i < cattwo - 1; i++)
251 			s = next_cat(s);
252 		if (s == NULL)
253 			errx(1, "too few fields in data file, line \"%s\"",
254 			    qp->q_text);
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 		qp->q_asked = TRUE;
263 		(void)printf("%s?\n", question);
264 		for (;; ++guesses) {
265 			if ((answer = fgetln(stdin, &len)) == NULL ||
266 			    answer[len - 1] != '\n') {
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 const char *
292 next_cat(const char *s)
293 {
294 	int esc;
295 
296 	if (s == NULL)
297 		return (NULL);
298 	esc = 0;
299 	for (;;)
300 		switch (*s++) {
301 		case '\0':
302 			return (NULL);
303 		case '\\':
304 			esc = 1;
305 			break;
306 		case ':':
307 			if (!esc)
308 				return (s);
309 		default:
310 			esc = 0;
311 			break;
312 		}
313 	/* NOTREACHED */
314 }
315 
316 char *
317 appdstr(char *s, const char *tp, size_t len)
318 {
319 	char *mp;
320 	const char *sp;
321 	int ch;
322 	char *m;
323 
324 	if ((m = malloc(strlen(s) + len + 1)) == NULL)
325 		errx(1, "malloc");
326 	for (mp = m, sp = s; (*mp++ = *sp++) != '\0'; )
327 		;
328 	--mp;
329 	if (*(mp - 1) == '\\')
330 		--mp;
331 
332 	while ((ch = *mp++ = *tp++) && ch != '\n')
333 		;
334 	if (*(mp - 2) == '\\')
335 		mp--;
336 	*mp = '\0';
337 
338 	free(s);
339 	return (m);
340 }
341 
342 void
343 score(u_int r, u_int w, u_int g)
344 {
345 	(void)printf("Rights %d, wrongs %d,", r, w);
346 	if (g)
347 		(void)printf(" extra guesses %d,", g);
348 	(void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0);
349 }
350 
351 void
352 downcase(char *p)
353 {
354 	int ch;
355 
356 	for (; (ch = *p) != '\0'; ++p)
357 		if (isascii(ch) && isupper(ch))
358 			*p = tolower(ch);
359 }
360 
361 void
362 usage(void)
363 {
364 	(void)fprintf(stderr,
365 	    "usage: quiz [-t] [-i file] category1 category2\n");
366 	exit(1);
367 }
368