]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - quiz/quiz.c
1 /* $NetBSD: quiz.c,v 1.13 1998/09/13 15:27:29 hubertf Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
9 * Commodore Business Machines.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
42 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
48 static char sccsid
[] = "@(#)quiz.c 8.3 (Berkeley) 5/4/95";
50 __RCSID("$NetBSD: quiz.c,v 1.13 1998/09/13 15:27:29 hubertf Exp $");
54 #include <sys/types.h>
66 #include "pathnames.h"
69 static int catone
, cattwo
, tflag
;
72 char *appdstr
__P((char *, char *, size_t));
73 void downcase
__P((char *));
74 void get_cats
__P((char *, char *));
75 void get_file
__P((char *));
76 int main
__P((int, char *[]));
77 char *next_cat
__P((char *));
78 void quiz
__P((void));
79 void score
__P((u_int
, u_int
, u_int
));
80 void show_index
__P((void));
81 void usage
__P((void)) __attribute__((__noreturn__
));
91 indexfile
= _PATH_QUIZIDX
;
92 while ((ch
= getopt(argc
, argv
, "i:t")) != -1)
114 get_cats(argv
[0], argv
[1]);
132 if ((fp
= fopen(file
, "r")) == NULL
)
137 * Should really free up space from any earlier read list
138 * but there are no reverse pointers to do so with.
142 while ((lp
= fgetln(fp
, &len
)) != NULL
) {
143 if (lp
[len
- 1] == '\n')
145 if (qp
->q_text
&& qp
->q_text
[strlen(qp
->q_text
) - 1] == '\\')
146 qp
->q_text
= appdstr(qp
->q_text
, lp
, len
);
148 if ((qp
->q_next
= malloc(sizeof(QE
))) == NULL
)
151 if ((qp
->q_text
= malloc(len
+ 1)) == NULL
)
153 strncpy(qp
->q_text
, lp
, len
);
154 qp
->q_text
[len
] = '\0';
155 qp
->q_asked
= qp
->q_answered
= FALSE
;
170 if ((pf
= popen(_PATH_PAGER
, "w")) == NULL
)
171 err(1, "%s", _PATH_PAGER
);
172 (void)fprintf(pf
, "Subjects:\n\n");
173 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
174 for (s
= next_cat(qp
->q_text
); s
; s
= next_cat(s
)) {
176 errx(1, "%s", rxperr
);
177 if ((p
= rxp_expand()) != NULL
)
178 (void)fprintf(pf
, "%s ", p
);
180 (void)fprintf(pf
, "\n");
182 (void)fprintf(pf
, "\n%s\n%s\n%s\n",
183 "For example, \"quiz victim killer\" prints a victim's name and you reply",
184 "with the killer, and \"quiz killer victim\" works the other way around.",
185 "Type an empty line to get the correct answer.");
199 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
200 s
= next_cat(qp
->q_text
);
201 catone
= cattwo
= i
= 0;
204 errx(1, "%s", rxperr
);
212 if (catone
&& cattwo
&& catone
!= cattwo
) {
213 if (!rxp_compile(qp
->q_text
))
214 errx(1, "%s", rxperr
);
215 get_file(rxp_expand());
219 errx(1, "invalid categories");
228 u_int guesses
, rights
, wrongs
;
230 char *answer
, *s
, *t
, question
[LINE_SZ
];
233 guesses
= rights
= wrongs
= 0;
237 next
= random() % qsize
;
239 for (i
= 0; i
< next
; i
++)
241 while (qp
&& qp
->q_answered
)
247 if (tflag
&& random() % 100 > 20) {
248 /* repeat questions in tutorial mode */
249 while (qp
&& (!qp
->q_asked
|| qp
->q_answered
))
255 for (i
= 0; i
< catone
- 1; i
++)
258 errx(1, "%s", rxperr
);
260 if (!t
|| *t
== '\0') {
261 qp
->q_answered
= TRUE
;
264 (void)strcpy(question
, t
);
266 for (i
= 0; i
< cattwo
- 1; i
++)
269 errx(1, "%s", rxperr
);
271 if (!t
|| *t
== '\0') {
272 qp
->q_answered
= TRUE
;
276 (void)printf("%s?\n", question
);
278 if ((answer
= fgetln(stdin
, &len
)) == NULL
||
279 answer
[len
- 1] != '\n') {
280 score(rights
, wrongs
, guesses
);
283 answer
[len
- 1] = '\0';
285 if (rxp_match(answer
)) {
286 (void)printf("Right!\n");
288 qp
->q_answered
= TRUE
;
291 if (*answer
== '\0') {
292 (void)printf("%s\n", t
);
295 qp
->q_answered
= TRUE
;
298 (void)printf("What?\n");
301 score(rights
, wrongs
, guesses
);
338 if ((m
= malloc(strlen(s
) + len
+ 1)) == NULL
)
340 for (mp
= m
, sp
= s
; (*mp
++ = *sp
++) != NULL
; )
343 if (*(mp
- 1) == '\\')
346 while ((ch
= *mp
++ = *tp
++) && ch
!= '\n')
358 (void)printf("Rights %d, wrongs %d,", r
, w
);
360 (void)printf(" extra guesses %d,", g
);
361 (void)printf(" score %d%%\n", (r
+ w
+ g
) ? r
* 100 / (r
+ w
+ g
) : 0);
370 for (; (ch
= *p
) != '\0'; ++p
)
371 if (isascii(ch
) && isupper(ch
))
378 (void)fprintf(stderr
, "quiz [-t] [-i file] category1 category2\n");