Home | History | Annotate | Line # | Download | only in ed
ed.h revision 1.9
      1 /* ed.h: type and constant definitions for the ed editor. */
      2 /*
      3  * Copyright (c) 1993 The Regents of the University of California.
      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.
     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  *
     37  *	@(#)ed.h	5.5 (Berkeley) 3/28/93
     38  */
     39 
     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
     45 #include <regex.h>
     46 #include <signal.h>
     47 
     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    (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 
     62 #define MAXFNAME MAXPATHLEN	/* max file name size */
     63 #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 */
     66 
     67 typedef regex_t pattern_t;
     68 
     69 #ifdef GNU_REGEX
     70 # define FASTMAP_SIZE 256	/* size of fasmap for 8 bit character set */
     71 #endif
     72 
     73 /* Line node */
     74 typedef struct	line {
     75 	struct line	*next;
     76 	struct line	*prev;
     77 	off_t		seek;		/* address of line in scratch buffer */
     78 
     79 #define ACTV INTHIBIT			/* active bit: high bit of len */
     80 
     81 	int		len;		/* length of line */
     82 } line_t;
     83 
     84 
     85 typedef struct undo {
     86 
     87 /* type of undo nodes */
     88 #define UADD	0
     89 #define UDEL 	1
     90 #define UMOV	2
     91 #define VMOV	3
     92 
     93 	int type;			/* command type */
     94 	line_t	*h;			/* head of list */
     95 	line_t  *t;			/* tail of list */
     96 } undo_t;
     97 
     98 #ifndef max
     99 # define max(a,b) ((a) > (b) ? (a) : (b))
    100 #endif
    101 #ifndef min
    102 # define min(a,b) ((a) < (b) ? (a) : (b))
    103 #endif
    104 
    105 /* nextln: return line after l mod k */
    106 #define nextln(l,k)	((l)+1 > (k) ? 0 : (l)+1)
    107 
    108 /* nextln: return line before l mod k */
    109 #define prevln(l,k)	((l)-1 < 0 ? (k) : (l)-1)
    110 
    111 #define	skipblanks() while (isspace(*ibufp) && *ibufp != '\n') ibufp++
    112 
    113 /* spl1: disable some interrupts (requires reliable signals) */
    114 #define spl1() mutex++
    115 
    116 /* spl0: enable all interrupts; check sigflags (requires reliable signals) */
    117 #define spl0() \
    118 if (--mutex == 0) { \
    119 	if (sigflags & (1 << SIGHUP)) dohup(SIGHUP); \
    120 	if (sigflags & (1 << SIGINT)) dointr(SIGINT); \
    121 }
    122 
    123 #if defined(sun) || defined(NO_REALLOC_NULL)
    124 /* CKBUF: assure at least a minimum size for buffer b */
    125 #define CKBUF(b,n,i,err) \
    126 if ((i) > (n)) { \
    127 	int ti = (n); \
    128 	char *ts; \
    129 	spl1(); \
    130 	if ((b) != NULL) { \
    131 		if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
    132 			fprintf(stderr, "%s\n", strerror(errno)); \
    133 			sprintf(errmsg, "out of memory"); \
    134 			spl0(); \
    135 			return err; \
    136 		} \
    137 	} else { \
    138 		if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
    139 			fprintf(stderr, "%s\n", strerror(errno)); \
    140 			sprintf(errmsg, "out of memory"); \
    141 			spl0(); \
    142 			return err; \
    143 		} \
    144 	} \
    145 	(n) = ti; \
    146 	(b) = ts; \
    147 	spl0(); \
    148 }
    149 #else /* NO_REALLOC_NULL */
    150 /* CKBUF: assure at least a minimum size for buffer b */
    151 #define CKBUF(b,n,i,err) \
    152 if ((i) > (n)) { \
    153 	int ti = (n); \
    154 	char *ts; \
    155 	spl1(); \
    156 	if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
    157 		fprintf(stderr, "%s\n", strerror(errno)); \
    158 		sprintf(errmsg, "out of memory"); \
    159 		spl0(); \
    160 		return err; \
    161 	} \
    162 	(n) = ti; \
    163 	(b) = ts; \
    164 	spl0(); \
    165 }
    166 #endif /* NO_REALLOC_NULL */
    167 
    168 /* requeue: link pred before succ */
    169 #define requeue(pred, succ) (pred)->next = (succ), (succ)->prev = (pred)
    170 
    171 /* insqueue: insert elem in circular queue after pred */
    172 #define insqueue(elem, pred) \
    173 { \
    174 	requeue((elem), (pred)->next); \
    175 	requeue((pred), elem); \
    176 }
    177 
    178 /* remqueue: remove elem from circular queue */
    179 #define remqueue(elem) requeue((elem)->prev, (elem)->next);
    180 
    181 /* nultonl: overwrite ASCII NULs with newlines */
    182 #define nultonl(s, l) translit(s, l, '\0', '\n')
    183 
    184 /* nltonul: overwrite newlines with ASCII NULs */
    185 #define nltonul(s, l) translit(s, l, '\n', '\0')
    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 
    199 /* local function declarations */
    200 int append __P((long, int));
    201 int cbcdec __P((char *, FILE *));
    202 int cbcenc __P((char *, int, FILE *));
    203 int ckglob __P((void));
    204 int ckrange __P((long, long));
    205 int desflush __P((FILE *));
    206 int desgetc __P((FILE *));
    207 void desinit __P((void));
    208 int desputc __P((int, FILE *));
    209 int docmd __P((int));
    210 void err __P((char *));
    211 char *ccl __P((char *));
    212 void cvtkey __P((char *, char *));
    213 long doglob __P((int));
    214 void dohup __P((int));
    215 void dointr __P((int));
    216 void dowinch __P((int));
    217 int doprint __P((long, long, int));
    218 long doread __P((long, char *));
    219 long dowrite __P((long, long, char *, char *));
    220 char *esctos __P((char *));
    221 long patscan __P((pattern_t *, int));
    222 long getaddr __P((line_t *));
    223 char *getcmdv __P((int *, int));
    224 char *getfn __P((void));
    225 int getkey __P((void));
    226 char *getlhs __P((int));
    227 int getline __P((void));
    228 int getlist __P((void));
    229 long getnum __P((int));
    230 long getone __P((void));
    231 line_t *getlp __P((long));
    232 int getrhs __P((int));
    233 int getshcmd __P((void));
    234 char *gettxt __P((line_t *));
    235 void init_buf __P((void));
    236 int join __P((long, long));
    237 int lndelete __P((long, long));
    238 line_t *lpdup __P((line_t *));
    239 void lpqueue __P((line_t *));
    240 void makekey __P((char *));
    241 char *makesub __P((int));
    242 char *translit __P((char *, int, int, int));
    243 int move __P((long));
    244 int oddesc __P((char *, char *));
    245 void onhup __P((int));
    246 void onintr __P((int));
    247 pattern_t *optpat __P((void));
    248 void putstr __P((char *, int, long, int));
    249 char *puttxt __P((char *));
    250 void quit __P((int));
    251 int regsub __P((pattern_t *, line_t *, int));
    252 int sbclose __P((void));
    253 int sbopen __P((void));
    254 int sgetline __P((FILE *));
    255 int catsub __P((char *, regmatch_t *, int));
    256 int subst __P((pattern_t *, int));
    257 int tobinhex __P((int, int));
    258 int transfer __P((long));
    259 int undo __P((void));
    260 undo_t *upush __P((int, long, long));
    261 void ureset __P((void));
    262 
    263 
    264 extern char *sys_errlist[];
    265 extern int mutex;
    266 extern int sigflags;
    267