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