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