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