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