re.c revision 117803
116Salm/* re.c: This file contains the regular expression interface routines for
216Salm   the ed line editor. */
316Salm/*-
41057Salm * Copyright (c) 1993 Andrew Moore, Talke Studio.
516Salm * All rights reserved.
616Salm *
716Salm * Redistribution and use in source and binary forms, with or without
816Salm * modification, are permitted provided that the following conditions
916Salm * are met:
1016Salm * 1. Redistributions of source code must retain the above copyright
1116Salm *    notice, this list of conditions and the following disclaimer.
1216Salm * 2. Redistributions in binary form must reproduce the above copyright
1316Salm *    notice, this list of conditions and the following disclaimer in the
1416Salm *    documentation and/or other materials provided with the distribution.
1516Salm *
161057Salm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1716Salm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1816Salm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191057Salm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2016Salm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2116Salm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2216Salm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2316Salm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2416Salm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2516Salm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2616Salm * SUCH DAMAGE.
2716Salm */
2816Salm
2999109Sobrien#include <sys/cdefs.h>
3099109Sobrien__FBSDID("$FreeBSD: head/bin/ed/re.c 117803 2003-07-20 10:24:09Z ru $");
3116Salm
3216Salm#include "ed.h"
3316Salm
341057Salm
3516Salmextern int patlock;
3616Salm
3781220Smikeconst char *errmsg = "";
3816Salm
398855Srgrimes/* get_compiled_pattern: return pointer to compiled pattern from command
401057Salm   buffer */
4116Salmpattern_t *
4290109Simpget_compiled_pattern(void)
4316Salm{
44117803Sru	static pattern_t *expr = NULL;
4581220Smike	static char error[1024];
4616Salm
47117803Sru	char *exprs;
481057Salm	char delimiter;
4916Salm	int n;
5016Salm
511057Salm	if ((delimiter = *ibufp) == ' ') {
5281220Smike		errmsg = "invalid pattern delimiter";
5316Salm		return NULL;
541057Salm	} else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
55117803Sru		if (!expr)
5681220Smike			errmsg = "no previous pattern";
57117803Sru		return expr;
58117803Sru	} else if ((exprs = extract_pattern(delimiter)) == NULL)
5916Salm		return NULL;
6016Salm	/* buffer alloc'd && not reserved */
61117803Sru	if (expr && !patlock)
62117803Sru		regfree(expr);
63117803Sru	else if ((expr = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
6416Salm		fprintf(stderr, "%s\n", strerror(errno));
6581220Smike		errmsg = "out of memory";
6616Salm		return NULL;
6716Salm	}
6816Salm	patlock = 0;
69117803Sru	if ((n = regcomp(expr, exprs, 0))) {
70117803Sru		regerror(n, expr, error, sizeof error);
7181220Smike		errmsg = error;
72117803Sru		free(expr);
73117803Sru		return expr = NULL;
7416Salm	}
75117803Sru	return expr;
7616Salm}
7716Salm
7816Salm
791057Salm/* extract_pattern: copy a pattern string from the command buffer; return
801057Salm   pointer to the copy */
8116Salmchar *
8290109Simpextract_pattern(int delimiter)
8316Salm{
841057Salm	static char *lhbuf = NULL;	/* buffer */
851057Salm	static int lhbufsz = 0;		/* buffer size */
861057Salm
8716Salm	char *nd;
8816Salm	int len;
8916Salm
901057Salm	for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
9116Salm		switch (*nd) {
9216Salm		default:
9316Salm			break;
9416Salm		case '[':
951057Salm			if ((nd = parse_char_class(++nd)) == NULL) {
9681220Smike				errmsg = "unbalanced brackets ([])";
9716Salm				return NULL;
9816Salm			}
9916Salm			break;
10016Salm		case '\\':
10116Salm			if (*++nd == '\n') {
10281220Smike				errmsg = "trailing backslash (\\)";
10316Salm				return NULL;
10416Salm			}
10516Salm			break;
10616Salm		}
10716Salm	len = nd - ibufp;
1081057Salm	REALLOC(lhbuf, lhbufsz, len + 1, NULL);
10916Salm	memcpy(lhbuf, ibufp, len);
11016Salm	lhbuf[len] = '\0';
11116Salm	ibufp = nd;
1121057Salm	return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
11316Salm}
11416Salm
11516Salm
1161057Salm/* parse_char_class: expand a POSIX character class */
11716Salmchar *
11890109Simpparse_char_class(char *s)
11916Salm{
12016Salm	int c, d;
12116Salm
12216Salm	if (*s == '^')
12316Salm		s++;
12416Salm	if (*s == ']')
12516Salm		s++;
12616Salm	for (; *s != ']' && *s != '\n'; s++)
12716Salm		if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
12816Salm			for (s++, c = *++s; *s != ']' || c != d; s++)
12916Salm				if ((c = *s) == '\n')
13016Salm					return NULL;
13116Salm	return  (*s == ']') ? s : NULL;
13216Salm}
133