]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - quiz/quiz.c
1 /* $NetBSD: quiz.c,v 1.10 1997/01/07 12:27:30 tls 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
41 static char copyright
[] =
42 "@(#) 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 static char rcsid
[] = "$NetBSD: quiz.c,v 1.10 1997/01/07 12:27:30 tls 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 char *next_cat
__P((char *));
77 void quiz
__P((void));
78 void score
__P((u_int
, u_int
, u_int
));
79 void show_index
__P((void));
80 void usage
__P((void));
90 indexfile
= _PATH_QUIZIDX
;
91 while ((ch
= getopt(argc
, argv
, "i:t")) != EOF
)
113 get_cats(argv
[0], argv
[1]);
131 if ((fp
= fopen(file
, "r")) == NULL
)
136 * Should really free up space from any earlier read list
137 * but there are no reverse pointers to do so with.
141 while ((lp
= fgetln(fp
, &len
)) != NULL
) {
143 if (qp
->q_text
&& qp
->q_text
[strlen(qp
->q_text
) - 1] == '\\')
144 qp
->q_text
= appdstr(qp
->q_text
, lp
, len
);
146 if ((qp
->q_next
= malloc(sizeof(QE
))) == NULL
)
149 if ((qp
->q_text
= strdup(lp
)) == NULL
)
151 qp
->q_asked
= qp
->q_answered
= FALSE
;
163 register char *p
, *s
;
166 if ((pf
= popen(_PATH_PAGER
, "w")) == NULL
)
167 err(1, "%s", _PATH_PAGER
);
168 (void)fprintf(pf
, "Subjects:\n\n");
169 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
170 for (s
= next_cat(qp
->q_text
); s
; s
= next_cat(s
)) {
172 errx(1, "%s", rxperr
);
173 if (p
= rxp_expand())
174 (void)fprintf(pf
, "%s ", p
);
176 (void)fprintf(pf
, "\n");
178 (void)fprintf(pf
, "\n%s\n%s\n%s\n",
179 "For example, \"quiz victim killer\" prints a victim's name and you reply",
180 "with the killer, and \"quiz killer victim\" works the other way around.",
181 "Type an empty line to get the correct answer.");
195 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
196 s
= next_cat(qp
->q_text
);
197 catone
= cattwo
= i
= 0;
200 errx(1, "%s", rxperr
);
208 if (catone
&& cattwo
&& catone
!= cattwo
) {
209 if (!rxp_compile(qp
->q_text
))
210 errx(1, "%s", rxperr
);
211 get_file(rxp_expand());
215 errx(1, "invalid categories");
224 u_int guesses
, rights
, wrongs
;
226 char *answer
, *s
, *t
, question
[LINE_SZ
];
229 guesses
= rights
= wrongs
= 0;
233 next
= random() % qsize
;
235 for (i
= 0; i
< next
; i
++)
237 while (qp
&& qp
->q_answered
)
243 if (tflag
&& random() % 100 > 20) {
244 /* repeat questions in tutorial mode */
245 while (qp
&& (!qp
->q_asked
|| qp
->q_answered
))
251 for (i
= 0; i
< catone
- 1; i
++)
254 errx(1, "%s", rxperr
);
256 if (!t
|| *t
== '\0') {
257 qp
->q_answered
= TRUE
;
260 (void)strcpy(question
, t
);
262 for (i
= 0; i
< cattwo
- 1; i
++)
265 errx(1, "%s", rxperr
);
267 if (!t
|| *t
== '\0') {
268 qp
->q_answered
= TRUE
;
272 (void)printf("%s?\n", question
);
274 if ((answer
= fgetln(stdin
, &len
)) == NULL
) {
275 score(rights
, wrongs
, guesses
);
278 answer
[len
- 1] = '\0';
280 if (rxp_match(answer
)) {
281 (void)printf("Right!\n");
283 qp
->q_answered
= TRUE
;
286 if (*answer
== '\0') {
287 (void)printf("%s\n", t
);
290 qp
->q_answered
= TRUE
;
293 (void)printf("What?\n");
296 score(rights
, wrongs
, guesses
);
321 register char *mp
, *sp
;
325 if ((m
= malloc(strlen(s
) + len
+ 1)) == NULL
)
327 for (mp
= m
, sp
= s
; *mp
++ = *sp
++;);
329 if (*(mp
- 1) == '\\')
332 while ((ch
= *mp
++ = *tp
++) && ch
!= '\n');
343 (void)printf("Rights %d, wrongs %d,", r
, w
);
345 (void)printf(" extra guesses %d,", g
);
346 (void)printf(" score %d%%\n", (r
+ w
+ g
) ? r
* 100 / (r
+ w
+ g
) : 0);
356 if (isascii(ch
) && isupper(ch
))
363 (void)fprintf(stderr
, "quiz [-t] [-i file] category1 category2\n");