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