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