xref: /titanic_50/usr/src/cmd/vi/port/ex.h (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /* Copyright (c) 1981 Regents of the University of California */
27 
28 /*
29  * Copyright (c) 2000 by Sun Microsystems, Inc.
30  * All rights reserved.
31  */
32 
33 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.34	*/
34 /*
35  * This file contains most of the declarations common to a large number
36  * of routines.  The file ex_vis.h contains declarations
37  * which are used only inside the screen editor.
38  * The file ex_tune.h contains parameters which can be diddled per installation.
39  *
40  * The declarations relating to the argument list, regular expressions,
41  * the temporary file data structure used by the editor
42  * and the data describing terminals are each fairly substantial and
43  * are kept in the files ex_{argv,re,temp,tty}.h which
44  * we #include separately.
45  *
46  * If you are going to dig into ex, you should look at the outline of the
47  * distribution of the code into files at the beginning of ex.c and ex_v.c.
48  * Code which is similar to that of ed is lightly or undocumented in spots
49  * (e.g. the regular expression code).  Newer code (e.g. open and visual)
50  * is much more carefully documented, and still rough in spots.
51  *
52  */
53 #ifdef UCBV7
54 #include <whoami.h>
55 #endif
56 #include <sys/types.h>
57 #include <ctype.h>
58 #include <errno.h>
59 #include <signal.h>
60 #include <setjmp.h>
61 #include <sys/stat.h>
62 #include <stdlib.h>
63 #include <limits.h>
64 
65 #define MULTI_BYTE_MAX MB_LEN_MAX
66 #define FTYPE(A)	(A.st_mode)
67 #define FMODE(A)	(A.st_mode)
68 #define	IDENTICAL(A,B)	(A.st_dev==B.st_dev && A.st_ino==B.st_ino)
69 #define ISBLK(A)	((A.st_mode & S_IFMT) == S_IFBLK)
70 #define ISCHR(A)	((A.st_mode & S_IFMT) == S_IFCHR)
71 #define ISDIR(A)	((A.st_mode & S_IFMT) == S_IFDIR)
72 #define ISFIFO(A)	((A.st_mode & S_IFMT) == S_IFIFO)
73 #define ISREG(A)	((A.st_mode & S_IFMT) == S_IFREG)
74 
75 #ifdef USG
76 #include <termio.h>
77 typedef struct termios SGTTY;
78 #else
79 #include <sgtty.h>
80 typedef struct sgttyb SGTTY;
81 #endif
82 
83 #ifdef PAVEL
84 #define SGTTY struct sgttyb	/* trick Pavel curses to not include <curses.h> */
85 #endif
86 typedef char bool;
87 typedef unsigned long chtype;
88 #include <term.h>
89 #define bool vi_bool
90 #ifdef PAVEL
91 #undef SGTTY
92 #endif
93 #ifndef var
94 #define var	extern
95 #endif
96 var char *exit_bold;		/* string to exit standout mode */
97 
98 /*
99  *	The following little dance copes with the new USG tty handling.
100  *	This stuff has the advantage of considerable flexibility, and
101  *	the disadvantage of being incompatible with anything else.
102  *	The presence of the symbol USG will indicate the new code:
103  *	in this case, we define CBREAK (because we can simulate it exactly),
104  *	but we won't actually use it, so we set it to a value that will
105  *	probably blow the compilation if we goof up.
106  */
107 #ifdef USG
108 #define CBREAK xxxxx
109 #endif
110 
111 extern	int errno;
112 
113 #ifndef VMUNIX
114 typedef	short	line;
115 #else
116 typedef	int	line;
117 #endif
118 typedef	short	bool;
119 
120 #include "ex_tune.h"
121 #include "ex_vars.h"
122 /*
123  * Options in the editor are referred to usually by "value(vi_name)" where
124  * name is all uppercase, i.e. "value(vi_PROMPT)".  This is actually a macro
125  * which expands to a fixed field in a static structure and so generates
126  * very little code.  The offsets for the option names in the structure
127  * are generated automagically from the structure initializing them in
128  * ex_data.c... see the shell script "makeoptions".
129  */
130 struct	option {
131 	unsigned char	*oname;
132 	unsigned char	*oabbrev;
133 	short	otype;		/* Types -- see below */
134 	short	odefault;	/* Default value */
135 	short	ovalue;		/* Current value */
136 	unsigned char	*osvalue;
137 };
138 
139 #define	ONOFF	0
140 #define	NUMERIC	1
141 #define	STRING	2		/* SHELL or DIRECTORY */
142 #define	OTERM	3
143 
144 #define	value(a)	options[a].ovalue
145 #define	svalue(a)	options[a].osvalue
146 
147 extern	 struct	option options[vi_NOPTS + 1];
148 
149 
150 /*
151  * The editor does not normally use the standard i/o library.  Because
152  * we expect the editor to be a heavily used program and because it
153  * does a substantial amount of input/output processing it is appropriate
154  * for it to call low level read/write primitives directly.  In fact,
155  * when debugging the editor we use the standard i/o library.  In any
156  * case the editor needs a printf which prints through "putchar" ala the
157  * old version 6 printf.  Thus we normally steal a copy of the "printf.c"
158  * and "strout" code from the standard i/o library and mung it for our
159  * purposes to avoid dragging in the stdio library headers, etc if we
160  * are not debugging.  Such a modified printf exists in "printf.c" here.
161  */
162 #ifdef TRACE
163 #include <stdio.h>
164 	var	FILE	*trace;
165 	var	bool	trubble;
166 	var	bool	techoin;
167 	var	unsigned char	tracbuf[BUFSIZ];
168 #undef	putchar
169 #undef	getchar
170 #else
171 /*
172  * Warning: do not change BUFSIZ without also changing LBSIZE in ex_tune.h
173  * Running with BUFSIZ set to anything besides what is in <stdio.h> is
174  * not recommended, if you use stdio.
175  */
176 #ifdef u370
177 #define	BUFSIZE	4096
178 #else
179 #define	BUFSIZE	(LINE_MAX*2)
180 #endif
181 #undef	NULL
182 #define	NULL	0
183 #undef	EOF
184 #define	EOF	-1
185 #endif
186 
187 /*
188  * Character constants and bits
189  *
190  * The editor uses the QUOTE bit as a flag to pass on with characters
191  * e.g. to the putchar routine.  The editor never uses a simple char variable.
192  * Only arrays of and pointers to characters are used and parameters and
193  * registers are never declared character.
194  */
195 #define	QUOTE	020000000000
196 #define	TRIM	017777777777
197 #define	NL	'\n'
198 #define	CR	'\r'
199 #define	DELETE	0177		/* See also ATTN, QUIT in ex_tune.h */
200 #define	ESCAPE	033
201 #undef	CTRL
202 #define	CTRL(c)	(c & 037)
203 
204 /*
205  * Miscellaneous random variables used in more than one place
206  */
207 var bool multibyte;
208 var	bool	aiflag;		/* Append/change/insert with autoindent */
209 var	bool	anymarks;	/* We have used '[a-z] */
210 var	int	chng;		/* Warn "No write" */
211 var	unsigned char	*Command;
212 var	short	defwind;	/* -w# change default window size */
213 var	int	dirtcnt;	/* When >= MAXDIRT, should sync temporary */
214 #ifdef SIGTSTP
215 var	bool	dosusp;		/* Do SIGTSTP in visual when ^Z typed */
216 #endif
217 var	bool	edited;		/* Current file is [Edited] */
218 var	line	*endcore;	/* Last available core location */
219 extern	 bool	endline;	/* Last cmd mode command ended with \n */
220 var	line	*fendcore;	/* First address in line pointer space */
221 var	unsigned char	file[FNSIZE];	/* Working file name */
222 var	unsigned char	genbuf[LBSIZE];	/* Working buffer when manipulating linebuf */
223 var	bool	hush;		/* Command line option - was given, hush up! */
224 var	unsigned char	*globp;		/* (Untyped) input string to command mode */
225 var	bool	holdcm;		/* Don't cursor address */
226 var	bool	inappend;	/* in ex command append mode */
227 var	bool	inglobal;	/* Inside g//... or v//... */
228 var	unsigned char	*initev;	/* Initial : escape for visual */
229 var	bool	inopen;		/* Inside open or visual */
230 var	unsigned char	*input;		/* Current position in cmd line input buffer */
231 var	bool	intty;		/* Input is a tty */
232 var	short	io;		/* General i/o unit (auto-closed on error!) */
233 extern	 short	lastc;		/* Last character ret'd from cmd input */
234 var	bool	laste;		/* Last command was an "e" (or "rec") */
235 var	unsigned char	lastmac;	/* Last macro called for ** */
236 var	unsigned char	lasttag[TAGSIZE];	/* Last argument to a tag command */
237 var	unsigned char	*linebp;	/* Used in substituting in \n */
238 var	unsigned char	linebuf[LBSIZE];	/* The primary line buffer */
239 var	bool	listf;		/* Command should run in list mode */
240 var	line	names['z'-'a'+2];	/* Mark registers a-z,' */
241 var	int	notecnt;	/* Count for notify (to visual from cmd) */
242 var	bool	numberf;	/* Command should run in number mode */
243 var	unsigned char	obuf[BUFSIZE];	/* Buffer for tty output */
244 var	short	oprompt;	/* Saved during source */
245 var	short	ospeed;		/* Output speed (from gtty) */
246 var	int	otchng;		/* Backup tchng to find changes in macros */
247 var	int	peekc;		/* Peek ahead character (cmd mode input) */
248 var	unsigned char	*pkill[2];	/* Trim for put with ragged (LISP) delete */
249 var	bool	pfast;		/* Have stty -nl'ed to go faster */
250 var	pid_t	pid;		/* Process id of child */
251 var	pid_t	ppid;		/* Process id of parent (e.g. main ex proc) */
252 var	jmp_buf	resetlab;	/* For error throws to top level (cmd mode) */
253 var	pid_t	rpid;		/* Pid returned from wait() */
254 var	bool	ruptible;	/* Interruptible is normal state */
255 var	bool	seenprompt;	/* 1 if have gotten user input */
256 var	bool	shudclob;	/* Have a prompt to clobber (e.g. on ^D) */
257 var	int	status;		/* Status returned from wait() */
258 var	int	tchng;		/* If nonzero, then [Modified] */
259 extern	short	tfile;		/* Temporary file unit */
260 var	bool	vcatch;		/* Want to catch an error (open/visual) */
261 var	jmp_buf	vreslab;	/* For error throws to a visual catch */
262 var	bool	writing;	/* 1 if in middle of a file write */
263 var	int	xchng;		/* Suppresses multiple "No writes" in !cmd */
264 #ifndef PRESUNEUC
265 var	char	mc_filler;	/* Right margin filler for multicolumn char */
266 var	bool	mc_wrap;	/* Multicolumn character wrap at right margin */
267 #endif /* PRESUNEUC */
268 var     int     inexrc;         /* boolean: in .exrc initialization */
269 
270 extern	int	termiosflag;	/* flag for using termios */
271 
272 /*
273  * Macros
274  */
275 #define	CP(a, b)	((void)strcpy(a, b))
276 			/*
277 			 * FIXUNDO: do we want to mung undo vars?
278 			 * Usually yes unless in a macro or global.
279 			 */
280 #define FIXUNDO		(inopen >= 0 && (inopen || !inglobal))
281 #define ckaw()		{if (chng && value(vi_AUTOWRITE) && !value(vi_READONLY)) \
282 				wop(0);\
283 			}
284 #define	copy(a,b,c)	Copy((char *) (a), (char *) (b), (c))
285 #define	eq(a, b)	((a) && (b) && strcmp(a, b) == 0)
286 #define	getexit(a)	copy(a, resetlab, sizeof (jmp_buf))
287 #define	lastchar()	lastc
288 #define	outchar(c)	(*Outchar)(c)
289 #define	pastwh()	((void)skipwh())
290 #define	pline(no)	(*Pline)(no)
291 #define	reset()		longjmp(resetlab,1)
292 #define	resexit(a)	copy(resetlab, a, sizeof (jmp_buf))
293 #define	setexit()	setjmp(resetlab)
294 #define	setlastchar(c)	lastc = c
295 #define	ungetchar(c)	peekc = c
296 
297 #define	CATCH		vcatch = 1; if (setjmp(vreslab) == 0) {
298 #define	ONERR		} else { vcatch = 0;
299 #define	ENDCATCH	} vcatch = 0;
300 
301 /*
302  * Environment like memory
303  */
304 var	unsigned char	altfile[FNSIZE];	/* Alternate file name */
305 extern	unsigned char	direct[ONMSZ];		/* Temp file goes here */
306 extern	unsigned char	shell[ONMSZ];		/* Copied to be settable */
307 var	unsigned char	uxb[UXBSIZE + 2];	/* Last !command for !! */
308 
309 /*
310  * The editor data structure for accessing the current file consists
311  * of an incore array of pointers into the temporary file tfile.
312  * Each pointer is 15 bits (the low bit is used by global) and is
313  * padded with zeroes to make an index into the temp file where the
314  * actual text of the line is stored.
315  *
316  * To effect undo, copies of affected lines are saved after the last
317  * line considered to be in the buffer, between dol and unddol.
318  * During an open or visual, which uses the command mode undo between
319  * dol and unddol, a copy of the entire, pre-command buffer state
320  * is saved between unddol and truedol.
321  */
322 var	line	*addr1;			/* First addressed line in a command */
323 var	line	*addr2;			/* Second addressed line */
324 var	line	*dol;			/* Last line in buffer */
325 var	line	*dot;			/* Current line */
326 var	line	*one;			/* First line */
327 var	line	*truedol;		/* End of all lines, including saves */
328 var	line	*unddol;		/* End of undo saved lines */
329 var	line	*zero;			/* Points to empty slot before one */
330 
331 /*
332  * Undo information
333  *
334  * For most commands we save lines changed by salting them away between
335  * dol and unddol before they are changed (i.e. we save the descriptors
336  * into the temp file tfile which is never garbage collected).  The
337  * lines put here go back after unddel, and to complete the undo
338  * we delete the lines [undap1,undap2).
339  *
340  * Undoing a move is much easier and we treat this as a special case.
341  * Similarly undoing a "put" is a special case for although there
342  * are lines saved between dol and unddol we don't stick these back
343  * into the buffer.
344  */
345 var	short	undkind;
346 
347 var	line	*unddel;	/* Saved deleted lines go after here */
348 var	line	*undap1;	/* Beginning of new lines */
349 var	line	*undap2;	/* New lines end before undap2 */
350 var	line	*undadot;	/* If we saved all lines, dot reverts here */
351 
352 #define	UNDCHANGE	0
353 #define	UNDMOVE		1
354 #define	UNDALL		2
355 #define	UNDNONE		3
356 #define	UNDPUT		4
357 
358 /*
359  * Various miscellaneous flags and buffers needed by the encryption routines.
360  */
361 #define	KSIZE   9       /* key size for encryption */
362 var	int	xflag;		/* True if we are in encryption mode */
363 var	int	xtflag;		/* True if the temp file is being encrypted */
364 var	int	kflag;		/* True if the key has been accepted */
365 var	int	crflag;		/* True if the key has been accepted  and the file
366 				   being read is ciphertext
367 				 */
368 var	int	perm[2];	/* pipe connection to crypt for file being edited */
369 var	int	tperm[2];	/* pipe connection to crypt for temporary file */
370 var	int permflag;
371 var 	int tpermflag;
372 var	unsigned char	*key;
373 var	unsigned char	crbuf[CRSIZE];
374 char	*getpass();
375 
376 var	bool	write_quit;	/* True if executing a 'wq' command */
377 var	int	errcnt;		/* number of error/warning messages in */
378 				/*	editing session (global flag)  */
379 /*
380  * Function type definitions
381  */
382 #define	NOSTR	(char *) 0
383 #define	NOLINE	(line *) 0
384 
385 extern	int	(*Outchar)();
386 extern	int	(*Pline)();
387 extern	int	(*Putchar)();
388 var	void	(*oldhup)();
389 int	(*setlist())();
390 int	(*setnorm())();
391 int	(*setnorm())();
392 int	(*setnumb())();
393 #ifndef PRESUNEUC
394 int	(*wdwc)(wchar_t);	/* tells kind of word character */
395 int	(*wdbdg)(wchar_t, wchar_t, int);	/* tells word binding force */
396 wchar_t	*(*wddlm)(wchar_t, wchar_t, int);	/* tells desired delimiter */
397 wchar_t	(*mcfllr)(void);	/* tells multicolumn filler character */
398 #endif /* PRESUNEUC */
399 char	*sbrk();
400 line	*address();
401 unsigned char	*cgoto();
402 unsigned char	*genindent();
403 unsigned char	*getblock();
404 char	*getenv();
405 line	*getmark();
406 unsigned char	*mesg();
407 unsigned char	*place();
408 unsigned char	*plural();
409 line	*scanfor();
410 line	*setin();
411 unsigned char	*strcat();
412 unsigned char	*strcpy();
413 unsigned char	*strend();
414 unsigned char	*tailpath();
415 char	*tgetstr();
416 char	*tgoto();
417 char	*ttyname();
418 line	*vback();
419 unsigned char	*vfindcol();
420 unsigned char	*vgetline();
421 unsigned char	*vinit();
422 unsigned char	*vpastwh();
423 unsigned char	*vskipwh();
424 int	put();
425 int	putreg();
426 int	YANKreg();
427 int	delete();
428 int	execlp();
429 int	vi_filter();
430 int	getfile();
431 int	getsub();
432 int	gettty();
433 int	join();
434 int	listchar();
435 off_t	lseek();
436 int	normchar();
437 int	normline();
438 int	numbline();
439 var	void	(*oldquit)();
440 #ifdef __STDC__
441 void	onhup(int);
442 void	onintr(int);
443 void	onemt(int);
444 void	oncore(int);
445 #ifdef CBREAK
446 void	vintr(int);
447 #endif
448 void	onsusp(int);
449 int	putch(char);
450 int	plodput(char);
451 int	vputch(char);
452 #else
453 void	onhup();
454 void	onintr();
455 void	onemt();
456 void	oncore();
457 #ifdef CBREAK
458 void	vintr();
459 #endif
460 void	onsusp();
461 int	putch();
462 int	plodput();
463 int	vputch();
464 #endif /* __STDC__ */
465 
466 int	shift();
467 int	termchar();
468 int	vfilter();
469 int	vshftop();
470 int	yank();
471 unsigned char *lastchr();
472 unsigned char *nextchr();
473 bool putoctal;
474 int	shift();
475 int	termchar();
476 int	vfilter();
477 int	vshftop();
478 int	yank();
479 unsigned char *lastchr();
480 unsigned char *nextchr();
481 bool putoctal;
482