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