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