Sync usage with man page.
[netbsd-mini2440.git] / dist / nvi / ip / ip_read.c
blob04090526f21b2d60076905e94072b02492990b22
1 /* $NetBSD$ */
3 /*-
4 * Copyright (c) 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "Id: ip_read.c,v 8.23 2001/06/25 15:19:24 skimo Exp (Berkeley) Date: 2001/06/25 15:19:24";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <termios.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <netinet/in.h>
29 #include "../common/common.h"
30 #include "../ex/script.h"
31 #include "../ipc/ip.h"
32 #include "extern.h"
34 extern GS *__global_list;
36 VIPFUNLIST const vipfuns[] = {
37 /* VI_C_BOL Cursor to start of line. */
38 {"", E_IPCOMMAND},
39 /* VI_C_BOTTOM 2 /* Cursor to bottom. */
40 {"", E_IPCOMMAND},
41 /* VI_C_DEL 3 /* Cursor delete. */
42 {"", E_IPCOMMAND},
43 /* VI_C_DOWN Cursor down N lines: IPO_INT. */
44 {"1", E_IPCOMMAND},
45 /* VI_C_EOL 5 /* Cursor to end of line. */
46 {"", E_IPCOMMAND},
47 /* VI_C_INSERT 6 /* Cursor: enter insert mode. */
48 {"", E_IPCOMMAND},
49 /* VI_C_LEFT 7 /* Cursor left. */
50 {"", E_IPCOMMAND},
51 /* VI_C_PGDOWN 8 /* Cursor down N pages: IPO_INT. */
52 {"1", E_IPCOMMAND},
53 /* VI_C_PGUP 9 /* Cursor up N lines: IPO_INT. */
54 {"1", E_IPCOMMAND},
55 /* VI_C_RIGHT 10 /* Cursor right. */
56 {"", E_IPCOMMAND},
57 /* VI_C_SEARCH 11 /* Cursor: search: IPO_INT, IPO_STR. */
58 {"a1", E_IPCOMMAND},
59 /* VI_C_SETTOP 12 /* Cursor: set screen top line: IPO_INT. */
60 {"1", E_IPCOMMAND},
61 /* VI_C_TOP 13 /* Cursor to top. */
62 {"", E_IPCOMMAND},
63 /* VI_C_UP 14 /* Cursor up N lines: IPO_INT. */
64 {"1", E_IPCOMMAND},
65 /* VI_EDIT 15 /* Edit a file: IPO_STR. */
66 {"a", E_IPCOMMAND},
67 /* VI_EDITOPT 16 /* Edit option: 2 * IPO_STR, IPO_INT. */
68 {"ab1", E_IPCOMMAND},
69 /* VI_EDITSPLIT 17 /* Split to a file: IPO_STR. */
70 {"a", E_IPCOMMAND},
71 /* VI_EOF 18 /* End of input (NOT ^D). */
72 {"", E_EOF},
73 /* VI_ERR 19 /* Input error. */
74 {"", E_ERR},
75 /* VI_FLAGS Flags */
76 {"1", E_FLAGS},
77 /* VI_INTERRUPT 20 /* Interrupt. */
78 {"", E_INTERRUPT},
79 /* VI_MOUSE_MOVE 21 /* Mouse click move: IPO_INT, IPO_INT. */
80 {"12", E_IPCOMMAND},
81 /* VI_QUIT 22 /* Quit. */
82 {"", E_IPCOMMAND},
83 /* VI_RESIZE Screen resize: IPO_INT, IPO_INT. */
84 {"12", E_WRESIZE},
85 /* VI_SEL_END 24 /* Select end: IPO_INT, IPO_INT. */
86 {"12", E_IPCOMMAND},
87 /* VI_SEL_START 25 /* Select start: IPO_INT, IPO_INT. */
88 {"12", E_IPCOMMAND},
89 /* VI_SIGHUP 26 /* SIGHUP. */
90 {"", E_SIGHUP},
91 /* VI_SIGTERM 27 /* SIGTERM. */
92 {"", E_SIGTERM},
93 /* VI_STRING Input string: IPO_STR. */
94 {"a", E_STRING},
95 /* VI_TAG 29 /* Tag. */
96 {"", E_IPCOMMAND},
97 /* VI_TAGAS 30 /* Tag to a string: IPO_STR. */
98 {"a", E_IPCOMMAND},
99 /* VI_TAGSPLIT 31 /* Split to a tag. */
100 {"", E_IPCOMMAND},
101 /* VI_UNDO 32 /* Undo. */
102 {"", E_IPCOMMAND},
103 /* VI_WQ 33 /* Write and quit. */
104 {"", E_IPCOMMAND},
105 /* VI_WRITE 34 /* Write. */
106 {"", E_IPCOMMAND},
107 /* VI_WRITEAS 35 /* Write as another file: IPO_STR. */
108 {"a", E_IPCOMMAND},
109 /* VI_EVENT_SUP */
112 typedef enum { INP_OK=0, INP_EOF, INP_ERR, INP_TIMEOUT } input_t;
114 static input_t ip_read __P((SCR *, IP_PRIVATE *, struct timeval *, int, int*));
115 static int ip_resize __P((SCR *, u_int32_t, u_int32_t));
116 static int ip_trans __P((SCR *, IP_PRIVATE *, EVENT *));
119 * ip_event --
120 * Return a single event.
122 * PUBLIC: int ip_event __P((SCR *, EVENT *, u_int32_t, int));
125 ip_event(SCR *sp, EVENT *evp, u_int32_t flags, int ms)
127 return ip_wevent(sp->wp, sp, evp, flags, ms);
131 * XXX probably better to require new_window to send size
132 * so we never have to call ip_wevent with sp == NULL
134 * ip_wevent --
135 * Return a single event.
137 * PUBLIC: int ip_wevent __P((WIN *, SCR *, EVENT *, u_int32_t, int));
140 ip_wevent(WIN *wp, SCR *sp, EVENT *evp, u_int32_t flags, int ms)
142 IP_PRIVATE *ipp;
143 struct timeval t, *tp;
144 int termread;
145 int nr;
147 if (LF_ISSET(EC_INTERRUPT)) { /* XXX */
148 evp->e_event = E_TIMEOUT;
149 return (0);
152 ipp = sp == NULL ? WIPP(wp) : IPP(sp);
154 /* Discard the last command. */
155 if (ipp->iskip != 0) {
156 ipp->iblen -= ipp->iskip;
157 memmove(ipp->ibuf, ipp->ibuf + ipp->iskip, ipp->iblen);
158 ipp->iskip = 0;
161 termread = F_ISSET(ipp, IP_IN_EX) ||
162 (sp && F_ISSET(sp, SC_SCR_EXWROTE));
164 /* Process possible remaining commands */
165 if (!termread && ipp->iblen >= IPO_CODE_LEN && ip_trans(sp, ipp, evp))
166 return 0;
168 /* Set timer. */
169 if (ms == 0)
170 tp = NULL;
171 else {
172 t.tv_sec = ms / 1000;
173 t.tv_usec = (ms % 1000) * 1000;
174 tp = &t;
177 /* Read input events. */
178 for (;;) {
179 switch (ip_read(sp, ipp, tp, termread, &nr)) {
180 case INP_OK:
181 if (termread) {
182 evp->e_csp = ipp->tbuf;
183 evp->e_len = nr;
184 evp->e_event = E_STRING;
185 } else if (!ip_trans(sp, ipp, evp))
186 continue;
187 break;
188 case INP_EOF:
189 evp->e_event = E_EOF;
190 break;
191 case INP_ERR:
192 evp->e_event = E_ERR;
193 break;
194 case INP_TIMEOUT:
195 evp->e_event = E_TIMEOUT;
196 break;
197 default:
198 abort();
200 break;
202 return (0);
206 * ip_read --
207 * Read characters from the input.
209 static input_t
210 ip_read(SCR *sp, IP_PRIVATE *ipp, struct timeval *tp, int termread, int *nr)
212 struct timeval poll;
213 GS *gp;
214 fd_set rdfd;
215 input_t rval;
216 size_t blen;
217 int maxfd;
218 char *bp;
219 int fd;
220 CHAR_T *wp;
221 size_t wlen;
223 gp = sp == NULL ? __global_list : sp->gp;
224 bp = ipp->ibuf + ipp->iblen;
225 blen = sizeof(ipp->ibuf) - ipp->iblen;
226 fd = termread ? ipp->t_fd : ipp->i_fd;
229 * 1: A read with an associated timeout, e.g., trying to complete
230 * a map sequence. If input exists, we fall into #2.
232 FD_ZERO(&rdfd);
233 poll.tv_sec = 0;
234 poll.tv_usec = 0;
235 if (tp != NULL) {
236 FD_SET(fd, &rdfd);
237 switch (select(fd + 1,
238 &rdfd, NULL, NULL, tp == NULL ? &poll : tp)) {
239 case 0:
240 return (INP_TIMEOUT);
241 case -1:
242 goto err;
243 default:
244 break;
249 * 2: Wait for input.
251 * Select on the command input and scripting window file descriptors.
252 * It's ugly that we wait on scripting file descriptors here, but it's
253 * the only way to keep from locking out scripting windows.
255 if (sp != NULL && F_ISSET(gp, G_SCRWIN)) {
256 FD_ZERO(&rdfd);
257 FD_SET(fd, &rdfd);
258 maxfd = fd;
259 if (sscr_check_input(sp, &rdfd, maxfd))
260 goto err;
264 * 3: Read the input.
266 switch (*nr = read(fd, termread ? (char *)ipp->tbuf : bp,
267 termread ? sizeof(ipp->tbuf)/sizeof(CHAR_T)
268 : blen)) {
269 case 0: /* EOF. */
270 rval = INP_EOF;
271 break;
272 case -1: /* Error or interrupt. */
273 err: rval = INP_ERR;
274 msgq(sp, M_SYSERR, "input");
275 break;
276 default: /* Input characters. */
277 if (!termread) ipp->iblen += *nr;
278 else {
279 CHAR2INT(sp, (char *)ipp->tbuf, *nr, wp, wlen);
280 MEMMOVEW(ipp->tbuf, wp, wlen);
282 rval = INP_OK;
283 break;
285 return (rval);
289 * ip_trans --
290 * Translate messages into events.
292 static int
293 ip_trans(SCR *sp, IP_PRIVATE *ipp, EVENT *evp)
295 u_int32_t skip, val;
296 char *fmt;
297 CHAR_T *wp;
298 size_t wlen;
300 if (ipp->ibuf[0] == CODE_OOB ||
301 ipp->ibuf[0] >= VI_EVENT_SUP)
304 * XXX: Protocol is out of sync?
306 abort();
308 fmt = vipfuns[ipp->ibuf[0]-1].format;
309 evp->e_event = vipfuns[ipp->ibuf[0]-1].e_event;
310 evp->e_ipcom = ipp->ibuf[0];
312 for (skip = IPO_CODE_LEN; *fmt != '\0'; ++fmt)
313 switch (*fmt) {
314 case '1':
315 case '2':
316 if (ipp->iblen < skip + IPO_INT_LEN)
317 return (0);
318 memcpy(&val, ipp->ibuf + skip, IPO_INT_LEN);
319 val = ntohl(val);
320 if (*fmt == '1')
321 evp->e_val1 = val;
322 else
323 evp->e_val2 = val;
324 skip += IPO_INT_LEN;
325 break;
326 case 'a':
327 case 'b':
328 if (ipp->iblen < skip + IPO_INT_LEN)
329 return (0);
330 memcpy(&val, ipp->ibuf + skip, IPO_INT_LEN);
331 val = ntohl(val);
332 skip += IPO_INT_LEN;
333 if (ipp->iblen < skip + val)
334 return (0);
335 if (*fmt == 'a') {
336 CHAR2INT(sp, ipp->ibuf + skip, val,
337 wp, wlen);
338 MEMCPYW(ipp->tbuf, wp, wlen);
339 evp->e_str1 = ipp->tbuf;
340 evp->e_len1 = wlen;
341 } else {
342 CHAR2INT(sp, ipp->ibuf + skip, val,
343 wp, wlen);
344 MEMCPYW(ipp->tbuf, wp, wlen);
345 evp->e_str2 = ipp->tbuf;
346 evp->e_len2 = wlen;
348 skip += val;
349 break;
352 ipp->iskip = skip;
354 if (evp->e_event == E_WRESIZE)
355 (void)ip_resize(sp, evp->e_val1, evp->e_val2);
357 return (1);
361 * ip_resize --
362 * Reset the options for a resize event.
364 static int
365 ip_resize(SCR *sp, u_int32_t lines, u_int32_t columns)
367 GS *gp;
368 int rval;
371 * XXX
372 * The IP screen has to know the lines and columns before anything
373 * else happens. So, we may not have a valid SCR pointer, and we
374 * have to deal with that.
376 if (sp == NULL) {
377 gp = __global_list;
378 OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = lines;
379 OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = columns;
380 return (0);
383 rval = api_opts_set(sp, L("lines"), NULL, lines, 0);
384 if (api_opts_set(sp, L("columns"), NULL, columns, 0))
385 rval = 1;
386 return (rval);