Home | History | Annotate | Line # | Download | only in ed
ed.h revision 1.29
      1 /*	$NetBSD: ed.h,v 1.29 2002/01/23 19:07:33 atatat Exp $	*/
      2 
      3 /* ed.h: type and constant definitions for the ed editor. */
      4 /*
      5  * Copyright (c) 1993 Andrew Moore
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  *
     29  *	@(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp
     30  */
     31 #include <sys/types.h>
     32 #if defined(BSD) && BSD >= 199103 || defined(__386BSD__)
     33 # include <sys/param.h>		/* for MAXPATHLEN */
     34 #endif
     35 #include <errno.h>
     36 #if defined(sun) || defined(__NetBSD__) || defined(__APPLE__)
     37 # include <limits.h>
     38 #endif
     39 #include <regex.h>
     40 #include <signal.h>
     41 #include <stdio.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 #include <unistd.h>
     45 
     46 #define ERR		(-2)
     47 #define EMOD		(-3)
     48 #define FATAL		(-4)
     49 
     50 #ifndef MAXPATHLEN
     51 # define MAXPATHLEN 255		/* _POSIX_PATH_MAX */
     52 #endif
     53 
     54 #define MINBUFSZ 512		/* minimum buffer size - must be > 0 */
     55 #define SE_MAX 30		/* max subexpressions in a regular expression */
     56 #ifdef INT_MAX
     57 # define LINECHARS INT_MAX	/* max chars per line */
     58 #else
     59 # define LINECHARS MAXINT	/* max chars per line */
     60 #endif
     61 
     62 /* gflags */
     63 #define GLB 001		/* global command */
     64 #define GPR 002		/* print after command */
     65 #define GLS 004		/* list after command */
     66 #define GNP 010		/* enumerate after command */
     67 #define GSG 020		/* global substitute */
     68 
     69 typedef regex_t pattern_t;
     70 
     71 /* Line node */
     72 typedef struct	line {
     73 	struct line	*q_forw;
     74 	struct line	*q_back;
     75 	off_t		seek;		/* address of line in scratch buffer */
     76 	int		len;		/* length of line */
     77 } line_t;
     78 
     79 
     80 typedef struct undo {
     81 
     82 /* type of undo nodes */
     83 #define UADD	0
     84 #define UDEL 	1
     85 #define UMOV	2
     86 #define VMOV	3
     87 
     88 	int type;			/* command type */
     89 	line_t	*h;			/* head of list */
     90 	line_t  *t;			/* tail of list */
     91 } undo_t;
     92 
     93 #ifndef max
     94 # define max(a,b) ((a) > (b) ? (a) : (b))
     95 #endif
     96 #ifndef min
     97 # define min(a,b) ((a) < (b) ? (a) : (b))
     98 #endif
     99 
    100 #define INC_MOD(l, k)	((l) + 1 > (k) ? 0 : (l) + 1)
    101 #define DEC_MOD(l, k)	((l) - 1 < 0 ? (k) : (l) - 1)
    102 
    103 /* SPL1: disable some interrupts (requires reliable signals) */
    104 #define SPL1() mutex++
    105 
    106 /* SPL0: enable all interrupts; check sigflags (requires reliable signals) */
    107 #define SPL0() \
    108 if (--mutex == 0) { \
    109 	if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \
    110 	if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \
    111 }
    112 
    113 /* STRTOL: convert a string to long */
    114 #define STRTOL(i, p) { \
    115 	if (((i = strtol(p, &p, 10)) == LONG_MIN || i == LONG_MAX) && \
    116 	    errno == ERANGE) { \
    117 		sprintf(errmsg, "number out of range"); \
    118 	    	i = 0; \
    119 		return ERR; \
    120 	} \
    121 }
    122 
    123 #if defined(sun) || defined(NO_REALLOC_NULL)
    124 /* REALLOC: assure at least a minimum size for buffer b */
    125 #define REALLOC(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 /* REALLOC: assure at least a minimum size for buffer b */
    151 #define REALLOC(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 /* REQUE: link pred before succ */
    169 #define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred)
    170 
    171 /* INSQUE: insert elem in circular queue after pred */
    172 #define INSQUE(elem, pred) \
    173 { \
    174 	REQUE((elem), (pred)->q_forw); \
    175 	REQUE((pred), elem); \
    176 }
    177 
    178 /* remque: remove_lines elem from circular queue */
    179 #define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw);
    180 
    181 /* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
    182 #define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
    183 
    184 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
    185 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
    186 
    187 #if defined(sun) && !defined(__SVR4)
    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 void add_line_node __P((line_t *));
    201 int append_lines __P((long));
    202 int apply_subst_template __P((char *, regmatch_t *, int, int));
    203 int build_active_list __P((int));
    204 int cbc_decode __P((char *, FILE *));
    205 int cbc_encode __P((char *, int, FILE *));
    206 int check_addr_range __P((long, long));
    207 void clear_active_list __P((void));
    208 void clear_undo_stack __P((void));
    209 int close_sbuf __P((void));
    210 int copy_lines __P((long));
    211 int delete_lines __P((long, long));
    212 void des_error __P((char *));
    213 int display_lines __P((long, long, int));
    214 line_t *dup_line_node __P((line_t *));
    215 int exec_command __P((void));
    216 long exec_global __P((int, int));
    217 void expand_des_key __P((char *, char *));
    218 int extract_addr_range __P((void));
    219 char *extract_pattern __P((int));
    220 int extract_subst_tail __P((int *, long *));
    221 char *extract_subst_template __P((void));
    222 int filter_lines __P((long, long, char *));
    223 int flush_des_file __P((FILE *));
    224 line_t *get_addressed_line_node __P((long));
    225 pattern_t *get_compiled_pattern __P((void));
    226 int get_des_char __P((FILE *));
    227 char *get_extended_line __P((int *, int));
    228 char *get_filename __P((void));
    229 int get_keyword __P((void));
    230 long get_line_node_addr __P((line_t *));
    231 long get_matching_node_addr __P((pattern_t *, int));
    232 long get_marked_node_addr __P((int));
    233 char *get_sbuf_line __P((line_t *));
    234 int get_shell_command __P((void));
    235 int get_stream_line __P((FILE *));
    236 int get_tty_line __P((void));
    237 void handle_hup __P((int));
    238 void handle_int __P((int));
    239 void handle_winch __P((int));
    240 int has_trailing_escape __P((char *, char *));
    241 int hex_to_binary __P((int, int));
    242 void init_buffers __P((void));
    243 void init_des_cipher __P((void));
    244 int is_legal_filename __P((char *));
    245 int join_lines __P((long, long));
    246 int mark_line_node __P((line_t *, int));
    247 int move_lines __P((long));
    248 line_t *next_active_node __P((void));
    249 long next_addr __P((void));
    250 int open_sbuf __P((void));
    251 char *parse_char_class __P((char *));
    252 int pop_undo_stack __P((void));
    253 undo_t *push_undo_stack __P((int, long, long));
    254 int put_des_char __P((int, FILE *));
    255 char *put_sbuf_line __P((char *));
    256 int put_stream_line __P((FILE *, char *, int));
    257 int put_tty_line __P((char *, int, long, int));
    258 void quit __P((int));
    259 long read_file __P((char *, long));
    260 long read_stream __P((FILE *, long));
    261 int search_and_replace __P((pattern_t *, int, int));
    262 int set_active_node __P((line_t *));
    263 void set_des_key __P((char *));
    264 void signal_hup __P((int));
    265 void signal_int __P((int));
    266 char *strip_escapes __P((char *));
    267 int substitute_matching_text __P((pattern_t *, line_t *, int, int));
    268 char *translit_text __P((char *, int, int, int));
    269 void unmark_line_node __P((line_t *));
    270 void unset_active_nodes __P((line_t *, line_t *));
    271 long write_file __P((char *, char *, long, long));
    272 long write_stream __P((FILE *, long, long));
    273 
    274 /* global buffers */
    275 extern char stdinbuf[];
    276 extern char *ibuf;
    277 extern char *ibufp;
    278 extern int ibufsz;
    279 
    280 /* global flags */
    281 extern int isbinary;
    282 extern int isglobal;
    283 extern int modified;
    284 extern int mutex;
    285 extern int sigflags;
    286 
    287 /* global vars */
    288 extern long addr_last;
    289 extern long current_addr;
    290 extern long first_addr;
    291 extern int lineno;
    292 extern long second_addr;
    293 extern long rows;
    294 extern int cols;
    295 extern int scripted;
    296 extern int ere;
    297 extern int des;
    298 extern int newline_added;	/* io.c */
    299 extern int patlock;
    300 extern char errmsg[];		/* re.c */
    301 extern long u_current_addr;	/* undo.c */
    302 extern long u_addr_last;	/* undo.c */
    303 #if defined(sun) && !defined(__SVR4)
    304 extern char *sys_errlist[];
    305 #endif
    306