1 /* ed.h: type and constant definitions for the ed editor. */
3 * Copyright (c) 1993 Andrew Moore
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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.7 2004/09/26 16:32:47 asmodai Exp $
32 #include <sys/param.h>
46 #define MINBUFSZ 512 /* minimum buffer size - must be > 0 */
47 #define SE_MAX 30 /* max subexpressions in a regular expression */
49 # define LINECHARS INT_MAX /* max chars per line */
51 # define LINECHARS MAXINT /* max chars per line */
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 */
61 typedef regex_t pattern_t;
67 off_t seek; /* address of line in scratch buffer */
68 int len; /* length of line */
74 /* type of undo nodes */
80 int type; /* command type */
81 line_t *h; /* head of list */
82 line_t *t; /* tail of list */
86 # define max(a,b) ((a) > (b) ? (a) : (b))
89 # define min(a,b) ((a) < (b) ? (a) : (b))
92 #define INC_MOD(l, k) ((l) + 1 > (k) ? 0 : (l) + 1)
93 #define DEC_MOD(l, k) ((l) - 1 < 0 ? (k) : (l) - 1)
95 /* SPL1: disable some interrupts (requires reliable signals) */
96 #define SPL1() mutex++
98 /* SPL0: enable all interrupts; check sigflags (requires reliable signals) */
100 if (--mutex == 0) { \
101 if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \
102 if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \
105 /* STRTOL: convert a string to long */
106 #define STRTOL(i, p) { \
107 if (((i = strtol(p, &p, 10)) == LONG_MIN || i == LONG_MAX) && \
109 sprintf(errmsg, "number out of range"); \
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) \
123 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
124 fprintf(stderr, "%s\n", strerror(errno)); \
125 sprintf(errmsg, "out of memory"); \
130 if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
131 fprintf(stderr, "%s\n", strerror(errno)); \
132 sprintf(errmsg, "out of memory"); \
141 #else /* NO_REALLOC_NULL */
142 /* REALLOC: assure at least a minimum size for buffer b */
143 #define REALLOC(b,n,i,err) \
148 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
149 fprintf(stderr, "%s\n", strerror(errno)); \
150 sprintf(errmsg, "out of memory"); \
158 #endif /* NO_REALLOC_NULL */
160 /* REQUE: link pred before succ */
161 #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred)
163 /* INSQUE: insert elem in circular queue after pred */
164 #define INSQUE(elem, pred) \
166 REQUE((elem), (pred)->q_forw); \
167 REQUE((pred), elem); \
170 /* REMQUE: remove_lines elem from circular queue */
171 #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw);
173 /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
174 #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
176 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
177 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
180 # define strerror(n) sys_errlist[n]
183 /* Local Function Declarations */
184 void add_line_node (line_t *);
185 int append_lines (long);
186 int apply_subst_template (char *, regmatch_t *, int, int);
187 int build_active_list (int);
188 int cbc_decode (char *, FILE *);
189 int cbc_encode (char *, int, FILE *);
190 int check_addr_range (long, long);
191 void clear_active_list (void);
192 void clear_undo_stack (void);
193 int close_sbuf (void);
194 int copy_lines (long);
195 int delete_lines (long, long);
196 void des_error (const char *);
197 int display_lines (long, long, int);
198 line_t *dup_line_node (line_t *);
199 int exec_command (void);
200 long exec_global (int, int);
201 void expand_des_key (char *, char *);
202 int extract_addr_range (void);
203 char *extract_pattern (int);
204 int extract_subst_tail (int *, long *);
205 char *extract_subst_template (void);
206 int filter_lines (long, long, char *);
207 int flush_des_file (FILE *);
208 line_t *get_addressed_line_node (long);
209 pattern_t *get_compiled_pattern (void);
210 int get_des_char (FILE *);
211 char *get_extended_line (int *, int);
212 char *get_filename (void);
213 int get_keyword (void);
214 long get_line_node_addr (line_t *);
215 long get_matching_node_addr (pattern_t *, int);
216 long get_marked_node_addr (int);
217 char *get_sbuf_line (line_t *);
218 int get_shell_command (void);
219 int get_stream_line (FILE *);
220 int get_tty_line (void);
221 void handle_hup (int);
222 void handle_int (int);
223 void handle_winch (int);
224 int has_trailing_escape (char *, char *);
225 int hex_to_binary (int, int);
226 void init_buffers (void);
227 void init_des_cipher (void);
228 int is_legal_filename (char *);
229 int join_lines (long, long);
230 int mark_line_node (line_t *, int);
231 int move_lines (long);
232 line_t *next_active_node (void);
233 long next_addr (void);
234 int open_sbuf (void);
235 char *parse_char_class (char *);
236 int pop_undo_stack (void);
237 undo_t *push_undo_stack (int, long, long);
238 int put_des_char (int, FILE *);
239 char *put_sbuf_line (char *);
240 int put_stream_line (FILE *, char *, int);
241 int put_tty_line (char *, int, long, int);
243 long read_file (char *, long);
244 long read_stream (FILE *, long);
245 int search_and_replace (pattern_t *, int, int);
246 int set_active_node (line_t *);
247 void set_des_key (char *);
248 void signal_hup (int);
249 void signal_int (int);
250 char *strip_escapes (const char *);
251 int substitute_matching_text (pattern_t *, line_t *, int, int);
252 char *translit_text (char *, int, int, int);
253 void unmark_line_node (line_t *);
254 void unset_active_nodes (line_t *, line_t *);
255 long write_file (const char *, const char *, long, long);
256 long write_stream (FILE *, long, long);
259 extern char stdinbuf[];
272 extern long addr_last;
273 extern long u_addr_last;
274 extern long current_addr;
275 extern long u_current_addr;
276 extern char errmsg[];
277 extern long first_addr;
279 extern long second_addr;
281 extern char *sys_errlist[];