BSD 4_2 release
[unix-history] / usr / src / usr.bin / learn / copy.c
index 8108e11..b8fb62d 100644 (file)
@@ -1,38 +1,43 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)copy.c     4.1     (Berkeley)      2/24/83";
+static char sccsid[] = "@(#)copy.c     4.2     (Berkeley)      4/25/83";
 #endif not lint
 
 #include "stdio.h"
 #include "signal.h"
 #include "lrnref.h"
 
 #endif not lint
 
 #include "stdio.h"
 #include "signal.h"
 #include "lrnref.h"
 
+char togo[50];
 char last[100];
 char logf[100];
 char subdir[100];
 char last[100];
 char logf[100];
 char subdir[100];
-extern char * ctime();
+extern char *ctime();
+extern int review;
+int noclobber;
 
 copy(prompt, fin)
 
 copy(prompt, fin)
+int prompt;
 FILE *fin;
 {
        FILE *fout, *f;
 FILE *fin;
 {
        FILE *fout, *f;
-       char s[100], t[100], s1[100], *r, *tod;
-       char nm[30];
+       char s[100], t[100], s1[100], nm[30];
+       char *r, *tod, c;
        int *p, tv[2];
        extern int intrpt(), *action();
        extern char *wordb();
        int nmatch = 0;
        int *p, tv[2];
        extern int intrpt(), *action();
        extern char *wordb();
        int nmatch = 0;
+       long mark;
 
        if (subdir[0]==0)
 
        if (subdir[0]==0)
-               sprintf(subdir, "../../%s", sname);
+               sprintf(subdir, "%s/%s", direct, sname);
        for (;;) {
                if (pgets(s, prompt, fin) == 0)
                        if (fin == stdin) {
        for (;;) {
                if (pgets(s, prompt, fin) == 0)
                        if (fin == stdin) {
-                               /* fprintf(stderr, "Don't type control-D\n"); */
-                               /* this didn't work out very well */
+                               fprintf(stderr, "Type \"bye\" if you want to leave learn.\n");
+                               fflush(stderr);
                                continue;
                        } else
                                break;
                                continue;
                        } else
                                break;
-               trim(s);
+               trim(s);                /* trim newline */
                /* change the sequence %s to lesson directory */
                /* if needed */
                for (r = s; *r; r++)
                /* change the sequence %s to lesson directory */
                /* if needed */
                for (r = s; *r; r++)
@@ -41,10 +46,10 @@ FILE *fin;
                                strcpy(s, s1);
                                break;
                        }
                                strcpy(s, s1);
                                break;
                        }
-               r = wordb(s, t);
-               p = action(t);
+               r = wordb(s, t);        /* t = first token, r = rest */
+               p = action(t);          /* p = token class */
                if (*p == ONCE) {       /* some actions done only once per script */
                if (*p == ONCE) {       /* some actions done only once per script */
-                       if (wrong) {    /* we are on 2nd time */
+                       if (wrong && !review) { /* we are on 2nd time */
                                scopy(fin, NULL);
                                continue;
                        }
                                scopy(fin, NULL);
                                continue;
                        }
@@ -53,11 +58,11 @@ FILE *fin;
                        p = action(t);
                }
                if (p == 0) {
                        p = action(t);
                }
                if (p == 0) {
-                       if (comfile >= 0) {
+                       if (comfile >= 0) {     /* if #pipe in effect ... */
                                write(comfile, s, strlen(s));
                                write(comfile, "\n", 1);
                        }
                                write(comfile, s, strlen(s));
                                write(comfile, "\n", 1);
                        }
-                       else {
+                       else {          /* else must be UNIX command ... */
                                signal(SIGINT, SIG_IGN);
                                status = mysys(s);
                                signal(SIGINT, intrpt);
                                signal(SIGINT, SIG_IGN);
                                status = mysys(s);
                                signal(SIGINT, intrpt);
@@ -83,6 +88,15 @@ FILE *fin;
                        else
                                scopy(fin, stdout);
                        break;
                        else
                                scopy(fin, stdout);
                        break;
+               case XYZZY:
+                       mark = ftell(scrin);
+                       if (r)
+                               rewind(scrin);
+                       while ((int)(c=fgetc(scrin)) != EOF)
+                               putchar(c);
+                       fflush(stdout);
+                       fseek(scrin, mark, 0);
+                       break;
                case NOP:
                        break;
                case MATCH:
                case NOP:
                        break;
                case MATCH:
@@ -107,9 +121,13 @@ FILE *fin;
                        scopy(fin, (status != 0) ? stdout : NULL);
                        break;
                case CREATE:
                        scopy(fin, (status != 0) ? stdout : NULL);
                        break;
                case CREATE:
-                       fout = fopen(r, "w");
+                       if (noclobber)
+                               fout = NULL;
+                       else
+                               fout = fopen(r, "w");
                        scopy(fin, fout);
                        scopy(fin, fout);
-                       fclose(fout);
+                       if (!noclobber)
+                               fclose(fout);
                        break;
                case CMP:
                        status = cmp(r);        /* contains two file names */
                        break;
                case CMP:
                        status = cmp(r);        /* contains two file names */
@@ -120,8 +138,25 @@ FILE *fin;
                        break;
                case USER:
                case NEXT:
                        break;
                case USER:
                case NEXT:
+                       if (noclobber)
+                               noclobber = 0;
                        more = 1;
                        return;
                        more = 1;
                        return;
+               case AGAIN:
+                       review = 0;
+                       if (!r) {
+                               r = todo;
+                               noclobber = 1;
+                               review = 1;
+                       }
+                       again = 1;
+                       strcpy(togo, r);
+                       unhook();
+                       return;
+               case SKIP:
+                       skip = 1;
+                       unhook();
+                       return;
                case COPYIN:
                        incopy = fopen(".copy", "w");
                        break;
                case COPYIN:
                        incopy = fopen(".copy", "w");
                        break;
@@ -130,10 +165,11 @@ FILE *fin;
                        incopy = NULL;
                        break;
                case COPYOUT:
                        incopy = NULL;
                        break;
                case COPYOUT:
-                       maktee();
+                       teed = maktee();
                        break;
                case UNCOPOUT:
                        untee();
                        break;
                case UNCOPOUT:
                        untee();
+                       teed = 0;
                        break;
                case PIPE:
                        comfile = makpipe();
                        break;
                case PIPE:
                        comfile = makpipe();
@@ -151,7 +187,10 @@ FILE *fin;
                        }
                        return;
                case WHERE:
                        }
                        return;
                case WHERE:
-                       printf("You are in lesson %s\n", todo);
+                       printf("You are in lesson %s of \"%s\" with a speed rating of %d.\n", todo, sname, speed);
+                       printf("You have completed %d out of a possible %d lessons.\n", sequence-1, total);
+                       if (r)
+                               tellwhich();
                        fflush(stdout);
                        break;
                case BYE:
                        fflush(stdout);
                        break;
                case BYE:
@@ -170,7 +209,7 @@ FILE *fin;
                                break;
                        if (logf[0] == 0)
                                sprintf(logf, "%s/log/%s", direct, sname);
                                break;
                        if (logf[0] == 0)
                                sprintf(logf, "%s/log/%s", direct, sname);
-                       f = fopen( (r? r : logf), "a");
+                       f = fopen((r ? r : logf), "a");
                        if (f == NULL)
                                break;
                        time(tv);
                        if (f == NULL)
                                break;
                        time(tv);
@@ -186,11 +225,13 @@ FILE *fin;
 }
 
 pgets(s, prompt, f)
 }
 
 pgets(s, prompt, f)
+char *s;
+int prompt;
 FILE *f;
 {
        if (prompt) {
                if (comfile < 0)
 FILE *f;
 {
        if (prompt) {
                if (comfile < 0)
-                       printf("$ ");
+                       fputs("% ", stdout);
                fflush(stdout);
        }
        if (fgets(s, 100,f))
                fflush(stdout);
        }
        if (fgets(s, 100,f))
@@ -208,13 +249,28 @@ char *s;
                *s=0;
 }
 
                *s=0;
 }
 
-scopy(fi, fo)  /* copy fi to fo until a line with # */
+scopy(fi, fo)  /* copy fi to fo until a line with #
+                * sequence "#\n" means a line not ending with \n
+                * control-M's are filtered out */
 FILE *fi, *fo;
 {
        int c;
 
        while ((c = getc(fi)) != '#' && c != EOF) {
                do {
 FILE *fi, *fo;
 {
        int c;
 
        while ((c = getc(fi)) != '#' && c != EOF) {
                do {
+                       if (c == '#')   {
+                               c = getc(fi);
+                               if (c == '\n')
+                                       break;
+                               if (c == EOF)   {
+                                       fflush(fo);
+                                       return;
+                               }
+                               if (fo != NULL)
+                                       putc('#', fo);
+                       }
+                       if (c == '\r')
+                               break;
                        if (fo != NULL)
                                putc(c, fo);
                        if (c == '\n')
                        if (fo != NULL)
                                putc(c, fo);
                        if (c == '\n')
@@ -226,18 +282,28 @@ FILE *fi, *fo;
        fflush(fo);
 }
 
        fflush(fo);
 }
 
-cmp(r) /* compare two files for status */
+cmp(r) /* compare two files for status; #cmp f1 f2 [ firstnlinesonly ] */
 char *r;
 {
 char *r;
 {
-       char *s;
+       char *s, *h;
        FILE *f1, *f2;
        FILE *f1, *f2;
-       int c1, c2, stat;
+       int c1, c2, stat, n;
 
        for (s = r; *s != ' ' && *s != '\0'; s++)
                ;
        *s++ = 0;       /* r contains file 1 */
        while (*s == ' ')
                s++;
 
        for (s = r; *s != ' ' && *s != '\0'; s++)
                ;
        *s++ = 0;       /* r contains file 1 */
        while (*s == ' ')
                s++;
+       for (h = s; *h != ' ' && *h != '\0'; h++)
+               ;
+       if (*h) {
+               *h++ = 0;
+               while (*h == ' ')
+                       h++;
+               n = atoi(h);
+       }
+       else
+               n = 077777;
        f1 = fopen(r, "r");
        f2 = fopen(s, "r");
        if (f1 == NULL || f2 == NULL)
        f1 = fopen(r, "r");
        f2 = fopen(s, "r");
        if (f1 == NULL || f2 == NULL)
@@ -250,6 +316,9 @@ char *r;
                        stat = 1;
                        break;
                }
                        stat = 1;
                        break;
                }
+               if (*h && c1 == '\n')
+                       if (--n)
+                               break;
                if (c1 == EOF || c2 == EOF)
                        break;
        }
                if (c1 == EOF || c2 == EOF)
                        break;
        }
@@ -274,3 +343,22 @@ char *s, *t;
                s++;
        return(c ? s : NULL);
 }
                s++;
        return(c ? s : NULL);
 }
+
+unhook()
+{
+       if (incopy) {
+               fclose(incopy);
+               incopy = NULL;
+       }
+       if (comfile >= 0) {
+               close(comfile);
+               wait(0);
+               comfile = -1;
+       }
+       if (teed) {
+               teed = 0;
+               untee();
+       }
+       fclose(scrin);
+       scrin = NULL;
+}