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 */ 29 30 #include <sys/param.h> 31 #include <errno.h> 32 #include <limits.h> 33 #include <regex.h> 34 #include <signal.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <unistd.h> 39 40 #define ERR (-2) 41 #define EMOD (-3) 42 #define FATAL (-4) 43 44 #define MINBUFSZ 512 /* minimum buffer size - must be > 0 */ 45 #define SE_MAX 30 /* max subexpressions in a regular expression */ 46 #ifdef INT_MAX 47 # define LINECHARS INT_MAX /* max chars per line */ 48 #else 49 # define LINECHARS MAXINT /* max chars per line */ 50 #endif 51 52 /* gflags */ 53 #define GLB 001 /* global command */ 54 #define GPR 002 /* print after command */ 55 #define GLS 004 /* list after command */ 56 #define GNP 010 /* enumerate after command */ 57 #define GSG 020 /* global substitute */ 58 59 typedef regex_t pattern_t; 60 61 /* Line node */ 62 typedef struct line { 63 struct line *q_forw; 64 struct line *q_back; 65 off_t seek; /* address of line in scratch buffer */ 66 int len; /* length of line */ 67 } line_t; 68 69 70 typedef struct undo { 71 72 /* type of undo nodes */ 73 #define UADD 0 74 #define UDEL 1 75 #define UMOV 2 76 #define VMOV 3 77 78 int type; /* command type */ 79 line_t *h; /* head of list */ 80 line_t *t; /* tail of list */ 81 } undo_t; 82 83 #ifndef max 84 # define max(a,b) ((a) > (b) ? (a) : (b)) 85 #endif 86 #ifndef min 87 # define min(a,b) ((a) < (b) ? (a) : (b)) 88 #endif 89 90 #define INC_MOD(l, k) ((l) + 1 > (k) ? 0 : (l) + 1) 91 #define DEC_MOD(l, k) ((l) - 1 < 0 ? (k) : (l) - 1) 92 93 /* SPL1: disable some interrupts (requires reliable signals) */ 94 #define SPL1() mutex++ 95 96 /* SPL0: enable all interrupts; check sigflags (requires reliable signals) */ 97 #define SPL0() \ 98 if (--mutex == 0) { \ 99 if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \ 100 if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \ 101 } 102 103 /* STRTOL: convert a string to long */ 104 #define STRTOL(i, p) { \ 105 if (((i = strtol(p, &p, 10)) == LONG_MIN || i == LONG_MAX) && \ 106 errno == ERANGE) { \ 107 errmsg = "number out of range"; \ 108 i = 0; \ 109 return ERR; \ 110 } \ 111 } 112 113 #if defined(sun) || defined(NO_REALLOC_NULL) 114 /* REALLOC: assure at least a minimum size for buffer b */ 115 #define REALLOC(b,n,i,err) \ 116 if ((i) > (n)) { \ 117 size_t ti = (n); \ 118 char *ts; \ 119 SPL1(); \ 120 if ((b) != NULL) { \ 121 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \ 122 fprintf(stderr, "%s\n", strerror(errno)); \ 123 errmsg = "out of memory"; \ 124 SPL0(); \ 125 return err; \ 126 } \ 127 } else { \ 128 if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \ 129 fprintf(stderr, "%s\n", strerror(errno)); \ 130 errmsg = "out of memory"; \ 131 SPL0(); \ 132 return err; \ 133 } \ 134 } \ 135 (n) = ti; \ 136 (b) = ts; \ 137 SPL0(); \ 138 } 139 #else /* NO_REALLOC_NULL */ 140 /* REALLOC: assure at least a minimum size for buffer b */ 141 #define REALLOC(b,n,i,err) \ 142 if ((i) > (n)) { \ 143 size_t ti = (n); \ 144 char *ts; \ 145 SPL1(); \ 146 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \ 147 fprintf(stderr, "%s\n", strerror(errno)); \ 148 errmsg = "out of memory"; \ 149 SPL0(); \ 150 return err; \ 151 } \ 152 (n) = ti; \ 153 (b) = ts; \ 154 SPL0(); \ 155 } 156 #endif /* NO_REALLOC_NULL */ 157 158 /* REQUE: link pred before succ */ 159 #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred) 160 161 /* INSQUE: insert elem in circular queue after pred */ 162 #define INSQUE(elem, pred) \ 163 { \ 164 REQUE((elem), (pred)->q_forw); \ 165 REQUE((pred), elem); \ 166 } 167 168 /* REMQUE: remove_lines elem from circular queue */ 169 #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw); 170 171 /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */ 172 #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n') 173 174 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */ 175 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0') 176 177 178 /* Local Function Declarations */ 179 void add_line_node(line_t *); 180 int append_lines(long); 181 int apply_subst_template(const char *, regmatch_t *, int, int); 182 int build_active_list(int); 183 int cbc_decode(unsigned char *, FILE *); 184 int cbc_encode(unsigned char *, int, FILE *); 185 int check_addr_range(long, long); 186 void clear_active_list(void); 187 void clear_undo_stack(void); 188 int close_sbuf(void); 189 int copy_lines(long); 190 int delete_lines(long, long); 191 int display_lines(long, long, int); 192 line_t *dup_line_node(line_t *); 193 int exec_command(void); 194 long exec_global(int, int); 195 int extract_addr_range(void); 196 char *extract_pattern(int); 197 int extract_subst_tail(int *, long *); 198 char *extract_subst_template(void); 199 int filter_lines(long, long, char *); 200 line_t *get_addressed_line_node(long); 201 pattern_t *get_compiled_pattern(void); 202 char *get_extended_line(int *, int); 203 char *get_filename(void); 204 int get_keyword(void); 205 long get_line_node_addr(line_t *); 206 long get_matching_node_addr(pattern_t *, int); 207 long get_marked_node_addr(int); 208 char *get_sbuf_line(line_t *); 209 int get_shell_command(void); 210 int get_stream_line(FILE *); 211 int get_tty_line(void); 212 void handle_hup(int); 213 void handle_int(int); 214 void handle_winch(int); 215 int has_trailing_escape(char *, char *); 216 int hex_to_binary(int, int); 217 void init_buffers(void); 218 int is_legal_filename(char *); 219 int join_lines(long, long); 220 int mark_line_node(line_t *, int); 221 int move_lines(long); 222 line_t *next_active_node(void); 223 long next_addr(void); 224 int open_sbuf(void); 225 char *parse_char_class(char *); 226 int pop_undo_stack(void); 227 undo_t *push_undo_stack(int, long, long); 228 const char *put_sbuf_line(const char *); 229 int put_stream_line(FILE *, const char *, int); 230 int put_tty_line(const char *, int, long, int); 231 void quit(int); 232 long read_file(char *, long); 233 long read_stream(FILE *, long); 234 int search_and_replace(pattern_t *, int, int); 235 int set_active_node(line_t *); 236 void signal_hup(int); 237 void signal_int(int); 238 char *strip_escapes(char *); 239 int substitute_matching_text(pattern_t *, line_t *, int, int); 240 char *translit_text(char *, int, int, int); 241 void unmark_line_node(line_t *); 242 void unset_active_nodes(line_t *, line_t *); 243 long write_file(char *, const char *, long, long); 244 long write_stream(FILE *, long, long); 245 246 /* global buffers */ 247 extern char stdinbuf[]; 248 extern char *ibuf; 249 extern char *ibufp; 250 extern int ibufsz; 251 252 /* global flags */ 253 extern int isbinary; 254 extern int isglobal; 255 extern int modified; 256 extern int mutex; 257 extern int sigflags; 258 259 /* global vars */ 260 extern long addr_last; 261 extern long current_addr; 262 extern const char *errmsg; 263 extern long first_addr; 264 extern int lineno; 265 extern long second_addr; 266 extern long u_addr_last; 267 extern long u_current_addr; 268 extern long rows; 269 extern int cols; 270 extern int newline_added; 271 extern int scripted; 272 extern int patlock; 273