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