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