re.c revision 268
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 if (n = regcomp(exp, exps, 0)) { 88 regerror(n, exp, errmsg, sizeof errmsg); 89 return NULL; 90 } 91 return exp; 92} 93 94 95extern int isbinary; 96 97/* getlhs: copy a pattern string from the command buffer; return pointer 98 to the copy */ 99char * 100getlhs(delim) 101 int delim; 102{ 103 char *nd; 104 int len; 105 106 for (nd = ibufp; *nd != delim && *nd != '\n'; nd++) 107 switch (*nd) { 108 default: 109 break; 110 case '[': 111 if ((nd = ccl(++nd)) == NULL) { 112 sprintf(errmsg, "unbalanced brackets ([])"); 113 return NULL; 114 } 115 break; 116 case '\\': 117 if (*++nd == '\n') { 118 sprintf(errmsg, "trailing backslash (\\)"); 119 return NULL; 120 } 121 break; 122 } 123 len = nd - ibufp; 124 CKBUF(lhbuf, lhbufsz, len + 1, NULL); 125 memcpy(lhbuf, ibufp, len); 126 lhbuf[len] = '\0'; 127 ibufp = nd; 128 return (isbinary) ? nultonl(lhbuf, len) : lhbuf; 129} 130 131 132/* ccl: expand a POSIX character class */ 133char * 134ccl(s) 135 char *s; 136{ 137 int c, d; 138 139 if (*s == '^') 140 s++; 141 if (*s == ']') 142 s++; 143 for (; *s != ']' && *s != '\n'; s++) 144 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) 145 for (s++, c = *++s; *s != ']' || c != d; s++) 146 if ((c = *s) == '\n') 147 return NULL; 148 return (*s == ']') ? s : NULL; 149} 150