1 /* ed.h: type and constant definitions for the ed editor. */ 2 /* 3 * Copyright (c) 1993 Andrew Moore 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * @(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp 28 * $FreeBSD: src/bin/ed/ed.h,v 1.13.2.1 2001/08/01 02:36:03 obrien Exp $ 29 * $DragonFly: src/bin/ed/ed.h,v 1.2 2003/06/17 04:22:49 dillon Exp $ 30 */ 31 32 #include <sys/param.h> 33 #include <errno.h> 34 #include <limits.h> 35 #include <regex.h> 36 #include <signal.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 42 #define ERR (-2) 43 #define EMOD (-3) 44 #define FATAL (-4) 45 46 #define MINBUFSZ 512 /* minimum buffer size - must be > 0 */ 47 #define SE_MAX 30 /* max subexpressions in a regular expression */ 48 #ifdef INT_MAX 49 # define LINECHARS INT_MAX /* max chars per line */ 50 #else 51 # define LINECHARS MAXINT /* max chars per line */ 52 #endif 53 54 /* gflags */ 55 #define GLB 001 /* global command */ 56 #define GPR 002 /* print after command */ 57 #define GLS 004 /* list after command */ 58 #define GNP 010 /* enumerate after command */ 59 #define GSG 020 /* global substitute */ 60 61 typedef regex_t pattern_t; 62 63 /* Line node */ 64 typedef struct line { 65 struct line *q_forw; 66 struct line *q_back; 67 off_t seek; /* address of line in scratch buffer */ 68 int len; /* length of line */ 69 } line_t; 70 71 72 typedef struct undo { 73 74 /* type of undo nodes */ 75 #define UADD 0 76 #define UDEL 1 77 #define UMOV 2 78 #define VMOV 3 79 80 int type; /* command type */ 81 line_t *h; /* head of list */ 82 line_t *t; /* tail of list */ 83 } undo_t; 84 85 #ifndef max 86 # define max(a,b) ((a) > (b) ? (a) : (b)) 87 #endif 88 #ifndef min 89 # define min(a,b) ((a) < (b) ? (a) : (b)) 90 #endif 91 92 #define INC_MOD(l, k) ((l) + 1 > (k) ? 0 : (l) + 1) 93 #define DEC_MOD(l, k) ((l) - 1 < 0 ? (k) : (l) - 1) 94 95 /* SPL1: disable some interrupts (requires reliable signals) */ 96 #define SPL1() mutex++ 97 98 /* SPL0: enable all interrupts; check sigflags (requires reliable signals) */ 99 #define SPL0() \ 100 if (--mutex == 0) { \ 101 if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \ 102 if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \ 103 } 104 105 /* STRTOL: convert a string to long */ 106 #define STRTOL(i, p) { \ 107 if (((i = strtol(p, &p, 10)) == LONG_MIN || i == LONG_MAX) && \ 108 errno == ERANGE) { \ 109 sprintf(errmsg, "number out of range"); \ 110 i = 0; \ 111 return ERR; \ 112 } \ 113 } 114 115 #if defined(sun) || defined(NO_REALLOC_NULL) 116 /* REALLOC: assure at least a minimum size for buffer b */ 117 #define REALLOC(b,n,i,err) \ 118 if ((i) > (n)) { \ 119 int ti = (n); \ 120 char *ts; \ 121 SPL1(); \ 122 if ((b) != NULL) { \ 123 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \ 124 fprintf(stderr, "%s\n", strerror(errno)); \ 125 sprintf(errmsg, "out of memory"); \ 126 SPL0(); \ 127 return err; \ 128 } \ 129 } else { \ 130 if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \ 131 fprintf(stderr, "%s\n", strerror(errno)); \ 132 sprintf(errmsg, "out of memory"); \ 133 SPL0(); \ 134 return err; \ 135 } \ 136 } \ 137 (n) = ti; \ 138 (b) = ts; \ 139 SPL0(); \ 140 } 141 #else /* NO_REALLOC_NULL */ 142 /* REALLOC: assure at least a minimum size for buffer b */ 143 #define REALLOC(b,n,i,err) \ 144 if ((i) > (n)) { \ 145 int ti = (n); \ 146 char *ts; \ 147 SPL1(); \ 148 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \ 149 fprintf(stderr, "%s\n", strerror(errno)); \ 150 sprintf(errmsg, "out of memory"); \ 151 SPL0(); \ 152 return err; \ 153 } \ 154 (n) = ti; \ 155 (b) = ts; \ 156 SPL0(); \ 157 } 158 #endif /* NO_REALLOC_NULL */ 159 160 /* REQUE: link pred before succ */ 161 #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred) 162 163 /* INSQUE: insert elem in circular queue after pred */ 164 #define INSQUE(elem, pred) \ 165 { \ 166 REQUE((elem), (pred)->q_forw); \ 167 REQUE((pred), elem); \ 168 } 169 170 /* REMQUE: remove_lines elem from circular queue */ 171 #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw); 172 173 /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */ 174 #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n') 175 176 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */ 177 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0') 178 179 #ifdef sun 180 # define strerror(n) sys_errlist[n] 181 #endif 182 183 #ifndef __P 184 # ifndef __STDC__ 185 # define __P(proto) () 186 # else 187 # define __P(proto) proto 188 # endif 189 #endif 190 191 /* Local Function Declarations */ 192 void add_line_node __P((line_t *)); 193 int append_lines __P((long)); 194 int apply_subst_template __P((char *, regmatch_t *, int, int)); 195 int build_active_list __P((int)); 196 int cbc_decode __P((char *, FILE *)); 197 int cbc_encode __P((char *, int, FILE *)); 198 int check_addr_range __P((long, long)); 199 void clear_active_list __P((void)); 200 void clear_undo_stack __P((void)); 201 int close_sbuf __P((void)); 202 int copy_lines __P((long)); 203 int delete_lines __P((long, long)); 204 void des_error __P((char *)); 205 int display_lines __P((long, long, int)); 206 line_t *dup_line_node __P((line_t *)); 207 int exec_command __P((void)); 208 long exec_global __P((int, int)); 209 void expand_des_key __P((char *, char *)); 210 int extract_addr_range __P((void)); 211 char *extract_pattern __P((int)); 212 int extract_subst_tail __P((int *, long *)); 213 char *extract_subst_template __P((void)); 214 int filter_lines __P((long, long, char *)); 215 int flush_des_file __P((FILE *)); 216 line_t *get_addressed_line_node __P((long)); 217 pattern_t *get_compiled_pattern __P((void)); 218 int get_des_char __P((FILE *)); 219 char *get_extended_line __P((int *, int)); 220 char *get_filename __P((void)); 221 int get_keyword __P((void)); 222 long get_line_node_addr __P((line_t *)); 223 long get_matching_node_addr __P((pattern_t *, int)); 224 long get_marked_node_addr __P((int)); 225 char *get_sbuf_line __P((line_t *)); 226 int get_shell_command __P((void)); 227 int get_stream_line __P((FILE *)); 228 int get_tty_line __P((void)); 229 void handle_hup __P((int)); 230 void handle_int __P((int)); 231 void handle_winch __P((int)); 232 int has_trailing_escape __P((char *, char *)); 233 int hex_to_binary __P((int, int)); 234 void init_buffers __P((void)); 235 void init_des_cipher __P((void)); 236 int is_legal_filename __P((char *)); 237 int join_lines __P((long, long)); 238 int mark_line_node __P((line_t *, int)); 239 int move_lines __P((long)); 240 line_t *next_active_node __P(()); 241 long next_addr __P((void)); 242 int open_sbuf __P((void)); 243 char *parse_char_class __P((char *)); 244 int pop_undo_stack __P((void)); 245 undo_t *push_undo_stack __P((int, long, long)); 246 int put_des_char __P((int, FILE *)); 247 char *put_sbuf_line __P((char *)); 248 int put_stream_line __P((FILE *, char *, int)); 249 int put_tty_line __P((char *, int, long, int)); 250 void quit __P((int)); 251 long read_file __P((char *, long)); 252 long read_stream __P((FILE *, long)); 253 int search_and_replace __P((pattern_t *, int, int)); 254 int set_active_node __P((line_t *)); 255 void set_des_key __P((char *)); 256 void signal_hup __P((int)); 257 void signal_int __P((int)); 258 char *strip_escapes __P((char *)); 259 int substitute_matching_text __P((pattern_t *, line_t *, int, int)); 260 char *translit_text __P((char *, int, int, int)); 261 void unmark_line_node __P((line_t *)); 262 void unset_active_nodes __P((line_t *, line_t *)); 263 long write_file __P((char *, char *, long, long)); 264 long write_stream __P((FILE *, long, long)); 265 266 /* global buffers */ 267 extern char stdinbuf[]; 268 extern char *ibuf; 269 extern char *ibufp; 270 extern int ibufsz; 271 272 /* global flags */ 273 extern int isbinary; 274 extern int isglobal; 275 extern int modified; 276 extern int mutex; 277 extern int sigflags; 278 279 /* global vars */ 280 extern long addr_last; 281 extern long current_addr; 282 extern char errmsg[]; 283 extern long first_addr; 284 extern int lineno; 285 extern long second_addr; 286 #ifdef sun 287 extern char *sys_errlist[]; 288 #endif 289