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. */
|
|
|
|
/*-
|
1993-11-12 13:48:43 +03:00
|
|
|
* Copyright (c) 1993 Andrew Moore, Talke Studio.
|
1993-04-08 05:07:16 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
1993-11-12 13:48:43 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
1993-04-08 05:07:16 +04:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1993-11-12 13:48:43 +03:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1993-04-08 05:07:16 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
char copyright1[] =
|
1993-11-12 13:48:43 +03:00
|
|
|
"@(#) Copyright (c) 1993 Andrew Moore, Talke Studio.\n\
|
1993-04-08 05:07:16 +04:00
|
|
|
All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1993-11-23 07:41:44 +03:00
|
|
|
/* static char sccsid[] = "@(#)ed.c 5.5 (Talke Studio) 3/28/93"; */
|
|
|
|
static char rcsid[] = "$Id: ed.c,v 1.19 1993/11/23 04:41:51 alm Exp $";
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
1993-04-23 06:08:48 +04:00
|
|
|
* CREDITS
|
1993-11-23 07:41:44 +03:00
|
|
|
*
|
|
|
|
* This program is based on the editor algorithm described in
|
|
|
|
* Brian W. Kernighan and P. J. Plauger's book "Software Tools
|
|
|
|
* in Pascal," Addison-Wesley, 1981.
|
|
|
|
*
|
1993-11-12 13:48:43 +03:00
|
|
|
* The buffering algorithm is attributed to Rodney Ruddock of
|
1993-04-23 06:08:48 +04:00
|
|
|
* 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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 */
|
1993-11-23 07:41:44 +03:00
|
|
|
char *lhbuf; /* lhs substitution buffer */
|
|
|
|
int lhbufsz; /* lhs substitution buffer size */
|
|
|
|
char *rhbuf; /* rhs substitution buffer */
|
|
|
|
int rhbufsz; /* rhs substitution buffer size */
|
|
|
|
int rhbufi; /* rhs substitution buffer index */
|
|
|
|
char *rbuf; /* substitute_matching_text buffer */
|
|
|
|
int rbufsz; /* substitute_matching_text buffer size */
|
1993-05-08 14:49:52 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
int isglobal; /* if set, doing a global command */
|
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 */
|
1993-05-27 05:27:46 +04:00
|
|
|
int red = 0; /* if set, restrict shell/directory access */
|
1993-05-08 14:49:52 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
char old_filename[MAXFNAME + 1] = ""; /* default filename */
|
|
|
|
long current_addr; /* current address */
|
|
|
|
long addr_last; /* last address */
|
1993-05-08 14:49:52 +04:00
|
|
|
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 */
|
1993-11-23 07:41:44 +03:00
|
|
|
int
|
1993-04-08 05:07:16 +04:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
int c, n;
|
|
|
|
long status = 0;
|
|
|
|
|
1993-05-27 05:27:46 +04:00
|
|
|
red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
|
1993-06-16 11:36:51 +04:00
|
|
|
top:
|
1993-04-08 05:07:16 +04:00
|
|
|
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
|
1993-11-23 07:41:44 +03:00
|
|
|
des = get_keyword();
|
1993-04-08 05:07:16 +04:00
|
|
|
#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-06-16 11:36:51 +04:00
|
|
|
if (argc > 1) {
|
|
|
|
optind = 1;
|
|
|
|
goto top;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
argv++;
|
1993-06-16 11:36:51 +04:00
|
|
|
argc--;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
/* assert: reliable signals! */
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef SIGWINCH
|
1993-11-23 07:41:44 +03:00
|
|
|
handle_winch(SIGWINCH);
|
|
|
|
if (isatty(0)) signal(SIGWINCH, handle_winch);
|
1993-05-08 14:49:52 +04:00
|
|
|
#endif
|
1993-11-23 07:41:44 +03:00
|
|
|
signal(SIGHUP, signal_hup);
|
1993-04-23 06:08:48 +04:00
|
|
|
signal(SIGQUIT, SIG_IGN);
|
1993-11-23 07:41:44 +03:00
|
|
|
signal(SIGINT, signal_int);
|
1993-05-08 14:49:52 +04:00
|
|
|
#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 {
|
1993-11-23 07:41:44 +03:00
|
|
|
init_buffers();
|
1993-05-08 14:49:52 +04:00
|
|
|
sigactive = 1; /* enable signal handlers */
|
1993-11-23 07:41:44 +03:00
|
|
|
if (argc && **argv && is_legal_filename(*argv)) {
|
|
|
|
if (read_file(0, *argv) < 0 && !isatty(0))
|
1993-05-08 14:49:52 +04:00
|
|
|
quit(2);
|
1993-05-27 05:27:46 +04:00
|
|
|
else if (**argv != '!')
|
1993-11-23 07:41:44 +03:00
|
|
|
strcpy(old_filename, *argv);
|
1993-05-27 05:27:46 +04:00
|
|
|
} else if (argc) {
|
|
|
|
fputs("?\n", stderr);
|
|
|
|
if (**argv == '\0')
|
|
|
|
sprintf(errmsg, "invalid filename");
|
|
|
|
if (!isatty(0))
|
|
|
|
quit(2);
|
1993-05-08 14:49:52 +04:00
|
|
|
}
|
|
|
|
}
|
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-11-23 07:41:44 +03:00
|
|
|
if ((n = get_input_line()) < 0) {
|
1993-05-08 14:49:52 +04:00
|
|
|
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-11-23 07:41:44 +03: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;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
isglobal = 0;
|
|
|
|
if ((status = extract_addr_range()) >= 0 &&
|
|
|
|
(status = exec_command()) >= 0)
|
|
|
|
if (!status || status &&
|
|
|
|
(status = display_lines(current_addr, current_addr,
|
|
|
|
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)) {
|
1993-11-23 07:41:44 +03: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
|
|
|
break;
|
1993-05-08 14:49:52 +04:00
|
|
|
case FATAL:
|
|
|
|
if (!isatty(0))
|
1993-11-23 07:41:44 +03:00
|
|
|
fprintf(stderr, garrulous ?
|
|
|
|
"script, line %d: %s\n" :
|
|
|
|
"", lineno, errmsg);
|
1993-05-08 14:49:52 +04:00
|
|
|
else
|
1993-11-23 07:41:44 +03:00
|
|
|
fprintf(stderr, garrulous ? "%s\n" :
|
|
|
|
"", errmsg);
|
1993-05-08 14:49:52 +04:00
|
|
|
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)) {
|
1993-11-23 07:41:44 +03:00
|
|
|
fprintf(stderr, garrulous ?
|
|
|
|
"script, line %d: %s\n" :
|
|
|
|
"", lineno, errmsg);
|
1993-04-10 15:46:38 +04:00
|
|
|
quit(2);
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
long first_addr, second_addr, addr_cnt;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* extract_addr_range: get line addresses from the command buffer until an
|
|
|
|
illegal address is seen; return status */
|
|
|
|
int
|
|
|
|
extract_addr_range()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-11-12 13:48:43 +03:00
|
|
|
long addr;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
addr_cnt = 0;
|
|
|
|
first_addr = second_addr = current_addr;
|
|
|
|
while ((addr = next_addr()) >= 0) {
|
|
|
|
addr_cnt++;
|
|
|
|
first_addr = second_addr;
|
|
|
|
second_addr = addr;
|
1993-04-08 05:07:16 +04:00
|
|
|
if (*ibufp != ',' && *ibufp != ';')
|
|
|
|
break;
|
1993-04-10 15:46:38 +04:00
|
|
|
else if (*ibufp++ == ';')
|
1993-11-23 07:41:44 +03:00
|
|
|
current_addr = addr;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
|
|
|
|
first_addr = second_addr;
|
1993-11-12 13:48:43 +03:00
|
|
|
return (addr == ERR) ? ERR : 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
|
|
|
|
#define MUST_BE_FIRST() \
|
1993-11-12 13:48:43 +03:00
|
|
|
if (!first) { sprintf(errmsg, "invalid address"); return ERR; }
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* next_addr: return the next line address in the command buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
long
|
1993-11-23 07:41:44 +03:00
|
|
|
next_addr()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-11-12 13:48:43 +03:00
|
|
|
char *hd;
|
1993-11-23 07:41:44 +03:00
|
|
|
long addr = current_addr;
|
1993-11-12 13:48:43 +03:00
|
|
|
int first = 1;
|
1993-11-23 07:41:44 +03:00
|
|
|
int c, n;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
SKIPBLANKS();
|
1993-11-12 13:48:43 +03:00
|
|
|
for (hd = ibufp;; first = 0)
|
|
|
|
switch (c = *ibufp) {
|
|
|
|
case '+':
|
|
|
|
case '\t':
|
|
|
|
case ' ':
|
|
|
|
case '-':
|
|
|
|
case '^':
|
1993-04-08 05:07:16 +04:00
|
|
|
ibufp++;
|
1993-11-23 07:41:44 +03:00
|
|
|
SKIPBLANKS();
|
|
|
|
n = (c == '-' || c == '^') ? -1 : 1;
|
1993-11-12 13:48:43 +03:00
|
|
|
if (isdigit(*ibufp))
|
1993-11-23 07:41:44 +03:00
|
|
|
addr += n * strtol(ibufp, &ibufp, 10);
|
|
|
|
else if (!isspace(c))
|
|
|
|
addr += n;
|
1993-11-12 13:48:43 +03:00
|
|
|
break;
|
|
|
|
case '0': case '1': case '2':
|
|
|
|
case '3': case '4': case '5':
|
|
|
|
case '6': case '7': case '8': case '9':
|
1993-11-23 07:41:44 +03:00
|
|
|
MUST_BE_FIRST();
|
1993-11-12 13:48:43 +03:00
|
|
|
addr = strtol(ibufp, &ibufp, 10);
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
case '$':
|
1993-11-23 07:41:44 +03:00
|
|
|
MUST_BE_FIRST();
|
1993-04-08 05:07:16 +04:00
|
|
|
ibufp++;
|
1993-11-23 07:41:44 +03:00
|
|
|
addr = (c == '.') ? current_addr : addr_last;
|
1993-11-12 13:48:43 +03:00
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
case '?':
|
1993-11-23 07:41:44 +03:00
|
|
|
MUST_BE_FIRST();
|
|
|
|
if ((addr = get_matching_node_addr(
|
|
|
|
get_compiled_pattern(), c == '/')) < 0)
|
1993-11-12 13:48:43 +03:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if (c == *ibufp)
|
1993-11-12 13:48:43 +03:00
|
|
|
ibufp++;
|
|
|
|
break;
|
|
|
|
case '\'':
|
1993-11-23 07:41:44 +03:00
|
|
|
MUST_BE_FIRST();
|
1993-11-12 13:48:43 +03:00
|
|
|
ibufp++;
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((addr = get_marked_node_addr(*ibufp++)) < 0)
|
1993-11-12 13:48:43 +03:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
case ',':
|
|
|
|
case ';':
|
|
|
|
if (first) {
|
|
|
|
ibufp++;
|
1993-11-23 07:41:44 +03:00
|
|
|
addr_cnt++;
|
|
|
|
second_addr = (c == ';') ? current_addr : 1;
|
|
|
|
addr = addr_last;
|
1993-11-12 13:48:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALL THROUGH */
|
|
|
|
default:
|
|
|
|
if (ibufp == hd)
|
|
|
|
return EOF;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if (addr < 0 || addr_last < addr) {
|
1993-11-12 13:48:43 +03:00
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
|
|
|
} else
|
|
|
|
return addr;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-11-12 13:48:43 +03:00
|
|
|
/* NOTREACHED */
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
|
|
|
|
#ifdef BACKWARDS
|
|
|
|
/* GET_THIRD_ADDR: get a legal address from the command buffer */
|
|
|
|
#define GET_THIRD_ADDR(addr) \
|
|
|
|
{ \
|
|
|
|
long ol1, ol2; \
|
|
|
|
\
|
|
|
|
ol1 = first_addr, ol2 = second_addr; \
|
|
|
|
if (extract_addr_range() < 0) \
|
|
|
|
return ERR; \
|
|
|
|
else if (addr_cnt == 0) { \
|
|
|
|
sprintf(errmsg, "destination expected"); \
|
|
|
|
return ERR; \
|
|
|
|
} else if (second_addr < 0 || addr_last < second_addr) { \
|
|
|
|
sprintf(errmsg, "invalid address"); \
|
|
|
|
return ERR; \
|
|
|
|
} \
|
|
|
|
addr = second_addr; \
|
|
|
|
first_addr = ol1, second_addr = ol2; \
|
|
|
|
}
|
|
|
|
#else /* BACKWARDS */
|
|
|
|
/* GET_THIRD_ADDR: get a legal address from the command buffer */
|
|
|
|
#define GET_THIRD_ADDR(addr) \
|
|
|
|
{ \
|
|
|
|
long ol1, ol2; \
|
|
|
|
\
|
|
|
|
ol1 = first_addr, ol2 = second_addr; \
|
|
|
|
if (extract_addr_range() < 0) \
|
|
|
|
return ERR; \
|
|
|
|
if (second_addr < 0 || addr_last < second_addr) { \
|
|
|
|
sprintf(errmsg, "invalid address"); \
|
|
|
|
return ERR; \
|
|
|
|
} \
|
|
|
|
addr = second_addr; \
|
|
|
|
first_addr = ol1, second_addr = ol2; \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
|
|
|
|
#define GET_COMMAND_SUFFIX() { \
|
1993-04-08 05:07:16 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
/* sflags */
|
1993-04-08 05:07:16 +04:00
|
|
|
#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-11-23 07:41:44 +03:00
|
|
|
long u_current_addr = -1; /* if >= 0, undo enabled */
|
|
|
|
long u_addr_last = -1; /* if >= 0, undo enabled */
|
|
|
|
int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
long rows = 22; /* scroll length: ws_row - 2 */
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* exec_command: execute the next command in command buffer; return print
|
1993-04-08 05:07:16 +04:00
|
|
|
request, if any */
|
1993-11-23 07:41:44 +03:00
|
|
|
int
|
|
|
|
exec_command()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
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;
|
1993-11-23 07:41:44 +03:00
|
|
|
long addr = 0;
|
1993-04-23 06:08:48 +04:00
|
|
|
int n = 0;
|
|
|
|
int c;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
SKIPBLANKS();
|
1993-04-23 06:08:48 +04:00
|
|
|
switch(c = *ibufp++) {
|
1993-04-08 05:07:16 +04:00
|
|
|
case 'a':
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if (append_lines(second_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case 'c':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(current_addr, current_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if (delete_lines(first_addr, second_addr) < 0 ||
|
|
|
|
append_lines(current_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case 'd':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(current_addr, current_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if (delete_lines(first_addr, second_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
|
|
|
|
current_addr = addr;
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'e':
|
1993-06-16 11:36:51 +04:00
|
|
|
if (modified && !scripted)
|
1993-04-08 05:07:16 +04:00
|
|
|
return EMOD;
|
|
|
|
/* fall through */
|
|
|
|
case 'E':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt > 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
} else if (!isspace(*ibufp)) {
|
|
|
|
sprintf(errmsg, "unexpected command suffix");
|
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
} else if ((fnp = get_filename()) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (delete_lines(1, addr_last) < 0)
|
1993-07-02 14:02:26 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
clear_undo_stack();
|
|
|
|
if (close_sbuf() < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if (open_sbuf() < 0)
|
1993-05-08 14:49:52 +04:00
|
|
|
return FATAL;
|
1993-11-23 07:41:44 +03:00
|
|
|
if (*fnp && *fnp != '!') strcpy(old_filename, fnp);
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef BACKWARDS
|
1993-11-23 07:41:44 +03:00
|
|
|
if (*fnp == '\0' && *old_filename == '\0') {
|
1993-05-08 14:49:52 +04:00
|
|
|
sprintf(errmsg, "no current filename");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
#endif
|
1993-11-23 07:41:44 +03:00
|
|
|
if (read_file(0, *fnp ? fnp : old_filename) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
clear_undo_stack();
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 0;
|
1993-11-23 07:41:44 +03:00
|
|
|
u_current_addr = u_addr_last = -1;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'f':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt > 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
} else if (!isspace(*ibufp)) {
|
|
|
|
sprintf(errmsg, "unexpected command suffix");
|
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
} else if ((fnp = get_filename()) == 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-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (*fnp) strcpy(old_filename, fnp);
|
|
|
|
printf("%s\n", strip_escapes(old_filename));
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
1993-04-23 06:08:48 +04:00
|
|
|
case 'g':
|
1993-11-23 07:41:44 +03:00
|
|
|
case 'v':
|
1993-04-23 06:08:48 +04:00
|
|
|
case 'G':
|
1993-11-23 07:41:44 +03:00
|
|
|
case 'V':
|
|
|
|
if (isglobal) {
|
|
|
|
sprintf(errmsg, "cannot nest global commands");
|
|
|
|
return ERR;
|
|
|
|
} else if (check_addr_range(1, addr_last) < 0)
|
|
|
|
return ERR;
|
|
|
|
else if (build_active_list(c == 'g' || c == 'G') < 0)
|
|
|
|
return ERR;
|
|
|
|
else if (n = (c == 'G' || c == 'V'))
|
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
isglobal++;
|
|
|
|
if (exec_global(n, gflag) < 0)
|
|
|
|
return ERR;
|
|
|
|
break;
|
1993-04-23 06:08:48 +04:00
|
|
|
case 'h':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt > 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
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-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt > 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
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-11-23 07:41:44 +03:00
|
|
|
if (second_addr == 0) {
|
1993-04-14 16:22:14 +04:00
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if (append_lines(second_addr - 1) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case 'j':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(current_addr, current_addr + 1) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if (first_addr != second_addr &&
|
|
|
|
join_lines(first_addr, second_addr) < 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-11-23 07:41:44 +03:00
|
|
|
if (second_addr == 0) {
|
1993-04-14 16:22:14 +04:00
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
1993-11-12 13:48:43 +03:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
|
1993-07-02 14:02:26 +04:00
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'l':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(current_addr, current_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
gflag = 0;
|
|
|
|
break;
|
|
|
|
case 'm':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(current_addr, current_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_THIRD_ADDR(addr);
|
|
|
|
if (first_addr <= addr && addr < second_addr) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "invalid destination");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if (move_lines(addr) < 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-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(current_addr, current_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
gflag = 0;
|
|
|
|
break;
|
|
|
|
case 'p':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(current_addr, current_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (display_lines(first_addr, second_addr, 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-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt > 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
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':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt > 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
1993-06-16 11:36:51 +04:00
|
|
|
gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
|
1993-04-23 06:08:48 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
} else if (addr_cnt == 0)
|
|
|
|
second_addr = addr_last;
|
|
|
|
if ((fnp = get_filename()) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if (*old_filename == '\0' && *fnp != '!')
|
|
|
|
strcpy(old_filename, fnp);
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef BACKWARDS
|
1993-11-23 07:41:44 +03:00
|
|
|
if (*fnp == '\0' && *old_filename == '\0') {
|
1993-05-08 14:49:52 +04:00
|
|
|
sprintf(errmsg, "no current filename");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
#endif
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((addr = read_file(second_addr, *fnp ? fnp : old_filename)) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if (addr && addr != addr_last)
|
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-08-30 06:20:18 +04:00
|
|
|
if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
|
|
|
|
sprintf(errmsg, "invalid pattern delimiter");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-23 06:08:48 +04:00
|
|
|
tpat = pat;
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL1();
|
|
|
|
if ((!sflags || (sflags & SGR)) &&
|
|
|
|
(tpat = get_compiled_pattern()) == NULL) {
|
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
} else if (tpat != pat) {
|
1993-04-23 06:08:48 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
SPL0();
|
|
|
|
if (!sflags && (sgflag = extract_subst_tail()) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if (isglobal)
|
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-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(current_addr, current_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if ((n = search_and_replace(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-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(current_addr, current_addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_THIRD_ADDR(addr);
|
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if (copy_lines(addr) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'u':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt > 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (pop_undo_stack() < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
} else if ((fnp = get_filename()) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt == 0 && !addr_last)
|
|
|
|
first_addr = second_addr = 0;
|
|
|
|
else if (check_addr_range(1, addr_last) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (*old_filename == '\0' && *fnp != '!')
|
|
|
|
strcpy(old_filename, fnp);
|
1993-05-08 14:49:52 +04:00
|
|
|
#ifdef BACKWARDS
|
1993-11-23 07:41:44 +03:00
|
|
|
if (*fnp == '\0' && *old_filename == '\0') {
|
1993-05-08 14:49:52 +04:00
|
|
|
sprintf(errmsg, "no current filename");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
#endif
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((addr = write_file(first_addr, second_addr,
|
|
|
|
*fnp ? fnp : old_filename, (c == 'W') ? "a" : "w")) < 0)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if (addr == addr_last)
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 0;
|
1993-06-16 11:36:51 +04:00
|
|
|
else if (modified && !scripted && n == 'q')
|
1993-04-23 06:08:48 +04:00
|
|
|
gflag = EMOD;
|
1993-04-08 05:07:16 +04:00
|
|
|
break;
|
|
|
|
case 'x':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt > 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
1993-04-08 05:07:16 +04:00
|
|
|
#ifdef DES
|
1993-11-23 07:41:44 +03:00
|
|
|
des = get_keyword();
|
1993-04-08 05:07:16 +04:00
|
|
|
#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-06-16 11:36:51 +04:00
|
|
|
#ifdef BACKWARDS
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
|
1993-06-16 11:36:51 +04:00
|
|
|
#else
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
|
1993-06-16 11:36:51 +04:00
|
|
|
#endif
|
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-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
if (display_lines(second_addr, min(addr_last,
|
|
|
|
second_addr + 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 '=':
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
|
|
|
printf("%d\n", addr_cnt ? second_addr : addr_last);
|
1993-05-08 14:49:52 +04:00
|
|
|
break;
|
|
|
|
case '!':
|
1993-11-23 07:41:44 +03:00
|
|
|
#ifndef EX_BANG
|
|
|
|
if (addr_cnt > 0) {
|
1993-05-08 14:49:52 +04:00
|
|
|
sprintf(errmsg, "unexpected address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
#endif
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((sflags = get_shell_command()) < 0)
|
1993-05-08 14:49:52 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
GET_COMMAND_SUFFIX();
|
1993-05-08 14:49:52 +04:00
|
|
|
if (sflags) printf("%s\n", shcmd + 1);
|
1993-11-23 07:41:44 +03:00
|
|
|
#ifdef EX_BANG
|
|
|
|
if (addr_cnt == 0) {
|
1993-05-08 14:49:52 +04:00
|
|
|
#endif
|
|
|
|
system(shcmd + 1);
|
|
|
|
if (!scripted) printf("!\n");
|
|
|
|
break;
|
1993-11-23 07:41:44 +03:00
|
|
|
#ifdef EX_BANG
|
1993-05-08 14:49:52 +04:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
if (!addr_last && !first_addr && !second_addr) {
|
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
} else if (check_addr_range(current_addr, current_addr) < 0)
|
1993-05-08 14:49:52 +04:00
|
|
|
return ERR;
|
|
|
|
else {
|
1993-11-23 07:41:44 +03:00
|
|
|
if (!isglobal) clear_undo_stack();
|
|
|
|
if (delete_lines(first_addr, second_addr) < 0)
|
1993-05-08 14:49:52 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
second_addr = current_addr;
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((addr = read_file(second_addr, shcmd)) < 0)
|
1993-05-08 14:49:52 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if (addr && addr != addr_last)
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case '\n':
|
1993-06-16 11:36:51 +04:00
|
|
|
#ifdef BACKWARDS
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(first_addr = 1, current_addr + 1) < 0
|
1993-06-16 11:36:51 +04:00
|
|
|
#else
|
1993-11-23 07:41:44 +03:00
|
|
|
if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
|
1993-06-16 11:36:51 +04:00
|
|
|
#endif
|
1993-11-23 07:41:44 +03:00
|
|
|
|| display_lines(second_addr, second_addr, 0) < 0)
|
1993-05-08 14:49:52 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
/* check_addr_range: return status of address range check */
|
|
|
|
int
|
|
|
|
check_addr_range(n, m)
|
|
|
|
long n, m;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-11-23 07:41:44 +03:00
|
|
|
if (addr_cnt == 0) {
|
|
|
|
first_addr = n;
|
|
|
|
second_addr = m;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
if (first_addr > second_addr || 1 > first_addr ||
|
|
|
|
second_addr > addr_last) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* build_active_list: add line matching a pattern to the global-active list */
|
|
|
|
int
|
|
|
|
build_active_list(isgcmd)
|
|
|
|
int isgcmd;
|
|
|
|
{
|
|
|
|
pattern_t *pat;
|
|
|
|
line_t *lp;
|
|
|
|
long n;
|
|
|
|
char *s;
|
|
|
|
char delimiter;
|
|
|
|
|
|
|
|
if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
|
|
|
|
sprintf(errmsg, "invalid pattern delimiter");
|
|
|
|
return ERR;
|
|
|
|
} else if ((pat = get_compiled_pattern()) == NULL)
|
|
|
|
return ERR;
|
|
|
|
else if (*ibufp == delimiter)
|
|
|
|
ibufp++;
|
|
|
|
clear_active_list();
|
|
|
|
lp = get_addressed_line_node(1);
|
|
|
|
for (n = 1; n <= addr_last; n++, lp = lp->next) {
|
|
|
|
if ((s = get_sbuf_line(lp)) == NULL)
|
|
|
|
return ERR;
|
|
|
|
if (isbinary)
|
|
|
|
s = NUL_TO_NEWLINE(s, lp->len);
|
|
|
|
if (first_addr <= n && n <= second_addr &&
|
|
|
|
!regexec(pat, s, 0, NULL, 0) == isgcmd)
|
|
|
|
if (set_active_node(lp) < 0)
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* exec_global: apply command list in the command buffer to the active
|
|
|
|
lines in a range; return command status */
|
1993-04-08 05:07:16 +04:00
|
|
|
long
|
1993-11-23 07:41:44 +03:00
|
|
|
exec_global(interact, gflag)
|
|
|
|
int interact;
|
|
|
|
int gflag;
|
|
|
|
{
|
|
|
|
static char *ocmd = NULL;
|
|
|
|
static int ocmdsz = 0;
|
|
|
|
|
|
|
|
line_t *lp = NULL;
|
|
|
|
int status;
|
|
|
|
int n;
|
|
|
|
char *cmd = NULL;
|
|
|
|
|
|
|
|
#ifdef BACKWARDS
|
|
|
|
if (!interact)
|
|
|
|
if (!strcmp(ibufp, "\n"))
|
|
|
|
cmd = "p\n"; /* null cmd-list == `p' */
|
|
|
|
else if ((cmd = get_extended_line(&n, 0)) == NULL)
|
|
|
|
return ERR;
|
|
|
|
#else
|
|
|
|
if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
|
|
|
|
return ERR;
|
|
|
|
#endif
|
|
|
|
clear_undo_stack();
|
|
|
|
while ((lp = next_active_node()) != NULL) {
|
|
|
|
if ((current_addr = get_line_node_addr(lp)) < 0)
|
|
|
|
return ERR;
|
|
|
|
if (interact) {
|
|
|
|
/* print current_addr; get a command in global syntax */
|
|
|
|
if (display_lines(current_addr, current_addr, gflag) < 0)
|
|
|
|
return ERR;
|
|
|
|
while ((n = get_input_line()) > 0 &&
|
|
|
|
ibuf[n - 1] != '\n')
|
|
|
|
clearerr(stdin);
|
|
|
|
if (n < 0)
|
|
|
|
return ERR;
|
|
|
|
else if (n == 0) {
|
|
|
|
sprintf(errmsg, "unexpected end-of-file");
|
|
|
|
return ERR;
|
|
|
|
} else if (n == 1 && !strcmp(ibuf, "\n"))
|
|
|
|
continue;
|
|
|
|
else if (n == 2 && !strcmp(ibuf, "&\n")) {
|
|
|
|
if (cmd == NULL) {
|
|
|
|
sprintf(errmsg, "no previous command");
|
|
|
|
return ERR;
|
|
|
|
} else cmd = ocmd;
|
|
|
|
} else if ((cmd = get_extended_line(&n, 0)) == NULL)
|
|
|
|
return ERR;
|
|
|
|
else {
|
|
|
|
CKBUF(ocmd, ocmdsz, n + 1, ERR);
|
|
|
|
memcpy(ocmd, cmd, n + 1);
|
|
|
|
cmd = ocmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ibufp = cmd;
|
|
|
|
for (; *ibufp;)
|
|
|
|
if ((status = extract_addr_range()) < 0 ||
|
|
|
|
(status = exec_command()) < 0 ||
|
|
|
|
status > 0 && (status = display_lines(
|
|
|
|
current_addr, current_addr, status)) < 0)
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get_matching_node_addr: return the address of the next line matching a
|
|
|
|
pattern in a given direction. wrap around begin/end of line queue if
|
|
|
|
necessary */
|
|
|
|
long
|
|
|
|
get_matching_node_addr(pat, dir)
|
1993-04-08 05:07:16 +04:00
|
|
|
pattern_t *pat;
|
|
|
|
int dir;
|
|
|
|
{
|
|
|
|
char *s;
|
1993-11-23 07:41:44 +03:00
|
|
|
long n = current_addr;
|
1993-05-08 14:49:52 +04:00
|
|
|
line_t *lp;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-12 13:48:43 +03:00
|
|
|
if (!pat) return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
do {
|
1993-11-23 07:41:44 +03:00
|
|
|
if (n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last)) {
|
|
|
|
lp = get_addressed_line_node(n);
|
|
|
|
if ((s = get_sbuf_line(lp)) == NULL)
|
1993-04-14 16:22:14 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
if (isbinary)
|
1993-11-23 07:41:44 +03:00
|
|
|
s = NUL_TO_NEWLINE(s, lp->len);
|
1993-05-08 14:49:52 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
} while (n != current_addr);
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "no match");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* get_filename: return pointer to copy of filename in the command buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *
|
1993-11-23 07:41:44 +03:00
|
|
|
get_filename()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
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') {
|
1993-11-23 07:41:44 +03:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
} else if ((ibufp = get_extended_line(&n, 1)) == NULL)
|
1993-04-23 06:08:48 +04:00
|
|
|
return NULL;
|
1993-11-23 07:41:44 +03:00
|
|
|
#ifdef EX_BANG
|
1993-05-08 14:49:52 +04:00
|
|
|
else if (*ibufp == '!') {
|
|
|
|
ibufp++;
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((n = get_shell_command()) < 0)
|
1993-05-08 14:49:52 +04:00
|
|
|
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
|
1993-11-23 07:41:44 +03:00
|
|
|
else if (*old_filename == '\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-11-23 07:41:44 +03:00
|
|
|
return is_legal_filename(file) ? file : NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
1993-05-27 05:27:46 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* extract_subst_tail: extract substitution tail from the command buffer */
|
|
|
|
int
|
|
|
|
extract_subst_tail()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-11-23 07:41:44 +03:00
|
|
|
char delimiter;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((delimiter = *ibufp) == '\n') {
|
1993-05-08 14:49:52 +04:00
|
|
|
rhbufi = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
return GPR;
|
1993-11-23 07:41:44 +03:00
|
|
|
} else if (extract_subst_template() == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
else if (*ibufp == '\n')
|
|
|
|
return GPR;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if (*ibufp == delimiter)
|
1993-04-08 05:07:16 +04:00
|
|
|
ibufp++;
|
|
|
|
if ('1' <= *ibufp && *ibufp <= '9')
|
|
|
|
return (int) strtol(ibufp, &ibufp, 10) << 8;
|
|
|
|
else if (*ibufp == 'g') {
|
|
|
|
ibufp++;
|
|
|
|
return GSG;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* extract_subst_template: return pointer to copy of substitution template
|
|
|
|
in the command buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *
|
1993-11-23 07:41:44 +03:00
|
|
|
extract_subst_template()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
int n = 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
int i = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
char c;
|
1993-11-23 07:41:44 +03:00
|
|
|
char delimiter = *ibufp++;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
if (*ibufp == '%' && *(ibufp + 1) == delimiter) {
|
1993-04-08 05:07:16 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
while (*ibufp != delimiter) {
|
1993-05-08 14:49:52 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
else if (!isglobal) {
|
|
|
|
while ((n = get_input_line()) == 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-11-23 07:41:44 +03:00
|
|
|
/* get_shell_command: read a shell command from stdin; return substitution
|
|
|
|
status */
|
1993-04-23 06:08:48 +04:00
|
|
|
int
|
1993-11-23 07:41:44 +03:00
|
|
|
get_shell_command()
|
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-05-27 05:27:46 +04:00
|
|
|
char *s; /* substitution char pointer */
|
1993-05-08 14:49:52 +04:00
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
1993-04-23 06:08:48 +04:00
|
|
|
|
1993-05-27 05:27:46 +04:00
|
|
|
if (red) {
|
|
|
|
sprintf(errmsg, "shell access restricted");
|
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
} else if ((s = ibufp = get_extended_line(&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-27 05:27:46 +04:00
|
|
|
if (s != ibufp) {
|
1993-05-08 14:49:52 +04:00
|
|
|
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);
|
1993-05-27 05:27:46 +04:00
|
|
|
for (s = shcmd + 1; s < shcmd + shcmdi;)
|
|
|
|
buf[i++] = *s++;
|
|
|
|
s = ibufp++;
|
1993-04-23 06:08:48 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '%':
|
1993-11-23 07:41:44 +03:00
|
|
|
if (*old_filename == '\0') {
|
1993-04-23 06:08:48 +04:00
|
|
|
sprintf(errmsg, "no current filename");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
j = strlen(s = strip_escapes(old_filename));
|
1993-05-08 14:49:52 +04:00
|
|
|
CKBUF(buf, n, i + j, ERR);
|
1993-05-27 05:27:46 +04:00
|
|
|
while (j--)
|
|
|
|
buf[i++] = *s++;
|
|
|
|
s = 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-05-27 05:27:46 +04:00
|
|
|
return *s == '!' || *s == '%';
|
1993-04-23 06:08:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* append_lines: insert text from stdin to after line n; stop when either a
|
1993-04-08 05:07:16 +04:00
|
|
|
single period is read or EOF; return status */
|
1993-11-23 07:41:44 +03:00
|
|
|
int
|
|
|
|
append_lines(n)
|
1993-04-08 05:07:16 +04:00
|
|
|
long n;
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
for (current_addr = n;;) {
|
|
|
|
if (!isglobal) {
|
|
|
|
if ((l = get_input_line()) < 0)
|
1993-05-08 14:49:52 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
SPL1();
|
1993-05-12 12:22:03 +04:00
|
|
|
do {
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((lp = put_sbuf_line(lp)) == NULL) {
|
|
|
|
SPL0();
|
1993-05-12 12:22:03 +04:00
|
|
|
return ERR;
|
|
|
|
} else if (up)
|
1993-11-23 07:41:44 +03:00
|
|
|
up->t = get_addressed_line_node(current_addr);
|
|
|
|
else if ((up = push_undo_stack(UADD, current_addr,
|
|
|
|
current_addr)) == NULL) {
|
|
|
|
SPL0();
|
1993-05-12 12:22:03 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
} while (lp != eot);
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
modified = 1;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
/* NOTREACHED */
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
1993-05-12 12:22:03 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* search_and_replace: for each line in a range, change text matching a pattern
|
|
|
|
according to a substitution template; return status */
|
|
|
|
int
|
|
|
|
search_and_replace(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
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
current_addr = first_addr - 1;
|
|
|
|
for (lc = 0; lc <= second_addr - first_addr; lc++) {
|
|
|
|
lp = get_addressed_line_node(++current_addr);
|
|
|
|
if ((len = substitute_matching_text(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-11-23 07:41:44 +03:00
|
|
|
if (delete_lines(current_addr, current_addr) < 0)
|
1993-07-02 14:02:26 +04:00
|
|
|
return ERR;
|
1993-05-08 14:49:52 +04:00
|
|
|
txt = rbuf;
|
|
|
|
eot = rbuf + len;
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL1();
|
1993-04-08 05:07:16 +04:00
|
|
|
do {
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((txt = put_sbuf_line(txt)) == NULL) {
|
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-04-15 08:58:32 +04:00
|
|
|
} else if (up)
|
1993-11-23 07:41:44 +03:00
|
|
|
up->t = get_addressed_line_node(current_addr);
|
|
|
|
else if ((up = push_undo_stack(UADD,
|
|
|
|
current_addr, current_addr)) == NULL) {
|
|
|
|
SPL0();
|
1993-04-15 08:58:32 +04:00
|
|
|
return ERR;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
} while (txt != eot);
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
nsubs++;
|
|
|
|
}
|
|
|
|
}
|
1993-04-10 15:46:38 +04:00
|
|
|
if (nsubs == 0 && !(gflag & GLB)) {
|
|
|
|
sprintf(errmsg, "no match");
|
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
} else if ((gflag & (GPR | GLS | GNP)) &&
|
|
|
|
display_lines(current_addr, current_addr, 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
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* substitute_matching_text: replace text matched by a pattern according to
|
|
|
|
a substitution template; return pointer to the modified text */
|
|
|
|
int
|
|
|
|
substitute_matching_text(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 */
|
1993-11-23 07:41:44 +03:00
|
|
|
int changed = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
int matchno = 0;
|
1993-05-08 14:49:52 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
if ((txt = get_sbuf_line(lp)) == NULL)
|
1993-05-08 14:49:52 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
if (isbinary) txt = NUL_TO_NEWLINE(txt, lp->len);
|
1993-11-12 13:48:43 +03:00
|
|
|
eot = txt + lp->len;
|
1993-04-08 05:07:16 +04:00
|
|
|
if (!regexec(pat, txt, SE_MAX, rm, 0)) {
|
|
|
|
do {
|
|
|
|
if (!kth || kth == ++matchno) {
|
1993-11-23 07:41:44 +03:00
|
|
|
changed++;
|
1993-05-08 14:49:52 +04:00
|
|
|
i = rm[0].rm_so;
|
|
|
|
CKBUF(rbuf, rbufsz, off + i, ERR);
|
1993-11-23 07:41:44 +03:00
|
|
|
if (isbinary)
|
|
|
|
txt = NEWLINE_TO_NUL(txt, rm[0].rm_eo);
|
1993-05-08 14:49:52 +04:00
|
|
|
memcpy(rbuf + off, txt, i);
|
1993-11-23 07:41:44 +03:00
|
|
|
off += i;
|
|
|
|
if ((off = apply_subst_template(txt, rm, off,
|
|
|
|
pat->re_nsub)) < 0)
|
1993-05-08 14:49:52 +04:00
|
|
|
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);
|
1993-11-23 07:41:44 +03:00
|
|
|
if (isbinary)
|
|
|
|
txt = NEWLINE_TO_NUL(txt, i);
|
1993-05-08 14:49:52 +04:00
|
|
|
memcpy(rbuf + off, txt, i);
|
|
|
|
off += i;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
txt += rm[0].rm_eo;
|
1993-11-23 07:41:44 +03:00
|
|
|
} while (*txt && (!changed || (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-11-23 07:41:44 +03:00
|
|
|
if (isbinary)
|
|
|
|
txt = NEWLINE_TO_NUL(txt, i);
|
1993-05-08 14:49:52 +04:00
|
|
|
memcpy(rbuf + off, txt, i);
|
|
|
|
memcpy(rbuf + off + i, "\n", 2);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
return changed ? off + i + 1 : 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* join_lines: replace a range of lines with the joined text of those lines */
|
|
|
|
int
|
|
|
|
join_lines(from, to)
|
1993-04-08 05:07:16 +04:00
|
|
|
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 size = 0;
|
|
|
|
line_t *bp, *ep;
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
ep = get_addressed_line_node(INC_MOD(to, addr_last));
|
|
|
|
bp = get_addressed_line_node(from);
|
|
|
|
for (; bp != ep; bp = bp->next) {
|
|
|
|
if ((s = get_sbuf_line(bp)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-12 13:48:43 +03:00
|
|
|
CKBUF(buf, n, size + bp->len, ERR);
|
|
|
|
memcpy(buf + size, s, bp->len);
|
1993-11-23 07:41:44 +03:00
|
|
|
size += bp->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);
|
1993-11-23 07:41:44 +03:00
|
|
|
if (delete_lines(from, to) < 0)
|
1993-07-02 14:02:26 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
current_addr = from - 1;
|
|
|
|
SPL1();
|
|
|
|
if (put_sbuf_line(buf) == NULL ||
|
|
|
|
push_undo_stack(UADD, current_addr, current_addr) == NULL) {
|
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0();
|
1993-05-08 14:49:52 +04:00
|
|
|
modified = 1;
|
1993-04-08 05:07:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* move_lines: move a range of lines */
|
|
|
|
int
|
|
|
|
move_lines(addr)
|
|
|
|
long addr;
|
1993-04-15 08:58:32 +04:00
|
|
|
{
|
1993-11-23 07:41:44 +03:00
|
|
|
line_t *b1, *a1, *b2, *a2;
|
|
|
|
long n = INC_MOD(second_addr, addr_last);
|
|
|
|
long p = first_addr - 1;
|
|
|
|
int done = (addr == first_addr - 1 || addr == second_addr);
|
1993-04-15 08:58:32 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL1();
|
1993-07-02 14:02:26 +04:00
|
|
|
if (done) {
|
1993-11-23 07:41:44 +03:00
|
|
|
a2 = get_addressed_line_node(n);
|
|
|
|
b2 = get_addressed_line_node(p);
|
|
|
|
current_addr = second_addr;
|
|
|
|
} else if (push_undo_stack(UMOV, p, n) == NULL ||
|
|
|
|
push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
|
|
|
|
SPL0();
|
1993-07-02 14:02:26 +04:00
|
|
|
return ERR;
|
|
|
|
} else {
|
1993-11-23 07:41:44 +03:00
|
|
|
a1 = get_addressed_line_node(n);
|
|
|
|
if (addr < first_addr) {
|
|
|
|
b1 = get_addressed_line_node(p);
|
|
|
|
b2 = get_addressed_line_node(addr);
|
|
|
|
/* this get_addressed_line_node last! */
|
|
|
|
} else {
|
|
|
|
b2 = get_addressed_line_node(addr);
|
|
|
|
b1 = get_addressed_line_node(p);
|
|
|
|
/* this get_addressed_line_node last! */
|
|
|
|
}
|
1993-07-02 14:02:26 +04:00
|
|
|
a2 = b2->next;
|
|
|
|
requeue(b2, b1->next);
|
|
|
|
requeue(a1->prev, a2);
|
|
|
|
requeue(b1, a1);
|
1993-11-23 07:41:44 +03:00
|
|
|
current_addr = addr + ((addr < first_addr) ?
|
|
|
|
second_addr - first_addr + 1 : 0);
|
1993-04-15 08:58:32 +04:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
if (isglobal)
|
|
|
|
unset_active_nodes(b2->next, a2);
|
|
|
|
SPL0();
|
1993-04-15 08:58:32 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* copy_lines: copy a range of lines; return status */
|
|
|
|
int
|
|
|
|
copy_lines(addr)
|
|
|
|
long addr;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-11-23 07:41:44 +03:00
|
|
|
line_t *lp, *np = get_addressed_line_node(first_addr);
|
1993-04-08 05:07:16 +04:00
|
|
|
undo_t *up = NULL;
|
1993-11-23 07:41:44 +03:00
|
|
|
long n = second_addr - first_addr + 1;
|
|
|
|
long m = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
current_addr = addr;
|
|
|
|
if (first_addr <= addr && addr < second_addr) {
|
|
|
|
n = addr - first_addr + 1;
|
|
|
|
m = second_addr - addr;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
|
|
|
|
for (; n-- > 0; np = np->next) {
|
|
|
|
SPL1();
|
|
|
|
if ((lp = dup_line_node(np)) == NULL) {
|
|
|
|
SPL0();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
add_line_node(lp);
|
|
|
|
if (up)
|
|
|
|
up->t = lp;
|
|
|
|
else if ((up = push_undo_stack(UADD, current_addr,
|
|
|
|
current_addr)) == NULL) {
|
|
|
|
SPL0();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
SPL0();
|
1993-04-15 08:58:32 +04:00
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* delete_lines: delete a range of lines */
|
|
|
|
int
|
|
|
|
delete_lines(from, to)
|
1993-04-08 05:07:16 +04:00
|
|
|
long from, to;
|
|
|
|
{
|
1993-11-23 07:41:44 +03:00
|
|
|
line_t *n, *p;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL1();
|
|
|
|
if (push_undo_stack(UDEL, from, to) == NULL) {
|
|
|
|
SPL0();
|
1993-04-15 08:58:32 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
n = get_addressed_line_node(INC_MOD(to, addr_last));
|
|
|
|
p = get_addressed_line_node(from - 1);
|
|
|
|
/* this get_addressed_line_node last! */
|
|
|
|
if (isglobal)
|
|
|
|
unset_active_nodes(p->next, n);
|
|
|
|
requeue(p, n);
|
|
|
|
addr_last -= to - from + 1;
|
|
|
|
current_addr = from - 1;
|
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* apply_subst_template: modify text according to a substitution template;
|
1993-05-08 14:49:52 +04:00
|
|
|
return offset to end of modified text */
|
1993-11-23 07:41:44 +03:00
|
|
|
int
|
|
|
|
apply_subst_template(boln, rm, off, re_nsub)
|
1993-04-08 05:07:16 +04:00
|
|
|
char *boln;
|
|
|
|
regmatch_t *rm;
|
1993-05-08 14:49:52 +04:00
|
|
|
int off;
|
1993-11-12 13:48:43 +03:00
|
|
|
int re_nsub;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-05-08 14:49:52 +04:00
|
|
|
int j = 0;
|
|
|
|
int k = 0;
|
1993-11-12 13:48:43 +03:00
|
|
|
int n;
|
1993-05-08 14:49:52 +04:00
|
|
|
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++];
|
1993-11-23 07:41:44 +03:00
|
|
|
} else if (*sub == '\\' && '1' <= *++sub && *sub <= '9' &&
|
|
|
|
(n = *sub - '0') <= re_nsub) {
|
1993-11-12 13:48:43 +03:00
|
|
|
j = rm[n].rm_so;
|
|
|
|
k = rm[n].rm_eo;
|
1993-05-08 14:49:52 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
/* display_lines: print a range of lines to stdout */
|
|
|
|
int
|
|
|
|
display_lines(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-11-23 07:41:44 +03:00
|
|
|
ep = get_addressed_line_node(INC_MOD(to, addr_last));
|
|
|
|
bp = get_addressed_line_node(from);
|
|
|
|
for (; bp != ep; bp = bp->next) {
|
|
|
|
if ((s = get_sbuf_line(bp)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-23 07:41:44 +03:00
|
|
|
output_line(s, bp->len, current_addr = from++, gflag);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
#define ESCAPES "\a\b\f\n\r\t\v\\"
|
|
|
|
#define ESCCHARS "abfnrtv\\"
|
|
|
|
|
1993-04-23 06:08:48 +04:00
|
|
|
int cols = 72; /* wrap column: ws_col - 8 */
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* output_line: print text to stdout */
|
1993-04-08 05:07:16 +04:00
|
|
|
void
|
1993-11-23 07:41:44 +03:00
|
|
|
output_line(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;
|
1993-11-23 07:41:44 +03:00
|
|
|
char *cp;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
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) {
|
1993-11-23 07:41:44 +03:00
|
|
|
if (31 < *s && *s < 127 && *s != '\\')
|
|
|
|
putchar(*s);
|
|
|
|
else {
|
|
|
|
putchar('\\');
|
|
|
|
col++;
|
|
|
|
if (*s && (cp = strchr(ESCAPES, *s)) != NULL)
|
|
|
|
putchar(ESCCHARS[cp - ESCAPES]);
|
|
|
|
else {
|
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;
|
|
|
|
}
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
} 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');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-06-16 11:36:51 +04:00
|
|
|
int newline_added; /* set if newline appended to input file */
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* read_file: read a text file into the editor buffer; return line count */
|
1993-04-08 05:07:16 +04:00
|
|
|
long
|
1993-11-23 07:41:44 +03:00
|
|
|
read_file(n, fn)
|
1993-04-08 05:07:16 +04:00
|
|
|
long n;
|
|
|
|
char *fn;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
1993-11-23 07:41:44 +03:00
|
|
|
line_t *lp = get_addressed_line_node(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;
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((fp = (*fn == '!') ? popen(fn + 1, "r") :
|
|
|
|
fopen(strip_escapes(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)
|
1993-11-23 07:41:44 +03:00
|
|
|
init_des_cipher();
|
|
|
|
for (current_addr = n; (len = get_file_line(fp)) > 0; size += len) {
|
|
|
|
SPL1();
|
|
|
|
if (put_sbuf_line(sbuf) == NULL) {
|
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
lp = lp->next;
|
1993-04-15 08:58:32 +04:00
|
|
|
if (up)
|
|
|
|
up->t = lp;
|
1993-11-23 07:41:44 +03:00
|
|
|
else if ((up = push_undo_stack(UADD, current_addr,
|
|
|
|
current_addr)) == NULL) {
|
|
|
|
SPL0();
|
1993-04-15 08:58:32 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
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-11-23 07:41:44 +03:00
|
|
|
return (len < 0) ? ERR : current_addr - n;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* write_file: write a range of lines to a file; return line count */
|
1993-04-08 05:07:16 +04:00
|
|
|
long
|
1993-11-23 07:41:44 +03:00
|
|
|
write_file(n, m, fn, mode)
|
1993-04-08 05:07:16 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
if ((fp = ((*fn == '!') ? popen(fn + 1, "w") : fopen(strip_escapes(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)
|
1993-11-23 07:41:44 +03:00
|
|
|
init_des_cipher();
|
1993-04-08 05:07:16 +04:00
|
|
|
if (n && !des)
|
1993-11-23 07:41:44 +03:00
|
|
|
for (lp = get_addressed_line_node(n); n <= m; n++, lp = lp->next) {
|
|
|
|
if ((s = get_sbuf_line(lp)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-12 13:48:43 +03:00
|
|
|
len = lp->len;
|
1993-11-23 07:41:44 +03:00
|
|
|
if (n != addr_last || !isbinary || !newline_added)
|
1993-05-08 14:49:52 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
for (lp = get_addressed_line_node(n); n <= m; n++, lp = lp->next) {
|
|
|
|
if ((s = get_sbuf_line(lp)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
1993-11-12 13:48:43 +03:00
|
|
|
len = lp->len;
|
1993-05-08 14:49:52 +04:00
|
|
|
while (len--) {
|
1993-11-23 07:41:44 +03:00
|
|
|
if (put_des_char(*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-11-23 07:41:44 +03:00
|
|
|
if (n != addr_last || !isbinary || !newline_added) {
|
|
|
|
if (put_des_char('\n', fp) < 0) {
|
1993-05-08 14:49:52 +04:00
|
|
|
fprintf(stderr, "%s: %s\n", fn, strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot write file");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
size++; /* for '\n' */
|
|
|
|
}
|
1993-11-12 13:48:43 +03:00
|
|
|
size += lp->len;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
if (des) {
|
1993-11-23 07:41:44 +03:00
|
|
|
flush_des_file(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-11-23 07:41:44 +03:00
|
|
|
/* push_undo_stack: return pointer to intialized undo node */
|
1993-04-08 05:07:16 +04:00
|
|
|
undo_t *
|
1993-11-23 07:41:44 +03:00
|
|
|
push_undo_stack(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)
|
1993-11-23 07:41:44 +03:00
|
|
|
if (ustack == NULL &&
|
|
|
|
(ustack = (undo_t *) malloc((usize = USIZE) * sizeof(undo_t))) == NULL) {
|
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
|
|
|
return NULL;
|
1993-04-14 16:22:14 +04:00
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif
|
|
|
|
t = ustack;
|
1993-11-23 07:41:44 +03:00
|
|
|
if (u_p < usize ||
|
|
|
|
(t = (undo_t *) realloc(ustack, (usize += USIZE) * sizeof(undo_t))) != NULL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
ustack = t;
|
|
|
|
ustack[u_p].type = type;
|
1993-11-23 07:41:44 +03:00
|
|
|
ustack[u_p].t = get_addressed_line_node(to);
|
|
|
|
ustack[u_p].h = get_addressed_line_node(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-11-23 07:41:44 +03:00
|
|
|
clear_undo_stack();
|
1993-04-08 05:07:16 +04:00
|
|
|
free(ustack);
|
|
|
|
ustack = NULL;
|
|
|
|
usize = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1993-07-02 14:02:26 +04:00
|
|
|
|
|
|
|
/* USWAP: swap undo nodes */
|
|
|
|
#define USWAP(x,y) { \
|
|
|
|
undo_t utmp; \
|
|
|
|
utmp = x, x = y, y = utmp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* pop_undo_stack: undo last change to the editor buffer */
|
|
|
|
int
|
|
|
|
pop_undo_stack()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-07-02 14:02:26 +04:00
|
|
|
long n;
|
1993-11-23 07:41:44 +03:00
|
|
|
long o_current_addr = current_addr;
|
|
|
|
long o_addr_last = addr_last;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
if (u_current_addr == -1 || u_addr_last == -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;
|
1993-11-23 07:41:44 +03:00
|
|
|
get_addressed_line_node(0); /* this get_addressed_line_node last! */
|
|
|
|
SPL1();
|
1993-07-02 14:02:26 +04:00
|
|
|
for (n = u_p; n-- > 0;) {
|
|
|
|
switch(ustack[n].type) {
|
1993-04-08 05:07:16 +04:00
|
|
|
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:
|
1993-07-02 14:02:26 +04:00
|
|
|
requeue(ustack[n - 1].h, ustack[n].h->next);
|
|
|
|
requeue(ustack[n].t->prev, ustack[n - 1].t);
|
1993-04-15 08:58:32 +04:00
|
|
|
requeue(ustack[n].h, ustack[n].t);
|
1993-07-02 14:02:26 +04:00
|
|
|
n--;
|
1993-04-15 08:58:32 +04:00
|
|
|
break;
|
1993-04-08 05:07:16 +04:00
|
|
|
default:
|
|
|
|
/*NOTREACHED*/
|
|
|
|
;
|
|
|
|
}
|
1993-07-02 14:02:26 +04:00
|
|
|
ustack[n].type ^= 1;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
/* reverse undo stack order */
|
1993-07-02 14:02:26 +04:00
|
|
|
for (n = u_p; n-- > (u_p + 1)/ 2;)
|
|
|
|
USWAP(ustack[n], ustack[u_p - 1 - n]);
|
1993-11-23 07:41:44 +03:00
|
|
|
if (isglobal)
|
|
|
|
clear_active_list();
|
|
|
|
current_addr = u_current_addr, u_current_addr = o_current_addr;
|
|
|
|
addr_last = u_addr_last, u_addr_last = o_addr_last;
|
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* clear_undo_stack: clear the undo stack */
|
1993-04-08 05:07:16 +04:00
|
|
|
void
|
1993-11-23 07:41:44 +03:00
|
|
|
clear_undo_stack()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-04-10 15:46:38 +04:00
|
|
|
line_t *lp, *ep, *tl;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
|
|
|
while (u_p--)
|
1993-07-02 14:02:26 +04:00
|
|
|
if (ustack[u_p].type == UDEL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
unmark_line_node(lp);
|
1993-04-10 15:46:38 +04:00
|
|
|
tl = lp->next;
|
1993-04-08 05:07:16 +04:00
|
|
|
free(lp);
|
|
|
|
}
|
|
|
|
}
|
1993-07-02 14:02:26 +04:00
|
|
|
u_p = 0;
|
1993-11-23 07:41:44 +03:00
|
|
|
u_current_addr = current_addr;
|
|
|
|
u_addr_last = addr_last;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-07-02 14:02:26 +04:00
|
|
|
#define MAXMARK 26 /* max number of marks */
|
|
|
|
|
|
|
|
line_t *mark[MAXMARK]; /* line markers */
|
|
|
|
int markno; /* line marker count */
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* get_marked_node_addr: return address of a marked line */
|
1993-07-02 14:05:24 +04:00
|
|
|
long
|
1993-11-23 07:41:44 +03:00
|
|
|
get_marked_node_addr(n)
|
1993-07-02 14:02:26 +04:00
|
|
|
int n;
|
1993-11-12 13:48:43 +03:00
|
|
|
{
|
1993-07-02 14:02:26 +04:00
|
|
|
if (!islower(n)) {
|
|
|
|
sprintf(errmsg, "invalid mark character");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
return get_line_node_addr(mark[n - 'a']);
|
1993-07-02 14:02:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* mark_line_node: set a line node mark */
|
1993-07-02 14:02:26 +04:00
|
|
|
int
|
1993-11-23 07:41:44 +03:00
|
|
|
mark_line_node(lp, n)
|
1993-07-02 14:02:26 +04:00
|
|
|
line_t *lp;
|
1993-11-23 07:41:44 +03:00
|
|
|
int n;
|
1993-07-02 14:02:26 +04:00
|
|
|
{
|
|
|
|
if (!islower(n)) {
|
|
|
|
sprintf(errmsg, "invalid mark character");
|
|
|
|
return ERR;
|
|
|
|
} else if (mark[n - 'a'] == NULL)
|
|
|
|
markno++;
|
|
|
|
mark[n - 'a'] = lp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-11-12 13:48:43 +03:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* unmark_line_node: clear line node mark */
|
1993-07-02 14:02:26 +04:00
|
|
|
void
|
1993-11-23 07:41:44 +03:00
|
|
|
unmark_line_node(lp)
|
1993-07-02 14:02:26 +04:00
|
|
|
line_t *lp;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
for (i = 0; markno && i < MAXMARK; i++)
|
|
|
|
if (mark[i] == lp) {
|
|
|
|
mark[i] = NULL;
|
|
|
|
markno--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
line_t **active_list; /* list of lines active in a global command */
|
|
|
|
long active_last; /* index of last active line in active_list */
|
|
|
|
long active_size; /* size of active_list */
|
|
|
|
long active_ptr; /* active_list index (non-decreasing) */
|
|
|
|
long active_ndx; /* active_list index (modulo active_last) */
|
|
|
|
|
|
|
|
|
|
|
|
/* set_active_node: add a line node to the global-active list */
|
|
|
|
int
|
|
|
|
set_active_node(lp)
|
|
|
|
line_t *lp;
|
|
|
|
{
|
|
|
|
if (active_last + 1 > active_size) {
|
|
|
|
int ti = active_size;
|
|
|
|
line_t **ts;
|
|
|
|
SPL1();
|
|
|
|
#if defined(sun) || defined(NO_REALLOC_NULL)
|
|
|
|
if (active_list != NULL) {
|
|
|
|
#endif
|
|
|
|
if ((ts = (line_t **) realloc(active_list,
|
|
|
|
(ti += MINBUFSZ) * sizeof(line_t **))) == NULL) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "out of memory");
|
|
|
|
SPL0();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
#if defined(sun) || defined(NO_REALLOC_NULL)
|
|
|
|
} else {
|
|
|
|
if ((ts = (line_t **) malloc((ti += MINBUFSZ) *
|
|
|
|
sizeof(line_t **))) == NULL) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "out of memory");
|
|
|
|
SPL0();
|
|
|
|
return ERR;
|
1993-07-02 14:02:26 +04:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
active_size = ti;
|
|
|
|
active_list = ts;
|
|
|
|
SPL0();
|
|
|
|
}
|
|
|
|
active_list[active_last++] = lp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* unset_active_nodes: remove a range of lines from the global-active list */
|
|
|
|
void
|
|
|
|
unset_active_nodes(np, mp)
|
|
|
|
line_t *np, *mp;
|
|
|
|
{
|
|
|
|
line_t *lp;
|
|
|
|
long i;
|
|
|
|
|
|
|
|
for (lp = np; lp != mp; lp = lp->next)
|
|
|
|
for (i = 0; i < active_last; i++)
|
|
|
|
if (active_list[active_ndx] == lp) {
|
|
|
|
active_list[active_ndx] = NULL;
|
|
|
|
active_ndx = INC_MOD(active_ndx, active_last - 1);
|
|
|
|
break;
|
|
|
|
} else active_ndx = INC_MOD(active_ndx, active_last - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* next_active_node: return the next global-active line node */
|
|
|
|
line_t *
|
|
|
|
next_active_node()
|
|
|
|
{
|
|
|
|
while (active_ptr < active_last && active_list[active_ptr] == NULL)
|
|
|
|
active_ptr++;
|
|
|
|
return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* clear_active_list: clear the global-active list */
|
|
|
|
void
|
|
|
|
clear_active_list()
|
|
|
|
{
|
|
|
|
SPL1();
|
|
|
|
active_size = active_last = active_ptr = active_ndx = 0;
|
|
|
|
free(active_list);
|
|
|
|
active_list = NULL;
|
|
|
|
SPL0();
|
1993-07-02 14:02:26 +04:00
|
|
|
}
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
|
|
|
|
/* get_file_line: read a line of text from a file; return line length */
|
|
|
|
int
|
|
|
|
get_file_line(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-11-23 07:41:44 +03:00
|
|
|
while (((c = des ? get_des_char(fp) : getc(fp)) != EOF || !feof(fp) &&
|
|
|
|
!ferror(fp)) && c != '\n') {
|
1993-05-08 14:49:52 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* get_input_line: read a line of text from stdin; return line length */
|
|
|
|
int
|
|
|
|
get_input_line()
|
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-11-23 07:41:44 +03:00
|
|
|
/* get_extended_line: get a an extended line from stdin */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *
|
1993-11-23 07:41:44 +03:00
|
|
|
get_extended_line(sizep, nonl)
|
1993-05-08 14:49:52 +04:00
|
|
|
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')
|
|
|
|
;
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((l = t - ibufp) < 2 || !has_trailing_escape(ibufp, ibufp + l - 1)) {
|
1993-06-16 11:36:51 +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-11-23 07:41:44 +03:00
|
|
|
if ((n = get_input_line()) < 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);
|
1993-06-16 11:36:51 +04:00
|
|
|
l += n;
|
1993-11-23 07:41:44 +03:00
|
|
|
if (n < 2 || !has_trailing_escape(cvbuf, cvbuf + l - 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-11-23 07:41:44 +03:00
|
|
|
/* dup_line_node: return a pointer to a copy of a line node */
|
1993-04-08 05:07:16 +04:00
|
|
|
line_t *
|
1993-11-23 07:41:44 +03:00
|
|
|
dup_line_node(lp)
|
1993-04-08 05:07:16 +04:00
|
|
|
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;
|
1993-11-12 13:48:43 +03:00
|
|
|
np->len = lp->len;
|
1993-04-08 05:07:16 +04:00
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* has_trailing_escape: return the parity of escapes preceding a character
|
|
|
|
in a string */
|
|
|
|
int
|
|
|
|
has_trailing_escape(s, t)
|
1993-04-08 05:07:16 +04:00
|
|
|
char *s;
|
|
|
|
char *t;
|
|
|
|
{
|
1993-11-23 07:41:44 +03:00
|
|
|
return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* strip_escapes: return copy of escaped string */
|
1993-04-23 06:08:48 +04:00
|
|
|
char *
|
1993-11-23 07:41:44 +03:00
|
|
|
strip_escapes(s)
|
1993-04-23 06:08:48 +04:00
|
|
|
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-11-23 07:41:44 +03:00
|
|
|
signal_hup(signo)
|
1993-04-10 15:46:38 +04:00
|
|
|
int signo;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
if (mutex)
|
1993-04-10 15:46:38 +04:00
|
|
|
sigflags |= (1 << signo);
|
1993-11-23 07:41:44 +03:00
|
|
|
else handle_hup(signo);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1993-11-23 07:41:44 +03:00
|
|
|
signal_int(signo)
|
1993-04-10 15:46:38 +04:00
|
|
|
int signo;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
if (mutex)
|
1993-04-10 15:46:38 +04:00
|
|
|
sigflags |= (1 << signo);
|
1993-11-23 07:41:44 +03:00
|
|
|
else handle_int(signo);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1993-11-23 07:41:44 +03:00
|
|
|
handle_hup(signo)
|
1993-04-10 15:46:38 +04:00
|
|
|
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-06-16 11:36:51 +04:00
|
|
|
if (!sigactive)
|
1993-05-08 14:49:52 +04:00
|
|
|
quit(1);
|
1993-04-10 15:46:38 +04:00
|
|
|
sigflags &= ~(1 << signo);
|
1993-11-23 07:41:44 +03:00
|
|
|
if (addr_last && write_file(1, addr_last, "ed.hup", "w") < 0 &&
|
|
|
|
(s = getenv("HOME")) != NULL &&
|
|
|
|
(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");
|
1993-11-23 07:41:44 +03:00
|
|
|
write_file(1, addr_last, hup, "w");
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1993-11-23 07:41:44 +03:00
|
|
|
handle_int(signo)
|
1993-04-10 15:46:38 +04:00
|
|
|
int signo;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-06-16 11:36:51 +04:00
|
|
|
if (!sigactive)
|
1993-05-08 14:49:52 +04:00
|
|
|
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
|
1993-11-23 07:41:44 +03:00
|
|
|
handle_winch(signo)
|
1993-04-10 15:46:38 +04:00
|
|
|
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
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* is_legal_filename: return a legal filename */
|
|
|
|
int
|
|
|
|
is_legal_filename(s)
|
1993-05-27 05:27:46 +04:00
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
|
|
|
|
sprintf(errmsg, "shell access restricted");
|
1993-11-23 07:41:44 +03:00
|
|
|
return 0;
|
1993-05-27 05:27:46 +04:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
return 1;
|
1993-05-27 05:27:46 +04:00
|
|
|
}
|