re.c revision 90109
1104964Sjeff/* re.c: This file contains the regular expression interface routines for 2330897Seadler the ed line editor. */ 3330897Seadler/*- 4104964Sjeff * Copyright (c) 1993 Andrew Moore, Talke Studio. 5104964Sjeff * All rights reserved. 6104964Sjeff * 7104964Sjeff * Redistribution and use in source and binary forms, with or without 8104964Sjeff * modification, are permitted provided that the following conditions 9104964Sjeff * are met: 10104964Sjeff * 1. Redistributions of source code must retain the above copyright 11104964Sjeff * notice, this list of conditions and the following disclaimer. 12104964Sjeff * 2. Redistributions in binary form must reproduce the above copyright 13104964Sjeff * notice, this list of conditions and the following disclaimer in the 14104964Sjeff * documentation and/or other materials provided with the distribution. 15104964Sjeff * 16104964Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17104964Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18104964Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19104964Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20104964Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21104964Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22104964Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23104964Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24104964Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25104964Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26104964Sjeff * SUCH DAMAGE. 27104964Sjeff */ 28104964Sjeff 29104964Sjeff#ifndef lint 30104964Sjeffstatic const char rcsid[] = 31104964Sjeff "$FreeBSD: head/bin/ed/re.c 90109 2002-02-02 06:36:49Z imp $"; 32104964Sjeff#endif /* not lint */ 33104964Sjeff 34104964Sjeff#include "ed.h" 35104964Sjeff 36104964Sjeff 37116182Sobrienextern int patlock; 38116182Sobrien 39116182Sobrienconst char *errmsg = ""; 40147565Speter 41177418Sjeff/* get_compiled_pattern: return pointer to compiled pattern from command 42147565Speter buffer */ 43104964Sjeffpattern_t * 44104964Sjeffget_compiled_pattern(void) 45176750Smarcel{ 46104964Sjeff static pattern_t *exp = NULL; 47104964Sjeff static char error[1024]; 48104964Sjeff 49123871Sjhb char *exps; 50104964Sjeff char delimiter; 51104964Sjeff int n; 52104964Sjeff 53104964Sjeff if ((delimiter = *ibufp) == ' ') { 54235459Srstone errmsg = "invalid pattern delimiter"; 55104964Sjeff return NULL; 56104964Sjeff } else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) { 57104964Sjeff if (!exp) 58139453Sjhb errmsg = "no previous pattern"; 59161599Sdavidxu return exp; 60160039Sobrien } else if ((exps = extract_pattern(delimiter)) == NULL) 61134689Sjulian return NULL; 62104964Sjeff /* buffer alloc'd && not reserved */ 63145256Sjkoshy if (exp && !patlock) 64145256Sjkoshy regfree(exp); 65145256Sjkoshy else if ((exp = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) { 66145256Sjkoshy fprintf(stderr, "%s\n", strerror(errno)); 67179297Sjb errmsg = "out of memory"; 68179297Sjb return NULL; 69179297Sjb } 70179297Sjb patlock = 0; 71179297Sjb if ((n = regcomp(exp, exps, 0))) { 72179297Sjb regerror(n, exp, error, sizeof error); 73107135Sjeff errmsg = error; 74107135Sjeff free(exp); 75107135Sjeff return exp = NULL; 76107135Sjeff } 77107135Sjeff return exp; 78107135Sjeff} 79107135Sjeff 80122355Sbde 81122355Sbde/* extract_pattern: copy a pattern string from the command buffer; return 82122355Sbde pointer to the copy */ 83107135Sjeffchar * 84122355Sbdeextract_pattern(int delimiter) 85107135Sjeff{ 86107135Sjeff static char *lhbuf = NULL; /* buffer */ 87187679Sjeff static int lhbufsz = 0; /* buffer size */ 88187357Sjeff 89134791Sjulian char *nd; 90163709Sjb int len; 91164936Sjulian 92298145Skib for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++) 93298145Skib switch (*nd) { 94163709Sjb default: 95164936Sjulian break; 96298145Skib case '[': 97298145Skib if ((nd = parse_char_class(++nd)) == NULL) { 98298145Skib errmsg = "unbalanced brackets ([])"; 99298145Skib return NULL; 100239153Smav } 101180923Sjhb break; 102164936Sjulian case '\\': 103187357Sjeff if (*++nd == '\n') { 104187357Sjeff errmsg = "trailing backslash (\\)"; 105187357Sjeff return NULL; 106109145Sjeff } 107109145Sjeff break; 108134791Sjulian } 109164936Sjulian len = nd - ibufp; 110177435Sjeff REALLOC(lhbuf, lhbufsz, len + 1, NULL); 111239157Smav memcpy(lhbuf, ibufp, len); 112134791Sjulian lhbuf[len] = '\0'; 113180923Sjhb ibufp = nd; 114180923Sjhb return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf; 115180923Sjhb} 116164936Sjulian 117164936Sjulian 118124955Sjeff/* parse_char_class: expand a POSIX character class */ 119180923Sjhbchar * 120180923Sjhbparse_char_class(char *s) 121180923Sjhb{ 122301456Skib int c, d; 123301456Skib 124301456Skib if (*s == '^') 125301456Skib s++; 126265108Smarius if (*s == ']') 127134791Sjulian s++; 128239185Smav for (; *s != ']' && *s != '\n'; s++) 129125288Sjeff if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) 130239185Smav for (s++, c = *++s; *s != ']' || c != d; s++) 131104964Sjeff if ((c = *s) == '\n') 132124955Sjeff return NULL; 133123871Sjhb return (*s == ']') ? s : NULL; 134124955Sjeff} 135139453Sjhb