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