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$");
3116Salm
3216Salm#include "ed.h"
3316Salm
3481220Smikeconst char *errmsg = "";
3516Salm
368855Srgrimes/* get_compiled_pattern: return pointer to compiled pattern from command
371057Salm   buffer */
3816Salmpattern_t *
3990109Simpget_compiled_pattern(void)
4016Salm{
41117803Sru	static pattern_t *expr = NULL;
4281220Smike	static char error[1024];
4316Salm
44117803Sru	char *exprs;
451057Salm	char delimiter;
4616Salm	int n;
4716Salm
481057Salm	if ((delimiter = *ibufp) == ' ') {
4981220Smike		errmsg = "invalid pattern delimiter";
5016Salm		return NULL;
511057Salm	} else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
52117803Sru		if (!expr)
5381220Smike			errmsg = "no previous pattern";
54117803Sru		return expr;
55117803Sru	} else if ((exprs = extract_pattern(delimiter)) == NULL)
5616Salm		return NULL;
5716Salm	/* buffer alloc'd && not reserved */
58117803Sru	if (expr && !patlock)
59117803Sru		regfree(expr);
60117803Sru	else if ((expr = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
6116Salm		fprintf(stderr, "%s\n", strerror(errno));
6281220Smike		errmsg = "out of memory";
6316Salm		return NULL;
6416Salm	}
6516Salm	patlock = 0;
66117803Sru	if ((n = regcomp(expr, exprs, 0))) {
67117803Sru		regerror(n, expr, error, sizeof error);
6881220Smike		errmsg = error;
69117803Sru		free(expr);
70117803Sru		return expr = NULL;
7116Salm	}
72117803Sru	return expr;
7316Salm}
7416Salm
7516Salm
761057Salm/* extract_pattern: copy a pattern string from the command buffer; return
771057Salm   pointer to the copy */
7816Salmchar *
7990109Simpextract_pattern(int delimiter)
8016Salm{
811057Salm	static char *lhbuf = NULL;	/* buffer */
821057Salm	static int lhbufsz = 0;		/* buffer size */
831057Salm
8416Salm	char *nd;
8516Salm	int len;
8616Salm
871057Salm	for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
8816Salm		switch (*nd) {
8916Salm		default:
9016Salm			break;
9116Salm		case '[':
92252374Skientzle			if ((nd = parse_char_class(nd + 1)) == NULL) {
9381220Smike				errmsg = "unbalanced brackets ([])";
9416Salm				return NULL;
9516Salm			}
9616Salm			break;
9716Salm		case '\\':
9816Salm			if (*++nd == '\n') {
9981220Smike				errmsg = "trailing backslash (\\)";
10016Salm				return NULL;
10116Salm			}
10216Salm			break;
10316Salm		}
10416Salm	len = nd - ibufp;
1051057Salm	REALLOC(lhbuf, lhbufsz, len + 1, NULL);
10616Salm	memcpy(lhbuf, ibufp, len);
10716Salm	lhbuf[len] = '\0';
10816Salm	ibufp = nd;
1091057Salm	return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
11016Salm}
11116Salm
11216Salm
1131057Salm/* parse_char_class: expand a POSIX character class */
11416Salmchar *
11590109Simpparse_char_class(char *s)
11616Salm{
11716Salm	int c, d;
11816Salm
11916Salm	if (*s == '^')
12016Salm		s++;
12116Salm	if (*s == ']')
12216Salm		s++;
12316Salm	for (; *s != ']' && *s != '\n'; s++)
12416Salm		if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
12516Salm			for (s++, c = *++s; *s != ']' || c != d; s++)
12616Salm				if ((c = *s) == '\n')
12716Salm					return NULL;
12816Salm	return  (*s == ']') ? s : NULL;
12916Salm}
130