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