home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-03-14 | 56.1 KB | 2,453 lines |
-
- : This is a shar archive. Extract with sh, not csh.
- : The rest of this file will extract:
- : param.c ptrfunc.c regexp.c regsub.c screen.c
- echo extracting - param.c
- sed 's/^X//' > param.c << '!EOR!'
- X/* $Header: /nw/tony/src/stevie/src/RCS/param.c,v 1.10 89/08/02 10:59:10 tony Exp $
- X *
- X * Code to handle user-settable parameters. This is all pretty much table-
- X * driven. To add a new parameter, put it in the params array, and add a
- X * macro for it in param.h. If it's a numeric parameter, add any necessary
- X * bounds checks to doset(). String parameters aren't currently supported.
- X */
- X
- X#include "stevie.h"
- X
- Xstruct param params[] = {
- X
- X { "tabstop", "ts", 8, P_NUM },
- X { "scroll", "scroll", 12, P_NUM },
- X { "report", "report", 5, P_NUM },
- X { "lines", "lines", 25, P_NUM },
- X
- X { "vbell", "vb", TRUE, P_BOOL },
- X { "showmatch", "sm", FALSE, P_BOOL },
- X { "wrapscan", "ws", TRUE, P_BOOL },
- X { "errorbells", "eb", FALSE, P_BOOL },
- X { "showmode", "mo", FALSE, P_BOOL },
- X { "backup", "bk", FALSE, P_BOOL },
- X { "return", "cr", TRUE, P_BOOL },
- X { "list", "list", FALSE, P_BOOL },
- X { "ignorecase", "ic", FALSE, P_BOOL },
- X { "autoindent", "ai", FALSE, P_BOOL },
- X { "number", "nu", FALSE, P_BOOL },
- X { "modelines", "ml", FALSE, P_BOOL },
- X { "tildeop", "to", FALSE, P_BOOL },
- X { "terse", "terse", FALSE, P_BOOL },
- X { "tagstack", "tg", FALSE, P_BOOL },
- X { "color", "co", -1, P_NUM },
- X { "", "", 0, 0, } /* end marker */
- X
- X};
- X
- Xstatic void showparms();
- X
- Xvoid
- Xdoset(arg)
- Xchar *arg; /* parameter string */
- X{
- X register int i;
- X register char *s;
- X bool_t did_lines = FALSE;
- X bool_t state = TRUE; /* new state of boolean parms. */
- X
- X if (arg == NULL) {
- X showparms(FALSE);
- X return;
- X }
- X if (strncmp(arg, "all", 3) == 0) {
- X showparms(TRUE);
- X return;
- X }
- X if (strncmp(arg, "no", 2) == 0) {
- X state = FALSE;
- X arg += 2;
- X }
- X
- X for (i=0; params[i].fullname[0] != NUL ;i++) {
- X s = params[i].fullname;
- X if (strncmp(arg, s, strlen(s)) == 0) /* matched full name */
- X break;
- X s = params[i].shortname;
- X if (strncmp(arg, s, strlen(s)) == 0) /* matched short name */
- X break;
- X }
- X
- X if (params[i].fullname[0] != NUL) { /* found a match */
- X if (params[i].flags & P_NUM) {
- X did_lines = (i == P_LI);
- X if (arg[strlen(s)] != '=' || state == FALSE)
- X emsg("Invalid set of numeric parameter");
- X else {
- X params[i].value = atoi(arg+strlen(s)+1);
- X params[i].flags |= P_CHANGED;
- X if (i==P_CO) setcolor (P(P_CO));
- X }
- X } else /* boolean */ {
- X if (arg[strlen(s)] == '=')
- X emsg("Invalid set of boolean parameter");
- X else {
- X params[i].value = state;
- X params[i].flags |= P_CHANGED;
- X }
- X }
- X } else
- X emsg("Unrecognized 'set' option");
- X
- X /*
- X * Update the screen in case we changed something like "tabstop"
- X * or "list" that will change its appearance.
- X */
- X updatescreen();
- X
- X if (did_lines) {
- X Rows = P(P_LI);
- X P(P_LI) = Rows = setrows( Rows );
- X /* setrows() is system-dependent.
- X * This assures no impossible values
- X * will be set.
- X */
- X if (screenalloc() == -1) return; /* allocate new screen buffers */
- X screenclear();
- X updatescreen();
- X }
- X /*
- X * Check the bounds for numeric parameters here
- X */
- X if (P(P_TS) <= 0 || P(P_TS) > 32) {
- X emsg("Invalid tab size specified");
- X P(P_TS) = 8;
- X return;
- X }
- X
- X if (P(P_SS) <= 0 || P(P_SS) > Rows) {
- X emsg("Invalid scroll size specified");
- X P(P_SS) = 12;
- X return;
- X }
- X
- X#ifndef TILDEOP
- X if (P(P_TO)) {
- X emsg("Tilde-operator not enabled");
- X P(P_TO) = FALSE;
- X return;
- X }
- X#endif
- X /*
- X * Check for another argument, and call doset() recursively, if
- X * found. If any argument results in an error, no further
- X * parameters are processed.
- X */
- X while (*arg != ' ' && *arg != '\t') { /* skip to next white space */
- X if (*arg == NUL)
- X return; /* end of parameter list */
- X arg++;
- X }
- X while (*arg == ' ' || *arg == '\t') /* skip to next non-white */
- X arg++;
- X
- X if (*arg)
- X doset(arg); /* recurse on next parameter */
- X}
- X
- Xstatic void
- Xshowparms(all)
- Xbool_t all; /* show ALL parameters */
- X{
- X register struct param *p;
- X char buf[64];
- X
- X gotocmd(TRUE, 0);
- X outstr("Parameters:\r\n");
- X
- X for (p = ¶ms[0]; p->fullname[0] != NUL ;p++) {
- X if (!all && ((p->flags & P_CHANGED) == 0))
- X continue;
- X if (p->flags & P_BOOL)
- X sprintf(buf, "\t%s%s\r\n",
- X (p->value ? "" : "no"), p->fullname);
- X else
- X sprintf(buf, "\t%s=%d\r\n", p->fullname, p->value);
- X
- X outstr(buf);
- X }
- X wait_return();
- X}
- !EOR!
- echo extracting - ptrfunc.c
- sed 's/^X//' > ptrfunc.c << '!EOR!'
- X/* $Header: /nw/tony/src/stevie/src/RCS/ptrfunc.c,v 1.5 89/03/11 22:43:12 tony Exp $
- X *
- X * The routines in this file attempt to imitate many of the operations
- X * that used to be performed on simple character pointers and are now
- X * performed on LPTR's. This makes it easier to modify other sections
- X * of the code. Think of an LPTR as representing a position in the file.
- X * Positions can be incremented, decremented, compared, etc. through
- X * the functions implemented here.
- X */
- X
- X#include "stevie.h"
- X
- X/*
- X * inc(p)
- X *
- X * Increment the line pointer 'p' crossing line boundaries as necessary.
- X * Return 1 when crossing a line, -1 when at end of file, 0 otherwise.
- X */
- Xint
- Xinc(lp)
- Xregister LPTR *lp;
- X{
- X register char *p;
- X
- X if (lp && lp->linep)
- X p = &(lp->linep->s[lp->index]);
- X else
- X return -1;
- X
- X if (*p != NUL) { /* still within line */
- X lp->index++;
- X return ((p[1] != NUL) ? 0 : 1);
- X }
- X
- X if (lp->linep->next != Fileend->linep) { /* there is a next line */
- X lp->index = 0;
- X lp->linep = lp->linep->next;
- X return 1;
- X }
- X
- X return -1;
- X}
- X
- X/*
- X * dec(p)
- X *
- X * Decrement the line pointer 'p' crossing line boundaries as necessary.
- X * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
- X */
- Xint
- Xdec(lp)
- Xregister LPTR *lp;
- X{
- X if (lp->index > 0) { /* still within line */
- X lp->index--;
- X return 0;
- X }
- X
- X if (lp->linep->prev != Filetop->linep) { /* there is a prior line */
- X lp->linep = lp->linep->prev;
- X lp->index = strlen(lp->linep->s);
- X return 1;
- X }
- X
- X lp->index = 0; /* stick at first char */
- X return -1; /* at start of file */
- X}
- X
- X/*
- X * gchar(lp) - get the character at position "lp"
- X */
- Xint
- Xgchar(lp)
- Xregister LPTR *lp;
- X{
- X if (lp && lp->linep)
- X return (lp->linep->s[lp->index]);
- X else
- X return 0;
- X}
- X
- X/*
- X * pchar(lp, c) - put character 'c' at position 'lp'
- X */
- Xvoid
- Xpchar(lp, c)
- Xregister LPTR *lp;
- Xchar c;
- X{
- X lp->linep->s[lp->index] = c;
- X}
- X
- X/*
- X * pswap(a, b) - swap two position pointers
- X */
- Xvoid
- Xpswap(a, b)
- Xregister LPTR *a, *b;
- X{
- X LPTR tmp;
- X
- X tmp = *a;
- X *a = *b;
- X *b = tmp;
- X}
- X
- X/*
- X * Position comparisons
- X */
- X
- Xbool_t
- Xlt(a, b)
- Xregister LPTR *a, *b;
- X{
- X register int an, bn;
- X
- X an = LINEOF(a);
- X bn = LINEOF(b);
- X
- X if (an != bn)
- X return (an < bn);
- X else
- X return (a->index < b->index);
- X}
- X
- X#if 0
- Xbool_t
- Xgt(a, b)
- XLPTR *a, *b;
- X{
- X register int an, bn;
- X
- X an = LINEOF(a);
- X bn = LINEOF(b);
- X
- X if (an != bn)
- X return (an > bn);
- X else
- X return (a->index > b->index);
- X}
- X#endif
- X
- Xbool_t
- Xequal(a, b)
- Xregister LPTR *a, *b;
- X{
- X return (a->linep == b->linep && a->index == b->index);
- X}
- X
- Xbool_t
- Xltoreq(a, b)
- Xregister LPTR *a, *b;
- X{
- X return (lt(a, b) || equal(a, b));
- X}
- X
- X#if 0
- Xbool_t
- Xgtoreq(a, b)
- XLPTR *a, *b;
- X{
- X return (gt(a, b) || equal(a, b));
- X}
- X#endif
- !EOR!
- echo extracting - regexp.c
- sed 's/^X//' > regexp.c << '!EOR!'
- X/* $Header: /nw/tony/src/stevie/src/RCS/regexp.c,v 1.5 89/07/07 16:27:11 tony Exp $
- X *
- X * NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
- X *
- X * This is NOT the original regular expression code as written by
- X * Henry Spencer. This code has been modified specifically for use
- X * with the STEVIE editor, and should not be used apart from compiling
- X * STEVIE. If you want a good regular expression library, get the
- X * original code. The copyright notice that follows is from the
- X * original.
- X *
- X * NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
- X *
- X *
- X * regcomp and regexec -- regsub and regerror are elsewhere
- X *
- X * Copyright (c) 1986 by University of Toronto.
- X * Written by Henry Spencer. Not derived from licensed software.
- X *
- X * Permission is granted to anyone to use this software for any
- X * purpose on any computer system, and to redistribute it freely,
- X * subject to the following restrictions:
- X *
- X * 1. The author is not responsible for the consequences of use of
- X * this software, no matter how awful, even if they arise
- X * from defects in it.
- X *
- X * 2. The origin of this software must not be misrepresented, either
- X * by explicit claim or by omission.
- X *
- X * 3. Altered versions must be plainly marked as such, and must not
- X * be misrepresented as being the original software.
- X *
- X * Beware that some of this code is subtly aware of the way operator
- X * precedence is structured in regular expressions. Serious changes in
- X * regular-expression syntax might require a total rethink.
- X *
- X */
- X
- X#include "env.h"
- X
- X#include <stdio.h>
- X#include "regexp.h"
- X#include "regmagic.h"
- X
- X/*
- X * The "internal use only" fields in regexp.h are present to pass info from
- X * compile to execute that permits the execute phase to run lots faster on
- X * simple cases. They are:
- X *
- X * regstart char that must begin a match; '\0' if none obvious
- X * reganch is the match anchored (at beginning-of-line only)?
- X * regmust string (pointer into program) that match must include, or NULL
- X * regmlen length of regmust string
- X *
- X * Regstart and reganch permit very fast decisions on suitable starting points
- X * for a match, cutting down the work a lot. Regmust permits fast rejection
- X * of lines that cannot possibly match. The regmust tests are costly enough
- X * that regcomp() supplies a regmust only if the r.e. contains something
- X * potentially expensive (at present, the only such thing detected is * or +
- X * at the start of the r.e., which can involve a lot of backup). Regmlen is
- X * supplied because the test in regexec() needs it and regcomp() is computing
- X * it anyway.
- X */
- X
- X/*
- X * Structure for regexp "program". This is essentially a linear encoding
- X * of a nondeterministic finite-state machine (aka syntax charts or
- X * "railroad normal form" in parsing technology). Each node is an opcode
- X * plus a "next" pointer, possibly plus an operand. "Next" pointers of
- X * all nodes except BRANCH implement concatenation; a "next" pointer with
- X * a BRANCH on both ends of it is connecting two alternatives. (Here we
- X * have one of the subtle syntax dependencies: an individual BRANCH (as
- X * opposed to a collection of them) is never concatenated with anything
- X * because of operator precedence.) The operand of some types of node is
- X * a literal string; for others, it is a node leading into a sub-FSM. In
- X * particular, the operand of a BRANCH node is the first node of the branch.
- X * (NB this is *not* a tree structure: the tail of the branch connects
- X * to the thing following the set of BRANCHes.) The opcodes are:
- X */
- X
- X/* definition number opnd? meaning */
- X#define END 0 /* no End of program. */
- X#define BOL 1 /* no Match "" at beginning of line. */
- X#define EOL 2 /* no Match "" at end of line. */
- X#define ANY 3 /* no Match any one character. */
- X#define ANYOF 4 /* str Match any character in this string. */
- X#define ANYBUT 5 /* str Match any character not in this string. */
- X#define BRANCH 6 /* node Match this alternative, or the next... */
- X#define BACK 7 /* no Match "", "next" ptr points backward. */
- X#define EXACTLY 8 /* str Match this string. */
- X#define NOTHING 9 /* no Match empty string. */
- X#define STAR 10 /* node Match this (simple) thing 0 or more times. */
- X#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
- X#define OPEN 20 /* no Mark this point in input as start of #n. */
- X /* OPEN+1 is number 1, etc. */
- X#define CLOSE 30 /* no Analogous to OPEN. */
- X
- X/*
- X * Opcode notes:
- X *
- X * BRANCH The set of branches constituting a single choice are hooked
- X * together with their "next" pointers, since precedence prevents
- X * anything being concatenated to any individual branch. The
- X * "next" pointer of the last BRANCH in a choice points to the
- X * thing following the whole choice. This is also where the
- X * final "next" pointer of each individual branch points; each
- X * branch starts with the operand node of a BRANCH node.
- X *
- X * BACK Normal "next" pointers all implicitly point forward; BACK
- X * exists to make loop structures possible.
- X *
- X * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
- X * BRANCH structures using BACK. Simple cases (one character
- X * per match) are implemented with STAR and PLUS for speed
- X * and to minimize recursive plunges.
- X *
- X * OPEN,CLOSE ...are numbered at compile time.
- X */
- X
- X/*
- X * A node is one char of opcode followed by two chars of "next" pointer.
- X * "Next" pointers are stored as two 8-bit pieces, high order first. The
- X * value is a positive offset from the opcode of the node containing it.
- X * An operand, if any, simply follows the node. (Note that much of the
- X * code generation knows about this implicit relationship.)
- X *
- X * Using two bytes for the "next" pointer is vast overkill for most things,
- X * but allows patterns to get big without disasters.
- X */
- X#define OP(p) (*(p))
- X#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
- X#define OPERAND(p) ((p) + 3)
- X
- X/*
- X * See regmagic.h for one further detail of program structure.
- X */
- X
- X
- X/*
- X * Utility definitions.
- X */
- X#ifndef CHARBITS
- X#define UCHARAT(p) ((int)*(unsigned char *)(p))
- X#else
- X#define UCHARAT(p) ((int)*(p)&CHARBITS)
- X#endif
- X
- X#define FAIL(m) { regerror(m); return(NULL); }
- X#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
- X#define META "^$.[()|?+*\\"
- X
- X/*
- X * Flags to be passed up and down.
- X */
- X#define HASWIDTH 01 /* Known never to match null string. */
- X#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
- X#define SPSTART 04 /* Starts with * or +. */
- X#define WORST 0 /* Worst case. */
- X
- X#ifndef ORIGINAL
- X/*
- X * The following supports the ability to ignore case in searches.
- X */
- X
- X#include <ctype.h>
- X
- Xint reg_ic = 0; /* set by callers to ignore case */
- X
- X/*
- X * mkup - convert to upper case IF we're doing caseless compares
- X */
- X#define mkup(c) ((reg_ic && islower(c)) ? toupper(c) : (c))
- X
- X#endif
- X
- X/*
- X * Global work variables for regcomp().
- X */
- Xstatic char *regparse; /* Input-scan pointer. */
- Xstatic int regnpar; /* () count. */
- Xstatic char regdummy;
- Xstatic char *regcode; /* Code-emit pointer; ®dummy = don't. */
- Xstatic long regsize; /* Code size. */
- X
- X/*
- X * Forward declarations for regcomp()'s friends.
- X */
- X#ifndef STATIC
- X#define STATIC static
- X#endif
- XSTATIC char *reg();
- XSTATIC char *regbranch();
- XSTATIC char *regpiece();
- XSTATIC char *regatom();
- XSTATIC char *regnode();
- XSTATIC char *regnext();
- XSTATIC void regc();
- XSTATIC void reginsert();
- XSTATIC void regtail();
- XSTATIC void regoptail();
- X#ifdef STRCSPN
- XSTATIC int strcspn();
- X#endif
- X
- X/*
- X - regcomp - compile a regular expression into internal code
- X *
- X * We can't allocate space until we know how big the compiled form will be,
- X * but we can't compile it (and thus know how big it is) until we've got a
- X * place to put the code. So we cheat: we compile it twice, once with code
- X * generation turned off and size counting turned on, and once "for real".
- X * This also means that we don't allocate space until we are sure that the
- X * thing really will compile successfully, and we never have to move the
- X * code and thus invalidate pointers into it. (Note that it has to be in
- X * one piece because free() must be able to free it all.)
- X *
- X * Beware that the optimization-preparation code in here knows about some
- X * of the structure of the compiled regexp.
- X */
- Xregexp *
- Xregcomp(exp)
- Xchar *exp;
- X{
- X register regexp *r;
- X register char *scan;
- X register char *longest;
- X register int len;
- X int flags;
- X extern char *malloc();
- X
- X if (exp == NULL)
- X FAIL("NULL argument");
- X
- X /* First pass: determine size, legality. */
- X regparse = exp;
- X regnpar = 1;
- X regsize = 0L;
- X regcode = ®dummy;
- X regc(MAGIC);
- X if (reg(0, &flags) == NULL)
- X return(NULL);
- X
- X /* Small enough for pointer-storage convention? */
- X if (regsize >= 32767L) /* Probably could be 65535L. */
- X FAIL("regexp too big");
- X
- X /* Allocate space. */
- X r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
- X if (r == NULL)
- X FAIL("out of space");
- X
- X /* Second pass: emit code. */
- X regparse = exp;
- X regnpar = 1;
- X regcode = r->program;
- X regc(MAGIC);
- X if (reg(0, &flags) == NULL)
- X return(NULL);
- X
- X /* Dig out information for optimizations. */
- X r->regstart = '\0'; /* Worst-case defaults. */
- X r->reganch = 0;
- X r->regmust = NULL;
- X r->regmlen = 0;
- X scan = r->program+1; /* First BRANCH. */
- X if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
- X scan = OPERAND(scan);
- X
- X /* Starting-point info. */
- X if (OP(scan) == EXACTLY)
- X r->regstart = *OPERAND(scan);
- X else if (OP(scan) == BOL)
- X r->reganch++;
- X
- X /*
- X * If there's something expensive in the r.e., find the
- X * longest literal string that must appear and make it the
- X * regmust. Resolve ties in favor of later strings, since
- X * the regstart check works with the beginning of the r.e.
- X * and avoiding duplication strengthens checking. Not a
- X * strong reason, but sufficient in the absence of others.
- X */
- X if (flags&SPSTART) {
- X longest = NULL;
- X len = 0;
- X for (; scan != NULL; scan = regnext(scan))
- X if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
- X longest = OPERAND(scan);
- X len = strlen(OPERAND(scan));
- X }
- X r->regmust = longest;
- X r->regmlen = len;
- X }
- X }
- X
- X return(r);
- X}
- X
- X/*
- X - reg - regular expression, i.e. main body or parenthesized thing
- X *
- X * Caller must absorb opening parenthesis.
- X *
- X * Combining parenthesis handling with the base level of regular expression
- X * is a trifle forced, but the need to tie the tails of the branches to what
- X * follows makes it hard to avoid.
- X */
- Xstatic char *
- Xreg(paren, flagp)
- Xint paren; /* Parenthesized? */
- Xint *flagp;
- X{
- X register char *ret;
- X register char *br;
- X register char *ender;
- X register int parno;
- X int flags;
- X
- X *flagp = HASWIDTH; /* Tentatively. */
- X
- X /* Make an OPEN node, if parenthesized. */
- X if (paren) {
- X if (regnpar >= NSUBEXP)
- X FAIL("too many ()");
- X parno = regnpar;
- X regnpar++;
- X ret = regnode(OPEN+parno);
- X } else
- X ret = NULL;
- X
- X /* Pick up the branches, linking them together. */
- X br = regbranch(&flags);
- X if (br == NULL)
- X return(NULL);
- X if (ret != NULL)
- X regtail(ret, br); /* OPEN -> first. */
- X else
- X ret = br;
- X if (!(flags&HASWIDTH))
- X *flagp &= ~HASWIDTH;
- X *flagp |= flags&SPSTART;
- X while (*regparse == '|') {
- X regparse++;
- X br = regbranch(&flags);
- X if (br == NULL)
- X return(NULL);
- X regtail(ret, br); /* BRANCH -> BRANCH. */
- X if (!(flags&HASWIDTH))
- X *flagp &= ~HASWIDTH;
- X *flagp |= flags&SPSTART;
- X }
- X
- X /* Make a closing node, and hook it on the end. */
- X ender = regnode((paren) ? CLOSE+parno : END);
- X regtail(ret, ender);
- X
- X /* Hook the tails of the branches to the closing node. */
- X for (br = ret; br != NULL; br = regnext(br))
- X regoptail(br, ender);
- X
- X /* Check for proper termination. */
- X if (paren && *regparse++ != ')') {
- X FAIL("unmatched ()");
- X } else if (!paren && *regparse != '\0') {
- X if (*regparse == ')') {
- X FAIL("unmatched ()");
- X } else
- X FAIL("junk on end"); /* "Can't happen". */
- X /* NOTREACHED */
- X }
- X
- X return(ret);
- X}
- X
- X/*
- X - regbranch - one alternative of an | operator
- X *
- X * Implements the concatenation operator.
- X */
- Xstatic char *
- Xregbranch(flagp)
- Xint *flagp;
- X{
- X register char *ret;
- X register char *chain;
- X register char *latest;
- X int flags;
- X
- X *flagp = WORST; /* Tentatively. */
- X
- X ret = regnode(BRANCH);
- X chain = NULL;
- X while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
- X latest = regpiece(&flags);
- X if (latest == NULL)
- X return(NULL);
- X *flagp |= flags&HASWIDTH;
- X if (chain == NULL) /* First piece. */
- X *flagp |= flags&SPSTART;
- X else
- X regtail(chain, latest);
- X chain = latest;
- X }
- X if (chain == NULL) /* Loop ran zero times. */
- X (void) regnode(NOTHING);
- X
- X return(ret);
- X}
- X
- X/*
- X - regpiece - something followed by possible [*+?]
- X *
- X * Note that the branching code sequences used for ? and the general cases
- X * of * and + are somewhat optimized: they use the same NOTHING node as
- X * both the endmarker for their branch list and the body of the last branch.
- X * It might seem that this node could be dispensed with entirely, but the
- X * endmarker role is not redundant.
- X */
- Xstatic char *
- Xregpiece(flagp)
- Xint *flagp;
- X{
- X register char *ret;
- X register char op;
- X register char *next;
- X int flags;
- X
- X ret = regatom(&flags);
- X if (ret == NULL)
- X return(NULL);
- X
- X op = *regparse;
- X if (!ISMULT(op)) {
- X *flagp = flags;
- X return(ret);
- X }
- X
- X if (!(flags&HASWIDTH) && op != '?')
- X FAIL("*+ operand could be empty");
- X *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
- X
- X if (op == '*' && (flags&SIMPLE))
- X reginsert(STAR, ret);
- X else if (op == '*') {
- X /* Emit x* as (x&|), where & means "self". */
- X reginsert(BRANCH, ret); /* Either x */
- X regoptail(ret, regnode(BACK)); /* and loop */
- X regoptail(ret, ret); /* back */
- X regtail(ret, regnode(BRANCH)); /* or */
- X regtail(ret, regnode(NOTHING)); /* null. */
- X } else if (op == '+' && (flags&SIMPLE))
- X reginsert(PLUS, ret);
- X else if (op == '+') {
- X /* Emit x+ as x(&|), where & means "self". */
- X next = regnode(BRANCH); /* Either */
- X regtail(ret, next);
- X regtail(regnode(BACK), ret); /* loop back */
- X regtail(next, regnode(BRANCH)); /* or */
- X regtail(ret, regnode(NOTHING)); /* null. */
- X } else if (op == '?') {
- X /* Emit x? as (x|) */
- X reginsert(BRANCH, ret); /* Either x */
- X regtail(ret, regnode(BRANCH)); /* or */
- X next = regnode(NOTHING); /* null. */
- X regtail(ret, next);
- X regoptail(ret, next);
- X }
- X regparse++;
- X if (ISMULT(*regparse))
- X FAIL("nested *?+");
- X
- X return(ret);
- X}
- X
- X/*
- X - regatom - the lowest level
- X *
- X * Optimization: gobbles an entire sequence of ordinary characters so that
- X * it can turn them into a single node, which is smaller to store and
- X * faster to run. Backslashed characters are exceptions, each becoming a
- X * separate node; the code is simpler that way and it's not worth fixing.
- X */
- Xstatic char *
- Xregatom(flagp)
- Xint *flagp;
- X{
- X register char *ret;
- X int flags;
- X
- X *flagp = WORST; /* Tentatively. */
- X
- X switch (*regparse++) {
- X case '^':
- X ret = regnode(BOL);
- X break;
- X case '$':
- X ret = regnode(EOL);
- X break;
- X case '.':
- X ret = regnode(ANY);
- X *flagp |= HASWIDTH|SIMPLE;
- X break;
- X case '[': {
- X register int class;
- X register int classend;
- X
- X if (*regparse == '^') { /* Complement of range. */
- X ret = regnode(ANYBUT);
- X regparse++;
- X } else
- X ret = regnode(ANYOF);
- X if (*regparse == ']' || *regparse == '-')
- X regc(*regparse++);
- X while (*regparse != '\0' && *regparse != ']') {
- X if (*regparse == '-') {
- X regparse++;
- X if (*regparse == ']' || *regparse == '\0')
- X regc('-');
- X else {
- X class = UCHARAT(regparse-2)+1;
- X classend = UCHARAT(regparse);
- X if (class > classend+1)
- X FAIL("invalid [] range");
- X for (; class <= classend; class++)
- X regc(class);
- X regparse++;
- X }
- X } else
- X regc(*regparse++);
- X }
- X regc('\0');
- X if (*regparse != ']')
- X FAIL("unmatched []");
- X regparse++;
- X *flagp |= HASWIDTH|SIMPLE;
- X }
- X break;
- X case '(':
- X ret = reg(1, &flags);
- X if (ret == NULL)
- X return(NULL);
- X *flagp |= flags&(HASWIDTH|SPSTART);
- X break;
- X case '\0':
- X case '|':
- X case ')':
- X FAIL("internal urp"); /* Supposed to be caught earlier. */
- X break;
- X case '?':
- X case '+':
- X case '*':
- X FAIL("?+* follows nothing");
- X break;
- X case '\\':
- X if (*regparse == '\0')
- X FAIL("trailing \\");
- X ret = regnode(EXACTLY);
- X regc(*regparse++);
- X regc('\0');
- X *flagp |= HASWIDTH|SIMPLE;
- X break;
- X default: {
- X register int len;
- X register char ender;
- X
- X regparse--;
- X len = strcspn(regparse, META);
- X if (len <= 0)
- X FAIL("internal disaster");
- X ender = *(regparse+len);
- X if (len > 1 && ISMULT(ender))
- X len--; /* Back off clear of ?+* operand. */
- X *flagp |= HASWIDTH;
- X if (len == 1)
- X *flagp |= SIMPLE;
- X ret = regnode(EXACTLY);
- X while (len > 0) {
- X regc(*regparse++);
- X len--;
- X }
- X regc('\0');
- X }
- X break;
- X }
- X
- X return(ret);
- X}
- X
- X/*
- X - regnode - emit a node
- X */
- Xstatic char * /* Location. */
- Xregnode(op)
- Xchar op;
- X{
- X register char *ret;
- X register char *ptr;
- X
- X ret = regcode;
- X if (ret == ®dummy) {
- X regsize += 3;
- X return(ret);
- X }
- X
- X ptr = ret;
- X *ptr++ = op;
- X *ptr++ = '\0'; /* Null "next" pointer. */
- X *ptr++ = '\0';
- X regcode = ptr;
- X
- X return(ret);
- X}
- X
- X/*
- X - regc - emit (if appropriate) a byte of code
- X */
- Xstatic void
- Xregc(b)
- Xchar b;
- X{
- X if (regcode != ®dummy)
- X *regcode++ = b;
- X else
- X regsize++;
- X}
- X
- X/*
- X - reginsert - insert an operator in front of already-emitted operand
- X *
- X * Means relocating the operand.
- X */
- Xstatic void
- Xreginsert(op, opnd)
- Xchar op;
- Xchar *opnd;
- X{
- X register char *src;
- X register char *dst;
- X register char *place;
- X
- X if (regcode == ®dummy) {
- X regsize += 3;
- X return;
- X }
- X
- X src = regcode;
- X regcode += 3;
- X dst = regcode;
- X while (src > opnd)
- X *--dst = *--src;
- X
- X place = opnd; /* Op node, where operand used to be. */
- X *place++ = op;
- X *place++ = '\0';
- X *place++ = '\0';
- X}
- X
- X/*
- X - regtail - set the next-pointer at the end of a node chain
- X */
- Xstatic void
- Xregtail(p, val)
- Xchar *p;
- Xchar *val;
- X{
- X register char *scan;
- X register char *temp;
- X register int offset;
- X
- X if (p == ®dummy)
- X return;
- X
- X /* Find last node. */
- X scan = p;
- X for (;;) {
- X temp = regnext(scan);
- X if (temp == NULL)
- X break;
- X scan = temp;
- X }
- X
- X if (OP(scan) == BACK)
- X offset = scan - val;
- X else
- X offset = val - scan;
- X *(scan+1) = (offset>>8)&0377;
- X *(scan+2) = offset&0377;
- X}
- X
- X/*
- X - regoptail - regtail on operand of first argument; nop if operandless
- X */
- Xstatic void
- Xregoptail(p, val)
- Xchar *p;
- Xchar *val;
- X{
- X /* "Operandless" and "op != BRANCH" are synonymous in practice. */
- X if (p == NULL || p == ®dummy || OP(p) != BRANCH)
- X return;
- X regtail(OPERAND(p), val);
- X}
- X
- X/*
- X * regexec and friends
- X */
- X
- X/*
- X * Global work variables for regexec().
- X */
- Xstatic char *reginput; /* String-input pointer. */
- Xstatic char *regbol; /* Beginning of input, for ^ check. */
- Xstatic char **regstartp; /* Pointer to startp array. */
- Xstatic char **regendp; /* Ditto for endp. */
- X
- X/*
- X * Forwards.
- X */
- XSTATIC int regtry();
- XSTATIC int regmatch();
- XSTATIC int regrepeat();
- X
- X#ifdef DEBUG
- Xint regnarrate = 0;
- Xvoid regdump();
- XSTATIC char *regprop();
- X#endif
- X
- X/*
- X - regexec - match a regexp against a string
- X */
- Xint
- Xregexec(prog, string, at_bol)
- Xregister regexp *prog;
- Xregister char *string;
- Xint at_bol;
- X{
- X register char *s;
- X extern char *cstrchr();
- X
- X /* Be paranoid... */
- X if (prog == NULL || string == NULL) {
- X regerror("NULL parameter");
- X return(0);
- X }
- X
- X /* Check validity of program. */
- X if (UCHARAT(prog->program) != MAGIC) {
- X regerror("corrupted program");
- X return(0);
- X }
- X
- X /* If there is a "must appear" string, look for it. */
- X if (prog->regmust != NULL) {
- X s = string;
- X while ((s = cstrchr(s, prog->regmust[0])) != NULL) {
- X if (cstrncmp(s, prog->regmust, prog->regmlen) == 0)
- X break; /* Found it. */
- X s++;
- X }
- X if (s == NULL) /* Not present. */
- X return(0);
- X }
- X
- X /* Mark beginning of line for ^ . */
- X if (at_bol)
- X regbol = string; /* is possible to match bol */
- X else
- X regbol = NULL; /* we aren't there, so don't match it */
- X
- X /* Simplest case: anchored match need be tried only once. */
- X if (prog->reganch)
- X return(regtry(prog, string));
- X
- X /* Messy cases: unanchored match. */
- X s = string;
- X if (prog->regstart != '\0')
- X /* We know what char it must start with. */
- X while ((s = cstrchr(s, prog->regstart)) != NULL) {
- X if (regtry(prog, s))
- X return(1);
- X s++;
- X }
- X else
- X /* We don't -- general case. */
- X do {
- X if (regtry(prog, s))
- X return(1);
- X } while (*s++ != '\0');
- X
- X /* Failure. */
- X return(0);
- X}
- X
- X/*
- X - regtry - try match at specific point
- X */
- Xstatic int /* 0 failure, 1 success */
- Xregtry(prog, string)
- Xregexp *prog;
- Xchar *string;
- X{
- X register int i;
- X register char **sp;
- X register char **ep;
- X
- X reginput = string;
- X regstartp = prog->startp;
- X regendp = prog->endp;
- X
- X sp = prog->startp;
- X ep = prog->endp;
- X for (i = NSUBEXP; i > 0; i--) {
- X *sp++ = NULL;
- X *ep++ = NULL;
- X }
- X if (regmatch(prog->program + 1)) {
- X prog->startp[0] = string;
- X prog->endp[0] = reginput;
- X return(1);
- X } else
- X return(0);
- X}
- X
- X/*
- X - regmatch - main matching routine
- X *
- X * Conceptually the strategy is simple: check to see whether the current
- X * node matches, call self recursively to see whether the rest matches,
- X * and then act accordingly. In practice we make some effort to avoid
- X * recursion, in particular by going through "ordinary" nodes (that don't
- X * need to know whether the rest of the match failed) by a loop instead of
- X * by recursion.
- X */
- Xstatic int /* 0 failure, 1 success */
- Xregmatch(prog)
- Xchar *prog;
- X{
- X register char *scan; /* Current node. */
- X char *next; /* Next node. */
- X extern char *strchr();
- X
- X scan = prog;
- X#ifdef DEBUG
- X if (scan != NULL && regnarrate)
- X fprintf(stderr, "%s(\n", regprop(scan));
- X#endif
- X while (scan != NULL) {
- X#ifdef DEBUG
- X if (regnarrate)
- X fprintf(stderr, "%s...\n", regprop(scan));
- X#endif
- X next = regnext(scan);
- X
- X switch (OP(scan)) {
- X case BOL:
- X if (reginput != regbol)
- X return(0);
- X break;
- X case EOL:
- X if (*reginput != '\0')
- X return(0);
- X break;
- X case ANY:
- X if (*reginput == '\0')
- X return(0);
- X reginput++;
- X break;
- X case EXACTLY: {
- X register int len;
- X register char *opnd;
- X
- X opnd = OPERAND(scan);
- X /* Inline the first character, for speed. */
- X if (mkup(*opnd) != mkup(*reginput))
- X return(0);
- X len = strlen(opnd);
- X if (len > 1 && cstrncmp(opnd,reginput,len) != 0)
- X return(0);
- X reginput += len;
- X }
- X break;
- X case ANYOF:
- X if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
- X return(0);
- X reginput++;
- X break;
- X case ANYBUT:
- X if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
- X return(0);
- X reginput++;
- X break;
- X case NOTHING:
- X break;
- X case BACK:
- X break;
- X case OPEN+1:
- X case OPEN+2:
- X case OPEN+3:
- X case OPEN+4:
- X case OPEN+5:
- X case OPEN+6:
- X case OPEN+7:
- X case OPEN+8:
- X case OPEN+9: {
- X register int no;
- X register char *save;
- X
- X no = OP(scan) - OPEN;
- X save = reginput;
- X
- X if (regmatch(next)) {
- X /*
- X * Don't set startp if some later
- X * invocation of the same parentheses
- X * already has.
- X */
- X if (regstartp[no] == NULL)
- X regstartp[no] = save;
- X return(1);
- X } else
- X return(0);
- X }
- X break;
- X case CLOSE+1:
- X case CLOSE+2:
- X case CLOSE+3:
- X case CLOSE+4:
- X case CLOSE+5:
- X case CLOSE+6:
- X case CLOSE+7:
- X case CLOSE+8:
- X case CLOSE+9: {
- X register int no;
- X register char *save;
- X
- X no = OP(scan) - CLOSE;
- X save = reginput;
- X
- X if (regmatch(next)) {
- X /*
- X * Don't set endp if some later
- X * invocation of the same parentheses
- X * already has.
- X */
- X if (regendp[no] == NULL)
- X regendp[no] = save;
- X return(1);
- X } else
- X return(0);
- X }
- X break;
- X case BRANCH: {
- X register char *save;
- X
- X if (OP(next) != BRANCH) /* No choice. */
- X next = OPERAND(scan); /* Avoid recursion. */
- X else {
- X do {
- X save = reginput;
- X if (regmatch(OPERAND(scan)))
- X return(1);
- X reginput = save;
- X scan = regnext(scan);
- X } while (scan != NULL && OP(scan) == BRANCH);
- X return(0);
- X /* NOTREACHED */
- X }
- X }
- X break;
- X case STAR:
- X case PLUS: {
- X register char nextch;
- X register int no;
- X register char *save;
- X register int min;
- X
- X /*
- X * Lookahead to avoid useless match attempts
- X * when we know what character comes next.
- X */
- X nextch = '\0';
- X if (OP(next) == EXACTLY)
- X nextch = *OPERAND(next);
- X min = (OP(scan) == STAR) ? 0 : 1;
- X save = reginput;
- X no = regrepeat(OPERAND(scan));
- X while (no >= min) {
- X /* If it could work, try it. */
- X if (nextch == '\0' || *reginput == nextch)
- X if (regmatch(next))
- X return(1);
- X /* Couldn't or didn't -- back up. */
- X no--;
- X reginput = save + no;
- X }
- X return(0);
- X }
- X break;
- X case END:
- X return(1); /* Success! */
- X break;
- X default:
- X regerror("memory corruption");
- X return(0);
- X break;
- X }
- X
- X scan = next;
- X }
- X
- X /*
- X * We get here only if there's trouble -- normally "case END" is
- X * the terminating point.
- X */
- X regerror("corrupted pointers");
- X return(0);
- X}
- X
- X/*
- X - regrepeat - repeatedly match something simple, report how many
- X */
- Xstatic int
- Xregrepeat(p)
- Xchar *p;
- X{
- X register int count = 0;
- X register char *scan;
- X register char *opnd;
- X
- X scan = reginput;
- X opnd = OPERAND(p);
- X switch (OP(p)) {
- X case ANY:
- X count = strlen(scan);
- X scan += count;
- X break;
- X case EXACTLY:
- X while (mkup(*opnd) == mkup(*scan)) {
- X count++;
- X scan++;
- X }
- X break;
- X case ANYOF:
- X while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
- X count++;
- X scan++;
- X }
- X break;
- X case ANYBUT:
- X while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
- X count++;
- X scan++;
- X }
- X break;
- X default: /* Oh dear. Called inappropriately. */
- X regerror("internal foulup");
- X count = 0; /* Best compromise. */
- X break;
- X }
- X reginput = scan;
- X
- X return(count);
- X}
- X
- X/*
- X - regnext - dig the "next" pointer out of a node
- X */
- Xstatic char *
- Xregnext(p)
- Xregister char *p;
- X{
- X register int offset;
- X
- X if (p == ®dummy)
- X return(NULL);
- X
- X offset = NEXT(p);
- X if (offset == 0)
- X return(NULL);
- X
- X if (OP(p) == BACK)
- X return(p-offset);
- X else
- X return(p+offset);
- X}
- X
- X#ifdef DEBUG
- X
- XSTATIC char *regprop();
- X
- X/*
- X - regdump - dump a regexp onto stdout in vaguely comprehensible form
- X */
- Xvoid
- Xregdump(r)
- Xregexp *r;
- X{
- X register char *s;
- X register char op = EXACTLY; /* Arbitrary non-END op. */
- X register char *next;
- X extern char *strchr();
- X
- X
- X s = r->program + 1;
- X while (op != END) { /* While that wasn't END last time... */
- X op = OP(s);
- X printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
- X next = regnext(s);
- X if (next == NULL) /* Next ptr. */
- X printf("(0)");
- X else
- X printf("(%d)", (s-r->program)+(next-s));
- X s += 3;
- X if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
- X /* Literal string, where present. */
- X while (*s != '\0') {
- X putchar(*s);
- X s++;
- X }
- X s++;
- X }
- X putchar('\n');
- X }
- X
- X /* Header fields of interest. */
- X if (r->regstart != '\0')
- X printf("start `%c' ", r->regstart);
- X if (r->reganch)
- X printf("anchored ");
- X if (r->regmust != NULL)
- X printf("must have \"%s\"", r->regmust);
- X printf("\n");
- X}
- X
- X/*
- X - regprop - printable representation of opcode
- X */
- Xstatic char *
- Xregprop(op)
- Xchar *op;
- X{
- X register char *p;
- X static char buf[50];
- X
- X (void) strcpy(buf, ":");
- X
- X switch (OP(op)) {
- X case BOL:
- X p = "BOL";
- X break;
- X case EOL:
- X p = "EOL";
- X break;
- X case ANY:
- X p = "ANY";
- X break;
- X case ANYOF:
- X p = "ANYOF";
- X break;
- X case ANYBUT:
- X p = "ANYBUT";
- X break;
- X case BRANCH:
- X p = "BRANCH";
- X break;
- X case EXACTLY:
- X p = "EXACTLY";
- X break;
- X case NOTHING:
- X p = "NOTHING";
- X break;
- X case BACK:
- X p = "BACK";
- X break;
- X case END:
- X p = "END";
- X break;
- X case OPEN+1:
- X case OPEN+2:
- X case OPEN+3:
- X case OPEN+4:
- X case OPEN+5:
- X case OPEN+6:
- X case OPEN+7:
- X case OPEN+8:
- X case OPEN+9:
- X sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
- X p = NULL;
- X break;
- X case CLOSE+1:
- X case CLOSE+2:
- X case CLOSE+3:
- X case CLOSE+4:
- X case CLOSE+5:
- X case CLOSE+6:
- X case CLOSE+7:
- X case CLOSE+8:
- X case CLOSE+9:
- X sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
- X p = NULL;
- X break;
- X case STAR:
- X p = "STAR";
- X break;
- X case PLUS:
- X p = "PLUS";
- X break;
- X default:
- X regerror("corrupted opcode");
- X break;
- X }
- X if (p != NULL)
- X (void) strcat(buf, p);
- X return(buf);
- X}
- X#endif
- X
- X/*
- X * The following is provided for those people who do not have strcspn() in
- X * their C libraries. They should get off their butts and do something
- X * about it; at least one public-domain implementation of those (highly
- X * useful) string routines has been published on Usenet.
- X */
- X#ifdef STRCSPN
- X/*
- X * strcspn - find length of initial segment of s1 consisting entirely
- X * of characters not from s2
- X */
- X
- Xstatic int
- Xstrcspn(s1, s2)
- Xchar *s1;
- Xchar *s2;
- X{
- X register char *scan1;
- X register char *scan2;
- X register int count;
- X
- X count = 0;
- X for (scan1 = s1; *scan1 != '\0'; scan1++) {
- X for (scan2 = s2; *scan2 != '\0';) /* ++ moved down. */
- X if (*scan1 == *scan2++)
- X return(count);
- X count++;
- X }
- X return(count);
- X}
- X#endif
- X
- Xint
- Xcstrncmp(s1, s2, n)
- Xchar *s1, *s2;
- Xint n;
- X{
- X char *p, *S1, *S2, *strsave();
- X int rval;
- X
- X if (!reg_ic)
- X return (strncmp(s1, s2, n));
- X
- X S1 = strsave(s1);
- X S2 = strsave(s2);
- X
- X for (p = S1; *p ;p++)
- X if (islower(*p))
- X *p = toupper(*p);
- X
- X for (p = S2; *p ;p++)
- X if (islower(*p))
- X *p = toupper(*p);
- X
- X rval = strncmp(S1, S2, n);
- X
- X free(S1);
- X free(S2);
- X
- X return rval;
- X}
- X
- Xchar *
- Xcstrchr(s, c)
- Xchar *s;
- Xchar c;
- X{
- X char *p;
- X
- X for (p = s; *p ;p++) {
- X if (mkup(*p) == mkup(c))
- X return p;
- X }
- X return NULL;
- X}
- !EOR!
- echo extracting - regsub.c
- sed 's/^X//' > regsub.c << '!EOR!'
- X/* $Header: /nw/tony/src/stevie/src/RCS/regsub.c,v 1.4 89/03/11 22:43:30 tony Exp $
- X *
- X * NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
- X *
- X * This is NOT the original regular expression code as written by
- X * Henry Spencer. This code has been modified specifically for use
- X * with the STEVIE editor, and should not be used apart from compiling
- X * STEVIE. If you want a good regular expression library, get the
- X * original code. The copyright notice that follows is from the
- X * original.
- X *
- X * NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
- X *
- X * regsub
- X *
- X * Copyright (c) 1986 by University of Toronto.
- X * Written by Henry Spencer. Not derived from licensed software.
- X *
- X * Permission is granted to anyone to use this software for any
- X * purpose on any computer system, and to redistribute it freely,
- X * subject to the following restrictions:
- X *
- X * 1. The author is not responsible for the consequences of use of
- X * this software, no matter how awful, even if they arise
- X * from defects in it.
- X *
- X * 2. The origin of this software must not be misrepresented, either
- X * by explicit claim or by omission.
- X *
- X * 3. Altered versions must be plainly marked as such, and must not
- X * be misrepresented as being the original software.
- X *
- X */
- X
- X#include <stdio.h>
- X#include "regexp.h"
- X#include "regmagic.h"
- X
- X#ifndef CHARBITS
- X#define UCHARAT(p) ((int)*(unsigned char *)(p))
- X#else
- X#define UCHARAT(p) ((int)*(p)&CHARBITS)
- X#endif
- X
- X/*
- X - regsub - perform substitutions after a regexp match
- X */
- Xvoid
- Xregsub(prog, source, dest)
- Xregexp *prog;
- Xchar *source;
- Xchar *dest;
- X{
- X register char *src;
- X register char *dst;
- X register char c;
- X register int no;
- X register int len;
- X extern char *strncpy();
- X
- X if (prog == NULL || source == NULL || dest == NULL) {
- X regerror("NULL parm to regsub");
- X return;
- X }
- X if (UCHARAT(prog->program) != MAGIC) {
- X regerror("damaged regexp fed to regsub");
- X return;
- X }
- X
- X src = source;
- X dst = dest;
- X while ((c = *src++) != '\0') {
- X if (c == '&')
- X no = 0;
- X else if (c == '\\' && '0' <= *src && *src <= '9')
- X no = *src++ - '0';
- X else
- X no = -1;
- X if (no < 0) { /* Ordinary character. */
- X if (c == '\\' && (*src == '\\' || *src == '&'))
- X c = *src++;
- X *dst++ = c;
- X } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
- X len = prog->endp[no] - prog->startp[no];
- X (void) strncpy(dst, prog->startp[no], len);
- X dst += len;
- X if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
- X regerror("damaged match string");
- X return;
- X }
- X }
- X }
- X *dst++ = '\0';
- X}
- !EOR!
- echo extracting - screen.c
- sed 's/^X//' > screen.c << '!EOR!'
- X/* $Header: /nw/tony/src/stevie/src/RCS/screen.c,v 1.8 89/08/02 09:26:33 tony Exp $
- X *
- X * Routines to manipulate the screen representations.
- X */
- X
- X#include "stevie.h"
- X
- X/*
- X * This gets set if we ignored an update request while input was pending.
- X * We check this when the input is drained to see if the screen should be
- X * updated.
- X */
- Xbool_t need_redraw = FALSE;
- X
- X/*
- X * The following variable is set (in filetonext) to the number of physical
- X * lines taken by the line the cursor is on. We use this to avoid extra
- X * calls to plines(). The optimized routines lfiletonext() and lnexttoscreen()
- X * make sure that the size of the cursor line hasn't changed. If so, lines
- X * below the cursor will move up or down and we need to call the routines
- X * filetonext() and nexttoscreen() to examine the entire screen.
- X */
- Xstatic int Cline_size; /* size (in rows) of the cursor line */
- Xstatic int Cline_row; /* starting row of the cursor line */
- X
- Xstatic char *mkline(); /* calculate line string for "number" mode */
- X
- X/*
- X * filetonext()
- X *
- X * Based on the current value of Topchar, transfer a screenfull of
- X * stuff from Filemem to Nextscreen, and update Botchar.
- X */
- X
- Xstatic void
- Xfiletonext()
- X{
- X register int row, col;
- X register char *screenp = Nextscreen;
- X LPTR memp;
- X LPTR save; /* save pos. in case line won't fit */
- X register char *endscreen;
- X register char *nextrow;
- X char extra[16];
- X int nextra = 0;
- X register int c;
- X int n;
- X bool_t done; /* if TRUE, we hit the end of the file */
- X bool_t didline; /* if TRUE, we finished the last line */
- X int srow; /* starting row of the current line */
- X int lno; /* number of the line we're doing */
- X int coff; /* column offset */
- X
- X coff = P(P_NU) ? 8 : 0;
- X
- X save = memp = *Topchar;
- X
- X if (P(P_NU))
- X lno = cntllines(Filemem, Topchar);
- X
- X /*
- X * The number of rows shown is Rows-1.
- X * The last line is the status/command line.
- X */
- X endscreen = &screenp[(Rows-1)*Columns];
- X
- X done = didline = FALSE;
- X srow = row = col = 0;
- X /*
- X * We go one past the end of the screen so we can find out if the
- X * last line fit on the screen or not.
- X */
- X while ( screenp <= endscreen && !done) {
- X
- X
- X if (P(P_NU) && col == 0 && memp.index == 0) {
- X strcpy(extra, mkline(lno++));
- X nextra = 8;
- X }
- X
- X /* Get the next character to put on the screen. */
- X
- X /* The 'extra' array contains the extra stuff that is */
- X /* inserted to represent special characters (tabs, and */
- X /* other non-printable stuff. The order in the 'extra' */
- X /* array is reversed. */
- X
- X if ( nextra > 0 )
- X c = extra[--nextra];
- X else {
- X c = (unsigned)(0xff & gchar(&memp));
- X if (inc(&memp) == -1)
- X done = 1;
- X /* when getting a character from the file, we */
- X /* may have to turn it into something else on */
- X /* the way to putting it into 'Nextscreen'. */
- X if ( c == TAB && !P(P_LS) ) {
- X strcpy(extra," ");
- X /* tab amount depends on current column */
- X nextra = ((P(P_TS)-1) - (col - coff)%P(P_TS));
- X c = ' ';
- X }
- X else if ( c == NUL && P(P_LS) ) {
- X extra[0] = NUL;
- X nextra = 1;
- X c = '$';
- X } else if ( (n = chars[c].ch_size) > 1 ) {
- X char *p;
- X nextra = 0;
- X p = chars[c].ch_str;
- X /* copy 'ch-str'ing into 'extra' in reverse */
- X while ( n > 1 )
- X extra[nextra++] = p[--n];
- X c = p[0];
- X }
- X }
- X
- X if (screenp == endscreen) {
- X /*
- X * We're one past the end of the screen. If the
- X * current character is null, then we really did
- X * finish, so set didline = TRUE. In either case,
- X * break out because we're done.
- X */
- X dec(&memp);
- X if (memp.index != 0 && c == NUL) {
- X didline = TRUE;
- X inc(&memp);
- X }
- X break;
- X }
- X
- X if ( c == NUL ) {
- X srow = ++row;
- X /*
- X * Save this position in case the next line won't
- X * fit on the screen completely.
- X */
- X save = memp;
- X /* get pointer to start of next row */
- X nextrow = &Nextscreen[row*Columns];
- X /* blank out the rest of this row */
- X while ( screenp != nextrow )
- X *screenp++ = ' ';
- X col = 0;
- X continue;
- X }
- X if ( col >= Columns ) {
- X row++;
- X col = 0;
- X }
- X /* store the character in Nextscreen */
- X *screenp++ = c;
- X col++;
- X }
- X /*
- X * If we didn't hit the end of the file, and we didn't finish
- X * the last line we were working on, then the line didn't fit.
- X */
- X if (!done && !didline) {
- X /*
- X * Clear the rest of the screen and mark the unused lines.
- X */
- X screenp = &Nextscreen[srow * Columns];
- X while (screenp < endscreen)
- X *screenp++ = ' ';
- X for (; srow < (Rows-1) ;srow++)
- X Nextscreen[srow * Columns] = '@';
- X *Botchar = save;
- X return;
- X }
- X /* make sure the rest of the screen is blank */
- X while ( screenp < endscreen )
- X *screenp++ = ' ';
- X /* put '~'s on rows that aren't part of the file. */
- X if ( col != 0 )
- X row++;
- X while ( row < Rows ) {
- X Nextscreen[row*Columns] = '~';
- X row++;
- X }
- X if (done) /* we hit the end of the file */
- X *Botchar = *Fileend;
- X else
- X *Botchar = memp; /* FIX - prev? */
- X}
- X
- X/*
- X * nexttoscreen
- X *
- X * Transfer the contents of Nextscreen to the screen, using Realscreen
- X * to avoid unnecessary output.
- X */
- Xstatic void
- Xnexttoscreen()
- X{
- X register char *np = Nextscreen;
- X register char *rp = Realscreen;
- X register char *endscreen;
- X register int row = 0, col = 0;
- X int gorow = -1, gocol = -1;
- X
- X if (anyinput()) {
- X need_redraw = TRUE;
- X return;
- X }
- X
- X endscreen = &np[(Rows-1)*Columns];
- X
- X CUROFF; /* disable cursor */
- X
- X for ( ; np < endscreen ; np++,rp++ ) {
- X /* If desired screen (contents of Nextscreen) does not */
- X /* match what's really there, put it there. */
- X if ( *np != *rp ) {
- X /* if we are positioned at the right place, */
- X /* we don't have to use windgoto(). */
- X if (gocol != col || gorow != row) {
- X /*
- X * If we're just off by one, don't send
- X * an entire esc. seq. (this happens a lot!)
- X */
- X if (gorow == row && gocol+1 == col) {
- X outchar(*(np-1));
- X gocol++;
- X } else
- X windgoto(gorow=row,gocol=col);
- X }
- X outchar(*rp = *np);
- X gocol++;
- X }
- X if ( ++col >= Columns ) {
- X col = 0;
- X row++;
- X }
- X }
- X CURON; /* enable cursor again */
- X}
- X
- X/*
- X * lfiletonext() - like filetonext() but only for cursor line
- X *
- X * Returns true if the size of the cursor line (in rows) hasn't changed.
- X * This determines whether or not we need to call filetonext() to examine
- X * the entire screen for changes.
- X */
- Xstatic bool_t
- Xlfiletonext()
- X{
- X register int row, col;
- X register char *screenp;
- X LPTR memp;
- X register char *nextrow;
- X char extra[16];
- X int nextra = 0;
- X register int c;
- X int n;
- X bool_t eof;
- X int lno; /* number of the line we're doing */
- X int coff; /* column offset */
- X
- X coff = P(P_NU) ? 8 : 0;
- X
- X /*
- X * This should be done more efficiently.
- X */
- X if (P(P_NU))
- X lno = cntllines(Filemem, Curschar);
- X
- X screenp = Nextscreen + (Cline_row * Columns);
- X
- X memp = *Curschar;
- X memp.index = 0;
- X
- X eof = FALSE;
- X col = 0;
- X row = Cline_row;
- X
- X while (!eof) {
- X
- X if (P(P_NU) && col == 0 && memp.index == 0) {
- X strcpy(extra, mkline(lno));
- X nextra = 8;
- X }
- X
- X /* Get the next character to put on the screen. */
- X
- X /* The 'extra' array contains the extra stuff that is */
- X /* inserted to represent special characters (tabs, and */
- X /* other non-printable stuff. The order in the 'extra' */
- X /* array is reversed. */
- X
- X if ( nextra > 0 )
- X c = extra[--nextra];
- X else {
- X c = (unsigned)(0xff & gchar(&memp));
- X if (inc(&memp) == -1)
- X eof = TRUE;
- X /* when getting a character from the file, we */
- X /* may have to turn it into something else on */
- X /* the way to putting it into 'Nextscreen'. */
- X if ( c == TAB && !P(P_LS) ) {
- X strcpy(extra," ");
- X /* tab amount depends on current column */
- X nextra = ((P(P_TS)-1) - (col - coff)%P(P_TS));
- X c = ' ';
- X } else if ( c == NUL && P(P_LS) ) {
- X extra[0] = NUL;
- X nextra = 1;
- X c = '$';
- X } else if ( c != NUL && (n=chars[c].ch_size) > 1 ) {
- X char *p;
- X nextra = 0;
- X p = chars[c].ch_str;
- X /* copy 'ch-str'ing into 'extra' in reverse */
- X while ( n > 1 )
- X extra[nextra++] = p[--n];
- X c = p[0];
- X }
- X }
- X
- X if ( c == NUL ) {
- X row++;
- X /* get pointer to start of next row */
- X nextrow = &Nextscreen[row*Columns];
- X /* blank out the rest of this row */
- X while ( screenp != nextrow )
- X *screenp++ = ' ';
- X col = 0;
- X break;
- X }
- X
- X if ( col >= Columns ) {
- X row++;
- X col = 0;
- X }
- X /* store the character in Nextscreen */
- X *screenp++ = c;
- X col++;
- X }
- X return ((row - Cline_row) == Cline_size);
- X}
- X
- X/*
- X * lnexttoscreen
- X *
- X * Like nexttoscreen() but only for the cursor line.
- X */
- Xstatic void
- Xlnexttoscreen()
- X{
- X register char *np = Nextscreen + (Cline_row * Columns);
- X register char *rp = Realscreen + (Cline_row * Columns);
- X register char *endline;
- X register int row, col;
- X int gorow = -1, gocol = -1;
- X
- X if (anyinput()) {
- X need_redraw = TRUE;
- X return;
- X }
- X
- X endline = np + (Cline_size * Columns);
- X
- X row = Cline_row;
- X col = 0;
- X
- X CUROFF; /* disable cursor */
- X
- X for ( ; np < endline ; np++,rp++ ) {
- X /* If desired screen (contents of Nextscreen) does not */
- X /* match what's really there, put it there. */
- X if ( *np != *rp ) {
- X /* if we are positioned at the right place, */
- X /* we don't have to use windgoto(). */
- X if (gocol != col || gorow != row) {
- X /*
- X * If we're just off by one, don't send
- X * an entire esc. seq. (this happens a lot!)
- X */
- X if (gorow == row && gocol+1 == col) {
- X outchar(*(np-1));
- X gocol++;
- X } else
- X windgoto(gorow=row,gocol=col);
- X }
- X outchar(*rp = *np);
- X gocol++;
- X }
- X if ( ++col >= Columns ) {
- X col = 0;
- X row++;
- X }
- X }
- X CURON; /* enable cursor again */
- X}
- X
- Xstatic char *
- Xmkline(n)
- Xregister int n;
- X{
- X static char lbuf[9];
- X register int i = 2;
- X
- X strcpy(lbuf, " ");
- X
- X lbuf[i++] = (n % 10) + '0';
- X n /= 10;
- X if (n != 0) {
- X lbuf[i++] = (n % 10) + '0';
- X n /= 10;
- X }
- X if (n != 0) {
- X lbuf[i++] = (n % 10) + '0';
- X n /= 10;
- X }
- X if (n != 0) {
- X lbuf[i++] = (n % 10) + '0';
- X n /= 10;
- X }
- X if (n != 0) {
- X lbuf[i++] = (n % 10) + '0';
- X n /= 10;
- X }
- X return lbuf;
- X}
- X
- X/*
- X * updateline() - update the line the cursor is on
- X *
- X * Updateline() is called after changes that only affect the line that
- X * the cursor is on. This improves performance tremendously for normal
- X * insert mode operation. The only thing we have to watch for is when
- X * the cursor line grows or shrinks around a row boundary. This means
- X * we have to repaint other parts of the screen appropriately. If
- X * lfiletonext() returns FALSE, the size of the cursor line (in rows)
- X * has changed and we have to call updatescreen() to do a complete job.
- X */
- Xvoid
- Xupdateline()
- X{
- X if (!lfiletonext())
- X updatescreen(); /* bag it, do the whole screen */
- X else
- X lnexttoscreen();
- X}
- X
- Xvoid
- Xupdatescreen()
- X{
- X extern bool_t interactive;
- X
- X if (interactive) {
- X filetonext();
- X nexttoscreen();
- X }
- X}
- X
- X/*
- X * prt_line() - print the given line
- X */
- Xvoid
- Xprt_line(s)
- Xchar *s;
- X{
- X register int si = 0;
- X register int c;
- X register int col = 0;
- X
- X char extra[16];
- X int nextra = 0;
- X int n;
- X
- X for (;;) {
- X
- X if ( nextra > 0 )
- X c = extra[--nextra];
- X else {
- X c = s[si++];
- X if ( c == TAB && !P(P_LS) ) {
- X strcpy(extra, " ");
- X /* tab amount depends on current column */
- X nextra = (P(P_TS) - 1) - col%P(P_TS);
- X c = ' ';
- X } else if ( c == NUL && P(P_LS) ) {
- X extra[0] = NUL;
- X nextra = 1;
- X c = '$';
- X } else if ( c != NUL && (n=chars[c].ch_size) > 1 ) {
- X char *p;
- X
- X nextra = 0;
- X p = chars[c].ch_str;
- X /* copy 'ch-str'ing into 'extra' in reverse */
- X while ( n > 1 )
- X extra[nextra++] = p[--n];
- X c = p[0];
- X }
- X }
- X
- X if ( c == NUL )
- X break;
- X
- X outchar(c);
- X col++;
- X }
- X}
- X
- Xvoid
- Xscreenclear()
- X{
- X register char *rp, *np;
- X register char *end;
- X
- X CLS; /* clear the display */
- X
- X rp = Realscreen;
- X end = Realscreen + Rows * Columns;
- X np = Nextscreen;
- X
- X /* blank out the stored screens */
- X while (rp != end)
- X *rp++ = *np++ = ' ';
- X}
- X
- Xvoid
- Xcursupdate()
- X{
- X register LPTR *p;
- X register int icnt, c, nlines;
- X register int i;
- X int didinc;
- X
- X if (bufempty()) { /* special case - file is empty */
- X *Topchar = *Filemem;
- X *Curschar = *Filemem;
- X } else if ( LINEOF(Curschar) < LINEOF(Topchar) ) {
- X nlines = cntllines(Curschar,Topchar);
- X /* if the cursor is above the top of */
- X /* the screen, put it at the top of the screen.. */
- X *Topchar = *Curschar;
- X Topchar->index = 0;
- X /* ... and, if we weren't very close to begin with, */
- X /* we scroll so that the line is close to the middle. */
- X if ( nlines > Rows/3 ) {
- X for (i=0, p = Topchar; i < Rows/3 ;i++, *Topchar = *p)
- X if ((p = prevline(p)) == NULL)
- X break;
- X } else
- X s_ins(0, nlines-1);
- X updatescreen();
- X }
- X else if (LINEOF(Curschar) >= LINEOF(Botchar)) {
- X nlines = cntllines(Botchar,Curschar);
- X /* If the cursor is off the bottom of the screen, */
- X /* put it at the top of the screen.. */
- X /* ... and back up */
- X if ( nlines > Rows/3 ) {
- X p = Curschar;
- X for (i=0; i < (2*Rows)/3 ;i++)
- X if ((p = prevline(p)) == NULL)
- X break;
- X *Topchar = *p;
- X } else {
- X scrollup(nlines);
- X }
- X updatescreen();
- X }
- X
- X Cursrow = Curscol = Cursvcol = 0;
- X for ( p=Topchar; p->linep != Curschar->linep ;p = nextline(p) )
- X Cursrow += plines(p);
- X
- X Cline_row = Cursrow;
- X Cline_size = plines(p);
- X
- X if (P(P_NU))
- X Curscol = 8;
- X
- X for (i=0; i <= Curschar->index ;i++) {
- X c = Curschar->linep->s[i];
- X /* A tab gets expanded, depending on the current column */
- X if ( c == TAB && !P(P_LS) )
- X icnt = P(P_TS) - (Cursvcol % P(P_TS));
- X else
- X icnt = chars[(unsigned)(c & 0xff)].ch_size;
- X Curscol += icnt;
- X Cursvcol += icnt;
- X if ( Curscol >= Columns ) {
- X Curscol -= Columns;
- X Cursrow++;
- X didinc = TRUE;
- X }
- X else
- X didinc = FALSE;
- X }
- X if (didinc)
- X Cursrow--;
- X
- X if (c == TAB && State == NORMAL && !P(P_LS)) {
- X Curscol--;
- X Cursvcol--;
- X } else {
- X Curscol -= icnt;
- X Cursvcol -= icnt;
- X }
- X if (Curscol < 0)
- X Curscol += Columns;
- X
- X if (set_want_col) {
- X Curswant = Cursvcol;
- X set_want_col = FALSE;
- X }
- X}
- X
- X/*
- X * The rest of the routines in this file perform screen manipulations.
- X * The given operation is performed physically on the screen. The
- X * corresponding change is also made to the internal screen image.
- X * In this way, the editor anticipates the effect of editing changes
- X * on the appearance of the screen. That way, when we call screenupdate
- X * a complete redraw isn't usually necessary. Another advantage is that
- X * we can keep adding code to anticipate screen changes, and in the
- X * meantime, everything still works.
- X */
- X
- X/*
- X * s_ins(row, nlines) - insert 'nlines' lines at 'row'
- X */
- Xvoid
- Xs_ins(row, nlines)
- Xint row;
- Xint nlines;
- X{
- X register char *s, *d; /* src & dest for block copy */
- X register char *e; /* end point for copy */
- X register int i;
- X
- X if ( ! CANIL ) /* can't do it */
- X return;
- X
- X /*
- X * It "looks" better if we do all the inserts at once
- X */
- X SAVCUR; /* save position */
- X windgoto(row, 0);
- X
- X CRTIL( row, nlines );
- X
- X windgoto(Rows-1, 0); /* delete any garbage that may have */
- X CLEOL; /* been shifted to the bottom line */
- X RESCUR; /* restore the cursor position */
- X
- X /*
- X * Now do a block move to update the internal screen image
- X */
- X d = Realscreen + (Columns * (Rows - 1)) - 1;
- X s = d - (Columns * nlines);
- X e = Realscreen + (Columns * row);
- X
- X while (s >= e)
- X *d-- = *s--;
- X
- X /*
- X * Clear the inserted lines
- X */
- X s = Realscreen + (row * Columns);
- X e = s + (nlines * Columns);
- X while (s < e)
- X *s++ = ' ';
- X}
- X
- X/*
- X * s_del(row, nlines) - delete 'nlines' lines at 'row'
- X */
- Xvoid
- Xs_del(row, nlines)
- Xint row;
- Xint nlines;
- X{
- X register char *s, *d, *e;
- X register int i;
- X
- X#ifndef BIOS
- X if ( ! CANDL ) return; /* can't do it */
- X#endif
- X
- X /* delete the lines */
- X SAVCUR; /* save position */
- X
- X windgoto (Rows-1, 0); /* go to status line */
- X CLEOL; /* Clear it */
- X windgoto (row, 0); /* Go to 1st line-to-del */
- X CRTDL( row, nlines ); /* Delete the lines */
- X RESCUR; /* Restore the cursor */
- X
- X /*
- X * do a block move to update the internal image
- X */
- X d = Realscreen + (row * Columns);
- X s = d + (nlines * Columns);
- X e = Realscreen + ((Rows - 1) * Columns);
- X
- X while (s < e)
- X *d++ = *s++;
- X
- X while (d < e) /* clear the lines at the bottom */
- X *d++ = ' ';
- X}
- !EOR!
-
-
-