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