glbl.c revision 1.1 1 1.1 alm /* glob.c: This file contains the global command routines for the ed line
2 1.1 alm editor */
3 1.1 alm /*-
4 1.1 alm * Copyright (c) 1993 Andrew Moore, Talke Studio.
5 1.1 alm * All rights reserved.
6 1.1 alm *
7 1.1 alm * Redistribution and use in source and binary forms, with or without
8 1.1 alm * modification, are permitted provided that the following conditions
9 1.1 alm * are met:
10 1.1 alm * 1. Redistributions of source code must retain the above copyright
11 1.1 alm * notice, this list of conditions and the following disclaimer.
12 1.1 alm * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 alm * notice, this list of conditions and the following disclaimer in the
14 1.1 alm * documentation and/or other materials provided with the distribution.
15 1.1 alm *
16 1.1 alm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 alm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 alm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 alm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 alm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 alm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 alm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 alm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 alm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 alm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 alm * SUCH DAMAGE.
27 1.1 alm */
28 1.1 alm
29 1.1 alm #ifndef lint
30 1.1 alm static char *rcsid = "@(#)glob.c,v 1.1 1994/02/01 00:34:40 alm Exp";
31 1.1 alm #endif /* not lint */
32 1.1 alm
33 1.1 alm #include <sys/ioctl.h>
34 1.1 alm #include <sys/wait.h>
35 1.1 alm
36 1.1 alm #include "ed.h"
37 1.1 alm
38 1.1 alm
39 1.1 alm /* build_active_list: add line matching a pattern to the global-active list */
40 1.1 alm int
41 1.1 alm build_active_list(isgcmd)
42 1.1 alm int isgcmd;
43 1.1 alm {
44 1.1 alm pattern_t *pat;
45 1.1 alm line_t *lp;
46 1.1 alm long n;
47 1.1 alm char *s;
48 1.1 alm char delimiter;
49 1.1 alm
50 1.1 alm if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
51 1.1 alm sprintf(errmsg, "invalid pattern delimiter");
52 1.1 alm return ERR;
53 1.1 alm } else if ((pat = get_compiled_pattern()) == NULL)
54 1.1 alm return ERR;
55 1.1 alm else if (*ibufp == delimiter)
56 1.1 alm ibufp++;
57 1.1 alm clear_active_list();
58 1.1 alm lp = get_addressed_line_node(first_addr);
59 1.1 alm for (n = first_addr; n <= second_addr; n++, lp = lp->q_forw) {
60 1.1 alm if ((s = get_sbuf_line(lp)) == NULL)
61 1.1 alm return ERR;
62 1.1 alm if (isbinary)
63 1.1 alm NUL_TO_NEWLINE(s, lp->len);
64 1.1 alm if (!regexec(pat, s, 0, NULL, 0) == isgcmd &&
65 1.1 alm set_active_node(lp) < 0)
66 1.1 alm return ERR;
67 1.1 alm }
68 1.1 alm return 0;
69 1.1 alm }
70 1.1 alm
71 1.1 alm
72 1.1 alm /* exec_global: apply command list in the command buffer to the active
73 1.1 alm lines in a range; return command status */
74 1.1 alm long
75 1.1 alm exec_global(interact, gflag)
76 1.1 alm int interact;
77 1.1 alm int gflag;
78 1.1 alm {
79 1.1 alm static char *ocmd = NULL;
80 1.1 alm static int ocmdsz = 0;
81 1.1 alm
82 1.1 alm line_t *lp = NULL;
83 1.1 alm int status;
84 1.1 alm int n;
85 1.1 alm char *cmd = NULL;
86 1.1 alm
87 1.1 alm #ifdef BACKWARDS
88 1.1 alm if (!interact)
89 1.1 alm if (!strcmp(ibufp, "\n"))
90 1.1 alm cmd = "p\n"; /* null cmd-list == `p' */
91 1.1 alm else if ((cmd = get_extended_line(&n, 0)) == NULL)
92 1.1 alm return ERR;
93 1.1 alm #else
94 1.1 alm if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
95 1.1 alm return ERR;
96 1.1 alm #endif
97 1.1 alm clear_undo_stack();
98 1.1 alm while ((lp = next_active_node()) != NULL) {
99 1.1 alm if ((current_addr = get_line_node_addr(lp)) < 0)
100 1.1 alm return ERR;
101 1.1 alm if (interact) {
102 1.1 alm /* print current_addr; get a command in global syntax */
103 1.1 alm if (display_lines(current_addr, current_addr, gflag) < 0)
104 1.1 alm return ERR;
105 1.1 alm while ((n = get_tty_line()) > 0 &&
106 1.1 alm ibuf[n - 1] != '\n')
107 1.1 alm clearerr(stdin);
108 1.1 alm if (n < 0)
109 1.1 alm return ERR;
110 1.1 alm else if (n == 0) {
111 1.1 alm sprintf(errmsg, "unexpected end-of-file");
112 1.1 alm return ERR;
113 1.1 alm } else if (n == 1 && !strcmp(ibuf, "\n"))
114 1.1 alm continue;
115 1.1 alm else if (n == 2 && !strcmp(ibuf, "&\n")) {
116 1.1 alm if (cmd == NULL) {
117 1.1 alm sprintf(errmsg, "no previous command");
118 1.1 alm return ERR;
119 1.1 alm } else cmd = ocmd;
120 1.1 alm } else if ((cmd = get_extended_line(&n, 0)) == NULL)
121 1.1 alm return ERR;
122 1.1 alm else {
123 1.1 alm REALLOC(ocmd, ocmdsz, n + 1, ERR);
124 1.1 alm memcpy(ocmd, cmd, n + 1);
125 1.1 alm cmd = ocmd;
126 1.1 alm }
127 1.1 alm
128 1.1 alm }
129 1.1 alm ibufp = cmd;
130 1.1 alm for (; *ibufp;)
131 1.1 alm if ((status = extract_addr_range()) < 0 ||
132 1.1 alm (status = exec_command()) < 0 ||
133 1.1 alm status > 0 && (status = display_lines(
134 1.1 alm current_addr, current_addr, status)) < 0)
135 1.1 alm return status;
136 1.1 alm }
137 1.1 alm return 0;
138 1.1 alm }
139 1.1 alm
140 1.1 alm
141 1.1 alm line_t **active_list; /* list of lines active in a global command */
142 1.1 alm long active_last; /* index of last active line in active_list */
143 1.1 alm long active_size; /* size of active_list */
144 1.1 alm long active_ptr; /* active_list index (non-decreasing) */
145 1.1 alm long active_ndx; /* active_list index (modulo active_last) */
146 1.1 alm
147 1.1 alm /* set_active_node: add a line node to the global-active list */
148 1.1 alm int
149 1.1 alm set_active_node(lp)
150 1.1 alm line_t *lp;
151 1.1 alm {
152 1.1 alm if (active_last + 1 > active_size) {
153 1.1 alm int ti = active_size;
154 1.1 alm line_t **ts;
155 1.1 alm SPL1();
156 1.1 alm #if defined(sun) || defined(NO_REALLOC_NULL)
157 1.1 alm if (active_list != NULL) {
158 1.1 alm #endif
159 1.1 alm if ((ts = (line_t **) realloc(active_list,
160 1.1 alm (ti += MINBUFSZ) * sizeof(line_t **))) == NULL) {
161 1.1 alm fprintf(stderr, "%s\n", strerror(errno));
162 1.1 alm sprintf(errmsg, "out of memory");
163 1.1 alm SPL0();
164 1.1 alm return ERR;
165 1.1 alm }
166 1.1 alm #if defined(sun) || defined(NO_REALLOC_NULL)
167 1.1 alm } else {
168 1.1 alm if ((ts = (line_t **) malloc((ti += MINBUFSZ) *
169 1.1 alm sizeof(line_t **))) == NULL) {
170 1.1 alm fprintf(stderr, "%s\n", strerror(errno));
171 1.1 alm sprintf(errmsg, "out of memory");
172 1.1 alm SPL0();
173 1.1 alm return ERR;
174 1.1 alm }
175 1.1 alm }
176 1.1 alm #endif
177 1.1 alm active_size = ti;
178 1.1 alm active_list = ts;
179 1.1 alm SPL0();
180 1.1 alm }
181 1.1 alm active_list[active_last++] = lp;
182 1.1 alm return 0;
183 1.1 alm }
184 1.1 alm
185 1.1 alm
186 1.1 alm /* unset_active_nodes: remove a range of lines from the global-active list */
187 1.1 alm void
188 1.1 alm unset_active_nodes(np, mp)
189 1.1 alm line_t *np, *mp;
190 1.1 alm {
191 1.1 alm line_t *lp;
192 1.1 alm long i;
193 1.1 alm
194 1.1 alm for (lp = np; lp != mp; lp = lp->q_forw)
195 1.1 alm for (i = 0; i < active_last; i++)
196 1.1 alm if (active_list[active_ndx] == lp) {
197 1.1 alm active_list[active_ndx] = NULL;
198 1.1 alm active_ndx = INC_MOD(active_ndx, active_last - 1);
199 1.1 alm break;
200 1.1 alm } else active_ndx = INC_MOD(active_ndx, active_last - 1);
201 1.1 alm }
202 1.1 alm
203 1.1 alm
204 1.1 alm /* next_active_node: return the next global-active line node */
205 1.1 alm line_t *
206 1.1 alm next_active_node()
207 1.1 alm {
208 1.1 alm while (active_ptr < active_last && active_list[active_ptr] == NULL)
209 1.1 alm active_ptr++;
210 1.1 alm return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
211 1.1 alm }
212 1.1 alm
213 1.1 alm
214 1.1 alm /* clear_active_list: clear the global-active list */
215 1.1 alm void
216 1.1 alm clear_active_list()
217 1.1 alm {
218 1.1 alm SPL1();
219 1.1 alm active_size = active_last = active_ptr = active_ndx = 0;
220 1.1 alm free(active_list);
221 1.1 alm active_list = NULL;
222 1.1 alm SPL0();
223 1.1 alm }
224