Merge from vendor branch FILE:
[dragonfly.git] / bin / ed / glbl.c
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.
6  *
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.
15  *
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.
27  *
28  * @(#)glob.c,v 1.1 1994/02/01 00:34:40 alm Exp
29  * $FreeBSD: src/bin/ed/glbl.c,v 1.9.2.1 2001/01/23 14:38:31 asmodai Exp $
30  * $DragonFly: src/bin/ed/glbl.c,v 1.3 2003/09/28 14:39:14 hmp Exp $
31  */
32
33 #include <sys/types.h>
34
35 #include <sys/ioctl.h>
36 #include <sys/wait.h>
37
38 #include "ed.h"
39
40
41 /* build_active_list:  add line matching a pattern to the global-active list */
42 int
43 build_active_list(int isgcmd)
44 {
45         pattern_t *pat;
46         line_t *lp;
47         long n;
48         char *s;
49         char delimiter;
50
51         if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
52                 sprintf(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;
68         }
69         return 0;
70 }
71
72
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)
77 {
78         static char *ocmd = NULL;
79         static int ocmdsz = 0;
80
81         line_t *lp = NULL;
82         int status;
83         int n;
84         char *cmd = NULL;
85
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                                 sprintf(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                                         sprintf(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;
125                         }
126
127                 }
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;
135         }
136         return 0;
137 }
138
139
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) */
145
146 /* set_active_node: add a line node to the global-active list */
147 int
148 set_active_node(line_t *lp)
149 {
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                                 sprintf(errmsg, "out of memory");
161                                 SPL0();
162                                 return ERR;
163                         }
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                                 sprintf(errmsg, "out of memory");
170                                 SPL0();
171                                 return ERR;
172                         }
173                 }
174 #endif
175                 active_size = ti;
176                 active_list = ts;
177                 SPL0();
178         }
179         active_list[active_last++] = lp;
180         return 0;
181 }
182
183
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)
187 {
188         line_t *lp;
189         long i;
190
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);
198 }
199
200
201 /* next_active_node: return the next global-active line node */
202 line_t *
203 next_active_node(void)
204 {
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;
208 }
209
210
211 /* clear_active_list: clear the global-active list */
212 void
213 clear_active_list(void)
214 {
215         SPL1();
216         active_size = active_last = active_ptr = active_ndx = 0;
217         free(active_list);
218         active_list = NULL;
219         SPL0();
220 }