1 /* glob.c: This file contains the global command routines for the ed line
4 * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * @(#)glob.c,v 1.1 1994/02/01 00:34:40 alm Exp
29 * $FreeBSD: src/bin/ed/glbl.c,v 1.13 2002/06/30 05:13:53 obrien Exp $
30 * $DragonFly: src/bin/ed/glbl.c,v 1.4 2007/04/06 23:36:54 pavalos Exp $
33 #include <sys/types.h>
35 #include <sys/ioctl.h>
41 /* build_active_list: add line matching a pattern to the global-active list */
43 build_active_list(int isgcmd
)
51 if ((delimiter
= *ibufp
) == ' ' || delimiter
== '\n') {
52 errmsg
= "invalid pattern delimiter";
54 } else if ((pat
= get_compiled_pattern()) == NULL
)
56 else if (*ibufp
== delimiter
)
59 lp
= get_addressed_line_node(first_addr
);
60 for (n
= first_addr
; n
<= second_addr
; n
++, lp
= lp
->q_forw
) {
61 if ((s
= get_sbuf_line(lp
)) == NULL
)
64 NUL_TO_NEWLINE(s
, lp
->len
);
65 if (!regexec(pat
, s
, 0, NULL
, 0) == isgcmd
&&
66 set_active_node(lp
) < 0)
73 /* exec_global: apply command list in the command buffer to the active
74 lines in a range; return command status */
76 exec_global(int interact
, int gflag
)
78 static char *ocmd
= NULL
;
79 static int ocmdsz
= 0;
88 if (!strcmp(ibufp
, "\n"))
89 cmd
= "p\n"; /* null cmd-list == `p' */
90 else if ((cmd
= get_extended_line(&n
, 0)) == NULL
)
93 if (!interact
&& (cmd
= get_extended_line(&n
, 0)) == NULL
)
97 while ((lp
= next_active_node()) != NULL
) {
98 if ((current_addr
= get_line_node_addr(lp
)) < 0)
101 /* print current_addr; get a command in global syntax */
102 if (display_lines(current_addr
, current_addr
, gflag
) < 0)
104 while ((n
= get_tty_line()) > 0 &&
110 errmsg
= "unexpected end-of-file";
112 } else if (n
== 1 && !strcmp(ibuf
, "\n"))
114 else if (n
== 2 && !strcmp(ibuf
, "&\n")) {
116 errmsg
= "no previous command";
119 } else if ((cmd
= get_extended_line(&n
, 0)) == NULL
)
122 REALLOC(ocmd
, ocmdsz
, n
+ 1, ERR
);
123 memcpy(ocmd
, cmd
, n
+ 1);
130 if ((status
= extract_addr_range()) < 0 ||
131 (status
= exec_command()) < 0 ||
132 (status
> 0 && (status
= display_lines(
133 current_addr
, current_addr
, status
)) < 0))
140 line_t
**active_list
; /* list of lines active in a global command */
141 long active_last
; /* index of last active line in active_list */
142 long active_size
; /* size of active_list */
143 long active_ptr
; /* active_list index (non-decreasing) */
144 long active_ndx
; /* active_list index (modulo active_last) */
146 /* set_active_node: add a line node to the global-active list */
148 set_active_node(line_t
*lp
)
150 if (active_last
+ 1 > active_size
) {
151 int ti
= active_size
;
154 #if defined(sun) || defined(NO_REALLOC_NULL)
155 if (active_list
!= NULL
) {
157 if ((ts
= (line_t
**) realloc(active_list
,
158 (ti
+= MINBUFSZ
) * sizeof(line_t
**))) == NULL
) {
159 fprintf(stderr
, "%s\n", strerror(errno
));
160 errmsg
= "out of memory";
164 #if defined(sun) || defined(NO_REALLOC_NULL)
166 if ((ts
= (line_t
**) malloc((ti
+= MINBUFSZ
) *
167 sizeof(line_t
**))) == NULL
) {
168 fprintf(stderr
, "%s\n", strerror(errno
));
169 errmsg
= "out of memory";
179 active_list
[active_last
++] = lp
;
184 /* unset_active_nodes: remove a range of lines from the global-active list */
186 unset_active_nodes(line_t
*np
, line_t
*mp
)
191 for (lp
= np
; lp
!= mp
; lp
= lp
->q_forw
)
192 for (i
= 0; i
< active_last
; i
++)
193 if (active_list
[active_ndx
] == lp
) {
194 active_list
[active_ndx
] = NULL
;
195 active_ndx
= INC_MOD(active_ndx
, active_last
- 1);
197 } else active_ndx
= INC_MOD(active_ndx
, active_last
- 1);
201 /* next_active_node: return the next global-active line node */
203 next_active_node(void)
205 while (active_ptr
< active_last
&& active_list
[active_ptr
] == NULL
)
207 return (active_ptr
< active_last
) ? active_list
[active_ptr
++] : NULL
;
211 /* clear_active_list: clear the global-active list */
213 clear_active_list(void)
216 active_size
= active_last
= active_ptr
= active_ndx
= 0;