/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Editor
*/
#include <crypt.h>
#include <libgen.h>
#include <wait.h>
#include <string.h>
#include <locale.h>
#include <regexpr.h>
#include <regex.h>
#include <errno.h>
#include <paths.h>
static const char *msgtab[] =
{
"write or open on pipe failed", /* 0 */
"warning: expecting `w'", /* 1 */
"mark not lower case ascii", /* 2 */
"Cannot open input file", /* 3 */
"PWB spec problem", /* 4 */
"nothing to undo", /* 5 */
"restricted shell", /* 6 */
"cannot create output file", /* 7 */
"filesystem out of space!", /* 8 */
"cannot open file", /* 9 */
"cannot link", /* 10 */
"Range endpoint too large", /* 11 */
"unknown command", /* 12 */
"search string not found", /* 13 */
"-", /* 14 */
"line out of range", /* 15 */
"bad number", /* 16 */
"bad range", /* 17 */
"Illegal address count", /* 18 */
"incomplete global expression", /* 19 */
"illegal suffix", /* 20 */
"illegal or missing filename", /* 21 */
"no space after command", /* 22 */
"fork failed - try again", /* 23 */
"maximum of 64 characters in file names", /* 24 */
"`\\digit' out of range", /* 25 */
"interrupt", /* 26 */
"line too long", /* 27 */
"illegal character in input file", /* 28 */
"write error", /* 29 */
"out of memory for append", /* 30 */
"temp file too big", /* 31 */
"I/O error on temp file", /* 32 */
"multiple globals not allowed", /* 33 */
"global too long", /* 34 */
"no match", /* 35 */
"illegal or missing delimiter", /* 36 */
"-", /* 37 */
"replacement string too long", /* 38 */
"illegal move destination", /* 39 */
"-", /* 40 */
"no remembered search string", /* 41 */
"'\\( \\)' imbalance", /* 42 */
"Too many `\\(' s", /* 43 */
"more than 2 numbers given", /* 44 */
"'\\}' expected", /* 45 */
"first number exceeds second", /* 46 */
"incomplete substitute", /* 47 */
"newline unexpected", /* 48 */
"'[ ]' imbalance", /* 49 */
"regular expression overflow", /* 50 */
"regular expression error", /* 51 */
"command expected", /* 52 */
"a, i, or c not allowed in G", /* 53 */
"end of line expected", /* 54 */
"no remembered replacement string", /* 55 */
"no remembered command", /* 56 */
"illegal redirection", /* 57 */
"possible concurrent update", /* 58 */
"-", /* 59 */
"the x command has become X (upper case)", /* 60 */
"Warning: 'w' may destroy input file "
"(due to `illegal char' read earlier)",
/* 61 */
"Caution: 'q' may lose data in buffer;"
" 'w' may destroy input file",
/* 62 */
"Encryption of string failed", /* 63 */
"Encryption facility not available", /* 64 */
"Cannot encrypt temporary file", /* 65 */
"Enter key:", /* 66 */
"Illegal byte sequence", /* 67 */
"File does not exist", /* 68 */
"tempnam failed", /* 69 */
"Cannot open temporary file", /* 70 */
0
};
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <termio.h>
#include <ctype.h>
#include <setjmp.h>
#include <fcntl.h>
#include <wchar.h> /* I18N */
#include <wctype.h> /* I18N */
#include <widec.h> /* I18N */
/* size of substitution replacement pattern buffer */
#define READ 0
extern char *optarg; /* Value of argument */
extern int optind; /* Indicator of argument */
struct Fspec {
char Fdel;
unsigned char Flim;
char Fmov;
char Ffill;
};
static char *fsp;
static int fsprtn;
static int sig;
static int Xqt = 0;
static int lastc;
static int funlink = 0;
static char *expbuf;
struct lin {
long cur;
long sav;
};
static int globflg;
static int initflg;
static long count;
/* out. Retains last line changed as current line. */
static char *nextip;
static char *linebp;
static int ninbuf;
static int peekc;
static int io;
static void quit(int);
static int xflag;
static int xtflag;
static int kflag;
static int crflag;
/* Flag for determining if file being read is encrypted */
static int hflag;
static int permflag;
static int tpermflag;
static int col;
static char *globp;
static int tline;
static char *tfname;
extern char *locs;
static int ichanged;
static int nleft;
static int anymarks;
static long subnewa;
static int fchange;
static int nline;
static int rflg;
static int readflg;
static int eflg;
static int qflg = 0;
static int ncflg;
static int listn;
static int listf;
static int pflag;
static long savtime;
static char *val;
static char *home;
static int nodelim;
int makekey(int *);
static void gdelete(void);
static void delete(void);
static void exfile(void);
static void newline(void);
static int gettty(void);
static void commands(void);
static void undo(void);
static void save(void);
static int stdtab(char *, char *);
static int numb(void);
static void red(char *);
static void newtime(void);
static void chktime(void);
static void getime(void);
static void mkfunny(void);
static int eopen(char *, int);
static void eclose(int f);
static void globaln(int);
static char *getkey(const char *);
static void error1(int);
static int getcopy(void);
static void move(int);
static void dosub(void);
static int getsub(void);
static int compsub(void);
static void substitute(int);
static void join(void);
static void global(int);
static void init(void);
static int getfile(void);
static void putfile(void);
static void onpipe(int);
static void onhup(int);
static void onintr(int);
static void setdot(void);
static void setall(void);
static void setnoaddr(void);
static void nonzero(void);
static void setzeroasone(void);
static long putline(void);
static char *getaline(long);
static char *getblock(long, long);
static char *place(char *, char *, char *);
static void putchr(unsigned char);
static int getchr(void);
static void unixcom(void);
static void putd();
#ifndef RESEARCH
static struct statvfs U;
static int Short = 0;
#endif
#ifdef NULLS
#endif
static long ccount;
static int errcnt = 0;
static void
{
(int)error(0);
}
int
{
int c;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
while (*p1++);
if (*p1 == '/')
break;
rflg++;
if (**argv == 'r')
rflg++;
while (1) {
switch (c) {
case 's':
vflag = 0;
break;
case 'p':
shflg = 1;
break;
case 'q':
vflag = 1;
break;
case 'x':
crflag = -1;
xflag = 1;
break;
case 'C':
crflag = 1;
xflag = 1;
break;
case '?':
"Usage: ed [- | -s] [-p string] [-x] [-C] [file]\n"
" red [- | -s] [-p string] [-x] [-C] [file]\n"));
exit(2);
}
}
vflag = 0;
optind++;
continue;
}
break;
}
if (xflag) {
if (permflag)
permflag = 1;
== -1) {
xflag = 0;
kflag = 0;
}
if (kflag == 0)
crflag = 0;
}
if (argc > 0) {
if (kflag)
exit(2);
}
globp = "e";
fflg++;
} else /* editing with no file so set savtime to 0 */
savtime = 0;
eflg++;
exit(2);
}
init();
commands();
return (0);
}
static void
commands(void)
{
int c;
int fsave, m, n;
for (;;) {
nodelim = 0;
if (pflag) {
pflag = 0;
goto print;
}
addr1 = 0;
addr2 = 0;
if ((c = getchr()) == ',') {
#ifdef XPG6
/* XPG4 - it was an error if the second address was */
/* input and the first address was ommitted */
/* Parse second address */
}
#endif
c = getchr();
goto swch;
} else if (c == ';') {
#ifdef XPG6
/* XPG4 - it was an error if the second address was */
/* input and the first address was ommitted */
/* Parse second address */
}
#endif
c = getchr();
goto swch;
} else
peekc = c;
do {
c = getchr();
break;
}
if ((c = getchr()) == ';') {
c = ',';
}
} while (c == ',');
if (addr1 == 0)
swch:
switch (c) {
case 'a':
setdot();
newline();
continue;
case 'c':
#ifdef XPG6
setzeroasone();
#endif
delete();
/* XPG4 - If no new lines are inserted, then the current */
/* line becomes the line after the lines deleted. */
continue;
case 'd':
delete();
continue;
case 'E':
fchange = 0;
c = 'e';
case 'e':
fflg++;
setnoaddr();
fchange = 0;
(void) error(1);
}
filename(c);
eflg++;
init();
goto caseread;
case 'f':
setnoaddr();
filename(c);
if (!ncflg) /* there is a filename */
getime();
else
ncflg--;
continue;
case 'g':
global(1);
continue;
case 'G':
globaln(1);
continue;
case 'h':
newline();
setnoaddr();
PUTM();
continue;
case 'H':
newline();
setnoaddr();
if (!hflag) {
hflag = 1;
PUTM();
}
else
hflag = 0;
continue;
case 'i':
#ifdef XPG6
setzeroasone();
#endif
setdot();
nonzero();
newline();
dot += 1;
continue;
case 'j':
if (addr2 == 0) {
}
setdot();
newline();
nonzero();
join();
continue;
case 'k':
(void) error(2);
newline();
setdot();
nonzero();
anymarks |= 01;
continue;
case 'm':
move(0);
continue;
case '\n':
if (addr2 == 0)
goto print;
case 'n':
listn++;
newline();
goto print;
case 'l':
listf++;
case 'p':
newline();
setdot();
nonzero();
do {
if (listn) {
putd();
putchr('\t');
}
}
pflag = 0;
listn = 0;
listf = 0;
continue;
case 'Q':
fchange = 0;
case 'q':
setnoaddr();
newline();
case 'r':
filename(c);
readflg = 1;
errno = 0;
lastc = '\n';
/* if first entering editor and file does not exist */
/* set saved access time to 0 */
if (eflg) {
savtime = 0;
eflg = 0;
if (c == 'e' && vflag == 0)
qflg = 1;
}
(void) error(68);
} else {
(void) error(3);
}
}
/* get last mod time of file */
/* eflg - entered editor with ed or e */
if (eflg) {
eflg = 0;
getime();
}
setall();
ninbuf = 0;
#ifdef NULLS
nulls = 0;
#endif
exfile();
readflg = 0;
fchange = n;
continue;
case 's':
setdot();
nonzero();
substitute(globp != 0);
continue;
case 't':
move(1);
continue;
case 'u':
setdot();
newline();
if (!initflg)
undo();
else
(void) error(5);
fchange = 1;
continue;
case 'v':
global(0);
continue;
case 'V':
globaln(0);
continue;
case 'W':
case 'w':
if (flag28) {
flag28 = 0;
fchange = 0;
(void) error(61);
}
setall();
/* on NULL-RE condition do not generate error */
(void) error(15);
filename(c);
if (Xqt) {
n = 1; /* set n so newtime will not execute */
} else {
(void) error(7);
} else {
#ifndef RESEARCH
/*
* Must determine if file is
* a symbolic link
*/
#endif
}
#ifndef RESEARCH
/*
* Determine if there are enough free blocks on system
*/
Short = 1;
(void) error(8);
}
Short = 0;
#endif
(void) error(9);
if (!(n = m))
chktime();
mkfunny();
/*
* If funlink equals one it means that
* funny points to a valid file which must
* be unlinked when interrupted.
*/
funlink = 1;
putfile();
exfile();
(void) error(10);
funlink = 0;
/* if filenames are the same */
if (!n)
newtime();
/* check if entire buffer was written */
continue;
}
} else
n = 1; /* set n so newtime will not execute */
(void) error(7);
}
putfile();
exfile();
if (!n) newtime();
/* Leave fchange alone if partial write was to another file */
continue;
case 'C':
crflag = 1;
/*
* C is same as X, but always assume input files are
* ciphertext
*/
goto encrypt;
case 'X':
crflag = -1;
setnoaddr();
newline();
xflag = 1;
if (permflag)
(void) crypt_close(perm);
permflag = 1;
== -1) {
xflag = 0;
kflag = 0;
crflag = 0;
(void) error(64);
}
if (kflag == 0)
crflag = 0;
continue;
case '=':
setall();
newline();
putd();
putchr('\n');
continue;
case '!':
unixcom();
continue;
case EOF:
return;
case 'P':
setnoaddr();
newline();
if (shflg)
shflg = 0;
else
shflg++;
continue;
}
if (c == 'x')
(void) error(60);
else
(void) error(12);
}
}
address(void)
{
int minus, c;
minus = 0;
a1 = 0;
for (;;) {
c = getchr();
if ('0' <= c && c <= '9') {
n = 0;
do {
n *= 10;
n += c - '0';
peekc = c;
if (a1 == 0)
if (minus < 0)
n = -n;
a1 += n;
minus = 0;
continue;
}
relerr = 0;
relerr++;
switch (c) {
case ' ':
case '\t':
continue;
case '+':
minus++;
if (a1 == 0)
continue;
case '-':
case '^':
minus--;
if (a1 == 0)
continue;
case '?':
case '/':
comple(c);
for (;;) {
if (c == '/') {
a1++;
} else {
a1--;
}
break;
(void) error(13);
}
break;
case '$':
break;
case '.':
break;
case '\'':
(void) error(2);
break;
break;
default:
peekc = c;
if (a1 == 0)
return (0);
/* on NULL-RE condition do not generate error */
(void) error(15);
return (a1);
}
if (relerr)
(void) error(16);
}
}
static void
setdot(void)
{
if (addr2 == 0)
(void) error(17);
}
static void
setall(void)
{
if (addr2 == 0) {
}
setdot();
}
static void
setnoaddr(void)
{
if (addr2)
(void) error(18);
}
static void
nonzero(void)
{
/* on NULL-RE condition do not generate error */
(void) error(15);
}
static void
setzeroasone(void)
{
/* for the c and i commands 0 equal to 1 address */
}
}
}
static void
newline(void)
{
int c;
if ((c = getchr()) == '\n')
return;
if (c == 'p' || c == 'l' || c == 'n') {
pflag++;
if (c == 'l') listf++;
if (c == 'n') listn++;
if ((c = getchr()) == '\n')
return;
}
(void) error(20);
}
static void
{
int c;
int i = 0;
count = 0;
c = getchr();
if (c == '\n' || c == EOF) {
(void) error(21);
/* ncflg set means do not get mod time of file */
/* since no filename followed f */
if (comm == 'f')
ncflg++;
return;
}
if (c != ' ')
(void) error(22);
while ((c = getchr()) == ' ');
if (c == '!')
if (c == '\n')
(void) error(21);
do {
if (++i >= FNSIZE)
(void) error(24);
*p1++ = c;
(void) error(21);
} while ((c = getchr()) != '\n');
*p1++ = 0;
if (Xqt)
if (comm == 'f') {
--Xqt;
(void) error(57);
}
else
return;
}
}
static void
exfile(void)
{
#ifdef NULLS
int c;
#endif
#ifndef RESEARCH
if (oldmask) {
oldmask = 0;
}
#endif
io = -1;
if (vflag) {
putd();
putchr('\n');
#ifdef NULLS
if (nulls) {
c = count;
nulls = 0;
putd();
count = c;
}
#endif
}
}
static void
{
putchr('\n');
lastc = '\n';
globflg = 0;
/* if interrupted a read, only part of file may be in buffer */
if (readflg) {
fchange = 0;
}
(void) error(26);
}
static void
{
/*
* if there are lines in file and file was not written
*/
char *fn;
if (fn) {
}
}
if (io > 0)
putfile();
}
fchange = 0;
++errcnt;
}
static int
{
int c;
fchange = 0;
flag28++;
}
readflg = 0;
++errcnt;
pflag = 0;
#ifndef RESEARCH
if (oldmask) {
oldmask = 0;
}
#endif
#ifdef NULLS /* Not really nulls, but close enough */
/* This is a bug because of buffering */
putd();
#endif
/* Cant open file or file does not exist */
if (qflg == 0) {
putchr('?');
}
else
qflg = 0;
}
else
{
putchr('?');
putchr('\n');
}
count = 0;
lseek(0, (long)0, 2);
if (globp)
lastc = '\n';
globp = 0;
if (lastc)
if (io) {
io = -1;
}
if (hflag)
PUTM();
if (code == 4)
return (0); /* Non-fatal error. */
/* NOTREACHED */
}
static int
getchr(void)
{
char c;
peekc = 0;
return (lastc);
}
if (globp) {
return (lastc);
globp = 0;
return (EOF);
}
if (read(0, &c, 1) <= 0)
lastc = (unsigned char)c;
return (lastc);
}
static int
gettty(void)
{
int c;
char *gf;
char *p;
p = linebuf;
while ((c = getchr()) != '\n') {
if (c == EOF) {
if (gf)
peekc = c;
return (c);
}
if (c == 0)
continue;
*p++ = c;
(void) error(27);
}
*p++ = 0;
return (EOF);
/*
*
* in Solaris backslash followed by special character "." is
* special character "." itself; (so terminating input mode can be
* "\.\n").
*
* entering line consisting of only 2 characters: ".\n"
*
* if (linebuf[0]=='\\' && linebuf[1]=='.' && linebuf[2]==0) {
* linebuf[0] = '.';
* linebuf[1] = 0;
* }
*/
return (0);
}
static int
getfile(void)
{
char c;
do {
if (--ninbuf < 0) {
*genbuf = '\n';
}
else
return (EOF);
if (crflag == -1) {
crflag = 2;
else
crflag = -2;
}
if (crflag > 0)
(void) error(63);
}
lastc = '\n';
(void) error(27);
}
#ifdef NULLS
*lp++ = '0';
nulls++;
#else
lp--;
continue;
#endif
}
count++;
} while (c != '\n');
*--lp = 0;
dot--;
putd();
putchr('\n');
}
return (0);
}
static void
putfile(void)
{
int n;
int nib;
do {
putd();
putchr('\n');
}
for (;;) {
if (--nib < 0) {
if (kflag)
(void) error(63);
(void) error(29);
}
count++;
break;
}
}
if (kflag)
(void) error(63);
(void) error(29);
}
static void
{
long tl;
nline = 0;
dot = a;
while ((*f)() == 0) {
lastc = '\n';
(void) error(30);
}
endcore += 512;
}
nline++;
}
}
static void
unixcom(void)
{
void (*savint)();
int retcode;
wchar_t c;
int len;
setnoaddr();
if (rflg)
(void) error(6);
/* read command til end */
/*
* a '!' found in beginning of command is replaced with the saved
* command. a '%' found in command is replaced with the current
* filename
*/
c = getchr();
if (c == '!') {
if (savcmd[0] == 0)
(void) error(56);
else {
--pcurcmd;
shflg = 1;
}
} else
UNGETC(c); /* put c back */
while (endflg == 1) {
*pcurcmd = (unsigned char)c;
len = 1;
}
}
if (c == '%') {
if (savedfile[0] == 0)
(void) error(21);
else {
(*pcurcmd++ = *psavedfile++));
--pcurcmd;
shflg = 1;
}
} else if (c == '\\') {
c = get_wchr();
if (c != '%')
*pcurcmd++ = '\\';
*pcurcmd = (unsigned char)c;
len = 1;
}
}
else
/* end of command hit */
endflg = 0;
}
*pcurcmd++ = 0;
if (shflg == 1)
/* save command */
exit(0100);
}
}
static void
{
fchange = 0;
if (flag28) {
flag28 = 0;
(void) error(62);
}
/*
* For case where user reads in BOTH a good
* file & a bad file
*/
(void) error(1);
}
if (kflag)
if (xtflag)
}
static void
delete(void)
{
setdot();
newline();
nonzero();
}
static void
{
do
fchange = 1;
}
static void
gdelete(void)
{
return;
a2++;
} else
}
fchange = 1;
}
static char *
{
int nl;
tl &= ~0377;
if (--nl == 0) {
}
return (linebuf);
}
static long
putline(void)
{
int nl;
long tl;
fchange = 1;
tl &= ~0377;
if (*bp++ == '\n') {
*--bp = 0;
break;
}
if (--nl == 0) {
}
}
return (nl);
}
static char *
{
int n;
/* bno is limited to 16 bits */
if (bno >= 65535) {
lastc = '\n';
(void) error(31);
}
}
if (ichanged) {
if (xtflag)
(void) error(63);
}
ichanged = 0;
if (xtflag)
(void) error(63);
}
if (oblock >= 0) {
if (xtflag) {
n = 512;
while (n--)
(void) error(63);
} else
}
}
static void
{
}
}
static void
init(void)
{
long *markp;
if (tfile != -1) {
}
tline = 2;
*markp++ = 0L;
subnewa = 0L;
anymarks = 0;
iblock = -1;
oblock = -1;
ichanged = 0;
initflg = 1;
exit(2);
}
if (xflag) {
xtflag = 1;
if (tpermflag)
(void) crypt_close(tperm);
tpermflag = 1;
xtflag = 0;
}
}
}
static void
global(int k)
{
char *gp;
wchar_t l;
wchar_t c;
int n;
int len;
if (globp)
(void) error(33);
setall();
nonzero();
(void) error(67);
if (l == '\n')
(void) error(19);
save();
comple(l);
while ((c = get_wchr()) != '\n') {
if (c == EOF)
(void) error(19);
/* '\\' has special meaning only if preceding a '\n' */
if (c == '\\') {
c = get_wchr();
if (c != '\n')
*gp++ = '\\';
}
(void) error(34);
*gp = (unsigned char)c;
len = 1;
}
}
*gp++ = 'p';
*gp++ = '\n';
*gp++ = 0;
}
/*
* Special case: g/.../d (avoid n^2 algorithm)
*/
gdelete();
return;
}
globflg = 1;
commands();
globflg = 0;
}
}
}
static void
join(void)
{
return;
(void) error(27);
}
}
static void
{
int nl;
long *markp;
ocerr2 = 0;
continue;
numpass = 0;
ocerr1 = 0;
inglob |= 01;
dosub();
if (gsubf) {
while (*loc2) {
break;
dosub();
}
}
if (ocerr1 == 0)continue; /* Don't put out-not changed. */
if (anymarks) {
}
}
if (ingsav)
return; /* Was in global-no error msg allowed. */
if (inglob == 0)
if (ocerr2 == 0)
}
static int
compsub(void)
{
int c;
char *p;
int n;
int i;
(void) error(67);
(void) error(36);
p = rhsbuf;
for (;;) {
(void) error(67);
if (cl == '\\') {
*p++ = '\\';
(void) error(38);
(void) error(67);
} else if (cl == '\n') {
if (nodelim == 1) {
nodelim = 0;
(void) error(36);
}
UNGETC('\n');
pflag++;
break;
}
break;
(void) error(38);
p += n;
}
*p++ = 0;
/*
* If there isn't a remembered string, it is an error;
* otherwise the right hand side is the previous right
* hand side.
*/
if (remflg == -1)
(void) error(55);
else
else {
remflg = 0;
}
c = 0;
if (peekc == 'g') {
}
c = peekc-'0';
peekc = 0; /* Allows getchr() to get next char. */
while (1) {
i = getchr();
if (i < '0' || i > '9')
break;
c = c*10 + i-'0';
if (c > LBSIZE-1)
}
peekc = i; /* Effectively an unget. */
}
newline();
return (c);
/*
* Returns occurrence value. 0 & 1 both do first occurrence
* only: c = 0 if ordinary substitute; c = 1
* if use 1 in global sub(s/a/b/1). 0 in global form is illegal.
*/
}
static int
getsub(void)
{
return (EOF);
linebp = 0;
return (0);
}
static void
dosub(void)
{
int c;
numpass++;
return;
}
ocerr1++;
ocerr2++;
while (c = *rp++) {
if (c == '&') {
continue;
} else if (c == '\\') {
c = *rp++;
continue;
}
}
*sp++ = c;
(void) error(27);
}
(void) error(27);
}
static char *
{
(void) error(27);
}
return (sp);
}
static void
{
int cclass = 0;
wchar_t c;
int n;
while (1) {
error1(67);
if (n == 0 || c == '\n') {
if (cclass)
error1(49);
else
break;
}
break;
if (cclass && c == ']') {
cclass = 0;
error1(50);
*cp++ = ']';
continue;
}
if (c == '[' && !cclass) {
cclass = 1;
error1(50);
*cp++ = '[';
error1(67);
if (n == 0 || c == '\n')
error1(49);
}
if (c == '\\' && !cclass) {
error1(50);
*cp++ = '\\';
error1(67);
if (n == 0 || c == '\n')
error1(36);
}
error1(50);
cp += n;
}
*cp = '\0';
if (n != 0 && c == '\n')
UNGETC('\n');
if (n == 0 || c == '\n')
nodelim = 1;
/*
* NULL RE: do not compile a null regular expression; but process
* input with last regular expression encountered
*/
if (genbuf[0] != '\0') {
if (expbuf)
}
if (regerrno)
}
static void
{
setdot();
nonzero();
(void) error(39);
newline();
if (cflag) {
} else {
}
ad2++;
return;
} else
(void) error(39);
fchange = 1;
}
static void
{
long t;
for (;;) {
return;
}
}
static int
getcopy(void)
{
return (EOF);
return (0);
}
/*
* Handles error code returned from comple() routine: regular expression
* compile and match routines
*/
static void
{
nbra = 0;
}
static int
{
char *p1;
int c;
for (c = 0; c < nbra; c++) {
braslist[c] = 0;
braelist[c] = 0;
}
if (gf)
else {
return (0);
locs = 0;
}
}
static void
putd()
{
int r;
r = (int)(count%10);
count /= 10;
if (count)
putd();
putchr(r + '0');
}
int
{
int n;
wchar_t c;
int sz, i;
/* deliberate attempt to remove constness of sp because */
/* it needs to be expanded */
putchr('\n');
}
else
putchr('\n');
if (i == -2)
return (0);
}
col = 0;
while (*sp) {
if (listf) {
if (n < 1)
(void) error(28);
else if (n == 1)
else {
sp += n;
putwchr(c);
}
} else {
}
}
#ifndef XPG6
if (listf)
#else
if (listf) {
/* xpg6 - ensure that the end of line $ is not preceeded with a "\" */
/* by doing a putchr() with listf=0, thereby avoiding the $ case */
/* statement in putchr() */
listf = 0;
listf++;
}
#endif
putchr('\n');
return (1);
}
static void
{
char *lp;
wchar_t c;
short len;
c = ac;
if (listf) {
if (!iswprint(c)) {
p = &buf[0];
*p = (unsigned char)c;
len = 1;
};
while (len--) {
col = 0;
*lp++ = '\\';
*lp++ = '\n';
}
*(unsigned char *)p++);
col += 4;
lp += 4;
}
} else {
len = 0;
col = 0;
*lp++ = '\\';
*lp++ = '\n';
}
*lp = (unsigned char)c;
len = 1;
}
}
} else {
*lp = (unsigned char)c;
len = 1;
}
}
return;
}
}
static void
putchr(unsigned char c)
{
char *lp;
int len;
if (listf && c != '\n') {
switch (c) {
case '\\' :
*lp++ = '\\';
*lp++ = '\\';
col += 2;
break;
case '\007' :
*lp++ = '\\';
*lp++ = 'a';
col += 2;
break;
case '\b' :
*lp++ = '\\';
*lp++ = 'b';
col += 2;
break;
case '\f' :
*lp++ = '\\';
*lp++ = 'f';
col += 2;
break;
case '\r' :
*lp++ = '\\';
*lp++ = 'r';
col += 2;
break;
case '\t' :
*lp++ = '\\';
*lp++ = 't';
col += 2;
break;
case '\v' :
*lp++ = '\\';
*lp++ = 'v';
col += 2;
break;
#ifdef XPG6
/* if $ characters are within the line preceed with \ */
case '$' :
*lp++ = '\\';
*lp++ = '$';
col += 2;
break;
#endif
default:
if (isprint(c)) {
*lp++ = c;
col += 1;
} else {
col += 4;
lp += 4;
}
break;
}
/*
* long lines are folded w/ pt of folding indicated by writing
*/
col = 0;
*lp++ = '\\';
*lp++ = '\n';
}
} else
*lp++ = c;
return;
}
}
static char *
{
struct termio b;
int save;
void (*sig)();
char *p;
int c;
p = key;
*p++ = c;
}
*p = 0;
return (key);
}
static void
globaln(int k)
{
char *gp;
int c;
int n;
int nfirst;
int len;
int pflag_save = 0;
int listf_save = 0;
int listn_save = 0;
if (globp)
(void) error(33);
setall();
nonzero();
(void) error(67);
if (cl == '\n')
(void) error(19);
save();
}
nfirst = 0;
newline();
/*
* preserve the p, l, and n suffix commands of the G and V
* commands during the interactive section and restore
* on completion of the G and V command.
*/
pflag_save = pflag;
listf_save = listf;
listn_save = listn;
pflag = 0;
listf = 0;
listn = 0;
(void) error(52);
if (c == 'a' || c == 'i' || c == 'c')
(void) error(53);
if (c == '\n') {
continue;
}
if (c != '&') {
*gp = (unsigned char)c;
len = 1;
}
while ((c = get_wchr()) != '\n') {
/* '\\' has special meaning only if preceding a '\n' */
if (c == '\\') {
c = get_wchr();
if (c != '\n')
*gp++ = '\\';
}
if ((gp + (unsigned int)MB_CUR_MAX) >=
(void) error(34);
*gp = (unsigned char)c;
len = 1;
}
}
*gp++ = '\n';
*gp++ = 0;
nfirst = 1;
} else if ((c = get_wchr()) != '\n')
(void) error(54);
if (nfirst) {
globflg = 1;
commands();
globflg = 0;
} else
(void) error(56);
globp = 0;
}
}
pflag = pflag_save;
listf = listf_save;
listn = listn_save;
}
static int
{
pid_t i;
int io;
if (rflg) { /* restricted shell */
if (Xqt) {
Xqt = 0;
(void) error(6);
}
}
if (!Xqt) {
if (fflg) {
if (crflag == -1) {
crflag = 2;
else
crflag = -2;
}
if (crflag > 0)
(void) error(63);
fflg = 0;
(void) error(4);
}
}
}
fflg = 0;
return (io);
}
if ((i = fork()) == 0) {
exit(1);
}
if (i == (pid_t)-1)
goto xerr;
}
static void
eclose(int f)
{
close(f);
if (Xqt)
}
static void
mkfunny(void)
{
p = file;
/*
* Go to end of file name
*/
while (*p)
p++;
while (*--p == '/') /* delete trailing slashes */
*p = '\0';
/*
* go back to beginning of file
*/
p = file;
/*
* Copy file name to funny setting p2 at
* basename of file.
*/
while (*p1++ = *p)
/*
* Set p1 to point to basename of tfname.
*/
p1++;
/*
* Copy tfname to file.
*/
}
static void
{
savtime = 0;
else
}
static void
{
(void) error(58);
}
}
static void
{
}
static void
/* and delete trailing '/' */
{
char *p;
p = op;
while (*p)
if (*p++ == '/'&& rflg) {
*op = 0;
(void) error(6);
}
/* delete trailing '/' */
while (p > op) {
if (*--p == '/')
*p = '\0';
else break;
}
}
/*
* Searches thru beginning of file looking for a string of the form
* <: values... :>
*
* where "values" are
*
* \b ignored
* s<num> sets the Flim to <num>
* t??? sets tab stop stuff
* d ignored
* m<num> ignored
* e ignored
*/
static int
{
int havespec, n;
int len;
len = 1;
switch (*fsp) {
case '<': if (havespec)
return (-1);
havespec = 1;
clear(f);
f->Ffill = 1;
fsp++;
continue;
}
case ' ': continue;
f->Flim = n;
continue;
continue;
case 'd': continue;
continue;
case 'e': continue;
case ':': if (!havespec) continue;
return (fsprtn);
default: if (!havespec) continue;
return (-1);
}
}
return (1);
}
static int
numb(void)
{
int n;
n = 0;
fsp--;
return (n);
}
static void
{
if (*++fsp == '-') {
else tstd(f);
return;
}
tlist(f);
return;
}
fsprtn = -1;
fsp--;
}
static void
{
int l, i;
l = 1;
for (i = 0; i < 20; i++)
f->Ftabs[i] = l += n;
f->Ftabs[i] = 0;
}
static void
{
}
static void
{
int n, last, i;
fsp--;
last = i = 0;
do {
fsprtn = -1;
return;
}
} while (*++fsp == ',');
f->Ftabs[i] = 0;
fsp--;
}
static int
{
char *l, *t;
int b;
l = line - 1;
b = 1;
t = f->Ftabs;
fsprtn = 0;
while (*++l && *l != '\n' && b < 511) {
if (*l == '\t') {
while (*t && b >= *t) t++;
if (*t == 0) fsprtn = -2;
} else buf[b++ - 1] = *l;
}
buf[b] = '\0';
*sz = b;
if (*l != '\0' && *l != '\n') {
return (-1);
}
buf[b-1] = *l;
return (-1);
return (fsprtn);
}
static void
{
f->Flim = 0;
}
static int
{
char *l, *t;
int b;
l = line - 1;
b = 1;
t = f->Ftabs;
while (*++l && *l != '\n' && b < 511) {
if (*l == '\t') {
while (*t && b >= *t) t++;
while (++b < *t);
} else b++;
}
return (-1);
return (0);
}
/*
* stdtabs: standard tabs table
* format: option code letter(s), null, tabs, null
*/
static char stdtabs[] = {
'a', 0, 1, 10, 16, 36, 72, 0, /* IBM 370 Assembler */
'a', '2', 0, 1, 10, 16, 40, 72, 0, /* IBM Assembler alternative */
'c', 0, 1, 8, 12, 16, 20, 55, 0, /* COBOL, normal */
'c', '2', 0, 1, 6, 10, 14, 49, 0, /* COBOL, crunched */
'c', '3', 0, 1, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50,
54, 58, 62, 67, 0,
'f', 0, 1, 7, 11, 15, 19, 23, 0, /* FORTRAN */
'p', 0, 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 0,
/* PL/I */
's', 0, 1, 10, 55, 0, /* SNOBOL */
'u', 0, 1, 12, 20, 44, 0, /* UNIVAC ASM */
0 };
/*
* stdtab: return tab list for any "canned" tab option.
* entry: option points to null-terminated option string
* tabvect points to vector to be filled in
* exit: return (0) if legal, tabvect filled, ending with zero
* return (-1) if unknown option
*/
static int
{
char *scan;
tabvect[0] = 0;
while (*scan) {
break;
} else
while (*scan++); /* skip over tab specs */
}
return (tabvect[0] ? 0 : -1);
}
/*
* strequal: checks strings for equality
* entry: scan1 points to scan pointer, str points to string
* exit: return (1) if equal, return (0) if not
* *scan1 is advanced to next nonzero byte after null
*/
static int
{
char c, *scan;
if (c == 0 && *str == 0)
return (1);
if (c)
while (*scan++);
return (0);
}
/* strcopy: copy source to destination */
static void
{
}
/* This is called before a buffer modifying command so that the */
/* current array of line ptrs is saved in sav and dot and dol are saved */
static void
save(void) {
LINE i;
int j;
for (j = 0; j <= 25; j++)
initflg = 0;
}
/* The undo command calls this to restore the previous ptr array sav */
/* and swap with cur - dot and dol are swapped also. This allows user to */
/* undo an undo */
static void
undo(void) {
int j;
long tmp;
/* swap arrays using the greater of dol or savdol as upper limit */
}
/*
* If the current text lines are swapped with the
* text lines in the save buffer, then swap the current
* marks with those in the save area.
*/
for (j = 0; j <= 25; j++) {
}
}
static wchar_t
get_wchr()
{
return (wc);
}