2013-05-20 01:16:08 -04:00
|
|
|
/* search.c -- implements search.h */
|
|
|
|
#include "search.h"
|
|
|
|
|
2010-11-14 21:10:03 -05:00
|
|
|
/* search.c
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
|
|
|
* The functions in this file implement commands that search in the forward
|
|
|
|
* and backward directions. There are no special characters in the search
|
|
|
|
* strings. Probably should have a regular expression search, or something
|
|
|
|
* like that.
|
|
|
|
*
|
|
|
|
* Aug. 1986 John M. Gamble:
|
|
|
|
* Made forward and reverse search use the same scan routine.
|
|
|
|
*
|
|
|
|
* Added a limited number of regular expressions - 'any',
|
|
|
|
* 'character class', 'closure', 'beginning of line', and
|
|
|
|
* 'end of line'.
|
|
|
|
*
|
|
|
|
* Replacement metacharacters will have to wait for a re-write of
|
|
|
|
* the replaces function, and a new variation of ldelete().
|
|
|
|
*
|
|
|
|
* For those curious as to my references, i made use of
|
|
|
|
* Kernighan & Plauger's "Software Tools."
|
|
|
|
* I deliberately did not look at any published grep or editor
|
|
|
|
* source (aside from this one) for inspiration. I did make use of
|
|
|
|
* Allen Hollub's bitmap routines as published in Doctor Dobb's Journal,
|
|
|
|
* June, 1985 and modified them for the limited needs of character class
|
|
|
|
* matching. Any inefficiences, bugs, stupid coding examples, etc.,
|
|
|
|
* are therefore my own responsibility.
|
|
|
|
*
|
|
|
|
* April 1987: John M. Gamble
|
|
|
|
* Deleted the "if (n == 0) n = 1;" statements in front of the
|
|
|
|
* search/hunt routines. Since we now use a do loop, these
|
|
|
|
* checks are unnecessary. Consolidated common code into the
|
|
|
|
* function delins(). Renamed global mclen matchlen,
|
|
|
|
* and added the globals matchline, matchoff, patmatch, and
|
|
|
|
* mlenold.
|
|
|
|
* This gave us the ability to unreplace regular expression searches,
|
|
|
|
* and to put the matched string into an evironment variable.
|
|
|
|
* SOON TO COME: Meta-replacement characters!
|
|
|
|
*
|
|
|
|
* 25-apr-87 DML
|
|
|
|
* - cleaned up an unneccessary if/else in forwsearch() and
|
|
|
|
* backsearch()
|
|
|
|
* - savematch() failed to malloc room for the terminating byte
|
|
|
|
* of the match string (stomp...stomp...). It does now. Also
|
|
|
|
* it now returns gracefully if malloc fails
|
|
|
|
*
|
|
|
|
* July 1987: John M. Gamble
|
|
|
|
* Set the variables matchlen and matchoff in the 'unreplace'
|
|
|
|
* section of replaces(). The function savematch() would
|
|
|
|
* get confused if you replaced, unreplaced, then replaced
|
|
|
|
* again (serves you right for being so wishy-washy...)
|
|
|
|
*
|
|
|
|
* August 1987: John M. Gamble
|
|
|
|
* Put in new function rmcstr() to create the replacement
|
|
|
|
* meta-character array. Modified delins() so that it knows
|
|
|
|
* whether or not to make use of the array. And, put in the
|
|
|
|
* appropriate new structures and variables.
|
|
|
|
*
|
2010-11-14 21:10:03 -05:00
|
|
|
* Modified by Petri Kutvonen
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
|
|
|
|
2010-11-14 21:10:03 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-05-28 03:10:16 -04:00
|
|
|
#include "basic.h"
|
2013-05-28 02:57:03 -04:00
|
|
|
#include "display.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "estruct.h"
|
|
|
|
#include "edef.h"
|
2013-05-28 02:46:44 -04:00
|
|
|
#include "input.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "line.h"
|
2013-09-18 02:25:39 -04:00
|
|
|
#include "log.h"
|
2005-10-01 01:52:45 -04:00
|
|
|
|
2010-12-05 15:35:55 -05:00
|
|
|
#if defined(MAGIC)
|
|
|
|
/*
|
|
|
|
* The variables magical and rmagical determine if there
|
|
|
|
* were actual metacharacters in the search and replace strings -
|
|
|
|
* if not, then we don't have to use the slower MAGIC mode
|
|
|
|
* search functions.
|
|
|
|
*/
|
|
|
|
static short int magical;
|
|
|
|
static short int rmagical;
|
|
|
|
static struct magic mcpat[NPAT]; /* The magic pattern. */
|
|
|
|
static struct magic tapcm[NPAT]; /* The reversed magic patterni. */
|
|
|
|
static struct magic_replacement rmcpat[NPAT]; /* The replacement magic array. */
|
|
|
|
#endif
|
|
|
|
|
2010-11-27 19:28:01 -05:00
|
|
|
static int amatch(struct magic *mcptr, int direct, struct line **pcwline, int *pcwoff);
|
2005-10-01 01:52:45 -04:00
|
|
|
static int readpattern(char *prompt, char *apat, int srch);
|
|
|
|
static int replaces(int kind, int f, int n);
|
2010-04-17 19:38:09 -04:00
|
|
|
static int nextch(struct line **pcurline, int *pcuroff, int dir);
|
2005-10-01 01:52:45 -04:00
|
|
|
static int mcstr(void);
|
|
|
|
static int rmcstr(void);
|
2010-11-27 19:28:01 -05:00
|
|
|
static int mceq(int bc, struct magic *mt);
|
|
|
|
static int cclmake(char **ppatptr, struct magic *mcptr);
|
2010-11-01 20:22:42 -04:00
|
|
|
static int biteq(int bc, char *cclmap);
|
|
|
|
static char *clearbits(void);
|
|
|
|
static void setbit(int bc, char *cclmap);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* forwsearch -- Search forward. Get a search string from the user, and
|
|
|
|
* search for the string. If found, reset the "." to be just after
|
|
|
|
* the match string, and (perhaps) repaint the display.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* int f, n; default flag / numeric argument
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int forwsearch(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int status = TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* If n is negative, search backwards.
|
|
|
|
* Otherwise proceed by asking for the search string.
|
|
|
|
*/
|
|
|
|
if (n < 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return backsearch(f, -n);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Ask the user for the text of a pattern. If the
|
|
|
|
* response is TRUE (responses other than FALSE are
|
|
|
|
* possible), search for the pattern for as long as
|
|
|
|
* n is positive (n == 0 will go through once, which
|
|
|
|
* is just fine).
|
|
|
|
*/
|
|
|
|
if ((status = readpattern("Search", &pat[0], TRUE)) == TRUE) {
|
|
|
|
do {
|
|
|
|
#if MAGIC
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((magical
|
|
|
|
&& curwp->w_bufp->b_mode & MDMAGIC) != 0)
|
|
|
|
status =
|
|
|
|
mcscanner(&mcpat[0], FORWARD, PTEND);
|
2005-05-31 11:50:56 -04:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
status = scanner(&pat[0], FORWARD, PTEND);
|
|
|
|
} while ((--n > 0) && status);
|
|
|
|
|
|
|
|
/* Save away the match, or complain
|
|
|
|
* if not there.
|
|
|
|
*/
|
|
|
|
if (status == TRUE)
|
|
|
|
savematch();
|
|
|
|
else
|
|
|
|
mlwrite("Not found");
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* forwhunt -- Search forward for a previously acquired search string.
|
|
|
|
* If found, reset the "." to be just after the match string,
|
|
|
|
* and (perhaps) repaint the display.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* int f, n; default flag / numeric argument
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int forwhunt(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int status = TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
if (n < 0) /* search backwards */
|
2010-08-29 06:03:55 -04:00
|
|
|
return backhunt(f, -n);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Make sure a pattern exists, or that we didn't switch
|
|
|
|
* into MAGIC mode until after we entered the pattern.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if (pat[0] == '\0') {
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("No pattern set");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#if MAGIC
|
|
|
|
if ((curwp->w_bufp->b_mode & MDMAGIC) != 0 &&
|
2005-09-30 18:26:09 -04:00
|
|
|
mcpat[0].mc_type == MCNIL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (!mcstr())
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Search for the pattern for as long as
|
|
|
|
* n is positive (n == 0 will go through once, which
|
|
|
|
* is just fine).
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
do {
|
2005-05-31 11:50:56 -04:00
|
|
|
#if MAGIC
|
|
|
|
if ((magical && curwp->w_bufp->b_mode & MDMAGIC) != 0)
|
|
|
|
status = mcscanner(&mcpat[0], FORWARD, PTEND);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
status = scanner(&pat[0], FORWARD, PTEND);
|
|
|
|
} while ((--n > 0) && status);
|
|
|
|
|
|
|
|
/* Save away the match, or complain
|
|
|
|
* if not there.
|
|
|
|
*/
|
|
|
|
if (status == TRUE)
|
|
|
|
savematch();
|
|
|
|
else
|
|
|
|
mlwrite("Not found");
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* backsearch -- Reverse search. Get a search string from the user, and
|
|
|
|
* search, starting at "." and proceeding toward the front of the buffer.
|
|
|
|
* If found "." is left pointing at the first character of the pattern
|
|
|
|
* (the last character that was matched).
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* int f, n; default flag / numeric argument
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int backsearch(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int status = TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* If n is negative, search forwards.
|
|
|
|
* Otherwise proceed by asking for the search string.
|
|
|
|
*/
|
|
|
|
if (n < 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return forwsearch(f, -n);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Ask the user for the text of a pattern. If the
|
|
|
|
* response is TRUE (responses other than FALSE are
|
|
|
|
* possible), search for the pattern for as long as
|
|
|
|
* n is positive (n == 0 will go through once, which
|
|
|
|
* is just fine).
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((status =
|
|
|
|
readpattern("Reverse search", &pat[0], TRUE)) == TRUE) {
|
2005-05-31 11:50:56 -04:00
|
|
|
do {
|
|
|
|
#if MAGIC
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((magical
|
|
|
|
&& curwp->w_bufp->b_mode & MDMAGIC) != 0)
|
|
|
|
status =
|
|
|
|
mcscanner(&tapcm[0], REVERSE, PTBEG);
|
2005-05-31 11:50:56 -04:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
status = scanner(&tap[0], REVERSE, PTBEG);
|
|
|
|
} while ((--n > 0) && status);
|
|
|
|
|
|
|
|
/* Save away the match, or complain
|
|
|
|
* if not there.
|
|
|
|
*/
|
|
|
|
if (status == TRUE)
|
|
|
|
savematch();
|
|
|
|
else
|
|
|
|
mlwrite("Not found");
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* backhunt -- Reverse search for a previously acquired search string,
|
|
|
|
* starting at "." and proceeding toward the front of the buffer.
|
|
|
|
* If found "." is left pointing at the first character of the pattern
|
|
|
|
* (the last character that was matched).
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* int f, n; default flag / numeric argument
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int backhunt(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int status = TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
if (n < 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return forwhunt(f, -n);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Make sure a pattern exists, or that we didn't switch
|
|
|
|
* into MAGIC mode until after we entered the pattern.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if (tap[0] == '\0') {
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("No pattern set");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#if MAGIC
|
|
|
|
if ((curwp->w_bufp->b_mode & MDMAGIC) != 0 &&
|
2005-09-30 18:26:09 -04:00
|
|
|
tapcm[0].mc_type == MCNIL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (!mcstr())
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Go search for it for as long as
|
|
|
|
* n is positive (n == 0 will go through once, which
|
|
|
|
* is just fine).
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
do {
|
2005-05-31 11:50:56 -04:00
|
|
|
#if MAGIC
|
|
|
|
if ((magical && curwp->w_bufp->b_mode & MDMAGIC) != 0)
|
|
|
|
status = mcscanner(&tapcm[0], REVERSE, PTBEG);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
status = scanner(&tap[0], REVERSE, PTBEG);
|
|
|
|
} while ((--n > 0) && status);
|
|
|
|
|
|
|
|
/* Save away the match, or complain
|
|
|
|
* if not there.
|
|
|
|
*/
|
|
|
|
if (status == TRUE)
|
|
|
|
savematch();
|
|
|
|
else
|
|
|
|
mlwrite("Not found");
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if MAGIC
|
|
|
|
/*
|
|
|
|
* mcscanner -- Search for a meta-pattern in either direction. If found,
|
|
|
|
* reset the "." to be at the start or just after the match string,
|
|
|
|
* and (perhaps) repaint the display.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2010-11-27 19:28:01 -05:00
|
|
|
* struct magic *mcpatrn; pointer into pattern
|
2005-10-01 01:52:45 -04:00
|
|
|
* int direct; which way to go.
|
|
|
|
* int beg_or_end; put point at beginning or end of pattern.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2010-11-27 19:28:01 -05:00
|
|
|
int mcscanner(struct magic *mcpatrn, int direct, int beg_or_end)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline; /* current line during scan */
|
2005-09-30 18:26:09 -04:00
|
|
|
int curoff; /* position within current line */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* If we are going in reverse, then the 'end' is actually
|
|
|
|
* the beginning of the pattern. Toggle it.
|
|
|
|
*/
|
|
|
|
beg_or_end ^= direct;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the old matchlen length, in case it is
|
|
|
|
* very different (closure) from the old length.
|
|
|
|
* This is important for query-replace undo
|
|
|
|
* command.
|
|
|
|
*/
|
|
|
|
mlenold = matchlen;
|
|
|
|
|
|
|
|
/* Setup local scan pointers to global ".".
|
|
|
|
*/
|
|
|
|
curline = curwp->w_dotp;
|
2005-09-30 18:26:09 -04:00
|
|
|
curoff = curwp->w_doto;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Scan each character until we hit the head link record.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
while (!boundry(curline, curoff, direct)) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* Save the current position in case we need to
|
|
|
|
* restore it on a match, and initialize matchlen to
|
|
|
|
* zero in case we are doing a search for replacement.
|
|
|
|
*/
|
|
|
|
matchline = curline;
|
|
|
|
matchoff = curoff;
|
|
|
|
matchlen = 0;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (amatch(mcpatrn, direct, &curline, &curoff)) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* A SUCCESSFULL MATCH!!!
|
|
|
|
* reset the global "." pointers.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if (beg_or_end == PTEND) { /* at end of string */
|
2005-05-31 11:50:56 -04:00
|
|
|
curwp->w_dotp = curline;
|
|
|
|
curwp->w_doto = curoff;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else { /* at beginning of string */
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
curwp->w_dotp = matchline;
|
|
|
|
curwp->w_doto = matchoff;
|
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_flag |= WFMOVE; /* flag that we have moved */
|
2005-05-31 11:50:56 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance the cursor.
|
|
|
|
*/
|
|
|
|
nextch(&curline, &curoff, direct);
|
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
return FALSE; /* We could not find a match. */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* amatch -- Search for a meta-pattern in either direction. Based on the
|
|
|
|
* recursive routine amatch() (for "anchored match") in
|
|
|
|
* Kernighan & Plauger's "Software Tools".
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2010-11-27 19:28:01 -05:00
|
|
|
* struct magic *mcptr; string to scan for
|
2005-10-01 01:52:45 -04:00
|
|
|
* int direct; which way to go.
|
2010-04-17 19:38:09 -04:00
|
|
|
* struct line **pcwline; current line during scan
|
2005-10-01 01:52:45 -04:00
|
|
|
* int *pcwoff; position within current line
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2010-11-27 19:28:01 -05:00
|
|
|
static int amatch(struct magic *mcptr, int direct, struct line **pcwline, int *pcwoff)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int c; /* character at current position */
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline; /* current line during scan */
|
2005-09-30 18:26:09 -04:00
|
|
|
int curoff; /* position within current line */
|
2005-05-31 11:50:56 -04:00
|
|
|
int nchars;
|
|
|
|
|
|
|
|
/* Set up local scan pointers to ".", and get
|
|
|
|
* the current character. Then loop around
|
|
|
|
* the pattern pointer until success or failure.
|
|
|
|
*/
|
|
|
|
curline = *pcwline;
|
|
|
|
curoff = *pcwoff;
|
|
|
|
|
|
|
|
/* The beginning-of-line and end-of-line metacharacters
|
|
|
|
* do not compare against characters, they compare
|
|
|
|
* against positions.
|
|
|
|
* BOL is guaranteed to be at the start of the pattern
|
|
|
|
* for forward searches, and at the end of the pattern
|
|
|
|
* for reverse searches. The reverse is true for EOL.
|
|
|
|
* So, for a start, we check for them on entry.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if (mcptr->mc_type == BOL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (curoff != 0)
|
|
|
|
return FALSE;
|
|
|
|
mcptr++;
|
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (mcptr->mc_type == EOL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (curoff != llength(curline))
|
|
|
|
return FALSE;
|
|
|
|
mcptr++;
|
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
while (mcptr->mc_type != MCNIL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
c = nextch(&curline, &curoff, direct);
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (mcptr->mc_type & CLOSURE) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* Try to match as many characters as possible
|
|
|
|
* against the current meta-character. A
|
|
|
|
* newline never matches a closure.
|
|
|
|
*/
|
|
|
|
nchars = 0;
|
2005-09-30 18:26:09 -04:00
|
|
|
while (c != '\n' && mceq(c, mcptr)) {
|
2005-05-31 11:50:56 -04:00
|
|
|
c = nextch(&curline, &curoff, direct);
|
|
|
|
nchars++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are now at the character that made us
|
|
|
|
* fail. Try to match the rest of the pattern.
|
|
|
|
* Shrink the closure by one for each failure.
|
|
|
|
* Since closure matches *zero* or more occurences
|
|
|
|
* of a pattern, a match may start even if the
|
|
|
|
* previous loop matched no characters.
|
|
|
|
*/
|
|
|
|
mcptr++;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
for (;;) {
|
|
|
|
c = nextch(&curline, &curoff,
|
|
|
|
direct ^ REVERSE);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (amatch
|
|
|
|
(mcptr, direct, &curline, &curoff)) {
|
2005-05-31 11:50:56 -04:00
|
|
|
matchlen += nchars;
|
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nchars-- == 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
} else { /* Not closure. */
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* The only way we'd get a BOL metacharacter
|
|
|
|
* at this point is at the end of the reversed pattern.
|
|
|
|
* The only way we'd get an EOL metacharacter
|
|
|
|
* here is at the end of a regular pattern.
|
|
|
|
* So if we match one or the other, and are at
|
|
|
|
* the appropriate position, we are guaranteed success
|
|
|
|
* (since the next pattern character has to be MCNIL).
|
|
|
|
* Before we report success, however, we back up by
|
|
|
|
* one character, so as to leave the cursor in the
|
|
|
|
* correct position. For example, a search for ")$"
|
|
|
|
* will leave the cursor at the end of the line, while
|
|
|
|
* a search for ")<NL>" will leave the cursor at the
|
|
|
|
* beginning of the next line. This follows the
|
|
|
|
* notion that the meta-character '$' (and likewise
|
|
|
|
* '^') match positions, not characters.
|
|
|
|
*/
|
2005-10-01 02:37:34 -04:00
|
|
|
if (mcptr->mc_type == BOL) {
|
2005-09-30 18:26:09 -04:00
|
|
|
if (curoff == llength(curline)) {
|
2005-05-31 11:50:56 -04:00
|
|
|
c = nextch(&curline, &curoff,
|
|
|
|
direct ^ REVERSE);
|
|
|
|
goto success;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
2005-05-31 11:50:56 -04:00
|
|
|
return FALSE;
|
2005-10-01 02:37:34 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-10-01 02:37:34 -04:00
|
|
|
if (mcptr->mc_type == EOL) {
|
2005-09-30 18:26:09 -04:00
|
|
|
if (curoff == 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
c = nextch(&curline, &curoff,
|
|
|
|
direct ^ REVERSE);
|
|
|
|
goto success;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
2005-05-31 11:50:56 -04:00
|
|
|
return FALSE;
|
2005-10-01 02:37:34 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Neither BOL nor EOL, so go through
|
|
|
|
* the meta-character equal function.
|
|
|
|
*/
|
|
|
|
if (!mceq(c, mcptr))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the length counter and
|
|
|
|
* advance the pattern pointer.
|
|
|
|
*/
|
|
|
|
matchlen++;
|
|
|
|
mcptr++;
|
2005-09-30 18:26:09 -04:00
|
|
|
} /* End of mcptr loop. */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* A SUCCESSFULL MATCH!!!
|
|
|
|
* Reset the "." pointers.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
success:
|
2005-05-31 11:50:56 -04:00
|
|
|
*pcwline = curline;
|
2005-09-30 18:26:09 -04:00
|
|
|
*pcwoff = curoff;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* scanner -- Search for a pattern in either direction. If found,
|
|
|
|
* reset the "." to be at the start or just after the match string,
|
|
|
|
* and (perhaps) repaint the display.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* unsigned char *patrn; string to scan for
|
|
|
|
* int direct; which way to go.
|
|
|
|
* int beg_or_end; put point at beginning or end of pattern.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 03:56:15 -04:00
|
|
|
int scanner(const char *patrn, int direct, int beg_or_end)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int c; /* character at current position */
|
2005-10-01 03:56:15 -04:00
|
|
|
const char *patptr; /* pointer into pattern */
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline; /* current line during scan */
|
2005-09-30 18:26:09 -04:00
|
|
|
int curoff; /* position within current line */
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *scanline; /* current line during scanning */
|
2005-09-30 18:26:09 -04:00
|
|
|
int scanoff; /* position in scanned line */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* If we are going in reverse, then the 'end' is actually
|
|
|
|
* the beginning of the pattern. Toggle it.
|
|
|
|
*/
|
|
|
|
beg_or_end ^= direct;
|
|
|
|
|
|
|
|
/* Set up local pointers to global ".".
|
|
|
|
*/
|
|
|
|
curline = curwp->w_dotp;
|
|
|
|
curoff = curwp->w_doto;
|
|
|
|
|
|
|
|
/* Scan each character until we hit the head link record.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
while (!boundry(curline, curoff, direct)) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* Save the current position in case we match
|
|
|
|
* the search string at this point.
|
|
|
|
*/
|
|
|
|
matchline = curline;
|
|
|
|
matchoff = curoff;
|
|
|
|
|
|
|
|
/* Get the character resolving newlines, and
|
|
|
|
* test it against first char in pattern.
|
|
|
|
*/
|
|
|
|
c = nextch(&curline, &curoff, direct);
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (eq(c, patrn[0])) { /* if we find it.. */
|
2005-05-31 11:50:56 -04:00
|
|
|
/* Setup scanning pointers.
|
|
|
|
*/
|
|
|
|
scanline = curline;
|
|
|
|
scanoff = curoff;
|
|
|
|
patptr = &patrn[0];
|
|
|
|
|
|
|
|
/* Scan through the pattern for a match.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
while (*++patptr != '\0') {
|
2005-05-31 11:50:56 -04:00
|
|
|
c = nextch(&scanline, &scanoff, direct);
|
|
|
|
|
|
|
|
if (!eq(c, *patptr))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A SUCCESSFULL MATCH!!!
|
|
|
|
* reset the global "." pointers
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if (beg_or_end == PTEND) { /* at end of string */
|
2005-05-31 11:50:56 -04:00
|
|
|
curwp->w_dotp = scanline;
|
|
|
|
curwp->w_doto = scanoff;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else { /* at beginning of string */
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
curwp->w_dotp = matchline;
|
|
|
|
curwp->w_doto = matchoff;
|
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_flag |= WFMOVE; /* Flag that we have moved. */
|
2005-05-31 11:50:56 -04:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
fail:; /* continue to search */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
return FALSE; /* We could not find a match */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* eq -- Compare two characters. The "bc" comes from the buffer, "pc"
|
|
|
|
* from the pattern. If we are not in EXACT mode, fold out the case.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int eq(unsigned char bc, unsigned char pc)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((curwp->w_bufp->b_mode & MDEXACT) == 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (islower(bc))
|
|
|
|
bc ^= DIFCASE;
|
|
|
|
|
|
|
|
if (islower(pc))
|
|
|
|
pc ^= DIFCASE;
|
|
|
|
}
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return bc == pc;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* readpattern -- Read a pattern. Stash it in apat. If it is the
|
|
|
|
* search string, create the reverse pattern and the magic
|
|
|
|
* pattern, assuming we are in MAGIC mode (and defined that way).
|
|
|
|
* Apat is not updated if the user types in an empty line. If
|
|
|
|
* the user typed an empty line, and there is no old pattern, it is
|
|
|
|
* an error. Display the old pattern, in the style of Jeff Lomicka.
|
|
|
|
* There is some do-it-yourself control expansion. Change to using
|
|
|
|
* <META> to delimit the end-of-pattern to allow <NL>s in the search
|
|
|
|
* string.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
static int readpattern(char *prompt, char *apat, int srch)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
int status;
|
2005-09-30 18:26:09 -04:00
|
|
|
char tpat[NPAT + 20];
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
strcpy(tpat, prompt); /* copy prompt to output string */
|
|
|
|
strcat(tpat, " ("); /* build new prompt string */
|
2005-09-30 18:26:09 -04:00
|
|
|
expandp(&apat[0], &tpat[strlen(tpat)], NPAT / 2); /* add old pattern */
|
2005-05-31 11:50:56 -04:00
|
|
|
strcat(tpat, ")<Meta>: ");
|
|
|
|
|
|
|
|
/* Read a pattern. Either we get one,
|
|
|
|
* or we just get the META charater, and use the previous pattern.
|
|
|
|
* Then, if it's the search string, make a reversed pattern.
|
|
|
|
* *Then*, make the meta-pattern, if we are defined that way.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((status = mlreplyt(tpat, tpat, NPAT, metac)) == TRUE) {
|
2005-05-31 11:50:56 -04:00
|
|
|
strcpy(apat, tpat);
|
2005-09-30 18:26:09 -04:00
|
|
|
if (srch) { /* If we are doing the search string. */
|
2005-05-31 11:50:56 -04:00
|
|
|
/* Reverse string copy, and remember
|
|
|
|
* the length for substitution purposes.
|
|
|
|
*/
|
|
|
|
rvstrcpy(tap, apat);
|
|
|
|
mlenold = matchlen = strlen(apat);
|
|
|
|
}
|
|
|
|
#if MAGIC
|
|
|
|
/* Only make the meta-pattern if in magic mode,
|
|
|
|
* since the pattern in question might have an
|
|
|
|
* invalid meta combination.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((curwp->w_bufp->b_mode & MDMAGIC) == 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
mcclear();
|
|
|
|
rmcclear();
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
|
|
|
status = srch ? mcstr() : rmcstr();
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
} else if (status == FALSE && apat[0] != 0) /* Old one */
|
2005-05-31 11:50:56 -04:00
|
|
|
status = TRUE;
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* savematch -- We found the pattern? Let's save it away.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
void savematch(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
char *ptr; /* pointer to last match string */
|
|
|
|
int j;
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline; /* line of last match */
|
2005-09-30 18:26:09 -04:00
|
|
|
int curoff; /* offset " " */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Free any existing match string, then
|
|
|
|
* attempt to allocate a new one.
|
|
|
|
*/
|
|
|
|
if (patmatch != NULL)
|
|
|
|
free(patmatch);
|
|
|
|
|
|
|
|
ptr = patmatch = malloc(matchlen + 1);
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (ptr != NULL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
curoff = matchoff;
|
|
|
|
curline = matchline;
|
|
|
|
|
|
|
|
for (j = 0; j < matchlen; j++)
|
|
|
|
*ptr++ = nextch(&curline, &curoff, FORWARD);
|
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rvstrcpy -- Reverse string copy.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
void rvstrcpy(char *rvstr, char *str)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int i;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
str += (i = strlen(str));
|
|
|
|
|
|
|
|
while (i-- > 0)
|
|
|
|
*rvstr++ = *--str;
|
|
|
|
|
|
|
|
*rvstr = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sreplace -- Search and replace.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* int f; default flag
|
|
|
|
* int n; # of repetitions wanted
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int sreplace(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-08-29 06:03:55 -04:00
|
|
|
return replaces(FALSE, f, n);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* qreplace -- search and replace with query.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* int f; default flag
|
|
|
|
* int n; # of repetitions wanted
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int qreplace(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-08-29 06:03:55 -04:00
|
|
|
return replaces(TRUE, f, n);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* replaces -- Search for a string and replace it with another
|
|
|
|
* string. Query might be enabled (according to kind).
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* int kind; Query enabled flag
|
|
|
|
* int f; default flag
|
|
|
|
* int n; # of repetitions wanted
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
static int replaces(int kind, int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int status; /* success flag on pattern inputs */
|
|
|
|
int rlength; /* length of replacement string */
|
|
|
|
int numsub; /* number of substitutions */
|
|
|
|
int nummatch; /* number of found matches */
|
2005-05-31 11:50:56 -04:00
|
|
|
int nlflag; /* last char of search string a <NL>? */
|
|
|
|
int nlrepl; /* was a replace done on the last line? */
|
|
|
|
char c; /* input char for query */
|
|
|
|
char tpat[NPAT]; /* temporary to hold search pattern */
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *origline; /* original "." position */
|
2005-05-31 11:50:56 -04:00
|
|
|
int origoff; /* and offset (for . query option) */
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lastline; /* position of last replace and */
|
2005-05-31 11:50:56 -04:00
|
|
|
int lastoff; /* offset (for 'u' query option) */
|
|
|
|
|
2013-04-30 22:06:08 -04:00
|
|
|
/* rfi */
|
|
|
|
lastline = NULL ;
|
|
|
|
lastoff = 0 ;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
|
2010-08-29 06:03:55 -04:00
|
|
|
return rdonly(); /* we are in read only mode */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Check for negative repetitions.
|
|
|
|
*/
|
|
|
|
if (f && n < 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Ask the user for the text of a pattern.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((status = readpattern((kind ==
|
|
|
|
FALSE ? "Replace" : "Query replace"),
|
|
|
|
&pat[0], TRUE))
|
|
|
|
!= TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Ask for the replacement string.
|
|
|
|
*/
|
|
|
|
if ((status = readpattern("with", &rpat[0], FALSE)) == ABORT)
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Find the length of the replacement string.
|
|
|
|
*/
|
|
|
|
rlength = strlen(&rpat[0]);
|
|
|
|
|
|
|
|
/* Set up flags so we can make sure not to do a recursive
|
|
|
|
* replace on the last line.
|
|
|
|
*/
|
|
|
|
nlflag = (pat[matchlen - 1] == '\n');
|
|
|
|
nlrepl = FALSE;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (kind) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* Build query replace question string.
|
|
|
|
*/
|
|
|
|
strcpy(tpat, "Replace '");
|
2005-09-30 18:26:09 -04:00
|
|
|
expandp(&pat[0], &tpat[strlen(tpat)], NPAT / 3);
|
2005-05-31 11:50:56 -04:00
|
|
|
strcat(tpat, "' with '");
|
2005-09-30 18:26:09 -04:00
|
|
|
expandp(&rpat[0], &tpat[strlen(tpat)], NPAT / 3);
|
2005-05-31 11:50:56 -04:00
|
|
|
strcat(tpat, "'? ");
|
|
|
|
|
|
|
|
/* Initialize last replaced pointers.
|
|
|
|
*/
|
|
|
|
lastline = NULL;
|
|
|
|
lastoff = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save original . position, init the number of matches and
|
|
|
|
* substitutions, and scan through the file.
|
|
|
|
*/
|
|
|
|
origline = curwp->w_dotp;
|
|
|
|
origoff = curwp->w_doto;
|
|
|
|
numsub = 0;
|
|
|
|
nummatch = 0;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
while ((f == FALSE || n > nummatch) &&
|
|
|
|
(nlflag == FALSE || nlrepl == FALSE)) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* Search for the pattern.
|
|
|
|
* If we search with a regular expression,
|
|
|
|
* matchlen is reset to the true length of
|
|
|
|
* the matched string.
|
|
|
|
*/
|
|
|
|
#if MAGIC
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((magical && curwp->w_bufp->b_mode & MDMAGIC) != 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (!mcscanner(&mcpat[0], FORWARD, PTBEG))
|
|
|
|
break;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
if (!scanner(&pat[0], FORWARD, PTBEG))
|
|
|
|
break; /* all done */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
++nummatch; /* Increment # of matches */
|
|
|
|
|
|
|
|
/* Check if we are on the last line.
|
|
|
|
*/
|
|
|
|
nlrepl = (lforw(curwp->w_dotp) == curwp->w_bufp->b_linep);
|
|
|
|
|
|
|
|
/* Check for query.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if (kind) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* Get the query.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
pprompt:mlwrite(&tpat[0], &pat[0],
|
|
|
|
&rpat[0]);
|
|
|
|
qprompt:
|
|
|
|
update(TRUE); /* show the proposed place to change */
|
|
|
|
c = tgetc(); /* and input */
|
|
|
|
mlwrite(""); /* and clear it */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* And respond appropriately.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
switch (c) {
|
2005-05-31 11:50:56 -04:00
|
|
|
#if PKCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
case 'Y':
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
case 'y': /* yes, substitute */
|
|
|
|
case ' ':
|
|
|
|
savematch();
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
#if PKCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
case 'N':
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
case 'n': /* no, onword */
|
|
|
|
forwchar(FALSE, 1);
|
|
|
|
continue;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
case '!': /* yes/stop asking */
|
|
|
|
kind = FALSE;
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
#if PKCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
case 'U':
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
case 'u': /* undo last and re-prompt */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* Restore old position.
|
|
|
|
*/
|
|
|
|
if (lastline == NULL) {
|
|
|
|
/* There is nothing to undo.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
TTbeep();
|
2005-05-31 11:50:56 -04:00
|
|
|
goto pprompt;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
curwp->w_dotp = lastline;
|
|
|
|
curwp->w_doto = lastoff;
|
|
|
|
lastline = NULL;
|
|
|
|
lastoff = 0;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* Delete the new string.
|
|
|
|
*/
|
|
|
|
backchar(FALSE, rlength);
|
|
|
|
#if PKCODE
|
|
|
|
matchline = curwp->w_dotp;
|
|
|
|
matchoff = curwp->w_doto;
|
|
|
|
#endif
|
|
|
|
status = delins(rlength, patmatch, FALSE);
|
|
|
|
if (status != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* Record one less substitution,
|
|
|
|
* backup, save our place, and
|
|
|
|
* reprompt.
|
|
|
|
*/
|
|
|
|
--numsub;
|
|
|
|
backchar(FALSE, mlenold);
|
|
|
|
matchline = curwp->w_dotp;
|
|
|
|
matchoff = curwp->w_doto;
|
|
|
|
goto pprompt;
|
|
|
|
|
|
|
|
case '.': /* abort! and return */
|
|
|
|
/* restore old position */
|
|
|
|
curwp->w_dotp = origline;
|
|
|
|
curwp->w_doto = origoff;
|
|
|
|
curwp->w_flag |= WFMOVE;
|
|
|
|
|
|
|
|
case BELL: /* abort! and stay */
|
|
|
|
mlwrite("Aborted!");
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
default: /* bitch and beep */
|
|
|
|
TTbeep();
|
|
|
|
|
|
|
|
case '?': /* help me */
|
|
|
|
mlwrite
|
|
|
|
("(Y)es, (N)o, (!)Do rest, (U)ndo last, (^G)Abort, (.)Abort back, (?)Help: ");
|
|
|
|
goto qprompt;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
} /* end of switch */
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* end of "if kind" */
|
2005-05-31 11:50:56 -04:00
|
|
|
/*
|
|
|
|
* Delete the sucker, and insert its
|
|
|
|
* replacement.
|
|
|
|
*/
|
|
|
|
status = delins(matchlen, &rpat[0], TRUE);
|
|
|
|
if (status != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Save our position, since we may
|
|
|
|
* undo this.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if (kind) {
|
2005-05-31 11:50:56 -04:00
|
|
|
lastline = curwp->w_dotp;
|
|
|
|
lastoff = curwp->w_doto;
|
|
|
|
}
|
|
|
|
|
|
|
|
numsub++; /* increment # of substitutions */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And report the results.
|
|
|
|
*/
|
|
|
|
mlwrite("%d substitutions", numsub);
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* delins -- Delete a specified length from the current point
|
|
|
|
* then either insert the string directly, or make use of
|
|
|
|
* replacement meta-array.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int delins(int dlength, char *instr, int use_meta)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
int status;
|
2005-05-31 11:50:56 -04:00
|
|
|
#if MAGIC
|
2010-11-28 20:56:30 -05:00
|
|
|
struct magic_replacement *rmcptr;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Zap what we gotta,
|
|
|
|
* and insert its replacement.
|
|
|
|
*/
|
|
|
|
if ((status = ldelete((long) dlength, FALSE)) != TRUE)
|
|
|
|
mlwrite("%%ERROR while deleting");
|
|
|
|
else
|
|
|
|
#if MAGIC
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((rmagical && use_meta) &&
|
|
|
|
(curwp->w_bufp->b_mode & MDMAGIC) != 0) {
|
|
|
|
rmcptr = &rmcpat[0];
|
|
|
|
while (rmcptr->mc_type != MCNIL && status == TRUE) {
|
|
|
|
if (rmcptr->mc_type == LITCHAR)
|
|
|
|
status = linstr(rmcptr->rstr);
|
|
|
|
else
|
|
|
|
status = linstr(patmatch);
|
|
|
|
rmcptr++;
|
|
|
|
}
|
|
|
|
} else
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
status = linstr(instr);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* expandp -- Expand control key sequences for output.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* char *srcstr; string to expand
|
|
|
|
* char *deststr; destination of expanded string
|
|
|
|
* int maxlength; maximum chars in destination
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int expandp(char *srcstr, char *deststr, int maxlength)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
unsigned char c; /* current char to translate */
|
|
|
|
|
|
|
|
/* Scan through the string.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
while ((c = *srcstr++) != 0) {
|
|
|
|
if (c == '\n') { /* it's a newline */
|
2005-05-31 11:50:56 -04:00
|
|
|
*deststr++ = '<';
|
|
|
|
*deststr++ = 'N';
|
|
|
|
*deststr++ = 'L';
|
|
|
|
*deststr++ = '>';
|
|
|
|
maxlength -= 4;
|
|
|
|
}
|
|
|
|
#if PKCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
else if ((c > 0 && c < 0x20) || c == 0x7f) /* control character */
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
|
|
|
else if (c < 0x20 || c == 0x7f) /* control character */
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
*deststr++ = '^';
|
|
|
|
*deststr++ = c ^ 0x40;
|
|
|
|
maxlength -= 2;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else if (c == '%') {
|
2005-05-31 11:50:56 -04:00
|
|
|
*deststr++ = '%';
|
|
|
|
*deststr++ = '%';
|
|
|
|
maxlength -= 2;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else { /* any other character */
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
*deststr++ = c;
|
|
|
|
maxlength--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for maxlength */
|
2005-09-30 18:26:09 -04:00
|
|
|
if (maxlength < 4) {
|
2005-05-31 11:50:56 -04:00
|
|
|
*deststr++ = '$';
|
|
|
|
*deststr = '\0';
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*deststr = '\0';
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* boundry -- Return information depending on whether we may search no
|
|
|
|
* further. Beginning of file and end of file are the obvious
|
|
|
|
* cases, but we may want to add further optional boundry restrictions
|
|
|
|
* in future, a' la VMS EDT. At the moment, just return TRUE or
|
|
|
|
* FALSE depending on if a boundry is hit (ouch).
|
|
|
|
*/
|
2010-04-17 19:38:09 -04:00
|
|
|
int boundry(struct line *curline, int curoff, int dir)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int border;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (dir == FORWARD) {
|
2005-05-31 11:50:56 -04:00
|
|
|
border = (curoff == llength(curline)) &&
|
2005-09-30 18:26:09 -04:00
|
|
|
(lforw(curline) == curbp->b_linep);
|
|
|
|
} else {
|
2005-05-31 11:50:56 -04:00
|
|
|
border = (curoff == 0) &&
|
2005-09-30 18:26:09 -04:00
|
|
|
(lback(curline) == curbp->b_linep);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return border;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nextch -- retrieve the next/previous character in the buffer,
|
|
|
|
* and advance/retreat the point.
|
|
|
|
* The order in which this is done is significant, and depends
|
|
|
|
* upon the direction of the search. Forward searches look at
|
|
|
|
* the current character and move, reverse searches move and
|
|
|
|
* look at the character.
|
|
|
|
*/
|
2010-04-17 19:38:09 -04:00
|
|
|
static int nextch(struct line **pcurline, int *pcuroff, int dir)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline;
|
2010-02-27 05:38:22 -05:00
|
|
|
int curoff;
|
|
|
|
int c;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
curline = *pcurline;
|
|
|
|
curoff = *pcuroff;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (dir == FORWARD) {
|
|
|
|
if (curoff == llength(curline)) { /* if at EOL */
|
2005-05-31 11:50:56 -04:00
|
|
|
curline = lforw(curline); /* skip to next line */
|
|
|
|
curoff = 0;
|
2005-09-30 18:26:09 -04:00
|
|
|
c = '\n'; /* and return a <NL> */
|
|
|
|
} else
|
2005-05-31 11:50:56 -04:00
|
|
|
c = lgetc(curline, curoff++); /* get the char */
|
2005-09-30 18:26:09 -04:00
|
|
|
} else { /* Reverse. */
|
|
|
|
|
|
|
|
if (curoff == 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
curline = lback(curline);
|
|
|
|
curoff = llength(curline);
|
|
|
|
c = '\n';
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
2005-05-31 11:50:56 -04:00
|
|
|
c = lgetc(curline, --curoff);
|
|
|
|
|
|
|
|
}
|
|
|
|
*pcurline = curline;
|
|
|
|
*pcuroff = curoff;
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return c;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if MAGIC
|
|
|
|
/*
|
|
|
|
* mcstr -- Set up the 'magic' array. The closure symbol is taken as
|
|
|
|
* a literal character when (1) it is the first character in the
|
|
|
|
* pattern, and (2) when preceded by a symbol that does not allow
|
|
|
|
* closure, such as a newline, beginning of line symbol, or another
|
|
|
|
* closure symbol.
|
|
|
|
*
|
|
|
|
* Coding comment (jmg): yes, i know i have gotos that are, strictly
|
|
|
|
* speaking, unnecessary. But right now we are so cramped for
|
|
|
|
* code space that i will grab what i can in order to remain
|
|
|
|
* within the 64K limit. C compilers actually do very little
|
|
|
|
* in the way of optimizing - they expect you to do that.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
static int mcstr(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-11-27 19:28:01 -05:00
|
|
|
struct magic *mcptr, *rtpcm;
|
2005-09-30 18:26:09 -04:00
|
|
|
char *patptr;
|
|
|
|
int mj;
|
|
|
|
int pchr;
|
|
|
|
int status = TRUE;
|
|
|
|
int does_closure = FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2010-11-27 19:28:01 -05:00
|
|
|
/* If we had metacharacters in the struct magic array previously,
|
2005-05-31 11:50:56 -04:00
|
|
|
* free up any bitmaps that may have been allocated.
|
|
|
|
*/
|
|
|
|
if (magical)
|
|
|
|
mcclear();
|
|
|
|
|
|
|
|
magical = FALSE;
|
|
|
|
mj = 0;
|
|
|
|
mcptr = &mcpat[0];
|
|
|
|
patptr = &pat[0];
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
while ((pchr = *patptr) && status) {
|
|
|
|
switch (pchr) {
|
|
|
|
case MC_CCL:
|
|
|
|
status = cclmake(&patptr, mcptr);
|
|
|
|
magical = TRUE;
|
|
|
|
does_closure = TRUE;
|
|
|
|
break;
|
|
|
|
case MC_BOL:
|
|
|
|
if (mj != 0)
|
|
|
|
goto litcase;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
mcptr->mc_type = BOL;
|
|
|
|
magical = TRUE;
|
|
|
|
does_closure = FALSE;
|
|
|
|
break;
|
|
|
|
case MC_EOL:
|
|
|
|
if (*(patptr + 1) != '\0')
|
|
|
|
goto litcase;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
mcptr->mc_type = EOL;
|
|
|
|
magical = TRUE;
|
|
|
|
does_closure = FALSE;
|
|
|
|
break;
|
|
|
|
case MC_ANY:
|
|
|
|
mcptr->mc_type = ANY;
|
|
|
|
magical = TRUE;
|
|
|
|
does_closure = TRUE;
|
|
|
|
break;
|
|
|
|
case MC_CLOSURE:
|
|
|
|
/* Does the closure symbol mean closure here?
|
|
|
|
* If so, back up to the previous element
|
|
|
|
* and indicate it is enclosed.
|
|
|
|
*/
|
|
|
|
if (!does_closure)
|
|
|
|
goto litcase;
|
|
|
|
mj--;
|
|
|
|
mcptr--;
|
|
|
|
mcptr->mc_type |= CLOSURE;
|
|
|
|
magical = TRUE;
|
|
|
|
does_closure = FALSE;
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Note: no break between MC_ESC case and the default.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
case MC_ESC:
|
|
|
|
if (*(patptr + 1) != '\0') {
|
|
|
|
pchr = *++patptr;
|
|
|
|
magical = TRUE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
litcase:mcptr->mc_type =
|
|
|
|
LITCHAR;
|
|
|
|
mcptr->u.lchar = pchr;
|
|
|
|
does_closure = (pchr != '\n');
|
|
|
|
break;
|
|
|
|
} /* End of switch. */
|
2005-05-31 11:50:56 -04:00
|
|
|
mcptr++;
|
|
|
|
patptr++;
|
|
|
|
mj++;
|
2005-09-30 18:26:09 -04:00
|
|
|
} /* End of while. */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Close off the meta-string.
|
|
|
|
*/
|
|
|
|
mcptr->mc_type = MCNIL;
|
|
|
|
|
|
|
|
/* Set up the reverse array, if the status is good. Please note the
|
|
|
|
* structure assignment - your compiler may not like that.
|
|
|
|
* If the status is not good, nil out the meta-pattern.
|
|
|
|
* The only way the status would be bad is from the cclmake()
|
|
|
|
* routine, and the bitmap for that member is guarenteed to be
|
|
|
|
* freed. So we stomp a MCNIL value there, and call mcclear()
|
|
|
|
* to free any other bitmaps.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if (status) {
|
2005-05-31 11:50:56 -04:00
|
|
|
rtpcm = &tapcm[0];
|
2005-09-30 18:26:09 -04:00
|
|
|
while (--mj >= 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
#if MSC | TURBO | VMS | USG | BSD | V7
|
|
|
|
*rtpcm++ = *--mcptr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
rtpcm->mc_type = MCNIL;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else {
|
2005-05-31 11:50:56 -04:00
|
|
|
(--mcptr)->mc_type = MCNIL;
|
|
|
|
mcclear();
|
|
|
|
}
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rmcstr -- Set up the replacement 'magic' array. Note that if there
|
|
|
|
* are no meta-characters encountered in the replacement string,
|
|
|
|
* the array is never actually created - we will just use the
|
|
|
|
* character array rpat[] as the replacement string.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
static int rmcstr(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-11-28 20:56:30 -05:00
|
|
|
struct magic_replacement *rmcptr;
|
2005-09-30 18:26:09 -04:00
|
|
|
char *patptr;
|
|
|
|
int status = TRUE;
|
|
|
|
int mj;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
patptr = &rpat[0];
|
|
|
|
rmcptr = &rmcpat[0];
|
|
|
|
mj = 0;
|
|
|
|
rmagical = FALSE;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
while (*patptr && status == TRUE) {
|
|
|
|
switch (*patptr) {
|
|
|
|
case MC_DITTO:
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* If there were non-magical characters
|
|
|
|
* in the string before reaching this
|
|
|
|
* character, plunk it in the replacement
|
|
|
|
* array before processing the current
|
|
|
|
* meta-character.
|
|
|
|
*/
|
|
|
|
if (mj != 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
rmcptr->mc_type = LITCHAR;
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((rmcptr->rstr =
|
|
|
|
malloc(mj + 1)) == NULL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("%%Out of memory");
|
|
|
|
status = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
strncpy(rmcptr->rstr, patptr - mj, mj);
|
2005-05-31 11:50:56 -04:00
|
|
|
rmcptr++;
|
|
|
|
mj = 0;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
rmcptr->mc_type = DITTO;
|
|
|
|
rmcptr++;
|
|
|
|
rmagical = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MC_ESC:
|
|
|
|
rmcptr->mc_type = LITCHAR;
|
|
|
|
|
|
|
|
/* We malloc mj plus two here, instead
|
|
|
|
* of one, because we have to count the
|
|
|
|
* current character.
|
|
|
|
*/
|
|
|
|
if ((rmcptr->rstr = malloc(mj + 2)) == NULL) {
|
|
|
|
mlwrite("%%Out of memory");
|
|
|
|
status = FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
break;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(rmcptr->rstr, patptr - mj, mj + 1);
|
|
|
|
|
|
|
|
/* If MC_ESC is not the last character
|
|
|
|
* in the string, find out what it is
|
|
|
|
* escaping, and overwrite the last
|
|
|
|
* character with it.
|
|
|
|
*/
|
|
|
|
if (*(patptr + 1) != '\0')
|
|
|
|
*((rmcptr->rstr) + mj) = *++patptr;
|
|
|
|
|
|
|
|
rmcptr++;
|
|
|
|
mj = 0;
|
|
|
|
rmagical = TRUE;
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
default:
|
|
|
|
mj++;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
patptr++;
|
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (rmagical && mj > 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
rmcptr->mc_type = LITCHAR;
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((rmcptr->rstr = malloc(mj + 1)) == NULL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("%%Out of memory.");
|
|
|
|
status = FALSE;
|
|
|
|
}
|
|
|
|
strncpy(rmcptr->rstr, patptr - mj, mj);
|
|
|
|
rmcptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rmcptr->mc_type = MCNIL;
|
2005-10-01 02:37:34 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-11-27 19:28:01 -05:00
|
|
|
* mcclear -- Free up any CCL bitmaps, and MCNIL the struct magic search arrays.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
void mcclear(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-11-27 19:28:01 -05:00
|
|
|
struct magic *mcptr;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
mcptr = &mcpat[0];
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
while (mcptr->mc_type != MCNIL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if ((mcptr->mc_type & MASKCL) == CCL ||
|
|
|
|
(mcptr->mc_type & MASKCL) == NCCL)
|
|
|
|
free(mcptr->u.cclmap);
|
|
|
|
mcptr++;
|
|
|
|
}
|
|
|
|
mcpat[0].mc_type = tapcm[0].mc_type = MCNIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-11-28 20:56:30 -05:00
|
|
|
* rmcclear -- Free up any strings, and MCNIL the struct magic_replacement array.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
void rmcclear(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-11-28 20:56:30 -05:00
|
|
|
struct magic_replacement *rmcptr;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
rmcptr = &rmcpat[0];
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
while (rmcptr->mc_type != MCNIL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (rmcptr->mc_type == LITCHAR)
|
|
|
|
free(rmcptr->rstr);
|
|
|
|
rmcptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rmcpat[0].mc_type = MCNIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mceq -- meta-character equality with a character. In Kernighan & Plauger's
|
|
|
|
* Software Tools, this is the function omatch(), but i felt there
|
|
|
|
* were too many functions with the 'match' name already.
|
|
|
|
*/
|
2010-11-27 19:28:01 -05:00
|
|
|
static int mceq(int bc, struct magic *mt)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int result;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
#if PKCODE
|
|
|
|
bc = bc & 0xFF;
|
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
switch (mt->mc_type & MASKCL) {
|
|
|
|
case LITCHAR:
|
|
|
|
result = eq(bc, mt->u.lchar);
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
case ANY:
|
|
|
|
result = (bc != '\n');
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
case CCL:
|
|
|
|
if (!(result = biteq(bc, mt->u.cclmap))) {
|
|
|
|
if ((curwp->w_bufp->b_mode & MDEXACT) == 0 &&
|
|
|
|
(isletter(bc))) {
|
|
|
|
result = biteq(CHCASE(bc), mt->u.cclmap);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
case NCCL:
|
|
|
|
result = !biteq(bc, mt->u.cclmap);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((curwp->w_bufp->b_mode & MDEXACT) == 0 &&
|
|
|
|
(isletter(bc))) {
|
|
|
|
result &= !biteq(CHCASE(bc), mt->u.cclmap);
|
|
|
|
}
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
default:
|
|
|
|
mlwrite("mceq: what is %d?", mt->mc_type);
|
|
|
|
result = FALSE;
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
} /* End of switch. */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return result;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2010-11-01 20:22:42 -04:00
|
|
|
extern char *clearbits(void);
|
2010-02-27 05:38:24 -05:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/*
|
|
|
|
* cclmake -- create the bitmap for the character class.
|
|
|
|
* ppatptr is left pointing to the end-of-character-class character,
|
|
|
|
* so that a loop may automatically increment with safety.
|
|
|
|
*/
|
2010-11-27 19:28:01 -05:00
|
|
|
static int cclmake(char **ppatptr, struct magic *mcptr)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-11-01 20:22:42 -04:00
|
|
|
char *bmap;
|
2010-02-27 05:38:22 -05:00
|
|
|
char *patptr;
|
|
|
|
int pchr, ochr;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((bmap = clearbits()) == NULL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("%%Out of memory");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mcptr->u.cclmap = bmap;
|
|
|
|
patptr = *ppatptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test the initial character(s) in ccl for
|
|
|
|
* special cases - negate ccl, or an end ccl
|
|
|
|
* character as a first character. Anything
|
|
|
|
* else gets set in the bitmap.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
if (*++patptr == MC_NCCL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
patptr++;
|
|
|
|
mcptr->mc_type = NCCL;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
2005-05-31 11:50:56 -04:00
|
|
|
mcptr->mc_type = CCL;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((ochr = *patptr) == MC_ECCL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("%%No characters in character class");
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (ochr == MC_ESC)
|
|
|
|
ochr = *++patptr;
|
|
|
|
|
|
|
|
setbit(ochr, bmap);
|
|
|
|
patptr++;
|
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
while (ochr != '\0' && (pchr = *patptr) != MC_ECCL) {
|
|
|
|
switch (pchr) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* Range character loses its meaning
|
|
|
|
* if it is the last character in
|
|
|
|
* the class.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
case MC_RCCL:
|
|
|
|
if (*(patptr + 1) == MC_ECCL)
|
|
|
|
setbit(pchr, bmap);
|
|
|
|
else {
|
|
|
|
pchr = *++patptr;
|
|
|
|
while (++ochr <= pchr)
|
|
|
|
setbit(ochr, bmap);
|
|
|
|
}
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Note: no break between case MC_ESC and the default.
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
case MC_ESC:
|
|
|
|
pchr = *++patptr;
|
|
|
|
default:
|
|
|
|
setbit(pchr, bmap);
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
patptr++;
|
|
|
|
ochr = pchr;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ppatptr = patptr;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (ochr == '\0') {
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("%%Character class not ended");
|
|
|
|
free(bmap);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* biteq -- is the character in the bitmap?
|
|
|
|
*/
|
2010-11-01 20:22:42 -04:00
|
|
|
static int biteq(int bc, char *cclmap)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
#if PKCODE
|
|
|
|
bc = bc & 0xFF;
|
|
|
|
#endif
|
|
|
|
if (bc >= HICHAR)
|
|
|
|
return FALSE;
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return (*(cclmap + (bc >> 3)) & BIT(bc & 7)) ? TRUE : FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clearbits -- Allocate and zero out a CCL bitmap.
|
|
|
|
*/
|
2010-11-01 20:22:42 -04:00
|
|
|
static char *clearbits(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-11-01 20:22:42 -04:00
|
|
|
char *cclstart;
|
|
|
|
char *cclmap;
|
|
|
|
int i;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2010-11-01 20:22:42 -04:00
|
|
|
if ((cclmap = cclstart = (char *)malloc(HIBYTE)) != NULL) {
|
|
|
|
for (i = 0; i < HIBYTE; i++)
|
2005-05-31 11:50:56 -04:00
|
|
|
*cclmap++ = 0;
|
2010-11-01 20:22:42 -04:00
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return cclstart;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setbit -- Set a bit (ON only) in the bitmap.
|
|
|
|
*/
|
2010-11-01 20:22:42 -04:00
|
|
|
static void setbit(int bc, char *cclmap)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
#if PKCODE
|
|
|
|
bc = bc & 0xFF;
|
|
|
|
#endif
|
|
|
|
if (bc < HICHAR)
|
|
|
|
*(cclmap + (bc >> 3)) |= BIT(bc & 7);
|
|
|
|
}
|
|
|
|
#endif
|