xref: /dflybsd-src/contrib/nvi2/ex/ex.h (revision 07bc39c2f4bbca56f12568e06d89da17f2eeb965)
1e0b8e63eSJohn Marino /*-
2e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994
3e0b8e63eSJohn Marino  *	The Regents of the University of California.  All rights reserved.
4e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5e0b8e63eSJohn Marino  *	Keith Bostic.  All rights reserved.
6e0b8e63eSJohn Marino  *
7e0b8e63eSJohn Marino  * See the LICENSE file for redistribution information.
8e0b8e63eSJohn Marino  */
9e0b8e63eSJohn Marino 
10e0b8e63eSJohn Marino #define	PROMPTCHAR	':'		/* Prompt using a colon. */
11e0b8e63eSJohn Marino 
12e0b8e63eSJohn Marino typedef struct _excmdlist {		/* Ex command table structure. */
13e0b8e63eSJohn Marino 	CHAR_T *name;			/* Command name, underlying function. */
14e0b8e63eSJohn Marino 	int (*fn)(SCR *, EXCMD *);
15e0b8e63eSJohn Marino 
16e0b8e63eSJohn Marino #define	E_ADDR1		0x00000001	/* One address. */
17e0b8e63eSJohn Marino #define	E_ADDR2		0x00000002	/* Two addresses. */
18e0b8e63eSJohn Marino #define	E_ADDR2_ALL	0x00000004	/* Zero/two addresses; zero == all. */
19e0b8e63eSJohn Marino #define	E_ADDR2_NONE	0x00000008	/* Zero/two addresses; zero == none. */
20e0b8e63eSJohn Marino #define	E_ADDR_ZERO	0x00000010	/* 0 is a legal addr1. */
21e0b8e63eSJohn Marino #define	E_ADDR_ZERODEF	0x00000020	/* 0 is default addr1 of empty files. */
22e0b8e63eSJohn Marino #define	E_AUTOPRINT	0x00000040	/* Command always sets autoprint. */
23e0b8e63eSJohn Marino #define	E_CLRFLAG	0x00000080	/* Clear the print (#, l, p) flags. */
24e0b8e63eSJohn Marino #define	E_NEWSCREEN	0x00000100	/* Create a new screen. */
25e0b8e63eSJohn Marino #define	E_SECURE	0x00000200	/* Permission denied if O_SECURE set. */
26e0b8e63eSJohn Marino #define	E_VIONLY	0x00000400	/* Meaningful only in vi. */
27e0b8e63eSJohn Marino #define	__INUSE1	0xfffff800	/* Same name space as EX_PRIVATE. */
28e0b8e63eSJohn Marino 	u_int16_t flags;
29e0b8e63eSJohn Marino 
30e0b8e63eSJohn Marino 	char *syntax;			/* Syntax script. */
31e0b8e63eSJohn Marino 	char *usage;			/* Usage line. */
32e0b8e63eSJohn Marino 	char *help;			/* Help line. */
33e0b8e63eSJohn Marino } EXCMDLIST;
34e0b8e63eSJohn Marino 
35e0b8e63eSJohn Marino #define	MAXCMDNAMELEN	12		/* Longest command name. */
36e0b8e63eSJohn Marino extern EXCMDLIST const cmds[];		/* Table of ex commands. */
37e0b8e63eSJohn Marino 
38e0b8e63eSJohn Marino /*
39e0b8e63eSJohn Marino  * !!!
40e0b8e63eSJohn Marino  * QUOTING NOTE:
41e0b8e63eSJohn Marino  *
42e0b8e63eSJohn Marino  * Historically, .exrc files and EXINIT variables could only use ^V as an
43e0b8e63eSJohn Marino  * escape character, neither ^Q or a user specified character worked.  We
44e0b8e63eSJohn Marino  * enforce that here, just in case someone depends on it.
45e0b8e63eSJohn Marino  */
46e0b8e63eSJohn Marino #define	IS_ESCAPE(sp, cmdp, ch)						\
47e0b8e63eSJohn Marino 	(F_ISSET(cmdp, E_VLITONLY) ?					\
48e0b8e63eSJohn Marino 	    (ch) == CH_LITERAL : KEY_VAL(sp, ch) == K_VLNEXT)
49e0b8e63eSJohn Marino 
50e0b8e63eSJohn Marino #define	IS_SHELLMETA(sp, ch)						\
51e0b8e63eSJohn Marino 	((ch) <= CHAR_MAX && strchr(O_STR(sp, O_SHELLMETA), ch) != NULL)
52e0b8e63eSJohn Marino 
53e0b8e63eSJohn Marino /*
54e0b8e63eSJohn Marino  * File state must be checked for each command -- any ex command may be entered
55e0b8e63eSJohn Marino  * at any time, and most of them won't work well if a file hasn't yet been read
56e0b8e63eSJohn Marino  * in.  Historic vi generally took the easy way out and dropped core.
57e0b8e63eSJohn Marino  */
58e0b8e63eSJohn Marino #define	NEEDFILE(sp, cmdp) {						\
59e0b8e63eSJohn Marino 	if ((sp)->ep == NULL) {						\
60e0b8e63eSJohn Marino 		ex_wemsg(sp, (cmdp)->cmd->name, EXM_NOFILEYET);		\
61e0b8e63eSJohn Marino 		return (1);						\
62e0b8e63eSJohn Marino 	}								\
63e0b8e63eSJohn Marino }
64e0b8e63eSJohn Marino 
65e0b8e63eSJohn Marino /* Range structures for global and @ commands. */
66e0b8e63eSJohn Marino typedef struct _range RANGE;
67e0b8e63eSJohn Marino struct _range {				/* Global command range. */
68e0b8e63eSJohn Marino 	TAILQ_ENTRY(_range) q;		/* Linked list of ranges. */
69e0b8e63eSJohn Marino 	recno_t start, stop;		/* Start/stop of the range. */
70e0b8e63eSJohn Marino };
71e0b8e63eSJohn Marino 
72e0b8e63eSJohn Marino /* Ex command structure. */
73e0b8e63eSJohn Marino struct _excmd {
74e0b8e63eSJohn Marino 	SLIST_ENTRY(_excmd) q;		/* Linked list of commands. */
75e0b8e63eSJohn Marino 
76e0b8e63eSJohn Marino 	char	 *if_name;		/* Associated file. */
77e0b8e63eSJohn Marino 	recno_t	  if_lno;		/* Associated line number. */
78e0b8e63eSJohn Marino 
79e0b8e63eSJohn Marino 	/* Clear the structure for the ex parser. */
80e0b8e63eSJohn Marino #define	CLEAR_EX_PARSER(cmdp)						\
81e0b8e63eSJohn Marino 	memset(&((cmdp)->cp), 0, ((char *)&(cmdp)->flags -		\
82e0b8e63eSJohn Marino 	    (char *)&((cmdp)->cp)) + sizeof((cmdp)->flags))
83e0b8e63eSJohn Marino 
84e0b8e63eSJohn Marino 	CHAR_T	 *cp;			/* Current command text. */
85e0b8e63eSJohn Marino 	size_t	  clen;			/* Current command length. */
86e0b8e63eSJohn Marino 
87e0b8e63eSJohn Marino 	CHAR_T	 *save_cmd;		/* Remaining command. */
88e0b8e63eSJohn Marino 	size_t	  save_cmdlen;		/* Remaining command length. */
89e0b8e63eSJohn Marino 
90e0b8e63eSJohn Marino 	EXCMDLIST const *cmd;		/* Command: entry in command table. */
91e0b8e63eSJohn Marino 	EXCMDLIST rcmd;			/* Command: table entry/replacement. */
92e0b8e63eSJohn Marino 
93e0b8e63eSJohn Marino 	TAILQ_HEAD(_rh, _range) rq[1];	/* @/global range: linked list. */
94e0b8e63eSJohn Marino 	recno_t   range_lno;		/* @/global range: set line number. */
95e0b8e63eSJohn Marino 	CHAR_T	 *o_cp;			/* Original @/global command. */
96e0b8e63eSJohn Marino 	size_t	  o_clen;		/* Original @/global command length. */
97e0b8e63eSJohn Marino #define	AGV_AT		0x01		/* @ buffer execution. */
98e0b8e63eSJohn Marino #define	AGV_AT_NORANGE	0x02		/* @ buffer execution without range. */
99e0b8e63eSJohn Marino #define	AGV_GLOBAL	0x04		/* global command. */
100e0b8e63eSJohn Marino #define	AGV_V		0x08		/* v command. */
101e0b8e63eSJohn Marino #define	AGV_ALL		(AGV_AT | AGV_AT_NORANGE | AGV_GLOBAL | AGV_V)
102e0b8e63eSJohn Marino 	u_int8_t  agv_flags;
103e0b8e63eSJohn Marino 
104e0b8e63eSJohn Marino 	/* Clear the structure before each ex command. */
105e0b8e63eSJohn Marino #define	CLEAR_EX_CMD(cmdp) {						\
106e0b8e63eSJohn Marino 	u_int32_t L__f = F_ISSET(cmdp, E_PRESERVE);			\
107e0b8e63eSJohn Marino 	memset(&((cmdp)->buffer), 0, ((char *)&(cmdp)->flags -		\
108e0b8e63eSJohn Marino 	    (char *)&((cmdp)->buffer)) + sizeof((cmdp)->flags));	\
109e0b8e63eSJohn Marino 	F_SET(cmdp, L__f);						\
110e0b8e63eSJohn Marino }
111e0b8e63eSJohn Marino 
112e0b8e63eSJohn Marino 	CHAR_T	  buffer;		/* Command: named buffer. */
113e0b8e63eSJohn Marino 	recno_t	  lineno;		/* Command: line number. */
114e0b8e63eSJohn Marino 	long	  count;		/* Command: signed count. */
115e0b8e63eSJohn Marino 	long	  flagoff;		/* Command: signed flag offset. */
116e0b8e63eSJohn Marino 	int	  addrcnt;		/* Command: addresses (0, 1 or 2). */
117e0b8e63eSJohn Marino 	MARK	  addr1;		/* Command: 1st address. */
118e0b8e63eSJohn Marino 	MARK	  addr2;		/* Command: 2nd address. */
119e0b8e63eSJohn Marino 	ARGS	**argv;			/* Command: array of arguments. */
120e0b8e63eSJohn Marino 	int	  argc;			/* Command: count of arguments. */
121e0b8e63eSJohn Marino 
122e0b8e63eSJohn Marino #define	E_C_BUFFER	0x00001		/* Buffer name specified. */
123e0b8e63eSJohn Marino #define	E_C_CARAT	0x00002		/*  ^ flag. */
124e0b8e63eSJohn Marino #define	E_C_COUNT	0x00004		/* Count specified. */
125e0b8e63eSJohn Marino #define	E_C_COUNT_NEG	0x00008		/* Count was signed negative. */
126e0b8e63eSJohn Marino #define	E_C_COUNT_POS	0x00010		/* Count was signed positive. */
127e0b8e63eSJohn Marino #define	E_C_DASH	0x00020		/*  - flag. */
128e0b8e63eSJohn Marino #define	E_C_DOT		0x00040		/*  . flag. */
129e0b8e63eSJohn Marino #define	E_C_EQUAL	0x00080		/*  = flag. */
130e0b8e63eSJohn Marino #define	E_C_FORCE	0x00100		/*  ! flag. */
131e0b8e63eSJohn Marino #define	E_C_HASH	0x00200		/*  # flag. */
132e0b8e63eSJohn Marino #define	E_C_LIST	0x00400		/*  l flag. */
133e0b8e63eSJohn Marino #define	E_C_PLUS	0x00800		/*  + flag. */
134e0b8e63eSJohn Marino #define	E_C_PRINT	0x01000		/*  p flag. */
135e0b8e63eSJohn Marino 	u_int16_t iflags;		/* User input information. */
136e0b8e63eSJohn Marino 
137e0b8e63eSJohn Marino #define	__INUSE2	0x000007ff	/* Same name space as EXCMDLIST. */
138e0b8e63eSJohn Marino #define	E_BLIGNORE	0x00000800	/* Ignore blank lines. */
139e0b8e63eSJohn Marino #define	E_NAMEDISCARD	0x00001000	/* Free/discard the name. */
140e0b8e63eSJohn Marino #define	E_NOAUTO	0x00002000	/* Don't do autoprint output. */
141e0b8e63eSJohn Marino #define	E_NOPRDEF	0x00004000	/* Don't print as default. */
142e0b8e63eSJohn Marino #define	E_NRSEP		0x00008000	/* Need to line adjust ex output. */
143e0b8e63eSJohn Marino #define	E_OPTNUM	0x00010000	/* Number edit option affected. */
144e0b8e63eSJohn Marino #define	E_VLITONLY	0x00020000	/* Use ^V quoting only. */
145e0b8e63eSJohn Marino #define	E_PRESERVE	0x0003f800	/* Bits to preserve across commands. */
146e0b8e63eSJohn Marino 
147e0b8e63eSJohn Marino #define	E_ABSMARK	0x00040000	/* Set the absolute mark. */
148e0b8e63eSJohn Marino #define	E_ADDR_DEF	0x00080000	/* Default addresses used. */
149e0b8e63eSJohn Marino #define	E_DELTA		0x00100000	/* Search address with delta. */
150e0b8e63eSJohn Marino #define	E_MODIFY	0x00200000	/* File name expansion modified arg. */
151e0b8e63eSJohn Marino #define	E_MOVETOEND	0x00400000	/* Move to the end of the file first. */
152e0b8e63eSJohn Marino #define	E_NEWLINE	0x00800000	/* Found ending <newline>. */
153e0b8e63eSJohn Marino #define	E_SEARCH_WMSG	0x01000000	/* Display search-wrapped message. */
154e0b8e63eSJohn Marino #define	E_USELASTCMD	0x02000000	/* Use the last command. */
155e0b8e63eSJohn Marino #define	E_VISEARCH	0x04000000	/* It's really a vi search command. */
156e0b8e63eSJohn Marino 	u_int32_t flags;		/* Current flags. */
157e0b8e63eSJohn Marino };
158e0b8e63eSJohn Marino 
159e0b8e63eSJohn Marino /* Ex private, per-screen memory. */
160e0b8e63eSJohn Marino typedef struct _ex_private {
161e0b8e63eSJohn Marino 					/* Tag file list. */
162e0b8e63eSJohn Marino 	TAILQ_HEAD(_tagfh, _tagf) tagfq[1];
163e0b8e63eSJohn Marino 	TAILQ_HEAD(_tqh, _tagq) tq[1];	/* Tag queue. */
164e0b8e63eSJohn Marino 	SLIST_HEAD(_csch, _csc) cscq[1];/* Cscope connection list. */
165e0b8e63eSJohn Marino 	CHAR_T	*tag_last;		/* Saved last tag string. */
166e0b8e63eSJohn Marino 
167e0b8e63eSJohn Marino 	CHAR_T	*lastbcomm;		/* Last bang command. */
168e0b8e63eSJohn Marino 
169e0b8e63eSJohn Marino 	ARGS   **args;			/* Command: argument list. */
170e0b8e63eSJohn Marino 	int	 argscnt;		/* Command: argument list count. */
171e0b8e63eSJohn Marino 	int	 argsoff;		/* Command: offset into arguments. */
172e0b8e63eSJohn Marino 
173e0b8e63eSJohn Marino 	u_int32_t fdef;			/* Saved E_C_* default command flags. */
174e0b8e63eSJohn Marino 
175e0b8e63eSJohn Marino 	char	*ibp;			/* File line input buffer. */
176e0b8e63eSJohn Marino 	size_t	 ibp_len;		/* File line input buffer length. */
177e0b8e63eSJohn Marino 	CONVWIN	 ibcw;			/* File line input conversion buffer. */
178e0b8e63eSJohn Marino 
179e0b8e63eSJohn Marino 	/*
180e0b8e63eSJohn Marino 	 * Buffers for the ex output.  The screen/vi support doesn't do any
181e0b8e63eSJohn Marino 	 * character buffering of any kind.  We do it here so that we're not
182e0b8e63eSJohn Marino 	 * calling the screen output routines on every character.
183e0b8e63eSJohn Marino 	 *
184e0b8e63eSJohn Marino 	 * XXX
185e0b8e63eSJohn Marino 	 * Change to grow dynamically.
186e0b8e63eSJohn Marino 	 */
187e0b8e63eSJohn Marino 	char	 obp[1024];		/* Ex output buffer. */
188e0b8e63eSJohn Marino 	size_t	 obp_len;		/* Ex output buffer length. */
189e0b8e63eSJohn Marino 
190e0b8e63eSJohn Marino #define	EXP_CSCINIT	0x01		/* Cscope initialized. */
191e0b8e63eSJohn Marino 	u_int8_t flags;
192e0b8e63eSJohn Marino } EX_PRIVATE;
193e0b8e63eSJohn Marino #define	EXP(sp)	((EX_PRIVATE *)((sp)->ex_private))
194e0b8e63eSJohn Marino 
195e0b8e63eSJohn Marino /*
196e0b8e63eSJohn Marino  * Filter actions:
197e0b8e63eSJohn Marino  *
198e0b8e63eSJohn Marino  *	FILTER_BANG	!:	filter text through the utility.
199e0b8e63eSJohn Marino  *	FILTER_RBANG	!:	read from the utility (without stdin).
200e0b8e63eSJohn Marino  *	FILTER_READ	read:	read from the utility (with stdin).
201e0b8e63eSJohn Marino  *	FILTER_WRITE	write:	write to the utility, display its output.
202e0b8e63eSJohn Marino  */
203e0b8e63eSJohn Marino enum filtertype { FILTER_BANG, FILTER_RBANG, FILTER_READ, FILTER_WRITE };
204e0b8e63eSJohn Marino 
205e0b8e63eSJohn Marino /* Ex common error messages. */
206e0b8e63eSJohn Marino typedef enum {
207e0b8e63eSJohn Marino 	EXM_EMPTYBUF,			/* Empty buffer. */
208e0b8e63eSJohn Marino 	EXM_FILECOUNT,			/* Too many file names. */
209e0b8e63eSJohn Marino 	EXM_NOCANON,			/* No terminal interface. */
210e0b8e63eSJohn Marino 	EXM_NOCANON_F,			/* EXM_NOCANO: filter version. */
211e0b8e63eSJohn Marino 	EXM_NOFILEYET,			/* Illegal until a file read in. */
212e0b8e63eSJohn Marino 	EXM_NOPREVBUF,			/* No previous buffer specified. */
213e0b8e63eSJohn Marino 	EXM_NOPREVRE,			/* No previous RE specified. */
214e0b8e63eSJohn Marino 	EXM_NOSUSPEND,			/* No suspension. */
215e0b8e63eSJohn Marino 	EXM_SECURE,			/* Illegal if secure edit option set. */
216e0b8e63eSJohn Marino 	EXM_SECURE_F,			/* EXM_SECURE: filter version */
217e0b8e63eSJohn Marino 	EXM_USAGE			/* Standard usage message. */
218e0b8e63eSJohn Marino } exm_t;
219e0b8e63eSJohn Marino 
220e0b8e63eSJohn Marino /* Ex address error types. */
221e0b8e63eSJohn Marino enum badaddr { A_COMBO, A_EMPTY, A_EOF, A_NOTSET, A_ZERO };
222e0b8e63eSJohn Marino 
223e0b8e63eSJohn Marino /* Ex common tag error messages. */
224e0b8e63eSJohn Marino typedef enum {
225e0b8e63eSJohn Marino 	TAG_BADLNO,		/* Tag line doesn't exist. */
226e0b8e63eSJohn Marino 	TAG_EMPTY,		/* Tags stack is empty. */
227e0b8e63eSJohn Marino 	TAG_SEARCH		/* Tags search pattern wasn't found. */
228e0b8e63eSJohn Marino } tagmsg_t;
229e0b8e63eSJohn Marino 
230e0b8e63eSJohn Marino #include "ex_def.h"
231*c5dc3490SJohn Marino #include "ex_extern.h"
232