10
===================================================================
11
RCS file: /cvsroot/openipmi/OpenIPMI/libedit/read.c,v
12
retrieving revision 1.1
14
--- read.c 10 Feb 2006 16:38:52 -0000 1.1
15
+++ read.c 28 Feb 2006 15:38:28 -0000
17
-/* $NetBSD: read.c,v 1.39 2005/08/02 12:11:14 christos Exp $ */
20
- * Copyright (c) 1992, 1993
21
- * The Regents of the University of California. All rights reserved.
23
- * This code is derived from software contributed to Berkeley by
24
- * Christos Zoulas of Cornell University.
26
- * Redistribution and use in source and binary forms, with or without
27
- * modification, are permitted provided that the following conditions
29
- * 1. Redistributions of source code must retain the above copyright
30
- * notice, this list of conditions and the following disclaimer.
31
- * 2. Redistributions in binary form must reproduce the above copyright
32
- * notice, this list of conditions and the following disclaimer in the
33
- * documentation and/or other materials provided with the distribution.
34
- * 3. Neither the name of the University nor the names of its contributors
35
- * may be used to endorse or promote products derived from this software
36
- * without specific prior written permission.
38
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52
-#if !defined(lint) && !defined(SCCSID)
54
-static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
56
-__RCSID("$NetBSD: read.c,v 1.39 2005/08/02 12:11:14 christos Exp $");
58
-#endif /* not lint && not SCCSID */
61
- * read.c: Clean this junk up! This is horrible code.
62
- * Terminal read functions
72
-private int read__fixio(int, int);
73
-private int read_preread(EditLine *);
74
-private int read_char(EditLine *, char *);
75
-private int read_getcmd(EditLine *, el_action_t *, char *);
78
- * Initialize the read stuff
81
-read_init(EditLine *el)
83
- /* builtin read_char */
84
- el->el_read.read_char = read_char;
90
- * Set the read char function to the one provided.
91
- * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
94
-el_read_setfn(EditLine *el, el_rfunc_t rc)
96
- el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
102
- * return the current read char function, or EL_BUILTIN_GETCFN
103
- * if it is the default one
105
-protected el_rfunc_t
106
-el_read_getfn(EditLine *el)
108
- return (el->el_read.read_char == read_char) ?
109
- EL_BUILTIN_GETCFN : el->el_read.read_char;
114
-#define MIN(A,B) ((A) < (B) ? (A) : (B))
119
-read_debug(EditLine *el)
122
- if (el->el_line.cursor > el->el_line.lastchar)
123
- (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
124
- if (el->el_line.cursor < el->el_line.buffer)
125
- (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
126
- if (el->el_line.cursor > el->el_line.limit)
127
- (void) fprintf(el->el_errfile, "cursor > limit\r\n");
128
- if (el->el_line.lastchar > el->el_line.limit)
129
- (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
130
- if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
131
- (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
133
-#endif /* DEBUG_EDIT */
137
- * Try to recover from a read error
141
-read__fixio(int fd __attribute__((__unused__)), int e)
145
- case -1: /* Make sure that the code is reachable */
152
-#endif /* EWOULDBLOCK */
154
-#if defined(POSIX) && defined(EAGAIN)
155
-#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
160
-#endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
161
-#endif /* POSIX && EAGAIN */
165
-#if defined(F_SETFL) && defined(O_NDELAY)
166
- if ((e = fcntl(fd, F_GETFL, 0)) == -1)
169
- if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
173
-#endif /* F_SETFL && O_NDELAY */
179
- if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
184
-#endif /* FIONBIO */
186
-#endif /* TRY_AGAIN */
187
- return (e ? 0 : -1);
199
- * Try to read the stuff in the input queue;
202
-read_preread(EditLine *el)
206
- if (el->el_tty.t_mode == ED_IO)
210
- (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
212
- char buf[EL_BUFSIZ];
214
- chrs = read(el->el_infd, buf,
215
- (size_t) MIN(chrs, EL_BUFSIZ - 1));
221
-#endif /* FIONREAD */
231
-el_push(EditLine *el, char *str)
233
- c_macro_t *ma = &el->el_chared.c_macro;
235
- if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
237
- if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
247
- * Return next command from the input stream.
250
-read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
256
- if ((num = el_getc(el, ch)) != 1) /* if EOF or error */
260
- if ((*ch & 0200)) {
261
- el->el_state.metanext = 0;
262
- cmd = CcViMap[' '];
267
- if (el->el_state.metanext) {
268
- el->el_state.metanext = 0;
271
- cmd = el->el_map.current[(unsigned char) *ch];
272
- if (cmd == ED_SEQUENCE_LEAD_IN) {
274
- switch (key_get(el, ch, &val)) {
279
- el_push(el, val.str);
283
- /* XXX: In the future to run a user function */
284
- RunCommand(val.str);
288
- EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
292
- if (el->el_map.alt == NULL)
293
- el->el_map.current = el->el_map.key;
294
- } while (cmd == ED_SEQUENCE_LEAD_IN);
301
- * Read a character from the tty.
304
-read_char(EditLine *el, char *cp)
309
- while ((num_read = read(el->el_infd, cp, 1)) == -1)
310
- if (!tried && read__fixio(el->el_infd, errno) == 0)
325
-el_getc(EditLine *el, char *cp)
328
- c_macro_t *ma = &el->el_chared.c_macro;
332
- if (ma->level < 0) {
333
- if (!read_preread(el))
339
- if (ma->macro[ma->level][ma->offset] == '\0') {
340
- el_free(ma->macro[ma->level--]);
344
- *cp = ma->macro[ma->level][ma->offset++] & 0377;
345
- if (ma->macro[ma->level][ma->offset] == '\0') {
346
- /* Needed for QuoteMode On */
347
- el_free(ma->macro[ma->level--]);
354
- (void) fprintf(el->el_errfile, "Turning raw mode on\n");
355
-#endif /* DEBUG_READ */
356
- if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
360
- (void) fprintf(el->el_errfile, "Reading a character\n");
361
-#endif /* DEBUG_READ */
362
- num_read = (*el->el_read.read_char)(el, cp);
364
- (void) fprintf(el->el_errfile, "Got it %c\n", *cp);
365
-#endif /* DEBUG_READ */
370
-read_prepare(EditLine *el)
372
- if (el->el_flags & HANDLE_SIGNALS)
374
- if (el->el_flags & NO_TTY)
376
- if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
379
- /* This is relatively cheap, and things go terribly wrong if
380
- we have the wrong size. */
382
- re_clear_display(el); /* reset the display stuff */
384
- re_refresh(el); /* print the prompt */
386
- if (el->el_flags & UNBUFFERED)
391
-read_finish(EditLine *el)
393
- if ((el->el_flags & UNBUFFERED) == 0)
394
- (void) tty_cookedmode(el);
395
- if (el->el_flags & HANDLE_SIGNALS)
400
-el_gets(EditLine *el, int *nread)
403
- el_action_t cmdnum = 0;
404
- int num; /* how many chars we have read at NL */
408
- c_macro_t *ma = &el->el_chared.c_macro;
409
-#endif /* FIONREAD */
411
- if (el->el_flags & NO_TTY) {
412
- char *cp = el->el_line.buffer;
415
- while ((*el->el_read.read_char)(el, cp) == 1) {
416
- /* make sure there is space for next character */
417
- if (cp + 1 >= el->el_line.limit) {
418
- idx = (cp - el->el_line.buffer);
419
- if (!ch_enlargebufs(el, 2))
421
- cp = &el->el_line.buffer[idx];
424
- if (el->el_flags & UNBUFFERED)
426
- if (cp[-1] == '\r' || cp[-1] == '\n')
430
- el->el_line.cursor = el->el_line.lastchar = cp;
433
- *nread = el->el_line.cursor - el->el_line.buffer;
434
- return (el->el_line.buffer);
439
- if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
442
- (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
444
- if (tty_rawmode(el) < 0) {
451
-#endif /* FIONREAD */
453
- if ((el->el_flags & UNBUFFERED) == 0)
456
- if (el->el_flags & EDIT_DISABLED) {
459
- if ((el->el_flags & UNBUFFERED) == 0)
460
- cp = el->el_line.buffer;
462
- cp = el->el_line.lastchar;
466
- while ((*el->el_read.read_char)(el, cp) == 1) {
467
- /* make sure there is space next character */
468
- if (cp + 1 >= el->el_line.limit) {
469
- idx = (cp - el->el_line.buffer);
470
- if (!ch_enlargebufs(el, 2))
472
- cp = &el->el_line.buffer[idx];
474
- if (*cp == 4) /* ought to be stty eof */
477
- crlf = cp[-1] == '\r' || cp[-1] == '\n';
478
- if (el->el_flags & UNBUFFERED)
484
- el->el_line.cursor = el->el_line.lastchar = cp;
487
- *nread = el->el_line.cursor - el->el_line.buffer;
488
- return (el->el_line.buffer);
491
- for (num = OKCMD; num == OKCMD;) { /* while still editing this
495
-#endif /* DEBUG_EDIT */
496
- /* if EOF or error */
497
- if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
499
- (void) fprintf(el->el_errfile,
500
- "Returning from el_gets %d\n", num);
501
-#endif /* DEBUG_READ */
504
- if ((unsigned int)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
506
- (void) fprintf(el->el_errfile,
507
- "ERROR: illegal command from key 0%o\r\n", ch);
508
-#endif /* DEBUG_EDIT */
509
- continue; /* try again */
511
- /* now do the real command */
515
- for (b = el->el_map.help; b->name; b++)
516
- if (b->func == cmdnum)
519
- (void) fprintf(el->el_errfile,
520
- "Executing %s\n", b->name);
522
- (void) fprintf(el->el_errfile,
523
- "Error command = %d\n", cmdnum);
525
-#endif /* DEBUG_READ */
526
- /* vi redo needs these way down the levels... */
527
- el->el_state.thiscmd = cmdnum;
528
- el->el_state.thisch = ch;
529
- if (el->el_map.type == MAP_VI &&
530
- el->el_map.current == el->el_map.key &&
531
- el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
532
- if (cmdnum == VI_DELETE_PREV_CHAR &&
533
- el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
534
- && isprint((unsigned char)el->el_chared.c_redo.pos[-1]))
535
- el->el_chared.c_redo.pos--;
537
- *el->el_chared.c_redo.pos++ = ch;
539
- retval = (*el->el_map.func[cmdnum]) (el, ch);
541
- (void) fprintf(el->el_errfile,
542
- "Returned state %d\n", retval );
543
-#endif /* DEBUG_READ */
545
- /* save the last command here */
546
- el->el_state.lastcmd = cmdnum;
548
- /* use any return value */
551
- re_refresh_cursor(el);
555
- re_clear_lines(el);
556
- re_clear_display(el);
563
- case CC_REFRESH_BEEP:
568
- case CC_NORM: /* normal char */
571
- case CC_ARGHACK: /* Suggested by Rich Salz */
572
- /* <rsalz@pineapple.bbn.com> */
573
- continue; /* keep going... */
575
- case CC_EOF: /* end of file typed */
576
- if ((el->el_flags & UNBUFFERED) == 0)
578
- else if (num == -1) {
579
- *el->el_line.lastchar++ = CONTROL('d');
580
- el->el_line.cursor = el->el_line.lastchar;
585
- case CC_NEWLINE: /* normal end of line */
586
- num = el->el_line.lastchar - el->el_line.buffer;
589
- case CC_FATAL: /* fatal error, reset to known state */
591
- (void) fprintf(el->el_errfile,
592
- "*** editor fatal ERROR ***\r\n\n");
593
-#endif /* DEBUG_READ */
594
- /* put (real) cursor in a known place */
595
- re_clear_display(el); /* reset the display stuff */
596
- ch_reset(el, 1); /* reset the input pointers */
597
- re_refresh(el); /* print the prompt again */
601
- default: /* functions we don't know about */
603
- (void) fprintf(el->el_errfile,
604
- "*** editor ERROR ***\r\n\n");
605
-#endif /* DEBUG_READ */
610
- el->el_state.argument = 1;
611
- el->el_state.doingarg = 0;
612
- el->el_chared.c_vcmd.action = NOP;
613
- if (el->el_flags & UNBUFFERED)
617
- term__flush(); /* flush any buffered output */
618
- /* make sure the tty is set up correctly */
619
- if ((el->el_flags & UNBUFFERED) == 0) {
625
- *nread = el->el_line.lastchar - el->el_line.buffer;
627
- return (num ? el->el_line.buffer : NULL);