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