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