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