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