re.c revision 49
1/* re.c: This file contains the regular expression interface routines for 2 the ed line editor. */ 3/*- 4 * Copyright (c) 1993 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley 8 * by Andrew Moore, Talke Studio. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39#ifndef lint 40static char sccsid[] = "@(#)re.c 5.5 (Berkeley) 3/28/93"; 41#endif /* not lint */ 42 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46 47#include "ed.h" 48 49extern char *lhbuf; 50extern int lhbufsz; 51extern char *ibufp; 52extern int ibufsz; 53extern int patlock; 54 55char errmsg[MAXFNAME + 40] = ""; 56 57/* optpat: return pointer to compiled pattern from command buffer */ 58pattern_t * 59optpat() 60{ 61 static pattern_t *exp = NULL; 62 63 char *exps; 64 char delim; 65 int n; 66 67 if ((delim = *ibufp) == '\n') { 68 if (!exp) sprintf(errmsg, "no previous pattern"); 69 return exp; 70 } else if (delim == ' ' || *++ibufp == '\n') { 71 sprintf(errmsg, "invalid pattern delimiter"); 72 return NULL; 73 } else if (*ibufp == delim) { 74 if (!exp) sprintf(errmsg, "no previous pattern"); 75 return exp; 76 } else if ((exps = getlhs(delim)) == NULL) 77 return NULL; 78 /* buffer alloc'd && not reserved */ 79 if (exp && !patlock) 80 regfree(exp); 81 else if ((exp = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) { 82 fprintf(stderr, "%s\n", strerror(errno)); 83 sprintf(errmsg, "out of memory"); 84 return NULL; 85 } 86 patlock = 0; 87#ifdef GNU_REGEX 88 /* initialize pattern buffer */ 89 exp->buffer = NULL; 90 exp->allocated = 0L; 91 exp->fastmap = 0; /* not used by GNU regex after 0.12 */ 92 exp->translate = 0; 93#endif 94 if (n = regcomp(exp, exps, 0)) { 95 regerror(n, exp, errmsg, sizeof errmsg); 96 return NULL; 97 } 98 return exp; 99} 100 101 102extern int isbinary; 103 104/* getlhs: copy a pattern string from the command buffer; return pointer 105 to the copy */ 106char * 107getlhs(delim) 108 int delim; 109{ 110 char *nd; 111 int len; 112 113 for (nd = ibufp; *nd != delim && *nd != '\n'; nd++) 114 switch (*nd) { 115 default: 116 break; 117 case '[': 118 if ((nd = ccl(++nd)) == NULL) { 119 sprintf(errmsg, "unbalanced brackets ([])"); 120 return NULL; 121 } 122 break; 123 case '\\': 124 if (*++nd == '\n') { 125 sprintf(errmsg, "trailing backslash (\\)"); 126 return NULL; 127 } 128 break; 129 } 130 len = nd - ibufp; 131 CKBUF(lhbuf, lhbufsz, len + 1, NULL); 132 memcpy(lhbuf, ibufp, len); 133 lhbuf[len] = '\0'; 134 ibufp = nd; 135 return (isbinary) ? nultonl(lhbuf, len) : lhbuf; 136} 137 138 139/* ccl: expand a POSIX character class */ 140char * 141ccl(s) 142 char *s; 143{ 144 int c, d; 145 146 if (*s == '^') 147 s++; 148 if (*s == ']') 149 s++; 150 for (; *s != ']' && *s != '\n'; s++) 151 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) 152 for (s++, c = *++s; *s != ']' || c != d; s++) 153 if ((c = *s) == '\n') 154 return NULL; 155 return (*s == ']') ? s : NULL; 156} 157