xref: /original-bsd/usr.bin/learn/learn/dounit.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)dounit.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "stdio.h"
13 #include "lrnref.h"
14 
15 int	remind = 2;		/* to remind user of "again" and "bye" */
16 extern	int	noclobber;
17 
18 dounit()
19 {
20 	char tbuff[100];
21 
22 	if (todo == 0)
23 		return;
24 	wrong = 0;
25 retry:
26 	if (!noclobber)
27 		lstart(todo);		/* clean up play directory */
28 	sprintf(tbuff, "%s/%s/L%s", direct, sname, todo); /* script = lesson */
29 	scrin = fopen(tbuff, "r");
30 	if (scrin == NULL) {
31 		perror(tbuff);
32 		fprintf(stderr, "Dounit:  no lesson %s.\n", tbuff);
33 		wrapup(1);
34 	}
35 
36 	copy(0, scrin);			/* print lesson, usually */
37 	if (more == 0)
38 		return;
39 	copy(1, stdin);			/* user takes over */
40 	if (skip)
41 		setdid(todo, sequence++);
42 	if (again || skip)		/* if "again" or "skip" */
43 		return;
44 	if (more == 0)
45 		return;
46 	copy(0, scrin);			/* evaluate user's response */
47 
48 	if (comfile >= 0)
49 		close(comfile);
50 	wait(&didok);
51 	didok = (status == 0);
52 	if (!didok) {
53 		wrong++;
54 		printf("\nSorry, that's %snot right.  Do you want to try again?  ",
55 			wrong > 1 ? "still " : "");
56 		fflush(stdout);
57 		for(;;) {
58 			gets(tbuff);
59 			if (tbuff[0] == 'y') {
60 				printf("Try the problem again.\n");
61 				if (remind--) {
62 					printf("[ Whenever you want to re-read the lesson, type \"again\".\n");
63 					printf("  You can always leave learn by typing \"bye\". ]\n");
64 				}
65 				goto retry;
66 			} else if (strcmp(tbuff, "bye") == 0) {
67 				wrapup(0);
68 			} else if (tbuff[0] == 'n') {
69 				wrong = 0;
70 				printf("\nOK.  That was lesson %s.\n", todo);
71 				printf("Skipping to next lesson.\n\n");
72 				fflush(stdout);
73 				break;
74 			} else {
75 				printf("\nPlease type yes, no or bye:  ");
76 				clearerr(stdin);
77 				fflush(stdout);
78 			}
79 		}
80 	}
81 	setdid(todo, sequence++);
82 }
83 
84