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