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