📄 sed0.c
字号:
/* sed0.c 3.2 87/04/01 */#ifndef lintstatic char *sccsid = "@(#)sed0.c 4.1 (ULTRIX) 7/17/90";#endif lint/************************************************************************ * * * Copyright (c) 1986 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************/#include <stdio.h>#include "sed.h"/************************************************************************* Modification history: ** 01 Lie-Min Hioe, 21-July-1989* Make character class regular expression operate in an* 8-bit transparent manner. * * 02 Lie-Min Hioe, 24-July-1989* Add the construction \?regular expression? for xpg3* conformancy. * *************************************************************************/struct label *labtab = ltab;char CGMES[] = "command garbled: %s\n";char TMMES[] = "Too much text: %s\n";char LTL[] = "Label too long: %s\n";char AD0MES[] = "No addresses allowed: %s\n";char AD1MES[] = "Only one address allowed: %s\n";char bittab[] = { 1, 2, 4, 8, 16, 32, 64, 128 };main(argc, argv)char *argv[];{ eargc = argc; eargv = argv; badp = &bad; aptr = abuf; lab = labtab + 1; /* 0 reserved for end-pointer */ rep = ptrspace; rep->ad1 = respace; lbend = &linebuf[LBSIZE]; hend = &holdsp[LBSIZE]; lcomend = &genbuf[71]; ptrend = &ptrspace[PTRSIZE]; reend = &respace[RESIZE]; labend = &labtab[LABSIZE]; lnum = 0; pending = 0; depth = 0; spend = linebuf; hspend = holdsp; fcode[0] = stdout; nfiles = 1; if(eargc == 1) exit(0); while (--eargc > 0 && (++eargv)[0][0] == '-') switch (eargv[0][1]) { case 'n': nflag++; continue; case 'f': if(eargc-- <= 0) exit(2); if((fin = fopen(*++eargv, "r")) == NULL) { fprintf(stderr, "Cannot open pattern-file: %s\n", *eargv); exit(2); } fcomp(); fclose(fin); continue; case 'e': eflag++; fcomp(); eflag = 0; continue; case 'g': gflag++; continue; default: fprintf(stdout, "Unknown flag: %c\n", eargv[0][1]); continue; } if(compfl == 0) { eargv--; eargc++; eflag++; fcomp(); eargv++; eargc--; eflag = 0; } if(depth) { fprintf(stderr, "Too many {'s"); exit(2); } labtab->address = rep; dechain();/* abort(); /*DEBUG*/ if(eargc <= 0) execute((char *)NULL); else while(--eargc >= 0) { execute(*eargv++); } fclose(stdout); exit(0);}fcomp(){ register char *p, *op, *tp; char *address(); union reptr *pt, *pt1; int i; struct label *lpt; compfl = 1; op = lastre; if(rline(linebuf) < 0) return; if(*linebuf == '#') { if(linebuf[1] == 'n') nflag = 1; } else { cp = linebuf; goto comploop; } for(;;) { if(rline(linebuf) < 0) break; cp = linebuf;comploop:/* fprintf(stdout, "cp: %s\n", cp); /*DEBUG*/ while(*cp == ' ' || *cp == '\t') cp++; if(*cp == '\0' || *cp == '#') continue; if(*cp == ';') { cp++; goto comploop; } p = address(rep->ad1); if(p == badp) { fprintf(stderr, CGMES, linebuf); exit(2); } if(p == rep->ad1) { if(op) rep->ad1 = op; else { fprintf(stderr, "First RE may not be null\n"); exit(2); } } else if(p == 0) { p = rep->ad1; rep->ad1 = 0; } else { op = rep->ad1; if(*cp == ',' || *cp == ';') { cp++; if((rep->ad2 = p) > reend) { fprintf(stderr, TMMES, linebuf); exit(2); } p = address(rep->ad2); if(p == badp || p == 0) { fprintf(stderr, CGMES, linebuf); exit(2); } if(p == rep->ad2) rep->ad2 = op; else op = rep->ad2; } else rep->ad2 = 0; } if(p > reend) { fprintf(stderr, "Too much text: %s\n", linebuf); exit(2); } while(*cp == ' ' || *cp == '\t') cp++;swit: switch(*cp++) { default: fprintf(stderr, "Unrecognized command: %s\n", linebuf); exit(2); case '!': rep->negfl = 1; goto swit; case '{': rep->command = BCOM; rep->negfl = !(rep->negfl); cmpend[depth++] = &rep->lb1; if(++rep >= ptrend) { fprintf(stderr, "Too many commands: %s\n", linebuf); exit(2); } rep->ad1 = p; if(*cp == '\0') continue; goto comploop; case '}': if(rep->ad1) { fprintf(stderr, AD0MES, linebuf); exit(2); } if(--depth < 0) { fprintf(stderr, "Too many }'s\n"); exit(2); } *cmpend[depth] = rep; rep->ad1 = p; continue; case '=': rep->command = EQCOM; if(rep->ad2) { fprintf(stderr, AD1MES, linebuf); exit(2); } break; case ':': if(rep->ad1) { fprintf(stderr, AD0MES, linebuf); exit(2); } while(*cp++ == ' '); cp--; tp = lab->asc; while((*tp++ = *cp++)) if(tp >= &(lab->asc[8])) { fprintf(stderr, LTL, linebuf); exit(2); } *--tp = '\0'; if(lpt = search(lab)) { if(lpt->address) { fprintf(stderr, "Duplicate labels: %s\n", linebuf); exit(2); } } else { lab->chain = 0; lpt = lab; if(++lab >= labend) { fprintf(stderr, "Too many labels: %s\n", linebuf); exit(2); } } lpt->address = rep; rep->ad1 = p; continue; case 'a': rep->command = ACOM; if(rep->ad2) { fprintf(stderr, AD1MES, linebuf); exit(2); } if(*cp == '\\') cp++; if(*cp++ != '\n') { fprintf(stderr, CGMES, linebuf); exit(2); } rep->re1 = p; p = text(rep->re1); break; case 'c': rep->command = CCOM; if(*cp == '\\') cp++; if(*cp++ != ('\n')) { fprintf(stderr, CGMES, linebuf); exit(2); } rep->re1 = p; p = text(rep->re1); break; case 'i': rep->command = ICOM; if(rep->ad2) { fprintf(stderr, AD1MES, linebuf); exit(2); } if(*cp == '\\') cp++; if(*cp++ != ('\n')) { fprintf(stderr, CGMES, linebuf); exit(2); } rep->re1 = p; p = text(rep->re1); break; case 'g': rep->command = GCOM; break; case 'G': rep->command = CGCOM; break; case 'h': rep->command = HCOM; break; case 'H': rep->command = CHCOM; break; case 't': rep->command = TCOM; goto jtcommon; case 'b': rep->command = BCOM;jtcommon: while(*cp++ == ' '); cp--; if(*cp == '\0') { if(pt = labtab->chain) { while(pt1 = pt->lb1) pt = pt1; pt->lb1 = rep; } else labtab->chain = rep; break; } tp = lab->asc; while((*tp++ = *cp++)) if(tp >= &(lab->asc[8])) { fprintf(stderr, LTL, linebuf); exit(2); } cp--; *--tp = '\0'; if(lpt = search(lab)) { if(lpt->address) { rep->lb1 = lpt->address; } else { pt = lpt->chain; while(pt1 = pt->lb1) pt = pt1; pt->lb1 = rep; } } else { lab->chain = rep; lab->address = 0; if(++lab >= labend) { fprintf(stderr, "Too many labels: %s\n", linebuf); exit(2); } } break; case 'n': rep->command = NCOM; break; case 'N': rep->command = CNCOM; break; case 'p': rep->command = PCOM; break; case 'P': rep->command = CPCOM; break; case 'r': rep->command = RCOM; if(rep->ad2) { fprintf(stderr, AD1MES, linebuf); exit(2); } if(*cp++ != ' ') { fprintf(stderr, CGMES, linebuf); exit(2); } rep->re1 = p; p = text(rep->re1); break; case 'd': rep->command = DCOM; break; case 'D': rep->command = CDCOM; rep->lb1 = ptrspace; break; case 'q': rep->command = QCOM; if(rep->ad2) { fprintf(stderr, AD1MES, linebuf); exit(2); } break; case 'l': rep->command = LCOM; break; case 's': rep->command = SCOM; seof = *cp++; rep->re1 = p; p = compile(rep->re1); if(p == badp) { fprintf(stderr, CGMES, linebuf); exit(2); } if(p == rep->re1) { rep->re1 = op; } else { op = rep->re1; } if((rep->rhs = p) > reend) { fprintf(stderr, TMMES, linebuf); exit(2); } if((p = compsub(rep->rhs)) == badp) { fprintf(stderr, CGMES, linebuf); exit(2); } if(*cp == 'g') { cp++; rep->gfl++; } else if(gflag) rep->gfl++; if(*cp == 'p') { cp++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -