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