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