1998-01-09 11:03:16 +03:00
|
|
|
/* $NetBSD: ex_subst.c,v 1.11 1998/01/09 08:08:05 perry Exp $ */
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
/*-
|
1994-03-28 08:28:20 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994
|
1994-01-24 08:52:58 +03:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-05-20 07:47:00 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-01-24 08:52:58 +03:00
|
|
|
*
|
1996-05-20 07:47:00 +04:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
#ifndef lint
|
1996-05-20 07:47:00 +04:00
|
|
|
static const char sccsid[] = "@(#)ex_subst.c 10.30 (Berkeley) 5/16/96";
|
1994-01-24 08:52:58 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/time.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <bitstring.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "../common/common.h"
|
|
|
|
#include "../vi/vi.h"
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
#define SUB_FIRST 0x01 /* The 'r' flag isn't reasonable. */
|
|
|
|
#define SUB_MUSTSETR 0x02 /* The 'r' flag is required. */
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
static int re_conv __P((SCR *, char **, int *));
|
|
|
|
static int re_cscope_conv __P((SCR *, char **, int *));
|
|
|
|
static int re_sub __P((SCR *,
|
|
|
|
char *, char **, size_t *, size_t *, regmatch_t [10]));
|
|
|
|
static int re_tag_conv __P((SCR *, char **, int *));
|
|
|
|
static int s __P((SCR *, EXCMD *, char *, regex_t *, u_int));
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
1996-05-20 07:47:00 +04:00
|
|
|
* ex_s --
|
1994-01-24 08:52:58 +03:00
|
|
|
* [line [,line]] s[ubstitute] [[/;]pat[/;]/repl[/;] [cgr] [count] [#lp]]
|
|
|
|
*
|
|
|
|
* Substitute on lines matching a pattern.
|
1996-05-20 07:47:00 +04:00
|
|
|
*
|
|
|
|
* PUBLIC: int ex_s __P((SCR *, EXCMD *));
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
int
|
1996-05-20 07:47:00 +04:00
|
|
|
ex_s(sp, cmdp)
|
1994-01-24 08:52:58 +03:00
|
|
|
SCR *sp;
|
1996-05-20 07:47:00 +04:00
|
|
|
EXCMD *cmdp;
|
1994-01-24 08:52:58 +03:00
|
|
|
{
|
1996-05-20 07:47:00 +04:00
|
|
|
regex_t *re;
|
1994-01-24 08:52:58 +03:00
|
|
|
size_t blen, len;
|
|
|
|
u_int flags;
|
1996-05-20 07:47:00 +04:00
|
|
|
int delim;
|
1994-01-24 08:52:58 +03:00
|
|
|
char *bp, *ptrn, *rep, *p, *t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip leading white space.
|
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* Historic vi allowed any non-alphanumeric to serve as the
|
|
|
|
* substitution command delimiter.
|
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* If the arguments are empty, it's the same as &, i.e. we
|
|
|
|
* repeat the last substitution.
|
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
if (cmdp->argc == 0)
|
|
|
|
goto subagain;
|
1994-01-24 08:52:58 +03:00
|
|
|
for (p = cmdp->argv[0]->bp,
|
|
|
|
len = cmdp->argv[0]->len; len > 0; --len, ++p) {
|
|
|
|
if (!isblank(*p))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len == 0)
|
1996-05-20 07:47:00 +04:00
|
|
|
subagain: return (ex_subagain(sp, cmdp));
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
delim = *p++;
|
1996-05-20 07:47:00 +04:00
|
|
|
if (isalnum(delim) || delim == '\\')
|
|
|
|
return (s(sp, cmdp, p, &sp->subre_c, SUB_MUSTSETR));
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* The full-blown substitute command reset the remembered
|
|
|
|
* state of the 'c' and 'g' suffices.
|
|
|
|
*/
|
|
|
|
sp->c_suffix = sp->g_suffix = 0;
|
1996-05-20 07:47:00 +04:00
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
/*
|
|
|
|
* Get the pattern string, toss escaped characters.
|
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* Historic vi accepted any of the following forms:
|
|
|
|
*
|
|
|
|
* :s/abc/def/ change "abc" to "def"
|
|
|
|
* :s/abc/def change "abc" to "def"
|
|
|
|
* :s/abc/ delete "abc"
|
|
|
|
* :s/abc delete "abc"
|
|
|
|
*
|
|
|
|
* QUOTING NOTE:
|
|
|
|
*
|
|
|
|
* Only toss an escape character if it escapes a delimiter.
|
|
|
|
* This means that "s/A/\\\\f" replaces "A" with "\\f". It
|
|
|
|
* would be nice to be more regular, i.e. for each layer of
|
|
|
|
* escaping a single escape character is removed, but that's
|
|
|
|
* not how the historic vi worked.
|
|
|
|
*/
|
|
|
|
for (ptrn = t = p;;) {
|
|
|
|
if (p[0] == '\0' || p[0] == delim) {
|
|
|
|
if (p[0] == delim)
|
|
|
|
++p;
|
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* Nul terminate the pattern string -- it's passed
|
|
|
|
* to regcomp which doesn't understand anything else.
|
|
|
|
*/
|
|
|
|
*t = '\0';
|
|
|
|
break;
|
|
|
|
}
|
1994-03-28 08:28:20 +04:00
|
|
|
if (p[0] == '\\')
|
1994-03-04 02:26:31 +03:00
|
|
|
if (p[1] == delim)
|
|
|
|
++p;
|
1994-03-28 08:28:20 +04:00
|
|
|
else if (p[1] == '\\')
|
1994-03-04 02:26:31 +03:00
|
|
|
*t++ = *p++;
|
1994-01-24 08:52:58 +03:00
|
|
|
*t++ = *p++;
|
|
|
|
}
|
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
/*
|
|
|
|
* If the pattern string is empty, use the last RE (not just the
|
|
|
|
* last substitution RE).
|
|
|
|
*/
|
|
|
|
if (*ptrn == '\0') {
|
1996-05-20 07:47:00 +04:00
|
|
|
if (sp->re == NULL) {
|
|
|
|
ex_emsg(sp, NULL, EXM_NOPREVRE);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
/* Compile the RE if necessary. */
|
|
|
|
if (!F_ISSET(sp, SC_RE_SEARCH) &&
|
|
|
|
re_compile(sp, sp->re, NULL, NULL, &sp->re_c, RE_C_SEARCH))
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
1996-05-20 07:47:00 +04:00
|
|
|
flags = 0;
|
|
|
|
} else {
|
1994-01-24 08:52:58 +03:00
|
|
|
/*
|
|
|
|
* !!!
|
1996-05-20 07:47:00 +04:00
|
|
|
* Compile the RE. Historic practice is that substitutes set
|
|
|
|
* the search direction as well as both substitute and search
|
|
|
|
* RE's. We compile the RE twice, as we don't want to bother
|
|
|
|
* ref counting the pattern string and (opaque) structure.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
if (re_compile(sp,
|
|
|
|
ptrn, &sp->re, &sp->re_len, &sp->re_c, RE_C_SEARCH))
|
|
|
|
return (1);
|
|
|
|
if (re_compile(sp,
|
|
|
|
ptrn, &sp->subre, &sp->subre_len, &sp->subre_c, RE_C_SUBST))
|
|
|
|
return (1);
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
flags = SUB_FIRST;
|
1996-05-20 07:47:00 +04:00
|
|
|
sp->searchdir = FORWARD;
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
1996-05-20 07:47:00 +04:00
|
|
|
re = &sp->re_c;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the replacement string.
|
|
|
|
*
|
|
|
|
* The special character & (\& if O_MAGIC not set) matches the
|
|
|
|
* entire RE. No handling of & is required here, it's done by
|
1996-05-20 07:47:00 +04:00
|
|
|
* re_sub().
|
1994-01-24 08:52:58 +03:00
|
|
|
*
|
1994-03-28 08:28:20 +04:00
|
|
|
* The special character ~ (\~ if O_MAGIC not set) inserts the
|
|
|
|
* previous replacement string into this replacement string.
|
|
|
|
* Count ~'s to figure out how much space we need. We could
|
|
|
|
* special case nonexistent last patterns or whether or not
|
|
|
|
* O_MAGIC is set, but it's probably not worth the effort.
|
|
|
|
*
|
1994-01-24 08:52:58 +03:00
|
|
|
* QUOTING NOTE:
|
|
|
|
*
|
|
|
|
* Only toss an escape character if it escapes a delimiter or
|
1994-03-28 08:28:20 +04:00
|
|
|
* if O_MAGIC is set and it escapes a tilde.
|
1994-08-17 20:35:35 +04:00
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* If the entire replacement pattern is "%", then use the last
|
|
|
|
* replacement pattern. This semantic was added to vi in System
|
|
|
|
* V and then percolated elsewhere, presumably around the time
|
|
|
|
* that it was added to their version of ed(1).
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
if (p[0] == '\0' || p[0] == delim) {
|
|
|
|
if (p[0] == delim)
|
|
|
|
++p;
|
1994-01-24 08:52:58 +03:00
|
|
|
if (sp->repl != NULL)
|
1996-05-20 07:47:00 +04:00
|
|
|
free(sp->repl);
|
1994-01-24 08:52:58 +03:00
|
|
|
sp->repl = NULL;
|
|
|
|
sp->repl_len = 0;
|
1994-08-17 20:35:35 +04:00
|
|
|
} else if (p[0] == '%' && (p[1] == '\0' || p[1] == delim))
|
|
|
|
p += p[1] == delim ? 2 : 1;
|
|
|
|
else {
|
1994-01-24 08:52:58 +03:00
|
|
|
for (rep = p, len = 0;
|
|
|
|
p[0] != '\0' && p[0] != delim; ++p, ++len)
|
|
|
|
if (p[0] == '~')
|
|
|
|
len += sp->repl_len;
|
|
|
|
GET_SPACE_RET(sp, bp, blen, len);
|
|
|
|
for (t = bp, len = 0, p = rep;;) {
|
|
|
|
if (p[0] == '\0' || p[0] == delim) {
|
|
|
|
if (p[0] == delim)
|
|
|
|
++p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p[0] == '\\') {
|
1994-03-28 08:28:20 +04:00
|
|
|
if (p[1] == delim)
|
1994-01-24 08:52:58 +03:00
|
|
|
++p;
|
1994-03-28 08:28:20 +04:00
|
|
|
else if (p[1] == '\\') {
|
|
|
|
*t++ = *p++;
|
|
|
|
++len;
|
|
|
|
} else if (p[1] == '~') {
|
1994-01-24 08:52:58 +03:00
|
|
|
++p;
|
|
|
|
if (!O_ISSET(sp, O_MAGIC))
|
|
|
|
goto tilde;
|
|
|
|
}
|
|
|
|
} else if (p[0] == '~' && O_ISSET(sp, O_MAGIC)) {
|
|
|
|
tilde: ++p;
|
|
|
|
memmove(t, sp->repl, sp->repl_len);
|
|
|
|
t += sp->repl_len;
|
|
|
|
len += sp->repl_len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*t++ = *p++;
|
|
|
|
++len;
|
|
|
|
}
|
1994-08-17 20:35:35 +04:00
|
|
|
if ((sp->repl_len = len) != 0) {
|
|
|
|
if (sp->repl != NULL)
|
|
|
|
free(sp->repl);
|
|
|
|
if ((sp->repl = malloc(len)) == NULL) {
|
|
|
|
msgq(sp, M_SYSERR, NULL);
|
|
|
|
FREE_SPACE(sp, bp, blen);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
memmove(sp->repl, bp, len);
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
|
|
|
FREE_SPACE(sp, bp, blen);
|
|
|
|
}
|
1996-05-20 07:47:00 +04:00
|
|
|
return (s(sp, cmdp, p, re, flags));
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_subagain --
|
|
|
|
* [line [,line]] & [cgr] [count] [#lp]]
|
|
|
|
*
|
|
|
|
* Substitute using the last substitute RE and replacement pattern.
|
1996-05-20 07:47:00 +04:00
|
|
|
*
|
|
|
|
* PUBLIC: int ex_subagain __P((SCR *, EXCMD *));
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
int
|
1996-05-20 07:47:00 +04:00
|
|
|
ex_subagain(sp, cmdp)
|
1994-01-24 08:52:58 +03:00
|
|
|
SCR *sp;
|
1996-05-20 07:47:00 +04:00
|
|
|
EXCMD *cmdp;
|
1994-01-24 08:52:58 +03:00
|
|
|
{
|
1996-05-20 07:47:00 +04:00
|
|
|
if (sp->subre == NULL) {
|
|
|
|
ex_emsg(sp, NULL, EXM_NOPREVRE);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
1996-05-20 07:47:00 +04:00
|
|
|
if (!F_ISSET(sp, SC_RE_SUBST) &&
|
|
|
|
re_compile(sp, sp->subre, NULL, NULL, &sp->subre_c, RE_C_SUBST))
|
|
|
|
return (1);
|
|
|
|
return (s(sp,
|
|
|
|
cmdp, cmdp->argc ? cmdp->argv[0]->bp : NULL, &sp->subre_c, 0));
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_subtilde --
|
|
|
|
* [line [,line]] ~ [cgr] [count] [#lp]]
|
|
|
|
*
|
|
|
|
* Substitute using the last RE and last substitute replacement pattern.
|
1996-05-20 07:47:00 +04:00
|
|
|
*
|
|
|
|
* PUBLIC: int ex_subtilde __P((SCR *, EXCMD *));
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
int
|
1996-05-20 07:47:00 +04:00
|
|
|
ex_subtilde(sp, cmdp)
|
1994-01-24 08:52:58 +03:00
|
|
|
SCR *sp;
|
1996-05-20 07:47:00 +04:00
|
|
|
EXCMD *cmdp;
|
1994-01-24 08:52:58 +03:00
|
|
|
{
|
1996-05-20 07:47:00 +04:00
|
|
|
if (sp->re == NULL) {
|
|
|
|
ex_emsg(sp, NULL, EXM_NOPREVRE);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
1996-05-20 07:47:00 +04:00
|
|
|
if (!F_ISSET(sp, SC_RE_SEARCH) &&
|
|
|
|
re_compile(sp, sp->re, NULL, NULL, &sp->re_c, RE_C_SEARCH))
|
|
|
|
return (1);
|
|
|
|
return (s(sp,
|
|
|
|
cmdp, cmdp->argc ? cmdp->argv[0]->bp : NULL, &sp->re_c, 0));
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
|
|
|
|
1994-03-28 08:28:20 +04:00
|
|
|
/*
|
1996-05-20 07:47:00 +04:00
|
|
|
* s --
|
|
|
|
* Do the substitution. This stuff is *really* tricky. There are lots of
|
|
|
|
* special cases, and general nastiness. Don't mess with it unless you're
|
|
|
|
* pretty confident.
|
|
|
|
*
|
1994-01-24 08:52:58 +03:00
|
|
|
* The nasty part of the substitution is what happens when the replacement
|
|
|
|
* string contains newlines. It's a bit tricky -- consider the information
|
|
|
|
* that has to be retained for "s/f\(o\)o/^M\1^M\1/". The solution here is
|
|
|
|
* to build a set of newline offsets which we use to break the line up later,
|
1996-05-20 07:47:00 +04:00
|
|
|
* when the replacement is done. Don't change it unless you're *damned*
|
1994-01-24 08:52:58 +03:00
|
|
|
* confident.
|
|
|
|
*/
|
|
|
|
#define NEEDNEWLINE(sp) { \
|
|
|
|
if (sp->newl_len == sp->newl_cnt) { \
|
|
|
|
sp->newl_len += 25; \
|
|
|
|
REALLOC(sp, sp->newl, size_t *, \
|
|
|
|
sp->newl_len * sizeof(size_t)); \
|
|
|
|
if (sp->newl == NULL) { \
|
|
|
|
sp->newl_len = 0; \
|
|
|
|
return (1); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BUILD(sp, l, len) { \
|
|
|
|
if (lbclen + (len) > lblen) { \
|
|
|
|
lblen += MAX(lbclen + (len), 256); \
|
|
|
|
REALLOC(sp, lb, char *, lblen); \
|
|
|
|
if (lb == NULL) { \
|
|
|
|
lbclen = 0; \
|
|
|
|
return (1); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
memmove(lb + lbclen, l, len); \
|
|
|
|
lbclen += len; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NEEDSP(sp, len, pnt) { \
|
|
|
|
if (lbclen + (len) > lblen) { \
|
|
|
|
lblen += MAX(lbclen + (len), 256); \
|
|
|
|
REALLOC(sp, lb, char *, lblen); \
|
|
|
|
if (lb == NULL) { \
|
|
|
|
lbclen = 0; \
|
|
|
|
return (1); \
|
|
|
|
} \
|
|
|
|
pnt = lb + lbclen; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1996-05-20 07:47:00 +04:00
|
|
|
s(sp, cmdp, s, re, flags)
|
1994-01-24 08:52:58 +03:00
|
|
|
SCR *sp;
|
1996-05-20 07:47:00 +04:00
|
|
|
EXCMD *cmdp;
|
1994-01-24 08:52:58 +03:00
|
|
|
char *s;
|
|
|
|
regex_t *re;
|
|
|
|
u_int flags;
|
|
|
|
{
|
1996-05-20 07:47:00 +04:00
|
|
|
EVENT ev;
|
1994-01-24 08:52:58 +03:00
|
|
|
MARK from, to;
|
1996-05-20 07:47:00 +04:00
|
|
|
TEXTH tiq;
|
1996-05-20 08:49:48 +04:00
|
|
|
recno_t elno, lno, slno;
|
|
|
|
long llno;
|
1994-08-17 20:35:35 +04:00
|
|
|
regmatch_t match[10];
|
1996-05-20 07:47:00 +04:00
|
|
|
size_t blen, cnt, last, lbclen, lblen, len, llen;
|
|
|
|
size_t offset, saved_offset, scno;
|
1994-08-17 20:35:35 +04:00
|
|
|
int cflag, lflag, nflag, pflag, rflag;
|
1994-03-28 08:28:20 +04:00
|
|
|
int didsub, do_eol_match, eflags, empty_ok, eval;
|
1994-08-17 20:35:35 +04:00
|
|
|
int linechanged, matched, quit, rval;
|
1994-01-24 08:52:58 +03:00
|
|
|
char *bp, *lb;
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
NEEDFILE(sp, cmdp);
|
|
|
|
|
|
|
|
slno = sp->lno;
|
|
|
|
scno = sp->cno;
|
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* Historically, the 'g' and 'c' suffices were always toggled as flags,
|
|
|
|
* so ":s/A/B/" was the same as ":s/A/B/ccgg". If O_EDCOMPATIBLE was
|
|
|
|
* not set, they were initialized to 0 for all substitute commands. If
|
|
|
|
* O_EDCOMPATIBLE was set, they were initialized to 0 only if the user
|
1996-05-20 07:47:00 +04:00
|
|
|
* specified substitute/replacement patterns (see ex_s()).
|
1994-08-17 20:35:35 +04:00
|
|
|
*/
|
|
|
|
if (!O_ISSET(sp, O_EDCOMPATIBLE))
|
|
|
|
sp->c_suffix = sp->g_suffix = 0;
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
/*
|
|
|
|
* Historic vi permitted the '#', 'l' and 'p' options in vi mode, but
|
|
|
|
* it only displayed the last change. I'd disallow them, but they are
|
|
|
|
* useful in combination with the [v]global commands. In the current
|
|
|
|
* model the problem is combining them with the 'c' flag -- the screen
|
|
|
|
* would have to flip back and forth between the confirm screen and the
|
|
|
|
* ex print screen, which would be pretty awful. We do display all
|
|
|
|
* changes, though, for what that's worth.
|
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* Historic vi was fairly strict about the order of "options", the
|
|
|
|
* count, and "flags". I'm somewhat fuzzy on the difference between
|
|
|
|
* options and flags, anyway, so this is a simpler approach, and we
|
|
|
|
* just take it them in whatever order the user gives them. (The ex
|
|
|
|
* usage statement doesn't reflect this.)
|
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
cflag = lflag = nflag = pflag = rflag = 0;
|
1996-05-20 07:47:00 +04:00
|
|
|
if (s == NULL)
|
|
|
|
goto noargs;
|
1994-01-24 08:52:58 +03:00
|
|
|
for (lno = OOBLNO; *s != '\0'; ++s)
|
|
|
|
switch (*s) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
1994-08-17 20:35:35 +04:00
|
|
|
continue;
|
|
|
|
case '+':
|
|
|
|
++cmdp->flagoff;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
--cmdp->flagoff;
|
1994-01-24 08:52:58 +03:00
|
|
|
break;
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
if (lno != OOBLNO)
|
|
|
|
goto usage;
|
|
|
|
errno = 0;
|
1996-05-20 08:49:48 +04:00
|
|
|
llno = strtoul(s, &s, 10);
|
1994-01-24 08:52:58 +03:00
|
|
|
if (*s == '\0') /* Loop increment correction. */
|
|
|
|
--s;
|
1996-05-20 08:49:48 +04:00
|
|
|
lno = llno;
|
|
|
|
if (llno != lno) {
|
|
|
|
errno = ERANGE;
|
|
|
|
llno = LONG_MAX;
|
|
|
|
}
|
1994-01-24 08:52:58 +03:00
|
|
|
if (errno == ERANGE) {
|
1996-05-20 08:49:48 +04:00
|
|
|
if (llno == LONG_MAX)
|
1996-05-20 07:47:00 +04:00
|
|
|
msgq(sp, M_ERR, "153|Count overflow");
|
1996-05-20 08:49:48 +04:00
|
|
|
else if (llno == LONG_MIN)
|
1996-05-20 07:47:00 +04:00
|
|
|
msgq(sp, M_ERR, "154|Count underflow");
|
1994-01-24 08:52:58 +03:00
|
|
|
else
|
|
|
|
msgq(sp, M_SYSERR, NULL);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* In historic vi, the count was inclusive from the
|
|
|
|
* second address.
|
|
|
|
*/
|
|
|
|
cmdp->addr1.lno = cmdp->addr2.lno;
|
|
|
|
cmdp->addr2.lno += lno - 1;
|
1996-05-20 07:47:00 +04:00
|
|
|
if (!db_exist(sp, cmdp->addr2.lno) &&
|
|
|
|
db_last(sp, &cmdp->addr2.lno))
|
|
|
|
return (1);
|
1994-01-24 08:52:58 +03:00
|
|
|
break;
|
|
|
|
case '#':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
1994-08-17 20:35:35 +04:00
|
|
|
sp->c_suffix = !sp->c_suffix;
|
1996-05-20 07:47:00 +04:00
|
|
|
|
|
|
|
/* Ex text structure initialization. */
|
|
|
|
if (F_ISSET(sp, SC_EX)) {
|
|
|
|
memset(&tiq, 0, sizeof(TEXTH));
|
|
|
|
CIRCLEQ_INIT(&tiq);
|
|
|
|
}
|
1994-01-24 08:52:58 +03:00
|
|
|
break;
|
|
|
|
case 'g':
|
1994-08-17 20:35:35 +04:00
|
|
|
sp->g_suffix = !sp->g_suffix;
|
1994-01-24 08:52:58 +03:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
if (LF_ISSET(SUB_FIRST)) {
|
|
|
|
msgq(sp, M_ERR,
|
1996-05-20 07:47:00 +04:00
|
|
|
"155|Regular expression specified; r flag meaningless");
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
1996-05-20 07:47:00 +04:00
|
|
|
if (!F_ISSET(sp, SC_RE_SEARCH)) {
|
|
|
|
ex_emsg(sp, NULL, EXM_NOPREVRE);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
rflag = 1;
|
1996-05-20 07:47:00 +04:00
|
|
|
re = &sp->re_c;
|
1994-01-24 08:52:58 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*s != '\0' || !rflag && LF_ISSET(SUB_MUSTSETR)) {
|
1996-05-20 07:47:00 +04:00
|
|
|
usage: ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
noargs: if (F_ISSET(sp, SC_VI) && sp->c_suffix && (lflag || nflag || pflag)) {
|
1994-01-24 08:52:58 +03:00
|
|
|
msgq(sp, M_ERR,
|
1996-05-20 07:47:00 +04:00
|
|
|
"156|The #, l and p flags may not be combined with the c flag in vi mode");
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bp: if interactive, line cache
|
|
|
|
* blen: if interactive, line cache length
|
|
|
|
* lb: build buffer pointer.
|
|
|
|
* lbclen: current length of built buffer.
|
|
|
|
* lblen; length of build buffer.
|
|
|
|
*/
|
|
|
|
bp = lb = NULL;
|
|
|
|
blen = lbclen = lblen = 0;
|
|
|
|
|
|
|
|
/* For each line... */
|
|
|
|
for (matched = quit = 0, lno = cmdp->addr1.lno,
|
|
|
|
elno = cmdp->addr2.lno; !quit && lno <= elno; ++lno) {
|
|
|
|
|
|
|
|
/* Someone's unhappy, time to stop. */
|
1996-05-20 07:47:00 +04:00
|
|
|
if (INTERRUPTED(sp))
|
1994-01-24 08:52:58 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Get the line. */
|
1996-05-20 07:47:00 +04:00
|
|
|
if (db_get(sp, lno, DBG_FATAL, &s, &llen))
|
|
|
|
goto err;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a local copy if doing confirmation -- when calling
|
|
|
|
* the confirm routine we're likely to lose the cached copy.
|
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
if (sp->c_suffix) {
|
1994-01-24 08:52:58 +03:00
|
|
|
if (bp == NULL) {
|
|
|
|
GET_SPACE_RET(sp, bp, blen, llen);
|
|
|
|
} else
|
|
|
|
ADD_SPACE_RET(sp, bp, blen, llen);
|
|
|
|
memmove(bp, s, llen);
|
|
|
|
s = bp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start searching from the beginning. */
|
|
|
|
offset = 0;
|
|
|
|
len = llen;
|
|
|
|
|
|
|
|
/* Reset the build buffer offset. */
|
|
|
|
lbclen = 0;
|
|
|
|
|
|
|
|
/* Reset empty match flag. */
|
|
|
|
empty_ok = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't want to have to do a setline if the line didn't
|
|
|
|
* change -- keep track of whether or not this line changed.
|
|
|
|
* If doing confirmations, don't want to keep setting the
|
|
|
|
* line if change is refused -- keep track of substitutions.
|
|
|
|
*/
|
|
|
|
didsub = linechanged = 0;
|
|
|
|
|
|
|
|
/* New line, do an EOL match. */
|
|
|
|
do_eol_match = 1;
|
|
|
|
|
|
|
|
/* It's not nul terminated, but we pretend it is. */
|
|
|
|
eflags = REG_STARTEND;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The search area is from s + offset to the EOL.
|
|
|
|
*
|
1994-08-17 20:35:35 +04:00
|
|
|
* Generally, match[0].rm_so is the offset of the start
|
|
|
|
* of the match from the start of the search, and offset
|
|
|
|
* is the offset of the start of the last search.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
nextmatch: match[0].rm_so = 0;
|
|
|
|
match[0].rm_eo = len;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/* Get the next match. */
|
1994-08-17 20:35:35 +04:00
|
|
|
eval = regexec(re, (char *)s + offset, 10, match, eflags);
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There wasn't a match or if there was an error, deal with
|
|
|
|
* it. If there was a previous match in this line, resolve
|
|
|
|
* the changes into the database. Otherwise, just move on.
|
|
|
|
*/
|
|
|
|
if (eval == REG_NOMATCH)
|
|
|
|
goto endmatch;
|
|
|
|
if (eval != 0) {
|
|
|
|
re_error(sp, eval, re);
|
1996-05-20 07:47:00 +04:00
|
|
|
goto err;
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
|
|
|
matched = 1;
|
|
|
|
|
|
|
|
/* Only the first search can match an anchored expression. */
|
|
|
|
eflags |= REG_NOTBOL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* It's possible to match 0-length strings -- for example, the
|
|
|
|
* command s;a*;X;, when matched against the string "aabb" will
|
|
|
|
* result in "XbXbX", i.e. the matches are "aa", the space
|
|
|
|
* between the b's and the space between the b's and the end of
|
|
|
|
* the string. There is a similar space between the beginning
|
|
|
|
* of the string and the a's. The rule that we use (because vi
|
|
|
|
* historically used it) is that any 0-length match, occurring
|
|
|
|
* immediately after a match, is ignored. Otherwise, the above
|
|
|
|
* example would have resulted in "XXbXbX". Another example is
|
|
|
|
* incorrectly using " *" to replace groups of spaces with one
|
|
|
|
* space.
|
|
|
|
*
|
|
|
|
* The way we do this is that if we just had a successful match,
|
|
|
|
* the starting offset does not skip characters, and the match
|
|
|
|
* is empty, ignore the match and move forward. If there's no
|
|
|
|
* more characters in the string, we were attempting to match
|
|
|
|
* after the last character, so quit.
|
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
if (!empty_ok && match[0].rm_so == 0 && match[0].rm_eo == 0) {
|
1994-01-24 08:52:58 +03:00
|
|
|
empty_ok = 1;
|
|
|
|
if (len == 0)
|
|
|
|
goto endmatch;
|
|
|
|
BUILD(sp, s + offset, 1)
|
|
|
|
++offset;
|
|
|
|
--len;
|
|
|
|
goto nextmatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Confirm change. */
|
1994-08-17 20:35:35 +04:00
|
|
|
if (sp->c_suffix) {
|
1994-01-24 08:52:58 +03:00
|
|
|
/*
|
|
|
|
* Set the cursor position for confirmation. Note,
|
|
|
|
* if we matched on a '$', the cursor may be past
|
|
|
|
* the end of line.
|
|
|
|
*/
|
|
|
|
from.lno = to.lno = lno;
|
1994-08-17 20:35:35 +04:00
|
|
|
from.cno = match[0].rm_so + offset;
|
1996-05-20 07:47:00 +04:00
|
|
|
to.cno = match[0].rm_eo + offset;
|
|
|
|
/*
|
|
|
|
* Both ex and vi have to correct for a change before
|
|
|
|
* the first character in the line.
|
|
|
|
*/
|
1994-01-24 08:52:58 +03:00
|
|
|
if (llen == 0)
|
|
|
|
from.cno = to.cno = 0;
|
1996-05-20 07:47:00 +04:00
|
|
|
if (F_ISSET(sp, SC_VI)) {
|
|
|
|
/*
|
|
|
|
* Only vi has to correct for a change after
|
|
|
|
* the last character in the line.
|
|
|
|
*
|
|
|
|
* XXX
|
|
|
|
* It would be nice to change the vi code so
|
|
|
|
* that we could display a cursor past EOL.
|
|
|
|
*/
|
1994-01-24 08:52:58 +03:00
|
|
|
if (to.cno >= llen)
|
|
|
|
to.cno = llen - 1;
|
|
|
|
if (from.cno >= llen)
|
|
|
|
from.cno = llen - 1;
|
1996-05-20 07:47:00 +04:00
|
|
|
|
|
|
|
sp->lno = from.lno;
|
|
|
|
sp->cno = from.cno;
|
|
|
|
if (vs_refresh(sp, 1))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
vs_update(sp, msg_cat(sp,
|
|
|
|
"169|Confirm change? [n]", NULL), NULL);
|
|
|
|
|
|
|
|
if (v_event_get(sp, &ev, 0, 0))
|
|
|
|
goto err;
|
|
|
|
switch (ev.e_event) {
|
|
|
|
case E_CHARACTER:
|
|
|
|
break;
|
|
|
|
case E_EOF:
|
|
|
|
case E_ERR:
|
|
|
|
case E_INTERRUPT:
|
|
|
|
goto lquit;
|
|
|
|
default:
|
|
|
|
v_event_err(sp, &ev);
|
|
|
|
goto lquit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ex_print(sp, cmdp, &from, &to, 0) ||
|
|
|
|
ex_scprint(sp, &from, &to))
|
|
|
|
goto lquit;
|
|
|
|
if (ex_txt(sp, &tiq, 0, TXT_CR))
|
|
|
|
goto err;
|
|
|
|
ev.e_c = tiq.cqh_first->lb[0];
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
1996-05-20 07:47:00 +04:00
|
|
|
|
|
|
|
switch (ev.e_c) {
|
|
|
|
case CH_YES:
|
1994-01-24 08:52:58 +03:00
|
|
|
break;
|
1996-05-20 07:47:00 +04:00
|
|
|
default:
|
|
|
|
case CH_NO:
|
1994-01-24 08:52:58 +03:00
|
|
|
didsub = 0;
|
1994-08-17 20:35:35 +04:00
|
|
|
BUILD(sp, s +offset, match[0].rm_eo);
|
1994-01-24 08:52:58 +03:00
|
|
|
goto skip;
|
1996-05-20 07:47:00 +04:00
|
|
|
case CH_QUIT:
|
|
|
|
/* Set the quit/interrupted flags. */
|
|
|
|
lquit: quit = 1;
|
|
|
|
F_SET(sp->gp, G_INTERRUPTED);
|
1994-03-28 08:28:20 +04:00
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
/*
|
1996-05-20 07:47:00 +04:00
|
|
|
* Resolve any changes, then return to (and
|
|
|
|
* exit from) the main loop.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
goto endmatch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
/*
|
|
|
|
* Set the cursor to the last position changed, converting
|
|
|
|
* from 1-based to 0-based.
|
|
|
|
*/
|
|
|
|
sp->lno = lno;
|
|
|
|
sp->cno = match[0].rm_so;
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
/* Copy the bytes before the match into the build buffer. */
|
1994-08-17 20:35:35 +04:00
|
|
|
BUILD(sp, s + offset, match[0].rm_so);
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/* Substitute the matching bytes. */
|
|
|
|
didsub = 1;
|
1996-05-20 07:47:00 +04:00
|
|
|
if (re_sub(sp, s + offset, &lb, &lbclen, &lblen, match))
|
|
|
|
goto err;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/* Set the change flag so we know this line was modified. */
|
|
|
|
linechanged = 1;
|
|
|
|
|
|
|
|
/* Move past the matched bytes. */
|
1994-08-17 20:35:35 +04:00
|
|
|
skip: offset += match[0].rm_eo;
|
|
|
|
len -= match[0].rm_eo;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/* A match cannot be followed by an empty pattern. */
|
|
|
|
empty_ok = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If doing a global change with confirmation, we have to
|
|
|
|
* update the screen. The basic idea is to store the line
|
|
|
|
* so the screen update routines can find it, and restart.
|
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
if (didsub && sp->c_suffix && sp->g_suffix) {
|
1994-01-24 08:52:58 +03:00
|
|
|
/*
|
|
|
|
* The new search offset will be the end of the
|
|
|
|
* modified line.
|
|
|
|
*/
|
|
|
|
saved_offset = lbclen;
|
|
|
|
|
|
|
|
/* Copy the rest of the line. */
|
|
|
|
if (len)
|
|
|
|
BUILD(sp, s + offset, len)
|
|
|
|
|
|
|
|
/* Set the new offset. */
|
|
|
|
offset = saved_offset;
|
|
|
|
|
|
|
|
/* Store inserted lines, adjusting the build buffer. */
|
|
|
|
last = 0;
|
|
|
|
if (sp->newl_cnt) {
|
|
|
|
for (cnt = 0;
|
|
|
|
cnt < sp->newl_cnt; ++cnt, ++lno, ++elno) {
|
1996-05-20 07:47:00 +04:00
|
|
|
if (db_insert(sp, lno,
|
1994-01-24 08:52:58 +03:00
|
|
|
lb + last, sp->newl[cnt] - last))
|
1996-05-20 07:47:00 +04:00
|
|
|
goto err;
|
1994-01-24 08:52:58 +03:00
|
|
|
last = sp->newl[cnt] + 1;
|
|
|
|
++sp->rptlines[L_ADDED];
|
|
|
|
}
|
|
|
|
lbclen -= last;
|
|
|
|
offset -= last;
|
|
|
|
sp->newl_cnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store and retrieve the line. */
|
1996-05-20 07:47:00 +04:00
|
|
|
if (db_set(sp, lno, lb + last, lbclen))
|
|
|
|
goto err;
|
|
|
|
if (db_get(sp, lno, DBG_FATAL, &s, &llen))
|
|
|
|
goto err;
|
1994-01-24 08:52:58 +03:00
|
|
|
ADD_SPACE_RET(sp, bp, blen, llen)
|
|
|
|
memmove(bp, s, llen);
|
|
|
|
s = bp;
|
|
|
|
len = llen - offset;
|
|
|
|
|
|
|
|
/* Restart the build. */
|
|
|
|
lbclen = 0;
|
|
|
|
BUILD(sp, s, offset);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we haven't already done the after-the-string
|
|
|
|
* match, do one. Set REG_NOTEOL so the '$' pattern
|
|
|
|
* only matches once.
|
|
|
|
*/
|
|
|
|
if (!do_eol_match)
|
|
|
|
goto endmatch;
|
|
|
|
if (offset == len) {
|
|
|
|
do_eol_match = 0;
|
|
|
|
eflags |= REG_NOTEOL;
|
|
|
|
}
|
|
|
|
goto nextmatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's a global:
|
|
|
|
*
|
|
|
|
* If at the end of the string, do a test for the after
|
|
|
|
* the string match. Set REG_NOTEOL so the '$' pattern
|
|
|
|
* only matches once.
|
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
if (sp->g_suffix && do_eol_match) {
|
1994-01-24 08:52:58 +03:00
|
|
|
if (len == 0) {
|
|
|
|
do_eol_match = 0;
|
|
|
|
eflags |= REG_NOTEOL;
|
|
|
|
}
|
|
|
|
goto nextmatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
endmatch: if (!linechanged)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Copy any remaining bytes into the build buffer. */
|
|
|
|
if (len)
|
|
|
|
BUILD(sp, s + offset, len)
|
|
|
|
|
|
|
|
/* Store inserted lines, adjusting the build buffer. */
|
|
|
|
last = 0;
|
|
|
|
if (sp->newl_cnt) {
|
|
|
|
for (cnt = 0;
|
|
|
|
cnt < sp->newl_cnt; ++cnt, ++lno, ++elno) {
|
1996-05-20 07:47:00 +04:00
|
|
|
if (db_insert(sp,
|
1994-01-24 08:52:58 +03:00
|
|
|
lno, lb + last, sp->newl[cnt] - last))
|
1996-05-20 07:47:00 +04:00
|
|
|
goto err;
|
1994-01-24 08:52:58 +03:00
|
|
|
last = sp->newl[cnt] + 1;
|
|
|
|
++sp->rptlines[L_ADDED];
|
|
|
|
}
|
|
|
|
lbclen -= last;
|
|
|
|
sp->newl_cnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store the changed line. */
|
1996-05-20 07:47:00 +04:00
|
|
|
if (db_set(sp, lno, lb + last, lbclen))
|
|
|
|
goto err;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/* Update changed line counter. */
|
1994-08-17 20:35:35 +04:00
|
|
|
if (sp->rptlchange != lno) {
|
|
|
|
sp->rptlchange = lno;
|
|
|
|
++sp->rptlines[L_CHANGED];
|
|
|
|
}
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* Display as necessary. Historic practice is to only
|
|
|
|
* display the last line of a line split into multiple
|
|
|
|
* lines.
|
|
|
|
*/
|
1994-01-24 08:52:58 +03:00
|
|
|
if (lflag || nflag || pflag) {
|
|
|
|
from.lno = to.lno = lno;
|
|
|
|
from.cno = to.cno = 0;
|
|
|
|
if (lflag)
|
1996-05-20 07:47:00 +04:00
|
|
|
(void)ex_print(sp, cmdp, &from, &to, E_C_LIST);
|
1994-01-24 08:52:58 +03:00
|
|
|
if (nflag)
|
1996-05-20 07:47:00 +04:00
|
|
|
(void)ex_print(sp, cmdp, &from, &to, E_C_HASH);
|
1994-01-24 08:52:58 +03:00
|
|
|
if (pflag)
|
1996-05-20 07:47:00 +04:00
|
|
|
(void)ex_print(sp, cmdp, &from, &to, E_C_PRINT);
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
1994-08-17 20:35:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !!!
|
1996-05-20 07:47:00 +04:00
|
|
|
* Historically, vi attempted to leave the cursor at the same place if
|
|
|
|
* the substitution was done at the current cursor position. Otherwise
|
|
|
|
* it moved it to the first non-blank of the last line changed. There
|
|
|
|
* were some problems: for example, :s/$/foo/ with the cursor on the
|
|
|
|
* last character of the line left the cursor on the last character, or
|
|
|
|
* the & command with multiple occurrences of the matching string in the
|
|
|
|
* line usually left the cursor in a fairly random position.
|
1994-08-17 20:35:35 +04:00
|
|
|
*
|
1996-05-20 07:47:00 +04:00
|
|
|
* We try to do the same thing, with the exception that if the user is
|
|
|
|
* doing substitution with confirmation, we move to the last line about
|
|
|
|
* which the user was consulted, as opposed to the last line that they
|
|
|
|
* actually changed. This prevents a screen flash if the user doesn't
|
|
|
|
* change many of the possible lines.
|
1994-08-17 20:35:35 +04:00
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
if (!sp->c_suffix && (sp->lno != slno || sp->cno != scno)) {
|
1994-08-17 20:35:35 +04:00
|
|
|
sp->cno = 0;
|
1996-05-20 07:47:00 +04:00
|
|
|
(void)nonblank(sp, sp->lno, &sp->cno);
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If not in a global command, and nothing matched, say so.
|
|
|
|
* Else, if none of the lines displayed, put something up.
|
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
rval = 0;
|
1994-01-24 08:52:58 +03:00
|
|
|
if (!matched) {
|
1996-05-20 07:47:00 +04:00
|
|
|
if (!F_ISSET(sp, SC_EX_GLOBAL)) {
|
|
|
|
msgq(sp, M_ERR, "157|No match found");
|
|
|
|
goto err;
|
|
|
|
}
|
1994-01-24 08:52:58 +03:00
|
|
|
} else if (!lflag && !nflag && !pflag)
|
1996-05-20 07:47:00 +04:00
|
|
|
F_SET(cmdp, E_AUTOPRINT);
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
if (0) {
|
1996-05-20 07:47:00 +04:00
|
|
|
err: rval = 1;
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bp != NULL)
|
|
|
|
FREE_SPACE(sp, bp, blen);
|
1994-08-17 20:35:35 +04:00
|
|
|
if (lb != NULL)
|
|
|
|
free(lb);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-05-20 07:47:00 +04:00
|
|
|
* re_compile --
|
|
|
|
* Compile the RE.
|
|
|
|
*
|
|
|
|
* PUBLIC: int re_compile __P((SCR *,
|
|
|
|
* PUBLIC: char *, char **, size_t *, regex_t *, u_int));
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
re_compile(sp, ptrn, ptrnp, lenp, rep, flags)
|
|
|
|
SCR *sp;
|
|
|
|
char *ptrn, **ptrnp;
|
|
|
|
size_t *lenp;
|
|
|
|
regex_t *rep;
|
|
|
|
u_int flags;
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
int reflags, replaced, rval;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* Set RE flags. */
|
|
|
|
reflags = 0;
|
|
|
|
if (!LF_ISSET(RE_C_CSCOPE | RE_C_TAG)) {
|
|
|
|
if (O_ISSET(sp, O_EXTENDED))
|
|
|
|
reflags |= REG_EXTENDED;
|
|
|
|
if (O_ISSET(sp, O_IGNORECASE))
|
|
|
|
reflags |= REG_ICASE;
|
|
|
|
if (O_ISSET(sp, O_ICLOWER)) {
|
|
|
|
for (p = ptrn; *p != '\0'; ++p)
|
|
|
|
if (isupper(*p))
|
|
|
|
break;
|
|
|
|
if (*p == '\0')
|
|
|
|
reflags |= REG_ICASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're replacing a saved value, clear the old one. */
|
|
|
|
if (LF_ISSET(RE_C_SEARCH) && F_ISSET(sp, SC_RE_SEARCH)) {
|
|
|
|
regfree(&sp->re_c);
|
|
|
|
F_CLR(sp, SC_RE_SEARCH);
|
|
|
|
}
|
|
|
|
if (LF_ISSET(RE_C_SUBST) && F_ISSET(sp, SC_RE_SUBST)) {
|
|
|
|
regfree(&sp->subre_c);
|
|
|
|
F_CLR(sp, SC_RE_SUBST);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're saving the string, it's a pattern we haven't seen before,
|
|
|
|
* so convert the vi-style RE's to POSIX 1003.2 RE's. Save a copy for
|
|
|
|
* later recompilation. Free any previously saved value.
|
|
|
|
*/
|
|
|
|
replaced = 0;
|
|
|
|
if (ptrnp != NULL) {
|
|
|
|
if (*ptrnp != NULL) {
|
|
|
|
free(*ptrnp);
|
|
|
|
*ptrnp = NULL;
|
|
|
|
}
|
|
|
|
if (LF_ISSET(RE_C_CSCOPE)) {
|
|
|
|
if (re_cscope_conv(sp, &ptrn, &replaced))
|
|
|
|
return (1);
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* Currently, the match-any-<blank> expression used in
|
|
|
|
* re_cscope_conv() requires extended RE's. This may
|
|
|
|
* not be right or safe.
|
|
|
|
*/
|
|
|
|
reflags |= REG_EXTENDED;
|
|
|
|
} else if (LF_ISSET(RE_C_TAG)) {
|
|
|
|
if (re_tag_conv(sp, &ptrn, &replaced))
|
|
|
|
return (1);
|
|
|
|
} else
|
|
|
|
if (re_conv(sp, &ptrn, &replaced))
|
|
|
|
return (1);
|
|
|
|
len = strlen(ptrn);
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = len;
|
|
|
|
if ((*ptrnp = v_strdup(sp, ptrn, len)) == NULL)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
rval = regcomp(rep, ptrn, reflags);
|
|
|
|
|
|
|
|
/* Free up any allocated memory. */
|
|
|
|
if (replaced)
|
|
|
|
FREE_SPACE(sp, ptrn, 0);
|
|
|
|
|
|
|
|
if (rval) {
|
|
|
|
if (!LF_ISSET(RE_C_SILENT))
|
|
|
|
re_error(sp, rval, rep);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LF_ISSET(RE_C_SEARCH))
|
|
|
|
F_SET(sp, SC_RE_SEARCH);
|
|
|
|
if (LF_ISSET(RE_C_SUBST))
|
|
|
|
F_SET(sp, SC_RE_SUBST);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* re_conv --
|
|
|
|
* Convert vi's regular expressions into something that the
|
|
|
|
* the POSIX 1003.2 RE functions can handle.
|
|
|
|
*
|
|
|
|
* There are three conversions we make to make vi's RE's (specifically
|
|
|
|
* the global, search, and substitute patterns) work with POSIX RE's.
|
|
|
|
*
|
|
|
|
* 1: If O_MAGIC is not set, strip backslashes from the magic character
|
|
|
|
* set (.[*~) that have them, and add them to the ones that don't.
|
|
|
|
* 2: If O_MAGIC is not set, the string "\~" is replaced with the text
|
|
|
|
* from the last substitute command's replacement string. If O_MAGIC
|
|
|
|
* is set, it's the string "~".
|
|
|
|
* 3: The pattern \<ptrn\> does "word" searches, convert it to use the
|
|
|
|
* new RE escapes.
|
|
|
|
*
|
|
|
|
* !!!/XXX
|
|
|
|
* This doesn't exactly match the historic behavior of vi because we do
|
|
|
|
* the ~ substitution before calling the RE engine, so magic characters
|
|
|
|
* in the replacement string will be expanded by the RE engine, and they
|
|
|
|
* weren't historically. It's a bug.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
re_conv(sp, ptrnp, replacedp)
|
|
|
|
SCR *sp;
|
|
|
|
char **ptrnp;
|
|
|
|
int *replacedp;
|
|
|
|
{
|
|
|
|
size_t blen, needlen;
|
|
|
|
int magic;
|
|
|
|
char *bp, *p, *t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First pass through, we figure out how much space we'll need.
|
|
|
|
* We do it in two passes, on the grounds that most of the time
|
|
|
|
* the user is doing a search and won't have magic characters.
|
|
|
|
* That way we can skip the malloc and memmove's.
|
|
|
|
*/
|
|
|
|
for (p = *ptrnp, magic = 0, needlen = 0; *p != '\0'; ++p)
|
|
|
|
switch (*p) {
|
|
|
|
case '\\':
|
|
|
|
switch (*++p) {
|
|
|
|
case '<':
|
|
|
|
magic = 1;
|
|
|
|
needlen += sizeof(RE_WSTART);
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
magic = 1;
|
|
|
|
needlen += sizeof(RE_WSTOP);
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
if (!O_ISSET(sp, O_MAGIC)) {
|
|
|
|
magic = 1;
|
|
|
|
needlen += sp->repl_len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
case '[':
|
|
|
|
case '*':
|
|
|
|
if (!O_ISSET(sp, O_MAGIC)) {
|
|
|
|
magic = 1;
|
|
|
|
needlen += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
needlen += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
if (O_ISSET(sp, O_MAGIC)) {
|
|
|
|
magic = 1;
|
|
|
|
needlen += sp->repl_len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
case '[':
|
|
|
|
case '*':
|
|
|
|
if (!O_ISSET(sp, O_MAGIC)) {
|
|
|
|
magic = 1;
|
|
|
|
needlen += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
needlen += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!magic) {
|
|
|
|
*replacedp = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get enough memory to hold the final pattern.
|
|
|
|
*
|
|
|
|
* XXX
|
|
|
|
* It's nul-terminated, for now.
|
|
|
|
*/
|
|
|
|
GET_SPACE_RET(sp, bp, blen, needlen + 1);
|
|
|
|
|
|
|
|
for (p = *ptrnp, t = bp; *p != '\0'; ++p)
|
|
|
|
switch (*p) {
|
|
|
|
case '\\':
|
|
|
|
switch (*++p) {
|
|
|
|
case '<':
|
|
|
|
memmove(t, RE_WSTART, sizeof(RE_WSTART) - 1);
|
|
|
|
t += sizeof(RE_WSTART) - 1;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
memmove(t, RE_WSTOP, sizeof(RE_WSTOP) - 1);
|
|
|
|
t += sizeof(RE_WSTOP) - 1;
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
if (O_ISSET(sp, O_MAGIC))
|
|
|
|
*t++ = '~';
|
|
|
|
else {
|
|
|
|
memmove(t, sp->repl, sp->repl_len);
|
|
|
|
t += sp->repl_len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
case '[':
|
|
|
|
case '*':
|
|
|
|
if (O_ISSET(sp, O_MAGIC))
|
|
|
|
*t++ = '\\';
|
|
|
|
*t++ = *p;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*t++ = '\\';
|
|
|
|
*t++ = *p;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
if (O_ISSET(sp, O_MAGIC)) {
|
|
|
|
memmove(t, sp->repl, sp->repl_len);
|
|
|
|
t += sp->repl_len;
|
|
|
|
} else
|
|
|
|
*t++ = '~';
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
case '[':
|
|
|
|
case '*':
|
|
|
|
if (!O_ISSET(sp, O_MAGIC))
|
|
|
|
*t++ = '\\';
|
|
|
|
*t++ = *p;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*t++ = *p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*t = '\0';
|
|
|
|
|
|
|
|
*ptrnp = bp;
|
|
|
|
*replacedp = 1;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* re_tag_conv --
|
|
|
|
* Convert a tags search path into something that the POSIX
|
|
|
|
* 1003.2 RE functions can handle.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
re_tag_conv(sp, ptrnp, replacedp)
|
|
|
|
SCR *sp;
|
|
|
|
char **ptrnp;
|
|
|
|
int *replacedp;
|
|
|
|
{
|
|
|
|
size_t blen, len;
|
|
|
|
int lastdollar;
|
|
|
|
char *bp, *p, *t;
|
|
|
|
|
|
|
|
*replacedp = 0;
|
|
|
|
|
|
|
|
len = strlen(p = *ptrnp);
|
|
|
|
|
|
|
|
/* Max memory usage is 2 times the length of the string. */
|
|
|
|
GET_SPACE_RET(sp, bp, blen, len * 2);
|
|
|
|
|
|
|
|
t = bp;
|
|
|
|
|
|
|
|
/* The last character is a '/' or '?', we just strip it. */
|
|
|
|
if (p[len - 1] == '/' || p[len - 1] == '?')
|
|
|
|
p[len - 1] = '\0';
|
|
|
|
|
|
|
|
/* The next-to-last character is a '$', and it's magic. */
|
|
|
|
if (p[len - 2] == '$') {
|
|
|
|
lastdollar = 1;
|
|
|
|
p[len - 2] = '\0';
|
|
|
|
} else
|
|
|
|
lastdollar = 0;
|
|
|
|
|
|
|
|
/* The first character is a '/' or '?', we just strip it. */
|
|
|
|
if (p[0] == '/' || p[0] == '?')
|
|
|
|
++p;
|
|
|
|
|
|
|
|
/* The second character is a '^', and it's magic. */
|
|
|
|
if (p[0] == '^')
|
|
|
|
*t++ = *p++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Escape every other magic character we can find, stripping the
|
|
|
|
* backslashes ctags inserts to escape the search delimiter
|
|
|
|
* characters.
|
|
|
|
*/
|
|
|
|
while (p[0]) {
|
|
|
|
/* Ctags escapes the search delimiter characters. */
|
|
|
|
if (p[0] == '\\' && (p[1] == '/' || p[1] == '?'))
|
|
|
|
++p;
|
|
|
|
else if (strchr("^.[]$*", p[0]))
|
|
|
|
*t++ = '\\';
|
|
|
|
*t++ = *p++;
|
|
|
|
}
|
|
|
|
if (lastdollar)
|
|
|
|
*t++ = '$';
|
|
|
|
*t++ = '\0';
|
|
|
|
|
|
|
|
*ptrnp = bp;
|
|
|
|
*replacedp = 1;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* re_cscope_conv --
|
|
|
|
* Convert a cscope search path into something that the POSIX
|
|
|
|
* 1003.2 RE functions can handle.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
re_cscope_conv(sp, ptrnp, replacedp)
|
|
|
|
SCR *sp;
|
|
|
|
char **ptrnp;
|
|
|
|
int *replacedp;
|
|
|
|
{
|
|
|
|
size_t blen, len, nspaces;
|
|
|
|
char *bp, *p, *re;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each space in the source line printed by cscope represents an
|
|
|
|
* arbitrary sequence of spaces, tabs, and comments.
|
|
|
|
*/
|
|
|
|
#define CSCOPE_RE_SPACE "([ \t]|/\\*([^*]|\\*/)*\\*/)*"
|
|
|
|
for (nspaces = 0, p = *ptrnp; *p != '\0'; ++p)
|
|
|
|
if (*p == ' ')
|
|
|
|
++nspaces;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate plenty of space:
|
|
|
|
* the string, plus potential escaping characters
|
|
|
|
* nspaces + 2 copies of CSCOPE_RE_SPACE
|
|
|
|
* ^, $, nul terminator characters
|
|
|
|
*/
|
|
|
|
len = (p - *ptrnp) * 2 + (nspaces + 2) * sizeof(CSCOPE_RE_SPACE) + 3;
|
|
|
|
GET_SPACE_RET(sp, bp, blen, len);
|
|
|
|
|
|
|
|
p = bp;
|
|
|
|
*p++ = '^';
|
|
|
|
memcpy(p, CSCOPE_RE_SPACE, sizeof(CSCOPE_RE_SPACE) - 1);
|
|
|
|
p += sizeof(CSCOPE_RE_SPACE) - 1;
|
|
|
|
|
|
|
|
for (re = *ptrnp; *re != '\0'; ++re)
|
|
|
|
if (*re == ' ') {
|
|
|
|
memcpy(p, CSCOPE_RE_SPACE, sizeof(CSCOPE_RE_SPACE) - 1);
|
|
|
|
p += sizeof(CSCOPE_RE_SPACE) - 1;
|
|
|
|
} else {
|
|
|
|
if (strchr("\\^.[]$*", *re))
|
|
|
|
*p++ = '\\';
|
|
|
|
*p++ = *re;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(p, CSCOPE_RE_SPACE, sizeof(CSCOPE_RE_SPACE) - 1);
|
|
|
|
p += sizeof(CSCOPE_RE_SPACE) - 1;
|
|
|
|
*p++ = '$';
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
*ptrnp = bp;
|
|
|
|
*replacedp = 1;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* re_error --
|
|
|
|
* Report a regular expression error.
|
|
|
|
*
|
|
|
|
* PUBLIC: void re_error __P((SCR *, int, regex_t *));
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
re_error(sp, errcode, preg)
|
|
|
|
SCR *sp;
|
|
|
|
int errcode;
|
|
|
|
regex_t *preg;
|
|
|
|
{
|
|
|
|
size_t s;
|
|
|
|
char *oe;
|
|
|
|
|
|
|
|
s = regerror(errcode, preg, "", 0);
|
|
|
|
if ((oe = malloc(s)) == NULL)
|
|
|
|
msgq(sp, M_SYSERR, NULL);
|
|
|
|
else {
|
|
|
|
(void)regerror(errcode, preg, oe, s);
|
|
|
|
msgq(sp, M_ERR, "RE error: %s", oe);
|
|
|
|
free(oe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* re_sub --
|
1994-01-24 08:52:58 +03:00
|
|
|
* Do the substitution for a regular expression.
|
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
static int
|
|
|
|
re_sub(sp, ip, lbp, lbclenp, lblenp, match)
|
1994-01-24 08:52:58 +03:00
|
|
|
SCR *sp;
|
|
|
|
char *ip; /* Input line. */
|
|
|
|
char **lbp;
|
|
|
|
size_t *lbclenp, *lblenp;
|
1994-08-17 20:35:35 +04:00
|
|
|
regmatch_t match[10];
|
1994-01-24 08:52:58 +03:00
|
|
|
{
|
|
|
|
enum { C_NOTSET, C_LOWER, C_ONELOWER, C_ONEUPPER, C_UPPER } conv;
|
|
|
|
size_t lbclen, lblen; /* Local copies. */
|
|
|
|
size_t mlen; /* Match length. */
|
|
|
|
size_t rpl; /* Remaining replacement length. */
|
|
|
|
char *rp; /* Replacement pointer. */
|
|
|
|
int ch;
|
|
|
|
int no; /* Match replacement offset. */
|
|
|
|
char *p, *t; /* Buffer pointers. */
|
|
|
|
char *lb; /* Local copies. */
|
|
|
|
|
|
|
|
lb = *lbp; /* Get local copies. */
|
|
|
|
lbclen = *lbclenp;
|
|
|
|
lblen = *lblenp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* QUOTING NOTE:
|
|
|
|
*
|
|
|
|
* There are some special sequences that vi provides in the
|
|
|
|
* replacement patterns.
|
|
|
|
* & string the RE matched (\& if nomagic set)
|
1994-03-28 08:28:20 +04:00
|
|
|
* \# n-th regular subexpression
|
1994-01-24 08:52:58 +03:00
|
|
|
* \E end \U, \L conversion
|
|
|
|
* \e end \U, \L conversion
|
|
|
|
* \l convert the next character to lower-case
|
|
|
|
* \L convert to lower-case, until \E, \e, or end of replacement
|
|
|
|
* \u convert the next character to upper-case
|
|
|
|
* \U convert to upper-case, until \E, \e, or end of replacement
|
|
|
|
*
|
|
|
|
* Otherwise, since this is the lowest level of replacement, discard
|
|
|
|
* all escape characters. This (hopefully) follows historic practice.
|
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
#define OUTCH(ch) { \
|
1994-01-24 08:52:58 +03:00
|
|
|
CHAR_T __ch = (ch); \
|
1994-08-17 20:35:35 +04:00
|
|
|
u_int __value = KEY_VAL(sp, __ch); \
|
1994-01-24 08:52:58 +03:00
|
|
|
if (__value == K_CR || __value == K_NL) { \
|
|
|
|
NEEDNEWLINE(sp); \
|
|
|
|
sp->newl[sp->newl_cnt++] = lbclen; \
|
|
|
|
} else if (conv != C_NOTSET) { \
|
|
|
|
switch (conv) { \
|
|
|
|
case C_ONELOWER: \
|
|
|
|
conv = C_NOTSET; \
|
|
|
|
/* FALLTHROUGH */ \
|
|
|
|
case C_LOWER: \
|
|
|
|
if (isupper(__ch)) \
|
|
|
|
__ch = tolower(__ch); \
|
|
|
|
break; \
|
|
|
|
case C_ONEUPPER: \
|
|
|
|
conv = C_NOTSET; \
|
|
|
|
/* FALLTHROUGH */ \
|
|
|
|
case C_UPPER: \
|
|
|
|
if (islower(__ch)) \
|
|
|
|
__ch = toupper(__ch); \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
NEEDSP(sp, 1, p); \
|
|
|
|
*p++ = __ch; \
|
|
|
|
++lbclen; \
|
|
|
|
}
|
|
|
|
conv = C_NOTSET;
|
|
|
|
for (rp = sp->repl, rpl = sp->repl_len, p = lb + lbclen; rpl--;) {
|
|
|
|
switch (ch = *rp++) {
|
|
|
|
case '&':
|
|
|
|
if (O_ISSET(sp, O_MAGIC)) {
|
|
|
|
no = 0;
|
|
|
|
goto subzero;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
if (rpl == 0)
|
|
|
|
break;
|
|
|
|
--rpl;
|
|
|
|
switch (ch = *rp) {
|
|
|
|
case '&':
|
1994-08-17 20:35:35 +04:00
|
|
|
++rp;
|
1994-01-24 08:52:58 +03:00
|
|
|
if (!O_ISSET(sp, O_MAGIC)) {
|
|
|
|
no = 0;
|
|
|
|
goto subzero;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
no = *rp++ - '0';
|
1994-08-17 20:35:35 +04:00
|
|
|
subzero: if (match[no].rm_so == -1 ||
|
|
|
|
match[no].rm_eo == -1)
|
|
|
|
break;
|
|
|
|
mlen = match[no].rm_eo - match[no].rm_so;
|
|
|
|
for (t = ip + match[no].rm_so; mlen--; ++t)
|
1996-05-20 07:47:00 +04:00
|
|
|
OUTCH(*t);
|
1994-01-24 08:52:58 +03:00
|
|
|
continue;
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
++rp;
|
|
|
|
conv = C_NOTSET;
|
|
|
|
continue;
|
|
|
|
case 'l':
|
|
|
|
++rp;
|
|
|
|
conv = C_ONELOWER;
|
|
|
|
continue;
|
|
|
|
case 'L':
|
|
|
|
++rp;
|
|
|
|
conv = C_LOWER;
|
|
|
|
continue;
|
|
|
|
case 'u':
|
|
|
|
++rp;
|
|
|
|
conv = C_ONEUPPER;
|
|
|
|
continue;
|
|
|
|
case 'U':
|
|
|
|
++rp;
|
|
|
|
conv = C_UPPER;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
++rp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1996-05-20 07:47:00 +04:00
|
|
|
OUTCH(ch);
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*lbp = lb; /* Update caller's information. */
|
|
|
|
*lbclenp = lbclen;
|
|
|
|
*lblenp = lblen;
|
|
|
|
return (0);
|
|
|
|
}
|