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