Home | History | Annotate | Line # | Download | only in ed
ed.h revision 1.16
      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  *	@(#)ed.h	5.5 (Talke Studio) 3/28/93
     28  */
     29 
     30 #include <unistd.h>
     31 #include <errno.h>
     32 #if defined(BSD) && BSD >= 199103 || defined(__386BSD__)
     33 # include <sys/param.h>		/* for MAXPATHLEN */
     34 #endif
     35 #include <regex.h>
     36 #include <signal.h>
     37 #ifdef sun
     38 # include <limits.h>
     39 #endif
     40 
     41 #define ERR		(-2)
     42 #define EMOD		(-3)
     43 #define FATAL		(-4)
     44 
     45 #ifndef MAXPATHLEN
     46 # define MAXPATHLEN 255		/* _POSIX_PATH_MAX */
     47 #endif
     48 
     49 #define MAXFNAME MAXPATHLEN	/* max file name size */
     50 #define MINBUFSZ 512		/* minimum buffer size - must be > 0 */
     51 #define SE_MAX 30		/* max subexpressions in a regular expression */
     52 #ifdef INT_MAX
     53 # define LINECHARS INT_MAX	/* max chars per line */
     54 #else
     55 # define LINECHARS MAXINT	/* max chars per line */
     56 #endif
     57 
     58 typedef regex_t pattern_t;
     59 
     60 /* Line node */
     61 typedef struct	line {
     62 	struct line	*next;
     63 	struct line	*prev;
     64 	off_t		seek;		/* address of line in scratch buffer */
     65 	int		len;		/* length of line */
     66 } line_t;
     67 
     68 
     69 typedef struct undo {
     70 
     71 /* type of undo nodes */
     72 #define UADD	0
     73 #define UDEL 	1
     74 #define UMOV	2
     75 #define VMOV	3
     76 
     77 	int type;			/* command type */
     78 	line_t	*h;			/* head of list */
     79 	line_t  *t;			/* tail of list */
     80 } undo_t;
     81 
     82 #ifndef max
     83 # define max(a,b) ((a) > (b) ? (a) : (b))
     84 #endif
     85 #ifndef min
     86 # define min(a,b) ((a) < (b) ? (a) : (b))
     87 #endif
     88 
     89 /* nextln: return line after l mod k */
     90 #define nextln(l,k)	((l)+1 > (k) ? 0 : (l)+1)
     91 
     92 /* prevln: return line before l mod k */
     93 #define prevln(l,k)	((l)-1 < 0 ? (k) : (l)-1)
     94 
     95 #define	skipblanks() while (isspace(*ibufp) && *ibufp != '\n') ibufp++
     96 
     97 /* spl1: disable some interrupts (requires reliable signals) */
     98 #define spl1() mutex++
     99 
    100 /* spl0: enable all interrupts; check sigflags (requires reliable signals) */
    101 #define spl0() \
    102 if (--mutex == 0) { \
    103 	if (sigflags & (1 << SIGHUP)) dohup(SIGHUP); \
    104 	if (sigflags & (1 << SIGINT)) dointr(SIGINT); \
    105 }
    106 
    107 #if defined(sun) || defined(NO_REALLOC_NULL)
    108 /* CKBUF: assure at least a minimum size for buffer b */
    109 #define CKBUF(b,n,i,err) \
    110 if ((i) > (n)) { \
    111 	int ti = (n); \
    112 	char *ts; \
    113 	spl1(); \
    114 	if ((b) != NULL) { \
    115 		if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
    116 			fprintf(stderr, "%s\n", strerror(errno)); \
    117 			sprintf(errmsg, "out of memory"); \
    118 			spl0(); \
    119 			return err; \
    120 		} \
    121 	} else { \
    122 		if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
    123 			fprintf(stderr, "%s\n", strerror(errno)); \
    124 			sprintf(errmsg, "out of memory"); \
    125 			spl0(); \
    126 			return err; \
    127 		} \
    128 	} \
    129 	(n) = ti; \
    130 	(b) = ts; \
    131 	spl0(); \
    132 }
    133 #else /* NO_REALLOC_NULL */
    134 /* CKBUF: assure at least a minimum size for buffer b */
    135 #define CKBUF(b,n,i,err) \
    136 if ((i) > (n)) { \
    137 	int ti = (n); \
    138 	char *ts; \
    139 	spl1(); \
    140 	if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
    141 		fprintf(stderr, "%s\n", strerror(errno)); \
    142 		sprintf(errmsg, "out of memory"); \
    143 		spl0(); \
    144 		return err; \
    145 	} \
    146 	(n) = ti; \
    147 	(b) = ts; \
    148 	spl0(); \
    149 }
    150 #endif /* NO_REALLOC_NULL */
    151 
    152 /* requeue: link pred before succ */
    153 #define requeue(pred, succ) (pred)->next = (succ), (succ)->prev = (pred)
    154 
    155 /* insqueue: insert elem in circular queue after pred */
    156 #define insqueue(elem, pred) \
    157 { \
    158 	requeue((elem), (pred)->next); \
    159 	requeue((pred), elem); \
    160 }
    161 
    162 /* remqueue: remove elem from circular queue */
    163 #define remqueue(elem) requeue((elem)->prev, (elem)->next);
    164 
    165 /* nultonl: overwrite ASCII NULs with newlines */
    166 #define nultonl(s, l) translit(s, l, '\0', '\n')
    167 
    168 /* nltonul: overwrite newlines with ASCII NULs */
    169 #define nltonul(s, l) translit(s, l, '\n', '\0')
    170 
    171 #ifndef strerror
    172 # define strerror(n) sys_errlist[n]
    173 #endif
    174 
    175 #ifndef __P
    176 # ifndef __STDC__
    177 #  define __P(proto) ()
    178 # else
    179 #  define __P(proto) proto
    180 # endif
    181 #endif
    182 
    183 /* Local Function Declarations */
    184 int append __P((long, int));
    185 int catsub __P((char *, regmatch_t *, int, int));
    186 int cbcdec __P((char *, FILE *));
    187 int cbcenc __P((char *, int, FILE *));
    188 char *ccl __P((char *));
    189 char *ckfn __P((char *));
    190 int ckglob __P((void));
    191 int ckrange __P((long, long));
    192 void clractive __P((void));
    193 void clrmark __P((line_t *));
    194 void cvtkey __P((char *, char *));
    195 int desflush __P((FILE *));
    196 int desgetc __P((FILE *));
    197 void desinit __P((void));
    198 int desputc __P((int, FILE *));
    199 int docmd __P((int));
    200 long doglob __P((int));
    201 void dohup __P((int));
    202 void dointr __P((int));
    203 int doprint __P((long, long, int));
    204 long doread __P((long, char *));
    205 void dowinch __P((int));
    206 long dowrite __P((long, long, char *, char *));
    207 void err __P((char *));
    208 char *esctos __P((char *));
    209 long getaddr __P((line_t *));
    210 char *getcmdv __P((int *, int));
    211 char *getfn __P((void));
    212 int getkey __P((void));
    213 char *getlhs __P((int));
    214 int getline __P((void));
    215 int getlist __P((void));
    216 line_t *getlp __P((long));
    217 long getmark __P((int));
    218 long getone __P((void));
    219 int getrhs __P((int));
    220 int getshcmd __P((void));
    221 char *gettxt __P((line_t *));
    222 void inited __P((void));
    223 int insactive __P((line_t *));
    224 int join __P((long, long, int));
    225 int lndelete __P((long, long, int));
    226 line_t *lpdup __P((line_t *));
    227 void lpqueue __P((line_t *));
    228 long patscan __P((pattern_t *, int));
    229 void makekey __P((char *));
    230 char *makesub __P((int));
    231 int move __P((long, int));
    232 line_t *nextactive __P(());
    233 int oddesc __P((char *, char *));
    234 void onhup __P((int));
    235 void onintr __P((int));
    236 pattern_t *optpat __P((void));
    237 int putmark __P((int, line_t *));
    238 void putstr __P((char *, int, long, int));
    239 char *puttxt __P((char *));
    240 void quit __P((int));
    241 int regsub __P((pattern_t *, line_t *, int));
    242 void remactive __P((line_t *));
    243 int sbclose __P((void));
    244 int sbopen __P((void));
    245 int sgetline __P((FILE *));
    246 int subst __P((pattern_t *, int, int));
    247 int tobinhex __P((int, int));
    248 int transfer __P((long));
    249 char *translit __P((char *, int, int, int));
    250 int undo __P((int));
    251 undo_t *upush __P((int, long, long));
    252 void ureset __P((void));
    253 
    254 extern char *sys_errlist[];
    255 extern int mutex;
    256 extern int sigflags;
    257