]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/sed.c
usb lib: add maxpkt and ntds to Altc struct
[plan9front.git] / sys / src / cmd / sed.c
index 790f2ed519c6aef366d0ca6b74e909d155e96a00..914aab7f330e025725f4a2fa4dfef79d194985bf 100644 (file)
@@ -96,7 +96,9 @@ SedCom pspace[MAXCMDS];                       /* Command storage */
 SedCom *pend = pspace+MAXCMDS;         /* End of command storage */
 SedCom *rep = pspace;                  /* Current fill point */
 
-Reprog *lastre = 0;                    /* Last regular expression */
+int    dollars;                        /* Number of dollar addresses */
+
+Reprog *lastre;                        /* Last regular expression */
 Resub  subexp[MAXSUB];                 /* sub-patterns of pattern match*/
 
 Rune   addspace[ADDSIZE];              /* Buffer for a, c, & i commands */
@@ -116,10 +118,10 @@ struct {                          /* Sed program input control block */
        };
 } prog;
 
-Rune   genbuf[LBSIZE];                 /* Miscellaneous buffer */
+Rune   genbuf[LBSIZE+1];               /* Miscellaneous buffer */
 
-FileCache      *fhead = 0;             /* Head of File Cache Chain */
-FileCache      *ftail = 0;             /* Tail of File Cache Chain */
+FileCache      *fhead;                 /* Head of File Cache Chain */
+FileCache      *ftail;                 /* Tail of File Cache Chain */
 
 Rune   *loc1;                          /* Start of pattern match */
 Rune   *loc2;                          /* End of pattern match */
@@ -136,27 +138,28 @@ Rune      *hspend = holdsp;               /* End of hold data */
 
 int    nflag;                          /* Command line flags */
 int    gflag;
+int    uflag;
 
 int    dolflag;                        /* Set when at true EOF */
 int    sflag;                          /* Set when substitution done */
 int    jflag;                          /* Set when jump required */
 int    delflag;                        /* Delete current line when set */
 
-long   lnum = 0;                       /* Input line count */
+long   lnum;                           /* Input line count */
 
 char   fname[MAXFILES][40];            /* File name cache */
 Biobuf *fcode[MAXFILES];               /* File ID cache */
-int    nfiles = 0;                     /* Cache fill point */
+int    nfiles;                         /* Cache fill point */
 
 Biobuf fout;                           /* Output stream */
 Biobuf stdin;                          /* Default input */
-Biobuf*        f = 0;                          /* Input data */
+Biobuf*        f;                              /* Input data */
 
 Label  ltab[LABSIZE];                  /* Label name symbol table */
 Label  *labend = ltab+LABSIZE;         /* End of label table */
 Label  *lab = ltab+1;                  /* Current Fill point */
 
-int    depth = 0;                      /* {} stack pointer */
+int    depth;                          /* {} stack pointer */
 
 Rune   bad;                            /* Dummy err ptr reference */
 Rune   *badp = &bad;
@@ -177,7 +180,6 @@ Reprog      *compile(void);
 Rune   *compsub(Rune *, Rune *);
 void   dechain(void);
 void   dosub(Rune *);
-int    ecmp(Rune *, Rune *, int);
 void   enroll(char *);
 void   errexit(void);
 int    executable(SedCom *);
@@ -212,7 +214,7 @@ main(int argc, char **argv)
        compfl = 0;
 
        if(argc == 1)
-               exits(0);
+               exits(nil);
        ARGBEGIN{
        case 'e':
                if (argc <= 1)
@@ -234,9 +236,11 @@ main(int argc, char **argv)
        case 'n':
                nflag++;
                continue;
-       default:
-               fprint(2, "sed: Unknown flag: %c\n", ARGC());
+       case 'u':
+               uflag++;
                continue;
+       default:
+               quit("Unknown flag: %c", ARGC());
        } ARGEND
 
        if(compfl == 0) {
@@ -254,12 +258,12 @@ main(int argc, char **argv)
        dechain();
 
        if(argc <= 0)
-               enroll(0);              /* Add stdin to cache */
+               enroll(nil);            /* Add stdin to cache */
        else
                while(--argc >= 0)
                        enroll(*argv++);
        execute();
-       exits(0);
+       exits(nil);
 }
 
 void
@@ -519,7 +523,7 @@ jtcommon:
                        seof = *cp++;
                        if ((rep->re1 = compile()) == 0) {
                                if(!lastre)
-                                       quit("First RE may not be null.");
+                                       quit("First RE may not be null");
                                rep->re1 = lastre;
                        }
                        rep->rhs = p;
@@ -683,6 +687,16 @@ regerror(char *s)
        quit(CGMES, L"r.e.-using", linebuf);
 }
 
