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