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