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 * $Id$ 29 */ 30 31 #ifndef lint 32 static char * const rcsid = "@(#)re.c,v 1.6 1994/02/01 00:34:43 alm Exp"; 33 #endif /* not lint */ 34 35 #include "ed.h" 36 37 38 extern int patlock; 39 40 char errmsg[MAXPATHLEN + 40] = ""; 41 42 /* get_compiled_pattern: return pointer to compiled pattern from command 43 buffer */ 44 pattern_t * 45 get_compiled_pattern() 46 { 47 static pattern_t *exp = NULL; 48 49 char *exps; 50 char delimiter; 51 int n; 52 53 if ((delimiter = *ibufp) == ' ') { 54 sprintf(errmsg, "invalid pattern delimiter"); 55 return NULL; 56 } else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) { 57 if (!exp) sprintf(errmsg, "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 = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) { 65 fprintf(stderr, "%s\n", strerror(errno)); 66 sprintf(errmsg, "out of memory"); 67 return NULL; 68 } 69 patlock = 0; 70 if ((n = regcomp(exp, exps, 0))) { 71 regerror(n, exp, errmsg, sizeof errmsg); 72 free(exp); 73 return exp = NULL; 74 } 75 return exp; 76 } 77 78 79 /* extract_pattern: copy a pattern string from the command buffer; return 80 pointer to the copy */ 81 char * 82 extract_pattern(delimiter) 83 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 sprintf(errmsg, "unbalanced brackets ([])"); 98 return NULL; 99 } 100 break; 101 case '\\': 102 if (*++nd == '\n') { 103 sprintf(errmsg, "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 char * 119 parse_char_class(s) 120 char *s; 121 { 122 int c, d; 123 124 if (*s == '^') 125 s++; 126 if (*s == ']') 127 s++; 128 for (; *s != ']' && *s != '\n'; s++) 129 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) 130 for (s++, c = *++s; *s != ']' || c != d; s++) 131 if ((c = *s) == '\n') 132 return NULL; 133 return (*s == ']') ? s : NULL; 134 } 135