1 #define ED_H_ID "$MirOS: src/bin/ed/ed.h,v 1.9 2013/10/31 20:05:37 tg Exp $"
2 /*	$OpenBSD: ed.h,v 1.11 2007/02/24 13:24:47 millert Exp $	*/
3 /*	$NetBSD: ed.h,v 1.23 1995/03/21 09:04:40 cgd Exp $	*/
4 
5 /* ed.h: type and constant definitions for the ed editor. */
6 /*
7  * Copyright © 2013
8  *	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
9  * Copyright (c) 1993 Andrew Moore
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp
34  */
35 
36 #include <sys/types.h>
37 #ifndef NO_SYS_PARAM_H
38 # include <sys/param.h>		/* for MAXPATHLEN */
39 #endif
40 #include <errno.h>
41 #if defined(sun) || defined(__NetBSD__) || defined(__OpenBSD__) || \
42     defined(__EXT_QNX)
43 # include <limits.h>
44 #endif
45 #include <regex.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #define ERR		(-2)
53 #define EMOD		(-3)
54 #define FATAL		(-4)
55 
56 #ifndef MAXPATHLEN
57 # define MAXPATHLEN 255		/* _POSIX_PATH_MAX */
58 #endif
59 
60 #define MINBUFSZ 512		/* minimum buffer size - must be > 0 */
61 #define SE_MAX 30		/* max subexpressions in a regular expression */
62 #ifdef INT_MAX
63 # define LINECHARS INT_MAX	/* max chars per line */
64 #else
65 # define LINECHARS MAXINT	/* max chars per line */
66 #endif
67 
68 #ifdef NO_FSEEKO
69 #define do_fseek fseek
70 #define do_ftell ftell
71 #define tp_ftell long
72 #else
73 #define do_fseek fseeko
74 #define do_ftell ftello
75 #define tp_ftell off_t
76 #endif
77 
78 /* gflags */
79 #define GLB 001		/* global command */
80 #define GPR 002		/* print after command */
81 #define GLS 004		/* list after command */
82 #define GNP 010		/* enumerate after command */
83 #define GSG 020		/* global substitute */
84 
85 typedef regex_t pattern_t;
86 
87 /* Line node */
88 typedef struct	line {
89 	struct line	*q_forw;
90 	struct line	*q_back;
91 	tp_ftell	adr;		/* address of line in scratch buffer */
92 	int		len;		/* length of line */
93 } line_t;
94 
95 
96 typedef struct undo {
97 
98 /* type of undo nodes */
99 #define UADD	0
100 #define UDEL 	1
101 #define UMOV	2
102 #define VMOV	3
103 
104 	int type;			/* command type */
105 	line_t	*h;			/* head of list */
106 	line_t  *t;			/* tail of list */
107 } undo_t;
108 
109 #ifndef MAX
110 # define MAX(a,b) ((a) > (b) ? (a) : (b))
111 #endif
112 #ifndef MIN
113 # define MIN(a,b) ((a) < (b) ? (a) : (b))
114 #endif
115 
116 #define INC_MOD(l, k)	((l) + 1 > (k) ? 0 : (l) + 1)
117 #define DEC_MOD(l, k)	((l) - 1 < 0 ? (k) : (l) - 1)
118 
119 /* SPL1: disable some interrupts (requires reliable signals) */
120 #define SPL1() mutex++
121 
122 /* SPL0: enable all interrupts; check sigflags (requires reliable signals) */
123 #define SPL0() \
124 if (--mutex == 0) { \
125 	if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \
126 	if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \
127 }
128 
129 /* STRTOI: convert a string to int */
130 #define STRTOI(i, p) { \
131 	long l = strtol(p, &p, 10); \
132 	if (l <= INT_MIN || l >= INT_MAX) { \
133 		seterrmsg("number out of range"); \
134 	    	i = 0; \
135 		return ERR; \
136 	} else \
137 		i = (int)l; \
138 }
139 
140 #if defined(sun) || defined(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 	int ti = (n); \
145 	char *ts; \
146 	SPL1(); \
147 	if ((b) != NULL) { \
148 		if ((ts = (char *) realloc((b), ti += MAX((i), MINBUFSZ))) == NULL) { \
149 			perror(NULL); \
150 			seterrmsg("out of memory"); \
151 			SPL0(); \
152 			return err; \
153 		} \
154 	} else { \
155 		if ((ts = (char *) malloc(ti += MAX((i), MINBUFSZ))) == NULL) { \
156 			perror(NULL); \
157 			seterrmsg("out of memory"); \
158 			SPL0(); \
159 			return err; \
160 		} \
161 	} \
162 	(n) = ti; \
163 	(b) = ts; \
164 	SPL0(); \
165 }
166 #else /* NO_REALLOC_NULL */
167 /* REALLOC: assure at least a minimum size for buffer b */
168 #define REALLOC(b,n,i,err) \
169 if ((i) > (n)) { \
170 	int ti = (n); \
171 	char *ts; \
172 	SPL1(); \
173 	if ((ts = (char *) realloc((b), ti += MAX((i), MINBUFSZ))) == NULL) { \
174 		perror(NULL); \
175 		seterrmsg("out of memory"); \
176 		SPL0(); \
177 		return err; \
178 	} \
179 	(n) = ti; \
180 	(b) = ts; \
181 	SPL0(); \
182 }
183 #endif /* NO_REALLOC_NULL */
184 
185 /* REQUE: link pred before succ */
186 #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred)
187 
188 /* INSQUE: insert elem in circular queue after pred */
189 #define INSQUE(elem, pred) \
190 { \
191 	REQUE((elem), (pred)->q_forw); \
192 	REQUE((pred), elem); \
193 }
194 
195 /* remque: remove_lines elem from circular queue */
196 #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw);
197 
198 /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
199 #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
200 
201 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
202 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
203 
204 #ifdef sun
205 # define strerror(n) sys_errlist[n]
206 #endif
207 
208 /* Local Function Declarations */
209 void add_line_node(line_t *);
210 int append_lines(int);
211 int apply_subst_template(char *, regmatch_t *, int, int);
212 int build_active_list(int);
213 int check_addr_range(int, int);
214 void clear_active_list(void);
215 void clear_undo_stack(void);
216 int close_sbuf(void);
217 int copy_lines(int);
218 int delete_lines(int, int);
219 int display_lines(int, int, int);
220 line_t *dup_line_node(line_t *);
221 int exec_command(void);
222 int exec_global(int, int);
223 int extract_addr_range(void);
224 char *extract_pattern(int);
225 int extract_subst_tail(int *, int *);
226 char *extract_subst_template(void);
227 int flush_des_file(FILE *);
228 line_t *get_addressed_line_node(int);
229 pattern_t *get_compiled_pattern(void);
230 int get_des_char(FILE *);
231 char *get_extended_line(int *, int);
232 char *get_filename(void);
233 int get_keyword(void);
234 int get_line_node_addr(line_t *);
235 int get_matching_node_addr(pattern_t *, int);
236 int get_marked_node_addr(int);
237 char *get_sbuf_line(line_t *);
238 int get_shell_command(void);
239 int get_stream_line(FILE *);
240 int get_tty_line(void);
241 void handle_hup(int) __attribute__((__noreturn__));
242 void handle_int(int) __attribute__((__noreturn__));
243 void handle_winch(int);
244 int has_trailing_escape(char *, char *);
245 void init_buffers(void);
246 void init_des_cipher(void);
247 int is_legal_filename(char *);
248 int join_lines(int, int);
249 int mark_line_node(line_t *, int);
250 int move_lines(int);
251 line_t *next_active_node(void);
252 int next_addr(void);
253 int open_sbuf(void);
254 char *parse_char_class(char *);
255 int pop_undo_stack(void);
256 undo_t *push_undo_stack(int, int, int);
257 int put_des_char(int, FILE *);
258 char *put_sbuf_line(char *);
259 int put_stream_line(FILE *, char *, int);
260 int put_tty_line(char *, int, int, int);
261 void quit(int) __attribute__((__noreturn__));
262 int read_file(char *, int);
263 int read_stream(FILE *, int);
264 int search_and_replace(pattern_t *, int, int);
265 int set_active_node(line_t *);
266 void seterrmsg(const char *);
267 void signal_hup(int);
268 void signal_int(int);
269 char *strip_escapes(const char *);
270 int substitute_matching_text(pattern_t *, line_t *, int, int);
271 char *translit_text(char *, int, int, int);
272 void unmark_line_node(line_t *);
273 void unset_active_nodes(line_t *, line_t *);
274 int write_file(const char *, const char *, int, int);
275 int write_stream(FILE *, int, int);
276 
277 /* global buffers */
278 extern char stdinbuf[];
279 extern char *ibuf;
280 extern char *ibufp;
281 extern int ibufsz;
282 
283 /* global flags */
284 extern int isbinary;
285 extern int isglobal;
286 extern int modified;
287 extern int mutex;
288 extern int sigflags;
289 
290 /* global vars */
291 extern int addr_last;
292 extern int current_addr;
293 extern char errmsg[MAXPATHLEN + 40];
294 extern int first_addr;
295 extern int lineno;
296 extern int second_addr;
297 #ifdef sun
298 extern char *sys_errlist[];
299 #endif
300