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