xref: /freebsd/bin/ed/glbl.c (revision 3ff369fed2a08f32dda232c10470b949bef9489f)
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
30 static const char rcsid[] =
31   "$FreeBSD$";
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 */
43 int
44 build_active_list(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 		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 */
76 long
77 exec_global(int interact, int gflag)
78 {
79 	static char *ocmd = NULL;
80 	static int ocmdsz = 0;
81 
82 	line_t *lp = NULL;
83 	int status;
84 	int n;
85 	char *cmd = NULL;
86 
87 #ifdef BACKWARDS
88 	if (!interact)
89 		if (!strcmp(ibufp, "\n"))
90 			cmd = "p\n";		/* null cmd-list == `p' */
91 		else if ((cmd = get_extended_line(&n, 0)) == NULL)
92 			return ERR;
93 #else
94 	if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
95 		return ERR;
96 #endif
97 	clear_undo_stack();
98 	while ((lp = next_active_node()) != NULL) {
99 		if ((current_addr = get_line_node_addr(lp)) < 0)
100 			return ERR;
101 		if (interact) {
102 			/* print current_addr; get a command in global syntax */
103 			if (display_lines(current_addr, current_addr, gflag) < 0)
104 				return ERR;
105 			while ((n = get_tty_line()) > 0 &&
106 			    ibuf[n - 1] != '\n')
107 				clearerr(stdin);
108 			if (n < 0)
109 				return ERR;
110 			else if (n == 0) {
111 				errmsg = "unexpected end-of-file";
112 				return ERR;
113 			} else if (n == 1 && !strcmp(ibuf, "\n"))
114 				continue;
115 			else if (n == 2 && !strcmp(ibuf, "&\n")) {
116 				if (cmd == NULL) {
117 					errmsg = "no previous command";
118 					return ERR;
119 				} else cmd = ocmd;
120 			} else if ((cmd = get_extended_line(&n, 0)) == NULL)
121 				return ERR;
122 			else {
123 				REALLOC(ocmd, ocmdsz, n + 1, ERR);
124 				memcpy(ocmd, cmd, n + 1);
125 				cmd = ocmd;
126 			}
127 
128 		}
129 		ibufp = cmd;
130 		for (; *ibufp;)
131 			if ((status = extract_addr_range()) < 0 ||
132 			    (status = exec_command()) < 0 ||
133 			    (status > 0 && (status = display_lines(
134 			    current_addr, current_addr, status)) < 0))
135 				return status;
136 	}
137 	return 0;
138 }
139 
140 
141 line_t **active_list;		/* list of lines active in a global command */
142 long active_last;		/* index of last active line in active_list */
143 long active_size;		/* size of active_list */
144 long active_ptr;		/* active_list index (non-decreasing) */
145 long active_ndx;		/* active_list index (modulo active_last) */
146 
147 /* set_active_node: add a line node to the global-active list */
148 int
149 set_active_node(line_t *lp)
150 {
151 	if (active_last + 1 > active_size) {
152 		int ti = active_size;
153 		line_t **ts;
154 		SPL1();
155 #if defined(sun) || defined(NO_REALLOC_NULL)
156 		if (active_list != NULL) {
157 #endif
158 			if ((ts = (line_t **) realloc(active_list,
159 			    (ti += MINBUFSZ) * sizeof(line_t **))) == NULL) {
160 				fprintf(stderr, "%s\n", strerror(errno));
161 				errmsg = "out of memory";
162 				SPL0();
163 				return ERR;
164 			}
165 #if defined(sun) || defined(NO_REALLOC_NULL)
166 		} else {
167 			if ((ts = (line_t **) malloc((ti += MINBUFSZ) *
168 			    sizeof(line_t **))) == NULL) {
169 				fprintf(stderr, "%s\n", strerror(errno));
170 				errmsg = "out of memory";
171 				SPL0();
172 				return ERR;
173 			}
174 		}
175 #endif
176 		active_size = ti;
177 		active_list = ts;
178 		SPL0();
179 	}
180 	active_list[active_last++] = lp;
181 	return 0;
182 }
183 
184 
185 /* unset_active_nodes: remove a range of lines from the global-active list */
186 void
187 unset_active_nodes(line_t *np, line_t *mp)
188 {
189 	line_t *lp;
190 	long i;
191 
192 	for (lp = np; lp != mp; lp = lp->q_forw)
193 		for (i = 0; i < active_last; i++)
194 			if (active_list[active_ndx] == lp) {
195 				active_list[active_ndx] = NULL;
196 				active_ndx = INC_MOD(active_ndx, active_last - 1);
197 				break;
198 			} else	active_ndx = INC_MOD(active_ndx, active_last - 1);
199 }
200 
201 
202 /* next_active_node: return the next global-active line node */
203 line_t *
204 next_active_node(void)
205 {
206 	while (active_ptr < active_last && active_list[active_ptr] == NULL)
207 		active_ptr++;
208 	return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
209 }
210 
211 
212 /* clear_active_list: clear the global-active list */
213 void
214 clear_active_list(void)
215 {
216 	SPL1();
217 	active_size = active_last = active_ptr = active_ndx = 0;
218 	free(active_list);
219 	active_list = NULL;
220 	SPL0();
221 }
222