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