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 (first) addresses */
+
+Reprog *lastre; /* Last regular expression */
Resub subexp[MAXSUB]; /* sub-patterns of pattern match*/
Rune addspace[ADDSIZE]; /* Buffer for a, c, & i commands */
};
} 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 */
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;
Rune *compsub(Rune *, Rune *);
void dechain(void);
void dosub(Rune *);
-int ecmp(Rune *, Rune *, int);
void enroll(char *);
void errexit(void);
int executable(SedCom *);
compfl = 0;
if(argc == 1)
- exits(0);
+ exits(nil);
ARGBEGIN{
case 'e':
if (argc <= 1)
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) {
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
address(&rep->ad1);
if (rep->ad1.type != A_NONE) {
+ if (rep->ad1.type == A_DOL)
+ dollars++;
if (rep->ad1.type == A_LAST) {
if (!lastre)
quit("First RE may not be null");
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;
while ((r = *cp++) != '\0') {
if(r == '\\') {
if (rhs < end)
- *rhs++ = 0xFFFF;
+ *rhs++ = Runemax;
else
return 0;
r = *cp++;
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)
{
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;
}
sp = place(sp, loc1, loc2);
continue;
}
- if (c == 0xFFFF && (c = *rp++) >= '1' && c < MAXSUB + '0') {
+ if (c == Runemax && (c = *rp++) >= '1' && c < MAXSUB + '0') {
n = c-'0';
if (subexp[n].rsp && subexp[n].rep) {
sp = place(sp, subexp[n].rsp, subexp[n].rep);
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++)
while (l1 < l2) {
*sp++ = *l1++;
if (sp >= &genbuf[LBSIZE])
- fprint(2, "sed: Output line too long.\n");
+ quit("Output line too long");
}
return sp;
}
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:
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:
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)
{
int c;
- char *s;
+ char *s, *e;
char buf[128];
Rune *p1;
Biobuf *fi;
Bputrune(&fout, *p1);
Bputc(&fout, '\n');
} else {
- for(s = buf, p1 = (*aptr)->text; *p1; p1++)
+ for(s = buf, e = buf+sizeof(buf)-UTFmax-1, p1 = (*aptr)->text; *p1 && s < e; p1++)
s += runetochar(s, p1);
*s = '\0';
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);
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;
if(p != addr) {
*p = '\0';
peekc = -1;
- if (fhead == 0)
+ if (fhead == nil)
dolflag = 1;
return p;
}
f = &stdin;
}
Blethal(f, nil);
+ if(uflag) Biofn(f, flushout);
fhead = fhead->next;
return 1;
}