xref: /freebsd/bin/ed/glbl.c (revision 7f3dea244c40159a41ab22da77a434d7c5b5e85a)
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 #if 0
31 static char * const rcsid = "@(#)glob.c,v 1.1 1994/02/01 00:34:40 alm Exp";
32 #else
33 static char * const rcsid =
34   "$FreeBSD$";
35 #endif
36 #endif /* not lint */
37 
38 #include <sys/ioctl.h>
39 #include <sys/wait.h>
40 
41 #include "ed.h"
42 
43 
44 /* build_active_list:  add line matching a pattern to the global-active list */
45 int
46 build_active_list(isgcmd)
47 	int isgcmd;
48 {
49 	pattern_t *pat;
50 	line_t *lp;
51 	long n;
52 	char *s;
53 	char delimiter;
54 
55 	if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
56 		sprintf(errmsg, "invalid pattern delimiter");
57 		return ERR;
58 	} else if ((pat = get_compiled_pattern()) == NULL)
59 		return ERR;
60 	else if (*ibufp == delimiter)
61 		ibufp++;
62 	clear_active_list();
63 	lp = get_addressed_line_node(first_addr);
64 	for (n = first_addr; n <= second_addr; n++, lp = lp->q_forw) {
65 		if ((s = get_sbuf_line(lp)) == NULL)
66 			return ERR;
67 		if (isbinary)
68 			NUL_TO_NEWLINE(s, lp->len);
69 		if (!regexec(pat, s, 0, NULL, 0) == isgcmd &&
70 		    set_active_node(lp) < 0)
71 			return ERR;
72 	}
73 	return 0;
74 }
75 
76 
77 /* exec_global: apply command list in the command buffer to the active
78    lines in a range; return command status */
79 long
80 exec_global(interact, gflag)
81 	int interact;
82 	int gflag;
83 {
84 	static char *ocmd = NULL;
85 	static int ocmdsz = 0;
86 
87 	line_t *lp = NULL;
88 	int status;
89 	int n;
90 	char *cmd = NULL;
91 
92 #ifdef BACKWARDS
93 	if (!interact)
94 		if (!strcmp(ibufp, "\n"))
95 			cmd = "p\n";		/* null cmd-list == `p' */
96 		else if ((cmd = get_extended_line(&n, 0)) == NULL)
97 			return ERR;
98 #else
99 	if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
100 		return ERR;
101 #endif
102 	clear_undo_stack();
103 	while ((lp = next_active_node()) != NULL) {
104 		if ((current_addr = get_line_node_addr(lp)) < 0)
105 			return ERR;
106 		if (interact) {
107 			/* print current_addr; get a command in global syntax */
108 			if (display_lines(current_addr, current_addr, gflag) < 0)
109 				return ERR;
110 			while ((n = get_tty_line()) > 0 &&
111 			    ibuf[n - 1] != '\n')
112 				clearerr(stdin);
113 			if (n < 0)
114 				return ERR;
115 			else if (n == 0) {
116 				sprintf(errmsg, "unexpected end-of-file");
117 				return ERR;
118 			} else if (n == 1 && !strcmp(ibuf, "\n"))
119 				continue;
120 			else if (n == 2 && !strcmp(ibuf, "&\n")) {
121 				if (cmd == NULL) {
122 					sprintf(errmsg, "no previous command");
123 					return ERR;
124 				} else cmd = ocmd;
125 			} else if ((cmd = get_extended_line(&n, 0)) == NULL)
126 				return ERR;
127 			else {
128 				REALLOC(ocmd, ocmdsz, n + 1, ERR);
129 				memcpy(ocmd, cmd, n + 1);
130 				cmd = ocmd;
131 			}
132 
133 		}
134 		ibufp = cmd;
135 		for (; *ibufp;)
136 			if ((status = extract_addr_range()) < 0 ||
137 			    (status = exec_command()) < 0 ||
138 			    (status > 0 && (status = display_lines(
139 			    current_addr, current_addr, status)) < 0))
140 				return status;
141 	}
142 	return 0;
143 }
144 
145 
146 line_t **active_list;		/* list of lines active in a global command */
147 long active_last;		/* index of last active line in active_list */
148 long active_size;		/* size of active_list */
149 long active_ptr;		/* active_list index (non-decreasing) */
150 long active_ndx;		/* active_list index (modulo active_last) */
151 
152 /* set_active_node: add a line node to the global-active list */
153 int
154 set_active_node(lp)
155 	line_t *lp;
156 {
157 	if (active_last + 1 > active_size) {
158 		int ti = active_size;
159 		line_t **ts;
160 		SPL1();
161 #if defined(sun) || defined(NO_REALLOC_NULL)
162 		if (active_list != NULL) {
163 #endif
164 			if ((ts = (line_t **) realloc(active_list,
165 			    (ti += MINBUFSZ) * sizeof(line_t **))) == NULL) {
166 				fprintf(stderr, "%s\n", strerror(errno));
167 				sprintf(errmsg, "out of memory");
168 				SPL0();
169 				return ERR;
170 			}
171 #if defined(sun) || defined(NO_REALLOC_NULL)
172 		} else {
173 			if ((ts = (line_t **) malloc((ti += MINBUFSZ) *
174 			    sizeof(line_t **))) == NULL) {
175 				fprintf(stderr, "%s\n", strerror(errno));
176 				sprintf(errmsg, "out of memory");
177 				SPL0();
178 				return ERR;
179 			}
180 		}
181 #endif
182 		active_size = ti;
183 		active_list = ts;
184 		SPL0();
185 	}
186 	active_list[active_last++] = lp;
187 	return 0;
188 }
189 
190 
191 /* unset_active_nodes: remove a range of lines from the global-active list */
192 void
193 unset_active_nodes(np, mp)
194 	line_t *np, *mp;
195 {
196 	line_t *lp;
197 	long i;
198 
199 	for (lp = np; lp != mp; lp = lp->q_forw)
200 		for (i = 0; i < active_last; i++)
201 			if (active_list[active_ndx] == lp) {
202 				active_list[active_ndx] = NULL;
203 				active_ndx = INC_MOD(active_ndx, active_last - 1);
204 				break;
205 			} else	active_ndx = INC_MOD(active_ndx, active_last - 1);
206 }
207 
208 
209 /* next_active_node: return the next global-active line node */
210 line_t *
211 next_active_node()
212 {
213 	while (active_ptr < active_last && active_list[active_ptr] == NULL)
214 		active_ptr++;
215 	return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
216 }
217 
218 
219 /* clear_active_list: clear the global-active list */
220 void
221 clear_active_list()
222 {
223 	SPL1();
224 	active_size = active_last = active_ptr = active_ndx = 0;
225 	free(active_list);
226 	active_list = NULL;
227 	SPL0();
228 }
229