xref: /original-bsd/usr.bin/ex/ex.h (revision 975cc92f)
1 /* Copyright (c) 1981 Regents of the University of California */
2 /* sccs id:	@(#)ex.h	7.2	07/26/81  */
3 #ifdef V6
4 #include <retrofit.h>
5 #endif
6 
7 /*
8  * Ex version 3 (see exact version in ex_cmds.c, search for /Version/)
9  *
10  * Mark Horton, UC Berkeley
11  * Bill Joy, UC Berkeley
12  * November 1979
13  *
14  * This file contains most of the declarations common to a large number
15  * of routines.  The file ex_vis.h contains declarations
16  * which are used only inside the screen editor.
17  * The file ex_tune.h contains parameters which can be diddled per installation.
18  *
19  * The declarations relating to the argument list, regular expressions,
20  * the temporary file data structure used by the editor
21  * and the data describing terminals are each fairly substantial and
22  * are kept in the files ex_{argv,re,temp,tty}.h which
23  * we #include separately.
24  *
25  * If you are going to dig into ex, you should look at the outline of the
26  * distribution of the code into files at the beginning of ex.c and ex_v.c.
27  * Code which is similar to that of ed is lightly or undocumented in spots
28  * (e.g. the regular expression code).  Newer code (e.g. open and visual)
29  * is much more carefully documented, and still rough in spots.
30  *
31  * Please forward bug reports to
32  *
33  *	Mark Horton
34  *	Computer Science Division, EECS
35  *	EVANS HALL
36  *	U.C. Berkeley 94704
37  *	(415) 642-4948
38  *	(415) 642-1024 (dept. office)
39  *
40  * or to csvax.mark@berkeley on the ARPA-net.  I would particularly like to hear
41  * of additional terminal descriptions you add to the termcap data base.
42  */
43 
44 #include <sys/types.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <signal.h>
48 #include <setjmp.h>
49 #include <sys/stat.h>
50 
51 #ifndef var
52 #define var	var
53 #endif
54 /*
55  *	The following little dance copes with the new USG tty handling.
56  *	This stuff has the advantage of considerable flexibility, and
57  *	the disadvantage of being incompatible with anything else.
58  *	The presence of the symbol USG3TTY will indicate the new code:
59  *	in this case, we define CBREAK (because we can simulate it exactly),
60  *	but we won't actually use it, so we set it to a value that will
61  *	probably blow the compilation if we goof up.
62  */
63 #ifdef USG3TTY
64 #include <termio.h>
65 #define CBREAK xxxxx
66 #else
67 #include <sgtty.h>
68 #endif
69 
70 extern	int errno;
71 
72 #ifndef VMUNIX
73 typedef	short	line;
74 #else
75 typedef	int	line;
76 #endif
77 typedef	short	bool;
78 
79 #include "ex_tune.h"
80 #include "ex_vars.h"
81 /*
82  * Options in the editor are referred to usually by "value(name)" where
83  * name is all uppercase, i.e. "value(PROMPT)".  This is actually a macro
84  * which expands to a fixed field in a static structure and so generates
85  * very little code.  The offsets for the option names in the structure
86  * are generated automagically from the structure initializing them in
87  * ex_data.c... see the shell script "makeoptions".
88  */
89 struct	option {
90 	char	*oname;
91 	char	*oabbrev;
92 	short	otype;		/* Types -- see below */
93 	short	odefault;	/* Default value */
94 	short	ovalue;		/* Current value */
95 	char	*osvalue;
96 };
97 
98 #define	ONOFF	0
99 #define	NUMERIC	1
100 #define	STRING	2		/* SHELL or DIRECTORY */
101 #define	OTERM	3
102 
103 #define	value(a)	options[a].ovalue
104 #define	svalue(a)	options[a].osvalue
105 
106 extern	 struct	option options[NOPTS + 1];
107 
108 
109 /*
110  * The editor does not normally use the standard i/o library.  Because
111  * we expect the editor to be a heavily used program and because it
112  * does a substantial amount of input/output processing it is appropriate
113  * for it to call low level read/write primitives directly.  In fact,
114  * when debugging the editor we use the standard i/o library.  In any
115  * case the editor needs a printf which prints through "putchar" ala the
116  * old version 6 printf.  Thus we normally steal a copy of the "printf.c"
117  * and "strout" code from the standard i/o library and mung it for our
118  * purposes to avoid dragging in the stdio library headers, etc if we
119  * are not debugging.  Such a modified printf exists in "printf.c" here.
120  */
121 #ifdef TRACE
122 #	include <stdio.h>
123 	var	FILE	*trace;
124 	var	bool	trubble;
125 	var	bool	techoin;
126 	var	char	tracbuf[BUFSIZ];
127 #	undef	putchar
128 #	undef	getchar
129 #else
130 # ifdef	VMUNIX
131 #	define	BUFSIZ	1024
132 # else
133 #	define	BUFSIZ	512
134 # endif
135 #	define	NULL	0
136 #	define	EOF	-1
137 #endif
138 
139 /*
140  * Character constants and bits
141  *
142  * The editor uses the QUOTE bit as a flag to pass on with characters
143  * e.g. to the putchar routine.  The editor never uses a simple char variable.
144  * Only arrays of and pointers to characters are used and parameters and
145  * registers are never declared character.
146  */
147 #define	QUOTE	0200
148 #define	TRIM	0177
149 #define	CTRL(c)	('c' & 037)
150 #define	NL	CTRL(j)
151 #define	CR	CTRL(m)
152 #define	DELETE	0177		/* See also ATTN, QUIT in ex_tune.h */
153 #define	ESCAPE	033
154 
155 /*
156  * Miscellaneous random variables used in more than one place
157  */
158 var	bool	aiflag;		/* Append/change/insert with autoindent */
159 var	bool	anymarks;	/* We have used '[a-z] */
160 var	int	chng;		/* Warn "No write" */
161 var	char	*Command;
162 var	short	defwind;	/* -w# change default window size */
163 var	int	dirtcnt;	/* When >= MAXDIRT, should sync temporary */
164 #ifdef TIOCLGET
165 var	bool	dosusp;		/* Do SIGTSTP in visual when ^Z typed */
166 #endif
167 var	bool	edited;		/* Current file is [Edited] */
168 var	line	*endcore;	/* Last available core location */
169 extern	 bool	endline;	/* Last cmd mode command ended with \n */
170 #ifndef VMUNIX
171 var	short	erfile;		/* Error message file unit */
172 #endif
173 var	line	*fendcore;	/* First address in line pointer space */
174 var	char	file[FNSIZE];	/* Working file name */
175 var	char	genbuf[LBSIZE];	/* Working buffer when manipulating linebuf */
176 var	bool	hush;		/* Command line option - was given, hush up! */
177 var	char	*globp;		/* (Untyped) input string to command mode */
178 var	bool	holdcm;		/* Don't cursor address */
179 var	bool	inappend;	/* in ex command append mode */
180 var	bool	inglobal;	/* Inside g//... or v//... */
181 var	char	*initev;	/* Initial : escape for visual */
182 var	bool	inopen;		/* Inside open or visual */
183 var	char	*input;		/* Current position in cmd line input buffer */
184 var	bool	intty;		/* Input is a tty */
185 var	short	io;		/* General i/o unit (auto-closed on error!) */
186 extern	 short	lastc;		/* Last character ret'd from cmd input */
187 var	bool	laste;		/* Last command was an "e" (or "rec") */
188 var	char	lastmac;	/* Last macro called for ** */
189 var	char	lasttag[TAGSIZE];	/* Last argument to a tag command */
190 var	char	*linebp;	/* Used in substituting in \n */
191 var	char	linebuf[LBSIZE];	/* The primary line buffer */
192 var	bool	listf;		/* Command should run in list mode */
193 var	char	*loc1;		/* Where re began to match (in linebuf) */
194 var	char	*loc2;		/* First char after re match (") */
195 var	line	names['z'-'a'+2];	/* Mark registers a-z,' */
196 var	int	notecnt;	/* Count for notify (to visual from cmd) */
197 var	bool	numberf;	/* Command should run in number mode */
198 var	char	obuf[BUFSIZ];	/* Buffer for tty output */
199 var	short	oprompt;	/* Saved during source */
200 extern	short	ospeed;		/* Output speed (from gtty) */
201 var	int	otchng;		/* Backup tchng to find changes in macros */
202 var	short	peekc;		/* Peek ahead character (cmd mode input) */
203 var	char	*pkill[2];	/* Trim for put with ragged (LISP) delete */
204 var	bool	pfast;		/* Have stty -nl'ed to go faster */
205 var	int	pid;		/* Process id of child */
206 var	int	ppid;		/* Process id of parent (e.g. main ex proc) */
207 var	jmp_buf	resetlab;	/* For error throws to top level (cmd mode) */
208 var	int	rpid;		/* Pid returned from wait() */
209 var	bool	ruptible;	/* Interruptible is normal state */
210 var	bool	seenprompt;	/* 1 if have gotten user input */
211 var	bool	shudclob;	/* Have a prompt to clobber (e.g. on ^D) */
212 var	int	status;		/* Status returned from wait() */
213 var	int	tchng;		/* If nonzero, then [Modified] */
214 extern	short	tfile;		/* Temporary file unit */
215 var	bool	vcatch;		/* Want to catch an error (open/visual) */
216 var	jmp_buf	vreslab;	/* For error throws to a visual catch */
217 var	bool	writing;	/* 1 if in middle of a file write */
218 var	int	xchng;		/* Suppresses multiple "No writes" in !cmd */
219 
220 /*
221  * Macros
222  */
223 #define	CP(a, b)	(ignore(strcpy(a, b)))
224 			/*
225 			 * FIXUNDO: do we want to mung undo vars?
226 			 * Usually yes unless in a macro or global.
227 			 */
228 #define FIXUNDO		(inopen >= 0 && (inopen || !inglobal))
229 #define ckaw()		{if (chng && value(AUTOWRITE)) wop(0);}
230 #define	copy(a,b,c)	Copy((char *) a, (char *) b, c)
231 #define	eq(a, b)	((a) && (b) && strcmp(a, b) == 0)
232 #define	getexit(a)	copy(a, resetlab, sizeof (jmp_buf))
233 #define	lastchar()	lastc
234 #define	outchar(c)	(*Outchar)(c)
235 #define	pastwh()	(ignore(skipwh()))
236 #define	pline(no)	(*Pline)(no)
237 #define	reset()		longjmp(resetlab,1)
238 #define	resexit(a)	copy(resetlab, a, sizeof (jmp_buf))
239 #define	setexit()	setjmp(resetlab)
240 #define	setlastchar(c)	lastc = c
241 #define	ungetchar(c)	peekc = c
242 
243 #define	CATCH		vcatch = 1; if (setjmp(vreslab) == 0) {
244 #define	ONERR		} else { vcatch = 0;
245 #define	ENDCATCH	} vcatch = 0;
246 
247 /*
248  * Environment like memory
249  */
250 var	char	altfile[FNSIZE];	/* Alternate file name */
251 extern	 char	direct[ONMSZ];		/* Temp file goes here */
252 extern	 char	shell[ONMSZ];		/* Copied to be settable */
253 extern	 char	ttytype[ONMSZ];		/* A long and pretty name */
254 var	char	uxb[UXBSIZE + 2];	/* Last !command for !! */
255 
256 /*
257  * The editor data structure for accessing the current file consists
258  * of an incore array of pointers into the temporary file tfile.
259  * Each pointer is 15 bits (the low bit is used by global) and is
260  * padded with zeroes to make an index into the temp file where the
261  * actual text of the line is stored.
262  *
263  * To effect undo, copies of affected lines are saved after the last
264  * line considered to be in the buffer, between dol and unddol.
265  * During an open or visual, which uses the command mode undo between
266  * dol and unddol, a copy of the entire, pre-command buffer state
267  * is saved between unddol and truedol.
268  */
269 var	line	*addr1;			/* First addressed line in a command */
270 var	line	*addr2;			/* Second addressed line */
271 var	line	*dol;			/* Last line in buffer */
272 var	line	*dot;			/* Current line */
273 var	line	*one;			/* First line */
274 var	line	*truedol;		/* End of all lines, including saves */
275 var	line	*unddol;		/* End of undo saved lines */
276 var	line	*zero;			/* Points to empty slot before one */
277 
278 /*
279  * Undo information
280  *
281  * For most commands we save lines changed by salting them away between
282  * dol and unddol before they are changed (i.e. we save the descriptors
283  * into the temp file tfile which is never garbage collected).  The
284  * lines put here go back after unddel, and to complete the undo
285  * we delete the lines [undap1,undap2).
286  *
287  * Undoing a move is much easier and we treat this as a special case.
288  * Similarly undoing a "put" is a special case for although there
289  * are lines saved between dol and unddol we don't stick these back
290  * into the buffer.
291  */
292 var	short	undkind;
293 
294 var	line	*unddel;	/* Saved deleted lines go after here */
295 var	line	*undap1;	/* Beginning of new lines */
296 var	line	*undap2;	/* New lines end before undap2 */
297 var	line	*undadot;	/* If we saved all lines, dot reverts here */
298 
299 #define	UNDCHANGE	0
300 #define	UNDMOVE		1
301 #define	UNDALL		2
302 #define	UNDNONE		3
303 #define	UNDPUT		4
304 
305 #ifdef CRYPT
306 /*
307  * Various miscellaneous flags and buffers needed by the encryption routines.
308  */
309 #define	KSIZE   9       /* key size for encryption */
310 #define	KEYPROMPT       "Key: "
311 var	int	xflag;		/* True if we are in encryption mode */
312 var	int	xtflag;		/* True if the temp file is being encrypted */
313 var	int	kflag;		/* True if the key has been accepted */
314 var	char	perm[768];
315 var	char	tperm[768];
316 var	char	*key;
317 var	char	crbuf[CRSIZE];
318 char	*getpass();
319 #endif
320 
321 /*
322  * Function type definitions
323  */
324 #define	NOSTR	(char *) 0
325 #define	NOLINE	(line *) 0
326 
327 extern	int	(*Outchar)();
328 extern	int	(*Pline)();
329 extern	int	(*Putchar)();
330 var	int	(*oldhup)();
331 int	(*setlist())();
332 int	(*setnorm())();
333 int	(*setnorm())();
334 int	(*setnumb())();
335 line	*address();
336 char	*cgoto();
337 char	*genindent();
338 char	*getblock();
339 char	*getenv();
340 line	*getmark();
341 char	*longname();
342 char	*mesg();
343 char	*place();
344 char	*plural();
345 line	*scanfor();
346 line	*setin();
347 char	*strcat();
348 char	*strcpy();
349 char	*strend();
350 char	*tailpath();
351 char	*tgetstr();
352 char	*tgoto();
353 char	*ttyname();
354 line	*vback();
355 char	*vfindcol();
356 char	*vgetline();
357 char	*vinit();
358 char	*vpastwh();
359 char	*vskipwh();
360 int	put();
361 int	putreg();
362 int	YANKreg();
363 int	delete();
364 int	execl();
365 int	filter();
366 int	getfile();
367 int	getsub();
368 int	gettty();
369 int	join();
370 int	listchar();
371 off_t	lseek();
372 int	normchar();
373 int	normline();
374 int	numbline();
375 var	int	(*oldquit)();
376 int	onhup();
377 int	onintr();
378 int	onsusp();
379 int	putch();
380 int	shift();
381 int	termchar();
382 int	vfilter();
383 #ifdef CBREAK
384 int	vintr();
385 #endif
386 int	vputch();
387 int	vshftop();
388 int	yank();
389 
390 /*
391  * C doesn't have a (void) cast, so we have to fake it for lint's sake.
392  */
393 #ifdef lint
394 #	define	ignore(a)	Ignore((char *) (a))
395 #	define	ignorf(a)	Ignorf((int (*) ()) (a))
396 #else
397 #	define	ignore(a)	a
398 #	define	ignorf(a)	a
399 #endif
400