116Salm/* ed.h: type and constant definitions for the ed editor. */
2139969Simp/*-
31057Salm * Copyright (c) 1993 Andrew Moore
416Salm * All rights reserved.
516Salm *
616Salm * Redistribution and use in source and binary forms, with or without
716Salm * modification, are permitted provided that the following conditions
816Salm * are met:
916Salm * 1. Redistributions of source code must retain the above copyright
1016Salm *    notice, this list of conditions and the following disclaimer.
1116Salm * 2. Redistributions in binary form must reproduce the above copyright
1216Salm *    notice, this list of conditions and the following disclaimer in the
1316Salm *    documentation and/or other materials provided with the distribution.
1416Salm *
1516Salm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1616Salm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1716Salm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1816Salm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1916Salm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2016Salm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2116Salm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2216Salm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2316Salm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2416Salm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2516Salm * SUCH DAMAGE.
2616Salm *
271297Salm *	@(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp
2850471Speter * $FreeBSD$
2916Salm */
3016Salm
3177407Simp#include <sys/param.h>
321057Salm#include <errno.h>
3377407Simp#include <limits.h>
3416Salm#include <regex.h>
3516Salm#include <signal.h>
361057Salm#include <stdio.h>
371057Salm#include <stdlib.h>
381057Salm#include <string.h>
391057Salm#include <unistd.h>
4016Salm
4116Salm#define ERR		(-2)
4216Salm#define EMOD		(-3)
4316Salm#define FATAL		(-4)
4416Salm
4516Salm#define MINBUFSZ 512		/* minimum buffer size - must be > 0 */
4616Salm#define SE_MAX 30		/* max subexpressions in a regular expression */
471057Salm#ifdef INT_MAX
481057Salm# define LINECHARS INT_MAX	/* max chars per line */
491057Salm#else
501057Salm# define LINECHARS MAXINT	/* max chars per line */
511057Salm#endif
5216Salm
531057Salm/* gflags */
541057Salm#define GLB 001		/* global command */
551057Salm#define GPR 002		/* print after command */
561057Salm#define GLS 004		/* list after command */
571057Salm#define GNP 010		/* enumerate after command */
581057Salm#define GSG 020		/* global substitute */
591057Salm
6016Salmtypedef regex_t pattern_t;
6116Salm
6216Salm/* Line node */
6316Salmtypedef struct	line {
641057Salm	struct line	*q_forw;
651057Salm	struct line	*q_back;
6616Salm	off_t		seek;		/* address of line in scratch buffer */
6716Salm	int		len;		/* length of line */
6816Salm} line_t;
6916Salm
7016Salm
7116Salmtypedef struct undo {
7216Salm
7316Salm/* type of undo nodes */
7416Salm#define UADD	0
7516Salm#define UDEL 	1
7616Salm#define UMOV	2
7716Salm#define VMOV	3
7816Salm
7916Salm	int type;			/* command type */
8016Salm	line_t	*h;			/* head of list */
8116Salm	line_t  *t;			/* tail of list */
8216Salm} undo_t;
8316Salm
8416Salm#ifndef max
8516Salm# define max(a,b) ((a) > (b) ? (a) : (b))
8616Salm#endif
8716Salm#ifndef min
8816Salm# define min(a,b) ((a) < (b) ? (a) : (b))
8916Salm#endif
9016Salm
911057Salm#define INC_MOD(l, k)	((l) + 1 > (k) ? 0 : (l) + 1)
921057Salm#define DEC_MOD(l, k)	((l) - 1 < 0 ? (k) : (l) - 1)
9316Salm
941057Salm/* SPL1: disable some interrupts (requires reliable signals) */
951057Salm#define SPL1() mutex++
9616Salm
971057Salm/* SPL0: enable all interrupts; check sigflags (requires reliable signals) */
981057Salm#define SPL0() \
9916Salmif (--mutex == 0) { \
1001057Salm	if (sigflags & (1 << (SIGHUP - 1))) handle_hup(SIGHUP); \
1011057Salm	if (sigflags & (1 << (SIGINT - 1))) handle_int(SIGINT); \
10216Salm}
10316Salm
1041057Salm/* STRTOL: convert a string to long */
1051057Salm#define STRTOL(i, p) { \
1061057Salm	if (((i = strtol(p, &p, 10)) == LONG_MIN || i == LONG_MAX) && \
1071057Salm	    errno == ERANGE) { \
10881220Smike		errmsg = "number out of range"; \
1091057Salm	    	i = 0; \
1101057Salm		return ERR; \
1111057Salm	} \
1121057Salm}
1131057Salm
11416Salm#if defined(sun) || defined(NO_REALLOC_NULL)
1151057Salm/* REALLOC: assure at least a minimum size for buffer b */
1161057Salm#define REALLOC(b,n,i,err) \
11716Salmif ((i) > (n)) { \
11816Salm	int ti = (n); \
11916Salm	char *ts; \
1201057Salm	SPL1(); \
12116Salm	if ((b) != NULL) { \
12216Salm		if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
12316Salm			fprintf(stderr, "%s\n", strerror(errno)); \
12481220Smike			errmsg = "out of memory"; \
1251057Salm			SPL0(); \
12616Salm			return err; \
12716Salm		} \
12816Salm	} else { \
12916Salm		if ((ts = (char *) malloc(ti += max((i), MINBUFSZ))) == NULL) { \
13016Salm			fprintf(stderr, "%s\n", strerror(errno)); \
13181220Smike			errmsg = "out of memory"; \
1321057Salm			SPL0(); \
13316Salm			return err; \
13416Salm		} \
13516Salm	} \
13616Salm	(n) = ti; \
13716Salm	(b) = ts; \
1381057Salm	SPL0(); \
13916Salm}
14016Salm#else /* NO_REALLOC_NULL */
1411057Salm/* REALLOC: assure at least a minimum size for buffer b */
1421057Salm#define REALLOC(b,n,i,err) \
14316Salmif ((i) > (n)) { \
14416Salm	int ti = (n); \
14516Salm	char *ts; \
1461057Salm	SPL1(); \
14716Salm	if ((ts = (char *) realloc((b), ti += max((i), MINBUFSZ))) == NULL) { \
14816Salm		fprintf(stderr, "%s\n", strerror(errno)); \
14981220Smike		errmsg = "out of memory"; \
1501057Salm		SPL0(); \
15116Salm		return err; \
15216Salm	} \
15316Salm	(n) = ti; \
15416Salm	(b) = ts; \
1551057Salm	SPL0(); \
15616Salm}
15716Salm#endif /* NO_REALLOC_NULL */
15816Salm
1591057Salm/* REQUE: link pred before succ */
1601057Salm#define REQUE(pred, succ) (pred)->q_forw = (succ), (succ)->q_back = (pred)
16116Salm
1621297Salm/* INSQUE: insert elem in circular queue after pred */
1631297Salm#define INSQUE(elem, pred) \
16416Salm{ \
1651057Salm	REQUE((elem), (pred)->q_forw); \
1661057Salm	REQUE((pred), elem); \
16716Salm}
16816Salm
1691297Salm/* REMQUE: remove_lines elem from circular queue */
1701297Salm#define REMQUE(elem) REQUE((elem)->q_back, (elem)->q_forw);
17116Salm
1721057Salm/* NUL_TO_NEWLINE: overwrite ASCII NULs with newlines */
1731057Salm#define NUL_TO_NEWLINE(s, l) translit_text(s, l, '\0', '\n')
17416Salm
1751057Salm/* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
1761057Salm#define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
17716Salm
178115717Smarkm#ifdef ED_DES_INCLUDES
179115717Smarkmvoid des_error(const char *);
180115717Smarkmvoid expand_des_key(char *, char *);
181115717Smarkmvoid set_des_key(DES_cblock *);
182115717Smarkm#endif
183115717Smarkm
184115717Smarkm/* Other DES support stuff */
185115717Smarkmvoid init_des_cipher(void);
186115717Smarkmint flush_des_file(FILE *);
187115717Smarkmint get_des_char(FILE *);
188115717Smarkmint put_des_char(int, FILE *);
189115717Smarkm
1901057Salm/* Local Function Declarations */
19190109Simpvoid add_line_node(line_t *);
19290109Simpint append_lines(long);
19390109Simpint apply_subst_template(const char *, regmatch_t *, int, int);
19490109Simpint build_active_list(int);
195101093Smarkmint cbc_decode(unsigned char *, FILE *);
196101093Smarkmint cbc_encode(unsigned char *, int, FILE *);
19790109Simpint check_addr_range(long, long);
19890109Simpvoid clear_active_list(void);
19990109Simpvoid clear_undo_stack(void);
20090109Simpint close_sbuf(void);
20190109Simpint copy_lines(long);
20290109Simpint delete_lines(long, long);
20390109Simpint display_lines(long, long, int);
20490109Simpline_t *dup_line_node(line_t *);
20590109Simpint exec_command(void);
20690109Simplong exec_global(int, int);
20790109Simpint extract_addr_range(void);
20890109Simpchar *extract_pattern(int);
20990109Simpint extract_subst_tail(int *, long *);
21090109Simpchar *extract_subst_template(void);
21190109Simpint filter_lines(long, long, char *);
21290109Simpline_t *get_addressed_line_node(long);
21390109Simppattern_t *get_compiled_pattern(void);
21490109Simpchar *get_extended_line(int *, int);
21590109Simpchar *get_filename(void);
21690109Simpint get_keyword(void);
21790109Simplong get_line_node_addr(line_t *);
21890109Simplong get_matching_node_addr(pattern_t *, int);
21990109Simplong get_marked_node_addr(int);
22090109Simpchar *get_sbuf_line(line_t *);
22190109Simpint get_shell_command(void);
22290109Simpint get_stream_line(FILE *);
22390109Simpint get_tty_line(void);
22490109Simpvoid handle_hup(int);
22590109Simpvoid handle_int(int);
22690109Simpvoid handle_winch(int);
22790109Simpint has_trailing_escape(char *, char *);
22890109Simpint hex_to_binary(int, int);
22990109Simpvoid init_buffers(void);
23090109Simpint is_legal_filename(char *);
23190109Simpint join_lines(long, long);
23290109Simpint mark_line_node(line_t *, int);
23390109Simpint move_lines(long);
23490109Simpline_t *next_active_node(void);
23590109Simplong next_addr(void);
23690109Simpint open_sbuf(void);
23790109Simpchar *parse_char_class(char *);
23890109Simpint pop_undo_stack(void);
23990109Simpundo_t *push_undo_stack(int, long, long);
24090109Simpconst char *put_sbuf_line(const char *);
24190109Simpint put_stream_line(FILE *, const char *, int);
24290109Simpint put_tty_line(const char *, int, long, int);
24390109Simpvoid quit(int);
24490109Simplong read_file(char *, long);
24590109Simplong read_stream(FILE *, long);
24690109Simpint search_and_replace(pattern_t *, int, int);
24790109Simpint set_active_node(line_t *);
24890109Simpvoid signal_hup(int);
24990109Simpvoid signal_int(int);
25090109Simpchar *strip_escapes(char *);
25190109Simpint substitute_matching_text(pattern_t *, line_t *, int, int);
25290109Simpchar *translit_text(char *, int, int, int);
25390109Simpvoid unmark_line_node(line_t *);
25490109Simpvoid unset_active_nodes(line_t *, line_t *);
25590109Simplong write_file(char *, const char *, long, long);
25690109Simplong write_stream(FILE *, long, long);
25716Salm
2581057Salm/* global buffers */
2591057Salmextern char stdinbuf[];
2601057Salmextern char *ibuf;
2611057Salmextern char *ibufp;
2621057Salmextern int ibufsz;
26316Salm
2641057Salm/* global flags */
2651057Salmextern int isbinary;
2661057Salmextern int isglobal;
2671057Salmextern int modified;
26816Salmextern int mutex;
26916Salmextern int sigflags;
2701057Salm
2711057Salm/* global vars */
2721057Salmextern long addr_last;
2731057Salmextern long current_addr;
27481220Smikeextern const char *errmsg;
2751057Salmextern long first_addr;
2761057Salmextern int lineno;
2771057Salmextern long second_addr;
27898465Sjmallettextern long u_addr_last;
27998465Sjmallettextern long u_current_addr;
280241737Sedextern long rows;
281241737Sedextern int cols;
282241737Sedextern int newline_added;
283241737Sedextern int des;
284241737Sedextern int scripted;
285241737Sedextern int patlock;
286