rlfe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /* A front-end using readline to "cook" input lines.
  2. *
  3. * Copyright (C) 2004, 1999 Per Bothner
  4. *
  5. * This front-end program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * Some code from Johnson & Troan: "Linux Application Development"
  11. * (Addison-Wesley, 1998) was used directly or for inspiration.
  12. *
  13. * 2003-11-07 Wolfgang Taeuber <wolfgang_taeuber@agilent.com>
  14. * Specify a history file and the size of the history file with command
  15. * line options; use EDITOR/VISUAL to set vi/emacs preference.
  16. */
  17. /* PROBLEMS/TODO:
  18. *
  19. * Only tested under GNU/Linux and Mac OS 10.x; needs to be ported.
  20. *
  21. * Switching between line-editing-mode vs raw-char-mode depending on
  22. * what tcgetattr returns is inherently not robust, plus it doesn't
  23. * work when ssh/telnetting in. A better solution is possible if the
  24. * tty system can send in-line escape sequences indicating the current
  25. * mode, echo'd input, etc. That would also allow a user preference
  26. * to set different colors for prompt, input, stdout, and stderr.
  27. *
  28. * When running mc -c under the Linux console, mc does not recognize
  29. * mouse clicks, which mc does when not running under rlfe.
  30. *
  31. * Pasting selected text containing tabs is like hitting the tab character,
  32. * which invokes readline completion. We don't want this. I don't know
  33. * if this is fixable without integrating rlfe into a terminal emulator.
  34. *
  35. * Echo suppression is a kludge, but can only be avoided with better kernel
  36. * support: We need a tty mode to disable "real" echoing, while still
  37. * letting the inferior think its tty driver to doing echoing.
  38. * Stevens's book claims SCR$ and BSD4.3+ have TIOCREMOTE.
  39. *
  40. * The latest readline may have some hooks we can use to avoid having
  41. * to back up the prompt. (See HAVE_ALREADY_PROMPTED.)
  42. *
  43. * Desirable readline feature: When in cooked no-echo mode (e.g. password),
  44. * echo characters are they are types with '*', but remove them when done.
  45. *
  46. * Asynchronous output while we're editing an input line should be
  47. * inserted in the output view *before* the input line, so that the
  48. * lines being edited (with the prompt) float at the end of the input.
  49. *
  50. * A "page mode" option to emulate more/less behavior: At each page of
  51. * output, pause for a user command. This required parsing the output
  52. * to keep track of line lengths. It also requires remembering the
  53. * output, if we want an option to scroll back, which suggests that
  54. * this should be integrated with a terminal emulator like xterm.
  55. */
  56. #include <stdio.h>
  57. #include <fcntl.h>
  58. #include <sys/types.h>
  59. #include <sys/socket.h>
  60. #include <netinet/in.h>
  61. #include <arpa/inet.h>
  62. #include <signal.h>
  63. #include <netdb.h>
  64. #include <stdlib.h>
  65. #include <errno.h>
  66. #include <grp.h>
  67. #include <string.h>
  68. #include <sys/stat.h>
  69. #include <unistd.h>
  70. #include <sys/ioctl.h>
  71. #include <termios.h>
  72. #include "config.h"
  73. #include "extern.h"
  74. #if defined (HAVE_SYS_WAIT_H)
  75. # include <sys/wait.h>
  76. #endif
  77. #ifdef READLINE_LIBRARY
  78. # include "readline.h"
  79. # include "history.h"
  80. #else
  81. # include <readline/readline.h>
  82. # include <readline/history.h>
  83. #endif
  84. #ifndef COMMAND
  85. #define COMMAND "/bin/bash"
  86. #endif
  87. #ifndef COMMAND_ARGS
  88. #define COMMAND_ARGS COMMAND
  89. #endif
  90. #ifndef ALT_COMMAND
  91. #define ALT_COMMAND "/bin/sh"
  92. #endif
  93. #ifndef ALT_COMMAND_ARGS
  94. #define ALT_COMMAND_ARGS ALT_COMMAND
  95. #endif
  96. #ifndef HAVE_MEMMOVE
  97. # if __GNUC__ > 1
  98. # define memmove(d, s, n) __builtin_memcpy(d, s, n)
  99. # else
  100. # define memmove(d, s, n) memcpy(d, s, n)
  101. # endif
  102. #else
  103. # define memmove(d, s, n) memcpy(d, s, n)
  104. #endif
  105. #define APPLICATION_NAME "rlfe"
  106. static int in_from_inferior_fd;
  107. static int out_to_inferior_fd;
  108. static void set_edit_mode ();
  109. static void usage_exit ();
  110. static char *hist_file = 0;
  111. static int hist_size = 0;
  112. /* Unfortunately, we cannot safely display echo from the inferior process.
  113. The reason is that the echo bit in the pty is "owned" by the inferior,
  114. and if we try to turn it off, we could confuse the inferior.
  115. Thus, when echoing, we get echo twice: First readline echoes while
  116. we're actually editing. Then we send the line to the inferior, and the
  117. terminal driver send back an extra echo.
  118. The work-around is to remember the input lines, and when we see that
  119. line come back, we supress the output.
  120. A better solution (supposedly available on SVR4) would be a smarter
  121. terminal driver, with more flags ... */
  122. #define ECHO_SUPPRESS_MAX 1024
  123. char echo_suppress_buffer[ECHO_SUPPRESS_MAX];
  124. int echo_suppress_start = 0;
  125. int echo_suppress_limit = 0;
  126. /*#define DEBUG*/
  127. #ifdef DEBUG
  128. FILE *logfile = NULL;
  129. #define DPRINT0(FMT) (fprintf(logfile, FMT), fflush(logfile))
  130. #define DPRINT1(FMT, V1) (fprintf(logfile, FMT, V1), fflush(logfile))
  131. #define DPRINT2(FMT, V1, V2) (fprintf(logfile, FMT, V1, V2), fflush(logfile))
  132. #else
  133. #define DPRINT0(FMT) ((void) 0) /* Do nothing */
  134. #define DPRINT1(FMT, V1) ((void) 0) /* Do nothing */
  135. #define DPRINT2(FMT, V1, V2) ((void) 0) /* Do nothing */
  136. #endif
  137. struct termios orig_term;
  138. /* Pid of child process. */
  139. static pid_t child = -1;
  140. static void
  141. sig_child (int signo)
  142. {
  143. int status;
  144. wait (&status);
  145. if (hist_file != 0)
  146. {
  147. write_history (hist_file);
  148. if (hist_size)
  149. history_truncate_file (hist_file, hist_size);
  150. }
  151. DPRINT0 ("(Child process died.)\n");
  152. tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
  153. exit (0);
  154. }
  155. volatile int propagate_sigwinch = 0;
  156. /* sigwinch_handler
  157. * propagate window size changes from input file descriptor to
  158. * master side of pty.
  159. */
  160. void sigwinch_handler(int signal) {
  161. propagate_sigwinch = 1;
  162. }
  163. /* get_slave_pty() returns an integer file descriptor.
  164. * If it returns < 0, an error has occurred.
  165. * Otherwise, it has returned the slave file descriptor.
  166. */
  167. int get_slave_pty(char *name) {
  168. struct group *gptr;
  169. gid_t gid;
  170. int slave = -1;
  171. /* chown/chmod the corresponding pty, if possible.
  172. * This will only work if the process has root permissions.
  173. * Alternatively, write and exec a small setuid program that
  174. * does just this.
  175. */
  176. if ((gptr = getgrnam("tty")) != 0) {
  177. gid = gptr->gr_gid;
  178. } else {
  179. /* if the tty group does not exist, don't change the
  180. * group on the slave pty, only the owner
  181. */
  182. gid = -1;
  183. }
  184. /* Note that we do not check for errors here. If this is code
  185. * where these actions are critical, check for errors!
  186. */
  187. chown(name, getuid(), gid);
  188. /* This code only makes the slave read/writeable for the user.
  189. * If this is for an interactive shell that will want to
  190. * receive "write" and "wall" messages, OR S_IWGRP into the
  191. * second argument below.
  192. */
  193. chmod(name, S_IRUSR|S_IWUSR);
  194. /* open the corresponding slave pty */
  195. slave = open(name, O_RDWR);
  196. return (slave);
  197. }
  198. /* Certain special characters, such as ctrl/C, we want to pass directly
  199. to the inferior, rather than letting readline handle them. */
  200. static char special_chars[20];
  201. static int special_chars_count;
  202. static void
  203. add_special_char(int ch)
  204. {
  205. if (ch != 0)
  206. special_chars[special_chars_count++] = ch;
  207. }
  208. static int eof_char;
  209. static int
  210. is_special_char(int ch)
  211. {
  212. int i;
  213. #if 0
  214. if (ch == eof_char && rl_point == rl_end)
  215. return 1;
  216. #endif
  217. for (i = special_chars_count; --i >= 0; )
  218. if (special_chars[i] == ch)
  219. return 1;
  220. return 0;
  221. }
  222. static char buf[1024];
  223. /* buf[0 .. buf_count-1] is the what has been emitted on the current line.
  224. It is used as the readline prompt. */
  225. static int buf_count = 0;
  226. int do_emphasize_input = 1;
  227. int current_emphasize_input;
  228. char *start_input_mode = "\033[1m";
  229. char *end_input_mode = "\033[0m";
  230. int num_keys = 0;
  231. static void maybe_emphasize_input (int on)
  232. {
  233. if (on == current_emphasize_input
  234. || (on && ! do_emphasize_input))
  235. return;
  236. fprintf (rl_outstream, on ? start_input_mode : end_input_mode);
  237. fflush (rl_outstream);
  238. current_emphasize_input = on;
  239. }
  240. static void
  241. null_prep_terminal (int meta)
  242. {
  243. }
  244. static void
  245. null_deprep_terminal ()
  246. {
  247. maybe_emphasize_input (0);
  248. }
  249. static int
  250. pre_input_change_mode (void)
  251. {
  252. return 0;
  253. }
  254. char pending_special_char;
  255. static void
  256. line_handler (char *line)
  257. {
  258. if (line == NULL)
  259. {
  260. char buf[1];
  261. DPRINT0("saw eof!\n");
  262. buf[0] = '\004'; /* ctrl/d */
  263. write (out_to_inferior_fd, buf, 1);
  264. }
  265. else
  266. {
  267. static char enter[] = "\r";
  268. /* Send line to inferior: */
  269. int length = strlen (line);
  270. if (length > ECHO_SUPPRESS_MAX-2)
  271. {
  272. echo_suppress_start = 0;
  273. echo_suppress_limit = 0;
  274. }
  275. else
  276. {
  277. if (echo_suppress_limit + length > ECHO_SUPPRESS_MAX - 2)
  278. {
  279. if (echo_suppress_limit - echo_suppress_start + length
  280. <= ECHO_SUPPRESS_MAX - 2)
  281. {
  282. memmove (echo_suppress_buffer,
  283. echo_suppress_buffer + echo_suppress_start,
  284. echo_suppress_limit - echo_suppress_start);
  285. echo_suppress_limit -= echo_suppress_start;
  286. echo_suppress_start = 0;
  287. }
  288. else
  289. {
  290. echo_suppress_limit = 0;
  291. }
  292. echo_suppress_start = 0;
  293. }
  294. memcpy (echo_suppress_buffer + echo_suppress_limit,
  295. line, length);
  296. echo_suppress_limit += length;
  297. echo_suppress_buffer[echo_suppress_limit++] = '\r';
  298. echo_suppress_buffer[echo_suppress_limit++] = '\n';
  299. }
  300. write (out_to_inferior_fd, line, length);
  301. if (pending_special_char == 0)
  302. {
  303. write (out_to_inferior_fd, enter, sizeof(enter)-1);
  304. if (*line)
  305. add_history (line);
  306. }
  307. free (line);
  308. }
  309. rl_callback_handler_remove ();
  310. buf_count = 0;
  311. num_keys = 0;
  312. if (pending_special_char != 0)
  313. {
  314. write (out_to_inferior_fd, &pending_special_char, 1);
  315. pending_special_char = 0;
  316. }
  317. }
  318. /* Value of rl_getc_function.
  319. Use this because readline should read from stdin, not rl_instream,
  320. points to the pty (so readline has monitor its terminal modes). */
  321. int
  322. my_rl_getc (FILE *dummy)
  323. {
  324. int ch = rl_getc (stdin);
  325. if (is_special_char (ch))
  326. {
  327. pending_special_char = ch;
  328. return '\r';
  329. }
  330. return ch;
  331. }
  332. int
  333. main(int argc, char** argv)
  334. {
  335. char *path;
  336. int i;
  337. int master;
  338. char *name;
  339. int in_from_tty_fd;
  340. struct sigaction act;
  341. struct winsize ws;
  342. struct termios t;
  343. int maxfd;
  344. fd_set in_set;
  345. static char empty_string[1] = "";
  346. char *prompt = empty_string;
  347. int ioctl_err = 0;
  348. int arg_base = 1;
  349. #ifdef DEBUG
  350. logfile = fopen("/tmp/rlfe.log", "w");
  351. #endif
  352. while (arg_base<argc)
  353. {
  354. if (argv[arg_base][0] != '-')
  355. break;
  356. if (arg_base+1 >= argc )
  357. usage_exit();
  358. switch(argv[arg_base][1])
  359. {
  360. case 'h':
  361. arg_base++;
  362. hist_file = argv[arg_base];
  363. break;
  364. case 's':
  365. arg_base++;
  366. hist_size = atoi(argv[arg_base]);
  367. if (hist_size<0)
  368. usage_exit();
  369. break;
  370. default:
  371. usage_exit();
  372. }
  373. arg_base++;
  374. }
  375. if (hist_file)
  376. read_history (hist_file);
  377. set_edit_mode ();
  378. rl_readline_name = APPLICATION_NAME;
  379. if ((master = OpenPTY (&name)) < 0)
  380. {
  381. perror("ptypair: could not open master pty");
  382. exit(1);
  383. }
  384. DPRINT1("pty name: '%s'\n", name);
  385. /* set up SIGWINCH handler */
  386. act.sa_handler = sigwinch_handler;
  387. sigemptyset(&(act.sa_mask));
  388. act.sa_flags = 0;
  389. if (sigaction(SIGWINCH, &act, NULL) < 0)
  390. {
  391. perror("ptypair: could not handle SIGWINCH ");
  392. exit(1);
  393. }
  394. if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
  395. {
  396. perror("ptypair: could not get window size");
  397. exit(1);
  398. }
  399. if ((child = fork()) < 0)
  400. {
  401. perror("cannot fork");
  402. exit(1);
  403. }
  404. if (child == 0)
  405. {
  406. int slave; /* file descriptor for slave pty */
  407. /* We are in the child process */
  408. close(master);
  409. #ifdef TIOCSCTTY
  410. if ((slave = get_slave_pty(name)) < 0)
  411. {
  412. perror("ptypair: could not open slave pty");
  413. exit(1);
  414. }
  415. #endif
  416. /* We need to make this process a session group leader, because
  417. * it is on a new PTY, and things like job control simply will
  418. * not work correctly unless there is a session group leader
  419. * and process group leader (which a session group leader
  420. * automatically is). This also disassociates us from our old
  421. * controlling tty.
  422. */
  423. if (setsid() < 0)
  424. {
  425. perror("could not set session leader");
  426. }
  427. /* Tie us to our new controlling tty. */
  428. #ifdef TIOCSCTTY
  429. if (ioctl(slave, TIOCSCTTY, NULL))
  430. {
  431. perror("could not set new controlling tty");
  432. }
  433. #else
  434. if ((slave = get_slave_pty(name)) < 0)
  435. {
  436. perror("ptypair: could not open slave pty");
  437. exit(1);
  438. }
  439. #endif
  440. /* make slave pty be standard in, out, and error */
  441. dup2(slave, STDIN_FILENO);
  442. dup2(slave, STDOUT_FILENO);
  443. dup2(slave, STDERR_FILENO);
  444. /* at this point the slave pty should be standard input */
  445. if (slave > 2)
  446. {
  447. close(slave);
  448. }
  449. /* Try to restore window size; failure isn't critical */
  450. if (ioctl(STDOUT_FILENO, TIOCSWINSZ, &ws) < 0)
  451. {
  452. perror("could not restore window size");
  453. }
  454. /* now start the shell */
  455. {
  456. static char* command_args[] = { COMMAND_ARGS, NULL };
  457. static char* alt_command_args[] = { ALT_COMMAND_ARGS, NULL };
  458. if (argc <= 1)
  459. {
  460. execvp (COMMAND, command_args);
  461. execvp (ALT_COMMAND, alt_command_args);
  462. }
  463. else
  464. execvp (argv[arg_base], &argv[arg_base]);
  465. }
  466. /* should never be reached */
  467. exit(1);
  468. }
  469. /* parent */
  470. signal (SIGCHLD, sig_child);
  471. /* Note that we only set termios settings for standard input;
  472. * the master side of a pty is NOT a tty.
  473. */
  474. tcgetattr(STDIN_FILENO, &orig_term);
  475. t = orig_term;
  476. eof_char = t.c_cc[VEOF];
  477. /* add_special_char(t.c_cc[VEOF]);*/
  478. add_special_char(t.c_cc[VINTR]);
  479. add_special_char(t.c_cc[VQUIT]);
  480. add_special_char(t.c_cc[VSUSP]);
  481. #if defined (VDISCARD)
  482. add_special_char(t.c_cc[VDISCARD]);
  483. #endif
  484. t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | \
  485. ECHOK | ECHOKE | ECHONL | ECHOPRT );
  486. t.c_iflag &= ~ICRNL;
  487. t.c_iflag |= IGNBRK;
  488. t.c_cc[VMIN] = 1;
  489. t.c_cc[VTIME] = 0;
  490. tcsetattr(STDIN_FILENO, TCSANOW, &t);
  491. in_from_inferior_fd = master;
  492. out_to_inferior_fd = master;
  493. rl_instream = fdopen (master, "r");
  494. rl_getc_function = my_rl_getc;
  495. rl_prep_term_function = null_prep_terminal;
  496. rl_deprep_term_function = null_deprep_terminal;
  497. rl_pre_input_hook = pre_input_change_mode;
  498. rl_callback_handler_install (prompt, line_handler);
  499. in_from_tty_fd = STDIN_FILENO;
  500. FD_ZERO (&in_set);
  501. maxfd = in_from_inferior_fd > in_from_tty_fd ? in_from_inferior_fd
  502. : in_from_tty_fd;
  503. for (;;)
  504. {
  505. int num;
  506. FD_SET (in_from_inferior_fd, &in_set);
  507. FD_SET (in_from_tty_fd, &in_set);
  508. num = select(maxfd+1, &in_set, NULL, NULL, NULL);
  509. if (propagate_sigwinch)
  510. {
  511. struct winsize ws;
  512. if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
  513. {
  514. ioctl (master, TIOCSWINSZ, &ws);
  515. }
  516. propagate_sigwinch = 0;
  517. continue;
  518. }
  519. if (num <= 0)
  520. {
  521. perror ("select");
  522. exit (-1);
  523. }
  524. if (FD_ISSET (in_from_tty_fd, &in_set))
  525. {
  526. extern int _rl_echoing_p;
  527. struct termios term_master;
  528. int do_canon = 1;
  529. int do_icrnl = 1;
  530. int ioctl_ret;
  531. DPRINT1("[tty avail num_keys:%d]\n", num_keys);
  532. /* If we can't get tty modes for the master side of the pty, we
  533. can't handle non-canonical-mode programs. Always assume the
  534. master is in canonical echo mode if we can't tell. */
  535. ioctl_ret = tcgetattr(master, &term_master);
  536. if (ioctl_ret >= 0)
  537. {
  538. do_canon = (term_master.c_lflag & ICANON) != 0;
  539. do_icrnl = (term_master.c_lflag & ICRNL) != 0;
  540. _rl_echoing_p = (term_master.c_lflag & ECHO) != 0;
  541. DPRINT1 ("echo,canon,crnl:%03d\n",
  542. 100 * _rl_echoing_p
  543. + 10 * do_canon
  544. + 1 * do_icrnl);
  545. }
  546. else
  547. {
  548. if (ioctl_err == 0)
  549. DPRINT1("tcgetattr on master fd failed: errno = %d\n", errno);
  550. ioctl_err = 1;
  551. }
  552. if (do_canon == 0 && num_keys == 0)
  553. {
  554. char ch[10];
  555. int count = read (STDIN_FILENO, ch, sizeof(ch));
  556. DPRINT1("[read %d chars from stdin: ", count);
  557. DPRINT2(" \"%.*s\"]\n", count, ch);
  558. if (do_icrnl)
  559. {
  560. int i = count;
  561. while (--i >= 0)
  562. {
  563. if (ch[i] == '\r')
  564. ch[i] = '\n';
  565. }
  566. }
  567. maybe_emphasize_input (1);
  568. write (out_to_inferior_fd, ch, count);
  569. }
  570. else
  571. {
  572. if (num_keys == 0)
  573. {
  574. int i;
  575. /* Re-install callback handler for new prompt. */
  576. if (prompt != empty_string)
  577. free (prompt);
  578. if (prompt == NULL)
  579. {
  580. DPRINT0("New empty prompt\n");
  581. prompt = empty_string;
  582. }
  583. else
  584. {
  585. if (do_emphasize_input && buf_count > 0)
  586. {
  587. prompt = malloc (buf_count + strlen (end_input_mode)
  588. + strlen (start_input_mode) + 5);
  589. sprintf (prompt, "\001%s\002%.*s\001%s\002",
  590. end_input_mode,
  591. buf_count, buf,
  592. start_input_mode);
  593. }
  594. else
  595. {
  596. prompt = malloc (buf_count + 1);
  597. memcpy (prompt, buf, buf_count);
  598. prompt[buf_count] = '\0';
  599. }
  600. DPRINT1("New prompt '%s'\n", prompt);
  601. #if 0 /* ifdef HAVE_RL_ALREADY_PROMPTED */
  602. /* Doesn't quite work when do_emphasize_input is 1. */
  603. rl_already_prompted = buf_count > 0;
  604. #else
  605. if (buf_count > 0)
  606. write (1, "\r", 1);
  607. #endif
  608. }
  609. rl_callback_handler_install (prompt, line_handler);
  610. }
  611. num_keys++;
  612. maybe_emphasize_input (1);
  613. rl_callback_read_char ();
  614. }
  615. }
  616. else /* output from inferior. */
  617. {
  618. int i;
  619. int count;
  620. int old_count;
  621. if (buf_count > (sizeof(buf) >> 2))
  622. buf_count = 0;
  623. count = read (in_from_inferior_fd, buf+buf_count,
  624. sizeof(buf) - buf_count);
  625. DPRINT2("read %d from inferior, buf_count=%d", count, buf_count);
  626. DPRINT2(": \"%.*s\"", count, buf+buf_count);
  627. maybe_emphasize_input (0);
  628. if (count <= 0)
  629. {
  630. DPRINT0 ("(Connection closed by foreign host.)\n");
  631. tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
  632. exit (0);
  633. }
  634. old_count = buf_count;
  635. /* Look for any pending echo that we need to suppress. */
  636. while (echo_suppress_start < echo_suppress_limit
  637. && count > 0
  638. && buf[buf_count] == echo_suppress_buffer[echo_suppress_start])
  639. {
  640. count--;
  641. buf_count++;
  642. echo_suppress_start++;
  643. }
  644. DPRINT1("suppressed %d characters of echo.\n", buf_count-old_count);
  645. /* Write to the terminal anything that was not suppressed. */
  646. if (count > 0)
  647. write (1, buf + buf_count, count);
  648. /* Finally, look for a prompt candidate.
  649. * When we get around to going input (from the keyboard),
  650. * we will consider the prompt to be anything since the last
  651. * line terminator. So we need to save that text in the
  652. * initial part of buf. However, anything before the
  653. * most recent end-of-line is not interesting. */
  654. buf_count += count;
  655. #if 1
  656. for (i = buf_count; --i >= old_count; )
  657. #else
  658. for (i = buf_count - 1; i-- >= buf_count - count; )
  659. #endif
  660. {
  661. if (buf[i] == '\n' || buf[i] == '\r')
  662. {
  663. i++;
  664. memmove (buf, buf+i, buf_count - i);
  665. buf_count -= i;
  666. break;
  667. }
  668. }
  669. DPRINT2("-> i: %d, buf_count: %d\n", i, buf_count);
  670. }
  671. }
  672. }
  673. static void set_edit_mode ()
  674. {
  675. int vi = 0;
  676. char *shellopts;
  677. shellopts = getenv ("SHELLOPTS");
  678. while (shellopts != 0)
  679. {
  680. if (strncmp ("vi", shellopts, 2) == 0)
  681. {
  682. vi = 1;
  683. break;
  684. }
  685. shellopts = strchr (shellopts + 1, ':');
  686. }
  687. if (!vi)
  688. {
  689. if (getenv ("EDITOR") != 0)
  690. vi |= strcmp (getenv ("EDITOR"), "vi") == 0;
  691. }
  692. if (vi)
  693. rl_variable_bind ("editing-mode", "vi");
  694. else
  695. rl_variable_bind ("editing-mode", "emacs");
  696. }
  697. static void usage_exit ()
  698. {
  699. fprintf (stderr, "Usage: rlfe [-h histfile] [-s size] cmd [arg1] [arg2] ...\n\n");
  700. exit (1);
  701. }