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