xref: /illumos-gate/usr/src/cmd/vi/port/ex.h (revision 141040e8a310da49386b596573e5dde5580572ec)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 /* Copyright (c) 1981 Regents of the University of California */
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 #ifdef UCBV7
62 #include <whoami.h>
63 #endif
64 #include <sys/types.h>
65 #include <ctype.h>
66 #include <errno.h>
67 #include <signal.h>
68 #include <setjmp.h>
69 #include <sys/stat.h>
70 #include <stdlib.h>
71 #include <limits.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	anymarks;	/* We have used '[a-z] */
216 var	int	chng;		/* Warn "No write" */
217 var	unsigned char	*Command;
218 var	short	defwind;	/* -w# change default window size */
219 var	int	dirtcnt;	/* When >= MAXDIRT, should sync temporary */
220 #ifdef SIGTSTP
221 var	bool	dosusp;		/* Do SIGTSTP in visual when ^Z typed */
222 #endif
223 var	bool	edited;		/* Current file is [Edited] */
224 var	line	*endcore;	/* Last available core location */
225 extern	 bool	endline;	/* Last cmd mode command ended with \n */
226 var	line	*fendcore;	/* First address in line pointer space */
227 var	unsigned char	file[FNSIZE];	/* Working file name */
228 var	unsigned char	genbuf[LBSIZE];	/* Working buffer when manipulating linebuf */
229 var	bool	hush;		/* Command line option - was given, hush up! */
230 var	unsigned char	*globp;		/* (Untyped) input string to command mode */
231 var	bool	holdcm;		/* Don't cursor address */
232 var	bool	inappend;	/* in ex command append mode */
233 var	bool	inglobal;	/* Inside g//... or v//... */
234 var	unsigned char	*initev;	/* Initial : escape for visual */
235 var	bool	inopen;		/* Inside open or visual */
236 var	unsigned char	*input;		/* Current position in cmd line input buffer */
237 var	bool	intty;		/* Input is a tty */
238 var	short	io;		/* General i/o unit (auto-closed on error!) */
239 extern	 short	lastc;		/* Last character ret'd from cmd input */
240 var	bool	laste;		/* Last command was an "e" (or "rec") */
241 var	unsigned char	lastmac;	/* Last macro called for ** */
242 var	unsigned char	lasttag[TAGSIZE];	/* Last argument to a tag command */
243 var	unsigned char	*linebp;	/* Used in substituting in \n */
244 var	unsigned char	linebuf[LBSIZE];	/* The primary line buffer */
245 var	bool	listf;		/* Command should run in list mode */
246 var	line	names['z'-'a'+2];	/* Mark registers a-z,' */
247 var	int	notecnt;	/* Count for notify (to visual from cmd) */
248 var	bool	numberf;	/* Command should run in number mode */
249 var	unsigned char	obuf[BUFSIZE];	/* Buffer for tty output */
250 var	short	oprompt;	/* Saved during source */
251 var	short	ospeed;		/* Output speed (from gtty) */
252 var	int	otchng;		/* Backup tchng to find changes in macros */
253 var	int	peekc;		/* Peek ahead character (cmd mode input) */
254 var	unsigned char	*pkill[2];	/* Trim for put with ragged (LISP) delete */
255 var	bool	pfast;		/* Have stty -nl'ed to go faster */
256 var	pid_t	pid;		/* Process id of child */
257 var	pid_t	ppid;		/* Process id of parent (e.g. main ex proc) */
258 var	jmp_buf	resetlab;	/* For error throws to top level (cmd mode) */
259 var	pid_t	rpid;		/* Pid returned from wait() */
260 var	bool	ruptible;	/* Interruptible is normal state */
261 var	bool	seenprompt;	/* 1 if have gotten user input */
262 var	bool	shudclob;	/* Have a prompt to clobber (e.g. on ^D) */
263 var	int	status;		/* Status returned from wait() */
264 var	int	tchng;		/* If nonzero, then [Modified] */
265 extern	short	tfile;		/* Temporary file unit */
266 var	bool	vcatch;		/* Want to catch an error (open/visual) */
267 var	jmp_buf	vreslab;	/* For error throws to a visual catch */
268 var	bool	writing;	/* 1 if in middle of a file write */
269 var	int	xchng;		/* Suppresses multiple "No writes" in !cmd */
270 #ifndef PRESUNEUC
271 var	char	mc_filler;	/* Right margin filler for multicolumn char */
272 var	bool	mc_wrap;	/* Multicolumn character wrap at right margin */
273 #endif /* PRESUNEUC */
274 var     int     inexrc;         /* boolean: in .exrc initialization */
275 
276 extern	int	termiosflag;	/* flag for using termios */
277 
278 /*
279  * Macros
280  */
281 #define	CP(a, b)	((void)strcpy(a, b))
282 			/*
283 			 * FIXUNDO: do we want to mung undo vars?
284 			 * Usually yes unless in a macro or global.
285 			 */
286 #define FIXUNDO		(inopen >= 0 && (inopen || !inglobal))
287 #define ckaw()		{if (chng && value(vi_AUTOWRITE) && !value(vi_READONLY)) \
288 				wop(0);\
289 			}
290 #define	copy(a,b,c)	Copy((char *) (a), (char *) (b), (c))
291 #define	eq(a, b)	((a) && (b) && strcmp(a, b) == 0)
292 #define	getexit(a)	copy(a, resetlab, sizeof (jmp_buf))
293 #define	lastchar()	lastc
294 #define	outchar(c)	(*Outchar)(c)
295 #define	pastwh()	((void)skipwh())
296 #define	pline(no)	(*Pline)(no)
297 #define	reset()		longjmp(resetlab,1)
298 #define	resexit(a)	copy(resetlab, a, sizeof (jmp_buf))
299 #define	setexit()	setjmp(resetlab)
300 #define	setlastchar(c)	lastc = c
301 #define	ungetchar(c)	peekc = c
302 
303 #define	CATCH		vcatch = 1; if (setjmp(vreslab) == 0) {
304 #define	ONERR		} else { vcatch = 0;
305 #define	ENDCATCH	} vcatch = 0;
306 
307 /*
308  * Environment like memory
309  */
310 var	unsigned char	altfile[FNSIZE];	/* Alternate file name */
311 extern	unsigned char	direct[ONMSZ];		/* Temp file goes here */
312 extern	unsigned char	shell[ONMSZ];		/* Copied to be settable */
313 var	unsigned char	uxb[UXBSIZE + 2];	/* Last !command for !! */
314 
315 /*
316  * The editor data structure for accessing the current file consists
317  * of an incore array of pointers into the temporary file tfile.
318  * Each pointer is 15 bits (the low bit is used by global) and is
319  * padded with zeroes to make an index into the temp file where the
320  * actual text of the line is stored.
321  *
322  * To effect undo, copies of affected lines are saved after the last
323  * line considered to be in the buffer, between dol and unddol.
324  * During an open or visual, which uses the command mode undo between
325  * dol and unddol, a copy of the entire, pre-command buffer state
326  * is saved between unddol and truedol.
327  */
328 var	line	*addr1;			/* First addressed line in a command */
329 var	line	*addr2;			/* Second addressed line */
330 var	line	*dol;			/* Last line in buffer */
331 var	line	*dot;			/* Current line */
332 var	line	*one;			/* First line */
333 var	line	*truedol;		/* End of all lines, including saves */
334 var	line	*unddol;		/* End of undo saved lines */
335 var	line	*zero;			/* Points to empty slot before one */
336 
337 /*
338  * Undo information
339  *
340  * For most commands we save lines changed by salting them away between
341  * dol and unddol before they are changed (i.e. we save the descriptors
342  * into the temp file tfile which is never garbage collected).  The
343  * lines put here go back after unddel, and to complete the undo
344  * we delete the lines [undap1,undap2).
345  *
346  * Undoing a move is much easier and we treat this as a special case.
347  * Similarly undoing a "put" is a special case for although there
348  * are lines saved between dol and unddol we don't stick these back
349  * into the buffer.
350  */
351 var	short	undkind;
352 
353 var	line	*unddel;	/* Saved deleted lines go after here */
354 var	line	*undap1;	/* Beginning of new lines */
355 var	line	*undap2;	/* New lines end before undap2 */
356 var	line	*undadot;	/* If we saved all lines, dot reverts here */
357 
358 #define	UNDCHANGE	0
359 #define	UNDMOVE		1
360 #define	UNDALL		2
361 #define	UNDNONE		3
362 #define	UNDPUT		4
363 
364 /*
365  * Various miscellaneous flags and buffers needed by the encryption routines.
366  */
367 #define	KSIZE   9       /* key size for encryption */
368 var	int	xflag;		/* True if we are in encryption mode */
369 var	int	xtflag;		/* True if the temp file is being encrypted */
370 var	int	kflag;		/* True if the key has been accepted */
371 var	int	crflag;		/* True if the key has been accepted  and the file
372 				   being read is ciphertext
373 				 */
374 var	int	perm[2];	/* pipe connection to crypt for file being edited */
375 var	int	tperm[2];	/* pipe connection to crypt for temporary file */
376 var	int permflag;
377 var 	int tpermflag;
378 var	unsigned char	*key;
379 var	unsigned char	crbuf[CRSIZE];
380 char	*getpass();
381 
382 var	bool	write_quit;	/* True if executing a 'wq' command */
383 var	int	errcnt;		/* number of error/warning messages in */
384 				/*	editing session (global flag)  */
385 /*
386  * Function type definitions
387  */
388 #define	NOSTR	(char *) 0
389 #define	NOLINE	(line *) 0
390 
391 #define	setterm visetterm
392 #define	draino vidraino
393 #define	gettmode vigettmode
394 
395 extern	int	(*Outchar)();
396 extern	int	(*Pline)();
397 extern	int	(*Putchar)();
398 var	void	(*oldhup)();
399 int	(*setlist())();
400 int	(*setnorm())();
401 int	(*setnorm())();
402 int	(*setnumb())();
403 #ifndef PRESUNEUC
404 int	(*wdwc)(wchar_t);	/* tells kind of word character */
405 int	(*wdbdg)(wchar_t, wchar_t, int);	/* tells word binding force */
406 wchar_t	*(*wddlm)(wchar_t, wchar_t, int);	/* tells desired delimiter */
407 wchar_t	(*mcfllr)(void);	/* tells multicolumn filler character */
408 #endif /* PRESUNEUC */
409 line	*address();
410 unsigned char	*cgoto();
411 unsigned char	*genindent();
412 unsigned char	*getblock();
413 char	*getenv();
414 line	*getmark();
415 unsigned char	*mesg();
416 unsigned char	*place();
417 unsigned char	*plural();
418 line	*scanfor();
419 void setin(line *);
420 unsigned char	*strend();
421 unsigned char	*tailpath();
422 char	*tgetstr();
423 char	*tgoto();
424 char	*ttyname();
425 line	*vback();
426 unsigned char	*vfindcol();
427 unsigned char	*vgetline();
428 unsigned char	*vinit();
429 unsigned char	*vpastwh();
430 unsigned char	*vskipwh();
431 int	put(void);
432 int	putreg(unsigned char);
433 int	YANKreg(int);
434 int	delete(bool);
435 int	vi_filter();
436 int	getfile();
437 int	getsub();
438 int	gettty();
439 int	join(int);
440 int	listchar(wchar_t);
441 int	normchar(wchar_t);
442 int	normline(void);
443 int	numbline(int);
444 var	void	(*oldquit)();
445 #ifdef __STDC__
446 void	onhup(int);
447 void	onintr(int);
448 void	onemt(int);
449 void	oncore(int);
450 #ifdef CBREAK
451 void	vintr(int);
452 #endif
453 void	onsusp(int);
454 int	putch(char);
455 int	plodput(char);
456 int	vputch(char);
457 #else
458 void	onhup();
459 void	onintr();
460 void	onemt();
461 void	oncore();
462 #ifdef CBREAK
463 void	vintr();
464 #endif
465 void	onsusp();
466 int	putch();
467 int	plodput();
468 int	vputch();
469 #endif /* __STDC__ */
470 
471 void	shift(int, int);
472 int	termchar(wchar_t);
473 int	vfilter();
474 int	vshftop();
475 int	yank(void);
476 unsigned char *lastchr();
477 unsigned char *nextchr();
478 bool putoctal;
479 
480 void	error();
481 void	error0(void);
482 void error1(unsigned char *);
483 void fixol(void);
484 void resetflav(void);
485 void serror(unsigned char *, unsigned char *);
486 void setflav(void);
487 void tailprim(unsigned char *, int, bool);
488 void vcontin(bool);
489 void squish(void);
490 void move1(int, line *);
491 void pragged(bool);
492 void zop2(int, int);
493 void plines(line *, line *, bool);
494 void pofix(void);
495 void undo(bool);
496 void somechange(void);
497 void savetag(char *);
498 void unsavetag(void);
499 void checkjunk(unsigned char);
500 void getone(void);
501 void rop3(int);
502 void rop2(void);
503 void putfile(int);
504 void wrerror(void);
505 void clrstats(void);
506 void slobber(int);
507 void flush(void);
508 void flush1(void);
509 void flush2(void);
510 void fgoto(void);
511 void flusho(void);
512 void comprhs(int);
513 int dosubcon(bool, line *);
514 void ugo(int, int);
515 void dosub(void);
516 void snote(int, int);
517 void cerror(unsigned char *);
518 void unterm(void);
519 int setend(void);
520 void prall(void);
521 void propts(void);
522 void propt(struct option *);
523 void killcnt(int);
524 void markpr(line *);
525 void merror1(unsigned char *);
526 void notempty(void);
527 int qcolumn(unsigned char *, unsigned char *);
528 void netchange(int);
529 void putmk1(line *, int);
530 int nqcolumn(unsigned char *, unsigned char *);
531 void syserror(int);
532 void cleanup(bool);
533 void blkio(short, unsigned char *, int (*)());
534 void tflush(void);
535 short partreg(unsigned char);
536 void kshift(void);
537 void YANKline(void);
538 void rbflush(void);
539 void waitfor(void);
540 void ovbeg(void);
541 void fixzero(void);
542 void savevis(void);
543 void undvis(void);
544 void setwind(void);
545 void vok(wchar_t *, int);
546 void vsetsiz(int);
547 void vinslin(int, int, int);
548 void vopenup(int, bool, int);
549 void vadjAL(int, int);
550 void vup1(void);
551 void vmoveitup(int, bool);
552 void vscroll(int);
553 void vscrap(void);
554 void vredraw(int);
555 void vdellin(int, int, int);
556 void vadjDL(int, int);
557 void vsyncCL(void);
558 void vsync(int);
559 void vsync1(int);
560 void vcloseup(int, int);
561 void sethard(void);
562 void vdirty(int, int);
563 void setBUF(unsigned char *);
564 void addto(unsigned char *, unsigned char *);
565 void macpush();
566 void setalarm(void);
567 void cancelalarm(void);
568 void grabtag(void);
569 void prepapp(void);
570 void vremote();
571 void vsave(void);
572 void vzop(bool, int, int);
573 void warnf();
574 int wordof(unsigned char, unsigned char *);
575 void setpk(void);
576 void back1(void);
577 void vdoappend(unsigned char *);
578 void vclrbyte(wchar_t *, int);
579 void vclreol(void);
580 void vsetcurs(unsigned char *);
581 void vigoto(int, int);
582 void vcsync(void);
583 void vgotoCL(int);
584 void vgoto(int, int);
585 void vmaktop(int, wchar_t *);
586 void vrigid(void);
587 void vneedpos(int);
588 void vnpins(int);
589 void vishft(void);
590 void viin(wchar_t);
591 void godm(void);
592 void enddm(void);
593 void goim(void);
594 void endim(void);
595 void vjumpto(line *, unsigned char *, unsigned char);
596 void vup(int, int, bool);
597 void vdown(int, int, bool);
598 void vcontext(line *, unsigned char);
599 void vclean(void);
600 void vshow(line *, line*);
601 void vreset(bool);
602 void vroll(int);
603 void vrollR(int);
604 void vnline(unsigned char *);
605 void noerror();
606 void getline(line);
607 void viprintf();
608 void gettmode(void);
609 void setterm(unsigned char *);
610 void draino(void);
611 int lfind();
612 void source();
613 void commands();
614 void addmac();
615 
616 unsigned char *lastchr();
617 unsigned char *nextchr();
618 bool putoctal;
619 
620 void setdot1(void);
621 
622 #ifdef __cplusplus
623 }
624 #endif
625 
626 #endif /* _EX_H */
627