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