1 /* $NetBSD: ed.h,v 1.33 2005/06/26 19:10:49 christos Exp $ */
3 /* ed.h: type and constant definitions for the ed editor. */
5 * Copyright (c) 1993 Andrew Moore
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp
31 #include <sys/types.h>
32 #if defined(BSD) && BSD >= 199103 || defined(__386BSD__)
33 # include <sys/param.h> /* for MAXPATHLEN */
36 #if defined(sun) || defined(__NetBSD__) || defined(__APPLE__)
51 # define MAXPATHLEN 255 /* _POSIX_PATH_MAX */
54 #define MINBUFSZ 512 /* minimum buffer size - must be > 0 */
55 #define SE_MAX 30 /* max subexpressions in a regular expression */
57 # define LINECHARS INT_MAX /* max chars per line */
59 # define LINECHARS MAXINT /* max chars per line */
63 #define GLB 001 /* global command */
64 #define GPR 002 /* print after command */
65 #define GLS 004 /* list after command */
66 #define GNP 010 /* enumerate after command */
67 #define GSG 020 /* global substitute */
69 typedef regex_t pattern_t
;
75 off_t seek
; /* address of line in scratch buffer */
76 int len
; /* length of line */
82 /* type of undo nodes */
88 int type
; /* command type */
89 line_t
*h
; /* head of list */
90 line_t
*t
; /* tail of list */
94 # define max(a,b) ((a) > (b) ? (a) : (b))
97 # define min(a,b) ((a) < (b) ? (a) : (b))
100 #define INC_MOD(l, k) ((l) + 1 > (k) ? 0 : (l) + 1)
101 #define DEC_MOD(l, k) ((l) - 1 < 0 ? (k) : (l) - 1)
103 /* SPL1: disable some interrupts (requires reliable signals) */
104 #define SPL1() mutex++
106 /* SPL0: enable all interrupts; check sigflags (requires reliable signals) */
108 if (--mutex == 0) { \
109 if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \
110 if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \
113 /* STRTOL: convert a string to long */
114 #define STRTOL(i, p) { \
116 if (((i = strtol(p, &p, 10)) == LONG_MIN || i == LONG_MAX) && \
118 sprintf(errmsg, "number out of range"); \
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) \
132 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
133 fprintf(stderr, "%s\n", strerror(errno)); \
134 sprintf(errmsg, "out of memory"); \
139 if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
140 fprintf(stderr, "%s\n", strerror(errno)); \
141 sprintf(errmsg, "out of memory"); \
150 #else /* NO_REALLOC_NULL */
151 /* REALLOC: assure at least a minimum size for buffer b */
152 #define REALLOC(b,n,i,err) \
157 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
158 fprintf(stderr, "%s\n", strerror(errno)); \
159 sprintf(errmsg, "out of memory"); \
167 #endif /* NO_REALLOC_NULL */
169 /* REQUE: link pred before succ */
170 #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred)
172 /* INSQUE: insert elem in circular queue after pred */
173 #define INSQUE(elem, pred) \
175 REQUE((elem), (pred)->q_forw); \
176 REQUE((pred), elem); \
179 /* remque: remove_lines elem from circular queue */
180 #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw);
182 /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
183 #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
185 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
186 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
188 #if defined(sun) && !defined(__SVR4)
189 # define strerror(n) sys_errlist[n]
192 /* Local Function Declarations */
193 void add_line_node(line_t
*);
194 int append_lines(long);
195 int apply_subst_template(char *, regmatch_t
*, int, int);
196 int build_active_list(int);
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 int flush_des_file(FILE *);
213 line_t
*get_addressed_line_node(long);
214 pattern_t
*get_compiled_pattern(void);
215 int get_des_char(FILE *);
216 char *get_extended_line(int *, int);
217 char *get_filename(void);
218 int get_keyword(void);
219 long get_line_node_addr(line_t
*);
220 long get_matching_node_addr(pattern_t
*, int);
221 long get_marked_node_addr(int);
222 char *get_sbuf_line(line_t
*);
223 int get_shell_command(void);
224 int get_stream_line(FILE *);
225 int get_tty_line(void);
226 void handle_hup(int);
227 void handle_int(int);
228 void handle_winch(int);
229 int has_trailing_escape(char *, char *);
230 void init_buffers(void);
231 void init_des_cipher(void);
232 int is_legal_filename(char *);
233 int join_lines(long, long);
234 int mark_line_node(line_t
*, int);
235 int move_lines(long);
236 line_t
*next_active_node(void);
237 long next_addr(void);
239 char *parse_char_class(char *);
240 int pop_undo_stack(void);
241 undo_t
*push_undo_stack(int, long, long);
242 int put_des_char(int, FILE *);
243 char *put_sbuf_line(char *);
244 int put_stream_line(FILE *, char *, int);
245 int put_tty_line(char *, int, long, int);
247 long read_file(char *, long);
248 long read_stream(FILE *, long);
249 int search_and_replace(pattern_t
*, int, int);
250 int set_active_node(line_t
*);
251 void signal_hup(int);
252 void signal_int(int);
253 char *strip_escapes(const char *);
254 int substitute_matching_text(pattern_t
*, line_t
*, int, int);
255 char *translit_text(char *, int, int, int);
256 void unmark_line_node(line_t
*);
257 void unset_active_nodes(line_t
*, line_t
*);
258 long write_file(const char *, const char *, long, long);
259 long write_stream(FILE *, long, long);
262 extern char stdinbuf
[];
275 extern long addr_last
;
276 extern long current_addr
;
277 extern long first_addr
;
279 extern long second_addr
;
285 extern int newline_added
; /* io.c */
287 extern char errmsg
[]; /* re.c */
288 extern long u_current_addr
; /* undo.c */
289 extern long u_addr_last
; /* undo.c */
290 #if defined(sun) && !defined(__SVR4)
291 extern char *sys_errlist
[];