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