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