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