1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1982-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                  David Korn <dgk@research.att.com>                   *
18*4887Schin *                                                                      *
19*4887Schin ***********************************************************************/
20*4887Schin #pragma prototyped
21*4887Schin #ifndef SEARCHSIZE
22*4887Schin /*
23*4887Schin  *  edit.h -  common data structure for vi and emacs edit options
24*4887Schin  *
25*4887Schin  *   David Korn
26*4887Schin  *   AT&T Labs
27*4887Schin  *
28*4887Schin  */
29*4887Schin 
30*4887Schin #define SEARCHSIZE	80
31*4887Schin 
32*4887Schin #include	"FEATURE/options"
33*4887Schin #include        "FEATURE/locale"
34*4887Schin #if !SHOPT_VSH && !SHOPT_ESH
35*4887Schin #   define ed_winsize()	(SEARCHSIZE)
36*4887Schin #else
37*4887Schin 
38*4887Schin #if !KSHELL
39*4887Schin #   include	<setjmp.h>
40*4887Schin #   include	<sig.h>
41*4887Schin #   include	<ctype.h>
42*4887Schin #endif /* KSHELL */
43*4887Schin 
44*4887Schin #include	"FEATURE/setjmp"
45*4887Schin #include	"terminal.h"
46*4887Schin 
47*4887Schin #define STRIP		0377
48*4887Schin #define LOOKAHEAD	80
49*4887Schin 
50*4887Schin #if SHOPT_MULTIBYTE
51*4887Schin #   ifndef ESS_MAXCHAR
52*4887Schin #	include	"national.h"
53*4887Schin #   endif /* ESS_MAXCHAR */
54*4887Schin     typedef wchar_t genchar;
55*4887Schin #   define CHARSIZE	(sizeof(wchar_t)<=2?3:sizeof(wchar_t))
56*4887Schin #else
57*4887Schin     typedef char genchar;
58*4887Schin #   define CHARSIZE	1
59*4887Schin #endif /* SHOPT_MULTIBYTE */
60*4887Schin 
61*4887Schin #define TABSIZE	8
62*4887Schin #define PRSIZE	160
63*4887Schin #define MAXLINE	1024		/* longest edit line permitted */
64*4887Schin 
65*4887Schin typedef struct _edit_pos
66*4887Schin {
67*4887Schin 	unsigned short line;
68*4887Schin 	unsigned short col;
69*4887Schin } Edpos_t;
70*4887Schin 
71*4887Schin typedef struct edit
72*4887Schin {
73*4887Schin 	sigjmp_buf e_env;
74*4887Schin 	int	e_kill;
75*4887Schin 	int	e_erase;
76*4887Schin 	int	e_werase;
77*4887Schin 	int	e_eof;
78*4887Schin 	int	e_lnext;
79*4887Schin 	int	e_fchar;
80*4887Schin 	int	e_plen;		/* length of prompt string */
81*4887Schin 	int	e_crlf;		/* zero if cannot return to beginning of line */
82*4887Schin 	int	e_llimit;	/* line length limit */
83*4887Schin 	int	e_hline;	/* current history line number */
84*4887Schin 	int	e_hloff;	/* line number offset for command */
85*4887Schin 	int	e_hismin;	/* minimum history line number */
86*4887Schin 	int	e_hismax;	/* maximum history line number */
87*4887Schin 	int	e_raw;		/* set when in raw mode or alt mode */
88*4887Schin 	int	e_cur;		/* current line position */
89*4887Schin 	int	e_eol;		/* end-of-line position */
90*4887Schin 	int	e_pcur;		/* current physical line position */
91*4887Schin 	int	e_peol;		/* end of physical line position */
92*4887Schin 	int	e_mode;		/* edit mode */
93*4887Schin 	int	e_lookahead;	/* index in look-ahead buffer */
94*4887Schin 	int	e_repeat;
95*4887Schin 	int	e_saved;
96*4887Schin 	int	e_fcol;		/* first column */
97*4887Schin 	int	e_ucol;		/* column for undo */
98*4887Schin 	int	e_wsize;	/* width of display window */
99*4887Schin 	char	*e_outbase;	/* pointer to start of output buffer */
100*4887Schin 	char	*e_outptr;	/* pointer to position in output buffer */
101*4887Schin 	char	*e_outlast;	/* pointer to end of output buffer */
102*4887Schin 	genchar	*e_inbuf;	/* pointer to input buffer */
103*4887Schin 	char	*e_prompt;	/* pointer to buffer containing the prompt */
104*4887Schin 	genchar	*e_ubuf;	/* pointer to the undo buffer */
105*4887Schin 	genchar	*e_killbuf;	/* pointer to delete buffer */
106*4887Schin 	char	e_search[SEARCHSIZE];	/* search string */
107*4887Schin 	genchar	*e_Ubuf;	/* temporary workspace buffer */
108*4887Schin 	genchar	*e_physbuf;	/* temporary workspace buffer */
109*4887Schin 	int	e_lbuf[LOOKAHEAD];/* pointer to look-ahead buffer */
110*4887Schin 	int	e_fd;		/* file descriptor */
111*4887Schin 	int	e_ttyspeed;	/* line speed, also indicates tty parms are valid */
112*4887Schin 	int	e_tabcount;
113*4887Schin #ifdef _hdr_utime
114*4887Schin 	ino_t	e_tty_ino;
115*4887Schin 	dev_t	e_tty_dev;
116*4887Schin 	char	*e_tty;
117*4887Schin #endif
118*4887Schin #if SHOPT_OLDTERMIO
119*4887Schin 	char	e_echoctl;
120*4887Schin 	char	e_tcgeta;
121*4887Schin 	struct termio e_ott;
122*4887Schin #endif
123*4887Schin #if SHOPT_MULTIBYTE
124*4887Schin 	int	e_curchar;
125*4887Schin 	int	e_cursize;
126*4887Schin #endif
127*4887Schin 	int	*e_globals;	/* global variables */
128*4887Schin 	genchar	*e_window;	/* display window  image */
129*4887Schin 	char	e_inmacro;	/* processing macro expansion */
130*4887Schin #if KSHELL
131*4887Schin 	char	e_vi_insert[2];	/* for sh_keytrap */
132*4887Schin 	int32_t e_col;		/* for sh_keytrap */
133*4887Schin #else
134*4887Schin 	char	e_prbuff[PRSIZE]; /* prompt buffer */
135*4887Schin #endif /* KSHELL */
136*4887Schin 	struct termios	e_ttyparm;      /* initial tty parameters */
137*4887Schin 	struct termios	e_nttyparm;     /* raw tty parameters */
138*4887Schin 	struct termios e_savetty;	/* saved terminal state */
139*4887Schin 	int	e_savefd;	/* file descriptor for saved terminal state */
140*4887Schin 	char	e_macro[4];	/* macro buffer */
141*4887Schin 	void	*e_vi;		/* vi specific data */
142*4887Schin 	void	*e_emacs;	/* emacs specific data */
143*4887Schin 	Shell_t	*sh;		/* interpreter pointer */
144*4887Schin 	char	*e_stkptr;	/* saved stack pointer */
145*4887Schin 	int	e_stkoff;	/* saved stack offset */
146*4887Schin 	char	**e_clist;	/* completion list after <ESC>= */
147*4887Schin 	int	e_nlist;	/* number of elements on completion list */
148*4887Schin 	int	e_multiline;	/* allow multiple lines for editing */
149*4887Schin 	int	e_winsz;	/* columns in window */
150*4887Schin 	Edpos_t	e_curpos;	/* cursor line and column */
151*4887Schin 	Namval_t *e_default;	/* variable containing default value */
152*4887Schin 	Namval_t *e_term;	/* TERM variable */
153*4887Schin 	char 	e_termname[80];	/* terminal name */
154*4887Schin } Edit_t;
155*4887Schin 
156*4887Schin #undef MAXWINDOW
157*4887Schin #define MAXWINDOW	300	/* maximum width window */
158*4887Schin #define FAST	2
159*4887Schin #define SLOW	1
160*4887Schin #define ESC	cntl('[')
161*4887Schin #define	UEOF	-2			/* user eof char synonym */
162*4887Schin #define	UINTR	-3			/* user intr char synonym */
163*4887Schin #define	UERASE	-4			/* user erase char synonym */
164*4887Schin #define	UKILL	-5			/* user kill char synonym */
165*4887Schin #define	UWERASE	-6			/* user word erase char synonym */
166*4887Schin #define	ULNEXT	-7			/* user next literal char synonym */
167*4887Schin 
168*4887Schin #if ( 'a' == 97) /* ASCII? */
169*4887Schin #   define	cntl(x)		(x&037)
170*4887Schin #else
171*4887Schin #   define cntl(c) (c=='D'?55:(c=='E'?45:(c=='F'?46:(c=='G'?'\a':(c=='H'?'\b': \
172*4887Schin 		(c=='I'?'\t':(c=='J'?'\n':(c=='T'?60:(c=='U'?61:(c=='V'?50: \
173*4887Schin 		(c=='W'?38:(c=='Z'?63:(c=='['?39:(c==']'?29: \
174*4887Schin 		(c<'J'?c+1-'A':(c+10-'J'))))))))))))))))
175*4887Schin #endif
176*4887Schin 
177*4887Schin #if !KSHELL
178*4887Schin #   define STRIP	0377
179*4887Schin #   define GMACS	1
180*4887Schin #   define EMACS	2
181*4887Schin #   define VIRAW	4
182*4887Schin #   define EDITVI	8
183*4887Schin #   define NOHIST	16
184*4887Schin #   define EDITMASK	15
185*4887Schin #   define is_option(m)	(opt_flag&(m))
186*4887Schin     extern char opt_flag;
187*4887Schin #   ifdef SYSCALL
188*4887Schin #	define read(fd,buff,n)	syscall(3,fd,buff,n)
189*4887Schin #   else
190*4887Schin #	define read(fd,buff,n)	rEAd(fd,buff,n)
191*4887Schin #   endif /* SYSCALL */
192*4887Schin #endif	/* KSHELL */
193*4887Schin 
194*4887Schin extern void	ed_crlf(Edit_t*);
195*4887Schin extern void	ed_putchar(Edit_t*, int);
196*4887Schin extern void	ed_ringbell(void);
197*4887Schin extern void	ed_setup(Edit_t*,int, int);
198*4887Schin extern void	ed_flush(Edit_t*);
199*4887Schin extern int	ed_getchar(Edit_t*,int);
200*4887Schin extern int	ed_virt_to_phys(Edit_t*,genchar*,genchar*,int,int,int);
201*4887Schin extern int	ed_window(void);
202*4887Schin extern void	ed_ungetchar(Edit_t*,int);
203*4887Schin extern int	ed_viread(void*, int, char*, int, int);
204*4887Schin extern int	ed_read(void*, int, char*, int, int);
205*4887Schin extern int	ed_emacsread(void*, int, char*, int, int);
206*4887Schin extern Edpos_t	ed_curpos(Edit_t*, genchar*, int, int, Edpos_t);
207*4887Schin extern int	ed_setcursor(Edit_t*, genchar*, int, int, int);
208*4887Schin #if KSHELL
209*4887Schin 	extern int	ed_macro(Edit_t*,int);
210*4887Schin 	extern int	ed_expand(Edit_t*, char[],int*,int*,int,int);
211*4887Schin 	extern int	ed_fulledit(Edit_t*);
212*4887Schin 	extern void	*ed_open(Shell_t*);
213*4887Schin #endif /* KSHELL */
214*4887Schin #   if SHOPT_MULTIBYTE
215*4887Schin 	extern int ed_internal(const char*, genchar*);
216*4887Schin 	extern int ed_external(const genchar*, char*);
217*4887Schin 	extern void ed_gencpy(genchar*,const genchar*);
218*4887Schin 	extern void ed_genncpy(genchar*,const genchar*,int);
219*4887Schin 	extern int ed_genlen(const genchar*);
220*4887Schin 	extern int ed_setwidth(const char*);
221*4887Schin #  endif /* SHOPT_MULTIBYTE */
222*4887Schin 
223*4887Schin extern const char	e_runvi[];
224*4887Schin #if !KSHELL
225*4887Schin    extern const char	e_version[];
226*4887Schin #endif /* KSHELL */
227*4887Schin 
228*4887Schin #if SHOPT_HISTEXPAND
229*4887Schin 
230*4887Schin /* flags */
231*4887Schin 
232*4887Schin #define	HIST_EVENT	0x1	/* event designator seen */
233*4887Schin #define HIST_QUESTION	0x2	/* question mark event designator */
234*4887Schin #define	HIST_HASH	0x4	/* hash event designator */
235*4887Schin #define HIST_WORDDSGN	0x8	/* word designator seen */
236*4887Schin #define HIST_QUICKSUBST	0x10	/* quick substition designator seen */
237*4887Schin #define HIST_SUBSTITUTE	0x20	/* for substition loop */
238*4887Schin #define	HIST_NEWLINE	0x40	/* newline in squashed white space */
239*4887Schin 
240*4887Schin /* modifier flags */
241*4887Schin 
242*4887Schin #define	HIST_PRINT		0x100	/* print new command */
243*4887Schin #define	HIST_QUOTE		0x200	/* quote resulting history line */
244*4887Schin #define	HIST_QUOTE_BR		0x400	/* quote every word on space break */
245*4887Schin #define	HIST_GLOBALSUBST	0x800	/* apply substition globally */
246*4887Schin 
247*4887Schin #define	HIST_ERROR		0x1000	/* an error ocurred */
248*4887Schin 
249*4887Schin /* flags to be returned */
250*4887Schin 
251*4887Schin #define	HIST_FLAG_RETURN_MASK	(HIST_EVENT|HIST_PRINT|HIST_ERROR)
252*4887Schin 
253*4887Schin extern int hist_expand(const char *, char **);
254*4887Schin #endif
255*4887Schin 
256*4887Schin #endif
257*4887Schin #endif
258