Home | History | Annotate | Line # | Download | only in ed
ed.h revision 1.21
      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.20  alm  *	@(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp
     28   1.1  cgd  */
     29   1.1  cgd 
     30  1.20  alm #include <sys/types.h>
     31  1.20  alm #if defined(BSD) && BSD >= 199103 || defined(__386BSD__)
     32  1.20  alm # include <sys/param.h>		/* for MAXPATHLEN */
     33  1.20  alm #endif
     34   1.3  alm #include <errno.h>
     35  1.20  alm #if defined(sun) || defined(__NetBSD__)
     36  1.20  alm # include <limits.h>
     37   1.1  cgd #endif
     38   1.7  alm #include <regex.h>
     39   1.9  alm #include <signal.h>
     40  1.20  alm #include <stdio.h>
     41  1.20  alm #include <stdlib.h>
     42  1.20  alm #include <string.h>
     43  1.20  alm #include <unistd.h>
     44   1.1  cgd 
     45   1.1  cgd #define ERR		(-2)
     46   1.9  alm #define EMOD		(-3)
     47   1.9  alm #define FATAL		(-4)
     48   1.1  cgd 
     49   1.1  cgd #ifndef MAXPATHLEN
     50   1.1  cgd # define MAXPATHLEN 255		/* _POSIX_PATH_MAX */
     51   1.1  cgd #endif
     52   1.1  cgd 
     53   1.9  alm #define MINBUFSZ 512		/* minimum buffer size - must be > 0 */
     54   1.1  cgd #define SE_MAX 30		/* max subexpressions in a regular expression */
     55  1.16  alm #ifdef INT_MAX
     56  1.16  alm # define LINECHARS INT_MAX	/* max chars per line */
     57  1.16  alm #else
     58  1.16  alm # define LINECHARS MAXINT	/* max chars per line */
     59  1.16  alm #endif
     60   1.1  cgd 
     61  1.20  alm /* gflags */
     62  1.20  alm #define GLB 001		/* global command */
     63  1.20  alm #define GPR 002		/* print after command */
     64  1.20  alm #define GLS 004		/* list after command */
     65  1.20  alm #define GNP 010		/* enumerate after command */
     66  1.20  alm #define GSG 020		/* global substitute */
     67  1.20  alm 
     68   1.1  cgd typedef regex_t pattern_t;
     69   1.1  cgd 
     70   1.1  cgd /* Line node */
     71   1.1  cgd typedef struct	line {
     72  1.20  alm 	struct line	*q_forw;
     73  1.20  alm 	struct line	*q_back;
     74   1.1  cgd 	off_t		seek;		/* address of line in scratch buffer */
     75   1.1  cgd 	int		len;		/* length of line */
     76   1.1  cgd } line_t;
     77   1.1  cgd 
     78   1.1  cgd 
     79   1.1  cgd typedef struct undo {
     80   1.1  cgd 
     81   1.1  cgd /* type of undo nodes */
     82   1.1  cgd #define UADD	0
     83   1.1  cgd #define UDEL 	1
     84   1.4  alm #define UMOV	2
     85   1.4  alm #define VMOV	3
     86   1.1  cgd 
     87   1.1  cgd 	int type;			/* command type */
     88   1.1  cgd 	line_t	*h;			/* head of list */
     89   1.1  cgd 	line_t  *t;			/* tail of list */
     90   1.1  cgd } undo_t;
     91   1.1  cgd 
     92   1.1  cgd #ifndef max
     93   1.9  alm # define max(a,b) ((a) > (b) ? (a) : (b))
     94   1.1  cgd #endif
     95   1.1  cgd #ifndef min
     96   1.9  alm # define min(a,b) ((a) < (b) ? (a) : (b))
     97   1.1  cgd #endif
     98   1.1  cgd 
     99  1.20  alm #define INC_MOD(l, k)	((l) + 1 > (k) ? 0 : (l) + 1)
    100  1.20  alm #define DEC_MOD(l, k)	((l) - 1 < 0 ? (k) : (l) - 1)
    101   1.1  cgd 
    102  1.17  alm /* SPL1: disable some interrupts (requires reliable signals) */
    103  1.17  alm #define SPL1() mutex++
    104   1.1  cgd 
    105  1.17  alm /* SPL0: enable all interrupts; check sigflags (requires reliable signals) */
    106  1.17  alm #define SPL0() \
    107   1.1  cgd if (--mutex == 0) { \
    108  1.20  alm 	if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \
    109  1.20  alm 	if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \
    110  1.20  alm }
    111  1.20  alm 
    112  1.20  alm /* STRTOL: convert a string to long */
    113  1.20  alm #define STRTOL(i, p) { \
    114  1.20  alm 	if (((i = strtol(p, &p, 10)) == LONG_MIN || i == LONG_MAX) && \
    115  1.20  alm 	    errno == ERANGE) { \
    116  1.20  alm 		sprintf(errmsg, "number out of range"); \
    117  1.20  alm 	    	i = 0; \
    118  1.20  alm 		return ERR; \
    119  1.20  alm 	} \
    120   1.1  cgd }
    121   1.1  cgd 
    122   1.9  alm #if defined(sun) || defined(NO_REALLOC_NULL)
    123  1.20  alm /* REALLOC: assure at least a minimum size for buffer b */
    124  1.20  alm #define REALLOC(b,n,i,err) \
    125   1.9  alm if ((i) > (n)) { \
    126   1.9  alm 	int ti = (n); \
    127   1.9  alm 	char *ts; \
    128  1.17  alm 	SPL1(); \
    129   1.9  alm 	if ((b) != NULL) { \
    130   1.9  alm 		if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
    131   1.9  alm 			fprintf(stderr, "%s\n", strerror(errno)); \
    132   1.9  alm 			sprintf(errmsg, "out of memory"); \
    133  1.17  alm 			SPL0(); \
    134   1.9  alm 			return err; \
    135   1.9  alm 		} \
    136   1.9  alm 	} else { \
    137   1.9  alm 		if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
    138   1.9  alm 			fprintf(stderr, "%s\n", strerror(errno)); \
    139   1.9  alm 			sprintf(errmsg, "out of memory"); \
    140  1.17  alm 			SPL0(); \
    141   1.9  alm 			return err; \
    142   1.9  alm 		} \
    143   1.9  alm 	} \
    144   1.9  alm 	(n) = ti; \
    145   1.9  alm 	(b) = ts; \
    146  1.17  alm 	SPL0(); \
    147   1.9  alm }
    148   1.9  alm #else /* NO_REALLOC_NULL */
    149  1.20  alm /* REALLOC: assure at least a minimum size for buffer b */
    150  1.20  alm #define REALLOC(b,n,i,err) \
    151   1.9  alm if ((i) > (n)) { \
    152   1.9  alm 	int ti = (n); \
    153   1.9  alm 	char *ts; \
    154  1.17  alm 	SPL1(); \
    155   1.9  alm 	if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
    156   1.9  alm 		fprintf(stderr, "%s\n", strerror(errno)); \
    157   1.9  alm 		sprintf(errmsg, "out of memory"); \
    158  1.17  alm 		SPL0(); \
    159   1.9  alm 		return err; \
    160   1.9  alm 	} \
    161   1.9  alm 	(n) = ti; \
    162   1.9  alm 	(b) = ts; \
    163  1.17  alm 	SPL0(); \
    164   1.9  alm }
    165   1.9  alm #endif /* NO_REALLOC_NULL */
    166   1.9  alm 
    167  1.20  alm /* REQUE: link pred before succ */
    168  1.20  alm #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred)
    169   1.1  cgd 
    170  1.21  alm /* INSQUE: insert elem in circular queue after pred */
    171  1.21  alm #define INSQUE(elem, pred) \
    172   1.1  cgd { \
    173  1.20  alm 	REQUE((elem), (pred)->q_forw); \
    174  1.20  alm 	REQUE((pred), elem); \
    175   1.1  cgd }
    176   1.1  cgd 
    177  1.20  alm /* remque: remove_lines elem from circular queue */
    178  1.21  alm #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw);
    179   1.1  cgd 
    180  1.17  alm /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
    181  1.17  alm #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
    182   1.9  alm 
    183  1.17  alm /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
    184  1.17  alm #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
    185   1.9  alm 
    186  1.20  alm #ifdef sun
    187   1.4  alm # define strerror(n) sys_errlist[n]
    188   1.4  alm #endif
    189   1.4  alm 
    190   1.1  cgd #ifndef __P
    191   1.1  cgd # ifndef __STDC__
    192   1.1  cgd #  define __P(proto) ()
    193   1.1  cgd # else
    194   1.1  cgd #  define __P(proto) proto
    195   1.1  cgd # endif
    196   1.1  cgd #endif
    197   1.1  cgd 
    198  1.16  alm /* Local Function Declarations */
    199  1.17  alm void add_line_node __P((line_t *));
    200  1.17  alm int append_lines __P((long));
    201  1.17  alm int apply_subst_template __P((char *, regmatch_t *, int, int));
    202  1.17  alm int build_active_list __P((int));
    203  1.17  alm int cbc_decode __P((char *, FILE *));
    204  1.17  alm int cbc_encode __P((char *, int, FILE *));
    205  1.17  alm int check_addr_range __P((long, long));
    206  1.17  alm void clear_active_list __P((void));
    207  1.17  alm void clear_undo_stack __P((void));
    208  1.17  alm int close_sbuf __P((void));
    209  1.17  alm int copy_lines __P((long));
    210  1.17  alm int delete_lines __P((long, long));
    211  1.17  alm void des_error __P((char *));
    212  1.17  alm int display_lines __P((long, long, int));
    213  1.17  alm line_t *dup_line_node __P((line_t *));
    214  1.17  alm int exec_command __P((void));
    215  1.17  alm long exec_global __P((int, int));
    216  1.17  alm void expand_des_key __P((char *, char *));
    217  1.17  alm int extract_addr_range __P((void));
    218  1.17  alm char *extract_pattern __P((int));
    219  1.20  alm int extract_subst_tail __P((int *, int *));
    220  1.17  alm char *extract_subst_template __P((void));
    221  1.20  alm int filter_lines __P((long, long, char *));
    222  1.17  alm int flush_des_file __P((FILE *));
    223  1.17  alm line_t *get_addressed_line_node __P((long));
    224  1.20  alm pattern_t *get_compiled_pattern __P((void));
    225  1.17  alm int get_des_char __P((FILE *));
    226  1.17  alm char *get_extended_line __P((int *, int));
    227  1.17  alm char *get_filename __P((void));
    228  1.17  alm int get_keyword __P((void));
    229  1.17  alm long get_line_node_addr __P((line_t *));
    230  1.17  alm long get_matching_node_addr __P((pattern_t *, int));
    231  1.17  alm long get_marked_node_addr __P((int));
    232  1.17  alm char *get_sbuf_line __P((line_t *));
    233  1.17  alm int get_shell_command __P((void));
    234  1.20  alm int get_stream_line __P((FILE *));
    235  1.20  alm int get_tty_line __P((void));
    236  1.17  alm void handle_hup __P((int));
    237  1.17  alm void handle_int __P((int));
    238  1.17  alm void handle_winch __P((int));
    239  1.17  alm int has_trailing_escape __P((char *, char *));
    240  1.17  alm int hex_to_binary __P((int, int));
    241  1.17  alm void init_buffers __P((void));
    242  1.17  alm void init_des_cipher __P((void));
    243  1.17  alm int is_legal_filename __P((char *));
    244  1.17  alm int join_lines __P((long, long));
    245  1.17  alm int mark_line_node __P((line_t *, int));
    246  1.17  alm int move_lines __P((long));
    247  1.17  alm line_t *next_active_node __P(());
    248  1.17  alm long next_addr __P((void));
    249  1.17  alm int open_sbuf __P((void));
    250  1.17  alm char *parse_char_class __P((char *));
    251  1.17  alm int pop_undo_stack __P((void));
    252  1.17  alm undo_t *push_undo_stack __P((int, long, long));
    253  1.17  alm int put_des_char __P((int, FILE *));
    254  1.17  alm char *put_sbuf_line __P((char *));
    255  1.20  alm int put_stream_line __P((FILE *, char *, int));
    256  1.20  alm int put_tty_line __P((char *, int, long, int));
    257   1.1  cgd void quit __P((int));
    258  1.20  alm long read_file __P((char *, long));
    259  1.20  alm long read_stream __P((FILE *, long));
    260  1.20  alm int search_and_replace __P((pattern_t *, int, int));
    261  1.17  alm int set_active_node __P((line_t *));
    262  1.17  alm void set_des_key __P((char *));
    263  1.17  alm void signal_hup __P((int));
    264  1.17  alm void signal_int __P((int));
    265  1.17  alm char *strip_escapes __P((char *));
    266  1.20  alm int substitute_matching_text __P((pattern_t *, line_t *, int, int));
    267  1.17  alm char *translit_text __P((char *, int, int, int));
    268  1.17  alm void unmark_line_node __P((line_t *));
    269  1.17  alm void unset_active_nodes __P((line_t *, line_t *));
    270  1.20  alm long write_file __P((char *, char *, long, long));
    271  1.20  alm long write_stream __P((FILE *, long, long));
    272   1.9  alm 
    273  1.20  alm /* global buffers */
    274  1.20  alm extern char stdinbuf[];
    275  1.20  alm extern char *ibuf;
    276  1.20  alm extern char *ibufp;
    277  1.20  alm extern int ibufsz;
    278  1.20  alm 
    279  1.20  alm /* global flags */
    280  1.20  alm extern int isbinary;
    281  1.20  alm extern int isglobal;
    282  1.20  alm extern int modified;
    283   1.9  alm extern int mutex;
    284   1.9  alm extern int sigflags;
    285  1.20  alm 
    286  1.20  alm /* global vars */
    287  1.20  alm extern long addr_last;
    288  1.20  alm extern long current_addr;
    289  1.20  alm extern char errmsg[];
    290  1.20  alm extern long first_addr;
    291  1.20  alm extern int lineno;
    292  1.20  alm extern long second_addr;
    293  1.20  alm #ifdef sun
    294  1.20  alm extern char *sys_errlist[];
    295  1.20  alm #endif
    296