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