[comp.sources.amiga] v89i133: stevie - vi editor clone v3.6, Part04/06

page%swap@Sun.COM (Bob Page) (05/12/89)

Submitted-by: grwalter@watmath.waterloo.edu (Fred Walter)
Posting-number: Volume 89, Issue 133
Archive-name: editors/stevie36.4

# This is a shell archive.
# Remove anything above and including the cut line.
# Then run the rest of the file through 'sh'.
# Unpacked files will be owned by you and have default permissions.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar: SHell ARchive
# Run the following text through 'sh' to create:
#	os2.c
#	os2.h
#	param.c
#	param.h
#	porting.doc
#	regexp.c
# This is archive 4 of a 6-part kit.
# This archive created: Thu May 11 19:41:27 1989
echo "extracting os2.c"
sed 's/^X//' << \SHAR_EOF > os2.c
X/*
X * OS/2 System-dependent routines.
X *
X * $Log:        os2.c,v $
X * Revision 1.2  88/04/25  16:50:19  tony
X * Minor changes for OS/2 version 1.1; also fixed up the RCS header.
X * 
X * Revision 1.1  88/03/21  12:04:23  tony
X * Initial revision
X * 
X *
X */
X
X#define INCL_BASE
X#include <os2.h>
X#include "stevie.h"
X
X/*
X * inchar() - get a character from the keyboard
X */
Xint
Xinchar()
X{
X    int             c;
X
X    for (;; beep()) {		/* loop until we get a valid character */
X
X	flushbuf();		/* flush any pending output */
X
X	switch (c = getch()) {
X	  case 0x1e:
X	    return K_CGRAVE;
X	  case 0:		/* special key */
X	  case 0xe0:		/* special key */
X	    if (State != NORMAL) {
X		c = getch();	/* throw away next char */
X		continue;	/* and loop for another char */
X	    }
X	    switch (c = getch()) {
X	      case 0x50:
X		return K_DARROW;
X	      case 0x48:
X		return K_UARROW;
X	      case 0x4b:
X		return K_LARROW;
X	      case 0x4d:
X		return K_RARROW;
X	      case 0x52:
X		return K_INSERT;
X	      case 0x47:
X		stuffReadbuff("1G");
X		return -1;
X	      case 0x4f:
X		stuffReadbuff("G");
X		return -1;
X	      case 0x51:
X		stuffReadbuff(mkstr(CTRL('F')));
X		return -1;
X	      case 0x49:
X		stuffReadbuff(mkstr(CTRL('B')));
X		return -1;
X		/*
X		 * Hard-code some useful function key macros. 
X		 */
X	      case 0x3b:	/* F1 */
X		stuffReadbuff(":p\n");
X		return -1;
X	      case 0x54:	/* SF1 */
X		stuffReadbuff(":p!\n");
X		return -1;
X	      case 0x3c:	/* F2 */
X		stuffReadbuff(":n\n");
X		return -1;
X	      case 0x55:	/* SF2 */
X		stuffReadbuff(":n!\n");
X		return -1;
X	      case 0x3d:	/* F3 */
X		stuffReadbuff(":e #\n");
X		return -1;
X	      case 0x3e:	/* F4 */
X		stuffReadbuff(":rew\n");
X		return -1;
X	      case 0x57:	/* SF4 */
X		stuffReadbuff(":rew!\n");
X		return -1;
X	      case 0x3f:	/* F5 */
X		stuffReadbuff("[[");
X		return -1;
X	      case 0x40:	/* F6 */
X		stuffReadbuff("]]");
X		return -1;
X	      case 0x41:	/* F7 */
X		stuffReadbuff("<<");
X		return -1;
X	      case 0x42:	/* F8 */
X		stuffReadbuff(">>");
X		return -1;
X	      case 0x43:	/* F9 */
X		stuffReadbuff(":x\n");
X		return -1;
X	      case 0x44:	/* F10 */
X		stuffReadbuff(":help\n");
X		return -1;
X	      default:
X		break;
X	    }
X	    break;
X
X	  default:
X	    return c;
X	}
X    }
X}
X
X#define BSIZE   2048
Xstatic char     outbuf[BSIZE];
Xstatic int      bpos = 0;
X
Xflushbuf()
X{
X    if (bpos != 0)
X	write(1, outbuf, bpos);
X    bpos = 0;
X}
X
X/*
X * Macro to output a character. Used within this file for speed.
X */
X#define outone(c)       outbuf[bpos++] = c; if (bpos >= BSIZE) flushbuf()
X
X/*
X * Function version for use outside this file.
X */
X
Xvoid
Xoutchar(c)
X    register char   c;
X{
X    outbuf[bpos++] = c;
X    if (bpos >= BSIZE)
X	flushbuf();
X}
X
Xstatic char     cell[2] = {0, 7};
X
X/*
X * outstr(s) - write a string to the console
X *
X * We implement insert/delete line escape sequences here. This is kind
X * of a kludge, but at least it's localized to a single point.
X */
Xvoid
Xoutstr(s)
X    register char  *s;
X{
X    if (strcmp(s, T_DL) == 0) {	/* delete line */
X	int             r, c;
X
X	flushbuf();
X	VioGetCurPos(&r, &c, 0);
X	VioScrollUp(r, 0, 100, 100, 1, cell, 0);
X	return;
X    }
X    if (strcmp(s, T_IL) == 0) {	/* insert line */
X	int             r, c;
X
X	flushbuf();
X	VioGetCurPos(&r, &c, 0);
X	VioScrollDn(r, 0, 100, 100, 1, cell, 0);
X	return;
X    }
X    while (*s) {
X	outone(*s++);
X    }
X}
X
Xvoid
Xbeep()
X{
X    if (RedrawingDisabled)
X	return;
X
X    outone('\007');
X}
X
Xvoid
Xsleep(n)
X    int             n;
X{
X    DosSleep(1000L * n);
X}
X
Xvoid
Xdelay()
X{
X    flushbuf();
X    DosSleep(300L);
X}
X
Xvoid
Xwindinit()
X{
X    Columns = 80;
X    P(P_LI) = Rows = 25;
X}
X
Xvoid
Xwindexit(r)
X    int             r;
X{
X    flushbuf();
X    exit(r);
X}
X
Xvoid
Xwindgoto(r, c)
X    register int    r, c;
X{
X    flushbuf();
X    VioSetCurPos(r, c, 0);
X}
X
XFILE           *
Xfopenb(fname, mode)
X    char           *fname;
X    char           *mode;
X{
X    FILE           *fopen();
X    char            modestr[16];
X
X    sprintf(modestr, "%sb", mode);
X    return fopen(fname, modestr);
X}
SHAR_EOF
echo "extracting os2.h"
sed 's/^X//' << \SHAR_EOF > os2.h
X/*
X * OS2 Machine-dependent routines. 
X */
X
Xint  inchar();
Xvoid outchar();
Xvoid outstr(), beep();
Xvoid windinit(), windexit(), windgoto();
Xvoid delay();
Xvoid sleep();
SHAR_EOF
echo "extracting param.c"
sed 's/^X//' << \SHAR_EOF > param.c
X/*
X * STEVIE - Simply Try this Editor for VI Enthusiasts
X *
X * Code Contributions By : Tim Thompson           twitch!tjt
X *                         Tony Andrews           onecom!wldrdg!tony 
X *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
X */
X
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
X			    {"", "", 0, 0}	/* end marker */
X};
X
Xstatic void     showparms();
X
Xvoid
Xdoset(arg, inter)
X    char           *arg;	/* parameter string */
X    bool_t          inter;	/* TRUE if called interactively */
X{
X    int             i;
X    char           *s;
X    bool_t          did_lines = FALSE;
X
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    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 (inter && (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	    }
X	} else {		/* boolean */
X	    if (inter && (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	if (inter)
X	    emsg("Unrecognized 'set' option");
X    }
X
X    if (did_lines) {
X	Rows = P(P_LI);
X	screenalloc();		/* allocate new screen buffers */
X	s_clear();
X    }
X    /*
X     * Mark the screen contents as not valid in case we changed something
X     * like "tabstop" or "list" that will change its appearance. 
X     */
X    if (inter)
X	S_NOT_VALID;
X
X    /*
X     * Check the bounds for numeric parameters here 
X     */
X    if (P(P_TS) <= 0 || P(P_TS) > 32) {
X	if (inter)
X	    emsg("Invalid tab size specified");
X	P(P_TS) = 8;
X	return;
X    }
X    if (P(P_SS) <= 0 || P(P_SS) > Rows) {
X	if (inter)
X	    emsg("Invalid scroll size specified");
X	P(P_SS) = 12;
X	return;
X    }
X    /*
X     * Check for another argument, and call doset() recursively, if found. If
X     * any argument results in an error, no further 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, TRUE);	/* recurse on next parameter, if present */
X}
X
Xstatic void
Xshowparms(all)
X    bool_t          all;	/* show ALL parameters */
X{
X    struct param   *p;
X    char            buf[64];
X
X    gotocmdline(YES, NUL);
X    outstr("Parameters:\r\n");
X
X    for (p = &params[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}
SHAR_EOF
echo "extracting param.h"
sed 's/^X//' << \SHAR_EOF > param.h
X/*
X * STEVIE - Simply Try this Editor for VI Enthusiasts
X *
X * Code Contributions By : Tim Thompson           twitch!tjt
X *                         Tony Andrews           onecom!wldrdg!tony 
X *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
X */
X
X/*
X * Settable parameters 
X */
X
Xstruct param {
X    char           *fullname;	/* full parameter name */
X    char           *shortname;	/* permissible abbreviation */
X    int             value;	/* parameter value */
X    int             flags;
X};
X
Xextern struct param params[];
X
X/*
X * Flags 
X */
X#define	P_BOOL		0x01	/* the parameter is boolean */
X#define	P_NUM		0x02	/* the parameter is numeric */
X#define	P_CHANGED	0x04	/* the parameter has been changed */
X
X/*
X * The following are the indices in the params array for each parameter 
X */
X
X/*
X * Numeric parameters 
X */
X#define	P_TS		0	/* tab size */
X#define	P_SS		1	/* scroll size */
X#define	P_RP		2	/* report */
X#define	P_LI		3	/* lines */
X
X/*
X * Boolean parameters 
X */
X#define	P_VB		4	/* visual bell */
X#define	P_SM		5	/* showmatch */
X#define	P_WS		6	/* wrap scan */
X#define	P_EB		7	/* error bells */
X#define	P_MO		8	/* show mode */
X#define	P_BK		9	/* make backups when writing out files */
X#define	P_CR		10	/* use cr-lf to terminate lines on writes */
X#define	P_LS		11	/* show tabs and newlines graphically */
X#define	P_IC		12	/* ignore case in searches */
X#define	P_AI		13	/* auto-indent */
X#define	P_NU		14	/* number lines on the screen */
X
X/*
X * Macro to get the value of a parameter 
X */
X#define	P(n)	(params[n].value)
SHAR_EOF
echo "extracting porting.doc"
sed 's/^X//' << \SHAR_EOF > porting.doc
X
X		 Release Notes for STEVIE - Version 3.31B
X
X			       Porting
X
X		     Tony Andrews -  March 6, 1988
X		    G. R. Walter -  January 6, 1988
X
X
X	Porting the editor is a relatively simple task. Most of the
Xcode is pretty machine-independent. For each environment, there is
Xa file of routines that perform various low-level operations that
Xtend to vary a lot from one machine to another. Another file contains
Xthe escape sequences to be used for each machine.
X
XNote however that 'char' is treated as unsigned so you need to set the
Xappropriate compiler flags if this is not the default.
X
X	The machine-dependent files currently used are:
X
Xtos.c:	 Atari ST - ifdef for either Megamax or Alcyon
Xtos.h
X
Xunix.c:	 UNIX System V
Xunix.h
X
Xos2.c:	 Microsoft OS/2
Xos2.h
X
Xdos.c:   MS DOS
Xdos.h
X
Xamiga.c: Amiga
Xamiga.h
X
Xbsd.c:   BSD 4.3 UNIX
Xbsd.h
X
X	Each of these files are around 150 lines long and deal with
Xlow-level issues like character I/O to the terminal, terminal
Xinitialization, cursor addressing, and so on. There are different
Xtradeoffs to be made depending on the environment. For example, the
XUNIX version buffers terminal output because of the relatively high
Xoverhead of system calls. A quick look at the files will make it clear
Xwhat needs to be done in a new environment.
X
X	Terminal escape sequences are in the file "term.h". These are
Xdefined statically, for the time being. There is some discussion in
Xterm.h regarding which sequences are optional and which are not. The
Xeditor is somewhat flexible in dealing with a lack of terminal
Xcapabilities.
X
X	The character set is in the file "charset.c".
X
X	Because not all C compilers support command line macro definitions,
Xthe #define's for system-specific macros are placed in the file "env.h".
XIf you port to a new system, add another line there to define the macro you
Xchoose for your port.
X
X	The basic process for doing a new port is:
X
X	1. Come up with a macro name to use when ifdef'ing your system-
X	   specific changes. Add a line to 'env.h' to define
X	   the macro name you've chosen.
X
X	2. Look at amiga.c, bsd.c, unix.c, tos.c, dos.c and os2.c and copy the
X	   one that comes closest to working on your system. Then modify your
X	   new file as needed.
X
X	3. Look at term.h and edit the file appropriately adding a new
X	   set of escape sequence definitions for your system.
X
X	4. Compiling and debug the editor.
SHAR_EOF
echo "extracting regexp.c"
sed 's/^X//' << \SHAR_EOF > regexp.c
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 * $Log:        regexp.c,v $
X * Revision 1.2  88/04/28  08:09:45  tony
X * First modification of the regexp library. Added an external variable
X * 'reg_ic' which can be set to indicate that case should be ignored.
X * Added a new parameter to regexec() to indicate that the given string
X * comes from the beginning of a line and is thus eligible to match
X * 'beginning-of-line'.
X * 
X */
X#include "env.h"
X
X#ifdef  MEGAMAX
Xoverlay "regexp"
X#endif
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
X				 * string. */
X#define BRANCH  6		/* node Match this alternative, or the
X				 * 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
X				 * times. */
X#define PLUS    11		/* node Match this (simple) thing 1 or more
X				 * times. */
X#define OPEN    20		/* no   Mark this point in input as start of
X				 * #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)         ((islower(c) && reg_ic) ? 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; &regdummy = 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)
X    char           *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    extern char    *alloc();
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 = &regdummy;
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((unsigned) (sizeof(regexp) + regsize));*/
X    r = (regexp *) alloc((unsigned) (sizeof(regexp) + 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 longest
X	 * literal string that must appear and make it the regmust.  Resolve
X	 * ties in favor of later strings, since the regstart check works
X	 * with the beginning of the r.e. and avoiding duplication
X	 * strengthens checking.  Not a strong reason, but sufficient in the
X	 * 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    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)
X    int             paren;	/* Parenthesized? */
X    int            *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    return (ret);
X}
X
X/*
X - regbranch - one alternative of an | operator
X *
X * Implements the concatenation operator.
X */
Xstatic char    *
Xregbranch(flagp)
X    int            *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)
X    int            *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    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)
X    int            *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; Not Reached */
X      case '?':
X      case '+':
X      case '*':
X	FAIL("?+* follows nothing");
X	/* break; Not Reached */
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)
X    char            op;
X{
X    register char  *ret;
X    register char  *ptr;
X
X    ret = regcode;
X    if (ret == &regdummy) {
X	regsize += 3;
X	return (ret);
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)
X    char            b;
X{
X    if (regcode != &regdummy)
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)
X    char            op;
X    char           *opnd;
X{
X    register char  *src;
X    register char  *dst;
X    register char  *place;
X
X    if (regcode == &regdummy) {
X	regsize += 3;
X	return;
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)
X    char           *p;
X    char           *val;
X{
X    register char  *scan;
X    register char  *temp;
X    register int    offset;
X
X    if (p == &regdummy)
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) = (char) ((offset >> 8) & 0377);
X    *(scan + 2) = (char) (offset & 0377);
X}
X
X/*
X - regoptail - regtail on operand of first argument; nop if operandless
X */
Xstatic void
Xregoptail(p, val)
X    char           *p;
X    char           *val;
X{
X    /* "Operandless" and "op != BRANCH" are synonymous in practice. */
X    if (p == NULL || p == &regdummy || 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)
X    register regexp *prog;
X    register char  *string;
X    int             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    /* Check validity of program. */
X    if (UCHARAT(prog->program) != MAGIC) {
X	regerror("corrupted program");
X	return (0);
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    /* 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)
X    regexp         *prog;
X    char           *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)
X    char           *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 invocation of the same
X		     * parentheses already has. 
X		     */
X		    if (regstartp[no] == NULL)
X			regstartp[no] = save;
X		    return (1);
X		} else
X		    return (0);
X	    }
X	    /* break; Not Reached */
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 invocation of the same
X		     * parentheses already has. 
X		     */
X		    if (regendp[no] == NULL)
X			regendp[no] = save;
X		    return (1);
X		} else
X		    return (0);
X	    }
X	    /* break; Not Reached */
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 when we know
X		 * 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; Not Reached */
X	  case END:
X	    return (1);		/* Success! */
X	    /* break; Not Reached */
X	  default:
X	    regerror("memory corruption");
X	    return (0);
X	    /* break; Not Reached */
X	}
X
X	scan = next;
X    }
X
X    /*
X     * We get here only if there's trouble -- normally "case END" is the
X     * 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)
X    char           *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)
X    register char  *p;
X{
X    register int    offset;
X
X    if (p == &regdummy)
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)
X    regexp         *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)
X    char           *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)
X    char           *s1;
X    char           *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)
X    char           *s1, *s2;
X    int             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)
X    char           *s;
X    char            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}
SHAR_EOF
echo "End of archive 4 (of 6)"
# if you want to concatenate archives, remove anything after this line
exit