]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - quiz/quiz.c
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
39 static char copyright
[] =
40 "@(#) Copyright (c) 1991, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid
[] = "@(#)quiz.c 8.2 (Berkeley) 1/3/94";
48 #include <sys/types.h>
57 #include "pathnames.h"
60 static int catone
, cattwo
, tflag
;
63 char *appdstr
__P((char *, char *, size_t));
64 void downcase
__P((char *));
65 void get_cats
__P((char *, char *));
66 void get_file
__P((char *));
67 char *next_cat
__P((char *));
68 void quiz
__P((void));
69 void score
__P((u_int
, u_int
, u_int
));
70 void show_index
__P((void));
71 void usage
__P((void));
81 indexfile
= _PATH_QUIZIDX
;
82 while ((ch
= getopt(argc
, argv
, "i:t")) != EOF
)
104 get_cats(argv
[0], argv
[1]);
122 if ((fp
= fopen(file
, "r")) == NULL
)
127 * Should really free up space from any earlier read list
128 * but there are no reverse pointers to do so with.
132 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
)
140 if ((qp
->q_text
= strdup(lp
)) == NULL
)
142 qp
->q_asked
= qp
->q_answered
= FALSE
;
154 register char *p
, *s
;
157 if ((pf
= popen(_PATH_PAGER
, "w")) == NULL
)
158 err(1, "%s", _PATH_PAGER
);
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
)) {
163 errx(1, "%s", rxperr
);
164 if (p
= rxp_expand())
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.");
186 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
187 s
= next_cat(qp
->q_text
);
188 catone
= cattwo
= i
= 0;
191 errx(1, "%s", rxperr
);
199 if (catone
&& cattwo
&& catone
!= cattwo
) {
200 if (!rxp_compile(qp
->q_text
))
201 errx(1, "%s", rxperr
);
202 get_file(rxp_expand());
206 errx(1, "invalid categories");
215 u_int guesses
, rights
, wrongs
;
217 char *answer
, *s
, *t
, question
[LINE_SZ
];
220 guesses
= rights
= wrongs
= 0;
224 next
= random() % qsize
;
226 for (i
= 0; i
< next
; i
++)
228 while (qp
&& qp
->q_answered
)
234 if (tflag
&& random() % 100 > 20) {
235 /* repeat questions in tutorial mode */
236 while (qp
&& (!qp
->q_asked
|| qp
->q_answered
))
242 for (i
= 0; i
< catone
- 1; i
++)
245 errx(1, "%s", rxperr
);
247 if (!t
|| *t
== '\0') {
248 qp
->q_answered
= TRUE
;
251 (void)strcpy(question
, t
);
253 for (i
= 0; i
< cattwo
- 1; i
++)
256 errx(1, "%s", rxperr
);
258 if (!t
|| *t
== '\0') {
259 qp
->q_answered
= TRUE
;
263 (void)printf("%s?\n", question
);
265 if ((answer
= fgetln(stdin
, &len
)) == NULL
) {
266 score(rights
, wrongs
, guesses
);
269 answer
[len
- 1] = '\0';
271 if (rxp_match(answer
)) {
272 (void)printf("Right!\n");
274 qp
->q_answered
= TRUE
;
277 if (*answer
== '\0') {
278 (void)printf("%s\n", t
);
281 qp
->q_answered
= TRUE
;
284 (void)printf("What?\n");
287 score(rights
, wrongs
, guesses
);
312 register char *mp
, *sp
;
316 if ((m
= malloc(strlen(s
) + len
+ 1)) == NULL
)
318 for (mp
= m
, sp
= s
; *mp
++ = *sp
++;);
320 if (*(mp
- 1) == '\\')
322 while ((ch
= *mp
++ = *tp
++) && ch
!= '\n');
333 (void)printf("Rights %d, wrongs %d,", r
, w
);
335 (void)printf(" extra guesses %d,", g
);
336 (void)printf(" score %d%%\n", (r
+ w
+ g
) ? r
* 100 / (r
+ w
+ g
) : 0);
346 if (isascii(ch
) && isupper(ch
))
353 (void)fprintf(stderr
, "quiz [-t] [-i file] category1 category2\n");