re.c revision 339
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) == ' ') { 68 sprintf(errmsg, "invalid pattern delimiter"); 69 return NULL; 70 } else if (delim == '\n' || *++ibufp == '\n' || *ibufp == delim) { 71 if (!exp) sprintf(errmsg, "no previous pattern"); 72 return exp; 73 } else if ((exps = getlhs(delim)) == NULL) 74 return NULL; 75 /* buffer alloc'd && not reserved */ 76 if (exp && !patlock) 77 regfree(exp); 78 else if ((exp = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) { 79 fprintf(stderr, "%s\n", strerror(errno)); 80 sprintf(errmsg, "out of memory"); 81 return NULL; 82 } 83 patlock = 0; 84 if (n = regcomp(exp, exps, 0)) { 85 regerror(n, exp, errmsg, sizeof errmsg); 86 free(exp); 87 return exp = NULL; 88 } 89 return exp; 90} 91 92 93extern int isbinary; 94 95/* getlhs: copy a pattern string from the command buffer; return pointer 96 to the copy */ 97char * 98getlhs(delim) 99 int delim; 100{ 101 char *nd; 102 int len; 103 104 for (nd = ibufp; *nd != delim && *nd != '\n'; nd++) 105 switch (*nd) { 106 default: 107 break; 108 case '[': 109 if ((nd = ccl(++nd)) == NULL) { 110 sprintf(errmsg, "unbalanced brackets ([])"); 111 return NULL; 112 } 113 break; 114 case '\\': 115 if (*++nd == '\n') { 116 sprintf(errmsg, "trailing backslash (\\)"); 117 return NULL; 118 } 119 break; 120 } 121 len = nd - ibufp; 122 CKBUF(lhbuf, lhbufsz, len + 1, NULL); 123 memcpy(lhbuf, ibufp, len); 124 lhbuf[len] = '\0'; 125 ibufp = nd; 126 return (isbinary) ? nultonl(lhbuf, len) : lhbuf; 127} 128 129 130/* ccl: expand a POSIX character class */ 131char * 132ccl(s) 133 char *s; 134{ 135 int c, d; 136 137 if (*s == '^') 138 s++; 139 if (*s == ']') 140 s++; 141 for (; *s != ']' && *s != '\n'; s++) 142 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) 143 for (s++, c = *++s; *s != ']' || c != d; s++) 144 if ((c = *s) == '\n') 145 return NULL; 146 return (*s == ']') ? s : NULL; 147} 148