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