Home | History | Annotate | Line # | Download | only in ed
glbl.c revision 1.3
      1  1.3  thorpej /*	$NetBSD: glbl.c,v 1.3 1997/07/20 06:35:38 thorpej Exp $	*/
      2  1.2      cgd 
      3  1.1      alm /* glob.c: This file contains the global command routines for the ed line
      4  1.1      alm    editor */
      5  1.1      alm /*-
      6  1.1      alm  * Copyright (c) 1993 Andrew Moore, Talke Studio.
      7  1.1      alm  * All rights reserved.
      8  1.1      alm  *
      9  1.1      alm  * Redistribution and use in source and binary forms, with or without
     10  1.1      alm  * modification, are permitted provided that the following conditions
     11  1.1      alm  * are met:
     12  1.1      alm  * 1. Redistributions of source code must retain the above copyright
     13  1.1      alm  *    notice, this list of conditions and the following disclaimer.
     14  1.1      alm  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1      alm  *    notice, this list of conditions and the following disclaimer in the
     16  1.1      alm  *    documentation and/or other materials provided with the distribution.
     17  1.1      alm  *
     18  1.1      alm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  1.1      alm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1      alm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1      alm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.1      alm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1      alm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1      alm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1      alm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1      alm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1      alm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1      alm  * SUCH DAMAGE.
     29  1.1      alm  */
     30  1.1      alm 
     31  1.3  thorpej #include <sys/cdefs.h>
     32  1.1      alm #ifndef lint
     33  1.2      cgd #if 0
     34  1.1      alm static char *rcsid = "@(#)glob.c,v 1.1 1994/02/01 00:34:40 alm Exp";
     35  1.2      cgd #else
     36  1.3  thorpej __RCSID("$NetBSD: glbl.c,v 1.3 1997/07/20 06:35:38 thorpej Exp $");
     37  1.2      cgd #endif
     38  1.1      alm #endif /* not lint */
     39  1.1      alm 
     40  1.1      alm #include <sys/ioctl.h>
     41  1.1      alm #include <sys/wait.h>
     42  1.1      alm 
     43  1.1      alm #include "ed.h"
     44  1.1      alm 
     45  1.1      alm 
     46  1.1      alm /* build_active_list:  add line matching a pattern to the global-active list */
     47  1.1      alm int
     48  1.1      alm build_active_list(isgcmd)
     49  1.1      alm 	int isgcmd;
     50  1.1      alm {
     51  1.1      alm 	pattern_t *pat;
     52  1.1      alm 	line_t *lp;
     53  1.1      alm 	long n;
     54  1.1      alm 	char *s;
     55  1.1      alm 	char delimiter;
     56  1.1      alm 
     57  1.1      alm 	if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
     58  1.1      alm 		sprintf(errmsg, "invalid pattern delimiter");
     59  1.1      alm 		return ERR;
     60  1.1      alm 	} else if ((pat = get_compiled_pattern()) == NULL)
     61  1.1      alm 		return ERR;
     62  1.1      alm 	else if (*ibufp == delimiter)
     63  1.1      alm 		ibufp++;
     64  1.1      alm 	clear_active_list();
     65  1.1      alm 	lp = get_addressed_line_node(first_addr);
     66  1.1      alm 	for (n = first_addr; n <= second_addr; n++, lp = lp->q_forw) {
     67  1.1      alm 		if ((s = get_sbuf_line(lp)) == NULL)
     68  1.1      alm 			return ERR;
     69  1.1      alm 		if (isbinary)
     70  1.1      alm 			NUL_TO_NEWLINE(s, lp->len);
     71  1.1      alm 		if (!regexec(pat, s, 0, NULL, 0) == isgcmd &&
     72  1.1      alm 		    set_active_node(lp) < 0)
     73  1.1      alm 			return ERR;
     74  1.1      alm 	}
     75  1.1      alm 	return 0;
     76  1.1      alm }
     77  1.1      alm 
     78  1.1      alm 
     79  1.1      alm /* exec_global: apply command list in the command buffer to the active
     80  1.1      alm    lines in a range; return command status */
     81  1.1      alm long
     82  1.1      alm exec_global(interact, gflag)
     83  1.1      alm 	int interact;
     84  1.1      alm 	int gflag;
     85  1.1      alm {
     86  1.1      alm 	static char *ocmd = NULL;
     87  1.1      alm 	static int ocmdsz = 0;
     88  1.1      alm 
     89  1.1      alm 	line_t *lp = NULL;
     90  1.1      alm 	int status;
     91  1.1      alm 	int n;
     92  1.1      alm 	char *cmd = NULL;
     93  1.1      alm 
     94  1.1      alm #ifdef BACKWARDS
     95  1.1      alm 	if (!interact)
     96  1.1      alm 		if (!strcmp(ibufp, "\n"))
     97  1.1      alm 			cmd = "p\n";		/* null cmd-list == `p' */
     98  1.1      alm 		else if ((cmd = get_extended_line(&n, 0)) == NULL)
     99  1.1      alm 			return ERR;
    100  1.1      alm #else
    101  1.1      alm 	if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
    102  1.1      alm 		return ERR;
    103  1.1      alm #endif
    104  1.1      alm 	clear_undo_stack();
    105  1.1      alm 	while ((lp = next_active_node()) != NULL) {
    106  1.1      alm 		if ((current_addr = get_line_node_addr(lp)) < 0)
    107  1.1      alm 			return ERR;
    108  1.1      alm 		if (interact) {
    109  1.1      alm 			/* print current_addr; get a command in global syntax */
    110  1.1      alm 			if (display_lines(current_addr, current_addr, gflag) < 0)
    111  1.1      alm 				return ERR;
    112  1.1      alm 			while ((n = get_tty_line()) > 0 &&
    113  1.1      alm 			    ibuf[n - 1] != '\n')
    114  1.1      alm 				clearerr(stdin);
    115  1.1      alm 			if (n < 0)
    116  1.1      alm 				return ERR;
    117  1.1      alm 			else if (n == 0) {
    118  1.1      alm 				sprintf(errmsg, "unexpected end-of-file");
    119  1.1      alm 				return ERR;
    120  1.1      alm 			} else if (n == 1 && !strcmp(ibuf, "\n"))
    121  1.1      alm 				continue;
    122  1.1      alm 			else if (n == 2 && !strcmp(ibuf, "&\n")) {
    123  1.1      alm 				if (cmd == NULL) {
    124  1.1      alm 					sprintf(errmsg, "no previous command");
    125  1.1      alm 					return ERR;
    126  1.1      alm 				} else cmd = ocmd;
    127  1.1      alm 			} else if ((cmd = get_extended_line(&n, 0)) == NULL)
    128  1.1      alm 				return ERR;
    129  1.1      alm 			else {
    130  1.1      alm 				REALLOC(ocmd, ocmdsz, n + 1, ERR);
    131  1.1      alm 				memcpy(ocmd, cmd, n + 1);
    132  1.1      alm 				cmd = ocmd;
    133  1.1      alm 			}
    134  1.1      alm 
    135  1.1      alm 		}
    136  1.1      alm 		ibufp = cmd;
    137  1.1      alm 		for (; *ibufp;)
    138  1.1      alm 			if ((status = extract_addr_range()) < 0 ||
    139  1.1      alm 			    (status = exec_command()) < 0 ||
    140  1.3  thorpej 			    (status > 0 && (status = display_lines(
    141  1.3  thorpej 			    current_addr, current_addr, status))) < 0)
    142  1.1      alm 				return status;
    143  1.1      alm 	}
    144  1.1      alm 	return 0;
    145  1.1      alm }
    146  1.1      alm 
    147  1.1      alm 
    148  1.1      alm line_t **active_list;		/* list of lines active in a global command */
    149  1.1      alm long active_last;		/* index of last active line in active_list */
    150  1.1      alm long active_size;		/* size of active_list */
    151  1.1      alm long active_ptr;		/* active_list index (non-decreasing) */
    152  1.1      alm long active_ndx;		/* active_list index (modulo active_last) */
    153  1.1      alm 
    154  1.1      alm /* set_active_node: add a line node to the global-active list */
    155  1.1      alm int
    156  1.1      alm set_active_node(lp)
    157  1.1      alm 	line_t *lp;
    158  1.1      alm {
    159  1.1      alm 	if (active_last + 1 > active_size) {
    160  1.1      alm 		int ti = active_size;
    161  1.1      alm 		line_t **ts;
    162  1.1      alm 		SPL1();
    163  1.1      alm #if defined(sun) || defined(NO_REALLOC_NULL)
    164  1.1      alm 		if (active_list != NULL) {
    165  1.1      alm #endif
    166  1.1      alm 			if ((ts = (line_t **) realloc(active_list,
    167  1.1      alm 			    (ti += MINBUFSZ) * sizeof(line_t **))) == NULL) {
    168  1.1      alm 				fprintf(stderr, "%s\n", strerror(errno));
    169  1.1      alm 				sprintf(errmsg, "out of memory");
    170  1.1      alm 				SPL0();
    171  1.1      alm 				return ERR;
    172  1.1      alm 			}
    173  1.1      alm #if defined(sun) || defined(NO_REALLOC_NULL)
    174  1.1      alm 		} else {
    175  1.1      alm 			if ((ts = (line_t **) malloc((ti += MINBUFSZ) *
    176  1.1      alm 			    sizeof(line_t **))) == NULL) {
    177  1.1      alm 				fprintf(stderr, "%s\n", strerror(errno));
    178  1.1      alm 				sprintf(errmsg, "out of memory");
    179  1.1      alm 				SPL0();
    180  1.1      alm 				return ERR;
    181  1.1      alm 			}
    182  1.1      alm 		}
    183  1.1      alm #endif
    184  1.1      alm 		active_size = ti;
    185  1.1      alm 		active_list = ts;
    186  1.1      alm 		SPL0();
    187  1.1      alm 	}
    188  1.1      alm 	active_list[active_last++] = lp;
    189  1.1      alm 	return 0;
    190  1.1      alm }
    191  1.1      alm 
    192  1.1      alm 
    193  1.1      alm /* unset_active_nodes: remove a range of lines from the global-active list */
    194  1.1      alm void
    195  1.1      alm unset_active_nodes(np, mp)
    196  1.1      alm 	line_t *np, *mp;
    197  1.1      alm {
    198  1.1      alm 	line_t *lp;
    199  1.1      alm 	long i;
    200  1.1      alm 
    201  1.1      alm 	for (lp = np; lp != mp; lp = lp->q_forw)
    202  1.1      alm 		for (i = 0; i < active_last; i++)
    203  1.1      alm 			if (active_list[active_ndx] == lp) {
    204  1.1      alm 				active_list[active_ndx] = NULL;
    205  1.1      alm 				active_ndx = INC_MOD(active_ndx, active_last - 1);
    206  1.1      alm 				break;
    207  1.1      alm 			} else	active_ndx = INC_MOD(active_ndx, active_last - 1);
    208  1.1      alm }
    209  1.1      alm 
    210  1.1      alm 
    211  1.1      alm /* next_active_node: return the next global-active line node */
    212  1.1      alm line_t *
    213  1.1      alm next_active_node()
    214  1.1      alm {
    215  1.1      alm 	while (active_ptr < active_last && active_list[active_ptr] == NULL)
    216  1.1      alm 		active_ptr++;
    217  1.1      alm 	return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
    218  1.1      alm }
    219  1.1      alm 
    220  1.1      alm 
    221  1.1      alm /* clear_active_list: clear the global-active list */
    222  1.1      alm void
    223  1.1      alm clear_active_list()
    224  1.1      alm {
    225  1.1      alm 	SPL1();
    226  1.1      alm 	active_size = active_last = active_ptr = active_ndx = 0;
    227  1.1      alm 	free(active_list);
    228  1.1      alm 	active_list = NULL;
    229  1.1      alm 	SPL0();
    230  1.1      alm }
    231