xref: /titanic_52/usr/src/cmd/vi/port/ex.h (revision b6c3f7863936abeae522e48a13887dddeb691a45)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 /* Copyright (c) 1981 Regents of the University of California */
31 
32 #ifndef _EX_H
33 #define	_EX_H
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /*
42  * This file contains most of the declarations common to a large number
43  * of routines.  The file ex_vis.h contains declarations
44  * which are used only inside the screen editor.
45  * The file ex_tune.h contains parameters which can be diddled per installation.
46  *
47  * The declarations relating to the argument list, regular expressions,
48  * the temporary file data structure used by the editor
49  * and the data describing terminals are each fairly substantial and
50  * are kept in the files ex_{argv,re,temp,tty}.h which
51  * we #include separately.
52  *
53  * If you are going to dig into ex, you should look at the outline of the
54  * distribution of the code into files at the beginning of ex.c and ex_v.c.
55  * Code which is similar to that of ed is lightly or undocumented in spots
56  * (e.g. the regular expression code).  Newer code (e.g. open and visual)
57  * is much more carefully documented, and still rough in spots.
58  *
59  */
60 #ifdef UCBV7
61 #include <whoami.h>
62 #endif
63 #include <sys/types.h>
64 #include <ctype.h>
65 #include <errno.h>
66 #include <signal.h>
67 #include <setjmp.h>
68 #include <sys/stat.h>
69 #include <stdlib.h>
70 #include <limits.h>
71 #include <libintl.h>
72 
73 #define MULTI_BYTE_MAX MB_LEN_MAX
74 #define FTYPE(A)	(A.st_mode)
75 #define FMODE(A)	(A.st_mode)
76 #define	IDENTICAL(A,B)	(A.st_dev==B.st_dev && A.st_ino==B.st_ino)
77 #define ISBLK(A)	((A.st_mode & S_IFMT) == S_IFBLK)
78 #define ISCHR(A)	((A.st_mode & S_IFMT) == S_IFCHR)
79 #define ISDIR(A)	((A.st_mode & S_IFMT) == S_IFDIR)
80 #define ISFIFO(A)	((A.st_mode & S_IFMT) == S_IFIFO)
81 #define ISREG(A)	((A.st_mode & S_IFMT) == S_IFREG)
82 
83 #ifdef USG
84 #include <termio.h>
85 typedef struct termios SGTTY;
86 #else
87 #include <sgtty.h>
88 typedef struct sgttyb SGTTY;
89 #endif
90 
91 #ifdef PAVEL
92 #define SGTTY struct sgttyb	/* trick Pavel curses to not include <curses.h> */
93 #endif
94 typedef char bool;
95 typedef unsigned long chtype;
96 #include <term.h>
97 #define bool vi_bool
98 #ifdef PAVEL
99 #undef SGTTY
100 #endif
101 #ifndef var
102 #define var	extern
103 #endif
104 var char *exit_bold;		/* string to exit standout mode */
105 
106 /*
107  *	The following little dance copes with the new USG tty handling.
108  *	This stuff has the advantage of considerable flexibility, and
109  *	the disadvantage of being incompatible with anything else.
110  *	The presence of the symbol USG will indicate the new code:
111  *	in this case, we define CBREAK (because we can simulate it exactly),
112  *	but we won't actually use it, so we set it to a value that will
113  *	probably blow the compilation if we goof up.
114  */
115 #ifdef USG
116 #define CBREAK xxxxx
117 #endif
118 
119 #ifndef VMUNIX
120 typedef	short	line;
121 #else
122 typedef	int	line;
123 #endif
124 typedef	short	bool;
125 
126 #include "ex_tune.h"
127 #include "ex_vars.h"
128 /*
129  * Options in the editor are referred to usually by "value(vi_name)" where
130  * name is all uppercase, i.e. "value(vi_PROMPT)".  This is actually a macro
131  * which expands to a fixed field in a static structure and so generates
132  * very little code.  The offsets for the option names in the structure
133  * are generated automagically from the structure initializing them in
134  * ex_data.c... see the shell script "makeoptions".
135  */
136 struct	option {
137 	unsigned char	*oname;
138 	unsigned char	*oabbrev;
139 	short	otype;		/* Types -- see below */
140 	short	odefault;	/* Default value */
141 	short	ovalue;		/* Current value */
142 	unsigned char	*osvalue;
143 };
144 
145 #define	ONOFF	0
146 #define	NUMERIC	1
147 #define	STRING	2		/* SHELL or DIRECTORY */
148 #define	OTERM	3
149 
150 #define	value(a)	options[a].ovalue
151 #define	svalue(a)	options[a].osvalue
152 
153 extern	 struct	option options[vi_NOPTS + 1];
154 
155 
156 /*
157  * The editor does not normally use the standard i/o library.  Because
158  * we expect the editor to be a heavily used program and because it
159  * does a substantial amount of input/output processing it is appropriate
160  * for it to call low level read/write primitives directly.  In fact,
161  * when debugging the editor we use the standard i/o library.  In any
162  * case the editor needs a printf which prints through "putchar" ala the
163  * old version 6 printf.  Thus we normally steal a copy of the "printf.c"
164  * and "strout" code from the standard i/o library and mung it for our
165  * purposes to avoid dragging in the stdio library headers, etc if we
166  * are not debugging.  Such a modified printf exists in "printf.c" here.
167  */
168 #ifdef TRACE
169 #include <stdio.h>
170 	var	FILE	*trace;
171 	var	bool	trubble;
172 	var	bool	techoin;
173 	var	unsigned char	tracbuf[BUFSIZ];
174 #undef	putchar
175 #undef	getchar
176 #else
177 /*
178  * Warning: do not change BUFSIZ without also changing LBSIZE in ex_tune.h
179  * Running with BUFSIZ set to anything besides what is in <stdio.h> is
180  * not recommended, if you use stdio.
181  */
182 #ifdef u370
183 #define	BUFSIZE	4096
184 #else
185 #define	BUFSIZE	(LINE_MAX*2)
186 #endif
187 #undef	NULL
188 #define	NULL	0
189 #undef	EOF
190 #define	EOF	-1
191 #endif
192 
193 /*
194  * Character constants and bits
195  *
196  * The editor uses the QUOTE bit as a flag to pass on with characters
197  * e.g. to the putchar routine.  The editor never uses a simple char variable.
198  * Only arrays of and pointers to characters are used and parameters and
199  * registers are never declared character.
200  */
201 #define	QUOTE	020000000000
202 #define	TRIM	017777777777
203 #define	NL	'\n'
204 #define	CR	'\r'
205 #define	DELETE	0177		/* See also ATTN, QUIT in ex_tune.h */
206 #define	ESCAPE	033
207 #undef	CTRL
208 #define	CTRL(c)	(c & 037)
209 
210 /*
211  * Miscellaneous random variables used in more than one place
212  */
213 var bool multibyte;
214 var	bool	aiflag;		/* Append/change/insert with autoindent */
215 var	bool	tagflg;		/* set for -t option and :tag command */
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 #define	setterm visetterm
393 #define	draino vidraino
394 #define	gettmode vigettmode
395 
396 extern	int	(*Outchar)();
397 extern	int	(*Pline)();
398 extern	int	(*Putchar)();
399 var	void	(*oldhup)();
400 int	(*setlist())();
401 int	(*setnorm())();
402 int	(*setnorm())();
403 int	(*setnumb())();
404 #ifndef PRESUNEUC
405 int	(*wdwc)(wchar_t);	/* tells kind of word character */
406 int	(*wdbdg)(wchar_t, wchar_t, int);	/* tells word binding force */
407 wchar_t	*(*wddlm)(wchar_t, wchar_t, int);	/* tells desired delimiter */
408 wchar_t	(*mcfllr)(void);	/* tells multicolumn filler character */
409 #endif /* PRESUNEUC */
410 line	*address();
411 unsigned char	*cgoto();
412 unsigned char	*genindent();
413 unsigned char	*getblock();
414 char	*getenv();
415 line	*getmark();
416 unsigned char	*mesg();
417 unsigned char	*place();
418 unsigned char	*plural();
419 line	*scanfor();
420 void setin(line *);
421 unsigned char	*strend();
422 unsigned char	*tailpath();
423 char	*tgetstr();
424 char	*tgoto();
425 char	*ttyname();
426 line	*vback();
427 unsigned char	*vfindcol();
428 unsigned char	*vgetline();
429 unsigned char	*vinit();
430 unsigned char	*vpastwh();
431 unsigned char	*vskipwh();
432 int	put(void);
433 int	putreg(unsigned char);
434 int	YANKreg(int);
435 int	delete(bool);
436 int	vi_filter();
437 int	getfile();
438 int	getsub();
439 int	gettty();
440 int	join(int);
441 int	listchar(wchar_t);
442 int	normchar(wchar_t);
443 int	normline(void);
444 int	numbline(int);
445 var	void	(*oldquit)();
446 #ifdef __STDC__
447 void	onhup(int);
448 void	onintr(int);
449 void	onemt(int);
450 void	oncore(int);
451 #ifdef CBREAK
452 void	vintr(int);
453 #endif
454 void	onsusp(int);
455 int	putch(char);
456 int	plodput(char);
457 int	vputch(char);
458 #else
459 void	onhup();
460 void	onintr();
461 void	onemt();
462 void	oncore();
463 #ifdef CBREAK
464 void	vintr();
465 #endif
466 void	onsusp();
467 int	putch();
468 int	plodput();
469 int	vputch();
470 #endif /* __STDC__ */
471 
472 void	shift(int, int);
473 int	termchar(wchar_t);
474 int	vfilter();
475 int	vshftop();
476 int	yank(void);
477 unsigned char *lastchr();
478 unsigned char *nextchr();
479 bool putoctal;
480 
481 void	error();
482 void	error0(void);
483 void error1(unsigned char *);
484 void fixol(void);
485 void resetflav(void);
486 void serror(unsigned char *, unsigned char *);
487 void setflav(void);
488 void tailprim(unsigned char *, int, bool);
489 void vcontin(bool);
490 void squish(void);
491 void move1(int, line *);
492 void pragged(bool);
493 void zop2(int, int);
494 void plines(line *, line *, bool);
495 void pofix(void);
496 void undo(bool);
497 void somechange(void);
498 void savetag(char *);
499 void unsavetag(void);
500 void checkjunk(unsigned char);
501 void getone(void);
502 void rop3(int);
503 void rop2(void);
504 void putfile(int);
505 void wrerror(void);
506 void clrstats(void);
507 void slobber(int);
508 void flush(void);
509 void flush1(void);
510 void flush2(void);
511 void fgoto(void);
512 void flusho(void);
513 void comprhs(int);
514 int dosubcon(bool, line *);
515 void ugo(int, int);
516 void dosub(void);
517 void snote(int, int);
518 void cerror(unsigned char *);
519 void unterm(void);
520 int setend(void);
521 void prall(void);
522 void propts(void);
523 void propt(struct option *);
524 void killcnt(int);
525 void markpr(line *);
526 void merror1(unsigned char *);
527 void notempty(void);
528 int qcolumn(unsigned char *, unsigned char *);
529 void netchange(int);
530 void putmk1(line *, int);
531 int nqcolumn(unsigned char *, unsigned char *);
532 void syserror(int);
533 void cleanup(bool);
534 void blkio(short, unsigned char *, int (*)());
535 void tflush(void);
536 short partreg(unsigned char);
537 void kshift(void);
538 void YANKline(void);
539 void rbflush(void);
540 void waitfor(void);
541 void ovbeg(void);
542 void fixzero(void);
543 void savevis(void);
544 void undvis(void);
545 void setwind(void);
546 void vok(wchar_t *, int);
547 void vsetsiz(int);
548 void vinslin(int, int, int);
549 void vopenup(int, bool, int);
550 void vadjAL(int, int);
551 void vup1(void);
552 void vmoveitup(int, bool);
553 void vscroll(int);
554 void vscrap(void);
555 void vredraw(int);
556 void vdellin(int, int, int);
557 void vadjDL(int, int);
558 void vsyncCL(void);
559 void vsync(int);
560 void vsync1(int);
561 void vcloseup(int, int);
562 void sethard(void);
563 void vdirty(int, int);
564 void setBUF(unsigned char *);
565 void addto(unsigned char *, unsigned char *);
566 void macpush();
567 void setalarm(void);
568 void cancelalarm(void);
569 void grabtag(void);
570 void prepapp(void);
571 void vremote();
572 void vsave(void);
573 void vzop(bool, int, int);
574 void warnf();
575 int wordof(unsigned char, unsigned char *);
576 void setpk(void);
577 void back1(void);
578 void vdoappend(unsigned char *);
579 void vclrbyte(wchar_t *, int);
580 void vclreol(void);
581 void vsetcurs(unsigned char *);
582 void vigoto(int, int);
583 void vcsync(void);
584 void vgotoCL(int);
585 void vgoto(int, int);
586 void vmaktop(int, wchar_t *);
587 void vrigid(void);
588 void vneedpos(int);
589 void vnpins(int);
590 void vishft(void);
591 void viin(wchar_t);
592 void godm(void);
593 void enddm(void);
594 void goim(void);
595 void endim(void);
596 void vjumpto(line *, unsigned char *, unsigned char);
597 void vup(int, int, bool);
598 void vdown(int, int, bool);
599 void vcontext(line *, unsigned char);
600 void vclean(void);
601 void vshow(line *, line*);
602 void vreset(bool);
603 void vroll(int);
604 void vrollR(int);
605 void vnline(unsigned char *);
606 void noerror();
607 void getline(line);
608 void viprintf();
609 void gettmode(void);
610 void setterm(unsigned char *);
611 void draino(void);
612 int lfind();
613 void source();
614 void commands();
615 void addmac();
616 void vmoveto();
617 void vrepaint();
618 void getDOT(void);
619 void vclear(void);
620 
621 unsigned char *lastchr();
622 unsigned char *nextchr();
623 bool putoctal;
624 
625 void setdot1(void);
626 
627 #ifdef __cplusplus
628 }
629 #endif
630 
631 #endif /* _EX_H */
632