2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
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 $
40 * $DragonFly: src/games/quiz/quiz.c,v 1.6 2007/09/08 01:29:14 corecode Exp $
43 #include <sys/types.h>
54 #include "pathnames.h"
57 static int catone, cattwo, tflag;
60 char *appdstr (char *, char *, size_t);
61 void downcase (char *);
62 void err (const char *, ...);
63 void get_cats (char *, char *);
64 void get_file (const char *);
65 char *next_cat (char *);
67 void score (u_int, u_int, u_int);
68 void show_index (void);
72 main(int argc, char **argv)
75 const char *indexfile;
80 indexfile = _PATH_QUIZIDX;
81 while ((ch = getopt(argc, argv, "i:t")) != -1)
103 get_cats(argv[0], argv[1]);
113 get_file(const char *file)
120 if ((fp = fopen(file, "r")) == NULL)
121 err("%s: %s", file, strerror(errno));
125 * Should really free up space from any earlier read list
126 * but there are no reverse pointers to do so with.
130 while ((lp = fgetln(fp, &len)) != NULL) {
133 if (qp->q_text && qp->q_text[strlen(qp->q_text) - 1] == '\\')
134 qp->q_text = appdstr(qp->q_text, lp, len);
136 if ((qp->q_next = malloc(sizeof(QE))) == NULL)
137 err("%s", strerror(errno));
140 if ((qp->q_text = strdup(lp)) == NULL)
141 err("%s", strerror(errno));
142 qp->q_asked = qp->q_answered = FALSE;
157 if ((pf = popen(_PATH_PAGER, "w")) == NULL)
158 err("%s: %s", _PATH_PAGER, strerror(errno));
159 (void)fprintf(pf, "Subjects:\n\n");
160 for (qp = qlist.q_next; qp; qp = qp->q_next) {
161 for (s = next_cat(qp->q_text); s; s = next_cat(s)) {
164 if ((p = rxp_expand()) != '\0')
165 (void)fprintf(pf, "%s ", p);
167 (void)fprintf(pf, "\n");
169 (void)fprintf(pf, "\n%s\n%s\n%s\n",
170 "For example, \"quiz victim killer\" prints a victim's name and you reply",
171 "with the killer, and \"quiz killer victim\" works the other way around.",
172 "Type an empty line to get the correct answer.");
177 get_cats(char *cat1, char *cat2)
185 for (qp = qlist.q_next; qp; qp = qp->q_next) {
186 s = next_cat(qp->q_text);
187 catone = cattwo = i = 0;
198 if (catone && cattwo && catone != cattwo) {
199 if (!rxp_compile(qp->q_text))
201 get_file(rxp_expand());
205 err("invalid categories");
214 u_int guesses, rights, wrongs;
216 char *answer, *s, *t, question[LINE_SZ];
219 guesses = rights = wrongs = 0;
223 next = random() % qsize;
225 for (i = 0; i < next; i++)
227 while (qp && qp->q_answered)
233 if (tflag && random() % 100 > 20) {
234 /* repeat questions in tutorial mode */
235 while (qp && (!qp->q_asked || qp->q_answered))
241 for (i = 0; i < catone - 1; i++)
246 if (!t || *t == '\0') {
247 qp->q_answered = TRUE;
250 (void)strcpy(question, t);
252 for (i = 0; i < cattwo - 1; i++)
257 if (!t || *t == '\0') {
258 qp->q_answered = TRUE;
262 (void)printf("%s?\n", question);
264 if ((answer = fgetln(stdin, &len)) == NULL) {
265 score(rights, wrongs, guesses);
268 answer[len - 1] = '\0';
270 if (rxp_match(answer)) {
271 (void)printf("Right!\n");
273 qp->q_answered = TRUE;
276 if (*answer == '\0') {
277 (void)printf("%s\n", t);
280 qp->q_answered = TRUE;
283 (void)printf("What?\n");
286 score(rights, wrongs, guesses);
306 appdstr(char *s, char *tp, size_t len)
311 if ((m = malloc(strlen(s) + len + 1)) == NULL)
312 err("%s", strerror(errno));
315 for (; (*mp++ = *sp++););
318 if (*(mp - 1) == '\\')
322 if (mp[len - 1] == '\n')
330 score(u_int r, u_int w, u_int g)
332 (void)printf("Rights %d, wrongs %d,", r, w);
334 (void)printf(" extra guesses %d,", g);
335 (void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0);
343 for (; (ch = *p); ++p)
344 if (isascii(ch) && isupper(ch))
351 (void)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n");
358 err(const char *fmt, ...)
362 (void)fprintf(stderr, "quiz: ");
363 (void)vfprintf(stderr, fmt, ap);
365 (void)fprintf(stderr, "\n");