quiz.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* $NetBSD: quiz.c,v 1.20 2004/01/27 20:30:30 jsm Exp $ */
  2. /*-
  3. * Copyright (c) 1991, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
  8. * Commodore Business Machines.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <sys/cdefs.h>
  35. #ifndef lint
  36. __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
  37. The Regents of the University of California. All rights reserved.\n");
  38. #endif /* not lint */
  39. #ifndef lint
  40. #if 0
  41. static char sccsid[] = "@(#)quiz.c 8.3 (Berkeley) 5/4/95";
  42. #else
  43. __RCSID("$NetBSD: quiz.c,v 1.20 2004/01/27 20:30:30 jsm Exp $");
  44. #endif
  45. #endif /* not lint */
  46. #include <sys/types.h>
  47. #include <ctype.h>
  48. #include <errno.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <ctype.h>
  53. #include <err.h>
  54. #include <time.h>
  55. #include <unistd.h>
  56. #include "quiz.h"
  57. #include "pathnames.h"
  58. static QE qlist;
  59. static int catone, cattwo, tflag;
  60. static u_int qsize;
  61. char *appdstr(char *, const char *, size_t);
  62. void downcase(char *);
  63. void get_cats(char *, char *);
  64. void get_file(const char *);
  65. int main(int, char *[]);
  66. const char *next_cat(const char *);
  67. void quiz(void);
  68. void score(u_int, u_int, u_int);
  69. void show_index(void);
  70. void usage(void) __attribute__((__noreturn__));
  71. int
  72. main(argc, argv)
  73. int argc;
  74. char *argv[];
  75. {
  76. int ch;
  77. const char *indexfile;
  78. /* Revoke setgid privileges */
  79. setregid(getgid(), getgid());
  80. indexfile = _PATH_QUIZIDX;
  81. while ((ch = getopt(argc, argv, "i:t")) != -1)
  82. switch(ch) {
  83. case 'i':
  84. indexfile = optarg;
  85. break;
  86. case 't':
  87. tflag = 1;
  88. break;
  89. case '?':
  90. default:
  91. usage();
  92. }
  93. argc -= optind;
  94. argv += optind;
  95. switch(argc) {
  96. case 0:
  97. get_file(indexfile);
  98. show_index();
  99. break;
  100. case 2:
  101. get_file(indexfile);
  102. get_cats(argv[0], argv[1]);
  103. quiz();
  104. break;
  105. default:
  106. usage();
  107. }
  108. exit(0);
  109. }
  110. void
  111. get_file(file)
  112. const char *file;
  113. {
  114. FILE *fp;
  115. QE *qp;
  116. size_t len;
  117. char *lp;
  118. if ((fp = fopen(file, "r")) == NULL)
  119. err(1, "%s", file);
  120. /*
  121. * XXX
  122. * Should really free up space from any earlier read list
  123. * but there are no reverse pointers to do so with.
  124. */
  125. qp = &qlist;
  126. qsize = 0;
  127. while ((lp = fgetln(fp, &len)) != NULL) {
  128. if (lp[len - 1] == '\n')
  129. lp[--len] = '\0';
  130. if (qp->q_text && qp->q_text[strlen(qp->q_text) - 1] == '\\')
  131. qp->q_text = appdstr(qp->q_text, lp, len);
  132. else {
  133. if ((qp->q_next = malloc(sizeof(QE))) == NULL)
  134. errx(1, "malloc");
  135. qp = qp->q_next;
  136. if ((qp->q_text = malloc(len + 1)) == NULL)
  137. errx(1, "malloc");
  138. strncpy(qp->q_text, lp, len);
  139. qp->q_text[len] = '\0';
  140. qp->q_asked = qp->q_answered = FALSE;
  141. qp->q_next = NULL;
  142. ++qsize;
  143. }
  144. }
  145. (void)fclose(fp);
  146. }
  147. void
  148. show_index()
  149. {
  150. QE *qp;
  151. const char *p, *s;
  152. FILE *pf;
  153. const char *pager;
  154. if (!isatty(1))
  155. pager = "cat";
  156. else {
  157. if (!(pager = getenv("PAGER")) || (*pager == 0))
  158. pager = _PATH_PAGER;
  159. }
  160. if ((pf = popen(pager, "w")) == NULL)
  161. err(1, "%s", pager);
  162. (void)fprintf(pf, "Subjects:\n\n");
  163. for (qp = qlist.q_next; qp; qp = qp->q_next) {
  164. for (s = next_cat(qp->q_text); s; s = next_cat(s)) {
  165. if (!rxp_compile(s))
  166. errx(1, "%s", rxperr);
  167. if ((p = rxp_expand()) != NULL)
  168. (void)fprintf(pf, "%s ", p);
  169. }
  170. (void)fprintf(pf, "\n");
  171. }
  172. (void)fprintf(pf, "\n%s\n%s\n%s\n",
  173. "For example, \"quiz victim killer\" prints a victim's name and you reply",
  174. "with the killer, and \"quiz killer victim\" works the other way around.",
  175. "Type an empty line to get the correct answer.");
  176. (void)pclose(pf);
  177. }
  178. void
  179. get_cats(cat1, cat2)
  180. char *cat1, *cat2;
  181. {
  182. QE *qp;
  183. int i;
  184. const char *s;
  185. downcase(cat1);
  186. downcase(cat2);
  187. for (qp = qlist.q_next; qp; qp = qp->q_next) {
  188. s = next_cat(qp->q_text);
  189. catone = cattwo = i = 0;
  190. while (s) {
  191. if (!rxp_compile(s))
  192. errx(1, "%s", rxperr);
  193. i++;
  194. if (rxp_match(cat1))
  195. catone = i;
  196. if (rxp_match(cat2))
  197. cattwo = i;
  198. s = next_cat(s);
  199. }
  200. if (catone && cattwo && catone != cattwo) {
  201. if (!rxp_compile(qp->q_text))
  202. errx(1, "%s", rxperr);
  203. get_file(rxp_expand());
  204. return;
  205. }
  206. }
  207. errx(1, "invalid categories");
  208. }
  209. void
  210. quiz()
  211. {
  212. QE *qp;
  213. int i;
  214. size_t len;
  215. u_int guesses, rights, wrongs;
  216. int next;
  217. char *answer, *t, question[LINE_SZ];
  218. const char *s;
  219. srandom(time(NULL));
  220. guesses = rights = wrongs = 0;
  221. for (;;) {
  222. if (qsize == 0)
  223. break;
  224. next = random() % qsize;
  225. qp = qlist.q_next;
  226. for (i = 0; i < next; i++)
  227. qp = qp->q_next;
  228. while (qp && qp->q_answered)
  229. qp = qp->q_next;
  230. if (!qp) {
  231. qsize = next;
  232. continue;
  233. }
  234. if (tflag && random() % 100 > 20) {
  235. /* repeat questions in tutorial mode */
  236. while (qp && (!qp->q_asked || qp->q_answered))
  237. qp = qp->q_next;
  238. if (!qp)
  239. continue;
  240. }
  241. s = qp->q_text;
  242. for (i = 0; i < catone - 1; i++)
  243. s = next_cat(s);
  244. if (!rxp_compile(s))
  245. errx(1, "%s", rxperr);
  246. t = rxp_expand();
  247. if (!t || *t == '\0') {
  248. qp->q_answered = TRUE;
  249. continue;
  250. }
  251. (void)strcpy(question, t);
  252. s = qp->q_text;
  253. for (i = 0; i < cattwo - 1; i++)
  254. s = next_cat(s);
  255. if (!rxp_compile(s))
  256. errx(1, "%s", rxperr);
  257. t = rxp_expand();
  258. if (!t || *t == '\0') {
  259. qp->q_answered = TRUE;
  260. continue;
  261. }
  262. qp->q_asked = TRUE;
  263. (void)printf("%s?\n", question);
  264. for (;; ++guesses) {
  265. if ((answer = fgetln(stdin, &len)) == NULL ||
  266. answer[len - 1] != '\n') {
  267. score(rights, wrongs, guesses);
  268. exit(0);
  269. }
  270. answer[len - 1] = '\0';
  271. downcase(answer);
  272. if (rxp_match(answer)) {
  273. (void)printf("Right!\n");
  274. ++rights;
  275. qp->q_answered = TRUE;
  276. break;
  277. }
  278. if (*answer == '\0') {
  279. (void)printf("%s\n", t);
  280. ++wrongs;
  281. if (!tflag)
  282. qp->q_answered = TRUE;
  283. break;
  284. }
  285. (void)printf("What?\n");
  286. }
  287. }
  288. score(rights, wrongs, guesses);
  289. }
  290. const char *
  291. next_cat(s)
  292. const char * s;
  293. {
  294. int esc;
  295. esc = 0;
  296. for (;;)
  297. switch (*s++) {
  298. case '\0':
  299. return (NULL);
  300. case '\\':
  301. esc = 1;
  302. break;
  303. case ':':
  304. if (!esc)
  305. return (s);
  306. default:
  307. esc = 0;
  308. break;
  309. }
  310. /* NOTREACHED */
  311. }
  312. char *
  313. appdstr(s, tp, len)
  314. char *s;
  315. const char *tp;
  316. size_t len;
  317. {
  318. char *mp;
  319. const char *sp;
  320. int ch;
  321. char *m;
  322. if ((m = malloc(strlen(s) + len + 1)) == NULL)
  323. errx(1, "malloc");
  324. for (mp = m, sp = s; (*mp++ = *sp++) != '\0'; )
  325. ;
  326. --mp;
  327. if (*(mp - 1) == '\\')
  328. --mp;
  329. while ((ch = *mp++ = *tp++) && ch != '\n')
  330. ;
  331. *mp = '\0';
  332. free(s);
  333. return (m);
  334. }
  335. void
  336. score(r, w, g)
  337. u_int r, w, g;
  338. {
  339. (void)printf("Rights %d, wrongs %d,", r, w);
  340. if (g)
  341. (void)printf(" extra guesses %d,", g);
  342. (void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0);
  343. }
  344. void
  345. downcase(p)
  346. char *p;
  347. {
  348. int ch;
  349. for (; (ch = *p) != '\0'; ++p)
  350. if (isascii(ch) && isupper(ch))
  351. *p = tolower(ch);
  352. }
  353. void
  354. usage()
  355. {
  356. (void)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n");
  357. exit(1);
  358. }