re.c revision 1.2 1 /* re.c: This file contains the regular expression interface routines for
2 the ed line editor. */
3 /*-
4 * Copyright (c) 1993 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley
8 * by Andrew Moore, Talke Studio.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 static char sccsid[] = "@(#)re.c 5.5 (Berkeley) 3/28/93";
41 #endif /* not lint */
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "ed.h"
48
49 extern char *ibufp;
50 extern int patlock;
51
52 char errmsg[MAXFNAME + 40] = "";
53
54 /* optpat: return pointer to compiled pattern from command buffer */
55 pattern_t *
56 optpat()
57 {
58 static pattern_t *exp = NULL;
59
60 char *exps;
61 char delim;
62 int n;
63
64 if ((delim = *ibufp) == '\n') {
65 sprintf(errmsg, "no previous pattern");
66 return exp;
67 } else if (delim == ' ' || *++ibufp == '\n') {
68 sprintf(errmsg, "invalid pattern delimiter");
69 return NULL;
70 } else if (*ibufp == delim) {
71 sprintf(errmsg, "no previous pattern");
72 return exp;
73 } else if ((exps = getlhs(delim)) == NULL)
74 return NULL;
75 /* buffer alloc'd && not reserved */
76 if (exp && !patlock)
77 regfree(exp);
78 else if ((exp = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
79 sprintf(errmsg, "out of memory");
80 return NULL;
81 }
82 patlock = 0;
83 #ifdef GNU_REGEX
84 /* initialize pattern buffer */
85 exp->buffer = NULL;
86 exp->allocated = 0L;
87 exp->fastmap = (char *) malloc(FASTMAP_SIZE);
88 exp->translate = 0;
89 #endif
90 if (n = regcomp(exp, exps, 0)) {
91 regerror(n, exp, errmsg, sizeof errmsg);
92 return NULL;
93 }
94 return exp;
95 }
96
97
98 /* getlhs: copy a pattern string from the command buffer; return pointer
99 to the copy */
100 char *
101 getlhs(delim)
102 int delim;
103 {
104 static char buf[MAXLINE];
105 char *nd;
106
107 for (nd = ibufp; *nd != delim && *nd != '\n'; nd++)
108 switch (*nd) {
109 default:
110 break;
111 case CCL:
112 if ((nd = ccl(CCLEND, ++nd)) == NULL) {
113 sprintf(errmsg, "unbalanced brackets ([])");
114 return NULL;
115 }
116 break;
117 case ESCHAR:
118 if (*++nd == '\n') {
119 sprintf(errmsg, "trailing backslash (\\)");
120 return NULL;
121 }
122 break;
123 }
124 strncpy(buf, ibufp, nd - ibufp);
125 buf[nd - ibufp] = '\0';
126 ibufp = nd;
127 return buf;
128 }
129
130
131 /* ccl: expand a character class */
132 char *
133 ccl(delim, src)
134 int delim;
135 char *src;
136 {
137 if (*src == '^')
138 src++;
139 if (*src == delim)
140 src++;
141 while (*src != delim && *src != '\n')
142 src++;
143 return (*src == delim) ? src : NULL;
144 }
145