Home | History | Annotate | Line # | Download | only in ed
re.c revision 1.3
      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
     40 static 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 
     49 extern char *ibufp;
     50 extern int patlock;
     51 
     52 char errmsg[MAXFNAME + 40] = "";
     53 
     54 /* optpat: return pointer to compiled pattern from command buffer */
     55 pattern_t *
     56 optpat()
     57 {
     58 	static pattern_t *exp = NULL;
     59 
     60 	char *exps;
     61 	char delim;
     62 	int n;
     63 
     64 	if ((delim = *ibufp) == '\n') {
     65 		sprintf(errmsg, "no previous pattern");
     66 		return exp;
     67 	} else if (delim == ' ' || *++ibufp == '\n') {
     68 		sprintf(errmsg, "invalid pattern delimiter");
     69 		return NULL;
     70 	} else if (*ibufp == delim) {
     71 		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 #ifdef GNU_REGEX
     85 	/* initialize pattern buffer */
     86 	exp->buffer = NULL;
     87 	exp->allocated = 0L;
     88 	exp->fastmap = (char *) malloc(FASTMAP_SIZE);
     89 	exp->translate = 0;
     90 #endif
     91 	if (n = regcomp(exp, exps, 0)) {
     92 		regerror(n, exp, errmsg, sizeof errmsg);
     93 		return NULL;
     94 	}
     95 	return exp;
     96 }
     97 
     98 
     99 /* getlhs: copy a pattern string from the command buffer; return pointer
    100    to the copy */
    101 char *
    102 getlhs(delim)
    103 	int delim;
    104 {
    105 	static char buf[MAXLINE];
    106 	char *nd;
    107 
    108 	for (nd = ibufp; *nd != delim && *nd != '\n'; nd++)
    109 		switch (*nd) {
    110 		default:
    111 			break;
    112 		case CCL:
    113 			if ((nd = ccl(CCLEND, ++nd)) == NULL) {
    114 				sprintf(errmsg, "unbalanced brackets ([])");
    115 				return NULL;
    116 			}
    117 			break;
    118 		case ESCHAR:
    119 			if (*++nd == '\n') {
    120 				sprintf(errmsg, "trailing backslash (\\)");
    121 				return NULL;
    122 			}
    123 			break;
    124 		}
    125 	strncpy(buf, ibufp, nd - ibufp);
    126 	buf[nd - ibufp] = '\0';
    127 	ibufp = nd;
    128 	return buf;
    129 }
    130 
    131 
    132 /* ccl: expand a character class */
    133 char *
    134 ccl(delim, src)
    135 	int	delim;
    136 	char	*src;
    137 {
    138 	if (*src == '^')
    139 		src++;
    140 	if (*src == delim)
    141 		src++;
    142 	while (*src != delim && *src != '\n')
    143 		src++;
    144 	return  (*src == delim) ? src : NULL;
    145 }
    146