ed.h revision 1.17 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 5.5 (Talke Studio) 3/28/93
28 */
29
30 #include <unistd.h>
31 #include <errno.h>
32 #if defined(BSD) && BSD >= 199103 || defined(__386BSD__)
33 # include <sys/param.h> /* for MAXPATHLEN */
34 #endif
35 #include <regex.h>
36 #include <signal.h>
37 #ifdef sun
38 # include <limits.h>
39 #endif
40
41 #define ERR (-2)
42 #define EMOD (-3)
43 #define FATAL (-4)
44
45 #ifndef MAXPATHLEN
46 # define MAXPATHLEN 255 /* _POSIX_PATH_MAX */
47 #endif
48
49 #define MAXFNAME MAXPATHLEN /* max file name size */
50 #define MINBUFSZ 512 /* minimum buffer size - must be > 0 */
51 #define SE_MAX 30 /* max subexpressions in a regular expression */
52 #ifdef INT_MAX
53 # define LINECHARS INT_MAX /* max chars per line */
54 #else
55 # define LINECHARS MAXINT /* max chars per line */
56 #endif
57
58 typedef regex_t pattern_t;
59
60 /* Line node */
61 typedef struct line {
62 struct line *next;
63 struct line *prev;
64 off_t seek; /* address of line in scratch buffer */
65 int len; /* length of line */
66 } line_t;
67
68
69 typedef struct undo {
70
71 /* type of undo nodes */
72 #define UADD 0
73 #define UDEL 1
74 #define UMOV 2
75 #define VMOV 3
76
77 int type; /* command type */
78 line_t *h; /* head of list */
79 line_t *t; /* tail of list */
80 } undo_t;
81
82 #ifndef max
83 # define max(a,b) ((a) > (b) ? (a) : (b))
84 #endif
85 #ifndef min
86 # define min(a,b) ((a) < (b) ? (a) : (b))
87 #endif
88
89 #define INC_MOD(l, k) ((l)+1 > (k) ? 0 : (l)+1)
90 #define DEC_MOD(l, k) ((l)-1 < 0 ? (k) : (l)-1)
91
92 #define SKIPBLANKS() while (isspace(*ibufp) && *ibufp != '\n') ibufp++
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)) handle_hup(SIGHUP); \
101 if (sigflags & (1 << SIGINT)) handle_int(SIGINT); \
102 }
103
104 #if defined(sun) || defined(NO_REALLOC_NULL)
105 /* CKBUF: assure at least a minimum size for buffer b */
106 #define CKBUF(b,n,i,err) \
107 if ((i) > (n)) { \
108 int ti = (n); \
109 char *ts; \
110 SPL1(); \
111 if ((b) != NULL) { \
112 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
113 fprintf(stderr, "%s\n", strerror(errno)); \
114 sprintf(errmsg, "out of memory"); \
115 SPL0(); \
116 return err; \
117 } \
118 } else { \
119 if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
120 fprintf(stderr, "%s\n", strerror(errno)); \
121 sprintf(errmsg, "out of memory"); \
122 SPL0(); \
123 return err; \
124 } \
125 } \
126 (n) = ti; \
127 (b) = ts; \
128 SPL0(); \
129 }
130 #else /* NO_REALLOC_NULL */
131 /* CKBUF: assure at least a minimum size for buffer b */
132 #define CKBUF(b,n,i,err) \
133 if ((i) > (n)) { \
134 int ti = (n); \
135 char *ts; \
136 SPL1(); \
137 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
138 fprintf(stderr, "%s\n", strerror(errno)); \
139 sprintf(errmsg, "out of memory"); \
140 SPL0(); \
141 return err; \
142 } \
143 (n) = ti; \
144 (b) = ts; \
145 SPL0(); \
146 }
147 #endif /* NO_REALLOC_NULL */
148
149 /* requeue: link pred before succ */
150 #define requeue(pred, succ) (pred)->next = (succ), (succ)->prev = (pred)
151
152 /* insqueue: insert elem in circular queue after pred */
153 #define insqueue(elem, pred) \
154 { \
155 requeue((elem), (pred)->next); \
156 requeue((pred), elem); \
157 }
158
159 /* remqueue: remove_lines elem from circular queue */
160 #define remqueue(elem) requeue((elem)->prev, (elem)->next);
161
162 /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
163 #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
164
165 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
166 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
167
168 #ifndef strerror
169 # define strerror(n) sys_errlist[n]
170 #endif
171
172 #ifndef __P
173 # ifndef __STDC__
174 # define __P(proto) ()
175 # else
176 # define __P(proto) proto
177 # endif
178 #endif
179
180 /* Local Function Declarations */
181 void add_line_node __P((line_t *));
182 int append_lines __P((long));
183 int apply_subst_template __P((char *, regmatch_t *, int, int));
184 int build_active_list __P((int));
185 int cbc_decode __P((char *, FILE *));
186 int cbc_encode __P((char *, int, FILE *));
187 int check_addr_range __P((long, long));
188 void clear_active_list __P((void));
189 void clear_undo_stack __P((void));
190 int close_sbuf __P((void));
191 int copy_lines __P((long));
192 int delete_lines __P((long, long));
193 void des_error __P((char *));
194 int display_lines __P((long, long, int));
195 line_t *dup_line_node __P((line_t *));
196 int exec_command __P((void));
197 long exec_global __P((int, int));
198 void expand_des_key __P((char *, char *));
199 int extract_addr_range __P((void));
200 char *extract_pattern __P((int));
201 int extract_subst_tail __P((void));
202 char *extract_subst_template __P((void));
203 int flush_des_file __P((FILE *));
204 line_t *get_addressed_line_node __P((long));
205 int get_des_char __P((FILE *));
206 pattern_t *get_compiled_pattern __P((void));
207 char *get_extended_line __P((int *, int));
208 int get_file_line __P((FILE *));
209 char *get_filename __P((void));
210 int get_input_line __P((void));
211 int get_keyword __P((void));
212 long get_line_node_addr __P((line_t *));
213 long get_matching_node_addr __P((pattern_t *, int));
214 long get_marked_node_addr __P((int));
215 char *get_sbuf_line __P((line_t *));
216 int get_shell_command __P((void));
217 void handle_hup __P((int));
218 void handle_int __P((int));
219 void handle_winch __P((int));
220 int has_trailing_escape __P((char *, char *));
221 int hex_to_binary __P((int, int));
222 void init_buffers __P((void));
223 void init_des_cipher __P((void));
224 int is_legal_filename __P((char *));
225 int join_lines __P((long, long));
226 int mark_line_node __P((line_t *, int));
227 int move_lines __P((long));
228 line_t *next_active_node __P(());
229 long next_addr __P((void));
230 int open_sbuf __P((void));
231 void output_line __P((char *, int, long, int));
232 char *parse_char_class __P((char *));
233 int pop_undo_stack __P((void));
234 undo_t *push_undo_stack __P((int, long, long));
235 int put_des_char __P((int, FILE *));
236 char *put_sbuf_line __P((char *));
237 void quit __P((int));
238 long read_file __P((long, char *));
239 int search_and_replace __P((pattern_t *, int));
240 int set_active_node __P((line_t *));
241 void set_des_key __P((char *));
242 void signal_hup __P((int));
243 void signal_int __P((int));
244 char *strip_escapes __P((char *));
245 int substitute_matching_text __P((pattern_t *, line_t *, int));
246 char *translit_text __P((char *, int, int, int));
247 void unmark_line_node __P((line_t *));
248 void unset_active_nodes __P((line_t *, line_t *));
249 long write_file __P((long, long, char *, char *));
250
251 extern char *sys_errlist[];
252 extern int mutex;
253 extern int sigflags;
254