xref: /openbsd/bin/ed/glbl.c (revision 404b540a)
1 /*	$OpenBSD: glbl.c,v 1.11 2007/09/03 14:26:54 deraadt Exp $	*/
2 /*	$NetBSD: glbl.c,v 1.2 1995/03/21 09:04:41 cgd Exp $	*/
3 
4 /* glob.c: This file contains the global command routines for the ed line
5    editor */
6 /*-
7  * Copyright (c) 1993 Andrew Moore, Talke Studio.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 #if 0
34 static char *rcsid = "@(#)glob.c,v 1.1 1994/02/01 00:34:40 alm Exp";
35 #else
36 static char rcsid[] = "$OpenBSD: glbl.c,v 1.11 2007/09/03 14:26:54 deraadt Exp $";
37 #endif
38 #endif /* not lint */
39 
40 #include <sys/ioctl.h>
41 #include <sys/wait.h>
42 
43 #include "ed.h"
44 
45 
46 /* build_active_list:  add line matching a pattern to the global-active list */
47 int
48 build_active_list(int isgcmd)
49 {
50 	pattern_t *pat;
51 	line_t *lp;
52 	int n;
53 	char *s;
54 	char delimiter;
55 
56 	if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
57 		seterrmsg("invalid pattern delimiter");
58 		return ERR;
59 	} else if ((pat = get_compiled_pattern()) == NULL)
60 		return ERR;
61 	else if (*ibufp == delimiter)
62 		ibufp++;
63 	clear_active_list();
64 	lp = get_addressed_line_node(first_addr);
65 	for (n = first_addr; n <= second_addr; n++, lp = lp->q_forw) {
66 		if ((s = get_sbuf_line(lp)) == NULL)
67 			return ERR;
68 		if (isbinary)
69 			NUL_TO_NEWLINE(s, lp->len);
70 		if (!regexec(pat, s, 0, NULL, 0) == isgcmd &&
71 		    set_active_node(lp) < 0)
72 			return ERR;
73 	}
74 	return 0;
75 }
76 
77 
78 /* exec_global: apply command list in the command buffer to the active
79    lines in a range; return command status */
80 int
81 exec_global(int interact, int gflag)
82 {
83 	static char *ocmd = NULL;
84 	static int ocmdsz = 0;
85 
86 	line_t *lp = NULL;
87 	int status;
88 	int n;
89 	char *cmd = NULL;
90 
91 #ifdef BACKWARDS
92 	if (!interact) {
93 		if (!strcmp(ibufp, "\n"))
94 			cmd = "p\n";		/* null cmd-list == `p' */
95 		else if ((cmd = get_extended_line(&n, 0)) == NULL)
96 			return ERR;
97 	}
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 				seterrmsg("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 					seterrmsg("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 int active_last;		/* index of last active line in active_list */
148 int active_size;		/* size of active_list */
149 int active_ptr;			/* active_list index (non-decreasing) */
150 int 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(line_t *lp)
155 {
156 	if (active_last + 1 > active_size) {
157 		int ti = active_size;
158 		line_t **ts;
159 		SPL1();
160 #if defined(sun) || defined(NO_REALLOC_NULL)
161 		if (active_list != NULL) {
162 #endif
163 			if ((ts = (line_t **) realloc(active_list,
164 			    (ti += MINBUFSZ) * sizeof(line_t **))) == NULL) {
165 				perror(NULL);
166 				seterrmsg("out of memory");
167 				SPL0();
168 				return ERR;
169 			}
170 #if defined(sun) || defined(NO_REALLOC_NULL)
171 		} else {
172 			if ((ts = (line_t **) calloc(ti += MINBUFSZ,
173 			    sizeof(line_t **))) == NULL) {
174 				perror(NULL);
175 				seterrmsg("out of memory");
176 				SPL0();
177 				return ERR;
178 			}
179 		}
180 #endif
181 		active_size = ti;
182 		active_list = ts;
183 		SPL0();
184 	}
185 	active_list[active_last++] = lp;
186 	return 0;
187 }
188 
189 
190 /* unset_active_nodes: remove a range of lines from the global-active list */
191 void
192 unset_active_nodes(line_t *np, line_t *mp)
193 {
194 	line_t *lp;
195 	int 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 */
208 line_t *
209 next_active_node(void)
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 */
218 void
219 clear_active_list(void)
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