Home | History | Annotate | Line # | Download | only in ed
glbl.c revision 1.4
      1 /*	$NetBSD: glbl.c,v 1.4 1998/08/19 01:33:31 thorpej Exp $	*/
      2 
      3 /* glob.c: This file contains the global command routines for the ed line
      4    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 #include <sys/cdefs.h>
     32 #ifndef lint
     33 #if 0
     34 static char *rcsid = "@(#)glob.c,v 1.1 1994/02/01 00:34:40 alm Exp";
     35 #else
     36 __RCSID("$NetBSD: glbl.c,v 1.4 1998/08/19 01:33:31 thorpej Exp $");
     37 #endif
     38 #endif /* not lint */
     39 
     40 #include <sys/ioctl.h>
     41 #include <sys/wait.h>
     42 
     43 #include "ed.h"
     44 
     45 
     46 /* build_active_list:  add line matching a pattern to the global-active list */
     47 int
     48 build_active_list(isgcmd)
     49 	int isgcmd;
     50 {
     51 	pattern_t *pat;
     52 	line_t *lp;
     53 	long n;
     54 	char *s;
     55 	char delimiter;
     56 
     57 	if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
     58 		sprintf(errmsg, "invalid pattern delimiter");
     59 		return ERR;
     60 	} else if ((pat = get_compiled_pattern()) == NULL)
     61 		return ERR;
     62 	else if (*ibufp == delimiter)
     63 		ibufp++;
     64 	clear_active_list();
     65 	lp = get_addressed_line_node(first_addr);
     66 	for (n = first_addr; n <= second_addr; n++, lp = lp->q_forw) {
     67 		if ((s = get_sbuf_line(lp)) == NULL)
     68 			return ERR;
     69 		if (isbinary)
     70 			NUL_TO_NEWLINE(s, lp->len);
     71 		if (!regexec(pat, s, 0, NULL, 0) == isgcmd &&
     72 		    set_active_node(lp) < 0)
     73 			return ERR;
     74 	}
     75 	return 0;
     76 }
     77 
     78 
     79 /* exec_global: apply command list in the command buffer to the active
     80    lines in a range; return command status */
     81 long
     82 exec_global(interact, gflag)
     83 	int interact;
     84 	int gflag;
     85 {
     86 	static char *ocmd = NULL;
     87 	static int ocmdsz = 0;
     88 
     89 	line_t *lp = NULL;
     90 	int status;
     91 	int n;
     92 	char *cmd = NULL;
     93 
     94 #ifdef BACKWARDS
     95 	if (!interact) {
     96 		if (!strcmp(ibufp, "\n"))
     97 			cmd = "p\n";		/* null cmd-list == `p' */
     98 		else if ((cmd = get_extended_line(&n, 0)) == NULL)
     99 			return ERR;
    100 	}
    101 #else
    102 	if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
    103 		return ERR;
    104 #endif
    105 	clear_undo_stack();
    106 	while ((lp = next_active_node()) != NULL) {
    107 		if ((current_addr = get_line_node_addr(lp)) < 0)
    108 			return ERR;
    109 		if (interact) {
    110 			/* print current_addr; get a command in global syntax */
    111 			if (display_lines(current_addr, current_addr, gflag) < 0)
    112 				return ERR;
    113 			while ((n = get_tty_line()) > 0 &&
    114 			    ibuf[n - 1] != '\n')
    115 				clearerr(stdin);
    116 			if (n < 0)
    117 				return ERR;
    118 			else if (n == 0) {
    119 				sprintf(errmsg, "unexpected end-of-file");
    120 				return ERR;
    121 			} else if (n == 1 && !strcmp(ibuf, "\n"))
    122 				continue;
    123 			else if (n == 2 && !strcmp(ibuf, "&\n")) {
    124 				if (cmd == NULL) {
    125 					sprintf(errmsg, "no previous command");
    126 					return ERR;
    127 				} else cmd = ocmd;
    128 			} else if ((cmd = get_extended_line(&n, 0)) == NULL)
    129 				return ERR;
    130 			else {
    131 				REALLOC(ocmd, ocmdsz, n + 1, ERR);
    132 				memcpy(ocmd, cmd, n + 1);
    133 				cmd = ocmd;
    134 			}
    135 
    136 		}
    137 		ibufp = cmd;
    138 		for (; *ibufp;)
    139 			if ((status = extract_addr_range()) < 0 ||
    140 			    (status = exec_command()) < 0 ||
    141 			    (status > 0 && (status = display_lines(
    142 			    current_addr, current_addr, status))) < 0)
    143 				return status;
    144 	}
    145 	return 0;
    146 }
    147 
    148 
    149 line_t **active_list;		/* list of lines active in a global command */
    150 long active_last;		/* index of last active line in active_list */
    151 long active_size;		/* size of active_list */
    152 long active_ptr;		/* active_list index (non-decreasing) */
    153 long active_ndx;		/* active_list index (modulo active_last) */
    154 
    155 /* set_active_node: add a line node to the global-active list */
    156 int
    157 set_active_node(lp)
    158 	line_t *lp;
    159 {
    160 	if (active_last + 1 > active_size) {
    161 		int ti = active_size;
    162 		line_t **ts;
    163 		SPL1();
    164 #if defined(sun) || defined(NO_REALLOC_NULL)
    165 		if (active_list != NULL) {
    166 #endif
    167 			if ((ts = (line_t **) realloc(active_list,
    168 			    (ti += MINBUFSZ) * sizeof(line_t **))) == NULL) {
    169 				fprintf(stderr, "%s\n", strerror(errno));
    170 				sprintf(errmsg, "out of memory");
    171 				SPL0();
    172 				return ERR;
    173 			}
    174 #if defined(sun) || defined(NO_REALLOC_NULL)
    175 		} else {
    176 			if ((ts = (line_t **) malloc((ti += MINBUFSZ) *
    177 			    sizeof(line_t **))) == NULL) {
    178 				fprintf(stderr, "%s\n", strerror(errno));
    179 				sprintf(errmsg, "out of memory");
    180 				SPL0();
    181 				return ERR;
    182 			}
    183 		}
    184 #endif
    185 		active_size = ti;
    186 		active_list = ts;
    187 		SPL0();
    188 	}
    189 	active_list[active_last++] = lp;
    190 	return 0;
    191 }
    192 
    193 
    194 /* unset_active_nodes: remove a range of lines from the global-active list */
    195 void
    196 unset_active_nodes(np, mp)
    197 	line_t *np, *mp;
    198 {
    199 	line_t *lp;
    200 	long i;
    201 
    202 	for (lp = np; lp != mp; lp = lp->q_forw)
    203 		for (i = 0; i < active_last; i++)
    204 			if (active_list[active_ndx] == lp) {
    205 				active_list[active_ndx] = NULL;
    206 				active_ndx = INC_MOD(active_ndx, active_last - 1);
    207 				break;
    208 			} else	active_ndx = INC_MOD(active_ndx, active_last - 1);
    209 }
    210 
    211 
    212 /* next_active_node: return the next global-active line node */
    213 line_t *
    214 next_active_node()
    215 {
    216 	while (active_ptr < active_last && active_list[active_ptr] == NULL)
    217 		active_ptr++;
    218 	return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
    219 }
    220 
    221 
    222 /* clear_active_list: clear the global-active list */
    223 void
    224 clear_active_list()
    225 {
    226 	SPL1();
    227 	active_size = active_last = active_ptr = active_ndx = 0;
    228 	free(active_list);
    229 	active_list = NULL;
    230 	SPL0();
    231 }
    232