re.c revision 1.12 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.
6 *
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.
15 *
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.
27 */
28
29 #ifndef lint
30 /* static char sccsid[] = "@(#)re.c 5.5 (Talke Studio) 3/28/93"; */
31 static char rcsid[] = "$Id: re.c,v 1.12 1993/11/23 04:41:55 alm Exp $";
32 #endif /* not lint */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "ed.h"
39
40 extern char *lhbuf;
41 extern int lhbufsz;
42 extern char *ibufp;
43 extern int ibufsz;
44 extern int patlock;
45
46 char errmsg[MAXFNAME + 40] = "";
47
48 /* get_compiled_pattern: return pointer to compiled pattern from command
49 buffer */
50 pattern_t *
51 get_compiled_pattern()
52 {
53 static pattern_t *exp = NULL;
54
55 char *exps;
56 char delimiter;
57 int n;
58
59 if ((delimiter = *ibufp) == ' ') {
60 sprintf(errmsg, "invalid pattern delimiter");
61 return NULL;
62 } else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
63 if (!exp) sprintf(errmsg, "no previous pattern");
64 return exp;
65 } else if ((exps = extract_pattern(delimiter)) == NULL)
66 return NULL;
67 /* buffer alloc'd && not reserved */
68 if (exp && !patlock)
69 regfree(exp);
70 else if ((exp = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
71 fprintf(stderr, "%s\n", strerror(errno));
72 sprintf(errmsg, "out of memory");
73 return NULL;
74 }
75 patlock = 0;
76 if (n = regcomp(exp, exps, 0)) {
77 regerror(n, exp, errmsg, sizeof errmsg);
78 free(exp);
79 return exp = NULL;
80 }
81 return exp;
82 }
83
84
85 extern int isbinary;
86
87 /* extract_pattern: copy a pattern string from the command buffer; return
88 pointer to the copy */
89 char *
90 extract_pattern(delimiter)
91 int delimiter;
92 {
93 char *nd;
94 int len;
95
96 for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
97 switch (*nd) {
98 default:
99 break;
100 case '[':
101 if ((nd = parse_char_class(++nd)) == NULL) {
102 sprintf(errmsg, "unbalanced brackets ([])");
103 return NULL;
104 }
105 break;
106 case '\\':
107 if (*++nd == '\n') {
108 sprintf(errmsg, "trailing backslash (\\)");
109 return NULL;
110 }
111 break;
112 }
113 len = nd - ibufp;
114 CKBUF(lhbuf, lhbufsz, len + 1, NULL);
115 memcpy(lhbuf, ibufp, len);
116 lhbuf[len] = '\0';
117 ibufp = nd;
118 return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
119 }
120
121
122 /* parse_char_class: expand a POSIX character class */
123 char *
124 parse_char_class(s)
125 char *s;
126 {
127 int c, d;
128
129 if (*s == '^')
130 s++;
131 if (*s == ']')
132 s++;
133 for (; *s != ']' && *s != '\n'; s++)
134 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
135 for (s++, c = *++s; *s != ']' || c != d; s++)
136 if ((c = *s) == '\n')
137 return NULL;
138 return (*s == ']') ? s : NULL;
139 }
140