4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 #include <sys/types.h>
40 union reptr
*abuf
[ABUFSIZE
+1];
45 char genbuf
[LBSIZE
+1];
52 char holdsp
[LBSIZE
+1];
56 long long tlno
[NLINES
];
94 char rub
[] = {"\\177"};
98 static int match(char *expbuf
, int gf
);
99 static int substitute(union reptr
*ipc
);
100 static void dosub(char *rhsbuf
, int n
);
101 static void command(union reptr
*ipc
);
102 static void arout(void);
113 if ((f
= open(file
, 0)) < 0) {
114 (void) fprintf(stderr
, "sed: ");
120 ifname
= "standard input";
133 if((execp
= gline(linebuf
)) == 0) {
139 for(ipc
= ptrspace
; ipc
->r1
.command
; ) {
149 } else if(*p2
== CLNUM
) {
150 c
= (unsigned char)p2
[1];
158 if(lnum
== tlno
[c
]) {
161 } else if(match(p2
, 0)) {
164 } else if(*p1
== CEND
) {
172 } else if(*p1
== CLNUM
) {
173 c
= (unsigned char)p1
[1];
174 if(lnum
!= tlno
[c
]) {
182 } else if(match(p1
, 0)) {
205 if((ipc
= ipc
->r2
.lb1
) == 0) {
213 if(!nflag
&& !delflag
) {
214 for(p1
= linebuf
; p1
< spend
; p1
++)
215 (void) putc(*p1
, stdout
);
216 (void) putc('\n', stdout
);
229 match(char *expbuf
, int gf
)
234 if(*expbuf
) return(0);
242 return(step(p1
, expbuf
));
246 substitute(union reptr
*ipc
)
248 if(match(ipc
->r1
.re1
, 0) == 0) return(0);
251 sflag
= 0; /* Flags if any substitution was made */
252 dosub(ipc
->r1
.rhs
, ipc
->r1
.gfl
);
256 if(match(ipc
->r1
.re1
, 1) == 0) break;
257 dosub(ipc
->r1
.rhs
, ipc
->r1
.gfl
);
264 dosub(char *rhsbuf
, int n
)
271 if(n
!= numpass
) return;
281 sp
= place(sp
, loc1
, loc2
);
282 else if (c
== '\\') {
284 if (c
>= '1' && c
< NBRA
+'1')
285 sp
= place(sp
, braslist
[c
-'1'], braelist
[c
-'1']);
290 if (sp
== &genbuf
[LBSIZE
+1]) {
291 (void) fprintf(stderr
, "Output line too long.\n");
297 loc2
= sp
- genbuf
+ linebuf
;
299 if (sp
== &genbuf
[LBSIZE
+1]) {
300 (void) fprintf(stderr
, "Output line too long.\n");
307 while (*lp
++ = *sp
++);
311 char *place(asp
, al1
, al2
)
312 char *asp
, *al1
, *al2
;
321 if (sp
== &genbuf
[LBSIZE
+1])
328 command(union reptr
*ipc
)
335 switch(ipc
->r1
.command
) {
338 if(aptr
>= &abuf
[ABUFSIZE
]) {
339 (void) fprintf(stderr
, "Too many appends or reads after line %lld\n",
349 if(!ipc
->r1
.inar
|| dolflag
) {
350 for(p1
= ipc
->r1
.re1
; *p1
; )
351 (void) putc(*p1
++, stdout
);
352 (void) putc('\n', stdout
);
369 while(*p2
++ = *p1
++);
375 (void) fprintf(stdout
, "%lld\n", lnum
);
381 while(*p1
++ = *p2
++);
390 if (p1
== &linebuf
[LBSIZE
+1]) {
391 (void) fprintf(stderr
, "Output line too long.\n");
394 } while(*p1
++ = *p2
++);
401 while(*p1
++ = *p2
++);
410 if (p1
== &holdsp
[LBSIZE
+1]) {
411 (void) fprintf(stderr
, "Hold space overflowed.\n");
414 } while(*p1
++ = *p2
++);
419 for(p1
= ipc
->r1
.re1
; *p1
; )
420 (void) putc(*p1
++, stdout
);
421 (void) putc('\n', stdout
);
434 if((unsigned char)*p1
>= 040) {
440 (void) fprintf(stdout
, "%s\n", genbuf
);
447 if(!isprint(*p1
& 0377)) {
451 (void) fprintf(stdout
, "%s\n", genbuf
);
454 *p2
++ = (*p1
>> 6) + '0';
457 (void) fprintf(stdout
, "%s\n", genbuf
);
460 *p2
++ = ((*p1
>> 3) & 07) + '0';
463 (void) fprintf(stdout
, "%s\n", genbuf
);
466 *p2
++ = (*p1
++ & 07) + '0';
469 (void) fprintf(stdout
, "%s\n", genbuf
);
476 (void) fprintf(stdout
, "%s\n", genbuf
);
481 p3
= trans
[(unsigned char)*p1
-1];
485 (void) fprintf(stdout
, "%s\n", genbuf
);
492 (void) fprintf(stdout
, "%s\n", genbuf
);
497 for(p1
= linebuf
; p1
< spend
; p1
++)
498 (void) putc(*p1
, stdout
);
499 (void) putc('\n', stdout
);
504 if((execp
= gline(linebuf
)) == 0) {
516 if((execp
= gline(spend
)) == 0) {
525 for(p1
= linebuf
; p1
< spend
; p1
++)
526 (void) putc(*p1
, stdout
);
527 (void) putc('\n', stdout
);
531 for(p1
= linebuf
; *p1
!= '\n' && *p1
!= '\0'; )
532 (void) putc(*p1
++, stdout
);
533 (void) putc('\n', stdout
);
538 for(p1
= linebuf
; p1
< spend
; p1
++)
539 (void) putc(*p1
, stdout
);
540 (void) putc('\n', stdout
);
542 if(aptr
> abuf
) arout();
543 (void) fclose(stdout
);
546 if(aptr
>= &abuf
[ABUFSIZE
]) {
547 (void) fprintf(stderr
, "Too many appends or reads after line %lld\n",
557 if(ipc
->r1
.pfl
&& nflag
&& i
)
558 if(ipc
->r1
.pfl
== 1) {
559 for(p1
= linebuf
; p1
< spend
; p1
++)
560 (void) putc(*p1
, stdout
);
561 (void) putc('\n', stdout
);
565 if(i
&& ipc
->r1
.fcode
)
570 if(sflag
== 0) break;
577 (void) fprintf(ipc
->r1
.fcode
, "%s\n", linebuf
);
578 (void) fflush(ipc
->r1
.fcode
);
583 while(*p2
++ = *p1
++);
586 while(*p2
++ = *p1
++);
590 while(*p2
++ = *p1
++);
597 while(*p1
= p2
[(unsigned char)*p1
]) p1
++;
613 if(f
< 0 || (c
= read(f
, ibuf
, BUFSIZ
)) == 0) {
617 (void) fprintf(stderr
, "sed: error reading ");
624 if ((c
= *p2
++) == '\n') {
626 if(f
< 0 || (c
= read(f
, ibuf
, BUFSIZ
)) == 0) {
635 (void) fprintf(stderr
, "sed: error reading ");
646 if(p1
< &linebuf
[LBSIZE
])
656 char *comple(x1
, ep
, x3
, x4
)
663 p
= compile(x1
, ep
+ 1, x3
, x4
);
676 comperr("Range endpoint too large: %s");
680 comperr("Bad number: %s");
684 comperr("``\\digit'' out of range: %s");
688 comperr("Illegal or missing delimiter: %s");
692 comperr("No remembered search string: %s");
696 comperr("\\( \\) imbalance: %s");
700 comperr("Too many \\(: %s");
704 comperr("More than 2 numbers given in \\{ \\}: %s");
708 comperr("} expected after \\: %s");
712 comperr("First number exceeds second in \\{ \\}: %s");
716 comperr("[ ] imbalance: %s");
724 (void) fprintf(stderr
, "Unknown regexp error code %d: %s\n",
742 if((*aptr
)->r1
.command
== ACOM
) {
743 for(p1
= (*aptr
)->r1
.re1
; *p1
; )
744 (void) putc(*p1
++, stdout
);
745 (void) putc('\n', stdout
);
747 if((fi
= fopen((*aptr
)->r1
.re1
, "r")) == NULL
)
749 while((t
= getc(fi
)) != EOF
) {
751 (void) putc(c
, stdout
);