1 /* $OpenBSD: ed.h,v 1.23 2024/07/16 05:01:10 deraadt Exp $ */ 2 /* $NetBSD: ed.h,v 1.23 1995/03/21 09:04:40 cgd Exp $ */ 3 4 /* ed.h: type and constant definitions for the ed editor. */ 5 /* 6 * Copyright (c) 1993 Andrew Moore 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 REGENTS 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 REGENTS 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 * @(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp 31 */ 32 33 #include <limits.h> 34 #include <regex.h> 35 #include <signal.h> 36 37 #define ERR (-2) 38 #define EMOD (-3) 39 #define FATAL (-4) 40 41 #define MINBUFSZ 512 /* minimum buffer size - must be > 0 */ 42 #define SE_MAX 30 /* max subexpressions in a regular expression */ 43 #define LINECHARS INT_MAX /* max chars per line */ 44 45 /* gflags */ 46 #define GLB 001 /* global command */ 47 #define GPR 002 /* print after command */ 48 #define GLS 004 /* list after command */ 49 #define GNP 010 /* enumerate after command */ 50 #define GSG 020 /* global substitute */ 51 52 /* Line node */ 53 typedef struct line { 54 struct line *q_forw; 55 struct line *q_back; 56 off_t seek; /* address of line in scratch buffer */ 57 int len; /* length of line */ 58 } line_t; 59 60 61 typedef struct undo { 62 63 /* type of undo nodes */ 64 #define UADD 0 65 #define UDEL 1 66 #define UMOV 2 67 #define VMOV 3 68 69 int type; /* command type */ 70 line_t *h; /* head of list */ 71 line_t *t; /* tail of list */ 72 } undo_t; 73 74 #ifndef max 75 # define max(a,b) ((a) > (b) ? (a) : (b)) 76 #endif 77 #ifndef min 78 # define min(a,b) ((a) < (b) ? (a) : (b)) 79 #endif 80 81 #define INC_MOD(l, k) ((l) + 1 > (k) ? 0 : (l) + 1) 82 #define DEC_MOD(l, k) ((l) - 1 < 0 ? (k) : (l) - 1) 83 84 /* SPL1: disable some interrupts (requires reliable signals) */ 85 #define SPL1() mutex++ 86 87 /* SPL0: enable all interrupts; check signal flags (requires reliable signals) */ 88 #define SPL0() \ 89 do { \ 90 if (--mutex == 0) { \ 91 if (sigint) \ 92 handle_int(SIGINT); \ 93 } \ 94 } while (0) 95 96 /* STRTOI: convert a string to int */ 97 #define STRTOI(i, p) { \ 98 long l = strtol(p, &p, 10); \ 99 if (l <= INT_MIN || l >= INT_MAX) { \ 100 seterrmsg("number out of range"); \ 101 i = 0; \ 102 return ERR; \ 103 } else \ 104 i = (int)l; \ 105 } 106 107 /* REALLOC: assure at least a minimum size for buffer b */ 108 #define REALLOC(b,n,i,err) \ 109 if ((i) > (n)) { \ 110 int ti = (n); \ 111 char *ts; \ 112 SPL1(); \ 113 if ((ts = realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \ 114 perror(NULL); \ 115 seterrmsg("out of memory"); \ 116 SPL0(); \ 117 return err; \ 118 } \ 119 (n) = ti; \ 120 (b) = ts; \ 121 SPL0(); \ 122 } 123 124 /* REQUE: link pred before succ */ 125 #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred) 126 127 /* INSQUE: insert elem in circular queue after pred */ 128 #define INSQUE(elem, pred) \ 129 { \ 130 REQUE((elem), (pred)->q_forw); \ 131 REQUE((pred), elem); \ 132 } 133 134 /* remque: remove_lines elem from circular queue */ 135 #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw); 136 137 /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */ 138 #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n') 139 140 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */ 141 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0') 142 143 /* Local Function Declarations */ 144 void add_line_node(line_t *); 145 int build_active_list(int); 146 void clear_active_list(void); 147 void clear_undo_stack(void); 148 int close_sbuf(void); 149 int delete_lines(int, int); 150 int display_lines(int, int, int); 151 int exec_command(void); 152 int exec_global(int, int); 153 int extract_addr_range(void); 154 int extract_subst_tail(int *, int *); 155 line_t *get_addressed_line_node(int); 156 regex_t *get_compiled_pattern(void); 157 char *get_extended_line(int *, int); 158 int get_line_node_addr(line_t *); 159 char *get_sbuf_line(line_t *); 160 int get_tty_line(void); 161 void handle_hup(void); 162 void handle_int(int); 163 int has_trailing_escape(char *, char *); 164 void init_buffers(void); 165 int open_sbuf(void); 166 int pop_undo_stack(void); 167 undo_t *push_undo_stack(int, int, int); 168 char *put_sbuf_line(char *); 169 int put_tty_line(char *, int, int, int); 170 void quit(int); 171 int read_file(char *, int); 172 int search_and_replace(regex_t *, int, int); 173 void seterrmsg(char *); 174 char *strip_escapes(char *); 175 char *translit_text(char *, int, int, int); 176 void unmark_line_node(line_t *); 177 void unset_active_nodes(line_t *, line_t *); 178 int write_file(char *, char *, int, int); 179 180 /* global buffers */ 181 extern char *ibuf; 182 extern char *ibufp; 183 extern int ibufsz; 184 185 /* global flags */ 186 extern int isbinary; 187 extern int isglobal; 188 extern int modified; 189 190 extern volatile sig_atomic_t mutex; 191 extern volatile sig_atomic_t sighup; 192 extern volatile sig_atomic_t sigint; 193 194 /* global vars */ 195 extern int addr_last; 196 extern int current_addr; 197 extern int first_addr; 198 extern int lineno; 199 extern int second_addr; 200