ed.h revision 1.16 1 1.1 cgd /* ed.h: type and constant definitions for the ed editor. */
2 1.1 cgd /*
3 1.16 alm * Copyright (c) 1993 Andrew Moore
4 1.1 cgd * All rights reserved.
5 1.1 cgd *
6 1.1 cgd * Redistribution and use in source and binary forms, with or without
7 1.1 cgd * modification, are permitted provided that the following conditions
8 1.1 cgd * are met:
9 1.1 cgd * 1. Redistributions of source code must retain the above copyright
10 1.1 cgd * notice, this list of conditions and the following disclaimer.
11 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer in the
13 1.1 cgd * documentation and/or other materials provided with the distribution.
14 1.1 cgd *
15 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 cgd * SUCH DAMAGE.
26 1.1 cgd *
27 1.16 alm * @(#)ed.h 5.5 (Talke Studio) 3/28/93
28 1.1 cgd */
29 1.1 cgd
30 1.1 cgd #include <unistd.h>
31 1.3 alm #include <errno.h>
32 1.9 alm #if defined(BSD) && BSD >= 199103 || defined(__386BSD__)
33 1.1 cgd # include <sys/param.h> /* for MAXPATHLEN */
34 1.1 cgd #endif
35 1.7 alm #include <regex.h>
36 1.9 alm #include <signal.h>
37 1.16 alm #ifdef sun
38 1.16 alm # include <limits.h>
39 1.16 alm #endif
40 1.1 cgd
41 1.1 cgd #define ERR (-2)
42 1.9 alm #define EMOD (-3)
43 1.9 alm #define FATAL (-4)
44 1.1 cgd
45 1.1 cgd #ifndef MAXPATHLEN
46 1.1 cgd # define MAXPATHLEN 255 /* _POSIX_PATH_MAX */
47 1.1 cgd #endif
48 1.1 cgd
49 1.1 cgd #define MAXFNAME MAXPATHLEN /* max file name size */
50 1.9 alm #define MINBUFSZ 512 /* minimum buffer size - must be > 0 */
51 1.1 cgd #define SE_MAX 30 /* max subexpressions in a regular expression */
52 1.16 alm #ifdef INT_MAX
53 1.16 alm # define LINECHARS INT_MAX /* max chars per line */
54 1.16 alm #else
55 1.16 alm # define LINECHARS MAXINT /* max chars per line */
56 1.16 alm #endif
57 1.1 cgd
58 1.1 cgd typedef regex_t pattern_t;
59 1.1 cgd
60 1.1 cgd /* Line node */
61 1.1 cgd typedef struct line {
62 1.1 cgd struct line *next;
63 1.1 cgd struct line *prev;
64 1.1 cgd off_t seek; /* address of line in scratch buffer */
65 1.1 cgd int len; /* length of line */
66 1.1 cgd } line_t;
67 1.1 cgd
68 1.1 cgd
69 1.1 cgd typedef struct undo {
70 1.1 cgd
71 1.1 cgd /* type of undo nodes */
72 1.1 cgd #define UADD 0
73 1.1 cgd #define UDEL 1
74 1.4 alm #define UMOV 2
75 1.4 alm #define VMOV 3
76 1.1 cgd
77 1.1 cgd int type; /* command type */
78 1.1 cgd line_t *h; /* head of list */
79 1.1 cgd line_t *t; /* tail of list */
80 1.1 cgd } undo_t;
81 1.1 cgd
82 1.1 cgd #ifndef max
83 1.9 alm # define max(a,b) ((a) > (b) ? (a) : (b))
84 1.1 cgd #endif
85 1.1 cgd #ifndef min
86 1.9 alm # define min(a,b) ((a) < (b) ? (a) : (b))
87 1.1 cgd #endif
88 1.1 cgd
89 1.3 alm /* nextln: return line after l mod k */
90 1.1 cgd #define nextln(l,k) ((l)+1 > (k) ? 0 : (l)+1)
91 1.1 cgd
92 1.16 alm /* prevln: return line before l mod k */
93 1.1 cgd #define prevln(l,k) ((l)-1 < 0 ? (k) : (l)-1)
94 1.1 cgd
95 1.1 cgd #define skipblanks() while (isspace(*ibufp) && *ibufp != '\n') ibufp++
96 1.1 cgd
97 1.1 cgd /* spl1: disable some interrupts (requires reliable signals) */
98 1.1 cgd #define spl1() mutex++
99 1.1 cgd
100 1.1 cgd /* spl0: enable all interrupts; check sigflags (requires reliable signals) */
101 1.1 cgd #define spl0() \
102 1.1 cgd if (--mutex == 0) { \
103 1.2 cgd if (sigflags & (1 << SIGHUP)) dohup(SIGHUP); \
104 1.2 cgd if (sigflags & (1 << SIGINT)) dointr(SIGINT); \
105 1.1 cgd }
106 1.1 cgd
107 1.9 alm #if defined(sun) || defined(NO_REALLOC_NULL)
108 1.9 alm /* CKBUF: assure at least a minimum size for buffer b */
109 1.9 alm #define CKBUF(b,n,i,err) \
110 1.9 alm if ((i) > (n)) { \
111 1.9 alm int ti = (n); \
112 1.9 alm char *ts; \
113 1.9 alm spl1(); \
114 1.9 alm if ((b) != NULL) { \
115 1.9 alm if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
116 1.9 alm fprintf(stderr, "%s\n", strerror(errno)); \
117 1.9 alm sprintf(errmsg, "out of memory"); \
118 1.9 alm spl0(); \
119 1.9 alm return err; \
120 1.9 alm } \
121 1.9 alm } else { \
122 1.9 alm if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
123 1.9 alm fprintf(stderr, "%s\n", strerror(errno)); \
124 1.9 alm sprintf(errmsg, "out of memory"); \
125 1.9 alm spl0(); \
126 1.9 alm return err; \
127 1.9 alm } \
128 1.9 alm } \
129 1.9 alm (n) = ti; \
130 1.9 alm (b) = ts; \
131 1.9 alm spl0(); \
132 1.9 alm }
133 1.9 alm #else /* NO_REALLOC_NULL */
134 1.9 alm /* CKBUF: assure at least a minimum size for buffer b */
135 1.9 alm #define CKBUF(b,n,i,err) \
136 1.9 alm if ((i) > (n)) { \
137 1.9 alm int ti = (n); \
138 1.9 alm char *ts; \
139 1.9 alm spl1(); \
140 1.9 alm if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
141 1.9 alm fprintf(stderr, "%s\n", strerror(errno)); \
142 1.9 alm sprintf(errmsg, "out of memory"); \
143 1.9 alm spl0(); \
144 1.9 alm return err; \
145 1.9 alm } \
146 1.9 alm (n) = ti; \
147 1.9 alm (b) = ts; \
148 1.9 alm spl0(); \
149 1.9 alm }
150 1.9 alm #endif /* NO_REALLOC_NULL */
151 1.9 alm
152 1.1 cgd /* requeue: link pred before succ */
153 1.1 cgd #define requeue(pred, succ) (pred)->next = (succ), (succ)->prev = (pred)
154 1.1 cgd
155 1.1 cgd /* insqueue: insert elem in circular queue after pred */
156 1.1 cgd #define insqueue(elem, pred) \
157 1.1 cgd { \
158 1.1 cgd requeue((elem), (pred)->next); \
159 1.1 cgd requeue((pred), elem); \
160 1.1 cgd }
161 1.1 cgd
162 1.1 cgd /* remqueue: remove elem from circular queue */
163 1.1 cgd #define remqueue(elem) requeue((elem)->prev, (elem)->next);
164 1.1 cgd
165 1.9 alm /* nultonl: overwrite ASCII NULs with newlines */
166 1.9 alm #define nultonl(s, l) translit(s, l, '\0', '\n')
167 1.9 alm
168 1.9 alm /* nltonul: overwrite newlines with ASCII NULs */
169 1.9 alm #define nltonul(s, l) translit(s, l, '\n', '\0')
170 1.9 alm
171 1.4 alm #ifndef strerror
172 1.4 alm # define strerror(n) sys_errlist[n]
173 1.4 alm #endif
174 1.4 alm
175 1.1 cgd #ifndef __P
176 1.1 cgd # ifndef __STDC__
177 1.1 cgd # define __P(proto) ()
178 1.1 cgd # else
179 1.1 cgd # define __P(proto) proto
180 1.1 cgd # endif
181 1.1 cgd #endif
182 1.1 cgd
183 1.16 alm /* Local Function Declarations */
184 1.1 cgd int append __P((long, int));
185 1.16 alm int catsub __P((char *, regmatch_t *, int, int));
186 1.1 cgd int cbcdec __P((char *, FILE *));
187 1.1 cgd int cbcenc __P((char *, int, FILE *));
188 1.16 alm char *ccl __P((char *));
189 1.10 alm char *ckfn __P((char *));
190 1.1 cgd int ckglob __P((void));
191 1.9 alm int ckrange __P((long, long));
192 1.16 alm void clractive __P((void));
193 1.16 alm void clrmark __P((line_t *));
194 1.16 alm void cvtkey __P((char *, char *));
195 1.9 alm int desflush __P((FILE *));
196 1.1 cgd int desgetc __P((FILE *));
197 1.9 alm void desinit __P((void));
198 1.1 cgd int desputc __P((int, FILE *));
199 1.1 cgd int docmd __P((int));
200 1.6 alm long doglob __P((int));
201 1.2 cgd void dohup __P((int));
202 1.2 cgd void dointr __P((int));
203 1.9 alm int doprint __P((long, long, int));
204 1.1 cgd long doread __P((long, char *));
205 1.16 alm void dowinch __P((int));
206 1.1 cgd long dowrite __P((long, long, char *, char *));
207 1.16 alm void err __P((char *));
208 1.1 cgd char *esctos __P((char *));
209 1.3 alm long getaddr __P((line_t *));
210 1.9 alm char *getcmdv __P((int *, int));
211 1.1 cgd char *getfn __P((void));
212 1.1 cgd int getkey __P((void));
213 1.1 cgd char *getlhs __P((int));
214 1.9 alm int getline __P((void));
215 1.1 cgd int getlist __P((void));
216 1.16 alm line_t *getlp __P((long));
217 1.13 alm long getmark __P((int));
218 1.1 cgd long getone __P((void));
219 1.9 alm int getrhs __P((int));
220 1.9 alm int getshcmd __P((void));
221 1.1 cgd char *gettxt __P((line_t *));
222 1.16 alm void inited __P((void));
223 1.16 alm int insactive __P((line_t *));
224 1.16 alm int join __P((long, long, int));
225 1.16 alm int lndelete __P((long, long, int));
226 1.1 cgd line_t *lpdup __P((line_t *));
227 1.1 cgd void lpqueue __P((line_t *));
228 1.16 alm long patscan __P((pattern_t *, int));
229 1.9 alm void makekey __P((char *));
230 1.9 alm char *makesub __P((int));
231 1.12 alm int move __P((long, int));
232 1.16 alm line_t *nextactive __P(());
233 1.1 cgd int oddesc __P((char *, char *));
234 1.1 cgd void onhup __P((int));
235 1.1 cgd void onintr __P((int));
236 1.1 cgd pattern_t *optpat __P((void));
237 1.12 alm int putmark __P((int, line_t *));
238 1.9 alm void putstr __P((char *, int, long, int));
239 1.1 cgd char *puttxt __P((char *));
240 1.1 cgd void quit __P((int));
241 1.9 alm int regsub __P((pattern_t *, line_t *, int));
242 1.16 alm void remactive __P((line_t *));
243 1.1 cgd int sbclose __P((void));
244 1.1 cgd int sbopen __P((void));
245 1.9 alm int sgetline __P((FILE *));
246 1.16 alm int subst __P((pattern_t *, int, int));
247 1.4 alm int tobinhex __P((int, int));
248 1.1 cgd int transfer __P((long));
249 1.12 alm char *translit __P((char *, int, int, int));
250 1.12 alm int undo __P((int));
251 1.4 alm undo_t *upush __P((int, long, long));
252 1.1 cgd void ureset __P((void));
253 1.9 alm
254 1.4 alm extern char *sys_errlist[];
255 1.9 alm extern int mutex;
256 1.9 alm extern int sigflags;
257