]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/ed/glbl.c
Remove $FreeBSD$: one-line .c pattern
[FreeBSD/FreeBSD.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
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/wait.h>
33
34 #include "ed.h"
35
36
37 /* build_active_list:  add line matching a pattern to the global-active list */
38 int
39 build_active_list(int isgcmd)
40 {
41         pattern_t *pat;
42         line_t *lp;
43         long n;
44         char *s;
45         char delimiter;
46
47         if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
48                 errmsg = "invalid pattern delimiter";
49                 return ERR;
50         } else if ((pat = get_compiled_pattern()) == NULL)
51                 return ERR;
52         else if (*ibufp == delimiter)
53                 ibufp++;
54         clear_active_list();
55         lp = get_addressed_line_node(first_addr);
56         for (n = first_addr; n <= second_addr; n++, lp = lp->q_forw) {
57                 if ((s = get_sbuf_line(lp)) == NULL)
58                         return ERR;
59                 if (isbinary)
60                         NUL_TO_NEWLINE(s, lp->len);
61                 if (!(regexec(pat, s, 0, NULL, 0) == isgcmd) &&
62                     set_active_node(lp) < 0)
63                         return ERR;
64         }
65         return 0;
66 }
67
68
69 /* exec_global: apply command list in the command buffer to the active
70    lines in a range; return command status */
71 long
72 exec_global(int interact, int gflag)
73 {
74         static char *ocmd = NULL;
75         static int ocmdsz = 0;
76
77         line_t *lp = NULL;
78         int status;
79         int n;
80         char *cmd = NULL;
81
82 #ifdef BACKWARDS
83         if (!interact)
84                 if (!strcmp(ibufp, "\n"))
85                         cmd = "p\n";            /* null cmd-list == `p' */
86                 else if ((cmd = get_extended_line(&n, 0)) == NULL)
87                         return ERR;
88 #else
89         if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
90                 return ERR;
91 #endif
92         clear_undo_stack();
93         while ((lp = next_active_node()) != NULL) {
94                 if ((current_addr = get_line_node_addr(lp)) < 0)
95                         return ERR;
96                 if (interact) {
97                         /* print current_addr; get a command in global syntax */
98                         if (display_lines(current_addr, current_addr, gflag) < 0)
99                                 return ERR;
100                         while ((n = get_tty_line()) > 0 &&
101                             ibuf[n - 1] != '\n')
102                                 clearerr(stdin);
103                         if (n < 0)
104                                 return ERR;
105                         else if (n == 0) {
106                                 errmsg = "unexpected end-of-file";
107                                 return ERR;
108                         } else if (n == 1 && !strcmp(ibuf, "\n"))
109                                 continue;
110                         else if (n == 2 && !strcmp(ibuf, "&\n")) {
111                                 if (cmd == NULL) {
112                                         errmsg = "no previous command";
113                                         return ERR;
114                                 } else cmd = ocmd;
115                         } else if ((cmd = get_extended_line(&n, 0)) == NULL)
116                                 return ERR;
117                         else {
118                                 REALLOC(ocmd, ocmdsz, n + 1, ERR);
119                                 memcpy(ocmd, cmd, n + 1);
120                                 cmd = ocmd;
121                         }
122
123                 }
124                 ibufp = cmd;
125                 for (; *ibufp;)
126                         if ((status = extract_addr_range()) < 0 ||
127                             (status = exec_command()) < 0 ||
128                             (status > 0 && (status = display_lines(
129                             current_addr, current_addr, status)) < 0))
130                                 return status;
131         }
132         return 0;
133 }
134
135
136 static line_t **active_list;    /* list of lines active in a global command */
137 static long active_last;        /* index of last active line in active_list */
138 static long active_size;        /* size of active_list */
139 static long active_ptr;         /* active_list index (non-decreasing) */
140 static long active_ndx;         /* active_list index (modulo active_last) */
141
142 /* set_active_node: add a line node to the global-active list */
143 int
144 set_active_node(line_t *lp)
145 {
146         if (active_last + 1 > active_size) {
147                 size_t ti = active_size;
148                 line_t **ts;
149                 SPL1();
150 #if defined(sun) || defined(NO_REALLOC_NULL)
151                 if (active_list != NULL) {
152 #endif
153                         if ((ts = (line_t **) realloc(active_list,
154                             (ti += MINBUFSZ) * sizeof(line_t *))) == NULL) {
155                                 fprintf(stderr, "%s\n", strerror(errno));
156                                 errmsg = "out of memory";
157                                 SPL0();
158                                 return ERR;
159                         }
160 #if defined(sun) || defined(NO_REALLOC_NULL)
161                 } else {
162                         if ((ts = (line_t **) malloc((ti += MINBUFSZ) *
163                             sizeof(line_t **))) == NULL) {
164                                 fprintf(stderr, "%s\n", strerror(errno));
165                                 errmsg = "out of memory";
166                                 SPL0();
167                                 return ERR;
168                         }
169                 }
170 #endif
171                 active_size = ti;
172                 active_list = ts;
173                 SPL0();
174         }
175         active_list[active_last++] = lp;
176         return 0;
177 }
178
179
180 /* unset_active_nodes: remove a range of lines from the global-active list */
181 void
182 unset_active_nodes(line_t *np, line_t *mp)
183 {
184         line_t *lp;
185         long i;
186
187         for (lp = np; lp != mp; lp = lp->q_forw)
188                 for (i = 0; i < active_last; i++)
189                         if (active_list[active_ndx] == lp) {
190                                 active_list[active_ndx] = NULL;
191                                 active_ndx = INC_MOD(active_ndx, active_last - 1);
192                                 break;
193                         } else  active_ndx = INC_MOD(active_ndx, active_last - 1);
194 }
195
196
197 /* next_active_node: return the next global-active line node */
198 line_t *
199 next_active_node(void)
200 {
201         while (active_ptr < active_last && active_list[active_ptr] == NULL)
202                 active_ptr++;
203         return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
204 }
205
206
207 /* clear_active_list: clear the global-active list */
208 void
209 clear_active_list(void)
210 {
211         SPL1();
212         active_size = active_last = active_ptr = active_ndx = 0;
213         free(active_list);
214         active_list = NULL;
215         SPL0();
216 }