147862Sbostic /*-
260836Sbostic * Copyright (c) 1991, 1993
360836Sbostic * The Regents of the University of California. All rights reserved.
451609Sbostic *
551609Sbostic * This code is derived from software contributed to Berkeley by
652145Sbostic * Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
752145Sbostic * Commodore Business Machines.
851609Sbostic *
951609Sbostic * %sccs.include.redist.c%
1047862Sbostic */
118867Smckusick
1247862Sbostic #ifndef lint
1360836Sbostic static char copyright[] =
1460836Sbostic "@(#) Copyright (c) 1991, 1993\n\
1560836Sbostic The Regents of the University of California. All rights reserved.\n";
1647862Sbostic #endif /* not lint */
178867Smckusick
1851609Sbostic #ifndef lint
19*69224Sbostic static char sccsid[] = "@(#)quiz.c 8.3 (Berkeley) 05/04/95";
2051609Sbostic #endif /* not lint */
2151609Sbostic
2251609Sbostic #include <sys/types.h>
23*69224Sbostic
24*69224Sbostic #include <ctype.h>
2551609Sbostic #include <errno.h>
268867Smckusick #include <stdio.h>
2751609Sbostic #include <stdlib.h>
2851609Sbostic #include <string.h>
29*69224Sbostic #include <time.h>
30*69224Sbostic #include <unistd.h>
31*69224Sbostic
3251609Sbostic #include "quiz.h"
3341774Sbostic #include "pathnames.h"
3441774Sbostic
3551609Sbostic static QE qlist;
3651609Sbostic static int catone, cattwo, tflag;
3751609Sbostic static u_int qsize;
388867Smckusick
3958443Sbostic char *appdstr __P((char *, char *, size_t));
4051609Sbostic void downcase __P((char *));
4151609Sbostic void err __P((const char *, ...));
4251609Sbostic void get_cats __P((char *, char *));
4351609Sbostic void get_file __P((char *));
4451609Sbostic char *next_cat __P((char *));
4551609Sbostic void quiz __P((void));
4651609Sbostic void score __P((u_int, u_int, u_int));
4751609Sbostic void show_index __P((void));
4851609Sbostic void usage __P((void));
498867Smckusick
5051609Sbostic int
main(argc,argv)5151609Sbostic main(argc, argv)
5251609Sbostic int argc;
5351609Sbostic char *argv[];
548867Smckusick {
5533196Sbostic register int ch;
5651609Sbostic char *indexfile;
578867Smckusick
5851609Sbostic indexfile = _PATH_QUIZIDX;
5951609Sbostic while ((ch = getopt(argc, argv, "i:t")) != EOF)
6051609Sbostic switch(ch) {
6151609Sbostic case 'i':
6251609Sbostic indexfile = optarg;
6351609Sbostic break;
6451609Sbostic case 't':
6551609Sbostic tflag = 1;
6651609Sbostic break;
6751609Sbostic case '?':
688867Smckusick default:
6951609Sbostic usage();
708867Smckusick }
7151609Sbostic argc -= optind;
7251609Sbostic argv += optind;
738867Smckusick
7451609Sbostic switch(argc) {
7551609Sbostic case 0:
7651609Sbostic get_file(indexfile);
7751609Sbostic show_index();
7851609Sbostic break;
7951609Sbostic case 2:
8051609Sbostic get_file(indexfile);
8151609Sbostic get_cats(argv[0], argv[1]);
8251609Sbostic quiz();
8351609Sbostic break;
8451609Sbostic default:
8551609Sbostic usage();
868867Smckusick }
8751609Sbostic exit(0);
888867Smckusick }
898867Smckusick
9051609Sbostic void
get_file(file)9151609Sbostic get_file(file)
9251609Sbostic char *file;
938867Smckusick {
9451609Sbostic register FILE *fp;
9551609Sbostic register QE *qp;
9651609Sbostic size_t len;
9751609Sbostic char *lp;
988867Smckusick
9951609Sbostic if ((fp = fopen(file, "r")) == NULL)
10051609Sbostic err("%s: %s", file, strerror(errno));
1018867Smckusick
10251609Sbostic /*
10351609Sbostic * XXX
10451609Sbostic * Should really free up space from any earlier read list
10551609Sbostic * but there are no reverse pointers to do so with.
10651609Sbostic */
10751609Sbostic qp = &qlist;
10851609Sbostic qsize = 0;
10965360Sbostic while ((lp = fgetln(fp, &len)) != NULL) {
11051609Sbostic if (qp->q_text && qp->q_text[strlen(qp->q_text) - 1] == '\\')
11158443Sbostic qp->q_text = appdstr(qp->q_text, lp, len);
11251609Sbostic else {
11351609Sbostic if ((qp->q_next = malloc(sizeof(QE))) == NULL)
11451609Sbostic err("%s", strerror(errno));
11551609Sbostic qp = qp->q_next;
11658443Sbostic lp[len - 1] = '\0';
11751609Sbostic if ((qp->q_text = strdup(lp)) == NULL)
11851609Sbostic err("%s", strerror(errno));
11951609Sbostic qp->q_asked = qp->q_answered = FALSE;
12051609Sbostic qp->q_next = NULL;
12151609Sbostic ++qsize;
1228867Smckusick }
1238867Smckusick }
12451609Sbostic (void)fclose(fp);
1258867Smckusick }
1268867Smckusick
12751609Sbostic void
show_index()12851609Sbostic show_index()
1298867Smckusick {
13051609Sbostic register QE *qp;
13151609Sbostic register char *p, *s;
13251609Sbostic FILE *pf;
1338867Smckusick
13451609Sbostic if ((pf = popen(_PATH_PAGER, "w")) == NULL)
13551609Sbostic err("%s: %s", _PATH_PAGER, strerror(errno));
13651609Sbostic (void)fprintf(pf, "Subjects:\n\n");
13751609Sbostic for (qp = qlist.q_next; qp; qp = qp->q_next) {
13851609Sbostic for (s = next_cat(qp->q_text); s; s = next_cat(s)) {
13951609Sbostic if (!rxp_compile(s))
14051609Sbostic err("%s", rxperr);
14151609Sbostic if (p = rxp_expand())
14251609Sbostic (void)fprintf(pf, "%s ", p);
1438867Smckusick }
14451609Sbostic (void)fprintf(pf, "\n");
1458867Smckusick }
14651609Sbostic (void)fprintf(pf, "\n%s\n%s\n%s\n",
14751609Sbostic "For example, \"quiz victim killer\" prints a victim's name and you reply",
14851609Sbostic "with the killer, and \"quiz killer victim\" works the other way around.",
14951609Sbostic "Type an empty line to get the correct answer.");
15051609Sbostic (void)pclose(pf);
1518867Smckusick }
1528867Smckusick
15351609Sbostic void
get_cats(cat1,cat2)15451609Sbostic get_cats(cat1, cat2)
15551609Sbostic char *cat1, *cat2;
1568867Smckusick {
15751609Sbostic register QE *qp;
15851609Sbostic int i;
15951609Sbostic char *s;
16051609Sbostic
16151609Sbostic downcase(cat1);
16251609Sbostic downcase(cat2);
16351609Sbostic for (qp = qlist.q_next; qp; qp = qp->q_next) {
16451609Sbostic s = next_cat(qp->q_text);
16551609Sbostic catone = cattwo = i = 0;
16651609Sbostic while (s) {
16751609Sbostic if (!rxp_compile(s))
16851609Sbostic err("%s", rxperr);
16951609Sbostic i++;
17051609Sbostic if (rxp_match(cat1))
17151609Sbostic catone = i;
17251609Sbostic if (rxp_match(cat2))
17351609Sbostic cattwo = i;
17451609Sbostic s = next_cat(s);
1758867Smckusick }
17651609Sbostic if (catone && cattwo && catone != cattwo) {
17751609Sbostic if (!rxp_compile(qp->q_text))
17851609Sbostic err("%s", rxperr);
17951609Sbostic get_file(rxp_expand());
18051609Sbostic return;
18151609Sbostic }
1828867Smckusick }
18351609Sbostic err("invalid categories");
1848867Smckusick }
1858867Smckusick
18651609Sbostic void
quiz()18751609Sbostic quiz()
1888867Smckusick {
18951609Sbostic register QE *qp;
19051609Sbostic register int i;
19158443Sbostic size_t len;
19251609Sbostic u_int guesses, rights, wrongs;
19351609Sbostic int next;
19458443Sbostic char *answer, *s, *t, question[LINE_SZ];
1958867Smckusick
19651609Sbostic srandom(time(NULL));
19751609Sbostic guesses = rights = wrongs = 0;
19851609Sbostic for (;;) {
19951609Sbostic if (qsize == 0)
2008867Smckusick break;
20151609Sbostic next = random() % qsize;
20251609Sbostic qp = qlist.q_next;
20351609Sbostic for (i = 0; i < next; i++)
20451609Sbostic qp = qp->q_next;
20551609Sbostic while (qp && qp->q_answered)
20651609Sbostic qp = qp->q_next;
20751609Sbostic if (!qp) {
20851609Sbostic qsize = next;
20951609Sbostic continue;
2108867Smckusick }
21151609Sbostic if (tflag && random() % 100 > 20) {
21251609Sbostic /* repeat questions in tutorial mode */
21351609Sbostic while (qp && (!qp->q_asked || qp->q_answered))
21451609Sbostic qp = qp->q_next;
21551609Sbostic if (!qp)
21651609Sbostic continue;
2178867Smckusick }
21851609Sbostic s = qp->q_text;
21951609Sbostic for (i = 0; i < catone - 1; i++)
22051609Sbostic s = next_cat(s);
22151609Sbostic if (!rxp_compile(s))
22251609Sbostic err("%s", rxperr);
22351609Sbostic t = rxp_expand();
22451609Sbostic if (!t || *t == '\0') {
22551609Sbostic qp->q_answered = TRUE;
2268867Smckusick continue;
2278867Smckusick }
22851609Sbostic (void)strcpy(question, t);
22951609Sbostic s = qp->q_text;
23051609Sbostic for (i = 0; i < cattwo - 1; i++)
23151609Sbostic s = next_cat(s);
23251609Sbostic if (!rxp_compile(s))
23351609Sbostic err("%s", rxperr);
23451609Sbostic t = rxp_expand();
23551609Sbostic if (!t || *t == '\0') {
23651609Sbostic qp->q_answered = TRUE;
23751609Sbostic continue;
23851609Sbostic }
23951609Sbostic qp->q_asked = TRUE;
24051609Sbostic (void)printf("%s?\n", question);
24151609Sbostic for (;; ++guesses) {
24265360Sbostic if ((answer = fgetln(stdin, &len)) == NULL) {
24351609Sbostic score(rights, wrongs, guesses);
24451609Sbostic exit(0);
24551609Sbostic }
24658443Sbostic answer[len - 1] = '\0';
24751609Sbostic downcase(answer);
24851609Sbostic if (rxp_match(answer)) {
24951609Sbostic (void)printf("Right!\n");
25051609Sbostic ++rights;
25151609Sbostic qp->q_answered = TRUE;
2528867Smckusick break;
2538867Smckusick }
25451609Sbostic if (*answer == '\0') {
25551609Sbostic (void)printf("%s\n", t);
25651609Sbostic ++wrongs;
25751609Sbostic if (!tflag)
25851609Sbostic qp->q_answered = TRUE;
2598867Smckusick break;
2608867Smckusick }
26151609Sbostic (void)printf("What?\n");
2628867Smckusick }
2638867Smckusick }
26451609Sbostic score(rights, wrongs, guesses);
2658867Smckusick }
2668867Smckusick
26751609Sbostic char *
next_cat(s)26851609Sbostic next_cat(s)
26951609Sbostic register char * s;
2708867Smckusick {
27151609Sbostic for (;;)
27251609Sbostic switch (*s++) {
27351609Sbostic case '\0':
27451609Sbostic return (NULL);
27551609Sbostic case '\\':
2768867Smckusick break;
27751609Sbostic case ':':
27851609Sbostic return (s);
2798867Smckusick }
28051609Sbostic /* NOTREACHED */
2818867Smckusick }
2828867Smckusick
28351609Sbostic char *
appdstr(s,tp,len)28458443Sbostic appdstr(s, tp, len)
28551609Sbostic char *s;
28651609Sbostic register char *tp;
28758443Sbostic size_t len;
2888867Smckusick {
28951609Sbostic register char *mp, *sp;
29051609Sbostic register int ch;
29151609Sbostic char *m;
29251609Sbostic
29358443Sbostic if ((m = malloc(strlen(s) + len + 1)) == NULL)
29451609Sbostic err("%s", strerror(errno));
29551609Sbostic for (mp = m, sp = s; *mp++ = *sp++;);
29651609Sbostic
29751609Sbostic if (*(mp - 1) == '\\')
29851609Sbostic --mp;
29951609Sbostic while ((ch = *mp++ = *tp++) && ch != '\n');
30051609Sbostic *mp = '\0';
30151609Sbostic
30251609Sbostic free(s);
30351609Sbostic return (m);
3048867Smckusick }
3058867Smckusick
30646755Sbostic void
score(r,w,g)30751609Sbostic score(r, w, g)
30851609Sbostic u_int r, w, g;
3098867Smckusick {
31051609Sbostic (void)printf("Rights %d, wrongs %d,", r, w);
31151609Sbostic if (g)
31251609Sbostic (void)printf(" extra guesses %d,", g);
31351609Sbostic (void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0);
3148867Smckusick }
31551609Sbostic
31651609Sbostic void
downcase(p)31751609Sbostic downcase(p)
31851609Sbostic register char *p;
3198867Smckusick {
32051609Sbostic register int ch;
32151609Sbostic
32251609Sbostic for (; ch = *p; ++p)
32351609Sbostic if (isascii(ch) && isupper(ch))
32451609Sbostic *p = tolower(ch);
3258867Smckusick }
3268867Smckusick
32751609Sbostic void
usage()32851609Sbostic usage()
32951609Sbostic {
33051609Sbostic (void)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n");
33151609Sbostic exit(1);
3328867Smckusick }
3338867Smckusick
33451609Sbostic #if __STDC__
33551609Sbostic #include <stdarg.h>
33651609Sbostic #else
33751609Sbostic #include <varargs.h>
33851609Sbostic #endif
33951609Sbostic
34051609Sbostic void
34151609Sbostic #if __STDC__
err(const char * fmt,...)34251609Sbostic err(const char *fmt, ...)
34351609Sbostic #else
34451609Sbostic err(fmt, va_alist)
34551609Sbostic char *fmt;
34651609Sbostic va_dcl
34751609Sbostic #endif
3488867Smckusick {
34951609Sbostic va_list ap;
35051609Sbostic #if __STDC__
35151609Sbostic va_start(ap, fmt);
35251609Sbostic #else
35351609Sbostic va_start(ap);
35451609Sbostic #endif
35551609Sbostic (void)fprintf(stderr, "quiz: ");
35651609Sbostic (void)vfprintf(stderr, fmt, ap);
35751609Sbostic va_end(ap);
35851609Sbostic (void)fprintf(stderr, "\n");
35951609Sbostic exit(1);
3608867Smckusick }
361