]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - quiz/quiz.c
2 * Copyright (c) 1991 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
6 * Jim R. Oldroyd at The Instruction Set.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
40 All rights reserved.\n";
44 /*static char sccsid[] = "from: @(#)quiz.c 5.1 (Berkeley) 11/10/91";*/
45 static char rcsid
[] = "$Id: quiz.c,v 1.6 1994/04/08 08:33:13 pk Exp $";
48 #include <sys/types.h>
57 #include "pathnames.h"
60 static int catone
, cattwo
, tflag
;
63 char *appdstr
__P((char *, char *));
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] == '\\') {
135 qp
->q_text
= appdstr(qp
->q_text
, lp
);
137 if ((qp
->q_next
= malloc(sizeof(QE
))) == NULL
)
141 if ((qp
->q_text
= strdup(lp
)) == NULL
)
143 qp
->q_asked
= qp
->q_answered
= FALSE
;
155 register char *p
, *s
;
158 if ((pf
= popen(_PATH_PAGER
, "w")) == NULL
)
159 err(1, "%s", _PATH_PAGER
);
160 (void)fprintf(pf
, "Subjects:\n\n");
161 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
162 for (s
= next_cat(qp
->q_text
); s
; s
= next_cat(s
)) {
164 errx(1, "%s", rxperr
);
165 if (p
= rxp_expand())
166 (void)fprintf(pf
, "%s ", p
);
168 (void)fprintf(pf
, "\n");
170 (void)fprintf(pf
, "\n%s\n%s\n%s\n",
171 "For example, \"quiz victim killer\" prints a victim's name and you reply",
172 "with the killer, and \"quiz killer victim\" works the other way around.",
173 "Type an empty line to get the correct answer.");
187 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
188 s
= next_cat(qp
->q_text
);
189 catone
= cattwo
= i
= 0;
192 errx(1, "%s", rxperr
);
200 if (catone
&& cattwo
&& catone
!= cattwo
) {
201 if (!rxp_compile(qp
->q_text
))
202 errx(1, "%s", rxperr
);
203 get_file(rxp_expand());
207 errx(1, "invalid categories");
215 u_int guesses
, rights
, wrongs
;
217 char *s
, *t
, question
[LINE_SZ
];
221 guesses
= rights
= wrongs
= 0;
225 next
= random() % qsize
;
227 for (i
= 0; i
< next
; i
++)
229 while (qp
&& qp
->q_answered
)
235 if (tflag
&& random() % 100 > 20) {
236 /* repeat questions in tutorial mode */
237 while (qp
&& (!qp
->q_asked
|| qp
->q_answered
))
243 for (i
= 0; i
< catone
- 1; i
++)
246 errx(1, "%s", rxperr
);
248 if (!t
|| *t
== '\0') {
249 qp
->q_answered
= TRUE
;
252 (void)strcpy(question
, t
);
254 for (i
= 0; i
< cattwo
- 1; i
++)
257 errx(1, "%s", rxperr
);
259 if (!t
|| *t
== '\0') {
260 qp
->q_answered
= TRUE
;
264 (void)printf("%s?\n", question
);
266 if ((answer
= fgetln(stdin
, &len
)) == NULL
) {
267 score(rights
, wrongs
, guesses
);
270 answer
[len
- 1] = '\0';
272 if (rxp_match(answer
)) {
273 (void)printf("Right!\n");
275 qp
->q_answered
= TRUE
;
278 if (*answer
== '\0') {
279 (void)printf("%s\n", t
);
282 qp
->q_answered
= TRUE
;
285 (void)printf("What?\n");
288 score(rights
, wrongs
, guesses
);
312 register char *mp
, *sp
;
316 if ((m
= malloc(strlen(s
) + strlen(tp
) + 1)) == NULL
)
318 for (mp
= m
, sp
= s
; *mp
++ = *sp
++;);
321 if (*(mp
- 1) == '\\')
323 while ((ch
= *mp
++ = *tp
++) && ch
!= '\n');
334 (void)printf("Rights %d, wrongs %d,", r
, w
);
336 (void)printf(" extra guesses %d,", g
);
337 (void)printf(" score %d%%\n", (r
+ w
+ g
) ? r
* 100 / (r
+ w
+ g
) : 0);
347 if (isascii(ch
) && isupper(ch
))
354 (void)fprintf(stderr
, "quiz [-t] [-i file] category1 category2\n");