xref: /openbsd/bin/ed/ed.h (revision 88910692)
1 /*	$OpenBSD: ed.h,v 1.22 2016/03/27 00:43:38 mmcc Exp $	*/
2 /*	$NetBSD: ed.h,v 1.23 1995/03/21 09:04:40 cgd Exp $	*/
3 
4 /* ed.h: type and constant definitions for the ed editor. */
5 /*
6  * Copyright (c) 1993 Andrew Moore
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp
31  */
32 
33 #include <limits.h>
34 #include <regex.h>
35 #include <signal.h>
36 
37 #define ERR		(-2)
38 #define EMOD		(-3)
39 #define FATAL		(-4)
40 
41 #define MINBUFSZ 512		/* minimum buffer size - must be > 0 */
42 #define SE_MAX 30		/* max subexpressions in a regular expression */
43 #define LINECHARS INT_MAX	/* max chars per line */
44 
45 /* gflags */
46 #define GLB 001		/* global command */
47 #define GPR 002		/* print after command */
48 #define GLS 004		/* list after command */
49 #define GNP 010		/* enumerate after command */
50 #define GSG 020		/* global substitute */
51 
52 /* Line node */
53 typedef struct	line {
54 	struct line	*q_forw;
55 	struct line	*q_back;
56 	off_t		seek;		/* address of line in scratch buffer */
57 	int		len;		/* length of line */
58 } line_t;
59 
60 
61 typedef struct undo {
62 
63 /* type of undo nodes */
64 #define UADD	0
65 #define UDEL 	1
66 #define UMOV	2
67 #define VMOV	3
68 
69 	int type;			/* command type */
70 	line_t	*h;			/* head of list */
71 	line_t  *t;			/* tail of list */
72 } undo_t;
73 
74 #ifndef max
75 # define max(a,b) ((a) > (b) ? (a) : (b))
76 #endif
77 #ifndef min
78 # define min(a,b) ((a) < (b) ? (a) : (b))
79 #endif
80 
81 #define INC_MOD(l, k)	((l) + 1 > (k) ? 0 : (l) + 1)
82 #define DEC_MOD(l, k)	((l) - 1 < 0 ? (k) : (l) - 1)
83 
84 /* SPL1: disable some interrupts (requires reliable signals) */
85 #define SPL1() mutex++
86 
87 /* SPL0: enable all interrupts; check signal flags (requires reliable signals) */
88 #define SPL0()						\
89 	do {						\
90 		if (--mutex == 0) {			\
91 			if (sighup)			\
92 				handle_hup(SIGHUP);	\
93 			if (sigint)			\
94 				handle_int(SIGINT);	\
95 		}					\
96 	} while (0)
97 
98 /* STRTOI: convert a string to int */
99 #define STRTOI(i, p) { \
100 	long l = strtol(p, &p, 10); \
101 	if (l <= INT_MIN || l >= INT_MAX) { \
102 		seterrmsg("number out of range"); \
103 	    	i = 0; \
104 		return ERR; \
105 	} else \
106 		i = (int)l; \
107 }
108 
109 /* REALLOC: assure at least a minimum size for buffer b */
110 #define REALLOC(b,n,i,err) \
111 if ((i) > (n)) { \
112 	int ti = (n); \
113 	char *ts; \
114 	SPL1(); \
115 	if ((ts = realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
116 		perror(NULL); \
117 		seterrmsg("out of memory"); \
118 		SPL0(); \
119 		return err; \
120 	} \
121 	(n) = ti; \
122 	(b) = ts; \
123 	SPL0(); \
124 }
125 
126 /* REQUE: link pred before succ */
127 #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred)
128 
129 /* INSQUE: insert elem in circular queue after pred */
130 #define INSQUE(elem, pred) \
131 { \
132 	REQUE((elem), (pred)->q_forw); \
133 	REQUE((pred), elem); \
134 }
135 
136 /* remque: remove_lines elem from circular queue */
137 #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw);
138 
139 /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
140 #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
141 
142 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
143 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
144 
145 /* Local Function Declarations */
146 void add_line_node(line_t *);
147 int build_active_list(int);
148 void clear_active_list(void);
149 void clear_undo_stack(void);
150 int close_sbuf(void);
151 int delete_lines(int, int);
152 int display_lines(int, int, int);
153 int exec_command(void);
154 int exec_global(int, int);
155 int extract_addr_range(void);
156 int extract_subst_tail(int *, int *);
157 line_t *get_addressed_line_node(int);
158 regex_t *get_compiled_pattern(void);
159 char *get_extended_line(int *, int);
160 int get_line_node_addr(line_t *);
161 char *get_sbuf_line(line_t *);
162 int get_tty_line(void);
163 void handle_hup(int);
164 void handle_int(int);
165 int has_trailing_escape(char *, char *);
166 void init_buffers(void);
167 int open_sbuf(void);
168 int pop_undo_stack(void);
169 undo_t *push_undo_stack(int, int, int);
170 char *put_sbuf_line(char *);
171 int put_tty_line(char *, int, int, int);
172 void quit(int);
173 int read_file(char *, int);
174 int search_and_replace(regex_t *, int, int);
175 void seterrmsg(char *);
176 char *strip_escapes(char *);
177 char *translit_text(char *, int, int, int);
178 void unmark_line_node(line_t *);
179 void unset_active_nodes(line_t *, line_t *);
180 int write_file(char *, char *, int, int);
181 
182 /* global buffers */
183 extern char *ibuf;
184 extern char *ibufp;
185 extern int ibufsz;
186 
187 /* global flags */
188 extern int isbinary;
189 extern int isglobal;
190 extern int modified;
191 
192 extern volatile sig_atomic_t mutex;
193 extern volatile sig_atomic_t sighup;
194 extern volatile sig_atomic_t sigint;
195 
196 /* global vars */
197 extern int addr_last;
198 extern int current_addr;
199 extern int first_addr;
200 extern int lineno;
201 extern int second_addr;
202