xref: /titanic_50/usr/src/lib/libshell/common/include/edit.h (revision 8a57cef6afbd3163c64efedaa20cbfab2c46b9cd)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
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 	char	e_crlf;		/* zero if cannot return to beginning of line */
82 	char	e_nocrnl;	/* don't put a new-line with ^L */
83 	int	e_llimit;	/* line length limit */
84 	int	e_hline;	/* current history line number */
85 	int	e_hloff;	/* line number offset for command */
86 	int	e_hismin;	/* minimum history line number */
87 	int	e_hismax;	/* maximum history line number */
88 	int	e_raw;		/* set when in raw mode or alt mode */
89 	int	e_cur;		/* current line position */
90 	int	e_eol;		/* end-of-line position */
91 	int	e_pcur;		/* current physical line position */
92 	int	e_peol;		/* end of physical line position */
93 	int	e_mode;		/* edit mode */
94 	int	e_lookahead;	/* index in look-ahead buffer */
95 	int	e_repeat;
96 	int	e_saved;
97 	int	e_fcol;		/* first column */
98 	int	e_ucol;		/* column for undo */
99 	int	e_wsize;	/* width of display window */
100 	char	*e_outbase;	/* pointer to start of output buffer */
101 	char	*e_outptr;	/* pointer to position in output buffer */
102 	char	*e_outlast;	/* pointer to end of output buffer */
103 	genchar	*e_inbuf;	/* pointer to input buffer */
104 	char	*e_prompt;	/* pointer to buffer containing the prompt */
105 	genchar	*e_ubuf;	/* pointer to the undo buffer */
106 	genchar	*e_killbuf;	/* pointer to delete buffer */
107 	char	e_search[SEARCHSIZE];	/* search string */
108 	genchar	*e_Ubuf;	/* temporary workspace buffer */
109 	genchar	*e_physbuf;	/* temporary workspace buffer */
110 	int	e_lbuf[LOOKAHEAD];/* pointer to look-ahead buffer */
111 	int	e_fd;		/* file descriptor */
112 	int	e_ttyspeed;	/* line speed, also indicates tty parms are valid */
113 	int	e_tabcount;
114 #ifdef _hdr_utime
115 	ino_t	e_tty_ino;
116 	dev_t	e_tty_dev;
117 	char	*e_tty;
118 #endif
119 #if SHOPT_OLDTERMIO
120 	char	e_echoctl;
121 	char	e_tcgeta;
122 	struct termio e_ott;
123 #endif
124 #if SHOPT_MULTIBYTE
125 	int	e_curchar;
126 	int	e_cursize;
127 #endif
128 	int	*e_globals;	/* global variables */
129 	genchar	*e_window;	/* display window  image */
130 	char	e_inmacro;	/* processing macro expansion */
131 #if KSHELL
132 	char	e_vi_insert[2];	/* for sh_keytrap */
133 	int32_t e_col;		/* for sh_keytrap */
134 #else
135 	char	e_prbuff[PRSIZE]; /* prompt buffer */
136 #endif /* KSHELL */
137 	struct termios	e_ttyparm;      /* initial tty parameters */
138 	struct termios	e_nttyparm;     /* raw tty parameters */
139 	struct termios e_savetty;	/* saved terminal state */
140 	int	e_savefd;	/* file descriptor for saved terminal state */
141 	char	e_macro[4];	/* macro buffer */
142 	void	*e_vi;		/* vi specific data */
143 	void	*e_emacs;	/* emacs specific data */
144 	Shell_t	*sh;		/* interpreter pointer */
145 	char	*e_stkptr;	/* saved stack pointer */
146 	int	e_stkoff;	/* saved stack offset */
147 	char	**e_clist;	/* completion list after <ESC>= */
148 	int	e_nlist;	/* number of elements on completion list */
149 	int	e_multiline;	/* allow multiple lines for editing */
150 	int	e_winsz;	/* columns in window */
151 	Edpos_t	e_curpos;	/* cursor line and column */
152 	Namval_t *e_default;	/* variable containing default value */
153 	Namval_t *e_term;	/* TERM variable */
154 	char 	e_termname[80];	/* terminal name */
155 } Edit_t;
156 
157 #undef MAXWINDOW
158 #define MAXWINDOW	300	/* maximum width window */
159 #define FAST	2
160 #define SLOW	1
161 #define ESC	cntl('[')
162 #define	UEOF	-2			/* user eof char synonym */
163 #define	UINTR	-3			/* user intr char synonym */
164 #define	UERASE	-4			/* user erase char synonym */
165 #define	UKILL	-5			/* user kill char synonym */
166 #define	UWERASE	-6			/* user word erase char synonym */
167 #define	ULNEXT	-7			/* user next literal char synonym */
168 
169 #if ( 'a' == 97) /* ASCII? */
170 #   define	cntl(x)		(x&037)
171 #else
172 #   define cntl(c) (c=='D'?55:(c=='E'?45:(c=='F'?46:(c=='G'?'\a':(c=='H'?'\b': \
173 		(c=='I'?'\t':(c=='J'?'\n':(c=='T'?60:(c=='U'?61:(c=='V'?50: \
174 		(c=='W'?38:(c=='Z'?63:(c=='['?39:(c==']'?29: \
175 		(c<'J'?c+1-'A':(c+10-'J'))))))))))))))))
176 #endif
177 
178 #if !KSHELL
179 #   define STRIP	0377
180 #   define GMACS	1
181 #   define EMACS	2
182 #   define VIRAW	4
183 #   define EDITVI	8
184 #   define NOHIST	16
185 #   define EDITMASK	15
186 #   define is_option(m)	(opt_flag&(m))
187     extern char opt_flag;
188 #   ifdef SYSCALL
189 #	define read(fd,buff,n)	syscall(3,fd,buff,n)
190 #   else
191 #	define read(fd,buff,n)	rEAd(fd,buff,n)
192 #   endif /* SYSCALL */
193 #endif	/* KSHELL */
194 
195 extern void	ed_crlf(Edit_t*);
196 extern void	ed_putchar(Edit_t*, int);
197 extern void	ed_ringbell(void);
198 extern void	ed_setup(Edit_t*,int, int);
199 extern void	ed_flush(Edit_t*);
200 extern int	ed_getchar(Edit_t*,int);
201 extern int	ed_virt_to_phys(Edit_t*,genchar*,genchar*,int,int,int);
202 extern int	ed_window(void);
203 extern void	ed_ungetchar(Edit_t*,int);
204 extern int	ed_viread(void*, int, char*, int, int);
205 extern int	ed_read(void*, int, char*, int, int);
206 extern int	ed_emacsread(void*, int, char*, int, int);
207 extern Edpos_t	ed_curpos(Edit_t*, genchar*, int, int, Edpos_t);
208 extern int	ed_setcursor(Edit_t*, genchar*, int, int, int);
209 #if KSHELL
210 	extern int	ed_macro(Edit_t*,int);
211 	extern int	ed_expand(Edit_t*, char[],int*,int*,int,int);
212 	extern int	ed_fulledit(Edit_t*);
213 	extern void	*ed_open(Shell_t*);
214 #endif /* KSHELL */
215 #   if SHOPT_MULTIBYTE
216 	extern int ed_internal(const char*, genchar*);
217 	extern int ed_external(const genchar*, char*);
218 	extern void ed_gencpy(genchar*,const genchar*);
219 	extern void ed_genncpy(genchar*,const genchar*,int);
220 	extern int ed_genlen(const genchar*);
221 	extern int ed_setwidth(const char*);
222 #  endif /* SHOPT_MULTIBYTE */
223 
224 extern const char	e_runvi[];
225 #if !KSHELL
226    extern const char	e_version[];
227 #endif /* KSHELL */
228 
229 #if SHOPT_HISTEXPAND
230 
231 /* flags */
232 
233 #define	HIST_EVENT	0x1	/* event designator seen */
234 #define HIST_QUESTION	0x2	/* question mark event designator */
235 #define	HIST_HASH	0x4	/* hash event designator */
236 #define HIST_WORDDSGN	0x8	/* word designator seen */
237 #define HIST_QUICKSUBST	0x10	/* quick substition designator seen */
238 #define HIST_SUBSTITUTE	0x20	/* for substition loop */
239 #define	HIST_NEWLINE	0x40	/* newline in squashed white space */
240 
241 /* modifier flags */
242 
243 #define	HIST_PRINT		0x100	/* print new command */
244 #define	HIST_QUOTE		0x200	/* quote resulting history line */
245 #define	HIST_QUOTE_BR		0x400	/* quote every word on space break */
246 #define	HIST_GLOBALSUBST	0x800	/* apply substition globally */
247 
248 #define	HIST_ERROR		0x1000	/* an error ocurred */
249 
250 /* flags to be returned */
251 
252 #define	HIST_FLAG_RETURN_MASK	(HIST_EVENT|HIST_PRINT|HIST_ERROR)
253 
254 extern int hist_expand(const char *, char **);
255 #endif
256 
257 #endif
258 #endif
259