1: #include "stdio.h" 2: #include "signal.h" 3: #include "lrnref" 4: 5: char last[100]; 6: char logf[100]; 7: char subdir[100]; 8: extern char * ctime(); 9: 10: copy(prompt, fin) 11: FILE *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: 184: pgets(s, prompt, f) 185: FILE *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: 198: trim(s) 199: char *s; 200: { 201: while (*s) 202: s++; 203: if (*--s == '\n') 204: *s=0; 205: } 206: 207: #define LINES 23 /* lines per screenful */ 208: 209: scopy(fi, fo) /* copy fi to fo until a line with # */ 210: FILE *fi, *fo; 211: { 212: int c, lines=0; 213: 214: while ((c = getc(fi)) != '#' && c != EOF) { 215: do { 216: if (fo != NULL) 217: putc(c, fo); 218: if (c == '\n') 219: break; 220: } while ((c = getc(fi)) != EOF); 221: if ((fo==stdout) && (++lines==LINES)) { 222: printf("Press Return to continue-"); 223: fflush(stdout); 224: while (getchar() != '\n') 225: ; 226: lines = 0; 227: } 228: } 229: if (c == '#') 230: ungetc(c, fi); 231: fflush(fo); 232: } 233: 234: cmp(r) /* compare two files for status */ 235: char *r; 236: { 237: char *s; 238: FILE *f1, *f2; 239: int c1, c2, stat; 240: 241: for (s = r; *s != ' ' && *s != '\0'; s++) 242: ; 243: *s++ = 0; /* r contains file 1 */ 244: while (*s == ' ') 245: s++; 246: f1 = fopen(r, "r"); 247: f2 = fopen(s, "r"); 248: if (f1 == NULL || f2 == NULL) 249: return(1); /* failure */ 250: stat = 0; 251: for (;;) { 252: c1 = getc(f1); 253: c2 = getc(f2); 254: if (c1 != c2) { 255: stat = 1; 256: break; 257: } 258: if (c1 == EOF || c2 == EOF) 259: break; 260: } 261: fclose(f1); 262: fclose(f2); 263: return(stat); 264: } 265: 266: char * 267: wordb(s, t) /* in s, t is prefix; return tail */ 268: char *s, *t; 269: { 270: int c; 271: 272: while (c = *s++) { 273: if (c == ' ' || c == '\t') 274: break; 275: *t++ = c; 276: } 277: *t = 0; 278: while (*s == ' ' || *s == '\t') 279: s++; 280: return(c ? s : NULL); 281: }