sync
[bitrig.git] / bin / ed / glbl.c
blob122e9c4038cadc76cd6a9f9e36de9ca871b22385
1 /* $OpenBSD: glbl.c,v 1.13 2012/12/04 02:40:48 deraadt Exp $ */
2 /* $NetBSD: glbl.c,v 1.2 1995/03/21 09:04:41 cgd Exp $ */
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.
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.
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.
32 #include <sys/ioctl.h>
33 #include <sys/wait.h>
35 #include "ed.h"
38 /* build_active_list: add line matching a pattern to the global-active list */
39 int
40 build_active_list(int isgcmd)
42 pattern_t *pat;
43 line_t *lp;
44 int n;
45 char *s;
46 char delimiter;
48 if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
49 seterrmsg("invalid pattern delimiter");
50 return ERR;
51 } else if ((pat = get_compiled_pattern()) == NULL)
52 return ERR;
53 else if (*ibufp == delimiter)
54 ibufp++;
55 clear_active_list();
56 lp = get_addressed_line_node(first_addr);
57 for (n = first_addr; n <= second_addr; n++, lp = lp->q_forw) {
58 if ((s = get_sbuf_line(lp)) == NULL)
59 return ERR;
60 if (isbinary)
61 NUL_TO_NEWLINE(s, lp->len);
62 if (!regexec(pat, s, 0, NULL, 0) == isgcmd &&
63 set_active_node(lp) < 0)
64 return ERR;
66 return 0;
70 /* exec_global: apply command list in the command buffer to the active
71 lines in a range; return command status */
72 int
73 exec_global(int interact, int gflag)
75 static char *ocmd = NULL;
76 static int ocmdsz = 0;
78 line_t *lp = NULL;
79 int status;
80 int n;
81 char *cmd = NULL;
83 #ifdef BACKWARDS
84 if (!interact) {
85 if (!strcmp(ibufp, "\n"))
86 cmd = "p\n"; /* null cmd-list == `p' */
87 else if ((cmd = get_extended_line(&n, 0)) == NULL)
88 return ERR;
90 #else
91 if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
92 return ERR;
93 #endif
94 clear_undo_stack();
95 while ((lp = next_active_node()) != NULL) {
96 if ((current_addr = get_line_node_addr(lp)) < 0)
97 return ERR;
98 if (interact) {
99 /* print current_addr; get a command in global syntax */
100 if (display_lines(current_addr, current_addr, gflag) < 0)
101 return ERR;
102 while ((n = get_tty_line()) > 0 &&
103 ibuf[n - 1] != '\n')
104 clearerr(stdin);
105 if (n < 0)
106 return ERR;
107 else if (n == 0) {
108 seterrmsg("unexpected end-of-file");
109 return ERR;
110 } else if (n == 1 && !strcmp(ibuf, "\n"))
111 continue;
112 else if (n == 2 && !strcmp(ibuf, "&\n")) {
113 if (cmd == NULL) {
114 seterrmsg("no previous command");
115 return ERR;
116 } else cmd = ocmd;
117 } else if ((cmd = get_extended_line(&n, 0)) == NULL)
118 return ERR;
119 else {
120 REALLOC(ocmd, ocmdsz, n + 1, ERR);
121 memcpy(ocmd, cmd, n + 1);
122 cmd = ocmd;
126 ibufp = cmd;
127 for (; *ibufp;)
128 if ((status = extract_addr_range()) < 0 ||
129 (status = exec_command()) < 0 ||
130 (status > 0 && (status = display_lines(
131 current_addr, current_addr, status)) < 0))
132 return status;
134 return 0;
138 line_t **active_list; /* list of lines active in a global command */
139 int active_last; /* index of last active line in active_list */
140 int active_size; /* size of active_list */
141 int active_ptr; /* active_list index (non-decreasing) */
142 int active_ndx; /* active_list index (modulo active_last) */
144 /* set_active_node: add a line node to the global-active list */
146 set_active_node(line_t *lp)
148 if (active_last + 1 > active_size) {
149 int ti = active_size;
150 line_t **ts;
151 SPL1();
152 if ((ts = (line_t **) realloc(active_list,
153 (ti += MINBUFSZ) * sizeof(line_t **))) == NULL) {
154 perror(NULL);
155 seterrmsg("out of memory");
156 SPL0();
157 return ERR;
159 active_size = ti;
160 active_list = ts;
161 SPL0();
163 active_list[active_last++] = lp;
164 return 0;
168 /* unset_active_nodes: remove a range of lines from the global-active list */
169 void
170 unset_active_nodes(line_t *np, line_t *mp)
172 line_t *lp;
173 int i;
175 for (lp = np; lp != mp; lp = lp->q_forw)
176 for (i = 0; i < active_last; i++)
177 if (active_list[active_ndx] == lp) {
178 active_list[active_ndx] = NULL;
179 active_ndx = INC_MOD(active_ndx, active_last - 1);
180 break;
181 } else active_ndx = INC_MOD(active_ndx, active_last - 1);
185 /* next_active_node: return the next global-active line node */
186 line_t *
187 next_active_node(void)
189 while (active_ptr < active_last && active_list[active_ptr] == NULL)
190 active_ptr++;
191 return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
195 /* clear_active_list: clear the global-active list */
196 void
197 clear_active_list(void)
199 SPL1();
200 active_size = active_last = active_ptr = active_ndx = 0;
201 free(active_list);
202 active_list = NULL;
203 SPL0();