Deleted Added
full compact
ed.h (268) ed.h (1057)
1/* ed.h: type and constant definitions for the ed editor. */
2/*
1/* ed.h: type and constant definitions for the ed editor. */
2/*
3 * Copyright (c) 1993 The Regents of the University of California.
3 * Copyright (c) 1993 Andrew Moore
4 * All rights reserved.
5 *
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Andrew Moore, Talke Studio.
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.
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.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
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 *
37 * @(#)ed.h 5.5 (Berkeley) 3/28/93
27 * @(#)$Id: ed.h,v 1.4 1993/12/15 15:22:02 alm Exp alm $
38 */
39
28 */
29
40#include <unistd.h>
41#include <errno.h>
42#if defined(BSD) && BSD >= 199103 || defined(__386BSD__)
43# include <sys/param.h> /* for MAXPATHLEN */
44#endif
30#if defined(BSD) && BSD >= 199103 || defined(__386BSD__)
31# include <sys/param.h> /* for MAXPATHLEN */
32#endif
33#include <errno.h>
34#ifdef sun
35# include <limits.h>
36#endif
45#include <regex.h>
46#include <signal.h>
37#include <regex.h>
38#include <signal.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
47
43
48#define BITSPERBYTE 8
49#define BITS(type) (BITSPERBYTE * (int)sizeof(type))
50#define CHARBITS BITS(char)
51#define INTBITS BITS(int)
52#define INTHIBIT (unsigned) (1 << (INTBITS - 1))
53
54#define ERR (-2)
55#define EMOD (-3)
56#define FATAL (-4)
57
58#ifndef MAXPATHLEN
59# define MAXPATHLEN 255 /* _POSIX_PATH_MAX */
60#endif
61
44#define ERR (-2)
45#define EMOD (-3)
46#define FATAL (-4)
47
48#ifndef MAXPATHLEN
49# define MAXPATHLEN 255 /* _POSIX_PATH_MAX */
50#endif
51
62#define MAXFNAME MAXPATHLEN /* max file name size */
63#define MINBUFSZ 512 /* minimum buffer size - must be > 0 */
52#define MINBUFSZ 512 /* minimum buffer size - must be > 0 */
64#define LINECHARS (INTHIBIT - 1) /* max chars per line */
65#define SE_MAX 30 /* max subexpressions in a regular expression */
53#define SE_MAX 30 /* max subexpressions in a regular expression */
54#ifdef INT_MAX
55# define LINECHARS INT_MAX /* max chars per line */
56#else
57# define LINECHARS MAXINT /* max chars per line */
58#endif
66
59
60/* gflags */
61#define GLB 001 /* global command */
62#define GPR 002 /* print after command */
63#define GLS 004 /* list after command */
64#define GNP 010 /* enumerate after command */
65#define GSG 020 /* global substitute */
66
67typedef regex_t pattern_t;
68
69/* Line node */
70typedef struct line {
67typedef regex_t pattern_t;
68
69/* Line node */
70typedef struct line {
71 struct line *next;
72 struct line *prev;
71 struct line *q_forw;
72 struct line *q_back;
73 off_t seek; /* address of line in scratch buffer */
73 off_t seek; /* address of line in scratch buffer */
74
75#define ACTV INTHIBIT /* active bit: high bit of len */
76
77 int len; /* length of line */
78} line_t;
79
80
81typedef struct undo {
82
83/* type of undo nodes */
84#define UADD 0

--- 8 unchanged lines hidden (view full) ---

93
94#ifndef max
95# define max(a,b) ((a) > (b) ? (a) : (b))
96#endif
97#ifndef min
98# define min(a,b) ((a) < (b) ? (a) : (b))
99#endif
100
74 int len; /* length of line */
75} line_t;
76
77
78typedef struct undo {
79
80/* type of undo nodes */
81#define UADD 0

--- 8 unchanged lines hidden (view full) ---

90
91#ifndef max
92# define max(a,b) ((a) > (b) ? (a) : (b))
93#endif
94#ifndef min
95# define min(a,b) ((a) < (b) ? (a) : (b))
96#endif
97
101/* nextln: return line after l mod k */
102#define nextln(l,k) ((l)+1 > (k) ? 0 : (l)+1)
98#define INC_MOD(l, k) ((l) + 1 > (k) ? 0 : (l) + 1)
99#define DEC_MOD(l, k) ((l) - 1 < 0 ? (k) : (l) - 1)
103
100
104/* nextln: return line before l mod k */
105#define prevln(l,k) ((l)-1 < 0 ? (k) : (l)-1)
101/* SPL1: disable some interrupts (requires reliable signals) */
102#define SPL1() mutex++
106
103
107#define skipblanks() while (isspace(*ibufp) && *ibufp != '\n') ibufp++
108
109/* spl1: disable some interrupts (requires reliable signals) */
110#define spl1() mutex++
111
112/* spl0: enable all interrupts; check sigflags (requires reliable signals) */
113#define spl0() \
104/* SPL0: enable all interrupts; check sigflags (requires reliable signals) */
105#define SPL0() \
114if (--mutex == 0) { \
106if (--mutex == 0) { \
115 if (sigflags & (1 << SIGHUP)) dohup(SIGHUP); \
116 if (sigflags & (1 << SIGINT)) dointr(SIGINT); \
107 if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \
108 if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \
117}
118
109}
110
111/* STRTOL: convert a string to long */
112#define STRTOL(i, p) { \
113 if (((i = strtol(p, &p, 10)) == LONG_MIN || i == LONG_MAX) && \
114 errno == ERANGE) { \
115 sprintf(errmsg, "number out of range"); \
116 i = 0; \
117 return ERR; \
118 } \
119}
120
119#if defined(sun) || defined(NO_REALLOC_NULL)
121#if defined(sun) || defined(NO_REALLOC_NULL)
120/* CKBUF: assure at least a minimum size for buffer b */
121#define CKBUF(b,n,i,err) \
122/* REALLOC: assure at least a minimum size for buffer b */
123#define REALLOC(b,n,i,err) \
122if ((i) > (n)) { \
123 int ti = (n); \
124 char *ts; \
124if ((i) > (n)) { \
125 int ti = (n); \
126 char *ts; \
125 spl1(); \
127 SPL1(); \
126 if ((b) != NULL) { \
127 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
128 fprintf(stderr, "%s\n", strerror(errno)); \
129 sprintf(errmsg, "out of memory"); \
128 if ((b) != NULL) { \
129 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
130 fprintf(stderr, "%s\n", strerror(errno)); \
131 sprintf(errmsg, "out of memory"); \
130 spl0(); \
132 SPL0(); \
131 return err; \
132 } \
133 } else { \
134 if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
135 fprintf(stderr, "%s\n", strerror(errno)); \
136 sprintf(errmsg, "out of memory"); \
133 return err; \
134 } \
135 } else { \
136 if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
137 fprintf(stderr, "%s\n", strerror(errno)); \
138 sprintf(errmsg, "out of memory"); \
137 spl0(); \
139 SPL0(); \
138 return err; \
139 } \
140 } \
141 (n) = ti; \
142 (b) = ts; \
140 return err; \
141 } \
142 } \
143 (n) = ti; \
144 (b) = ts; \
143 spl0(); \
145 SPL0(); \
144}
145#else /* NO_REALLOC_NULL */
146}
147#else /* NO_REALLOC_NULL */
146/* CKBUF: assure at least a minimum size for buffer b */
147#define CKBUF(b,n,i,err) \
148/* REALLOC: assure at least a minimum size for buffer b */
149#define REALLOC(b,n,i,err) \
148if ((i) > (n)) { \
149 int ti = (n); \
150 char *ts; \
150if ((i) > (n)) { \
151 int ti = (n); \
152 char *ts; \
151 spl1(); \
153 SPL1(); \
152 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
153 fprintf(stderr, "%s\n", strerror(errno)); \
154 sprintf(errmsg, "out of memory"); \
154 if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
155 fprintf(stderr, "%s\n", strerror(errno)); \
156 sprintf(errmsg, "out of memory"); \
155 spl0(); \
157 SPL0(); \
156 return err; \
157 } \
158 (n) = ti; \
159 (b) = ts; \
158 return err; \
159 } \
160 (n) = ti; \
161 (b) = ts; \
160 spl0(); \
162 SPL0(); \
161}
162#endif /* NO_REALLOC_NULL */
163
163}
164#endif /* NO_REALLOC_NULL */
165
164/* requeue: link pred before succ */
165#define requeue(pred, succ) (pred)->next = (succ), (succ)->prev = (pred)
166/* REQUE: link pred before succ */
167#define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred)
166
168
167/* insqueue: insert elem in circular queue after pred */
168#define insqueue(elem, pred) \
169#ifdef NEED_INSQUE
170/* insque: insert elem in circular queue after pred */
171#define insque(elem, pred) \
169{ \
172{ \
170 requeue((elem), (pred)->next); \
171 requeue((pred), elem); \
173 REQUE((elem), (pred)->q_forw); \
174 REQUE((pred), elem); \
172}
173
175}
176
174/* remqueue: remove elem from circular queue */
175#define remqueue(elem) requeue((elem)->prev, (elem)->next);
177/* remque: remove_lines elem from circular queue */
178#define remque(elem) REQUE((elem)->q_back, (elem)->q_forw);
179#endif /* NEED_INSQUE */
176
180
177/* nultonl: overwrite ASCII NULs with newlines */
178#define nultonl(s, l) translit(s, l, '\0', '\n')
181/* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
182#define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
179
183
180/* nltonul: overwrite newlines with ASCII NULs */
181#define nltonul(s, l) translit(s, l, '\n', '\0')
184/* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
185#define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
182
183#ifndef strerror
184# define strerror(n) sys_errlist[n]
185#endif
186
187#ifndef __P
188# ifndef __STDC__
189# define __P(proto) ()
190# else
191# define __P(proto) proto
192# endif
193#endif
194
186
187#ifndef strerror
188# define strerror(n) sys_errlist[n]
189#endif
190
191#ifndef __P
192# ifndef __STDC__
193# define __P(proto) ()
194# else
195# define __P(proto) proto
196# endif
197#endif
198
195/* local function declarations */
196int append __P((long, int));
197int cbcdec __P((char *, FILE *));
198int cbcenc __P((char *, int, FILE *));
199char *ckfn __P((char *));
200int ckglob __P((void));
201int ckrange __P((long, long));
202int desflush __P((FILE *));
203int desgetc __P((FILE *));
204void desinit __P((void));
205int desputc __P((int, FILE *));
206int docmd __P((int));
207void err __P((char *));
208char *ccl __P((char *));
209void clrmark __P((line_t *));
210void cvtkey __P((char *, char *));
211long doglob __P((int));
212void dohup __P((int));
213void dointr __P((int));
214void dowinch __P((int));
215int doprint __P((long, long, int));
216long doread __P((long, char *));
217long dowrite __P((long, long, char *, char *));
218char *esctos __P((char *));
219long patscan __P((pattern_t *, int));
220long getaddr __P((line_t *));
221char *getcmdv __P((int *, int));
222char *getfn __P((void));
223int getkey __P((void));
224char *getlhs __P((int));
225int getline __P((void));
226int getlist __P((void));
227long getmark __P((int));
228long getnum __P((int));
229long getone __P((void));
230line_t *getlp __P((long));
231int getrhs __P((int));
232int getshcmd __P((void));
233char *gettxt __P((line_t *));
234void init_buf __P((void));
235int join __P((long, long));
236int lndelete __P((long, long));
237line_t *lpdup __P((line_t *));
238void lpqueue __P((line_t *));
239void makekey __P((char *));
240char *makesub __P((int));
241int move __P((long, int));
242int oddesc __P((char *, char *));
243void onhup __P((int));
244void onintr __P((int));
245pattern_t *optpat __P((void));
246int putmark __P((int, line_t *));
247void putstr __P((char *, int, long, int));
248char *puttxt __P((char *));
199/* Local Function Declarations */
200void add_line_node __P((line_t *));
201int append_lines __P((long));
202int apply_subst_template __P((char *, regmatch_t *, int, int));
203int build_active_list __P((int));
204int cbc_decode __P((char *, FILE *));
205int cbc_encode __P((char *, int, FILE *));
206int check_addr_range __P((long, long));
207void clear_active_list __P((void));
208void clear_undo_stack __P((void));
209int close_sbuf __P((void));
210int copy_lines __P((long));
211int delete_lines __P((long, long));
212void des_error __P((char *));
213int display_lines __P((long, long, int));
214line_t *dup_line_node __P((line_t *));
215int exec_command __P((void));
216long exec_global __P((int, int));
217void expand_des_key __P((char *, char *));
218int extract_addr_range __P((void));
219char *extract_pattern __P((int));
220int extract_subst_tail __P((int *, int *));
221char *extract_subst_template __P((void));
222int filter_lines __P((long, long, char *));
223int flush_des_file __P((FILE *));
224line_t *get_addressed_line_node __P((long));
225pattern_t *get_compiled_pattern __P((void));
226int get_des_char __P((FILE *));
227char *get_extended_line __P((int *, int));
228char *get_filename __P((void));
229int get_keyword __P((void));
230long get_line_node_addr __P((line_t *));
231long get_matching_node_addr __P((pattern_t *, int));
232long get_marked_node_addr __P((int));
233char *get_sbuf_line __P((line_t *));
234int get_shell_command __P((void));
235int get_stream_line __P((FILE *));
236int get_tty_line __P((void));
237void handle_hup __P((int));
238void handle_int __P((int));
239void handle_winch __P((int));
240int has_trailing_escape __P((char *, char *));
241int hex_to_binary __P((int, int));
242void init_buffers __P((void));
243void init_des_cipher __P((void));
244int is_legal_filename __P((char *));
245int join_lines __P((long, long));
246int mark_line_node __P((line_t *, int));
247int move_lines __P((long));
248line_t *next_active_node __P(());
249long next_addr __P((void));
250int open_sbuf __P((void));
251char *parse_char_class __P((char *));
252int pop_undo_stack __P((void));
253undo_t *push_undo_stack __P((int, long, long));
254int put_des_char __P((int, FILE *));
255char *put_sbuf_line __P((char *));
256int put_stream_line __P((FILE *, char *, int));
257int put_tty_line __P((char *, int, long, int));
249void quit __P((int));
258void quit __P((int));
250int regsub __P((pattern_t *, line_t *, int));
251int sbclose __P((void));
252int sbopen __P((void));
253int sgetline __P((FILE *));
254int catsub __P((char *, regmatch_t *, int));
255int subst __P((pattern_t *, int));
256int tobinhex __P((int, int));
257int transfer __P((long));
258char *translit __P((char *, int, int, int));
259int undo __P((int));
260undo_t *upush __P((int, long, long));
261void ureset __P((void));
259long read_file __P((char *, long));
260long read_stream __P((FILE *, long));
261int search_and_replace __P((pattern_t *, int, int));
262int set_active_node __P((line_t *));
263void set_des_key __P((char *));
264void signal_hup __P((int));
265void signal_int __P((int));
266char *strip_escapes __P((char *));
267int substitute_matching_text __P((pattern_t *, line_t *, int, int));
268char *translit_text __P((char *, int, int, int));
269void unmark_line_node __P((line_t *));
270void unset_active_nodes __P((line_t *, line_t *));
271long write_file __P((char *, char *, long, long));
272long write_stream __P((FILE *, long, long));
262
273
274/* global buffers */
275extern char stdinbuf[];
276extern char *ibuf;
277extern char *ibufp;
278extern int ibufsz;
263
279
264extern char *sys_errlist[];
280/* global flags */
281extern int isbinary;
282extern int isglobal;
283extern int modified;
265extern int mutex;
266extern int sigflags;
284extern int mutex;
285extern int sigflags;
286
287/* global vars */
288extern long addr_last;
289extern long current_addr;
290extern char errmsg[];
291extern long first_addr;
292extern int lineno;
293extern long second_addr;
294#ifdef sun
295extern char *sys_errlist[];
296#endif