Regular expressions: Diffs between Sys V and BSD

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Nov 9 16:29:24 AEST 1989


In article <1199 at radig.uucp> peter at radig.UUCP (Peter Radig) writes:
>How can I change the calls of `re_comp' and `re_exp' to the Sys V functions
>`regcmp' and `regex'.

Don't do that; use <regexp.h> instead.  Here's a BSD emulation for System V:


/*
	BSD-style regular expression interface for UNIX System V

	last edit:	04-Aug-1989	D A Gwyn
*/

static struct re_msg
	{
	int	number;
	char	*message;
	}	re_msgtab[] =
	{
	{	11,	"range endpoint too large"	},
	{	16,	"bad number"	},
	{	25,	"`\\digit' out of range"	},
	{	35,	"no match"	},
	{	36,	"illegal or missing delimiter"	},
	{	41,	"no remembered search string"	},
	{	42,	"'\\( \\)' imbalance"	},
	{	43,	"too many `\\(' s"	},
	{	44,	"more than 2 numbers given"	},
	{	45,	"'\\}' expected"	},
	{	46,	"first number exceeds second"	},
	{	49,	"'[ ]' imbalance"	},
	{	50,	"regular expression overflow"	},
	{	51,	"regular expression error"	},
	{	0,	"unknown r.e. error"	}
	};

static char	*re_err;		/* sneak path to error string */

/*	Handle error from <regexp.h>	*/

static void
re_error( n )
	register int		n;	/* error number */
	{
	register struct re_msg	*mp;

	for ( mp = re_msgtab; mp->number > 0; ++mp )
		if ( mp->number == n )
			break;

	re_err = mp->message;
	}

/* macros for <regexp.h> */
#define INIT		register char	*re_str = instring;
#define GETC()		(*re_str == '\0' ? '\0' : (int)*re_str++)
#define UNGETC( c )	--re_str
#define PEEKC()		((int)*re_str)
#define RETURN( c )	return (char *)0
/* compile() doesn't expect ERROR() to return; kludge supplied by Gary Moss: */
#define ERROR( n )	if ( (n) >= 0 ) { re_error( n ); return re_err; } else

/* following for safety's sake */
#define	sed	re_sed
#define	nbra	re_nbra
#define	loc1	re_loc1
#define	loc2	re_loc2
#define	locs	re_locs
#define	circf	re_circf
#define	compile	re_cmpl			/* avoid truncated name collision! */
#define	step	re_step
#define	advance	re_advance

#include	<regexp.h>

#define	ESIZE	512
static char	re_buf[ESIZE];		/* compiled r.e. */

/*	Compile regular expression	*/

char	*
re_comp( s )				/* returns 0 or ptr to error message */
	char	*s;
	{
	re_err = (char *)0;

	if ( s != (char *)0 && *s != '\0' )
		(void)compile( s, re_buf, &re_buf[ESIZE], '\0' );
	else if ( re_buf[0] == '\0' )
		ERROR( 41 );		/* no remembered search string */
	/* else use remembered search string from previous call */

	return re_err;
	}

/*	Test for match against compiled expression	*/

int
re_exec( s )				/* returns 1 if s matches, else 0 */
	char	*s;
	{
	locs = 0;			/* ??? */
	return step( s, re_buf );
	}



More information about the Comp.unix.wizards mailing list