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