read.c (237448) | read.c (238173) |
---|---|
1/*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Christos Zoulas of Cornell University. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 22 unchanged lines hidden (view full) --- 31 * 32 * $NetBSD: read.c,v 1.52 2009/07/22 15:57:00 christos Exp $ 33 */ 34 35#if !defined(lint) && !defined(SCCSID) 36static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93"; 37#endif /* not lint && not SCCSID */ 38#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Christos Zoulas of Cornell University. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 22 unchanged lines hidden (view full) --- 31 * 32 * $NetBSD: read.c,v 1.52 2009/07/22 15:57:00 christos Exp $ 33 */ 34 35#if !defined(lint) && !defined(SCCSID) 36static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93"; 37#endif /* not lint && not SCCSID */ 38#include <sys/cdefs.h> |
39__FBSDID("$FreeBSD: head/lib/libedit/read.c 237448 2012-06-22 18:01:22Z pfg $"); | 39__FBSDID("$FreeBSD: head/lib/libedit/read.c 238173 2012-07-06 16:43:56Z pfg $"); |
40 41/* 42 * read.c: Clean this junk up! This is horrible code. 43 * Terminal read functions 44 */ 45#include "sys.h" 46#include <errno.h> 47#include <fcntl.h> 48#include <unistd.h> 49#include <stdlib.h> 50#include "el.h" 51 | 40 41/* 42 * read.c: Clean this junk up! This is horrible code. 43 * Terminal read functions 44 */ 45#include "sys.h" 46#include <errno.h> 47#include <fcntl.h> 48#include <unistd.h> 49#include <stdlib.h> 50#include "el.h" 51 |
52#define OKCMD -1 /* must be -1! */ | 52#define OKCMD -1 |
53 54private int read__fixio(int, int); 55private int read_preread(EditLine *); 56private int read_char(EditLine *, char *); 57private int read_getcmd(EditLine *, el_action_t *, char *); 58private void read_pop(c_macro_t *); 59 60/* read_init(): --- 104 unchanged lines hidden (view full) --- 165 e = 1; 166 } 167#endif /* FIONBIO */ 168 169#endif /* TRY_AGAIN */ 170 return (e ? 0 : -1); 171 172 case EINTR: | 53 54private int read__fixio(int, int); 55private int read_preread(EditLine *); 56private int read_char(EditLine *, char *); 57private int read_getcmd(EditLine *, el_action_t *, char *); 58private void read_pop(c_macro_t *); 59 60/* read_init(): --- 104 unchanged lines hidden (view full) --- 165 e = 1; 166 } 167#endif /* FIONBIO */ 168 169#endif /* TRY_AGAIN */ 170 return (e ? 0 : -1); 171 172 case EINTR: |
173 return (-1); | 173 return (0); |
174 175 default: 176 return (-1); 177 } 178} 179 180 181/* read_preread(): --- 48 unchanged lines hidden (view full) --- 230 * Return next command from the input stream. 231 */ 232private int 233read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch) 234{ 235 el_action_t cmd; 236 int num; 237 | 174 175 default: 176 return (-1); 177 } 178} 179 180 181/* read_preread(): --- 48 unchanged lines hidden (view full) --- 230 * Return next command from the input stream. 231 */ 232private int 233read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch) 234{ 235 el_action_t cmd; 236 int num; 237 |
238 el->el_errno = 0; | |
239 do { | 238 do { |
240 if ((num = el_getc(el, ch)) != 1) { /* if EOF or error */ 241 el->el_errno = num == 0 ? 0 : errno; | 239 if ((num = el_getc(el, ch)) != 1) /* if EOF or error */ |
242 return (num); | 240 return (num); |
243 } | |
244 245#ifdef KANJI 246 if ((*ch & 0200)) { 247 el->el_state.metanext = 0; 248 cmd = CcViMap[' ']; 249 break; 250 } else 251#endif /* KANJI */ --- 35 unchanged lines hidden (view full) --- 287 * Read a character from the tty. 288 */ 289private int 290read_char(EditLine *el, char *cp) 291{ 292 ssize_t num_read; 293 int tried = 0; 294 | 241 242#ifdef KANJI 243 if ((*ch & 0200)) { 244 el->el_state.metanext = 0; 245 cmd = CcViMap[' ']; 246 break; 247 } else 248#endif /* KANJI */ --- 35 unchanged lines hidden (view full) --- 284 * Read a character from the tty. 285 */ 286private int 287read_char(EditLine *el, char *cp) 288{ 289 ssize_t num_read; 290 int tried = 0; 291 |
295 again: 296 el->el_signal->sig_no = 0; 297 while ((num_read = read(el->el_infd, cp, 1)) == -1) { 298 if (el->el_signal->sig_no == SIGCONT) { 299 sig_set(el); 300 el_set(el, EL_REFRESH); 301 goto again; 302 } | 292 while ((num_read = read(el->el_infd, cp, 1)) == -1) |
303 if (!tried && read__fixio(el->el_infd, errno) == 0) 304 tried = 1; 305 else { 306 *cp = '\0'; 307 return (-1); 308 } | 293 if (!tried && read__fixio(el->el_infd, errno) == 0) 294 tried = 1; 295 else { 296 *cp = '\0'; 297 return (-1); 298 } |
309 } | 299 |
310 return (int)num_read; 311} 312 313/* read_pop(): 314 * Pop a macro from the stack 315 */ 316private void 317read_pop(c_macro_t *ma) --- 90 unchanged lines hidden (view full) --- 408public const char * 409el_gets(EditLine *el, int *nread) 410{ 411 int retval; 412 el_action_t cmdnum = 0; 413 int num; /* how many chars we have read at NL */ 414 char ch; 415 int crlf = 0; | 300 return (int)num_read; 301} 302 303/* read_pop(): 304 * Pop a macro from the stack 305 */ 306private void 307read_pop(c_macro_t *ma) --- 90 unchanged lines hidden (view full) --- 398public const char * 399el_gets(EditLine *el, int *nread) 400{ 401 int retval; 402 el_action_t cmdnum = 0; 403 int num; /* how many chars we have read at NL */ 404 char ch; 405 int crlf = 0; |
416 int nrb; | |
417#ifdef FIONREAD 418 c_macro_t *ma = &el->el_chared.c_macro; 419#endif /* FIONREAD */ 420 | 406#ifdef FIONREAD 407 c_macro_t *ma = &el->el_chared.c_macro; 408#endif /* FIONREAD */ 409 |
421 if (nread == NULL) 422 nread = &nrb; | |
423 *nread = 0; 424 425 if (el->el_flags & NO_TTY) { 426 char *cp = el->el_line.buffer; 427 size_t idx; 428 | 410 *nread = 0; 411 412 if (el->el_flags & NO_TTY) { 413 char *cp = el->el_line.buffer; 414 size_t idx; 415 |
429 while ((num = (*el->el_read.read_char)(el, cp)) == 1) { | 416 while ((*el->el_read.read_char)(el, cp) == 1) { |
430 /* make sure there is space for next character */ 431 if (cp + 1 >= el->el_line.limit) { 432 idx = (cp - el->el_line.buffer); 433 if (!ch_enlargebufs(el, 2)) 434 break; 435 cp = &el->el_line.buffer[idx]; 436 } 437 cp++; 438 if (el->el_flags & UNBUFFERED) 439 break; 440 if (cp[-1] == '\r' || cp[-1] == '\n') 441 break; 442 } | 417 /* make sure there is space for next character */ 418 if (cp + 1 >= el->el_line.limit) { 419 idx = (cp - el->el_line.buffer); 420 if (!ch_enlargebufs(el, 2)) 421 break; 422 cp = &el->el_line.buffer[idx]; 423 } 424 cp++; 425 if (el->el_flags & UNBUFFERED) 426 break; 427 if (cp[-1] == '\r' || cp[-1] == '\n') 428 break; 429 } |
443 if (num == -1) { 444 if (errno == EINTR) 445 cp = el->el_line.buffer; 446 el->el_errno = errno; 447 } | |
448 449 el->el_line.cursor = el->el_line.lastchar = cp; 450 *cp = '\0'; | 430 431 el->el_line.cursor = el->el_line.lastchar = cp; 432 *cp = '\0'; |
451 *nread = (int)(el->el_line.cursor - el->el_line.buffer); 452 goto done; | 433 if (nread) 434 *nread = (int)(el->el_line.cursor - el->el_line.buffer); 435 return (*nread ? el->el_line.buffer : NULL); |
453 } 454 455 456#ifdef FIONREAD 457 if (el->el_tty.t_mode == EX_IO && ma->level < 0) { 458 long chrs = 0; 459 460 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs); 461 if (chrs == 0) { 462 if (tty_rawmode(el) < 0) { | 436 } 437 438 439#ifdef FIONREAD 440 if (el->el_tty.t_mode == EX_IO && ma->level < 0) { 441 long chrs = 0; 442 443 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs); 444 if (chrs == 0) { 445 if (tty_rawmode(el) < 0) { |
463 errno = 0; 464 *nread = 0; | 446 if (nread) 447 *nread = 0; |
465 return (NULL); 466 } 467 } 468 } 469#endif /* FIONREAD */ 470 471 if ((el->el_flags & UNBUFFERED) == 0) 472 read_prepare(el); 473 474 if (el->el_flags & EDIT_DISABLED) { 475 char *cp; 476 size_t idx; | 448 return (NULL); 449 } 450 } 451 } 452#endif /* FIONREAD */ 453 454 if ((el->el_flags & UNBUFFERED) == 0) 455 read_prepare(el); 456 457 if (el->el_flags & EDIT_DISABLED) { 458 char *cp; 459 size_t idx; |
477 | |
478 if ((el->el_flags & UNBUFFERED) == 0) 479 cp = el->el_line.buffer; 480 else 481 cp = el->el_line.lastchar; 482 483 term__flush(el); 484 | 460 if ((el->el_flags & UNBUFFERED) == 0) 461 cp = el->el_line.buffer; 462 else 463 cp = el->el_line.lastchar; 464 465 term__flush(el); 466 |
485 while ((num = (*el->el_read.read_char)(el, cp)) == 1) { | 467 while ((*el->el_read.read_char)(el, cp) == 1) { |
486 /* make sure there is space next character */ 487 if (cp + 1 >= el->el_line.limit) { 488 idx = (cp - el->el_line.buffer); 489 if (!ch_enlargebufs(el, 2)) 490 break; 491 cp = &el->el_line.buffer[idx]; 492 } 493 cp++; 494 crlf = cp[-1] == '\r' || cp[-1] == '\n'; 495 if (el->el_flags & UNBUFFERED) 496 break; 497 if (crlf) 498 break; 499 } 500 | 468 /* make sure there is space next character */ 469 if (cp + 1 >= el->el_line.limit) { 470 idx = (cp - el->el_line.buffer); 471 if (!ch_enlargebufs(el, 2)) 472 break; 473 cp = &el->el_line.buffer[idx]; 474 } 475 cp++; 476 crlf = cp[-1] == '\r' || cp[-1] == '\n'; 477 if (el->el_flags & UNBUFFERED) 478 break; 479 if (crlf) 480 break; 481 } 482 |
501 if (num == -1) { 502 if (errno == EINTR) 503 cp = el->el_line.buffer; 504 el->el_errno = errno; 505 } 506 | |
507 el->el_line.cursor = el->el_line.lastchar = cp; 508 *cp = '\0'; | 483 el->el_line.cursor = el->el_line.lastchar = cp; 484 *cp = '\0'; |
509 goto done; | 485 if (nread) 486 *nread = (int)(el->el_line.cursor - el->el_line.buffer); 487 return (*nread ? el->el_line.buffer : NULL); |
510 } 511 512 for (num = OKCMD; num == OKCMD;) { /* while still editing this 513 * line */ 514#ifdef DEBUG_EDIT 515 read_debug(el); 516#endif /* DEBUG_EDIT */ 517 /* if EOF or error */ 518 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) { 519#ifdef DEBUG_READ 520 (void) fprintf(el->el_errfile, 521 "Returning from el_gets %d\n", num); 522#endif /* DEBUG_READ */ 523 break; 524 } | 488 } 489 490 for (num = OKCMD; num == OKCMD;) { /* while still editing this 491 * line */ 492#ifdef DEBUG_EDIT 493 read_debug(el); 494#endif /* DEBUG_EDIT */ 495 /* if EOF or error */ 496 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) { 497#ifdef DEBUG_READ 498 (void) fprintf(el->el_errfile, 499 "Returning from el_gets %d\n", num); 500#endif /* DEBUG_READ */ 501 break; 502 } |
525 if (el->el_errno == EINTR) { 526 el->el_line.buffer[0] = '\0'; 527 el->el_line.lastchar = 528 el->el_line.cursor = el->el_line.buffer; 529 break; 530 } | |
531 if ((unsigned int)cmdnum >= (unsigned int)el->el_map.nfunc) { /* BUG CHECK command */ 532#ifdef DEBUG_EDIT 533 (void) fprintf(el->el_errfile, 534 "ERROR: illegal command from key 0%o\r\n", ch); 535#endif /* DEBUG_EDIT */ 536 continue; /* try again */ 537 } 538 /* now do the real command */ --- 101 unchanged lines hidden (view full) --- 640 if (el->el_flags & UNBUFFERED) 641 break; 642 } 643 644 term__flush(el); /* flush any buffered output */ 645 /* make sure the tty is set up correctly */ 646 if ((el->el_flags & UNBUFFERED) == 0) { 647 read_finish(el); | 503 if ((unsigned int)cmdnum >= (unsigned int)el->el_map.nfunc) { /* BUG CHECK command */ 504#ifdef DEBUG_EDIT 505 (void) fprintf(el->el_errfile, 506 "ERROR: illegal command from key 0%o\r\n", ch); 507#endif /* DEBUG_EDIT */ 508 continue; /* try again */ 509 } 510 /* now do the real command */ --- 101 unchanged lines hidden (view full) --- 612 if (el->el_flags & UNBUFFERED) 613 break; 614 } 615 616 term__flush(el); /* flush any buffered output */ 617 /* make sure the tty is set up correctly */ 618 if ((el->el_flags & UNBUFFERED) == 0) { 619 read_finish(el); |
648 *nread = num != -1 ? num : 0; | 620 if (nread) 621 *nread = num; |
649 } else { | 622 } else { |
650 *nread = (int)(el->el_line.lastchar - el->el_line.buffer); | 623 if (nread) 624 *nread = 625 (int)(el->el_line.lastchar - el->el_line.buffer); |
651 } | 626 } |
652done: 653 if (*nread == 0) { 654 if (num == -1) { 655 *nread = -1; 656 errno = el->el_errno; 657 } 658 return NULL; 659 } else 660 return el->el_line.buffer; | 627 return (num ? el->el_line.buffer : NULL); |
661} | 628} |