+int
+flushout(Biobufhdr *bp, void *v, long n)
+{
+       int i;
+       
+       for(i = 0; i < nfiles; i++)
+               Bflush(fcode[i]);
+       return read(bp->fid, v, n);
+}
+
 void
 newfile(enum PTYPE type, char *name)
 {
@@ -692,6 +706,7 @@ newfile(enum PTYPE type, char *name)
                if ((prog.bp = Bopen(name, OREAD)) == 0)
                        quit("Cannot open pattern-file: %s\n", name);
                Blethal(prog.bp, nil);
+               if(uflag) Biofn(prog.bp, flushout);
        }
        prog.type = type;
 }
@@ -750,9 +765,10 @@ address(Addr *ap)
        int c;
        long lno;
 
-       if((c = *cp++) == '$')
+       if((c = *cp++) == '$'){
                ap->type = A_DOL;
-       else if(c == '/') {
+               dollars++;
+       }else if(c == '/') {
                seof = c;
                if (ap->rp = compile())
                        ap->type = A_RE;
@@ -1062,19 +1078,18 @@ dosub(Rune *rhsbuf)
                                continue;
                        }
                        else {
-                               fprint(2, "sed: Invalid back reference \\%d\n",n);
-                               errexit();
+                               quit("Invalid back reference \\%d", n);
                        }
                }
                *sp++ = c;
                if (sp >= &genbuf[LBSIZE])
-                       fprint(2, "sed: Output line too long.\n");
+                       quit("Output line too long");
        }
        lp = loc2;
        loc2 = sp - genbuf + linebuf;
        while (*sp++ = *lp++)
                if (sp >= &genbuf[LBSIZE])
-                       fprint(2, "sed: Output line too long.\n");
+                       quit("Output line too long");
        lp = linebuf;
        sp = genbuf;
        while (*lp++ = *sp++)
@@ -1088,7 +1103,7 @@ place(Rune *sp, Rune *l1, Rune *l2)
        while (l1 < l2) {
                *sp++ = *l1++;
                if (sp >= &genbuf[LBSIZE])
-                       fprint(2, "sed: Output line too long.\n");
+                       quit("Output line too long");
        }
        return sp;
 }
@@ -1129,8 +1144,7 @@ command(SedCom *ipc)
        case ACOM:
                *aptr++ = ipc;
                if(aptr >= abuf+MAXADDS)
-                       quit("sed: Too many appends after line %ld\n",
-                               (char *)lnum);
+                       quit("Too many appends after line %ld", lnum);
                *aptr = 0;
                break;
        case CCOM:
@@ -1261,12 +1275,11 @@ cpcom:
                        putline(&fout, linebuf, spend-linebuf);
                if(aptr > abuf)
                        arout();
-               exits(0);
+               exits(nil);
        case RCOM:
                *aptr++ = ipc;
                if(aptr >= &abuf[MAXADDS])
-                       quit("sed: Too many reads after line %ld\n",
-                               (char *)lnum);
+                       quit("Too many reads after line %ld", lnum);
                *aptr = 0;
                break;
        case SCOM:
@@ -1324,13 +1337,6 @@ putline(Biobuf *bp, Rune *buf, int n)
                Bputrune(bp, *buf++);
        Bputc(bp, '\n');
 }
-ecmp(Rune *a, Rune *b, int count)
-{
-       while(count--)
-               if(*a++ != *b++)
-                       return 0;
-       return 1;
-}
 
 void
 arout(void)
@@ -1353,6 +1359,7 @@ arout(void)
                        if((fi = Bopen(buf, OREAD)) == 0)
                                continue;
                        Blethal(fi, nil);
+                       if(uflag) Biofn(fi, flushout);
                        while((c = Bgetc(fi)) >= 0)
                                Bputc(&fout, c);
                        Bterm(fi);
@@ -1401,7 +1408,7 @@ gline(Rune *addr)
                p = addr;
                for (c = (peekc? peekc: Bgetrune(f)); c >= 0; c = Bgetrune(f)) {
                        if (c == '\n') {
-                               if ((peekc = Bgetrune(f)) < 0 && fhead == 0)
+                               if (dollars != 0 && (peekc = Bgetrune(f)) < 0 && fhead == nil)
                                        dolflag = 1;
                                *p = '\0';
                                return p;
@@ -1413,7 +1420,7 @@ gline(Rune *addr)
                if(p != addr) {
                        *p = '\0';
                        peekc = -1;
-                       if (fhead == 0)
+                       if (fhead == nil)
                                dolflag = 1;
                        return p;
                }
@@ -1457,6 +1464,7 @@ opendata(void)
                f = &stdin;
        }
        Blethal(f, nil);
+       if(uflag) Biofn(f, flushout);
        fhead = fhead->next;
        return 1;
 }