Bell 32V development
[unix-history] / usr / src / cmd / learn / copy.c
CommitLineData
bbddffb1
TL
1#include "stdio.h"
2#include "signal.h"
3#include "lrnref"
4
5char last[100];
6char logf[100];
7char subdir[100];
8extern char * ctime();
9
10copy(prompt, fin)
11FILE *fin;
12{
13 FILE *fout, *f;
14 char s[100], t[100], s1[100], *r, *tod;
15 char nm[30];
16 int *p, tv[2];
17 extern int intrpt(), *action();
18 extern char *wordb();
19 int nmatch = 0;
20
21 if (subdir[0]==0)
22 sprintf(subdir, "../../%s", sname);
23 for (;;) {
24 if (pgets(s, prompt, fin) == 0)
25 if (fin == stdin) {
26 /* fprintf(stderr, "Don't type control-D\n"); */
27 /* this didn't work out very well */
28 continue;
29 } else
30 break;
31 trim(s);
32 /* change the sequence %s to lesson directory */
33 /* if needed */
34 for (r = s; *r; r++)
35 if (*r == '%') {
36 sprintf(s1, s, subdir, subdir, subdir);
37 strcpy(s, s1);
38 break;
39 }
40 r = wordb(s, t);
41 p = action(t);
42 if (*p == ONCE) { /* some actions done only once per script */
43 if (wrong) { /* we are on 2nd time */
44 scopy(fin, NULL);
45 continue;
46 }
47 strcpy(s, r);
48 r = wordb(s, t);
49 p = action(t);
50 }
51 if (p == 0) {
52 if (comfile >= 0) {
53 write(comfile, s, strlen(s));
54 write(comfile, "\n", 1);
55 }
56 else {
57 signal(SIGINT, SIG_IGN);
58 status = mysys(s);
59 signal(SIGINT, intrpt);
60 }
61 if (incopy) {
62 fprintf(incopy, "%s\n", s);
63 strcpy(last, s);
64 }
65 continue;
66 }
67 switch (*p) {
68 case READY:
69 if (incopy && r) {
70 fprintf(incopy, "%s\n", r);
71 strcpy(last, r);
72 }
73 return;
74 case PRINT:
75 if (wrong)
76 scopy(fin, NULL); /* don't repeat message */
77 else if (r)
78 list(r);
79 else
80 scopy(fin, stdout);
81 break;
82 case NOP:
83 break;
84 case MATCH:
85 if (nmatch > 0) /* we have already passed */
86 scopy(fin, NULL);
87 else if ((status = strcmp(r, last)) == 0) { /* did we pass this time? */
88 nmatch++;
89 scopy(fin, stdout);
90 } else
91 scopy(fin, NULL);
92 break;
93 case BAD:
94 if (strcmp(r, last) == 0) {
95 scopy(fin, stdout);
96 } else
97 scopy(fin, NULL);
98 break;
99 case SUCCEED:
100 scopy(fin, (status == 0) ? stdout : NULL);
101 break;
102 case FAIL:
103 scopy(fin, (status != 0) ? stdout : NULL);
104 break;
105 case CREATE:
106 fout = fopen(r, "w");
107 scopy(fin, fout);
108 fclose(fout);
109 break;
110 case CMP:
111 status = cmp(r); /* contains two file names */
112 break;
113 case MV:
114 sprintf(nm, "%s/L%s.%s", subdir, todo, r);
115 fcopy(r, nm);
116 break;
117 case USER:
118 case NEXT:
119 more = 1;
120 return;
121 case COPYIN:
122 incopy = fopen(".copy", "w");
123 break;
124 case UNCOPIN:
125 fclose(incopy);
126 incopy = NULL;
127 break;
128 case COPYOUT:
129 maktee();
130 break;
131 case UNCOPOUT:
132 untee();
133 break;
134 case PIPE:
135 comfile = makpipe();
136 break;
137 case UNPIPE:
138 close(comfile);
139 wait(0);
140 comfile = -1;
141 break;
142 case YES:
143 case NO:
144 if (incopy) {
145 fprintf(incopy, "%s\n", s);
146 strcpy(last, s);
147 }
148 return;
149 case WHERE:
150 printf("You are in lesson %s\n", todo);
151 fflush(stdout);
152 break;
153 case BYE:
154 more=0;
155 return;
156 case CHDIR:
157 printf("cd not allowed\n");
158 fflush(stdout);
159 break;
160 case LEARN:
161 printf("You are already in learn.\n");
162 fflush(stdout);
163 break;
164 case LOG:
165 if (!logging)
166 break;
167 if (logf[0] == 0)
168 sprintf(logf, "%s/log/%s", direct, sname);
169 f = fopen( (r? r : logf), "a");
170 if (f == NULL)
171 break;
172 time(tv);
173 tod = ctime(tv);
174 tod[24] = 0;
175 fprintf(f, "%s L%-6s %s %2d %s\n", tod,
176 todo, status? "fail" : "pass", speed, pwline);
177 fclose(f);
178 break;
179 }
180 }
181 return;
182}
183
184pgets(s, prompt, f)
185FILE *f;
186{
187 if (prompt) {
188 if (comfile < 0)
189 printf("$ ");
190 fflush(stdout);
191 }
192 if (fgets(s, 100,f))
193 return(1);
194 else
195 return(0);
196}
197
198trim(s)
199char *s;
200{
201 while (*s)
202 s++;
203 if (*--s == '\n')
204 *s=0;
205}
206
207scopy(fi, fo) /* copy fi to fo until a line with # */
208FILE *fi, *fo;
209{
210 int c;
211
212 while ((c = getc(fi)) != '#' && c != EOF) {
213 do {
214 if (fo != NULL)
215 putc(c, fo);
216 if (c == '\n')
217 break;
218 } while ((c = getc(fi)) != EOF);
219 }
220 if (c == '#')
221 ungetc(c, fi);
222 fflush(fo);
223}
224
225cmp(r) /* compare two files for status */
226char *r;
227{
228 char *s;
229 FILE *f1, *f2;
230 int c1, c2, stat;
231
232 for (s = r; *s != ' ' && *s != '\0'; s++)
233 ;
234 *s++ = 0; /* r contains file 1 */
235 while (*s == ' ')
236 s++;
237 f1 = fopen(r, "r");
238 f2 = fopen(s, "r");
239 if (f1 == NULL || f2 == NULL)
240 return(1); /* failure */
241 stat = 0;
242 for (;;) {
243 c1 = getc(f1);
244 c2 = getc(f2);
245 if (c1 != c2) {
246 stat = 1;
247 break;
248 }
249 if (c1 == EOF || c2 == EOF)
250 break;
251 }
252 fclose(f1);
253 fclose(f2);
254 return(stat);
255}
256
257char *
258wordb(s, t) /* in s, t is prefix; return tail */
259char *s, *t;
260{
261 int c;
262
263 while (c = *s++) {
264 if (c == ' ' || c == '\t')
265 break;
266 *t++ = c;
267 }
268 *t = 0;
269 while (*s == ' ' || *s == '\t')
270 s++;
271 return(c ? s : NULL);
272}