Makefile.inc1: don't build aicasm
[dragonfly.git] / bin / ed / glbl.c
blob1219b09c1d458788fd934aff01f9437279e8b3b0
1 /* glob.c: This file contains the global command routines for the ed line
2 editor */
3 /*-
4 * Copyright (c) 1993 Andrew Moore, Talke Studio.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
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>
36 #include <sys/wait.h>
38 #include "ed.h"
41 /* build_active_list: add line matching a pattern to the global-active list */
42 int
43 build_active_list(int isgcmd)
45 pattern_t *pat;
46 line_t *lp;
47 long n;
48 char *s;
49 char delimiter;
51 if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
52 errmsg = "invalid pattern delimiter";
53 return ERR;
54 } else if ((pat = get_compiled_pattern()) == NULL)
55 return ERR;
56 else if (*ibufp == delimiter)
57 ibufp++;
58 clear_active_list();
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)
62 return ERR;
63 if (isbinary)
64 NUL_TO_NEWLINE(s, lp->len);
65 if (!regexec(pat, s, 0, NULL, 0) == isgcmd &&
66 set_active_node(lp) < 0)
67 return ERR;
69 return 0;
73 /* exec_global: apply command list in the command buffer to the active
74 lines in a range; return command status */
75 long
76 exec_global(int interact, int gflag)
78 static char *ocmd = NULL;
79 static int ocmdsz = 0;
81 line_t *lp = NULL;
82 int status;
83 int n;
84 char *cmd = NULL;
86 #ifdef BACKWARDS
87 if (!interact)
88 if (!strcmp(ibufp, "\n"))
89 cmd = "p\n"; /* null cmd-list == `p' */
90 else if ((cmd = get_extended_line(&n, 0)) == NULL)
91 return ERR;
92 #else
93 if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
94 return ERR;
95 #endif
96 clear_undo_stack();
97 while ((lp = next_active_node()) != NULL) {
98 if ((current_addr = get_line_node_addr(lp)) < 0)
99 return ERR;
100 if (interact) {
101 /* print current_addr; get a command in global syntax */
102 if (display_lines(current_addr, current_addr, gflag) < 0)
103 return ERR;
104 while ((n = get_tty_line()) > 0 &&
105 ibuf[n - 1] != '\n')
106 clearerr(stdin);
107 if (n < 0)
108 return ERR;
109 else if (n == 0) {
110 errmsg = "unexpected end-of-file";
111 return ERR;
112 } else if (n == 1 && !strcmp(ibuf, "\n"))
113 continue;
114 else if (n == 2 && !strcmp(ibuf, "&\n")) {
115 if (cmd == NULL) {
116 errmsg = "no previous command";
117 return ERR;
118 } else cmd = ocmd;
119 } else if ((cmd = get_extended_line(&n, 0)) == NULL)
120 return ERR;
121 else {
122 REALLOC(ocmd, ocmdsz, n + 1, ERR);
123 memcpy(ocmd, cmd, n + 1);
124 cmd = ocmd;
128 ibufp = cmd;
129 for (; *ibufp;)
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))
134 return status;
136 return 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;
152 line_t **ts;
153 SPL1();
154 #if defined(sun) || defined(NO_REALLOC_NULL)
155 if (active_list != NULL) {
156 #endif
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";
161 SPL0();
162 return ERR;
164 #if defined(sun) || defined(NO_REALLOC_NULL)
165 } else {
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";
170 SPL0();
171 return ERR;
174 #endif
175 active_size = ti;
176 active_list = ts;
177 SPL0();
179 active_list[active_last++] = lp;
180 return 0;
184 /* unset_active_nodes: remove a range of lines from the global-active list */
185 void
186 unset_active_nodes(line_t *np, line_t *mp)
188 line_t *lp;
189 long i;
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);
196 break;
197 } else active_ndx = INC_MOD(active_ndx, active_last - 1);
201 /* next_active_node: return the next global-active line node */
202 line_t *
203 next_active_node(void)
205 while (active_ptr < active_last && active_list[active_ptr] == NULL)
206 active_ptr++;
207 return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
211 /* clear_active_list: clear the global-active list */
212 void
213 clear_active_list(void)
215 SPL1();
216 active_size = active_last = active_ptr = active_ndx = 0;
217 free(active_list);
218 active_list = NULL;
219 SPL0();