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