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