Annotation of researchv10no/cmd/learn/lib/src/copy.c, revision 1.1.1.1

1.1       root        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 != 0 && *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:                                if (!prompt) /* if not input from tty,
                     54:                                                delay write to sh, fix timing prob */
                     55:                                        sleep(2);
                     56:                                write(comfile, s, strlen(s));
                     57:                                write(comfile, "\n", 1);
                     58:                        }
                     59:                        else {
                     60:                                signal(SIGINT, SIG_IGN);
                     61:                                status = mysys(s);
                     62:                                signal(SIGINT, intrpt);
                     63:                        }
                     64:                        if (incopy) {
                     65:                                fprintf(incopy, "%s\n", s);
                     66:                                strcpy(last, s);
                     67:                        }
                     68:                        continue;
                     69:                }
                     70:                switch (*p) {
                     71:                case READY:
                     72:                        if (incopy && r) {
                     73:                                fprintf(incopy, "%s\n", r);
                     74:                                strcpy(last, r);
                     75:                        }
                     76:                        return;
                     77:                case PRINT:
                     78:                        if (wrong)
                     79:                                scopy(fin, NULL);       /* don't repeat message */
                     80:                        else if (r)
                     81:                                list(r);
                     82:                        else
                     83:                                scopy(fin, stdout);
                     84:                        break;
                     85:                case NOP:
                     86:                        break;
                     87:                case MATCH:
                     88:                        if (nmatch > 0) /* we have already passed */
                     89:                                scopy(fin, NULL);
                     90:                        else if ((status = strcmp(r, last)) == 0) {     /* did we pass this time? */
                     91:                                nmatch++;
                     92:                                scopy(fin, stdout);
                     93:                        } else
                     94:                                scopy(fin, NULL);
                     95:                        break;
                     96:                case BAD:
                     97:                        if (strcmp(r, last) == 0) {
                     98:                                scopy(fin, stdout);
                     99:                        } else
                    100:                                scopy(fin, NULL);
                    101:                        break;
                    102:                case SUCCEED:
                    103:                        scopy(fin, (status == 0) ? stdout : NULL);
                    104:                        break;
                    105:                case FAIL:
                    106:                        scopy(fin, (status != 0) ? stdout : NULL);
                    107:                        break;
                    108:                case CREATE:
                    109:                        fout = fopen(r, "w");
                    110:                        scopy(fin, fout);
                    111:                        fclose(fout);
                    112:                        break;
                    113:                case CMP:
                    114:                        status = cmp(r);        /* contains two file names */
                    115:                        break;
                    116:                case MV:
                    117:                        sprintf(nm, "%s/L%s.%s", subdir, todo, r);
                    118:                        fcopy(r, nm);
                    119:                        break;
                    120:                case USER:
                    121:                case NEXT:
                    122:                        more = 1;
                    123:                        return;
                    124:                case COPYIN:
                    125:                        incopy = fopen(".copy", "w");
                    126:                        break;
                    127:                case UNCOPIN:
                    128:                        fclose(incopy);
                    129:                        incopy = NULL;
                    130:                        break;
                    131:                case COPYOUT:
                    132:                        maktee();
                    133:                        break;
                    134:                case UNCOPOUT:
                    135:                        untee();
                    136:                        break;
                    137:                case PIPE:
                    138:                        comfile = makpipe();
                    139:                        break;
                    140:                case UNPIPE:
                    141:                        close(comfile);
                    142:                        wait(0);
                    143:                        comfile = -1;
                    144:                        break;
                    145:                case YES:
                    146:                case NO:
                    147:                        if (incopy) {
                    148:                                fprintf(incopy, "%s\n", s);
                    149:                                strcpy(last, s);
                    150:                        }
                    151:                        return;
                    152:                case WHERE:
                    153:                        printf("You are in lesson %s\n", todo);
                    154:                        fflush(stdout);
                    155:                        break;
                    156:                case BYE:
                    157:                        more=0;
                    158:                        return;
                    159:                case CHDIR:
                    160:                        printf("cd not allowed\n");
                    161:                        fflush(stdout);
                    162:                        break;
                    163:                case LEARN:
                    164:                        printf("You are already in learn.\n");
                    165:                        fflush(stdout);
                    166:                        break;
                    167:                case LOG:
                    168:                        if (!logging)
                    169:                                break;
                    170:                        if (logf[0] == 0)
                    171:                                sprintf(logf, "%s/log/%s", direct, sname);
                    172:                        f = fopen( (r? r : logf), "a");
                    173:                        if (f == NULL)
                    174:                                break;
                    175:                        time(tv);
                    176:                        tod = ctime(tv);
                    177:                        tod[24] = 0;
                    178:                        fprintf(f, "%s L%-6s %s %2d %s\n", tod,
                    179:                                todo, status? "fail" : "pass", speed, pwline);
                    180:                        fclose(f);
                    181:                        break;
                    182:                }
                    183:        }
                    184:        return;
                    185: }
                    186: 
                    187: pgets(s, prompt, f)
                    188: FILE *f;
                    189: {
                    190:        if (prompt) {
                    191:                if (comfile < 0)
                    192:                        printf("$ ");
                    193:                fflush(stdout);
                    194:        }
                    195:        if (fgets(s, 100,f))
                    196:                return(1);
                    197:        else
                    198:                return(0);
                    199: }
                    200: 
                    201: trim(s)
                    202: char *s;
                    203: {
                    204:        while (*s)
                    205:                s++;
                    206:        if (*--s == '\n')
                    207:                *s=0;
                    208: }
                    209: 
                    210: scopy(fi, fo)  /* copy fi to fo until a line with # */
                    211: FILE *fi, *fo;
                    212: {
                    213:        int c;
                    214: 
                    215:        while ((c = getc(fi)) != '#' && c != EOF) {
                    216:                do {
                    217:                        if (fo != NULL)
                    218:                                putc(c, fo);
                    219:                        if (c == '\n')
                    220:                                break;
                    221:                } while ((c = getc(fi)) != EOF);
                    222:        }
                    223:        if (c == '#')
                    224:                ungetc(c, fi);
                    225:        if (fo != NULL)
                    226:                fflush(fo);
                    227: }
                    228: 
                    229: cmp(r) /* compare two files for status */
                    230: char *r;
                    231: {
                    232:        char *s;
                    233:        FILE *f1, *f2;
                    234:        int c1, c2, stat;
                    235: 
                    236:        for (s = r; *s != ' ' && *s != '\0'; s++)
                    237:                ;
                    238:        *s++ = 0;       /* r contains file 1 */
                    239:        while (*s == ' ')
                    240:                s++;
                    241:        f1 = fopen(r, "r");
                    242:        f2 = fopen(s, "r");
                    243:        if (f1 == NULL || f2 == NULL)
                    244:                return(1);      /* failure */
                    245:        stat = 0;
                    246:        for (;;) {
                    247:                c1 = getc(f1);
                    248:                c2 = getc(f2);
                    249:                if (c1 != c2) {
                    250:                        stat = 1;
                    251:                        break;
                    252:                }
                    253:                if (c1 == EOF || c2 == EOF)
                    254:                        break;
                    255:        }
                    256:        fclose(f1);
                    257:        fclose(f2);
                    258:        return(stat);
                    259: }
                    260: 
                    261: char *
                    262: wordb(s, t)    /* in s, t is prefix; return tail */
                    263: char *s, *t;
                    264: {
                    265:        int c;
                    266: 
                    267:        while (c = *s++) {
                    268:                if (c == ' ' || c == '\t')
                    269:                        break;
                    270:                *t++ = c;
                    271:        }
                    272:        *t = 0;
                    273:        while (*s == ' ' || *s == '\t')
                    274:                s++;
                    275:        return(c ? s : NULL);
                    276: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.