2 70bcc780 2004-03-25 devnull * sed -- stream editor
6 70bcc780 2004-03-25 devnull #include <u.h>
7 70bcc780 2004-03-25 devnull #include <libc.h>
8 70bcc780 2004-03-25 devnull #include <bio.h>
9 70bcc780 2004-03-25 devnull #include <regexp.h>
12 70bcc780 2004-03-25 devnull DEPTH = 20, /* max nesting depth of {} */
13 70bcc780 2004-03-25 devnull MAXCMDS = 512, /* max sed commands */
14 70bcc780 2004-03-25 devnull ADDSIZE = 10000, /* size of add & read buffer */
15 70bcc780 2004-03-25 devnull MAXADDS = 20, /* max pending adds and reads */
16 70bcc780 2004-03-25 devnull LBSIZE = 8192, /* input line size */
17 70bcc780 2004-03-25 devnull LABSIZE = 50, /* max label name size */
18 70bcc780 2004-03-25 devnull MAXSUB = 10, /* max number of sub reg exp */
19 70bcc780 2004-03-25 devnull MAXFILES = 120, /* max output files */
21 70bcc780 2004-03-25 devnull /* An address is a line #, a R.E., "$", a reference to the last
22 70bcc780 2004-03-25 devnull * R.E., or nothing.
24 70bcc780 2004-03-25 devnull typedef struct {
33 70bcc780 2004-03-25 devnull long line; /* Line # */
34 70bcc780 2004-03-25 devnull Reprog *rp; /* Compiled R.E. */
38 70bcc780 2004-03-25 devnull typedef struct SEDCOM {
39 70bcc780 2004-03-25 devnull Addr ad1; /* optional start address */
40 70bcc780 2004-03-25 devnull Addr ad2; /* optional end address */
42 70bcc780 2004-03-25 devnull Reprog *re1; /* compiled R.E. */
43 70bcc780 2004-03-25 devnull Rune *text; /* added text or file name */
44 70bcc780 2004-03-25 devnull struct SEDCOM *lb1; /* destination command of branch */
46 70bcc780 2004-03-25 devnull Rune *rhs; /* Right-hand side of substitution */
47 70bcc780 2004-03-25 devnull Biobuf* fcode; /* File ID for read and write */
48 70bcc780 2004-03-25 devnull char command; /* command code -see below */
49 70bcc780 2004-03-25 devnull char gfl; /* 'Global' flag for substitutions */
50 70bcc780 2004-03-25 devnull char pfl; /* 'print' flag for substitutions */
51 70bcc780 2004-03-25 devnull char active; /* 1 => data between start and end */
52 70bcc780 2004-03-25 devnull char negfl; /* negation flag */
53 70bcc780 2004-03-25 devnull } SedCom;
55 70bcc780 2004-03-25 devnull /* Command Codes for field SedCom.command */
56 70bcc780 2004-03-25 devnull #define ACOM 01
57 70bcc780 2004-03-25 devnull #define BCOM 020
58 70bcc780 2004-03-25 devnull #define CCOM 02
59 70bcc780 2004-03-25 devnull #define CDCOM 025
60 70bcc780 2004-03-25 devnull #define CNCOM 022
61 70bcc780 2004-03-25 devnull #define COCOM 017
62 70bcc780 2004-03-25 devnull #define CPCOM 023
63 70bcc780 2004-03-25 devnull #define DCOM 03
64 70bcc780 2004-03-25 devnull #define ECOM 015
65 70bcc780 2004-03-25 devnull #define EQCOM 013
66 70bcc780 2004-03-25 devnull #define FCOM 016
67 70bcc780 2004-03-25 devnull #define GCOM 027
68 70bcc780 2004-03-25 devnull #define CGCOM 030
69 70bcc780 2004-03-25 devnull #define HCOM 031
70 70bcc780 2004-03-25 devnull #define CHCOM 032
71 70bcc780 2004-03-25 devnull #define ICOM 04
72 70bcc780 2004-03-25 devnull #define LCOM 05
73 70bcc780 2004-03-25 devnull #define NCOM 012
74 70bcc780 2004-03-25 devnull #define PCOM 010
75 70bcc780 2004-03-25 devnull #define QCOM 011
76 70bcc780 2004-03-25 devnull #define RCOM 06
77 70bcc780 2004-03-25 devnull #define SCOM 07
78 70bcc780 2004-03-25 devnull #define TCOM 021
79 70bcc780 2004-03-25 devnull #define WCOM 014
80 70bcc780 2004-03-25 devnull #define CWCOM 024
81 70bcc780 2004-03-25 devnull #define YCOM 026
82 70bcc780 2004-03-25 devnull #define XCOM 033
85 70bcc780 2004-03-25 devnull typedef struct label { /* Label symbol table */
86 70bcc780 2004-03-25 devnull Rune asc[9]; /* Label name */
87 70bcc780 2004-03-25 devnull SedCom *chain;
88 70bcc780 2004-03-25 devnull SedCom *address; /* Command associated with label */
91 70bcc780 2004-03-25 devnull typedef struct FILE_CACHE { /* Data file control block */
92 70bcc780 2004-03-25 devnull struct FILE_CACHE *next; /* Forward Link */
93 70bcc780 2004-03-25 devnull char *name; /* Name of file */
94 70bcc780 2004-03-25 devnull } FileCache;
96 70bcc780 2004-03-25 devnull SedCom pspace[MAXCMDS]; /* Command storage */
97 70bcc780 2004-03-25 devnull SedCom *pend = pspace+MAXCMDS; /* End of command storage */
98 70bcc780 2004-03-25 devnull SedCom *rep = pspace; /* Current fill point */
100 70bcc780 2004-03-25 devnull Reprog *lastre = 0; /* Last regular expression */
101 70bcc780 2004-03-25 devnull Resub subexp[MAXSUB]; /* sub-patterns of pattern match*/
103 70bcc780 2004-03-25 devnull Rune addspace[ADDSIZE]; /* Buffer for a, c, & i commands */
104 70bcc780 2004-03-25 devnull Rune *addend = addspace+ADDSIZE;
106 70bcc780 2004-03-25 devnull SedCom *abuf[MAXADDS]; /* Queue of pending adds & reads */
107 70bcc780 2004-03-25 devnull SedCom **aptr = abuf;
109 70bcc780 2004-03-25 devnull struct { /* Sed program input control block */
110 70bcc780 2004-03-25 devnull enum PTYPE /* Either on command line or in file */
111 70bcc780 2004-03-25 devnull { P_ARG,
114 70bcc780 2004-03-25 devnull union PCTL { /* Pointer to data */
115 70bcc780 2004-03-25 devnull Biobuf *bp;
116 70bcc780 2004-03-25 devnull char *curr;
120 70bcc780 2004-03-25 devnull Rune genbuf[LBSIZE]; /* Miscellaneous buffer */
122 70bcc780 2004-03-25 devnull FileCache *fhead = 0; /* Head of File Cache Chain */
123 70bcc780 2004-03-25 devnull FileCache *ftail = 0; /* Tail of File Cache Chain */
125 70bcc780 2004-03-25 devnull Rune *loc1; /* Start of pattern match */
126 70bcc780 2004-03-25 devnull Rune *loc2; /* End of pattern match */
127 70bcc780 2004-03-25 devnull Rune seof; /* Pattern delimiter char */
129 70bcc780 2004-03-25 devnull Rune linebuf[LBSIZE+1]; /* Input data buffer */
130 70bcc780 2004-03-25 devnull Rune *lbend = linebuf+LBSIZE; /* End of buffer */
131 70bcc780 2004-03-25 devnull Rune *spend = linebuf; /* End of input data */
132 70bcc780 2004-03-25 devnull Rune *cp; /* Current scan point in linebuf */
134 70bcc780 2004-03-25 devnull Rune holdsp[LBSIZE+1]; /* Hold buffer */
135 70bcc780 2004-03-25 devnull Rune *hend = holdsp+LBSIZE; /* End of hold buffer */
136 70bcc780 2004-03-25 devnull Rune *hspend = holdsp; /* End of hold data */
138 70bcc780 2004-03-25 devnull int nflag; /* Command line flags */
139 70bcc780 2004-03-25 devnull int gflag;
141 70bcc780 2004-03-25 devnull int dolflag; /* Set when at true EOF */
142 70bcc780 2004-03-25 devnull int sflag; /* Set when substitution done */
143 70bcc780 2004-03-25 devnull int jflag; /* Set when jump required */
144 70bcc780 2004-03-25 devnull int delflag; /* Delete current line when set */
146 70bcc780 2004-03-25 devnull long lnum = 0; /* Input line count */
148 70bcc780 2004-03-25 devnull char fname[MAXFILES][40]; /* File name cache */
149 70bcc780 2004-03-25 devnull Biobuf *fcode[MAXFILES]; /* File ID cache */
150 70bcc780 2004-03-25 devnull int nfiles = 0; /* Cache fill point */
152 70bcc780 2004-03-25 devnull Biobuf fout; /* Output stream */
153 70bcc780 2004-03-25 devnull Biobuf bstdin; /* Default input */
154 70bcc780 2004-03-25 devnull Biobuf* f = 0; /* Input data */
156 70bcc780 2004-03-25 devnull Label ltab[LABSIZE]; /* Label name symbol table */
157 70bcc780 2004-03-25 devnull Label *labend = ltab+LABSIZE; /* End of label table */
158 70bcc780 2004-03-25 devnull Label *lab = ltab+1; /* Current Fill point */
160 70bcc780 2004-03-25 devnull int depth = 0; /* {} stack pointer */
162 70bcc780 2004-03-25 devnull Rune bad; /* Dummy err ptr reference */
163 70bcc780 2004-03-25 devnull Rune *badp = &bad;
166 70bcc780 2004-03-25 devnull char CGMES[] = "Command garbled: %S";
167 70bcc780 2004-03-25 devnull char TMMES[] = "Too much text: %S";
168 70bcc780 2004-03-25 devnull char LTL[] = "Label too long: %S";
169 70bcc780 2004-03-25 devnull char AD0MES[] = "No addresses allowed: %S";
170 70bcc780 2004-03-25 devnull char AD1MES[] = "Only one address allowed: %S";
172 70bcc780 2004-03-25 devnull void address(Addr *);
173 70bcc780 2004-03-25 devnull void arout(void);
174 70bcc780 2004-03-25 devnull int cmp(char *, char *);
175 70bcc780 2004-03-25 devnull int rcmp(Rune *, Rune *);
176 70bcc780 2004-03-25 devnull void command(SedCom *);
177 70bcc780 2004-03-25 devnull Reprog *compile(void);
178 70bcc780 2004-03-25 devnull Rune *compsub(Rune *, Rune *);
179 70bcc780 2004-03-25 devnull void dechain(void);
180 70bcc780 2004-03-25 devnull void dosub(Rune *);
181 70bcc780 2004-03-25 devnull int ecmp(Rune *, Rune *, int);
182 70bcc780 2004-03-25 devnull void enroll(char *);
183 70bcc780 2004-03-25 devnull void errexit(void);
184 70bcc780 2004-03-25 devnull int executable(SedCom *);
185 70bcc780 2004-03-25 devnull void execute(void);
186 70bcc780 2004-03-25 devnull void fcomp(void);
187 70bcc780 2004-03-25 devnull long getrune(void);
188 70bcc780 2004-03-25 devnull Rune *gline(Rune *);
189 70bcc780 2004-03-25 devnull int match(Reprog *, Rune *);
190 70bcc780 2004-03-25 devnull void newfile(enum PTYPE, char *);
191 70bcc780 2004-03-25 devnull int opendata(void);
192 70bcc780 2004-03-25 devnull Biobuf *open_file(char *);
193 70bcc780 2004-03-25 devnull Rune *place(Rune *, Rune *, Rune *);
194 70bcc780 2004-03-25 devnull void quit(char *, char *);
195 70bcc780 2004-03-25 devnull int rline(Rune *, Rune *);
196 70bcc780 2004-03-25 devnull Label *search(Label *);
197 70bcc780 2004-03-25 devnull int substitute(SedCom *);
198 70bcc780 2004-03-25 devnull char *text(char *);
199 70bcc780 2004-03-25 devnull Rune *stext(Rune *, Rune *);
200 70bcc780 2004-03-25 devnull int ycomp(SedCom *);
201 70bcc780 2004-03-25 devnull char * trans(int c);
202 70bcc780 2004-03-25 devnull void putline(Biobuf *bp, Rune *buf, int n);
205 70bcc780 2004-03-25 devnull main(int argc, char **argv)
207 70bcc780 2004-03-25 devnull int compfl;
209 70bcc780 2004-03-25 devnull lnum = 0;
210 70bcc780 2004-03-25 devnull Binit(&fout, 1, OWRITE);
211 70bcc780 2004-03-25 devnull fcode[nfiles++] = &fout;
212 70bcc780 2004-03-25 devnull compfl = 0;
214 70bcc780 2004-03-25 devnull if(argc == 1)
215 70bcc780 2004-03-25 devnull exits(0);
216 70bcc780 2004-03-25 devnull ARGBEGIN{
217 70bcc780 2004-03-25 devnull case 'n':
218 70bcc780 2004-03-25 devnull nflag++;
219 70bcc780 2004-03-25 devnull continue;
220 70bcc780 2004-03-25 devnull case 'f':
221 70bcc780 2004-03-25 devnull if(argc <= 1)
222 70bcc780 2004-03-25 devnull quit("no pattern-file", 0);
223 70bcc780 2004-03-25 devnull newfile(P_FILE, ARGF());
224 70bcc780 2004-03-25 devnull fcomp();
225 70bcc780 2004-03-25 devnull compfl = 1;
226 70bcc780 2004-03-25 devnull continue;
227 70bcc780 2004-03-25 devnull case 'e':
228 70bcc780 2004-03-25 devnull if (argc <= 1)
229 70bcc780 2004-03-25 devnull quit("missing pattern", 0);
230 70bcc780 2004-03-25 devnull newfile(P_ARG, ARGF());
231 70bcc780 2004-03-25 devnull fcomp();
232 70bcc780 2004-03-25 devnull compfl = 1;
233 70bcc780 2004-03-25 devnull continue;
234 70bcc780 2004-03-25 devnull case 'g':
235 70bcc780 2004-03-25 devnull gflag++;
236 70bcc780 2004-03-25 devnull continue;
237 70bcc780 2004-03-25 devnull default:
238 70bcc780 2004-03-25 devnull fprint(2, "sed: Unknown flag: %c\n", ARGC());
239 70bcc780 2004-03-25 devnull continue;
240 70bcc780 2004-03-25 devnull } ARGEND
242 70bcc780 2004-03-25 devnull if(compfl == 0) {
243 70bcc780 2004-03-25 devnull if (--argc < 0)
244 70bcc780 2004-03-25 devnull quit("missing pattern", 0);
245 70bcc780 2004-03-25 devnull newfile(P_ARG, *argv++);
246 70bcc780 2004-03-25 devnull fcomp();
249 70bcc780 2004-03-25 devnull if(depth)
250 70bcc780 2004-03-25 devnull quit("Too many {'s", 0);
252 70bcc780 2004-03-25 devnull ltab[0].address = rep;
254 70bcc780 2004-03-25 devnull dechain();
256 70bcc780 2004-03-25 devnull if(argc <= 0)
257 70bcc780 2004-03-25 devnull enroll(0); /* Add stdin to cache */
258 70bcc780 2004-03-25 devnull else while(--argc >= 0) {
259 70bcc780 2004-03-25 devnull enroll(*argv++);
261 70bcc780 2004-03-25 devnull execute();
262 70bcc780 2004-03-25 devnull exits(0);
265 70bcc780 2004-03-25 devnull fcomp(void)
267 70bcc780 2004-03-25 devnull Rune *tp;
268 70bcc780 2004-03-25 devnull SedCom *pt, *pt1;
270 70bcc780 2004-03-25 devnull Label *lpt;
272 70bcc780 2004-03-25 devnull static Rune *p = addspace;
273 70bcc780 2004-03-25 devnull static SedCom **cmpend[DEPTH]; /* stack of {} operations */
275 70bcc780 2004-03-25 devnull while (rline(linebuf, lbend) >= 0) {
276 70bcc780 2004-03-25 devnull cp = linebuf;
277 70bcc780 2004-03-25 devnull comploop:
278 70bcc780 2004-03-25 devnull while(*cp == ' ' || *cp == '\t')
280 70bcc780 2004-03-25 devnull if(*cp == '\0' || *cp == '#')
281 70bcc780 2004-03-25 devnull continue;
282 70bcc780 2004-03-25 devnull if(*cp == ';') {
284 70bcc780 2004-03-25 devnull goto comploop;
287 70bcc780 2004-03-25 devnull address(&rep->ad1);
288 70bcc780 2004-03-25 devnull if (rep->ad1.type != A_NONE) {
289 70bcc780 2004-03-25 devnull if (rep->ad1.type == A_LAST) {
290 70bcc780 2004-03-25 devnull if (!lastre)
291 70bcc780 2004-03-25 devnull quit("First RE may not be null", 0);
292 70bcc780 2004-03-25 devnull rep->ad1.type = A_RE;
293 4bda94f9 2004-03-25 devnull rep->ad1.u.rp = lastre;
295 70bcc780 2004-03-25 devnull if(*cp == ',' || *cp == ';') {
297 70bcc780 2004-03-25 devnull address(&rep->ad2);
298 70bcc780 2004-03-25 devnull if (rep->ad2.type == A_LAST) {
299 70bcc780 2004-03-25 devnull rep->ad1.type = A_RE;
300 4bda94f9 2004-03-25 devnull rep->ad2.u.rp = lastre;
303 70bcc780 2004-03-25 devnull rep->ad2.type = A_NONE;
305 70bcc780 2004-03-25 devnull while(*cp == ' ' || *cp == '\t')
309 70bcc780 2004-03-25 devnull switch(*cp++) {
311 70bcc780 2004-03-25 devnull default:
312 70bcc780 2004-03-25 devnull quit("Unrecognized command: %S", (char *)linebuf);
314 70bcc780 2004-03-25 devnull case '!':
315 70bcc780 2004-03-25 devnull rep->negfl = 1;
316 70bcc780 2004-03-25 devnull goto swit;
318 70bcc780 2004-03-25 devnull case '{':
319 70bcc780 2004-03-25 devnull rep->command = BCOM;
320 70bcc780 2004-03-25 devnull rep->negfl = !(rep->negfl);
321 35d95bae 2004-03-25 devnull cmpend[depth++] = &rep->u.lb1;
322 70bcc780 2004-03-25 devnull if(++rep >= pend)
323 70bcc780 2004-03-25 devnull quit("Too many commands: %S", (char *) linebuf);
324 70bcc780 2004-03-25 devnull if(*cp == '\0') continue;
325 70bcc780 2004-03-25 devnull goto comploop;
327 70bcc780 2004-03-25 devnull case '}':
328 70bcc780 2004-03-25 devnull if(rep->ad1.type != A_NONE)
329 70bcc780 2004-03-25 devnull quit(AD0MES, (char *) linebuf);
330 70bcc780 2004-03-25 devnull if(--depth < 0)
331 70bcc780 2004-03-25 devnull quit("Too many }'s", 0);
332 70bcc780 2004-03-25 devnull *cmpend[depth] = rep;
333 70bcc780 2004-03-25 devnull if(*cp == 0) continue;
334 70bcc780 2004-03-25 devnull goto comploop;
336 70bcc780 2004-03-25 devnull case '=':
337 70bcc780 2004-03-25 devnull rep->command = EQCOM;
338 70bcc780 2004-03-25 devnull if(rep->ad2.type != A_NONE)
339 70bcc780 2004-03-25 devnull quit(AD1MES, (char *) linebuf);
342 70bcc780 2004-03-25 devnull case ':':
343 70bcc780 2004-03-25 devnull if(rep->ad1.type != A_NONE)
344 70bcc780 2004-03-25 devnull quit(AD0MES, (char *) linebuf);
346 70bcc780 2004-03-25 devnull while(*cp == ' ')
348 70bcc780 2004-03-25 devnull tp = lab->asc;
349 70bcc780 2004-03-25 devnull while (*cp && *cp != ';' && *cp != ' ' && *cp != '\t' && *cp != '#') {
350 70bcc780 2004-03-25 devnull *tp++ = *cp++;
351 70bcc780 2004-03-25 devnull if(tp >= &(lab->asc[8]))
352 70bcc780 2004-03-25 devnull quit(LTL, (char *) linebuf);
354 70bcc780 2004-03-25 devnull *tp = '\0';
356 70bcc780 2004-03-25 devnull if(lpt = search(lab)) {
357 70bcc780 2004-03-25 devnull if(lpt->address)
358 70bcc780 2004-03-25 devnull quit("Duplicate labels: %S", (char *) linebuf);
359 70bcc780 2004-03-25 devnull } else {
360 70bcc780 2004-03-25 devnull lab->chain = 0;
361 70bcc780 2004-03-25 devnull lpt = lab;
362 70bcc780 2004-03-25 devnull if(++lab >= labend)
363 70bcc780 2004-03-25 devnull quit("Too many labels: %S", (char *) linebuf);
365 70bcc780 2004-03-25 devnull lpt->address = rep;
366 70bcc780 2004-03-25 devnull if (*cp == '#')
367 70bcc780 2004-03-25 devnull continue;
368 70bcc780 2004-03-25 devnull rep--; /* reuse this slot */
371 70bcc780 2004-03-25 devnull case 'a':
372 70bcc780 2004-03-25 devnull rep->command = ACOM;
373 70bcc780 2004-03-25 devnull if(rep->ad2.type != A_NONE)
374 70bcc780 2004-03-25 devnull quit(AD1MES, (char *) linebuf);
375 70bcc780 2004-03-25 devnull if(*cp == '\\') cp++;
376 70bcc780 2004-03-25 devnull if(*cp++ != '\n')
377 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
378 35d95bae 2004-03-25 devnull rep->u.text = p;
379 70bcc780 2004-03-25 devnull p = stext(p, addend);
381 70bcc780 2004-03-25 devnull case 'c':
382 70bcc780 2004-03-25 devnull rep->command = CCOM;
383 70bcc780 2004-03-25 devnull if(*cp == '\\') cp++;
384 70bcc780 2004-03-25 devnull if(*cp++ != '\n')
385 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
386 35d95bae 2004-03-25 devnull rep->u.text = p;
387 70bcc780 2004-03-25 devnull p = stext(p, addend);
389 70bcc780 2004-03-25 devnull case 'i':
390 70bcc780 2004-03-25 devnull rep->command = ICOM;
391 70bcc780 2004-03-25 devnull if(rep->ad2.type != A_NONE)
392 70bcc780 2004-03-25 devnull quit(AD1MES, (char *) linebuf);
393 70bcc780 2004-03-25 devnull if(*cp == '\\') cp++;
394 70bcc780 2004-03-25 devnull if(*cp++ != '\n')
395 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
396 35d95bae 2004-03-25 devnull rep->u.text = p;
397 70bcc780 2004-03-25 devnull p = stext(p, addend);
400 70bcc780 2004-03-25 devnull case 'g':
401 70bcc780 2004-03-25 devnull rep->command = GCOM;
404 70bcc780 2004-03-25 devnull case 'G':
405 70bcc780 2004-03-25 devnull rep->command = CGCOM;
408 70bcc780 2004-03-25 devnull case 'h':
409 70bcc780 2004-03-25 devnull rep->command = HCOM;
412 70bcc780 2004-03-25 devnull case 'H':
413 70bcc780 2004-03-25 devnull rep->command = CHCOM;
416 70bcc780 2004-03-25 devnull case 't':
417 70bcc780 2004-03-25 devnull rep->command = TCOM;
418 70bcc780 2004-03-25 devnull goto jtcommon;
420 70bcc780 2004-03-25 devnull case 'b':
421 70bcc780 2004-03-25 devnull rep->command = BCOM;
422 70bcc780 2004-03-25 devnull jtcommon:
423 70bcc780 2004-03-25 devnull while(*cp == ' ')cp++;
424 70bcc780 2004-03-25 devnull if(*cp == '\0') {
425 70bcc780 2004-03-25 devnull if(pt = ltab[0].chain) {
426 35d95bae 2004-03-25 devnull while(pt1 = pt->u.lb1)
427 70bcc780 2004-03-25 devnull pt = pt1;
428 35d95bae 2004-03-25 devnull pt->u.lb1 = rep;
430 70bcc780 2004-03-25 devnull ltab[0].chain = rep;
433 70bcc780 2004-03-25 devnull tp = lab->asc;
434 70bcc780 2004-03-25 devnull while((*tp++ = *cp++))
435 70bcc780 2004-03-25 devnull if(tp >= &(lab->asc[8]))
436 70bcc780 2004-03-25 devnull quit(LTL, (char *) linebuf);
438 70bcc780 2004-03-25 devnull tp[-1] = '\0';
440 70bcc780 2004-03-25 devnull if(lpt = search(lab)) {
441 70bcc780 2004-03-25 devnull if(lpt->address) {
442 35d95bae 2004-03-25 devnull rep->u.lb1 = lpt->address;
443 70bcc780 2004-03-25 devnull } else {
444 70bcc780 2004-03-25 devnull pt = lpt->chain;
445 35d95bae 2004-03-25 devnull while(pt1 = pt->u.lb1)
446 70bcc780 2004-03-25 devnull pt = pt1;
447 35d95bae 2004-03-25 devnull pt->u.lb1 = rep;
449 70bcc780 2004-03-25 devnull } else {
450 70bcc780 2004-03-25 devnull lab->chain = rep;
451 70bcc780 2004-03-25 devnull lab->address = 0;
452 70bcc780 2004-03-25 devnull if(++lab >= labend)
453 70bcc780 2004-03-25 devnull quit("Too many labels: %S",
454 70bcc780 2004-03-25 devnull (char *) linebuf);
458 70bcc780 2004-03-25 devnull case 'n':
459 70bcc780 2004-03-25 devnull rep->command = NCOM;
462 70bcc780 2004-03-25 devnull case 'N':
463 70bcc780 2004-03-25 devnull rep->command = CNCOM;
466 70bcc780 2004-03-25 devnull case 'p':
467 70bcc780 2004-03-25 devnull rep->command = PCOM;
470 70bcc780 2004-03-25 devnull case 'P':
471 70bcc780 2004-03-25 devnull rep->command = CPCOM;
474 70bcc780 2004-03-25 devnull case 'r':
475 70bcc780 2004-03-25 devnull rep->command = RCOM;
476 70bcc780 2004-03-25 devnull if(rep->ad2.type != A_NONE)
477 70bcc780 2004-03-25 devnull quit(AD1MES, (char *) linebuf);
478 70bcc780 2004-03-25 devnull if(*cp++ != ' ')
479 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
480 35d95bae 2004-03-25 devnull rep->u.text = p;
481 70bcc780 2004-03-25 devnull p = stext(p, addend);
484 70bcc780 2004-03-25 devnull case 'd':
485 70bcc780 2004-03-25 devnull rep->command = DCOM;
488 70bcc780 2004-03-25 devnull case 'D':
489 70bcc780 2004-03-25 devnull rep->command = CDCOM;
490 35d95bae 2004-03-25 devnull rep->u.lb1 = pspace;
493 70bcc780 2004-03-25 devnull case 'q':
494 70bcc780 2004-03-25 devnull rep->command = QCOM;
495 70bcc780 2004-03-25 devnull if(rep->ad2.type != A_NONE)
496 70bcc780 2004-03-25 devnull quit(AD1MES, (char *) linebuf);
499 70bcc780 2004-03-25 devnull case 'l':
500 70bcc780 2004-03-25 devnull rep->command = LCOM;
503 70bcc780 2004-03-25 devnull case 's':
504 70bcc780 2004-03-25 devnull rep->command = SCOM;
505 70bcc780 2004-03-25 devnull seof = *cp++;
506 35d95bae 2004-03-25 devnull if ((rep->u.re1 = compile()) == 0) {
507 70bcc780 2004-03-25 devnull if(!lastre)
508 70bcc780 2004-03-25 devnull quit("First RE may not be null.", 0);
509 35d95bae 2004-03-25 devnull rep->u.re1 = lastre;
511 70bcc780 2004-03-25 devnull rep->rhs = p;
512 70bcc780 2004-03-25 devnull if((p = compsub(p, addend)) == 0)
513 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
514 70bcc780 2004-03-25 devnull if(*cp == 'g') {
516 70bcc780 2004-03-25 devnull rep->gfl++;
517 70bcc780 2004-03-25 devnull } else if(gflag)
518 70bcc780 2004-03-25 devnull rep->gfl++;
520 70bcc780 2004-03-25 devnull if(*cp == 'p') {
522 70bcc780 2004-03-25 devnull rep->pfl = 1;
525 70bcc780 2004-03-25 devnull if(*cp == 'P') {
527 70bcc780 2004-03-25 devnull rep->pfl = 2;
530 70bcc780 2004-03-25 devnull if(*cp == 'w') {
532 70bcc780 2004-03-25 devnull if(*cp++ != ' ')
533 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
534 70bcc780 2004-03-25 devnull text(fname[nfiles]);
535 70bcc780 2004-03-25 devnull for(i = nfiles - 1; i >= 0; i--)
536 70bcc780 2004-03-25 devnull if(cmp(fname[nfiles],fname[i]) == 0) {
537 70bcc780 2004-03-25 devnull rep->fcode = fcode[i];
538 70bcc780 2004-03-25 devnull goto done;
540 70bcc780 2004-03-25 devnull if(nfiles >= MAXFILES)
541 70bcc780 2004-03-25 devnull quit("Too many files in w commands 1", 0);
542 70bcc780 2004-03-25 devnull rep->fcode = open_file(fname[nfiles]);
546 70bcc780 2004-03-25 devnull case 'w':
547 70bcc780 2004-03-25 devnull rep->command = WCOM;
548 70bcc780 2004-03-25 devnull if(*cp++ != ' ')
549 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
550 70bcc780 2004-03-25 devnull text(fname[nfiles]);
551 70bcc780 2004-03-25 devnull for(i = nfiles - 1; i >= 0; i--)
552 70bcc780 2004-03-25 devnull if(cmp(fname[nfiles], fname[i]) == 0) {
553 70bcc780 2004-03-25 devnull rep->fcode = fcode[i];
554 70bcc780 2004-03-25 devnull goto done;
556 70bcc780 2004-03-25 devnull if(nfiles >= MAXFILES){
557 70bcc780 2004-03-25 devnull fprint(2, "sed: Too many files in w commands 2 \n");
558 70bcc780 2004-03-25 devnull fprint(2, "nfiles = %d; MAXF = %d\n", nfiles, MAXFILES);
559 70bcc780 2004-03-25 devnull errexit();
561 70bcc780 2004-03-25 devnull rep->fcode = open_file(fname[nfiles]);
564 70bcc780 2004-03-25 devnull case 'x':
565 70bcc780 2004-03-25 devnull rep->command = XCOM;
568 70bcc780 2004-03-25 devnull case 'y':
569 70bcc780 2004-03-25 devnull rep->command = YCOM;
570 70bcc780 2004-03-25 devnull seof = *cp++;
571 70bcc780 2004-03-25 devnull if (ycomp(rep) == 0)
572 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
577 70bcc780 2004-03-25 devnull if(++rep >= pend)
578 70bcc780 2004-03-25 devnull quit("Too many commands, last: %S", (char *) linebuf);
580 70bcc780 2004-03-25 devnull if(*cp++ != '\0') {
581 70bcc780 2004-03-25 devnull if(cp[-1] == ';')
582 70bcc780 2004-03-25 devnull goto comploop;
583 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
589 70bcc780 2004-03-25 devnull Biobuf *
590 70bcc780 2004-03-25 devnull open_file(char *name)
592 70bcc780 2004-03-25 devnull Biobuf *bp;
595 70bcc780 2004-03-25 devnull if ((bp = malloc(sizeof(Biobuf))) == 0)
596 70bcc780 2004-03-25 devnull quit("Out of memory", 0);
597 70bcc780 2004-03-25 devnull if ((fd = open(name, OWRITE)) < 0 &&
598 70bcc780 2004-03-25 devnull (fd = create(name, OWRITE, 0666)) < 0)
599 70bcc780 2004-03-25 devnull quit("Cannot create %s", name);
600 70bcc780 2004-03-25 devnull Binit(bp, fd, OWRITE);
601 70bcc780 2004-03-25 devnull Bseek(bp, 0, 2);
602 70bcc780 2004-03-25 devnull fcode[nfiles++] = bp;
603 70bcc780 2004-03-25 devnull return bp;
607 70bcc780 2004-03-25 devnull compsub(Rune *rhs, Rune *end)
611 70bcc780 2004-03-25 devnull while ((r = *cp++) != '\0') {
612 70bcc780 2004-03-25 devnull if(r == '\\') {
613 70bcc780 2004-03-25 devnull if (rhs < end)
614 70bcc780 2004-03-25 devnull *rhs++ = 0xFFFF;
616 70bcc780 2004-03-25 devnull return 0;
617 70bcc780 2004-03-25 devnull r = *cp++;
618 70bcc780 2004-03-25 devnull if(r == 'n')
619 70bcc780 2004-03-25 devnull r = '\n';
620 70bcc780 2004-03-25 devnull } else {
621 70bcc780 2004-03-25 devnull if(r == seof) {
622 70bcc780 2004-03-25 devnull if (rhs < end)
623 70bcc780 2004-03-25 devnull *rhs++ = '\0';
625 70bcc780 2004-03-25 devnull return 0;
626 70bcc780 2004-03-25 devnull return rhs;
629 70bcc780 2004-03-25 devnull if (rhs < end)
630 70bcc780 2004-03-25 devnull *rhs++ = r;
632 70bcc780 2004-03-25 devnull return 0;
635 70bcc780 2004-03-25 devnull return 0;
638 70bcc780 2004-03-25 devnull Reprog *
639 70bcc780 2004-03-25 devnull compile(void)
642 70bcc780 2004-03-25 devnull char *ep;
643 70bcc780 2004-03-25 devnull char expbuf[512];
645 70bcc780 2004-03-25 devnull if((c = *cp++) == seof) /* '//' */
646 70bcc780 2004-03-25 devnull return 0;
647 70bcc780 2004-03-25 devnull ep = expbuf;
649 70bcc780 2004-03-25 devnull if (c == 0 || c == '\n')
650 70bcc780 2004-03-25 devnull quit(TMMES, (char *) linebuf);
651 70bcc780 2004-03-25 devnull if (c == '\\') {
652 70bcc780 2004-03-25 devnull if (ep >= expbuf+sizeof(expbuf))
653 70bcc780 2004-03-25 devnull quit(TMMES, (char *) linebuf);
654 70bcc780 2004-03-25 devnull ep += runetochar(ep, &c);
655 70bcc780 2004-03-25 devnull if ((c = *cp++) == 'n')
656 70bcc780 2004-03-25 devnull c = '\n';
658 70bcc780 2004-03-25 devnull if (ep >= expbuf+sizeof(expbuf))
659 70bcc780 2004-03-25 devnull quit(TMMES, (char *) linebuf);
660 70bcc780 2004-03-25 devnull ep += runetochar(ep, &c);
661 70bcc780 2004-03-25 devnull } while ((c = *cp++) != seof);
662 70bcc780 2004-03-25 devnull *ep = 0;
663 70bcc780 2004-03-25 devnull return lastre = regcomp(expbuf);
667 70bcc780 2004-03-25 devnull regerror(char *s)
669 70bcc780 2004-03-25 devnull USED(s);
670 70bcc780 2004-03-25 devnull quit(CGMES, (char *) linebuf);
674 70bcc780 2004-03-25 devnull newfile(enum PTYPE type, char *name)
676 70bcc780 2004-03-25 devnull if (type == P_ARG)
677 70bcc780 2004-03-25 devnull prog.pctl.curr = name;
678 70bcc780 2004-03-25 devnull else if ((prog.pctl.bp = Bopen(name, OREAD)) == 0)
679 70bcc780 2004-03-25 devnull quit("Cannot open pattern-file: %s\n", name);
680 70bcc780 2004-03-25 devnull prog.type = type;
684 70bcc780 2004-03-25 devnull rline(Rune *buf, Rune *end)
689 70bcc780 2004-03-25 devnull while ((c = getrune()) >= 0) {
691 70bcc780 2004-03-25 devnull if (r == '\\') {
692 70bcc780 2004-03-25 devnull if (buf <= end)
693 70bcc780 2004-03-25 devnull *buf++ = r;
694 70bcc780 2004-03-25 devnull if ((c = getrune()) < 0)
697 70bcc780 2004-03-25 devnull } else if (r == '\n') {
698 70bcc780 2004-03-25 devnull *buf = '\0';
699 70bcc780 2004-03-25 devnull return(1);
701 70bcc780 2004-03-25 devnull if (buf <= end)
702 70bcc780 2004-03-25 devnull *buf++ = r;
704 70bcc780 2004-03-25 devnull *buf = '\0';
705 70bcc780 2004-03-25 devnull return(-1);
709 70bcc780 2004-03-25 devnull getrune(void)
711 70bcc780 2004-03-25 devnull char *p;
715 70bcc780 2004-03-25 devnull if (prog.type == P_ARG) {
716 70bcc780 2004-03-25 devnull if ((p = prog.pctl.curr) != 0) {
717 70bcc780 2004-03-25 devnull if (*p) {
718 70bcc780 2004-03-25 devnull prog.pctl.curr += chartorune(&r, p);
720 70bcc780 2004-03-25 devnull } else {
721 70bcc780 2004-03-25 devnull c = '\n'; /* fake an end-of-line */
722 70bcc780 2004-03-25 devnull prog.pctl.curr = 0;
726 70bcc780 2004-03-25 devnull } else if ((c = Bgetrune(prog.pctl.bp)) < 0)
727 70bcc780 2004-03-25 devnull Bterm(prog.pctl.bp);
728 70bcc780 2004-03-25 devnull return c;
732 70bcc780 2004-03-25 devnull address(Addr *ap)
735 70bcc780 2004-03-25 devnull long lno;
737 70bcc780 2004-03-25 devnull if((c = *cp++) == '$')
738 70bcc780 2004-03-25 devnull ap->type = A_DOL;
739 70bcc780 2004-03-25 devnull else if(c == '/') {
740 70bcc780 2004-03-25 devnull seof = c;
741 4bda94f9 2004-03-25 devnull if (ap->u.rp = compile())
742 70bcc780 2004-03-25 devnull ap->type = A_RE;
744 70bcc780 2004-03-25 devnull ap->type = A_LAST;
746 70bcc780 2004-03-25 devnull else if (c >= '0' && c <= '9') {
747 70bcc780 2004-03-25 devnull lno = c-'0';
748 70bcc780 2004-03-25 devnull while ((c = *cp) >= '0' && c <= '9')
749 70bcc780 2004-03-25 devnull lno = lno*10 + *cp++-'0';
750 70bcc780 2004-03-25 devnull if(!lno)
751 70bcc780 2004-03-25 devnull quit("line number 0 is illegal",0);
752 70bcc780 2004-03-25 devnull ap->type = A_LINE;
753 4bda94f9 2004-03-25 devnull ap->u.line = lno;
757 70bcc780 2004-03-25 devnull ap->type = A_NONE;
762 70bcc780 2004-03-25 devnull cmp(char *a, char *b) /* compare characters */
764 70bcc780 2004-03-25 devnull while(*a == *b++)
765 70bcc780 2004-03-25 devnull if (*a == '\0')
766 70bcc780 2004-03-25 devnull return(0);
767 70bcc780 2004-03-25 devnull else a++;
768 70bcc780 2004-03-25 devnull return(1);
772 70bcc780 2004-03-25 devnull rcmp(Rune *a, Rune *b) /* compare runes */
774 70bcc780 2004-03-25 devnull while(*a == *b++)
775 70bcc780 2004-03-25 devnull if (*a == '\0')
776 70bcc780 2004-03-25 devnull return(0);
777 70bcc780 2004-03-25 devnull else a++;
778 70bcc780 2004-03-25 devnull return(1);
782 70bcc780 2004-03-25 devnull text(char *p) /* extract character string */
786 70bcc780 2004-03-25 devnull while(*cp == '\t' || *cp == ' ')
788 70bcc780 2004-03-25 devnull while (*cp) {
789 70bcc780 2004-03-25 devnull if ((r = *cp++) == '\\')
790 70bcc780 2004-03-25 devnull if ((r = *cp++) == 0)
792 70bcc780 2004-03-25 devnull if (r == '\n')
793 70bcc780 2004-03-25 devnull while (*cp == '\t' || *cp == ' ')
795 70bcc780 2004-03-25 devnull p += runetochar(p, &r);
797 70bcc780 2004-03-25 devnull *p++ = '\0';
798 70bcc780 2004-03-25 devnull return p;
802 70bcc780 2004-03-25 devnull stext(Rune *p, Rune *end) /* extract rune string */
804 70bcc780 2004-03-25 devnull while(*cp == '\t' || *cp == ' ')
806 70bcc780 2004-03-25 devnull while (*cp) {
807 70bcc780 2004-03-25 devnull if (*cp == '\\')
808 70bcc780 2004-03-25 devnull if (*++cp == 0)
810 70bcc780 2004-03-25 devnull if (p >= end-1)
811 70bcc780 2004-03-25 devnull quit(TMMES, (char *) linebuf);
812 70bcc780 2004-03-25 devnull if ((*p++ = *cp++) == '\n')
813 70bcc780 2004-03-25 devnull while(*cp == '\t' || *cp == ' ')
816 70bcc780 2004-03-25 devnull *p++ = 0;
817 70bcc780 2004-03-25 devnull return p;
822 70bcc780 2004-03-25 devnull search (Label *ptr)
824 70bcc780 2004-03-25 devnull Label *rp;
826 70bcc780 2004-03-25 devnull for (rp = ltab; rp < ptr; rp++)
827 70bcc780 2004-03-25 devnull if(rcmp(rp->asc, ptr->asc) == 0)
828 70bcc780 2004-03-25 devnull return(rp);
829 70bcc780 2004-03-25 devnull return(0);
833 70bcc780 2004-03-25 devnull dechain(void)
835 70bcc780 2004-03-25 devnull Label *lptr;
836 70bcc780 2004-03-25 devnull SedCom *rptr, *trptr;
838 70bcc780 2004-03-25 devnull for(lptr = ltab; lptr < lab; lptr++) {
840 70bcc780 2004-03-25 devnull if(lptr->address == 0)
841 70bcc780 2004-03-25 devnull quit("Undefined label: %S", (char *) lptr->asc);
843 70bcc780 2004-03-25 devnull if(lptr->chain) {
844 70bcc780 2004-03-25 devnull rptr = lptr->chain;
845 35d95bae 2004-03-25 devnull while(trptr = rptr->u.lb1) {
846 35d95bae 2004-03-25 devnull rptr->u.lb1 = lptr->address;
847 70bcc780 2004-03-25 devnull rptr = trptr;
849 35d95bae 2004-03-25 devnull rptr->u.lb1 = lptr->address;
855 70bcc780 2004-03-25 devnull ycomp(SedCom *r)
858 70bcc780 2004-03-25 devnull Rune *rp;
859 70bcc780 2004-03-25 devnull Rune c, *tsp, highc;
860 70bcc780 2004-03-25 devnull Rune *sp;
862 70bcc780 2004-03-25 devnull highc = 0;
863 70bcc780 2004-03-25 devnull for(tsp = cp; *tsp != seof; tsp++) {
864 70bcc780 2004-03-25 devnull if(*tsp == '\\')
866 70bcc780 2004-03-25 devnull if(*tsp == '\n' || *tsp == '\0')
867 70bcc780 2004-03-25 devnull return(0);
868 70bcc780 2004-03-25 devnull if (*tsp > highc) highc = *tsp;
871 35d95bae 2004-03-25 devnull if ((rp = r->u.text = (Rune *) malloc(sizeof(Rune)*(highc+2))) == 0)
872 70bcc780 2004-03-25 devnull quit("Out of memory", 0);
873 70bcc780 2004-03-25 devnull *rp++ = highc; /* save upper bound */
874 70bcc780 2004-03-25 devnull for (i = 0; i <= highc; i++)
875 70bcc780 2004-03-25 devnull rp[i] = i;
876 70bcc780 2004-03-25 devnull sp = cp;
877 70bcc780 2004-03-25 devnull while((c = *sp++) != seof) {
878 70bcc780 2004-03-25 devnull if(c == '\\' && *sp == 'n') {
880 70bcc780 2004-03-25 devnull c = '\n';
882 70bcc780 2004-03-25 devnull if((rp[c] = *tsp++) == '\\' && *tsp == 'n') {
883 70bcc780 2004-03-25 devnull rp[c] = '\n';
886 70bcc780 2004-03-25 devnull if(rp[c] == seof || rp[c] == '\0') {
887 35d95bae 2004-03-25 devnull free(r->u.re1);
888 35d95bae 2004-03-25 devnull r->u.re1 = 0;
889 70bcc780 2004-03-25 devnull return(0);
892 70bcc780 2004-03-25 devnull if(*tsp != seof) {
893 35d95bae 2004-03-25 devnull free(r->u.re1);
894 35d95bae 2004-03-25 devnull r->u.re1 = 0;
895 70bcc780 2004-03-25 devnull return(0);
897 70bcc780 2004-03-25 devnull cp = tsp+1;
898 70bcc780 2004-03-25 devnull return(1);
902 70bcc780 2004-03-25 devnull execute(void)
904 70bcc780 2004-03-25 devnull SedCom *ipc;
906 70bcc780 2004-03-25 devnull while (spend = gline(linebuf)){
907 70bcc780 2004-03-25 devnull for(ipc = pspace; ipc->command; ) {
908 70bcc780 2004-03-25 devnull if (!executable(ipc)) {
910 70bcc780 2004-03-25 devnull continue;
912 70bcc780 2004-03-25 devnull command(ipc);
914 70bcc780 2004-03-25 devnull if(delflag)
916 70bcc780 2004-03-25 devnull if(jflag) {
917 70bcc780 2004-03-25 devnull jflag = 0;
918 35d95bae 2004-03-25 devnull if((ipc = ipc->u.lb1) == 0)
924 70bcc780 2004-03-25 devnull if(!nflag && !delflag)
925 70bcc780 2004-03-25 devnull putline(&fout, linebuf, spend-linebuf);
926 70bcc780 2004-03-25 devnull if(aptr > abuf) {
927 70bcc780 2004-03-25 devnull arout();
929 70bcc780 2004-03-25 devnull delflag = 0;
932 70bcc780 2004-03-25 devnull /* determine if a statement should be applied to an input line */
934 70bcc780 2004-03-25 devnull executable(SedCom *ipc)
936 70bcc780 2004-03-25 devnull if (ipc->active) { /* Addr1 satisfied - accept until Addr2 */
937 70bcc780 2004-03-25 devnull if (ipc->active == 1) /* Second line */
938 70bcc780 2004-03-25 devnull ipc->active = 2;
939 70bcc780 2004-03-25 devnull switch(ipc->ad2.type) {
940 70bcc780 2004-03-25 devnull case A_NONE: /* No second addr; use first */
941 70bcc780 2004-03-25 devnull ipc->active = 0;
943 70bcc780 2004-03-25 devnull case A_DOL: /* Accept everything */
944 70bcc780 2004-03-25 devnull return !ipc->negfl;
945 70bcc780 2004-03-25 devnull case A_LINE: /* Line at end of range? */
946 4bda94f9 2004-03-25 devnull if (lnum <= ipc->ad2.u.line) {
947 4bda94f9 2004-03-25 devnull if (ipc->ad2.u.line == lnum)
948 70bcc780 2004-03-25 devnull ipc->active = 0;
949 70bcc780 2004-03-25 devnull return !ipc->negfl;
951 70bcc780 2004-03-25 devnull ipc->active = 0; /* out of range */
952 70bcc780 2004-03-25 devnull return ipc->negfl;
953 70bcc780 2004-03-25 devnull case A_RE: /* Check for matching R.E. */
954 4bda94f9 2004-03-25 devnull if (match(ipc->ad2.u.rp, linebuf))
955 70bcc780 2004-03-25 devnull ipc->active = 0;
956 70bcc780 2004-03-25 devnull return !ipc->negfl;
957 70bcc780 2004-03-25 devnull default: /* internal error */
958 70bcc780 2004-03-25 devnull quit("Internal error", 0);
961 70bcc780 2004-03-25 devnull switch (ipc->ad1.type) { /* Check first address */
962 70bcc780 2004-03-25 devnull case A_NONE: /* Everything matches */
963 70bcc780 2004-03-25 devnull return !ipc->negfl;
964 70bcc780 2004-03-25 devnull case A_DOL: /* Only last line */
965 70bcc780 2004-03-25 devnull if (dolflag)
966 70bcc780 2004-03-25 devnull return !ipc->negfl;
968 70bcc780 2004-03-25 devnull case A_LINE: /* Check line number */
969 4bda94f9 2004-03-25 devnull if (ipc->ad1.u.line == lnum) {
970 70bcc780 2004-03-25 devnull ipc->active = 1; /* In range */
971 70bcc780 2004-03-25 devnull return !ipc->negfl;
974 70bcc780 2004-03-25 devnull case A_RE: /* Check R.E. */
975 4bda94f9 2004-03-25 devnull if (match(ipc->ad1.u.rp, linebuf)) {
976 70bcc780 2004-03-25 devnull ipc->active = 1; /* In range */
977 70bcc780 2004-03-25 devnull return !ipc->negfl;
980 70bcc780 2004-03-25 devnull default:
981 70bcc780 2004-03-25 devnull quit("Internal error", 0);
983 70bcc780 2004-03-25 devnull return ipc->negfl;
987 70bcc780 2004-03-25 devnull match(Reprog *pattern, Rune *buf)
989 70bcc780 2004-03-25 devnull if (!pattern)
990 70bcc780 2004-03-25 devnull return 0;
991 70bcc780 2004-03-25 devnull subexp[0].s.rsp = buf;
992 70bcc780 2004-03-25 devnull subexp[0].e.rep = 0;
993 70bcc780 2004-03-25 devnull if (rregexec(pattern, linebuf, subexp, MAXSUB)) {
994 70bcc780 2004-03-25 devnull loc1 = subexp[0].s.rsp;
995 70bcc780 2004-03-25 devnull loc2 = subexp[0].e.rep;
996 70bcc780 2004-03-25 devnull return 1;
998 70bcc780 2004-03-25 devnull loc1 = loc2 = 0;
999 70bcc780 2004-03-25 devnull return 0;
1003 70bcc780 2004-03-25 devnull substitute(SedCom *ipc)
1005 70bcc780 2004-03-25 devnull int len;
1007 35d95bae 2004-03-25 devnull if(!match(ipc->u.re1, linebuf))
1008 70bcc780 2004-03-25 devnull return 0;
1011 70bcc780 2004-03-25 devnull * we have at least one match. some patterns, e.g. '$' or '^', can
1012 70bcc780 2004-03-25 devnull * produce zero-length matches, so during a global substitute we
1013 70bcc780 2004-03-25 devnull * must bump to the character after a zero-length match to keep from looping.
1015 70bcc780 2004-03-25 devnull sflag = 1;
1016 70bcc780 2004-03-25 devnull if(ipc->gfl == 0) /* single substitution */
1017 70bcc780 2004-03-25 devnull dosub(ipc->rhs);
1019 70bcc780 2004-03-25 devnull do{ /* global substitution */
1020 70bcc780 2004-03-25 devnull len = loc2-loc1; /* length of match */
1021 70bcc780 2004-03-25 devnull dosub(ipc->rhs); /* dosub moves loc2 */
1022 70bcc780 2004-03-25 devnull if(*loc2 == 0) /* end of string */
1024 70bcc780 2004-03-25 devnull if(len == 0) /* zero-length R.E. match */
1025 70bcc780 2004-03-25 devnull loc2++; /* bump over zero-length match */
1026 70bcc780 2004-03-25 devnull if(*loc2 == 0) /* end of string */
1028 35d95bae 2004-03-25 devnull } while(match(ipc->u.re1, loc2));
1029 70bcc780 2004-03-25 devnull return 1;
1033 70bcc780 2004-03-25 devnull dosub(Rune *rhsbuf)
1035 70bcc780 2004-03-25 devnull Rune *lp, *sp;
1036 70bcc780 2004-03-25 devnull Rune *rp;
1037 70bcc780 2004-03-25 devnull int c, n;
1039 70bcc780 2004-03-25 devnull lp = linebuf;
1040 70bcc780 2004-03-25 devnull sp = genbuf;
1041 70bcc780 2004-03-25 devnull rp = rhsbuf;
1042 70bcc780 2004-03-25 devnull while (lp < loc1)
1043 70bcc780 2004-03-25 devnull *sp++ = *lp++;
1044 70bcc780 2004-03-25 devnull while(c = *rp++) {
1045 70bcc780 2004-03-25 devnull if (c == '&') {
1046 70bcc780 2004-03-25 devnull sp = place(sp, loc1, loc2);
1047 70bcc780 2004-03-25 devnull continue;
1049 70bcc780 2004-03-25 devnull if (c == 0xFFFF && (c = *rp++) >= '1' && c < MAXSUB+'0') {
1050 70bcc780 2004-03-25 devnull n = c-'0';
1051 70bcc780 2004-03-25 devnull if (subexp[n].s.rsp && subexp[n].e.rep) {
1052 70bcc780 2004-03-25 devnull sp = place(sp, subexp[n].s.rsp, subexp[n].e.rep);
1053 70bcc780 2004-03-25 devnull continue;
1056 70bcc780 2004-03-25 devnull fprint(2, "sed: Invalid back reference \\%d\n",n);
1057 70bcc780 2004-03-25 devnull errexit();
1060 70bcc780 2004-03-25 devnull *sp++ = c;
1061 70bcc780 2004-03-25 devnull if (sp >= &genbuf[LBSIZE])
1062 70bcc780 2004-03-25 devnull fprint(2, "sed: Output line too long.\n");
1064 70bcc780 2004-03-25 devnull lp = loc2;
1065 70bcc780 2004-03-25 devnull loc2 = sp - genbuf + linebuf;
1066 70bcc780 2004-03-25 devnull while (*sp++ = *lp++)
1067 70bcc780 2004-03-25 devnull if (sp >= &genbuf[LBSIZE])
1068 70bcc780 2004-03-25 devnull fprint(2, "sed: Output line too long.\n");
1069 70bcc780 2004-03-25 devnull lp = linebuf;
1070 70bcc780 2004-03-25 devnull sp = genbuf;
1071 70bcc780 2004-03-25 devnull while (*lp++ = *sp++)
1073 70bcc780 2004-03-25 devnull spend = lp-1;
1077 70bcc780 2004-03-25 devnull place(Rune *sp, Rune *l1, Rune *l2)
1079 70bcc780 2004-03-25 devnull while (l1 < l2) {
1080 70bcc780 2004-03-25 devnull *sp++ = *l1++;
1081 70bcc780 2004-03-25 devnull if (sp >= &genbuf[LBSIZE])
1082 70bcc780 2004-03-25 devnull fprint(2, "sed: Output line too long.\n");
1084 70bcc780 2004-03-25 devnull return(sp);
1088 70bcc780 2004-03-25 devnull trans(int c)
1090 70bcc780 2004-03-25 devnull static char buf[] = "\\x0000";
1091 70bcc780 2004-03-25 devnull static char hex[] = "0123456789abcdef";
1093 70bcc780 2004-03-25 devnull switch(c) {
1094 70bcc780 2004-03-25 devnull case '\b':
1095 70bcc780 2004-03-25 devnull return "\\b";
1096 70bcc780 2004-03-25 devnull case '\n':
1097 70bcc780 2004-03-25 devnull return "\\n";
1098 70bcc780 2004-03-25 devnull case '\r':
1099 70bcc780 2004-03-25 devnull return "\\r";
1100 70bcc780 2004-03-25 devnull case '\t':
1101 70bcc780 2004-03-25 devnull return "\\t";
1102 70bcc780 2004-03-25 devnull case '\\':
1103 70bcc780 2004-03-25 devnull return "\\\\";
1105 70bcc780 2004-03-25 devnull buf[2] = hex[(c>>12)&0xF];
1106 70bcc780 2004-03-25 devnull buf[3] = hex[(c>>8)&0xF];
1107 70bcc780 2004-03-25 devnull buf[4] = hex[(c>>4)&0xF];
1108 70bcc780 2004-03-25 devnull buf[5] = hex[c&0xF];
1109 70bcc780 2004-03-25 devnull return buf;
1113 70bcc780 2004-03-25 devnull command(SedCom *ipc)
1115 70bcc780 2004-03-25 devnull int i, c;
1116 70bcc780 2004-03-25 devnull Rune *p1, *p2;
1117 70bcc780 2004-03-25 devnull char *ucp;
1118 70bcc780 2004-03-25 devnull Rune *rp;
1119 70bcc780 2004-03-25 devnull Rune *execp;
1121 70bcc780 2004-03-25 devnull switch(ipc->command) {
1123 70bcc780 2004-03-25 devnull case ACOM:
1124 70bcc780 2004-03-25 devnull *aptr++ = ipc;
1125 70bcc780 2004-03-25 devnull if(aptr >= abuf+MAXADDS) {
1126 70bcc780 2004-03-25 devnull quit("sed: Too many appends after line %ld\n",
1127 70bcc780 2004-03-25 devnull (char *) lnum);
1129 70bcc780 2004-03-25 devnull *aptr = 0;
1131 70bcc780 2004-03-25 devnull case CCOM:
1132 70bcc780 2004-03-25 devnull delflag = 1;
1133 70bcc780 2004-03-25 devnull if(ipc->active == 1) {
1134 35d95bae 2004-03-25 devnull for(rp = ipc->u.text; *rp; rp++)
1135 70bcc780 2004-03-25 devnull Bputrune(&fout, *rp);
1136 70bcc780 2004-03-25 devnull Bputc(&fout, '\n');
1139 70bcc780 2004-03-25 devnull case DCOM:
1140 70bcc780 2004-03-25 devnull delflag++;
1142 70bcc780 2004-03-25 devnull case CDCOM:
1143 70bcc780 2004-03-25 devnull p1 = p2 = linebuf;
1144 70bcc780 2004-03-25 devnull while(*p1 != '\n') {
1145 70bcc780 2004-03-25 devnull if(*p1++ == 0) {
1146 70bcc780 2004-03-25 devnull delflag++;
1147 70bcc780 2004-03-25 devnull return;
1151 70bcc780 2004-03-25 devnull while(*p2++ = *p1++)
1153 70bcc780 2004-03-25 devnull spend = p2-1;
1154 70bcc780 2004-03-25 devnull jflag++;
1156 70bcc780 2004-03-25 devnull case EQCOM:
1157 70bcc780 2004-03-25 devnull Bprint(&fout, "%ld\n", lnum);
1159 70bcc780 2004-03-25 devnull case GCOM:
1160 70bcc780 2004-03-25 devnull p1 = linebuf;
1161 70bcc780 2004-03-25 devnull p2 = holdsp;
1162 70bcc780 2004-03-25 devnull while(*p1++ = *p2++)
1164 70bcc780 2004-03-25 devnull spend = p1-1;
1166 70bcc780 2004-03-25 devnull case CGCOM:
1167 70bcc780 2004-03-25 devnull *spend++ = '\n';
1168 70bcc780 2004-03-25 devnull p1 = spend;
1169 70bcc780 2004-03-25 devnull p2 = holdsp;
1170 70bcc780 2004-03-25 devnull while(*p1++ = *p2++)
1171 70bcc780 2004-03-25 devnull if(p1 >= lbend)
1173 70bcc780 2004-03-25 devnull spend = p1-1;
1175 70bcc780 2004-03-25 devnull case HCOM:
1176 70bcc780 2004-03-25 devnull p1 = holdsp;
1177 70bcc780 2004-03-25 devnull p2 = linebuf;
1178 70bcc780 2004-03-25 devnull while(*p1++ = *p2++);
1179 70bcc780 2004-03-25 devnull hspend = p1-1;
1181 70bcc780 2004-03-25 devnull case CHCOM:
1182 70bcc780 2004-03-25 devnull *hspend++ = '\n';
1183 70bcc780 2004-03-25 devnull p1 = hspend;
1184 70bcc780 2004-03-25 devnull p2 = linebuf;
1185 70bcc780 2004-03-25 devnull while(*p1++ = *p2++)
1186 70bcc780 2004-03-25 devnull if(p1 >= hend)
1188 70bcc780 2004-03-25 devnull hspend = p1-1;
1190 70bcc780 2004-03-25 devnull case ICOM:
1191 35d95bae 2004-03-25 devnull for(rp = ipc->u.text; *rp; rp++)
1192 70bcc780 2004-03-25 devnull Bputrune(&fout, *rp);
1193 70bcc780 2004-03-25 devnull Bputc(&fout, '\n');
1195 70bcc780 2004-03-25 devnull case BCOM:
1196 70bcc780 2004-03-25 devnull jflag = 1;
1198 70bcc780 2004-03-25 devnull case LCOM:
1200 70bcc780 2004-03-25 devnull for (i = 0, rp = linebuf; *rp; rp++) {
1201 70bcc780 2004-03-25 devnull c = *rp;
1202 70bcc780 2004-03-25 devnull if(c >= 0x20 && c < 0x7F && c != '\\') {
1203 70bcc780 2004-03-25 devnull Bputc(&fout, c);
1204 70bcc780 2004-03-25 devnull if(i++ > 71) {
1205 70bcc780 2004-03-25 devnull Bprint(&fout, "\\\n");
1208 70bcc780 2004-03-25 devnull } else {
1209 70bcc780 2004-03-25 devnull for (ucp = trans(*rp); *ucp; ucp++){
1210 70bcc780 2004-03-25 devnull c = *ucp;
1211 70bcc780 2004-03-25 devnull Bputc(&fout, c);
1212 70bcc780 2004-03-25 devnull if(i++ > 71) {
1213 70bcc780 2004-03-25 devnull Bprint(&fout, "\\\n");
1219 70bcc780 2004-03-25 devnull if(c == ' ')
1220 70bcc780 2004-03-25 devnull Bprint(&fout, "\\n");
1221 70bcc780 2004-03-25 devnull Bputc(&fout, '\n');
1223 70bcc780 2004-03-25 devnull case NCOM:
1224 70bcc780 2004-03-25 devnull if(!nflag)
1225 70bcc780 2004-03-25 devnull putline(&fout, linebuf, spend-linebuf);
1227 70bcc780 2004-03-25 devnull if(aptr > abuf)
1228 70bcc780 2004-03-25 devnull arout();
1229 70bcc780 2004-03-25 devnull if((execp = gline(linebuf)) == 0) {
1230 70bcc780 2004-03-25 devnull delflag = 1;
1233 70bcc780 2004-03-25 devnull spend = execp;
1235 70bcc780 2004-03-25 devnull case CNCOM:
1236 70bcc780 2004-03-25 devnull if(aptr > abuf)
1237 70bcc780 2004-03-25 devnull arout();
1238 70bcc780 2004-03-25 devnull *spend++ = '\n';
1239 70bcc780 2004-03-25 devnull if((execp = gline(spend)) == 0) {
1240 70bcc780 2004-03-25 devnull delflag = 1;
1243 70bcc780 2004-03-25 devnull spend = execp;
1245 70bcc780 2004-03-25 devnull case PCOM:
1246 70bcc780 2004-03-25 devnull putline(&fout, linebuf, spend-linebuf);
1248 70bcc780 2004-03-25 devnull case CPCOM:
1250 70bcc780 2004-03-25 devnull for(rp = linebuf; *rp && *rp != '\n'; rp++)
1251 70bcc780 2004-03-25 devnull Bputc(&fout, *rp);
1252 70bcc780 2004-03-25 devnull Bputc(&fout, '\n');
1254 70bcc780 2004-03-25 devnull case QCOM:
1255 70bcc780 2004-03-25 devnull if(!nflag)
1256 70bcc780 2004-03-25 devnull putline(&fout, linebuf, spend-linebuf);
1257 70bcc780 2004-03-25 devnull if(aptr > abuf)
1258 70bcc780 2004-03-25 devnull arout();
1259 70bcc780 2004-03-25 devnull exits(0);
1260 70bcc780 2004-03-25 devnull case RCOM:
1261 70bcc780 2004-03-25 devnull *aptr++ = ipc;
1262 70bcc780 2004-03-25 devnull if(aptr >= &abuf[MAXADDS])
1263 70bcc780 2004-03-25 devnull quit("sed: Too many reads after line %ld\n",
1264 70bcc780 2004-03-25 devnull (char *) lnum);
1265 70bcc780 2004-03-25 devnull *aptr = 0;
1267 70bcc780 2004-03-25 devnull case SCOM:
1268 70bcc780 2004-03-25 devnull i = substitute(ipc);
1269 70bcc780 2004-03-25 devnull if(i && ipc->pfl)
1270 70bcc780 2004-03-25 devnull if(ipc->pfl == 1)
1271 70bcc780 2004-03-25 devnull putline(&fout, linebuf, spend-linebuf);
1273 70bcc780 2004-03-25 devnull goto cpcom;
1274 70bcc780 2004-03-25 devnull if(i && ipc->fcode)
1275 70bcc780 2004-03-25 devnull goto wcom;
1278 70bcc780 2004-03-25 devnull case TCOM:
1279 70bcc780 2004-03-25 devnull if(sflag == 0) break;
1280 70bcc780 2004-03-25 devnull sflag = 0;
1281 70bcc780 2004-03-25 devnull jflag = 1;
1285 70bcc780 2004-03-25 devnull case WCOM:
1286 70bcc780 2004-03-25 devnull putline(ipc->fcode,linebuf, spend-linebuf);
1288 70bcc780 2004-03-25 devnull case XCOM:
1289 70bcc780 2004-03-25 devnull p1 = linebuf;
1290 70bcc780 2004-03-25 devnull p2 = genbuf;
1291 70bcc780 2004-03-25 devnull while(*p2++ = *p1++);
1292 70bcc780 2004-03-25 devnull p1 = holdsp;
1293 70bcc780 2004-03-25 devnull p2 = linebuf;
1294 70bcc780 2004-03-25 devnull while(*p2++ = *p1++);
1295 70bcc780 2004-03-25 devnull spend = p2 - 1;
1296 70bcc780 2004-03-25 devnull p1 = genbuf;
1297 70bcc780 2004-03-25 devnull p2 = holdsp;
1298 70bcc780 2004-03-25 devnull while(*p2++ = *p1++);
1299 70bcc780 2004-03-25 devnull hspend = p2 - 1;
1301 70bcc780 2004-03-25 devnull case YCOM:
1302 70bcc780 2004-03-25 devnull p1 = linebuf;
1303 35d95bae 2004-03-25 devnull p2 = ipc->u.text;
1304 70bcc780 2004-03-25 devnull for (i = *p2++; *p1; p1++){
1305 70bcc780 2004-03-25 devnull if (*p1 <= i) *p1 = p2[*p1];
1313 70bcc780 2004-03-25 devnull putline(Biobuf *bp, Rune *buf, int n)
1315 70bcc780 2004-03-25 devnull while (n--)
1316 70bcc780 2004-03-25 devnull Bputrune(bp, *buf++);
1317 70bcc780 2004-03-25 devnull Bputc(bp, '\n');
1321 70bcc780 2004-03-25 devnull ecmp(Rune *a, Rune *b, int count)
1323 70bcc780 2004-03-25 devnull while(count--)
1324 70bcc780 2004-03-25 devnull if(*a++ != *b++) return(0);
1325 70bcc780 2004-03-25 devnull return(1);
1329 70bcc780 2004-03-25 devnull arout(void)
1331 70bcc780 2004-03-25 devnull Rune *p1;
1332 70bcc780 2004-03-25 devnull Biobuf *fi;
1334 70bcc780 2004-03-25 devnull char *s;
1335 70bcc780 2004-03-25 devnull char buf[128];
1337 70bcc780 2004-03-25 devnull for (aptr = abuf; *aptr; aptr++) {
1338 70bcc780 2004-03-25 devnull if((*aptr)->command == ACOM) {
1339 35d95bae 2004-03-25 devnull for(p1 = (*aptr)->u.text; *p1; p1++ )
1340 70bcc780 2004-03-25 devnull Bputrune(&fout, *p1);
1341 70bcc780 2004-03-25 devnull Bputc(&fout, '\n');
1342 70bcc780 2004-03-25 devnull } else {
1343 35d95bae 2004-03-25 devnull for(s = buf, p1= (*aptr)->u.text; *p1; p1++)
1344 70bcc780 2004-03-25 devnull s += runetochar(s, p1);
1345 70bcc780 2004-03-25 devnull *s = '\0';
1346 70bcc780 2004-03-25 devnull if((fi = Bopen(buf, OREAD)) == 0)
1347 70bcc780 2004-03-25 devnull continue;
1348 70bcc780 2004-03-25 devnull while((c = Bgetc(fi)) >= 0)
1349 70bcc780 2004-03-25 devnull Bputc(&fout, c);
1350 70bcc780 2004-03-25 devnull Bterm(fi);
1353 70bcc780 2004-03-25 devnull aptr = abuf;
1354 70bcc780 2004-03-25 devnull *aptr = 0;
1358 70bcc780 2004-03-25 devnull errexit(void)
1360 70bcc780 2004-03-25 devnull exits("error");
1364 70bcc780 2004-03-25 devnull quit (char *msg, char *arg)
1366 70bcc780 2004-03-25 devnull fprint(2, "sed: ");
1367 70bcc780 2004-03-25 devnull fprint(2, msg, arg);
1368 70bcc780 2004-03-25 devnull fprint(2, "\n");
1369 70bcc780 2004-03-25 devnull errexit();
1373 70bcc780 2004-03-25 devnull gline(Rune *addr)
1375 70bcc780 2004-03-25 devnull long c;
1376 70bcc780 2004-03-25 devnull Rune *p;
1378 70bcc780 2004-03-25 devnull static long peekc = 0;
1380 70bcc780 2004-03-25 devnull if (f == 0 && opendata() < 0)
1381 70bcc780 2004-03-25 devnull return 0;
1382 70bcc780 2004-03-25 devnull sflag = 0;
1383 70bcc780 2004-03-25 devnull lnum++;
1384 70bcc780 2004-03-25 devnull /* Bflush(&fout);********* dumped 4/30/92 - bobf****/
1386 70bcc780 2004-03-25 devnull p = addr;
1387 70bcc780 2004-03-25 devnull for (c = (peekc ? peekc : Bgetrune(f)); c >= 0; c = Bgetrune(f)) {
1388 70bcc780 2004-03-25 devnull if (c == '\n') {
1389 70bcc780 2004-03-25 devnull if ((peekc = Bgetrune(f)) < 0) {
1390 70bcc780 2004-03-25 devnull if (fhead == 0)
1391 70bcc780 2004-03-25 devnull dolflag = 1;
1393 70bcc780 2004-03-25 devnull *p = '\0';
1394 70bcc780 2004-03-25 devnull return p;
1396 70bcc780 2004-03-25 devnull if (c && p < lbend)
1397 70bcc780 2004-03-25 devnull *p++ = c;
1399 70bcc780 2004-03-25 devnull /* return partial final line, adding implicit newline */
1400 70bcc780 2004-03-25 devnull if(p != addr) {
1401 70bcc780 2004-03-25 devnull *p = '\0';
1402 70bcc780 2004-03-25 devnull peekc = -1;
1403 70bcc780 2004-03-25 devnull if (fhead == 0)
1404 70bcc780 2004-03-25 devnull dolflag = 1;
1405 70bcc780 2004-03-25 devnull return p;
1407 70bcc780 2004-03-25 devnull peekc = 0;
1408 70bcc780 2004-03-25 devnull Bterm(f);
1409 70bcc780 2004-03-25 devnull } while (opendata() > 0); /* Switch to next stream */
1411 70bcc780 2004-03-25 devnull return 0;
1414 70bcc780 2004-03-25 devnull /* Data file input section - the intent is to transparently
1415 70bcc780 2004-03-25 devnull * catenate all data input streams.
1418 70bcc780 2004-03-25 devnull enroll(char *filename) /* Add a file to the input file cache */
1420 70bcc780 2004-03-25 devnull FileCache *fp;
1422 70bcc780 2004-03-25 devnull if ((fp = (FileCache *) malloc(sizeof (FileCache))) == 0)
1423 70bcc780 2004-03-25 devnull quit("Out of memory", 0);
1424 70bcc780 2004-03-25 devnull if (ftail == 0)
1425 70bcc780 2004-03-25 devnull fhead = fp;
1427 70bcc780 2004-03-25 devnull ftail->next = fp;
1428 70bcc780 2004-03-25 devnull ftail = fp;
1429 70bcc780 2004-03-25 devnull fp->next = 0;
1430 70bcc780 2004-03-25 devnull fp->name = filename; /* 0 => stdin */
1434 70bcc780 2004-03-25 devnull opendata(void)
1436 70bcc780 2004-03-25 devnull if (fhead == 0)
1437 70bcc780 2004-03-25 devnull return -1;
1438 70bcc780 2004-03-25 devnull if (fhead->name) {
1439 70bcc780 2004-03-25 devnull if ((f = Bopen(fhead->name, OREAD)) == 0)
1440 70bcc780 2004-03-25 devnull quit("Can't open %s", fhead->name);
1441 70bcc780 2004-03-25 devnull } else {
1442 70bcc780 2004-03-25 devnull Binit(&bstdin, 0, OREAD);
1443 70bcc780 2004-03-25 devnull f = &bstdin;
1445 70bcc780 2004-03-25 devnull fhead = fhead->next;
1446 70bcc780 2004-03-25 devnull return 1;