1*56c391f4Smartijn /* $OpenBSD: re.c,v 1.19 2018/06/19 12:36:18 martijn Exp $ */
2df930be7Sderaadt /* $NetBSD: re.c,v 1.14 1995/03/21 09:04:48 cgd Exp $ */
3df930be7Sderaadt
4df930be7Sderaadt /* re.c: This file contains the regular expression interface routines for
5df930be7Sderaadt the ed line editor. */
6df930be7Sderaadt /*-
7df930be7Sderaadt * Copyright (c) 1993 Andrew Moore, Talke Studio.
8df930be7Sderaadt * All rights reserved.
9df930be7Sderaadt *
10df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
11df930be7Sderaadt * modification, are permitted provided that the following conditions
12df930be7Sderaadt * are met:
13df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
14df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
15df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
16df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
17df930be7Sderaadt * documentation and/or other materials provided with the distribution.
18df930be7Sderaadt *
19df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df930be7Sderaadt * SUCH DAMAGE.
30df930be7Sderaadt */
31df930be7Sderaadt
32df7744bfSmmcc #include <regex.h>
33df7744bfSmmcc #include <signal.h>
34df7744bfSmmcc #include <stdio.h>
35df7744bfSmmcc #include <stdlib.h>
36df7744bfSmmcc #include <string.h>
37df7744bfSmmcc
38df930be7Sderaadt #include "ed.h"
39df930be7Sderaadt
407aba2227Stobias static char *extract_pattern(int);
417aba2227Stobias static char *parse_char_class(char *);
42df930be7Sderaadt
43df930be7Sderaadt extern int patlock;
44df930be7Sderaadt
45df930be7Sderaadt
46df930be7Sderaadt /* get_compiled_pattern: return pointer to compiled pattern from command
47df930be7Sderaadt buffer */
486cdce336Smillert regex_t *
get_compiled_pattern(void)4928416801Sderaadt get_compiled_pattern(void)
50df930be7Sderaadt {
516cdce336Smillert static regex_t *exp = NULL;
52d9ee4fa8Stobias char errbuf[128] = "";
53df930be7Sderaadt
54df930be7Sderaadt char *exps;
55df930be7Sderaadt char delimiter;
56df930be7Sderaadt int n;
57df930be7Sderaadt
58df930be7Sderaadt if ((delimiter = *ibufp) == ' ') {
5980acfbc0Sderaadt seterrmsg("invalid pattern delimiter");
60df930be7Sderaadt return NULL;
61df930be7Sderaadt } else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
6245c72c98Smillert if (!exp)
6380acfbc0Sderaadt seterrmsg("no previous pattern");
64df930be7Sderaadt return exp;
65df930be7Sderaadt } else if ((exps = extract_pattern(delimiter)) == NULL)
66df930be7Sderaadt return NULL;
67df930be7Sderaadt /* buffer alloc'd && not reserved */
68df930be7Sderaadt if (exp && !patlock)
69df930be7Sderaadt regfree(exp);
706cdce336Smillert else if ((exp = malloc(sizeof(regex_t))) == NULL) {
71d1e48962Smillert perror(NULL);
7280acfbc0Sderaadt seterrmsg("out of memory");
73df930be7Sderaadt return NULL;
74df930be7Sderaadt }
75df930be7Sderaadt patlock = 0;
7684e1e806Smillert if ((n = regcomp(exp, exps, 0)) != 0) {
77d9ee4fa8Stobias regerror(n, exp, errbuf, sizeof errbuf);
78d9ee4fa8Stobias seterrmsg(errbuf);
79df930be7Sderaadt free(exp);
80df930be7Sderaadt return exp = NULL;
81df930be7Sderaadt }
82df930be7Sderaadt return exp;
83df930be7Sderaadt }
84df930be7Sderaadt
85df930be7Sderaadt
86df930be7Sderaadt /* extract_pattern: copy a pattern string from the command buffer; return
87df930be7Sderaadt pointer to the copy */
887aba2227Stobias static char *
extract_pattern(int delimiter)8928416801Sderaadt extract_pattern(int delimiter)
90df930be7Sderaadt {
91df930be7Sderaadt static char *lhbuf = NULL; /* buffer */
92df930be7Sderaadt static int lhbufsz = 0; /* buffer size */
93df930be7Sderaadt
94df930be7Sderaadt char *nd;
95df930be7Sderaadt int len;
96df930be7Sderaadt
97*56c391f4Smartijn for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
98df930be7Sderaadt switch (*nd) {
99df930be7Sderaadt default:
100df930be7Sderaadt break;
101*56c391f4Smartijn case '[':
102*56c391f4Smartijn if ((nd = parse_char_class(++nd)) == NULL) {
103*56c391f4Smartijn seterrmsg("unbalanced brackets ([])");
104*56c391f4Smartijn return NULL;
105*56c391f4Smartijn }
106*56c391f4Smartijn break;
107df930be7Sderaadt case '\\':
108df930be7Sderaadt if (*++nd == '\n') {
10980acfbc0Sderaadt seterrmsg("trailing backslash (\\)");
110df930be7Sderaadt return NULL;
111df930be7Sderaadt }
112df930be7Sderaadt break;
113df930be7Sderaadt }
114df930be7Sderaadt len = nd - ibufp;
115df930be7Sderaadt REALLOC(lhbuf, lhbufsz, len + 1, NULL);
116df930be7Sderaadt memcpy(lhbuf, ibufp, len);
117df930be7Sderaadt lhbuf[len] = '\0';
118df930be7Sderaadt ibufp = nd;
119df930be7Sderaadt return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
120df930be7Sderaadt }
121*56c391f4Smartijn
122*56c391f4Smartijn
123*56c391f4Smartijn /* parse_char_class: expand a POSIX character class */
124*56c391f4Smartijn static char *
parse_char_class(char * s)125*56c391f4Smartijn parse_char_class(char *s)
126*56c391f4Smartijn {
127*56c391f4Smartijn int c, d;
128*56c391f4Smartijn
129*56c391f4Smartijn if (*s == '^')
130*56c391f4Smartijn s++;
131*56c391f4Smartijn if (*s == ']')
132*56c391f4Smartijn s++;
133*56c391f4Smartijn for (; *s != ']' && *s != '\n'; s++)
134*56c391f4Smartijn if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
135*56c391f4Smartijn for (s++, c = *++s; *s != ']' || c != d; s++)
136*56c391f4Smartijn if ((c = *s) == '\n')
137*56c391f4Smartijn return NULL;
138*56c391f4Smartijn return (*s == ']') ? s : NULL;
139*56c391f4Smartijn }
140