1 /* $OpenBSD: re.c,v 1.16 2015/10/09 21:24:05 tobias Exp $ */ 2 /* $NetBSD: re.c,v 1.14 1995/03/21 09:04:48 cgd Exp $ */ 3 4 /* re.c: This file contains the regular expression interface routines for 5 the ed line editor. */ 6 /*- 7 * Copyright (c) 1993 Andrew Moore, Talke Studio. 8 * All rights reserved. 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include "ed.h" 33 34 static char *extract_pattern(int); 35 static char *parse_char_class(char *); 36 37 extern int patlock; 38 39 40 /* get_compiled_pattern: return pointer to compiled pattern from command 41 buffer */ 42 regex_t * 43 get_compiled_pattern(void) 44 { 45 static regex_t *exp = NULL; 46 char errbuf[128] = ""; 47 48 char *exps; 49 char delimiter; 50 int n; 51 52 if ((delimiter = *ibufp) == ' ') { 53 seterrmsg("invalid pattern delimiter"); 54 return NULL; 55 } else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) { 56 if (!exp) 57 seterrmsg("no previous pattern"); 58 return exp; 59 } else if ((exps = extract_pattern(delimiter)) == NULL) 60 return NULL; 61 /* buffer alloc'd && not reserved */ 62 if (exp && !patlock) 63 regfree(exp); 64 else if ((exp = malloc(sizeof(regex_t))) == NULL) { 65 perror(NULL); 66 seterrmsg("out of memory"); 67 return NULL; 68 } 69 patlock = 0; 70 if ((n = regcomp(exp, exps, 0)) != 0) { 71 regerror(n, exp, errbuf, sizeof errbuf); 72 seterrmsg(errbuf); 73 free(exp); 74 return exp = NULL; 75 } 76 return exp; 77 } 78 79 80 /* extract_pattern: copy a pattern string from the command buffer; return 81 pointer to the copy */ 82 static char * 83 extract_pattern(int delimiter) 84 { 85 static char *lhbuf = NULL; /* buffer */ 86 static int lhbufsz = 0; /* buffer size */ 87 88 char *nd; 89 int len; 90 91 for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++) 92 switch (*nd) { 93 default: 94 break; 95 case '[': 96 if ((nd = parse_char_class(++nd)) == NULL) { 97 seterrmsg("unbalanced brackets ([])"); 98 return NULL; 99 } 100 break; 101 case '\\': 102 if (*++nd == '\n') { 103 seterrmsg("trailing backslash (\\)"); 104 return NULL; 105 } 106 break; 107 } 108 len = nd - ibufp; 109 REALLOC(lhbuf, lhbufsz, len + 1, NULL); 110 memcpy(lhbuf, ibufp, len); 111 lhbuf[len] = '\0'; 112 ibufp = nd; 113 return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf; 114 } 115 116 117 /* parse_char_class: expand a POSIX character class */ 118 static char * 119 parse_char_class(char *s) 120 { 121 int c, d; 122 123 if (*s == '^') 124 s++; 125 if (*s == ']') 126 s++; 127 for (; *s != ']' && *s != '\n'; s++) 128 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) 129 for (s++, c = *++s; *s != ']' || c != d; s++) 130 if ((c = *s) == '\n') 131 return NULL; 132 return (*s == ']') ? s : NULL; 133 } 134