pg: Add missing dummy stack frames for mcount for x86_64.
[dragonfly.git] / bin / ed / re.c
blobf80943bce3c5a4e8bac291c49763b4bc740fc8d9
1 /* re.c: This file contains the regular expression interface routines for
2 the ed line editor. */
3 /*-
4 * Copyright (c) 1993 Andrew Moore, Talke Studio.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
28 * @(#)re.c,v 1.6 1994/02/01 00:34:43 alm Exp
29 * $FreeBSD: head/bin/ed/re.c 252374 2013-06-29 15:49:26Z kientzle $
32 #include "ed.h"
34 const char *errmsg = "";
36 /* get_compiled_pattern: return pointer to compiled pattern from command
37 buffer */
38 pattern_t *
39 get_compiled_pattern(void)
41 static pattern_t *expr = NULL;
42 static char error[1024];
44 char *exprs;
45 char delimiter;
46 int n;
48 if ((delimiter = *ibufp) == ' ') {
49 errmsg = "invalid pattern delimiter";
50 return NULL;
51 } else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
52 if (!expr)
53 errmsg = "no previous pattern";
54 return expr;
55 } else if ((exprs = extract_pattern(delimiter)) == NULL)
56 return NULL;
57 /* buffer alloc'd && not reserved */
58 if (expr && !patlock)
59 regfree(expr);
60 else if ((expr = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
61 fprintf(stderr, "%s\n", strerror(errno));
62 errmsg = "out of memory";
63 return NULL;
65 patlock = 0;
66 if ((n = regcomp(expr, exprs, 0))) {
67 regerror(n, expr, error, sizeof error);
68 errmsg = error;
69 free(expr);
70 return expr = NULL;
72 return expr;
76 /* extract_pattern: copy a pattern string from the command buffer; return
77 pointer to the copy */
78 char *
79 extract_pattern(int delimiter)
81 static char *lhbuf = NULL; /* buffer */
82 static int lhbufsz = 0; /* buffer size */
84 char *nd;
85 int len;
87 for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
88 switch (*nd) {
89 default:
90 break;
91 case '[':
92 if ((nd = parse_char_class(nd + 1)) == NULL) {
93 errmsg = "unbalanced brackets ([])";
94 return NULL;
96 break;
97 case '\\':
98 if (*++nd == '\n') {
99 errmsg = "trailing backslash (\\)";
100 return NULL;
102 break;
104 len = nd - ibufp;
105 REALLOC(lhbuf, lhbufsz, len + 1, NULL);
106 memcpy(lhbuf, ibufp, len);
107 lhbuf[len] = '\0';
108 ibufp = nd;
109 return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
113 /* parse_char_class: expand a POSIX character class */
114 char *
115 parse_char_class(char *s)
117 int c, d;
119 if (*s == '^')
120 s++;
121 if (*s == ']')
122 s++;
123 for (; *s != ']' && *s != '\n'; s++)
124 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
125 for (s++, c = *++s; *s != ']' || c != d; s++)
126 if ((c = *s) == '\n')
127 return NULL;
128 return (*s == ']') ? s : NULL;