1 1.22 rillig /* $NetBSD: re.c,v 1.22 2025/09/17 20:35:11 rillig Exp $ */ 2 1.14 cgd 3 1.1 cgd /* re.c: This file contains the regular expression interface routines for 4 1.1 cgd the ed line editor. */ 5 1.1 cgd /*- 6 1.11 alm * Copyright (c) 1993 Andrew Moore, Talke Studio. 7 1.1 cgd * All rights reserved. 8 1.1 cgd * 9 1.1 cgd * Redistribution and use in source and binary forms, with or without 10 1.1 cgd * modification, are permitted provided that the following conditions 11 1.1 cgd * are met: 12 1.1 cgd * 1. Redistributions of source code must retain the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer. 14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 cgd * notice, this list of conditions and the following disclaimer in the 16 1.1 cgd * documentation and/or other materials provided with the distribution. 17 1.1 cgd * 18 1.11 alm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 1.11 alm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 1.1 cgd * SUCH DAMAGE. 29 1.1 cgd */ 30 1.1 cgd 31 1.15 thorpej #include <sys/cdefs.h> 32 1.1 cgd #ifndef lint 33 1.14 cgd #if 0 34 1.13 alm static char *rcsid = "@(#)re.c,v 1.6 1994/02/01 00:34:43 alm Exp"; 35 1.14 cgd #else 36 1.22 rillig __RCSID("$NetBSD: re.c,v 1.22 2025/09/17 20:35:11 rillig Exp $"); 37 1.14 cgd #endif 38 1.1 cgd #endif /* not lint */ 39 1.1 cgd 40 1.21 dholland #include <stdarg.h> 41 1.13 alm #include "ed.h" 42 1.1 cgd 43 1.1 cgd 44 1.13 alm char errmsg[MAXPATHLEN + 40] = ""; 45 1.1 cgd 46 1.21 dholland void 47 1.21 dholland seterrmsg(const char *fmt, ...) 48 1.21 dholland { 49 1.21 dholland va_list ap; 50 1.21 dholland 51 1.21 dholland va_start(ap, fmt); 52 1.21 dholland vsnprintf(errmsg, sizeof(errmsg), fmt, ap); 53 1.21 dholland va_end(ap); 54 1.21 dholland } 55 1.21 dholland 56 1.22 rillig /* get_compiled_pattern: return pointer to compiled pattern from command 57 1.12 alm buffer */ 58 1.1 cgd pattern_t * 59 1.19 xtraeme get_compiled_pattern(void) 60 1.1 cgd { 61 1.18 thorpej static pattern_t *expr = NULL; 62 1.1 cgd 63 1.1 cgd char *exps; 64 1.12 alm char delimiter; 65 1.1 cgd int n; 66 1.1 cgd 67 1.12 alm if ((delimiter = *ibufp) == ' ') { 68 1.21 dholland seterrmsg("invalid pattern delimiter"); 69 1.1 cgd return NULL; 70 1.12 alm } else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) { 71 1.21 dholland if (!expr) seterrmsg("no previous pattern"); 72 1.18 thorpej return expr; 73 1.12 alm } else if ((exps = extract_pattern(delimiter)) == NULL) 74 1.1 cgd return NULL; 75 1.1 cgd /* buffer alloc'd && not reserved */ 76 1.18 thorpej if (expr && !patlock) 77 1.18 thorpej regfree(expr); 78 1.18 thorpej else if ((expr = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) { 79 1.3 alm fprintf(stderr, "%s\n", strerror(errno)); 80 1.21 dholland seterrmsg("out of memory"); 81 1.1 cgd return NULL; 82 1.1 cgd } 83 1.1 cgd patlock = 0; 84 1.18 thorpej if ((n = regcomp(expr, exps, ere)) != 0) { 85 1.18 thorpej regerror(n, expr, errmsg, sizeof errmsg); 86 1.18 thorpej free(expr); 87 1.18 thorpej return expr = NULL; 88 1.1 cgd } 89 1.18 thorpej return expr; 90 1.1 cgd } 91 1.1 cgd 92 1.1 cgd 93 1.12 alm /* extract_pattern: copy a pattern string from the command buffer; return 94 1.12 alm pointer to the copy */ 95 1.1 cgd char * 96 1.19 xtraeme extract_pattern(int delimiter) 97 1.1 cgd { 98 1.13 alm static char *lhbuf = NULL; /* buffer */ 99 1.13 alm static int lhbufsz = 0; /* buffer size */ 100 1.13 alm 101 1.1 cgd char *nd; 102 1.5 alm int len; 103 1.1 cgd 104 1.12 alm for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++) 105 1.1 cgd switch (*nd) { 106 1.1 cgd default: 107 1.1 cgd break; 108 1.5 alm case '[': 109 1.20 joerg if ((nd = parse_char_class(nd + 1)) == NULL) { 110 1.21 dholland seterrmsg("unbalanced brackets ([])"); 111 1.1 cgd return NULL; 112 1.1 cgd } 113 1.1 cgd break; 114 1.5 alm case '\\': 115 1.1 cgd if (*++nd == '\n') { 116 1.21 dholland seterrmsg("trailing backslash (\\)"); 117 1.1 cgd return NULL; 118 1.1 cgd } 119 1.1 cgd break; 120 1.1 cgd } 121 1.5 alm len = nd - ibufp; 122 1.13 alm REALLOC(lhbuf, lhbufsz, len + 1, NULL); 123 1.5 alm memcpy(lhbuf, ibufp, len); 124 1.5 alm lhbuf[len] = '\0'; 125 1.1 cgd ibufp = nd; 126 1.12 alm return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf; 127 1.1 cgd } 128 1.1 cgd 129 1.1 cgd 130 1.12 alm /* parse_char_class: expand a POSIX character class */ 131 1.1 cgd char * 132 1.19 xtraeme parse_char_class(char *s) 133 1.1 cgd { 134 1.6 alm int c, d; 135 1.6 alm 136 1.6 alm if (*s == '^') 137 1.6 alm s++; 138 1.6 alm if (*s == ']') 139 1.6 alm s++; 140 1.6 alm for (; *s != ']' && *s != '\n'; s++) 141 1.6 alm if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) 142 1.6 alm for (s++, c = *++s; *s != ']' || c != d; s++) 143 1.6 alm if ((c = *s) == '\n') 144 1.6 alm return NULL; 145 1.6 alm return (*s == ']') ? s : NULL; 146 1.1 cgd } 147