1993-04-08 05:07:16 +04:00
|
|
|
/* ed.c: This file contains the main control and user-interface routines
|
|
|
|
for the ed line editor. */
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley
|
|
|
|
* by Andrew Moore, Talke Studio.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
1993-04-23 06:08:48 +04:00
|
|
|
/*-
|
|
|
|
* Kernighan/Plauger, "Software Tools in Pascal," (c) 1981 by
|
|
|
|
* Addison-Wesley Publishing Company, Inc. Reprinted with permission of
|
|
|
|
* the publisher.
|
|
|
|
*/
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
char copyright1[] =
|
|
|
|
"@(#) Copyright (c) 1993 The Regents of the University of California.\n\
|
|
|
|
All rights reserved.\n";
|
1993-04-23 06:08:48 +04:00
|
|
|
char copyright2[] =
|
|
|
|
"@(#) Kernighan/Plauger, Software Tools in Pascal, (c) 1981 by\n\
|
|
|
|
Addison-Wesley Publishing Company, Inc. Reprinted with permission of\n\
|
|
|
|
the publisher.\n";
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)ed.c 5.5 (Berkeley) 3/28/93";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
1993-04-23 06:08:48 +04:00
|
|
|
* CREDITS
|
|
|
|
* The buf.c algorithm is attributed to Rodney Ruddock of
|
|
|
|
* the University of Guelph, Guelph, Ontario.
|
1993-04-14 16:22:14 +04:00
|
|
|
*
|
1993-04-23 06:08:48 +04:00
|
|
|
* The cbc.c encryption code is adapted from
|
|
|
|
* the bdes program by Matt Bishop of Dartmouth College,
|
|
|
|
* Hanover, NH.
|
1993-04-08 05:07:16 +04:00
|
|
|
*
|
1993-05-08 14:49:52 +04:00
|
|
|
* Addison-Wesley Publishing Company generously granted
|
1993-04-23 06:08:48 +04:00
|
|
|
* permission to distribute this program over Internet.
|
1993-04-08 05:07:16 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <pwd.h>
|
1993-04-10 15:46:38 +04:00
|
|
|
#include <sys/ioctl.h>
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
#include "ed.h"
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef _POSIX_SOURCE
|
1993-04-28 07:37:34 +04:00
|
|
|
sigjmp_buf env;
|
|
|
|
#else
|
|
|
|
jmp_buf env;
|
|
|
|
#endif
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
/* static buffers */
|
|
|
|
char *shcmd; /* shell command buffer */
|
|
|
|
int shcmdsz; /* shell command buffer size */
|
|
|
|
int shcmdi; /* shell command buffer index */
|
|
|
|
char *cvbuf; /* global command buffer */
|
|
|
|
int cvbufsz; /* global command buffer size */
|
|
|
|
char *lhbuf; /* lhs buffer */
|
|
|
|
int lhbufsz; /* lhs buffer size */
|
|
|
|
char *rhbuf; /* rhs buffer */
|
|
|
|
int rhbufsz; /* rhs buffer size */
|
|
|
|
int rhbufi; /* rhs buffer index */
|
|
|
|
char *rbuf; /* regsub buffer */
|
|
|
|
int rbufsz; /* regsub buffer size */
|
|
|
|
char *sbuf; /* file i/o buffer */
|
|
|
|
int sbufsz; /* file i/o buffer size */
|
|
|
|
char *ibuf; /* ed command-line buffer */
|
|
|
|
int ibufsz; /* ed command-line buffer size */
|
|
|
|
char *ibufp; /* pointer to ed command-line buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
/* global flags */
|
1993-05-08 14:49:52 +04:00
|
|
|
int isbinary; /* if set, buffer contains ASCII NULs */
|
1993-04-23 06:08:48 +04:00
|
|
|
int modified; /* if set, buffer modified since last write */
|
1993-04-08 05:07:16 +04:00
|
|
|
int garrulous = 0; /* if set, print all error messages */
|
1993-04-23 06:08:48 +04:00
|
|
|
int scripted = 0; /* if set, suppress diagnostics */
|
|
|
|
int des = 0; /* if set, use crypt(3) for i/o */
|
1993-04-08 05:07:16 +04:00
|
|
|
int mutex = 0; /* if set, signals set "sigflags" */
|
|
|
|
int sigflags = 0; /* if set, signals received while mutex set */
|
1993-05-08 14:49:52 +04:00
|
|
|
int sigactive = 0; /* if set, signal handlers are enabled */
|
|
|
|
|
|
|
|
char dfn[MAXFNAME + 1] = ""; /* default filename */
|
|
|
|
long curln; /* current address */
|
|
|
|
long lastln; /* last address */
|
|
|
|
int lineno; /* script line number */
|
|
|
|
char *prompt; /* command-line prompt */
|
|
|
|
char *dps = "*"; /* default command-line prompt */
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
char *usage = "usage: %s [-] [-sx] [-p string] [name]\n";
|
1993-04-23 06:08:48 +04:00
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
extern char errmsg[];
|
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
|
|
|
|
|
|
|
/* ed: line editor */
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
int c, n;
|
|
|
|
long status = 0;
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "p:sx")) != EOF)
|
|
|
|
switch(c) {
|
|
|
|
case 'p': /* set prompt */
|
|
|
|
prompt = optarg;
|
|
|
|
break;
|
|
|
|
case 's': /* run script */
|
1993-04-23 06:08:48 +04:00
|
|
|
scripted = 1;
|
1993-05-08 14:49:52 +04:00
|
|
|
break;
|
1993-04-08 05:07:16 +04:00
|
|
|
case 'x': /* use crypt */
|
|
|
|
#ifdef DES
|
|
|
|
des = getkey();
|
|
|
|
#else
|
|
|
|
fprintf(stderr, "crypt unavailable\n?\n");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1993-04-23 06:08:48 +04:00
|
|
|
fprintf(stderr, usage, argv[0]);
|
1993-04-08 05:07:16 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
if (argc && **argv == '-') {
|
1993-04-23 06:08:48 +04:00
|
|
|
scripted = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
/* assert: reliable signals! */
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef SIGWINCH
|
|
|
|
dowinch(SIGWINCH);
|
|
|
|
if (isatty(0)) signal(SIGWINCH, dowinch);
|
|
|
|
#endif
|
1993-04-23 06:08:48 +04:00
|
|
|
signal(SIGHUP, onhup);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
1993-05-08 14:49:52 +04:00
|
|
|
signal(SIGINT, onintr);
|
|
|
|
#ifdef _POSIX_SOURCE
|
|
|
|
if (status = sigsetjmp(env, 1))
|
1993-04-28 07:37:34 +04:00
|
|
|
#else
|
1993-05-08 14:49:52 +04:00
|
|
|
if (status = setjmp(env))
|
1993-04-28 07:37:34 +04:00
|
|
|
#endif
|
1993-05-08 14:49:52 +04:00
|
|
|
{
|
1993-04-23 06:08:48 +04:00
|
|
|
fputs("\n?\n", stderr);
|
|
|
|
sprintf(errmsg, "interrupt");
|
1993-05-08 14:49:52 +04:00
|
|
|
} else {
|
|
|
|
init_buf();
|
|
|
|
sigactive = 1; /* enable signal handlers */
|
|
|
|
if (argc) {
|
|
|
|
if (**argv != '!')
|
|
|
|
strcpy(dfn, *argv);
|
|
|
|
if (doread(0, *argv) < 0 && !isatty(0))
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
for (;;) {
|
1993-04-23 06:08:48 +04:00
|
|
|
if (status < 0 && garrulous)
|
|
|
|
fprintf(stderr, "%s\n", errmsg);
|
|
|
|
if (prompt) {
|
|
|
|
printf("%s", prompt);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((n = getline()) < 0) {
|
|
|
|
status = ERR;
|
|
|
|
continue;
|
|
|
|
} else if (n == 0) {
|
1993-04-23 06:08:48 +04:00
|
|
|
if (modified && !scripted) {
|
1993-05-08 14:49:52 +04:00
|
|
|
fputs("?\n", stderr);
|
1993-04-23 06:08:48 +04:00
|
|
|
sprintf(errmsg, "warning: file modified");
|
|
|
|
if (!isatty(0)) {
|
1993-05-08 14:49:52 +04:00
|
|
|
fprintf(stderr, garrulous ? "script, line %d: %s\n"
|
|
|
|
: "", lineno, errmsg);
|
1993-04-23 06:08:48 +04:00
|
|
|
quit(2);
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
clearerr(stdin);
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
status = EMOD;
|
|
|
|
continue;
|
1993-05-08 14:49:52 +04:00
|
|
|
} else
|
|
|
|
quit(0);
|
|
|
|
} else if (ibuf[n - 1] != '\n') {
|
|
|
|
/* discard line */
|
|
|
|
sprintf(errmsg, "unexpected end-of-file");
|
1993-04-08 05:07:16 +04:00
|
|
|
clearerr(stdin);
|
|
|
|
status = ERR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((n = getlist()) >= 0 && (status = ckglob()) != 0) {
|
1993-05-08 14:49:52 +04:00
|
|
|
if (status > 0 && (status = doglob(status)) >= 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
curln = status;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if ((status = n) >= 0 && (status = docmd(0)) >= 0) {
|
1993-05-08 14:49:52 +04:00
|
|
|
if (!status || status
|
|
|
|
&& (status = doprint(curln, curln, status)) >= 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (status) {
|
|
|
|
case EOF:
|
|
|
|
quit(0);
|
|
|
|
case EMOD:
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
fputs("?\n", stderr); /* give warning */
|
1993-04-23 06:08:48 +04:00
|
|
|
sprintf(errmsg, "warning: file modified");
|
|
|
|
if (!isatty(0)) {
|
|
|
|
fprintf(stderr, garrulous ? "script, line %d: %s\n" : "", lineno, errmsg);
|
|
|
|
quit(2);
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
1993-05-08 14:49:52 +04:00
|
|
|
case FATAL:
|
|
|
|
if (!isatty(0))
|
|
|
|
fprintf(stderr, garrulous ? "script, line %d: %s\n" : "", lineno, errmsg);
|
|
|
|
else
|
|
|
|
fprintf(stderr, garrulous ? "%s\n" : "", errmsg);
|
|
|
|
quit(3);
|
1993-04-08 05:07:16 +04:00
|
|
|
default:
|
1993-04-10 15:46:38 +04:00
|
|
|
fputs("?\n", stderr);
|
|
|
|
if (!isatty(0)) {
|
|
|
|
fprintf(stderr, garrulous ? "script, line %d: %s\n" : "", lineno, errmsg);
|
|
|
|
quit(2);
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
long line1, line2, nlines;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
/* getlist: get line numbers from the command buffer until an illegal
|
|
|
|
address is seen. return range status */
|
|
|
|
getlist()
|
|
|
|
{
|
|
|
|
long num;
|
|
|
|
|
|
|
|
nlines = line2 = 0;
|
|
|
|
while ((num = getone()) >= 0) {
|
|
|
|
line1 = line2;
|
|
|
|
line2 = num;
|
|
|
|
nlines++;
|
|
|
|
if (*ibufp != ',' && *ibufp != ';')
|
|
|
|
break;
|
1993-04-10 15:46:38 +04:00
|
|
|
else if (*ibufp++ == ';')
|
1993-04-08 05:07:16 +04:00
|
|
|
curln = num;
|
|
|
|
}
|
|
|
|
nlines = min(nlines, 2);
|
|
|
|
if (nlines == 0)
|
|
|
|
line2 = curln;
|
|
|
|
if (nlines <= 1)
|
|
|
|
line1 = line2;
|
|
|
|
return (num == ERR) ? ERR : nlines;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* getone: return the next line number in the command buffer */
|
|
|
|
long
|
|
|
|
getone()
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
long i, num;
|
|
|
|
|
|
|
|
if ((num = getnum(1)) < 0)
|
|
|
|
return num;
|
|
|
|
for (;;) {
|
|
|
|
c = isspace(*ibufp);
|
|
|
|
skipblanks();
|
|
|
|
c = c && isdigit(*ibufp);
|
|
|
|
if (!c && *ibufp != '+' && *ibufp != '-' && *ibufp != '^')
|
|
|
|
break;
|
|
|
|
c = c ? '+' : *ibufp++;
|
|
|
|
if ((i = getnum(0)) < 0) {
|
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (c == '+')
|
|
|
|
num += i;
|
|
|
|
else num -= i;
|
|
|
|
}
|
|
|
|
if (num > lastln || num < 0) {
|
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
1993-04-14 16:22:14 +04:00
|
|
|
|
|
|
|
#define MAXMARK 26 /* max number of marks */
|
|
|
|
|
|
|
|
line_t *mark[MAXMARK]; /* line markers */
|
|
|
|
int markno; /* line marker count */
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
/* getnum: return a relative line number from the command buffer */
|
|
|
|
long
|
|
|
|
getnum(first)
|
|
|
|
int first;
|
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
pattern_t *pat;
|
1993-04-08 05:07:16 +04:00
|
|
|
char c;
|
|
|
|
|
|
|
|
skipblanks();
|
|
|
|
if (isdigit(*ibufp))
|
|
|
|
return strtol(ibufp, &ibufp, 10);
|
|
|
|
switch(c = *ibufp) {
|
|
|
|
case '.':
|
|
|
|
ibufp++;
|
|
|
|
return first ? curln : ERR;
|
|
|
|
case '$':
|
|
|
|
ibufp++;
|
|
|
|
return first ? lastln : ERR;
|
|
|
|
case '/':
|
|
|
|
case '?':
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((pat = optpat()) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
else if (*ibufp == c)
|
|
|
|
ibufp++;
|
1993-05-08 14:49:52 +04:00
|
|
|
return first ? patscan(pat, (c == '/') ? 1 : 0) : ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
case '^':
|
|
|
|
case '-':
|
|
|
|
case '+':
|
|
|
|
return first ? curln : 1;
|
|
|
|
case '\'':
|
|
|
|
ibufp++;
|
1993-04-14 16:22:14 +04:00
|
|
|
return (first && islower(*ibufp)) ? getaddr(mark[*ibufp++ - 'a']) : ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
case '%':
|
|
|
|
case ',':
|
|
|
|
case ';':
|
|
|
|
if (first) {
|
|
|
|
ibufp++;
|
|
|
|
line2 = (c == ';') ? curln : 1;
|
|
|
|
nlines++;
|
|
|
|
return lastln;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return first ? EOF : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* gflags */
|
|
|
|
#define GLB 001 /* global command */
|
|
|
|
#define GPR 002 /* print after command */
|
|
|
|
#define GLS 004 /* list after command */
|
|
|
|
#define GNP 010 /* enumerate after command */
|
|
|
|
#define GSG 020 /* global substitute */
|
|
|
|
|
|
|
|
|
|
|
|
/* VRFYCMD: verify the command suffix in the command buffer */
|
|
|
|
#define VRFYCMD() { \
|
|
|
|
int done = 0; \
|
|
|
|
do { \
|
|
|
|
switch(*ibufp) { \
|
|
|
|
case 'p': \
|
|
|
|
gflag |= GPR, ibufp++; \
|
|
|
|
break; \
|
|
|
|
case 'l': \
|
|
|
|
gflag |= GLS, ibufp++; \
|
|
|
|
break; \
|
|
|
|
case 'n': \
|
|
|
|
gflag |= GNP, ibufp++; \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
done++; \
|
|
|
|
} \
|
|
|
|
} while (!done); \
|
|
|
|
if (*ibufp++ != '\n') { \
|
|
|
|
sprintf(errmsg, "invalid command suffix"); \
|
|
|
|
return ERR; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-14 16:22:14 +04:00
|
|
|
/* ckglob: set lines matching a pattern in the command buffer; return
|
1993-04-08 05:07:16 +04:00
|
|
|
global status */
|
|
|
|
ckglob()
|
|
|
|
{
|
1993-04-23 06:08:48 +04:00
|
|
|
pattern_t *pat;
|
1993-04-08 05:07:16 +04:00
|
|
|
char c, delim;
|
|
|
|
char *s;
|
|
|
|
int nomatch;
|
|
|
|
long n;
|
1993-04-23 06:08:48 +04:00
|
|
|
line_t *lp;
|
|
|
|
int gflag = 0; /* print suffix of interactive cmd */
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
if ((c = *ibufp) == 'V' || c == 'G')
|
1993-04-23 06:08:48 +04:00
|
|
|
gflag = GLB;
|
1993-04-08 05:07:16 +04:00
|
|
|
else if (c != 'g' && c != 'v')
|
|
|
|
return 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(1, lastln) < 0)
|
1993-04-23 06:08:48 +04:00
|
|
|
return ERR;
|
|
|
|
else if ((delim = *++ibufp) == ' ' || delim == '\n') {
|
|
|
|
sprintf(errmsg, "invalid pattern delimiter");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-23 06:08:48 +04:00
|
|
|
} else if ((pat = optpat()) == NULL)
|
|
|
|
return ERR;
|
|
|
|
else if (*ibufp == delim)
|
1993-04-08 05:07:16 +04:00
|
|
|
ibufp++;
|
1993-04-23 06:08:48 +04:00
|
|
|
if (gflag)
|
|
|
|
VRFYCMD(); /* get print suffix */
|
1993-05-08 14:49:52 +04:00
|
|
|
for (lp = getlp(n = 1); n <= lastln; n++, lp = lp->next) {
|
|
|
|
if ((s = gettxt(lp)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-23 06:08:48 +04:00
|
|
|
lp->len &= ~ACTV; /* zero ACTV bit */
|
1993-05-08 14:49:52 +04:00
|
|
|
if (isbinary)
|
|
|
|
s = nultonl(s, lp->len & ~ACTV);
|
1993-04-08 05:07:16 +04:00
|
|
|
if (line1 <= n && n <= line2
|
1993-04-23 06:08:48 +04:00
|
|
|
&& (!(nomatch = regexec(pat, s, 0, NULL, 0))
|
1993-04-08 05:07:16 +04:00
|
|
|
&& (c == 'g' || c == 'G')
|
|
|
|
|| nomatch && (c == 'v' || c == 'V')))
|
1993-04-23 06:08:48 +04:00
|
|
|
lp->len |= ACTV;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-04-23 06:08:48 +04:00
|
|
|
return gflag | GSG;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* doglob: apply command list in the command buffer to the active
|
|
|
|
lines in a range; return command status */
|
|
|
|
long
|
1993-04-23 06:08:48 +04:00
|
|
|
doglob(gflag)
|
|
|
|
int gflag;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-04-23 06:08:48 +04:00
|
|
|
static char *ocmd = NULL;
|
1993-05-08 14:49:52 +04:00
|
|
|
static int ocmdsz = 0;
|
1993-04-23 06:08:48 +04:00
|
|
|
|
|
|
|
line_t *lp = NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
long lc;
|
|
|
|
int status;
|
|
|
|
int n;
|
1993-04-23 06:08:48 +04:00
|
|
|
int interact = gflag & ~GSG; /* GLB & gflag ? */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *cmd = NULL;
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
if (!interact && (cmd = getcmdv(&n, 0)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
ureset();
|
|
|
|
for (;;) {
|
1993-05-08 14:49:52 +04:00
|
|
|
for (lp = getlp(lc = 1); lc <= lastln; lc++, lp = lp->next)
|
1993-04-23 06:08:48 +04:00
|
|
|
if (lp->len & ACTV) /* active line */
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
if (lc > lastln)
|
|
|
|
break;
|
1993-04-23 06:08:48 +04:00
|
|
|
lp->len ^= ACTV; /* zero ACTV bit */
|
1993-04-08 05:07:16 +04:00
|
|
|
curln = lc;
|
1993-04-23 06:08:48 +04:00
|
|
|
if (interact) {
|
1993-04-08 05:07:16 +04:00
|
|
|
/* print curln and get a command in global syntax */
|
1993-05-08 14:49:52 +04:00
|
|
|
if (doprint(curln, curln, 0) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
while ((n = getline()) > 0
|
1993-04-08 05:07:16 +04:00
|
|
|
&& ibuf[n - 1] != '\n')
|
|
|
|
clearerr(stdin);
|
1993-05-08 14:49:52 +04:00
|
|
|
if (n < 0)
|
|
|
|
return ERR;
|
|
|
|
else if (n == 0) {
|
|
|
|
sprintf(errmsg, "unexpected end-of-file");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-20 07:15:26 +04:00
|
|
|
} else if (n == 1 && !strcmp(ibuf, "\n"))
|
1993-04-08 05:07:16 +04:00
|
|
|
continue;
|
1993-05-20 07:15:26 +04:00
|
|
|
else if (n == 2 && !strcmp(ibuf, "&\n")) {
|
1993-04-08 05:07:16 +04:00
|
|
|
if (cmd == NULL) {
|
1993-04-23 06:08:48 +04:00
|
|
|
sprintf(errmsg, "no previous command");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-23 06:08:48 +04:00
|
|
|
} else cmd = ocmd;
|
1993-05-08 14:49:52 +04:00
|
|
|
} else if ((cmd = getcmdv(&n, 0)) == NULL)
|
1993-04-23 06:08:48 +04:00
|
|
|
return ERR;
|
|
|
|
else {
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(ocmd, ocmdsz, n + 1, ERR);
|
|
|
|
memcpy(ocmd, cmd, n + 1);
|
|
|
|
cmd = ocmd;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-04-23 06:08:48 +04:00
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
ibufp = cmd;
|
|
|
|
for (; *ibufp;)
|
|
|
|
if ((status = getlist()) < 0
|
|
|
|
|| (status = docmd(1)) < 0
|
|
|
|
|| (status > 0
|
1993-05-08 14:49:52 +04:00
|
|
|
&& (status = doprint(curln, curln, status)) < 0))
|
1993-04-08 05:07:16 +04:00
|
|
|
return status;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
return ((interact & ~GLB ) && doprint(curln, curln, interact) < 0) ? ERR : curln;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-10 15:46:38 +04:00
|
|
|
/* GETLINE3: get a legal address from the command buffer */
|
|
|
|
#define GETLINE3(num) \
|
|
|
|
{ \
|
|
|
|
long ol1, ol2; \
|
|
|
|
\
|
|
|
|
ol1 = line1, ol2 = line2; \
|
|
|
|
if (getlist() < 0) \
|
|
|
|
return ERR; \
|
|
|
|
if (line2 < 0 || lastln < line2) { \
|
|
|
|
sprintf(errmsg, "invalid address"); \
|
|
|
|
return ERR; \
|
|
|
|
} \
|
|
|
|
num = line2; \
|
|
|
|
line1 = ol1, line2 = ol2; \
|
|
|
|
}
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
/* sgflags */
|
|
|
|
#define SGG 001 /* complement previous global substitute suffix */
|
|
|
|
#define SGP 002 /* complement previous print suffix */
|
1993-04-23 06:08:48 +04:00
|
|
|
#define SGR 004 /* use last regex instead of last pat */
|
1993-04-08 05:07:16 +04:00
|
|
|
#define SGF 010 /* newline found */
|
|
|
|
|
1993-04-15 08:58:32 +04:00
|
|
|
long ucurln = -1; /* if >= 0, undo enabled */
|
1993-04-16 12:38:44 +04:00
|
|
|
long ulastln = -1; /* if >= 0, undo enabled */
|
1993-04-08 05:07:16 +04:00
|
|
|
int usw = 0; /* if set, undo last undo */
|
|
|
|
int patlock = 0; /* if set, pattern not released by optpat() */
|
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
long rows = 22; /* scroll length: ws_row - 2 */
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
/* docmd: execute the next command in command buffer; return print
|
|
|
|
request, if any */
|
|
|
|
docmd(glob)
|
|
|
|
int glob;
|
|
|
|
{
|
1993-04-23 06:08:48 +04:00
|
|
|
static pattern_t *pat = NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
static int sgflag = 0;
|
|
|
|
|
|
|
|
pattern_t *tpat;
|
1993-04-23 06:08:48 +04:00
|
|
|
char *fnp;
|
1993-04-08 05:07:16 +04:00
|
|
|
int gflag = 0;
|
1993-04-23 06:08:48 +04:00
|
|
|
int sflags = 0;
|
|
|
|
long num = 0;
|
|
|
|
int n = 0;
|
|
|
|
int c;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
skipblanks();
|
1993-04-23 06:08:48 +04:00
|
|
|
switch(c = *ibufp++) {
|
1993-04-08 05:07:16 +04:00
|
|
|
case 'a':
|
|
|
|
VRFYCMD();
|
|
|
|
if (!glob) ureset();
|
1993-04-10 15:46:38 +04:00
|
|
|
if (append(line2, glob) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case 'c':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(curln, curln) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
|
|
|
if (!glob) ureset();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (lndelete(line1, line2) < 0 || append(curln, glob) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case 'd':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(curln, curln) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
|
|
|
if (!glob) ureset();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (lndelete(line1, line2) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-14 16:22:14 +04:00
|
|
|
else if (nextln(curln, lastln) != 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
curln = nextln(curln, lastln);
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'e':
|
1993-04-23 06:08:48 +04:00
|
|
|
if (modified)
|
1993-04-08 05:07:16 +04:00
|
|
|
return EMOD;
|
|
|
|
/* fall through */
|
|
|
|
case 'E':
|
|
|
|
if (nlines > 0) {
|
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
} else if (!isspace(*ibufp)) {
|
|
|
|
sprintf(errmsg, "unexpected command suffix");
|
|
|
|
return ERR;
|
1993-04-23 06:08:48 +04:00
|
|
|
} else if ((fnp = getfn()) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
1993-05-08 14:49:52 +04:00
|
|
|
memset(mark, 0, sizeof mark);
|
|
|
|
lndelete(1, lastln);
|
1993-04-08 05:07:16 +04:00
|
|
|
ureset();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (sbclose() < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
else if (sbopen() < 0)
|
|
|
|
return FATAL;
|
1993-04-23 06:08:48 +04:00
|
|
|
if (*fnp && *fnp != '!') strcpy(dfn, fnp);
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef BACKWARDS
|
|
|
|
if (*fnp == '\0' && *dfn == '\0') {
|
|
|
|
sprintf(errmsg, "no current filename");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
#endif
|
1993-04-23 06:08:48 +04:00
|
|
|
if (doread(0, *fnp ? fnp : dfn) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
ureset();
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 0;
|
1993-04-16 12:38:44 +04:00
|
|
|
ucurln = ulastln = -1;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (nlines > 0) {
|
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
} else if (!isspace(*ibufp)) {
|
|
|
|
sprintf(errmsg, "unexpected command suffix");
|
|
|
|
return ERR;
|
1993-04-23 06:08:48 +04:00
|
|
|
} else if ((fnp = getfn()) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
else if (*fnp == '!') {
|
|
|
|
sprintf(errmsg, "invalid redirection");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
VRFYCMD();
|
1993-04-23 06:08:48 +04:00
|
|
|
if (*fnp) strcpy(dfn, fnp);
|
|
|
|
printf("%s\n", dfn);
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
1993-04-23 06:08:48 +04:00
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
sprintf(errmsg, "cannot nest global commands");
|
|
|
|
return ERR;
|
|
|
|
case 'h':
|
1993-04-08 05:07:16 +04:00
|
|
|
if (nlines > 0) {
|
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
VRFYCMD();
|
1993-04-23 06:08:48 +04:00
|
|
|
if (*errmsg) fprintf(stderr, "%s\n", errmsg);
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
1993-04-23 06:08:48 +04:00
|
|
|
case 'H':
|
1993-04-08 05:07:16 +04:00
|
|
|
if (nlines > 0) {
|
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
VRFYCMD();
|
1993-04-23 06:08:48 +04:00
|
|
|
if ((garrulous = 1 - garrulous) && *errmsg)
|
|
|
|
fprintf(stderr, "%s\n", errmsg);
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'i':
|
1993-04-14 16:22:14 +04:00
|
|
|
if (line2 == 0) {
|
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
VRFYCMD();
|
|
|
|
if (!glob) ureset();
|
1993-04-10 15:46:38 +04:00
|
|
|
if (append(prevln(line2, lastln), glob) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case 'j':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(curln, curln + 1) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
|
|
|
if (!glob) ureset();
|
1993-04-15 08:58:32 +04:00
|
|
|
if (line1 != line2 && join(line1, line2) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case 'k':
|
1993-05-08 14:49:52 +04:00
|
|
|
c = *ibufp++;
|
1993-04-14 16:22:14 +04:00
|
|
|
if (line2 == 0) {
|
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
} else if (!islower(c)) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "invalid mark character");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
VRFYCMD();
|
1993-04-14 16:22:14 +04:00
|
|
|
if (!mark[c - 'a']) markno++;
|
1993-05-08 14:49:52 +04:00
|
|
|
mark[c - 'a'] = getlp(line2);
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'l':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(curln, curln) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (doprint(line1, line2, gflag | GLS) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
gflag = 0;
|
|
|
|
break;
|
|
|
|
case 'm':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(curln, curln) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-10 15:46:38 +04:00
|
|
|
GETLINE3(num);
|
1993-04-08 05:07:16 +04:00
|
|
|
if (line1 <= num && num < line2) {
|
|
|
|
sprintf(errmsg, "invalid destination");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-10 15:46:38 +04:00
|
|
|
VRFYCMD();
|
1993-04-08 05:07:16 +04:00
|
|
|
if (!glob) ureset();
|
1993-04-16 12:38:44 +04:00
|
|
|
if (num == line1 - 1 || num == line2)
|
1993-04-15 08:58:32 +04:00
|
|
|
curln = line2;
|
|
|
|
else if (move(num) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-16 12:38:44 +04:00
|
|
|
else
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'n':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(curln, curln) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (doprint(line1, line2, gflag | GNP) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
gflag = 0;
|
|
|
|
break;
|
|
|
|
case 'p':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(curln, curln) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (doprint(line1, line2, gflag | GPR) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
gflag = 0;
|
|
|
|
break;
|
1993-04-23 06:08:48 +04:00
|
|
|
case 'P':
|
1993-04-08 05:07:16 +04:00
|
|
|
if (nlines > 0) {
|
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
VRFYCMD();
|
1993-04-23 06:08:48 +04:00
|
|
|
prompt = prompt ? NULL : optarg ? optarg : dps;
|
|
|
|
break;
|
|
|
|
case 'q':
|
1993-04-08 05:07:16 +04:00
|
|
|
case 'Q':
|
|
|
|
if (nlines > 0) {
|
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
VRFYCMD();
|
1993-04-23 06:08:48 +04:00
|
|
|
gflag = (modified && !scripted && c != 'Q') ? EMOD : EOF;
|
|
|
|
break;
|
1993-04-08 05:07:16 +04:00
|
|
|
case 'r':
|
1993-04-10 15:46:38 +04:00
|
|
|
if (!isspace(*ibufp)) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected command suffix");
|
|
|
|
return ERR;
|
1993-04-14 16:22:14 +04:00
|
|
|
} else if (nlines == 0)
|
|
|
|
line2 = lastln;
|
1993-04-23 06:08:48 +04:00
|
|
|
if ((fnp = getfn()) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
|
|
|
if (!glob) ureset();
|
1993-04-23 06:08:48 +04:00
|
|
|
if (*dfn == '\0' && *fnp != '!') strcpy(dfn, fnp);
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef BACKWARDS
|
|
|
|
if (*fnp == '\0' && *dfn == '\0') {
|
|
|
|
sprintf(errmsg, "no current filename");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
#endif
|
1993-04-23 06:08:48 +04:00
|
|
|
if ((num = doread(line2, *fnp ? fnp : dfn)) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-15 08:58:32 +04:00
|
|
|
else if (num && num != lastln)
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
do {
|
|
|
|
switch(*ibufp) {
|
|
|
|
case '\n':
|
1993-04-23 06:08:48 +04:00
|
|
|
sflags |=SGF;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'g':
|
1993-04-23 06:08:48 +04:00
|
|
|
sflags |= SGG;
|
1993-04-08 05:07:16 +04:00
|
|
|
ibufp++;
|
|
|
|
break;
|
|
|
|
case 'p':
|
1993-04-23 06:08:48 +04:00
|
|
|
sflags |= SGP;
|
1993-04-08 05:07:16 +04:00
|
|
|
ibufp++;
|
|
|
|
break;
|
|
|
|
case 'r':
|
1993-04-23 06:08:48 +04:00
|
|
|
sflags |= SGR;
|
1993-04-08 05:07:16 +04:00
|
|
|
ibufp++;
|
|
|
|
break;
|
|
|
|
default:
|
1993-04-23 06:08:48 +04:00
|
|
|
if (sflags) {
|
|
|
|
sprintf(errmsg, "invalid command suffix");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-04-23 06:08:48 +04:00
|
|
|
} while (sflags && *ibufp != '\n');
|
|
|
|
if (sflags && !pat) {
|
|
|
|
sprintf(errmsg, "no previous substitution");
|
|
|
|
return ERR;
|
|
|
|
} else if (!(sflags & SGF))
|
1993-04-08 05:07:16 +04:00
|
|
|
sgflag &= 0xff;
|
1993-04-23 06:08:48 +04:00
|
|
|
tpat = pat;
|
1993-04-08 05:07:16 +04:00
|
|
|
spl1();
|
1993-04-23 06:08:48 +04:00
|
|
|
if ((!sflags || (sflags & SGR))
|
1993-04-08 05:07:16 +04:00
|
|
|
&& (tpat = optpat()) == NULL)
|
|
|
|
return ERR;
|
1993-04-23 06:08:48 +04:00
|
|
|
else if (tpat != pat) {
|
|
|
|
if (pat) {
|
|
|
|
regfree(pat);
|
|
|
|
free(pat);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-04-23 06:08:48 +04:00
|
|
|
pat = tpat;
|
1993-04-08 05:07:16 +04:00
|
|
|
patlock = 1; /* reserve pattern */
|
1993-04-23 06:08:48 +04:00
|
|
|
} else if (pat == NULL) {
|
|
|
|
/* NOTREACHED */
|
|
|
|
sprintf(errmsg, "no previous substitution");
|
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
spl0();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (!sflags && (sgflag = getrhs(glob)) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-16 12:38:44 +04:00
|
|
|
else if (glob)
|
1993-04-08 05:07:16 +04:00
|
|
|
sgflag |= GLB;
|
|
|
|
else
|
|
|
|
sgflag &= ~GLB;
|
1993-04-23 06:08:48 +04:00
|
|
|
if (sflags & SGG)
|
1993-04-08 05:07:16 +04:00
|
|
|
sgflag ^= GSG;
|
1993-04-23 06:08:48 +04:00
|
|
|
if (sflags & SGP)
|
1993-04-28 07:37:34 +04:00
|
|
|
sgflag ^= GPR, sgflag &= ~(GLS | GNP);
|
1993-04-08 05:07:16 +04:00
|
|
|
do {
|
|
|
|
switch(*ibufp) {
|
|
|
|
case 'p':
|
|
|
|
sgflag |= GPR, ibufp++;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
sgflag |= GLS, ibufp++;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
sgflag |= GNP, ibufp++;
|
|
|
|
break;
|
|
|
|
default:
|
1993-04-23 06:08:48 +04:00
|
|
|
n++;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-04-23 06:08:48 +04:00
|
|
|
} while (!n);
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(curln, curln) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
|
|
|
if (!glob) ureset();
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((n = subst(pat, sgflag)) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-23 06:08:48 +04:00
|
|
|
else if (n)
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 't':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(curln, curln) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-10 15:46:38 +04:00
|
|
|
GETLINE3(num);
|
1993-04-08 05:07:16 +04:00
|
|
|
VRFYCMD();
|
|
|
|
if (!glob) ureset();
|
|
|
|
if (transfer(num) < 0)
|
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
if (nlines > 0) {
|
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
VRFYCMD();
|
|
|
|
if (undo() < 0)
|
|
|
|
return ERR;
|
|
|
|
break;
|
1993-04-23 06:08:48 +04:00
|
|
|
case 'v':
|
|
|
|
case 'V':
|
|
|
|
sprintf(errmsg, "cannot nest global commands");
|
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
case 'w':
|
1993-04-23 06:08:48 +04:00
|
|
|
case 'W':
|
|
|
|
if ((n = *ibufp) == 'q' || n == 'Q') {
|
1993-04-08 05:07:16 +04:00
|
|
|
gflag = EOF;
|
|
|
|
ibufp++;
|
1993-04-14 16:22:14 +04:00
|
|
|
}
|
|
|
|
if (!isspace(*ibufp)) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected command suffix");
|
|
|
|
return ERR;
|
1993-04-23 06:08:48 +04:00
|
|
|
} else if ((fnp = getfn()) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
if (nlines == 0 && !lastln)
|
|
|
|
line1 = line2 = 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
else if (ckrange(1, lastln) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
1993-04-23 06:08:48 +04:00
|
|
|
if (*dfn == '\0' && *fnp != '!') strcpy(dfn, fnp);
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef BACKWARDS
|
|
|
|
if (*fnp == '\0' && *dfn == '\0') {
|
|
|
|
sprintf(errmsg, "no current filename");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
#endif
|
1993-04-23 06:08:48 +04:00
|
|
|
if ((num = dowrite(line1, line2, *fnp ? fnp : dfn, (c == 'W') ? "a" : "w")) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-23 06:08:48 +04:00
|
|
|
else if (num == lastln)
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 0;
|
1993-04-23 06:08:48 +04:00
|
|
|
else if (n == 'q' && modified)
|
|
|
|
gflag = EMOD;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
if (nlines > 0) {
|
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
VRFYCMD();
|
|
|
|
#ifdef DES
|
|
|
|
des = getkey();
|
|
|
|
#else
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "crypt unavailable");
|
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'z':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ckrange(line1 = 1, curln + !glob) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-14 16:22:14 +04:00
|
|
|
else if ('0' < *ibufp && *ibufp <= '9')
|
1993-04-10 15:46:38 +04:00
|
|
|
rows = strtol(ibufp, &ibufp, 10);
|
1993-04-08 05:07:16 +04:00
|
|
|
VRFYCMD();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (doprint(line2, min(lastln, line2 + rows - 1), gflag) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
gflag = 0;
|
|
|
|
break;
|
1993-05-08 14:49:52 +04:00
|
|
|
case '=':
|
|
|
|
VRFYCMD();
|
|
|
|
printf("%d\n", nlines ? line2 : lastln);
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
#ifndef VI_BANG
|
|
|
|
if (nlines > 0) {
|
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if ((sflags = getshcmd()) < 0)
|
|
|
|
return ERR;
|
|
|
|
VRFYCMD();
|
|
|
|
if (sflags) printf("%s\n", shcmd + 1);
|
|
|
|
#ifdef VI_BANG
|
|
|
|
if (nlines == 0) {
|
|
|
|
#endif
|
|
|
|
system(shcmd + 1);
|
|
|
|
if (!scripted) printf("!\n");
|
|
|
|
break;
|
|
|
|
#ifdef VI_BANG
|
|
|
|
}
|
|
|
|
if (!lastln && !line1 && !line2) {
|
|
|
|
if (!glob) ureset();
|
|
|
|
} else if (ckrange(curln, curln) < 0)
|
|
|
|
return ERR;
|
|
|
|
else {
|
|
|
|
if (!glob) ureset();
|
|
|
|
if (lndelete(line1, line2) < 0)
|
|
|
|
return ERR;
|
|
|
|
line2 = curln;
|
|
|
|
modified = 1;
|
|
|
|
}
|
|
|
|
if ((num = doread(line2, shcmd)) < 0)
|
|
|
|
return ERR;
|
|
|
|
else if (num && num != lastln)
|
|
|
|
modified = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case '\n':
|
|
|
|
if (ckrange(line1 = 1, curln + !glob) < 0
|
|
|
|
|| doprint(line2, line2, 0) < 0)
|
|
|
|
return ERR;
|
|
|
|
break;
|
1993-04-08 05:07:16 +04:00
|
|
|
default:
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "unknown command");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
return gflag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
/* ckrange: return status of line number range check */
|
|
|
|
ckrange(def1, def2)
|
1993-04-08 05:07:16 +04:00
|
|
|
long def1, def2;
|
|
|
|
{
|
|
|
|
if (nlines == 0) {
|
|
|
|
line1 = def1;
|
|
|
|
line2 = def2;
|
|
|
|
}
|
|
|
|
if (line1 > line2 || 1 > line1 || line2 > lastln) {
|
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
/* patscan: return the number of the next line matching a pattern in a
|
1993-04-08 05:07:16 +04:00
|
|
|
given direction. wrap around begin/end of line queue if necessary */
|
|
|
|
long
|
1993-05-08 14:49:52 +04:00
|
|
|
patscan(pat, dir)
|
1993-04-08 05:07:16 +04:00
|
|
|
pattern_t *pat;
|
|
|
|
int dir;
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
long n = curln;
|
1993-05-08 14:49:52 +04:00
|
|
|
line_t *lp;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
do {
|
1993-05-08 14:49:52 +04:00
|
|
|
if (n = dir ? nextln(n, lastln) : prevln(n, lastln)) {
|
|
|
|
if ((s = gettxt(lp = getlp(n))) == NULL)
|
1993-04-14 16:22:14 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
if (isbinary)
|
|
|
|
s = nultonl(s, lp->len & ~ACTV);
|
|
|
|
if (!regexec(pat, s, 0, NULL, 0))
|
1993-04-14 16:22:14 +04:00
|
|
|
return n;
|
1993-05-08 14:49:52 +04:00
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
} while (n != curln);
|
|
|
|
sprintf(errmsg, "no match");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
/* getfn: return pointer to copy of filename in the command buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *
|
|
|
|
getfn()
|
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
static char *file = NULL;
|
|
|
|
static int filesz = 0;
|
|
|
|
|
|
|
|
int n;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
if (*ibufp != '\n') {
|
|
|
|
skipblanks();
|
1993-04-23 06:08:48 +04:00
|
|
|
if (*ibufp == '\n') {
|
|
|
|
sprintf(errmsg, "invalid filename");
|
1993-04-08 05:07:16 +04:00
|
|
|
return NULL;
|
1993-05-08 14:49:52 +04:00
|
|
|
} else if ((ibufp = getcmdv(&n, 1)) == NULL)
|
1993-04-23 06:08:48 +04:00
|
|
|
return NULL;
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef VI_BANG
|
|
|
|
else if (*ibufp == '!') {
|
|
|
|
ibufp++;
|
|
|
|
if ((n = getshcmd()) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (n) printf("%s\n", shcmd + 1);
|
|
|
|
return shcmd;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else if (n - 1 > MAXFNAME) {
|
|
|
|
sprintf(errmsg, "filename too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef BACKWARDS
|
|
|
|
else if (*dfn == '\0') {
|
1993-04-23 06:08:48 +04:00
|
|
|
sprintf(errmsg, "no current filename");
|
1993-04-08 05:07:16 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
#endif
|
|
|
|
CKBUF(file, filesz, MAXFNAME + 1, NULL);
|
|
|
|
for (n = 0; *ibufp != '\n';)
|
|
|
|
file[n++] = *ibufp++;
|
|
|
|
file[n] = '\0';
|
1993-04-08 05:07:16 +04:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* getrhs: extract substitution template from the command buffer */
|
1993-05-08 14:49:52 +04:00
|
|
|
getrhs(glob)
|
1993-04-08 05:07:16 +04:00
|
|
|
int glob;
|
|
|
|
{
|
|
|
|
char delim;
|
|
|
|
|
|
|
|
if ((delim = *ibufp) == '\n') {
|
1993-05-08 14:49:52 +04:00
|
|
|
rhbufi = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
return GPR;
|
1993-05-08 14:49:52 +04:00
|
|
|
} else if (makesub(glob) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
else if (*ibufp == '\n')
|
|
|
|
return GPR;
|
|
|
|
else if (*ibufp == delim)
|
|
|
|
ibufp++;
|
|
|
|
if ('1' <= *ibufp && *ibufp <= '9')
|
|
|
|
return (int) strtol(ibufp, &ibufp, 10) << 8;
|
|
|
|
else if (*ibufp == 'g') {
|
|
|
|
ibufp++;
|
|
|
|
return GSG;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
/* makesub: return pointer to copy of substitution template in the command
|
|
|
|
buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *
|
1993-05-08 14:49:52 +04:00
|
|
|
makesub(glob)
|
1993-04-08 05:07:16 +04:00
|
|
|
int glob;
|
|
|
|
{
|
|
|
|
int n = 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
int i = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
char delim = *ibufp++;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (*ibufp == '%' && *(ibufp + 1) == delim) {
|
|
|
|
ibufp++;
|
1993-05-08 14:49:52 +04:00
|
|
|
if (!rhbuf) sprintf(errmsg, "no previous substitution");
|
|
|
|
return rhbuf;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
while (*ibufp != delim) {
|
|
|
|
CKBUF(rhbuf, rhbufsz, i + 2, NULL);
|
|
|
|
if ((c = rhbuf[i++] = *ibufp++) == '\n' && *ibufp == '\0') {
|
|
|
|
i--, ibufp--;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
1993-05-08 14:49:52 +04:00
|
|
|
} else if (c != '\\')
|
1993-04-08 05:07:16 +04:00
|
|
|
;
|
1993-05-08 14:49:52 +04:00
|
|
|
else if ((rhbuf[i++] = *ibufp++) != '\n')
|
1993-04-08 05:07:16 +04:00
|
|
|
;
|
1993-05-08 14:49:52 +04:00
|
|
|
else if (!glob) {
|
|
|
|
while ((n = getline()) == 0
|
|
|
|
|| n > 0 && ibuf[n - 1] != '\n')
|
1993-04-08 05:07:16 +04:00
|
|
|
clearerr(stdin);
|
1993-05-08 14:49:52 +04:00
|
|
|
if (n < 0)
|
|
|
|
return NULL;
|
|
|
|
} else
|
|
|
|
/*NOTREACHED*/
|
1993-04-08 05:07:16 +04:00
|
|
|
;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(rhbuf, rhbufsz, i + 1, NULL);
|
|
|
|
rhbuf[rhbufi = i] = '\0';
|
|
|
|
return rhbuf;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
/* getshcmd: read a shell command up a maximum size from stdin; return
|
|
|
|
substitution status */
|
|
|
|
int
|
1993-05-08 14:49:52 +04:00
|
|
|
getshcmd()
|
1993-04-23 06:08:48 +04:00
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
static char *buf = NULL;
|
|
|
|
static int n = 0;
|
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
char *ip;
|
1993-05-08 14:49:52 +04:00
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
1993-04-23 06:08:48 +04:00
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((ip = ibufp = getcmdv(&j, 1)) == NULL)
|
1993-04-23 06:08:48 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(buf, n, j + 1, ERR);
|
|
|
|
buf[i++] = '!'; /* prefix command w/ bang */
|
|
|
|
while (*ibufp != '\n')
|
1993-04-23 06:08:48 +04:00
|
|
|
switch (*ibufp) {
|
|
|
|
default:
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(buf, n, i + 2, ERR);
|
|
|
|
buf[i++] = *ibufp;
|
|
|
|
if (*ibufp++ == '\\')
|
|
|
|
buf[i++] = *ibufp++;
|
1993-04-23 06:08:48 +04:00
|
|
|
break;
|
|
|
|
case '!':
|
1993-05-08 14:49:52 +04:00
|
|
|
if (ip != ibufp) {
|
|
|
|
CKBUF(buf, n, i + 1, ERR);
|
|
|
|
buf[i++] = *ibufp++;
|
|
|
|
}
|
|
|
|
#ifdef BACKWARDS
|
|
|
|
else if (shcmd == NULL || *(shcmd + 1) == '\0')
|
|
|
|
#else
|
|
|
|
else if (shcmd == NULL)
|
|
|
|
#endif
|
|
|
|
{
|
1993-04-23 06:08:48 +04:00
|
|
|
sprintf(errmsg, "no previous command");
|
|
|
|
return ERR;
|
|
|
|
} else {
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(buf, n, i + shcmdi, ERR);
|
|
|
|
for (ip = shcmd + 1; ip < shcmd + shcmdi;)
|
|
|
|
buf[i++] = *ip++;
|
|
|
|
ip = ibufp++;
|
1993-04-23 06:08:48 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
if (*dfn == '\0') {
|
|
|
|
sprintf(errmsg, "no current filename");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
j = strlen(dfn);
|
|
|
|
CKBUF(buf, n, i + j, ERR);
|
|
|
|
for (ip = dfn; ip < dfn + j;)
|
|
|
|
buf[i++] = *ip++;
|
|
|
|
ip = ibufp++;
|
1993-04-23 06:08:48 +04:00
|
|
|
break;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(shcmd, shcmdsz, i + 1, ERR);
|
|
|
|
memcpy(shcmd, buf, i);
|
|
|
|
shcmd[shcmdi = i] = '\0';
|
1993-04-23 06:08:48 +04:00
|
|
|
return *ip == '!' || *ip == '%';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
/* append: insert text from stdin to after line n; stop when either a
|
|
|
|
single period is read or EOF; return status */
|
|
|
|
append(n, glob)
|
|
|
|
long n;
|
|
|
|
int glob;
|
|
|
|
{
|
|
|
|
int l;
|
1993-05-08 14:49:52 +04:00
|
|
|
char *lp = ibuf;
|
1993-05-12 12:22:03 +04:00
|
|
|
char *eot;
|
1993-04-08 05:07:16 +04:00
|
|
|
undo_t *up = NULL;
|
|
|
|
|
|
|
|
for (curln = n;;) {
|
|
|
|
if (!glob) {
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((l = getline()) < 0)
|
|
|
|
return ERR;
|
|
|
|
else if (l == 0 || ibuf[l - 1] != '\n') {
|
1993-04-08 05:07:16 +04:00
|
|
|
clearerr(stdin);
|
|
|
|
return l ? EOF : 0;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
lp = ibuf;
|
1993-04-08 05:07:16 +04:00
|
|
|
} else if (*(lp = ibufp) == '\0')
|
|
|
|
return 0;
|
1993-05-12 12:22:03 +04:00
|
|
|
else {
|
1993-04-08 05:07:16 +04:00
|
|
|
while (*ibufp++ != '\n')
|
|
|
|
;
|
1993-05-12 12:22:03 +04:00
|
|
|
l = ibufp - lp;
|
|
|
|
}
|
|
|
|
if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
|
1993-04-08 05:07:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
1993-05-12 12:22:03 +04:00
|
|
|
eot = lp + l;
|
1993-04-08 05:07:16 +04:00
|
|
|
spl1();
|
1993-05-12 12:22:03 +04:00
|
|
|
do {
|
|
|
|
if ((lp = puttxt(lp)) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
} else if (up)
|
|
|
|
up->t = getlp(curln);
|
|
|
|
else if ((up = upush(UADD, curln, curln)) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
} while (lp != eot);
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
modified = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-05-12 12:22:03 +04:00
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
#ifdef sun
|
|
|
|
/* subst: change all text matching a pattern in a range of lines according to
|
|
|
|
a substitution template; return status */
|
1993-05-08 14:49:52 +04:00
|
|
|
subst(pat, gflag)
|
1993-04-08 05:07:16 +04:00
|
|
|
pattern_t *pat;
|
|
|
|
int gflag;
|
|
|
|
{
|
|
|
|
undo_t *up = NULL;
|
1993-05-08 14:49:52 +04:00
|
|
|
char *txt;
|
|
|
|
char *eot;
|
1993-04-08 05:07:16 +04:00
|
|
|
line_t *bp, *ep, *np;
|
|
|
|
long ocl;
|
|
|
|
long nsubs = 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
int len;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
ep = getlp(curln = line2);
|
|
|
|
for (bp = getlp(line1); bp != ep->next; bp = bp->next)
|
|
|
|
if ((len = regsub(pat, bp, gflag)) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
else if (!len) {
|
1993-04-08 05:07:16 +04:00
|
|
|
/* add copy of bp after current line - this avoids
|
|
|
|
overloading the undo structure, since only two
|
|
|
|
undo nodes are needed for the whole substitution;
|
|
|
|
the cost is high, but the less than if undo is
|
1993-04-23 06:08:48 +04:00
|
|
|
overloaded on a Sun evidently. XXX */
|
1993-04-08 05:07:16 +04:00
|
|
|
if ((np = lpdup(bp)) == NULL)
|
|
|
|
return ERR;
|
|
|
|
spl1();
|
|
|
|
lpqueue(np);
|
|
|
|
if (up)
|
1993-05-08 14:49:52 +04:00
|
|
|
up->t = getlp(curln);
|
1993-04-15 08:58:32 +04:00
|
|
|
else if ((up = upush(UADD, curln, curln)) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
} else {
|
1993-05-08 14:49:52 +04:00
|
|
|
txt = rbuf;
|
|
|
|
eot = rbuf + len;
|
1993-04-08 05:07:16 +04:00
|
|
|
spl1();
|
|
|
|
do {
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((txt = puttxt(txt)) == NULL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
return ERR;
|
1993-04-15 08:58:32 +04:00
|
|
|
} else if (up)
|
1993-05-08 14:49:52 +04:00
|
|
|
up->t = getlp(curln);
|
1993-04-15 08:58:32 +04:00
|
|
|
else if ((up = upush(UADD, curln, curln)) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
} while (txt != eot);
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
nsubs++;
|
|
|
|
}
|
|
|
|
ocl = curln;
|
1993-05-08 14:49:52 +04:00
|
|
|
lndelete(line1, line2);
|
1993-04-08 05:07:16 +04:00
|
|
|
curln = ocl - (line2 - line1 + 1);
|
1993-04-10 15:46:38 +04:00
|
|
|
if (nsubs == 0 && !(gflag & GLB)) {
|
|
|
|
sprintf(errmsg, "no match");
|
|
|
|
return ERR;
|
1993-04-28 07:37:34 +04:00
|
|
|
} else if ((gflag & (GPR | GLS | GNP))
|
1993-05-08 14:49:52 +04:00
|
|
|
&& doprint(curln, curln, gflag) < 0)
|
1993-04-28 07:37:34 +04:00
|
|
|
return ERR;
|
1993-04-10 15:46:38 +04:00
|
|
|
return 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
#else /* sun */
|
|
|
|
|
|
|
|
|
|
|
|
/* subst: change all text matching a pattern in a range of lines according to
|
|
|
|
a substitution template; return status */
|
1993-05-08 14:49:52 +04:00
|
|
|
subst(pat, gflag)
|
1993-04-08 05:07:16 +04:00
|
|
|
pattern_t *pat;
|
|
|
|
int gflag;
|
|
|
|
{
|
|
|
|
undo_t *up;
|
1993-05-08 14:49:52 +04:00
|
|
|
char *txt;
|
|
|
|
char *eot;
|
1993-04-08 05:07:16 +04:00
|
|
|
long lc;
|
|
|
|
int nsubs = 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
line_t *lp;
|
|
|
|
int len;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
curln = prevln(line1, lastln);
|
|
|
|
for (lc = 0; lc <= line2 - line1; lc++) {
|
1993-05-08 14:49:52 +04:00
|
|
|
lp = getlp(curln = nextln(curln, lastln));
|
|
|
|
if ((len = regsub(pat, lp, gflag)) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
else if (len) {
|
1993-04-08 05:07:16 +04:00
|
|
|
up = NULL;
|
1993-05-08 14:49:52 +04:00
|
|
|
lndelete(curln, curln);
|
|
|
|
txt = rbuf;
|
|
|
|
eot = rbuf + len;
|
1993-04-08 05:07:16 +04:00
|
|
|
spl1();
|
|
|
|
do {
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((txt = puttxt(txt)) == NULL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
return ERR;
|
1993-04-15 08:58:32 +04:00
|
|
|
} else if (up)
|
1993-05-08 14:49:52 +04:00
|
|
|
up->t = getlp(curln);
|
1993-04-15 08:58:32 +04:00
|
|
|
else if ((up = upush(UADD, curln, curln)) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
} while (txt != eot);
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
nsubs++;
|
|
|
|
}
|
|
|
|
}
|
1993-04-10 15:46:38 +04:00
|
|
|
if (nsubs == 0 && !(gflag & GLB)) {
|
|
|
|
sprintf(errmsg, "no match");
|
|
|
|
return ERR;
|
1993-04-28 07:37:34 +04:00
|
|
|
} else if ((gflag & (GPR | GLS | GNP))
|
1993-05-08 14:49:52 +04:00
|
|
|
&& doprint(curln, curln, gflag) < 0)
|
1993-04-28 07:37:34 +04:00
|
|
|
return ERR;
|
1993-04-10 15:46:38 +04:00
|
|
|
return 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
#endif /* sun */
|
|
|
|
|
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
/* regsub: replace text matched by a pattern according to a substitution
|
|
|
|
template; return pointer to the modified text */
|
1993-05-08 14:49:52 +04:00
|
|
|
regsub(pat, lp, gflag)
|
1993-04-08 05:07:16 +04:00
|
|
|
pattern_t *pat;
|
1993-05-08 14:49:52 +04:00
|
|
|
line_t *lp;
|
1993-04-08 05:07:16 +04:00
|
|
|
int gflag;
|
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
int off = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
int kth = gflag >> 8; /* substitute kth match only */
|
|
|
|
int chngd = 0;
|
|
|
|
int matchno = 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
int len;
|
|
|
|
int i = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
regmatch_t rm[SE_MAX];
|
1993-05-08 14:49:52 +04:00
|
|
|
char *txt;
|
|
|
|
char *eot;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((txt = gettxt(lp)) == NULL)
|
|
|
|
return ERR;
|
|
|
|
len = lp->len & ~ACTV;
|
|
|
|
eot = txt + len;
|
|
|
|
if (isbinary) txt = nultonl(txt, len);
|
1993-04-08 05:07:16 +04:00
|
|
|
if (!regexec(pat, txt, SE_MAX, rm, 0)) {
|
|
|
|
do {
|
|
|
|
if (!kth || kth == ++matchno) {
|
|
|
|
chngd++;
|
1993-05-08 14:49:52 +04:00
|
|
|
i = rm[0].rm_so;
|
|
|
|
CKBUF(rbuf, rbufsz, off + i, ERR);
|
|
|
|
if (isbinary) txt = nltonul(txt, rm[0].rm_eo);
|
|
|
|
memcpy(rbuf + off, txt, i);
|
|
|
|
if ((off = catsub(txt, rm, off += i)) < 0)
|
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
} else {
|
1993-05-08 14:49:52 +04:00
|
|
|
i = rm[0].rm_eo;
|
|
|
|
CKBUF(rbuf, rbufsz, off + i, ERR);
|
|
|
|
if (isbinary) txt = nltonul(txt, i);
|
|
|
|
memcpy(rbuf + off, txt, i);
|
|
|
|
off += i;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
txt += rm[0].rm_eo;
|
|
|
|
} while (*txt && (!chngd || (gflag & GSG) && rm[0].rm_eo)
|
|
|
|
&& !regexec(pat, txt, SE_MAX, rm, REG_NOTBOL));
|
1993-05-08 14:49:52 +04:00
|
|
|
i = eot - txt;
|
|
|
|
CKBUF(rbuf, rbufsz, off + i + 2, ERR);
|
|
|
|
if (i > 0 && !rm[0].rm_eo && (gflag & GSG)) {
|
1993-04-23 06:08:48 +04:00
|
|
|
sprintf(errmsg, "infinite substitution loop");
|
1993-05-08 14:49:52 +04:00
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
if (isbinary) txt = nltonul(txt, i);
|
|
|
|
memcpy(rbuf + off, txt, i);
|
|
|
|
memcpy(rbuf + off + i, "\n", 2);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
return chngd ? off + i + 1 : 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* join: replace a range of lines with the joined text of those lines */
|
|
|
|
join(from, to)
|
|
|
|
long from;
|
|
|
|
long to;
|
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
static char *buf = NULL;
|
|
|
|
static int n;
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
char *s;
|
|
|
|
int len = 0;
|
|
|
|
int size = 0;
|
|
|
|
line_t *bp, *ep;
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
ep = getlp(nextln(to, lastln));
|
|
|
|
for (bp = getlp(from); bp != ep; bp = bp->next, size += len) {
|
|
|
|
if ((s = gettxt(bp)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
len = bp->len & ~ACTV;
|
|
|
|
CKBUF(buf, n, size + len, ERR);
|
|
|
|
memcpy(buf + size, s, len);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(buf, n, size + 2, ERR);
|
|
|
|
memcpy(buf + size, "\n", 2);
|
|
|
|
lndelete(from, to);
|
1993-04-08 05:07:16 +04:00
|
|
|
curln = from - 1;
|
|
|
|
spl1();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (puttxt(buf) == NULL
|
1993-04-15 08:58:32 +04:00
|
|
|
|| upush(UADD, curln, curln) == NULL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
spl0();
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-15 08:58:32 +04:00
|
|
|
/* move: move a range of lines */
|
|
|
|
move(num)
|
|
|
|
long num;
|
|
|
|
{
|
|
|
|
line_t *b1, *a1, *b2, *a2;
|
|
|
|
long n = nextln(line2, lastln);
|
|
|
|
long p = prevln(line1, lastln);
|
|
|
|
|
|
|
|
spl1();
|
|
|
|
if (upush(UMOV, p, n) == NULL
|
|
|
|
|| upush(UMOV, num, nextln(num, lastln)) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
a1 = getlp(n);
|
1993-04-15 08:58:32 +04:00
|
|
|
if (num < line1)
|
1993-05-08 14:49:52 +04:00
|
|
|
b1 = getlp(p), b2 = getlp(num); /* this getlp last! */
|
|
|
|
else b2 = getlp(num), b1 = getlp(p); /* this getlp last! */
|
1993-04-15 08:58:32 +04:00
|
|
|
a2 = b2->next;
|
|
|
|
requeue(b2, b1->next);
|
|
|
|
requeue(a1->prev, a2);
|
|
|
|
requeue(b1, a1);
|
1993-04-16 12:38:44 +04:00
|
|
|
curln = num + ((num < line1) ? line2 - line1 + 1 : 0);
|
1993-04-15 08:58:32 +04:00
|
|
|
spl0();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
/* transfer: copy a range of lines; return status */
|
|
|
|
transfer(num)
|
|
|
|
long num;
|
|
|
|
{
|
|
|
|
line_t *lp;
|
|
|
|
long nl, nt, lc;
|
|
|
|
long mid = (num < line2) ? num : line2;
|
|
|
|
undo_t *up = NULL;
|
|
|
|
|
|
|
|
curln = num;
|
|
|
|
for (nt = 0, nl = line1; nl <= mid; nl++, nt++) {
|
|
|
|
spl1();
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((lp = lpdup(getlp(nl))) == NULL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
lpqueue(lp);
|
1993-04-15 08:58:32 +04:00
|
|
|
if (up)
|
|
|
|
up->t = lp;
|
|
|
|
else if ((up = upush(UADD, curln, curln)) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
}
|
|
|
|
for (nl += nt, lc = line2 + nt; nl <= lc; nl += 2, lc++) {
|
|
|
|
spl1();
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((lp = lpdup(getlp(nl))) == NULL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
lpqueue(lp);
|
1993-04-15 08:58:32 +04:00
|
|
|
if (up)
|
|
|
|
up->t = lp;
|
|
|
|
else if ((up = upush(UADD, curln, curln)) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
/* lndelete: delete a range of lines */
|
|
|
|
lndelete(from, to)
|
1993-04-08 05:07:16 +04:00
|
|
|
long from, to;
|
|
|
|
{
|
|
|
|
line_t *before, *after;
|
|
|
|
|
|
|
|
spl1();
|
1993-04-15 08:58:32 +04:00
|
|
|
if (upush(UDEL, from, to) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
after = getlp(nextln(to, lastln));
|
|
|
|
before = getlp(prevln(from, lastln)); /* this getlp last! */
|
1993-04-08 05:07:16 +04:00
|
|
|
requeue(before, after);
|
1993-04-15 08:58:32 +04:00
|
|
|
lastln -= to - from + 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
curln = prevln(from, lastln);
|
|
|
|
spl0();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
/* catsub: modify text according to a substitution template;
|
|
|
|
return offset to end of modified text */
|
|
|
|
catsub(boln, rm, off)
|
1993-04-08 05:07:16 +04:00
|
|
|
char *boln;
|
|
|
|
regmatch_t *rm;
|
1993-05-08 14:49:52 +04:00
|
|
|
int off;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
int j = 0;
|
|
|
|
int k = 0;
|
|
|
|
char *sub = rhbuf;
|
|
|
|
|
|
|
|
for (; sub - rhbuf < rhbufi; sub++)
|
|
|
|
if (*sub == '&') {
|
|
|
|
j = rm[0].rm_so;
|
|
|
|
k = rm[0].rm_eo;
|
|
|
|
CKBUF(rbuf, rbufsz, off + k - j, ERR);
|
|
|
|
while (j < k)
|
|
|
|
rbuf[off++] = boln[j++];
|
|
|
|
} else if (*sub == '\\' && '1' <= *++sub && *sub <= '9'
|
1993-04-08 05:07:16 +04:00
|
|
|
&& rm[*sub - '0'].rm_so >= 0
|
|
|
|
&& rm[*sub - '0'].rm_eo >= 0) {
|
1993-05-08 14:49:52 +04:00
|
|
|
j = rm[*sub - '0'].rm_so;
|
|
|
|
k = rm[*sub - '0'].rm_eo;
|
|
|
|
CKBUF(rbuf, rbufsz, off + k - j, ERR);
|
|
|
|
while (j < k)
|
|
|
|
rbuf[off++] = boln[j++];
|
|
|
|
} else {
|
|
|
|
CKBUF(rbuf, rbufsz, off + 1, ERR);
|
|
|
|
rbuf[off++] = *sub;
|
|
|
|
}
|
|
|
|
CKBUF(rbuf, rbufsz, off + 1, ERR);
|
|
|
|
rbuf[off] = '\0';
|
|
|
|
return off;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
/* doprint: print a range of lines to stdout */
|
|
|
|
doprint(from, to, gflag)
|
1993-04-08 05:07:16 +04:00
|
|
|
long from;
|
|
|
|
long to;
|
|
|
|
int gflag;
|
|
|
|
{
|
|
|
|
line_t *bp;
|
|
|
|
line_t *ep;
|
|
|
|
char *s;
|
|
|
|
|
1993-04-15 08:58:32 +04:00
|
|
|
if (!from) {
|
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
ep = getlp(nextln(to, lastln));
|
|
|
|
for (bp = getlp(from); bp != ep; bp = bp->next) {
|
|
|
|
if ((s = gettxt(bp)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
putstr(s, bp->len & ~ACTV, curln = from++, gflag);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
int cols = 72; /* wrap column: ws_col - 8 */
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
/* putstr: print text to stdout */
|
1993-04-08 05:07:16 +04:00
|
|
|
void
|
1993-05-08 14:49:52 +04:00
|
|
|
putstr(s, l, n, gflag)
|
1993-04-08 05:07:16 +04:00
|
|
|
char *s;
|
1993-05-08 14:49:52 +04:00
|
|
|
int l;
|
1993-04-08 05:07:16 +04:00
|
|
|
long n;
|
|
|
|
int gflag;
|
|
|
|
{
|
|
|
|
int col = 0;
|
|
|
|
|
|
|
|
if (gflag & GNP) {
|
|
|
|
printf("%ld\t", n);
|
|
|
|
col = 8;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
for (; l--; s++) {
|
1993-04-10 15:46:38 +04:00
|
|
|
if ((gflag & GLS) && ++col > cols) {
|
1993-04-08 05:07:16 +04:00
|
|
|
fputs("\\\n", stdout);
|
|
|
|
col = 1;
|
|
|
|
}
|
|
|
|
if (gflag & GLS) {
|
|
|
|
switch (*s) {
|
|
|
|
case '\b':
|
|
|
|
fputs("\\b", stdout);
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
fputs("\\f", stdout);
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
fputs("\\n", stdout);
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
fputs("\\r", stdout);
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
fputs("\\t", stdout);
|
|
|
|
break;
|
|
|
|
case '\v':
|
|
|
|
fputs("\\v", stdout);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (*s < 32 || 126 < *s) {
|
|
|
|
putchar('\\');
|
1993-05-08 14:49:52 +04:00
|
|
|
putchar((((unsigned char) *s & 0300) >> 6) + '0');
|
|
|
|
putchar((((unsigned char) *s & 070) >> 3) + '0');
|
|
|
|
putchar(((unsigned char) *s & 07) + '0');
|
1993-04-08 05:07:16 +04:00
|
|
|
col += 2;
|
|
|
|
} else if (*s == '\\')
|
|
|
|
fputs("\\\\", stdout);
|
|
|
|
else {
|
|
|
|
putchar(*s);
|
|
|
|
col--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
col++;
|
|
|
|
} else
|
|
|
|
putchar(*s);
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifndef BACKWARDS
|
1993-04-08 05:07:16 +04:00
|
|
|
if (gflag & GLS)
|
|
|
|
putchar('$');
|
1993-05-08 14:49:52 +04:00
|
|
|
#endif
|
1993-04-08 05:07:16 +04:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int newline_added; /* long record split across lines */
|
|
|
|
|
|
|
|
/* doread: read a text file into the editor buffer; return line count */
|
|
|
|
long
|
|
|
|
doread(n, fn)
|
|
|
|
long n;
|
|
|
|
char *fn;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
1993-05-08 14:49:52 +04:00
|
|
|
line_t *lp = getlp(n);
|
1993-04-08 05:07:16 +04:00
|
|
|
unsigned long size = 0;
|
|
|
|
undo_t *up = NULL;
|
|
|
|
int len;
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
isbinary = newline_added = 0;
|
|
|
|
if ((fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(esctos(fn), "r")) == NULL) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s: %s\n", fn, strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot open input file");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
} else if (des)
|
|
|
|
desinit();
|
|
|
|
for (curln = n; (len = sgetline(fp)) > 0; size += len) {
|
1993-04-08 05:07:16 +04:00
|
|
|
spl1();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (puttxt(sbuf) == NULL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
lp = lp->next;
|
1993-04-15 08:58:32 +04:00
|
|
|
if (up)
|
|
|
|
up->t = lp;
|
|
|
|
else if ((up = upush(UADD, curln, curln)) == NULL) {
|
|
|
|
spl0();
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s: %s\n", fn, strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot close input file");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
if (newline_added && !isbinary)
|
|
|
|
fputs("newline appended\n", stderr);
|
1993-04-08 05:07:16 +04:00
|
|
|
if (des) size += 8 - size % 8;
|
1993-04-23 06:08:48 +04:00
|
|
|
fprintf(stderr, !scripted ? "%lu\n" : "", size);
|
1993-05-08 14:49:52 +04:00
|
|
|
return (len < 0) ? ERR : curln - n;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* dowrite: write the text of a range of lines to a file; return line count */
|
|
|
|
long
|
|
|
|
dowrite(n, m, fn, mode)
|
|
|
|
long n;
|
|
|
|
long m;
|
|
|
|
char *fn;
|
|
|
|
char *mode;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
line_t *lp;
|
1993-05-08 14:49:52 +04:00
|
|
|
unsigned long size = 0;
|
1993-04-23 06:08:48 +04:00
|
|
|
long lc = n ? m - n + 1 : 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
char *s = NULL;
|
|
|
|
int len;
|
|
|
|
int ct;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((fp = ((*fn == '!') ? popen(fn + 1, "w") : fopen(esctos(fn), mode))) == NULL) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s: %s\n", fn, strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot open output file");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
} else if (des)
|
|
|
|
desinit();
|
1993-04-08 05:07:16 +04:00
|
|
|
if (n && !des)
|
1993-05-08 14:49:52 +04:00
|
|
|
for (lp = getlp(n); n <= m; n++, lp = lp->next) {
|
|
|
|
if ((s = gettxt(lp)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
len = lp->len & ~ACTV;
|
|
|
|
if (n != lastln || !isbinary || !newline_added)
|
|
|
|
s[len++] = '\n';
|
|
|
|
if ((ct = fwrite(s, sizeof(char), len, fp)) < 0 || ct != len) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s: %s\n", fn, strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "cannot write file");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
size += len;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
else if (n)
|
1993-05-08 14:49:52 +04:00
|
|
|
for (lp = getlp(n); n <= m; n++, lp = lp->next) {
|
|
|
|
if ((s = gettxt(lp)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
len = lp->len & ~ACTV;
|
|
|
|
while (len--) {
|
|
|
|
if (desputc(*s++, fp) == EOF && ferror(fp)) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s: %s\n", fn, strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "cannot write file");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
if (n != lastln || !isbinary || !newline_added) {
|
|
|
|
if (desputc('\n', fp) < 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", fn, strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot write file");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
size++; /* for '\n' */
|
|
|
|
}
|
|
|
|
size += (lp->len & ~ACTV);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
if (des) {
|
1993-05-08 14:49:52 +04:00
|
|
|
desflush(fp); /* flush buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
size += 8 - size % 8; /* adjust DES size */
|
|
|
|
}
|
1993-04-14 16:22:14 +04:00
|
|
|
if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", fn, strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot close output file");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-23 06:08:48 +04:00
|
|
|
fprintf(stderr, !scripted ? "%lu\n" : "", size);
|
|
|
|
return lc;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define USIZE 100 /* undo stack size */
|
|
|
|
undo_t *ustack = NULL; /* undo stack */
|
|
|
|
long usize = 0; /* stack size variable */
|
|
|
|
long u_p = 0; /* undo stack pointer */
|
|
|
|
|
|
|
|
|
1993-04-15 08:58:32 +04:00
|
|
|
/* upush: return pointer to intialized undo node */
|
1993-04-08 05:07:16 +04:00
|
|
|
undo_t *
|
1993-04-15 08:58:32 +04:00
|
|
|
upush(type, from, to)
|
1993-04-08 05:07:16 +04:00
|
|
|
int type;
|
|
|
|
long from;
|
|
|
|
long to;
|
|
|
|
{
|
|
|
|
undo_t *t;
|
|
|
|
|
|
|
|
#if defined(sun) || defined(NO_REALLOC_NULL)
|
|
|
|
if (ustack == NULL
|
1993-04-14 16:22:14 +04:00
|
|
|
&& (ustack = (undo_t *) malloc((usize = USIZE) * sizeof(undo_t))) == NULL) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "out of memory");
|
1993-04-08 05:07:16 +04:00
|
|
|
return NULL;
|
1993-04-14 16:22:14 +04:00
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif
|
|
|
|
t = ustack;
|
|
|
|
if (u_p < usize
|
|
|
|
|| (t = (undo_t *) realloc(ustack, (usize += USIZE) * sizeof(undo_t))) != NULL) {
|
|
|
|
ustack = t;
|
|
|
|
ustack[u_p].type = type;
|
1993-05-08 14:49:52 +04:00
|
|
|
ustack[u_p].t = getlp(to);
|
|
|
|
ustack[u_p].h = getlp(from);
|
1993-04-08 05:07:16 +04:00
|
|
|
return ustack + u_p++;
|
|
|
|
}
|
|
|
|
/* out of memory - release undo stack */
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "out of memory");
|
1993-04-08 05:07:16 +04:00
|
|
|
ureset();
|
|
|
|
free(ustack);
|
|
|
|
ustack = NULL;
|
|
|
|
usize = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* undo: undo last change to the editor buffer */
|
|
|
|
undo()
|
|
|
|
{
|
|
|
|
long n = usw ? 0 : u_p - 1;
|
|
|
|
int i = usw ? 1 : -1;
|
|
|
|
long j = u_p;
|
|
|
|
long ocurln = curln;
|
1993-04-16 12:38:44 +04:00
|
|
|
long olastln = lastln;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-04-16 12:38:44 +04:00
|
|
|
if (ucurln == -1 || ulastln == -1) {
|
1993-04-15 08:58:32 +04:00
|
|
|
sprintf(errmsg, "nothing to undo");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-15 08:58:32 +04:00
|
|
|
} else if (u_p)
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
|
|
|
getlp(0); /* this getlp last! */
|
1993-04-08 05:07:16 +04:00
|
|
|
spl1();
|
1993-04-15 08:58:32 +04:00
|
|
|
for (; j-- > 0; n += i)
|
1993-04-08 05:07:16 +04:00
|
|
|
switch(ustack[n].type ^ usw) {
|
|
|
|
case UADD:
|
|
|
|
requeue(ustack[n].h->prev, ustack[n].t->next);
|
|
|
|
break;
|
|
|
|
case UDEL:
|
|
|
|
requeue(ustack[n].h->prev, ustack[n].h);
|
|
|
|
requeue(ustack[n].t, ustack[n].t->next);
|
|
|
|
break;
|
1993-04-15 08:58:32 +04:00
|
|
|
case UMOV:
|
|
|
|
case VMOV:
|
|
|
|
requeue(ustack[n + i].h, ustack[n].h->next);
|
|
|
|
requeue(ustack[n].t->prev, ustack[n + i].t);
|
|
|
|
requeue(ustack[n].h, ustack[n].t);
|
|
|
|
n += i, j--;
|
|
|
|
break;
|
1993-04-08 05:07:16 +04:00
|
|
|
default:
|
|
|
|
/*NOTREACHED*/
|
|
|
|
;
|
|
|
|
}
|
|
|
|
usw = 1 - usw;
|
1993-04-16 12:38:44 +04:00
|
|
|
curln = ucurln, ucurln = ocurln;
|
|
|
|
lastln = ulastln, ulastln = olastln;
|
1993-04-08 05:07:16 +04:00
|
|
|
spl0();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ureset: clear the undo stack */
|
|
|
|
void
|
|
|
|
ureset()
|
|
|
|
{
|
1993-04-10 15:46:38 +04:00
|
|
|
line_t *lp, *ep, *tl;
|
1993-04-14 16:22:14 +04:00
|
|
|
int i;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
while (u_p--)
|
|
|
|
if ((ustack[u_p].type ^ usw) == UDEL) {
|
|
|
|
ep = ustack[u_p].t->next;
|
1993-04-10 15:46:38 +04:00
|
|
|
for (lp = ustack[u_p].h; lp != ep; lp = tl) {
|
1993-04-14 16:22:14 +04:00
|
|
|
if (markno)
|
|
|
|
for (i = 0; i < MAXMARK; i++)
|
|
|
|
if (mark[i] == lp) {
|
|
|
|
mark[i] = NULL;
|
|
|
|
markno--;
|
|
|
|
}
|
1993-04-10 15:46:38 +04:00
|
|
|
tl = lp->next;
|
1993-04-08 05:07:16 +04:00
|
|
|
free(lp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u_p = usw = 0;
|
|
|
|
ucurln = curln;
|
1993-04-16 12:38:44 +04:00
|
|
|
ulastln = lastln;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* sgetline: read a line of text up a maximum size from a file; return
|
|
|
|
line length */
|
1993-05-08 14:49:52 +04:00
|
|
|
sgetline(fp)
|
1993-04-08 05:07:16 +04:00
|
|
|
FILE *fp;
|
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
register int c;
|
|
|
|
register int i = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
while (((c = des ? desgetc(fp) : getc(fp)) != EOF || !feof(fp) && !ferror(fp)) && c != '\n') {
|
|
|
|
CKBUF(sbuf, sbufsz, i + 1, ERR);
|
|
|
|
if (!(sbuf[i++] = c)) isbinary = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(sbuf, sbufsz, i + 2, ERR);
|
|
|
|
if (c == '\n')
|
|
|
|
sbuf[i++] = c;
|
|
|
|
else if (feof(fp) && i) {
|
|
|
|
sbuf[i++] = '\n';
|
|
|
|
newline_added = 1;
|
|
|
|
} else if (ferror(fp)) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot read input file");
|
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
sbuf[i] = '\0';
|
|
|
|
return (isbinary && newline_added && i) ? --i : i;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* getline: read a line of text up a maximum size from stdin; return
|
|
|
|
line length */
|
1993-05-08 14:49:52 +04:00
|
|
|
getline()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
register int i = 0;
|
|
|
|
register int oi = 0;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
/* Read one character at a time to avoid i/o contention with shell
|
|
|
|
escapes invoked by nonterminal input, e.g.,
|
|
|
|
ed - <<EOF
|
|
|
|
!cat
|
|
|
|
hello, world
|
|
|
|
EOF */
|
|
|
|
for (;;)
|
|
|
|
switch (read(0, &c, 1)) {
|
|
|
|
default:
|
|
|
|
oi = 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(ibuf, ibufsz, i + 2, ERR);
|
|
|
|
if (!(ibuf[i++] = c)) isbinary = 1;
|
|
|
|
if (c != '\n')
|
|
|
|
continue;
|
|
|
|
lineno++; /* script line no. */
|
|
|
|
ibuf[i] = '\0';
|
|
|
|
ibufp = ibuf;
|
1993-04-08 05:07:16 +04:00
|
|
|
return i;
|
|
|
|
case 0:
|
|
|
|
if (i != oi) {
|
|
|
|
oi = i;
|
|
|
|
continue;
|
1993-05-08 14:49:52 +04:00
|
|
|
} else if (i)
|
|
|
|
ibuf[i] = '\0';
|
|
|
|
ibufp = ibuf;
|
1993-04-08 05:07:16 +04:00
|
|
|
return i;
|
1993-05-08 14:49:52 +04:00
|
|
|
case -1:
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot read standard input");
|
|
|
|
clearerr(stdin);
|
|
|
|
ibufp = NULL;
|
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
/* getcmdv: get a command vector */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *
|
1993-05-08 14:49:52 +04:00
|
|
|
getcmdv(sizep, nonl)
|
|
|
|
int *sizep;
|
|
|
|
int nonl;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-04-23 06:08:48 +04:00
|
|
|
int l, n;
|
1993-05-20 07:15:26 +04:00
|
|
|
char *t = ibufp;
|
1993-04-23 06:08:48 +04:00
|
|
|
|
1993-05-20 07:15:26 +04:00
|
|
|
while (*t++ != '\n')
|
|
|
|
;
|
|
|
|
if ((l = t - ibufp) < 2 || !oddesc(ibufp, ibufp + l - 1)) {
|
1993-05-08 14:49:52 +04:00
|
|
|
*sizep = l;
|
1993-04-23 06:08:48 +04:00
|
|
|
return ibufp;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
*sizep = -1;
|
|
|
|
CKBUF(cvbuf, cvbufsz, l, NULL);
|
|
|
|
memcpy(cvbuf, ibufp, l);
|
|
|
|
*(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
|
|
|
|
if (nonl) l--; /* strip newline */
|
1993-04-08 05:07:16 +04:00
|
|
|
for (;;) {
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((n = getline()) < 0)
|
1993-04-23 06:08:48 +04:00
|
|
|
return NULL;
|
1993-05-08 14:49:52 +04:00
|
|
|
else if (n == 0 || ibuf[n - 1] != '\n') {
|
|
|
|
sprintf(errmsg, "unexpected end-of-file");
|
1993-04-10 15:46:38 +04:00
|
|
|
return NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(cvbuf, cvbufsz, l + n, NULL);
|
|
|
|
memcpy(cvbuf + l, ibuf, n);
|
|
|
|
if (n < 2 || !oddesc(cvbuf, cvbuf + (l += n) - 1))
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
1993-05-08 14:49:52 +04:00
|
|
|
*(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
|
|
|
|
if (nonl) l--; /* strip newline */
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(cvbuf, cvbufsz, l + 1, NULL);
|
|
|
|
cvbuf[l] = '\0';
|
|
|
|
*sizep = l;
|
|
|
|
return cvbuf;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
/* lpdup: return a pointer to a copy of a line node */
|
1993-04-08 05:07:16 +04:00
|
|
|
line_t *
|
|
|
|
lpdup(lp)
|
|
|
|
line_t *lp;
|
|
|
|
{
|
|
|
|
line_t *np;
|
|
|
|
|
|
|
|
if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "out of memory");
|
1993-04-08 05:07:16 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
np->seek = lp->seek;
|
|
|
|
np->len = (lp->len & ~ACTV); /* zero ACTV bit */
|
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* oddesc: return the parity of escapes preceding a character in a
|
|
|
|
string */
|
|
|
|
oddesc(s, t)
|
|
|
|
char *s;
|
|
|
|
char *t;
|
|
|
|
{
|
|
|
|
return (s == t || *(t - 1) != '\\') ? 0 : !oddesc(s, t - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
/* esctos: return copy of escaped string */
|
1993-04-23 06:08:48 +04:00
|
|
|
char *
|
|
|
|
esctos(s)
|
|
|
|
char *s;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
static char *file = NULL;
|
|
|
|
static int filesz = 0;
|
1993-04-23 06:08:48 +04:00
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
CKBUF(file, filesz, MAXFNAME + 1, NULL);
|
|
|
|
/* assert: no trailing escape */
|
|
|
|
while (file[i++] = (*s == '\\') ? *++s : *s)
|
1993-04-23 06:08:48 +04:00
|
|
|
s++;
|
|
|
|
return file;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1993-04-10 15:46:38 +04:00
|
|
|
onhup(signo)
|
|
|
|
int signo;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
if (mutex)
|
1993-04-10 15:46:38 +04:00
|
|
|
sigflags |= (1 << signo);
|
|
|
|
else dohup(signo);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1993-04-10 15:46:38 +04:00
|
|
|
onintr(signo)
|
|
|
|
int signo;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
if (mutex)
|
1993-04-10 15:46:38 +04:00
|
|
|
sigflags |= (1 << signo);
|
|
|
|
else dointr(signo);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1993-04-10 15:46:38 +04:00
|
|
|
dohup(signo)
|
|
|
|
int signo;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
char *hup = NULL; /* hup filename */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *s;
|
|
|
|
int n;
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
if (!sigactive)
|
|
|
|
quit(1);
|
1993-04-10 15:46:38 +04:00
|
|
|
sigflags &= ~(1 << signo);
|
1993-05-08 14:49:52 +04:00
|
|
|
if (lastln && dowrite(1, lastln, "ed.hup", "w") < 0
|
1993-04-08 05:07:16 +04:00
|
|
|
&& (s = getenv("HOME")) != NULL
|
1993-05-08 14:49:52 +04:00
|
|
|
&& (n = strlen(s)) + 8 <= MAXFNAME /* "ed.hup" + '/' */
|
|
|
|
&& (hup = (char *) malloc(n + 10)) != NULL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
strcpy(hup, s);
|
|
|
|
if (hup[n - 1] != '/')
|
|
|
|
hup[n] = '/', hup[n+1] = '\0';
|
|
|
|
strcat(hup, "ed.hup");
|
|
|
|
dowrite(1, lastln, hup, "w");
|
|
|
|
}
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1993-04-10 15:46:38 +04:00
|
|
|
dointr(signo)
|
|
|
|
int signo;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
if (!sigactive)
|
|
|
|
quit(1);
|
1993-04-10 15:46:38 +04:00
|
|
|
sigflags &= ~(1 << signo);
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef _POSIX_SOURCE
|
1993-04-28 07:37:34 +04:00
|
|
|
siglongjmp(env, -1);
|
|
|
|
#else
|
1993-04-08 05:07:16 +04:00
|
|
|
longjmp(env, -1);
|
1993-04-28 07:37:34 +04:00
|
|
|
#endif
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-05-08 14:49:52 +04:00
|
|
|
struct winsize ws; /* window size structure */
|
|
|
|
|
1993-04-10 15:46:38 +04:00
|
|
|
void
|
|
|
|
dowinch(signo)
|
|
|
|
int signo;
|
|
|
|
{
|
|
|
|
sigflags &= ~(1 << signo);
|
|
|
|
if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
|
|
|
|
if (ws.ws_row > 2) rows = ws.ws_row - 2;
|
|
|
|
if (ws.ws_col > 8) cols = ws.ws_col - 8;
|
|
|
|
}
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
|
|
|
|
|
|
|
|
unsigned char ctab[256]; /* character translation table */
|
|
|
|
|
|
|
|
/* translit: translate characters in a string */
|
|
|
|
char *
|
|
|
|
translit(s, len, from, to)
|
|
|
|
char *s;
|
|
|
|
int len;
|
|
|
|
int from;
|
|
|
|
int to;
|
|
|
|
{
|
|
|
|
static int i = 0;
|
|
|
|
|
|
|
|
unsigned char *us;
|
|
|
|
|
|
|
|
ctab[i] = i; /* restore table to initial state */
|
|
|
|
ctab[i = from] = to;
|
|
|
|
for (us = (unsigned char *) s; len-- > 0; us++)
|
|
|
|
*us = ctab[*us];
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
line_t line0; /* initial node of line queue */
|
|
|
|
|
|
|
|
/* init_buf: open scratch buffer; initialize line queue */
|
|
|
|
void
|
|
|
|
init_buf()
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (sbopen() < 0)
|
|
|
|
quit(2);
|
|
|
|
requeue(&line0, &line0);
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
ctab[i] = i;
|
|
|
|
}
|