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