rltty.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /* rltty.c -- functions to prepare and restore the terminal for readline's
  2. use. */
  3. /* Copyright (C) 1992-2005 Free Software Foundation, Inc.
  4. This file is part of the GNU Readline Library (Readline), a library
  5. for reading lines of text with interactive input and history editing.
  6. Readline is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Readline is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define READLINE_LIBRARY
  18. #if defined (HAVE_CONFIG_H)
  19. # include <config.h>
  20. #endif
  21. #include <sys/types.h>
  22. #include <signal.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #if defined (HAVE_UNISTD_H)
  26. # include <unistd.h>
  27. #endif /* HAVE_UNISTD_H */
  28. #include "rldefs.h"
  29. #if defined (GWINSZ_IN_SYS_IOCTL)
  30. # include <sys/ioctl.h>
  31. #endif /* GWINSZ_IN_SYS_IOCTL */
  32. #include "rltty.h"
  33. #include "readline.h"
  34. #include "rlprivate.h"
  35. #if !defined (errno)
  36. extern int errno;
  37. #endif /* !errno */
  38. rl_vintfunc_t *rl_prep_term_function = rl_prep_terminal;
  39. rl_voidfunc_t *rl_deprep_term_function = rl_deprep_terminal;
  40. static void set_winsize PARAMS((int));
  41. /* **************************************************************** */
  42. /* */
  43. /* Saving and Restoring the TTY */
  44. /* */
  45. /* **************************************************************** */
  46. /* Non-zero means that the terminal is in a prepped state. */
  47. static int terminal_prepped;
  48. static _RL_TTY_CHARS _rl_tty_chars, _rl_last_tty_chars;
  49. /* If non-zero, means that this process has called tcflow(fd, TCOOFF)
  50. and output is suspended. */
  51. #if defined (__ksr1__)
  52. static int ksrflow;
  53. #endif
  54. /* Dummy call to force a backgrounded readline to stop before it tries
  55. to get the tty settings. */
  56. static void
  57. set_winsize (tty)
  58. int tty;
  59. {
  60. #if defined (TIOCGWINSZ)
  61. struct winsize w;
  62. if (ioctl (tty, TIOCGWINSZ, &w) == 0)
  63. (void) ioctl (tty, TIOCSWINSZ, &w);
  64. #endif /* TIOCGWINSZ */
  65. }
  66. #if defined (NO_TTY_DRIVER)
  67. /* Nothing */
  68. #elif defined (NEW_TTY_DRIVER)
  69. /* Values for the `flags' field of a struct bsdtty. This tells which
  70. elements of the struct bsdtty have been fetched from the system and
  71. are valid. */
  72. #define SGTTY_SET 0x01
  73. #define LFLAG_SET 0x02
  74. #define TCHARS_SET 0x04
  75. #define LTCHARS_SET 0x08
  76. struct bsdtty {
  77. struct sgttyb sgttyb; /* Basic BSD tty driver information. */
  78. int lflag; /* Local mode flags, like LPASS8. */
  79. #if defined (TIOCGETC)
  80. struct tchars tchars; /* Terminal special characters, including ^S and ^Q. */
  81. #endif
  82. #if defined (TIOCGLTC)
  83. struct ltchars ltchars; /* 4.2 BSD editing characters */
  84. #endif
  85. int flags; /* Bitmap saying which parts of the struct are valid. */
  86. };
  87. #define TIOTYPE struct bsdtty
  88. static TIOTYPE otio;
  89. static void save_tty_chars PARAMS((TIOTYPE *));
  90. static int _get_tty_settings PARAMS((int, TIOTYPE *));
  91. static int get_tty_settings PARAMS((int, TIOTYPE *));
  92. static int _set_tty_settings PARAMS((int, TIOTYPE *));
  93. static int set_tty_settings PARAMS((int, TIOTYPE *));
  94. static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));
  95. static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t));
  96. static void
  97. save_tty_chars (tiop)
  98. TIOTYPE *tiop;
  99. {
  100. _rl_last_tty_chars = _rl_tty_chars;
  101. if (tiop->flags & SGTTY_SET)
  102. {
  103. _rl_tty_chars.t_erase = tiop->sgttyb.sg_erase;
  104. _rl_tty_chars.t_kill = tiop->sgttyb.sg_kill;
  105. }
  106. if (tiop->flags & TCHARS_SET)
  107. {
  108. _rl_intr_char = _rl_tty_chars.t_intr = tiop->tchars.t_intrc;
  109. _rl_quit_char = _rl_tty_chars.t_quit = tiop->tchars.t_quitc;
  110. _rl_tty_chars.t_start = tiop->tchars.t_startc;
  111. _rl_tty_chars.t_stop = tiop->tchars.t_stopc;
  112. _rl_tty_chars.t_eof = tiop->tchars.t_eofc;
  113. _rl_tty_chars.t_eol = '\n';
  114. _rl_tty_chars.t_eol2 = tiop->tchars.t_brkc;
  115. }
  116. if (tiop->flags & LTCHARS_SET)
  117. {
  118. _rl_susp_char = _rl_tty_chars.t_susp = tiop->ltchars.t_suspc;
  119. _rl_tty_chars.t_dsusp = tiop->ltchars.t_dsuspc;
  120. _rl_tty_chars.t_reprint = tiop->ltchars.t_rprntc;
  121. _rl_tty_chars.t_flush = tiop->ltchars.t_flushc;
  122. _rl_tty_chars.t_werase = tiop->ltchars.t_werasc;
  123. _rl_tty_chars.t_lnext = tiop->ltchars.t_lnextc;
  124. }
  125. _rl_tty_chars.t_status = -1;
  126. }
  127. static int
  128. get_tty_settings (tty, tiop)
  129. int tty;
  130. TIOTYPE *tiop;
  131. {
  132. set_winsize (tty);
  133. tiop->flags = tiop->lflag = 0;
  134. errno = 0;
  135. if (ioctl (tty, TIOCGETP, &(tiop->sgttyb)) < 0)
  136. return -1;
  137. tiop->flags |= SGTTY_SET;
  138. #if defined (TIOCLGET)
  139. if (ioctl (tty, TIOCLGET, &(tiop->lflag)) == 0)
  140. tiop->flags |= LFLAG_SET;
  141. #endif
  142. #if defined (TIOCGETC)
  143. if (ioctl (tty, TIOCGETC, &(tiop->tchars)) == 0)
  144. tiop->flags |= TCHARS_SET;
  145. #endif
  146. #if defined (TIOCGLTC)
  147. if (ioctl (tty, TIOCGLTC, &(tiop->ltchars)) == 0)
  148. tiop->flags |= LTCHARS_SET;
  149. #endif
  150. return 0;
  151. }
  152. static int
  153. set_tty_settings (tty, tiop)
  154. int tty;
  155. TIOTYPE *tiop;
  156. {
  157. if (tiop->flags & SGTTY_SET)
  158. {
  159. ioctl (tty, TIOCSETN, &(tiop->sgttyb));
  160. tiop->flags &= ~SGTTY_SET;
  161. }
  162. _rl_echoing_p = 1;
  163. #if defined (TIOCLSET)
  164. if (tiop->flags & LFLAG_SET)
  165. {
  166. ioctl (tty, TIOCLSET, &(tiop->lflag));
  167. tiop->flags &= ~LFLAG_SET;
  168. }
  169. #endif
  170. #if defined (TIOCSETC)
  171. if (tiop->flags & TCHARS_SET)
  172. {
  173. ioctl (tty, TIOCSETC, &(tiop->tchars));
  174. tiop->flags &= ~TCHARS_SET;
  175. }
  176. #endif
  177. #if defined (TIOCSLTC)
  178. if (tiop->flags & LTCHARS_SET)
  179. {
  180. ioctl (tty, TIOCSLTC, &(tiop->ltchars));
  181. tiop->flags &= ~LTCHARS_SET;
  182. }
  183. #endif
  184. return 0;
  185. }
  186. static void
  187. prepare_terminal_settings (meta_flag, oldtio, tiop)
  188. int meta_flag;
  189. TIOTYPE oldtio, *tiop;
  190. {
  191. _rl_echoing_p = (oldtio.sgttyb.sg_flags & ECHO);
  192. _rl_echoctl = (oldtio.sgttyb.sg_flags & ECHOCTL);
  193. /* Copy the original settings to the structure we're going to use for
  194. our settings. */
  195. tiop->sgttyb = oldtio.sgttyb;
  196. tiop->lflag = oldtio.lflag;
  197. #if defined (TIOCGETC)
  198. tiop->tchars = oldtio.tchars;
  199. #endif
  200. #if defined (TIOCGLTC)
  201. tiop->ltchars = oldtio.ltchars;
  202. #endif
  203. tiop->flags = oldtio.flags;
  204. /* First, the basic settings to put us into character-at-a-time, no-echo
  205. input mode. */
  206. tiop->sgttyb.sg_flags &= ~(ECHO | CRMOD);
  207. tiop->sgttyb.sg_flags |= CBREAK;
  208. /* If this terminal doesn't care how the 8th bit is used, then we can
  209. use it for the meta-key. If only one of even or odd parity is
  210. specified, then the terminal is using parity, and we cannot. */
  211. #if !defined (ANYP)
  212. # define ANYP (EVENP | ODDP)
  213. #endif
  214. if (((oldtio.sgttyb.sg_flags & ANYP) == ANYP) ||
  215. ((oldtio.sgttyb.sg_flags & ANYP) == 0))
  216. {
  217. tiop->sgttyb.sg_flags |= ANYP;
  218. /* Hack on local mode flags if we can. */
  219. #if defined (TIOCLGET)
  220. # if defined (LPASS8)
  221. tiop->lflag |= LPASS8;
  222. # endif /* LPASS8 */
  223. #endif /* TIOCLGET */
  224. }
  225. #if defined (TIOCGETC)
  226. # if defined (USE_XON_XOFF)
  227. /* Get rid of terminal output start and stop characters. */
  228. tiop->tchars.t_stopc = -1; /* C-s */
  229. tiop->tchars.t_startc = -1; /* C-q */
  230. /* If there is an XON character, bind it to restart the output. */
  231. if (oldtio.tchars.t_startc != -1)
  232. rl_bind_key (oldtio.tchars.t_startc, rl_restart_output);
  233. # endif /* USE_XON_XOFF */
  234. /* If there is an EOF char, bind _rl_eof_char to it. */
  235. if (oldtio.tchars.t_eofc != -1)
  236. _rl_eof_char = oldtio.tchars.t_eofc;
  237. # if defined (NO_KILL_INTR)
  238. /* Get rid of terminal-generated SIGQUIT and SIGINT. */
  239. tiop->tchars.t_quitc = -1; /* C-\ */
  240. tiop->tchars.t_intrc = -1; /* C-c */
  241. # endif /* NO_KILL_INTR */
  242. #endif /* TIOCGETC */
  243. #if defined (TIOCGLTC)
  244. /* Make the interrupt keys go away. Just enough to make people happy. */
  245. tiop->ltchars.t_dsuspc = -1; /* C-y */
  246. tiop->ltchars.t_lnextc = -1; /* C-v */
  247. #endif /* TIOCGLTC */
  248. }
  249. #else /* !defined (NEW_TTY_DRIVER) */
  250. #if !defined (VMIN)
  251. # define VMIN VEOF
  252. #endif
  253. #if !defined (VTIME)
  254. # define VTIME VEOL
  255. #endif
  256. #if defined (TERMIOS_TTY_DRIVER)
  257. # define TIOTYPE struct termios
  258. # define DRAIN_OUTPUT(fd) tcdrain (fd)
  259. # define GETATTR(tty, tiop) (tcgetattr (tty, tiop))
  260. # ifdef M_UNIX
  261. # define SETATTR(tty, tiop) (tcsetattr (tty, TCSANOW, tiop))
  262. # else
  263. # define SETATTR(tty, tiop) (tcsetattr (tty, TCSADRAIN, tiop))
  264. # endif /* !M_UNIX */
  265. #else
  266. # define TIOTYPE struct termio
  267. # define DRAIN_OUTPUT(fd)
  268. # define GETATTR(tty, tiop) (ioctl (tty, TCGETA, tiop))
  269. # define SETATTR(tty, tiop) (ioctl (tty, TCSETAW, tiop))
  270. #endif /* !TERMIOS_TTY_DRIVER */
  271. static TIOTYPE otio;
  272. static void save_tty_chars PARAMS((TIOTYPE *));
  273. static int _get_tty_settings PARAMS((int, TIOTYPE *));
  274. static int get_tty_settings PARAMS((int, TIOTYPE *));
  275. static int _set_tty_settings PARAMS((int, TIOTYPE *));
  276. static int set_tty_settings PARAMS((int, TIOTYPE *));
  277. static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));
  278. static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t));
  279. static void _rl_bind_tty_special_chars PARAMS((Keymap, TIOTYPE));
  280. #if defined (FLUSHO)
  281. # define OUTPUT_BEING_FLUSHED(tp) (tp->c_lflag & FLUSHO)
  282. #else
  283. # define OUTPUT_BEING_FLUSHED(tp) 0
  284. #endif
  285. static void
  286. save_tty_chars (tiop)
  287. TIOTYPE *tiop;
  288. {
  289. _rl_last_tty_chars = _rl_tty_chars;
  290. _rl_tty_chars.t_eof = tiop->c_cc[VEOF];
  291. _rl_tty_chars.t_eol = tiop->c_cc[VEOL];
  292. #ifdef VEOL2
  293. _rl_tty_chars.t_eol2 = tiop->c_cc[VEOL2];
  294. #endif
  295. _rl_tty_chars.t_erase = tiop->c_cc[VERASE];
  296. #ifdef VWERASE
  297. _rl_tty_chars.t_werase = tiop->c_cc[VWERASE];
  298. #endif
  299. _rl_tty_chars.t_kill = tiop->c_cc[VKILL];
  300. #ifdef VREPRINT
  301. _rl_tty_chars.t_reprint = tiop->c_cc[VREPRINT];
  302. #endif
  303. _rl_intr_char = _rl_tty_chars.t_intr = tiop->c_cc[VINTR];
  304. _rl_quit_char = _rl_tty_chars.t_quit = tiop->c_cc[VQUIT];
  305. #ifdef VSUSP
  306. _rl_susp_char = _rl_tty_chars.t_susp = tiop->c_cc[VSUSP];
  307. #endif
  308. #ifdef VDSUSP
  309. _rl_tty_chars.t_dsusp = tiop->c_cc[VDSUSP];
  310. #endif
  311. #ifdef VSTART
  312. _rl_tty_chars.t_start = tiop->c_cc[VSTART];
  313. #endif
  314. #ifdef VSTOP
  315. _rl_tty_chars.t_stop = tiop->c_cc[VSTOP];
  316. #endif
  317. #ifdef VLNEXT
  318. _rl_tty_chars.t_lnext = tiop->c_cc[VLNEXT];
  319. #endif
  320. #ifdef VDISCARD
  321. _rl_tty_chars.t_flush = tiop->c_cc[VDISCARD];
  322. #endif
  323. #ifdef VSTATUS
  324. _rl_tty_chars.t_status = tiop->c_cc[VSTATUS];
  325. #endif
  326. }
  327. #if defined (_AIX) || defined (_AIX41)
  328. /* Currently this is only used on AIX */
  329. static void
  330. rltty_warning (msg)
  331. char *msg;
  332. {
  333. _rl_errmsg ("warning: %s", msg);
  334. }
  335. #endif
  336. #if defined (_AIX)
  337. void
  338. setopost(tp)
  339. TIOTYPE *tp;
  340. {
  341. if ((tp->c_oflag & OPOST) == 0)
  342. {
  343. _rl_errmsg ("warning: turning on OPOST for terminal\r");
  344. tp->c_oflag |= OPOST|ONLCR;
  345. }
  346. }
  347. #endif
  348. static int
  349. _get_tty_settings (tty, tiop)
  350. int tty;
  351. TIOTYPE *tiop;
  352. {
  353. int ioctl_ret;
  354. while (1)
  355. {
  356. ioctl_ret = GETATTR (tty, tiop);
  357. if (ioctl_ret < 0)
  358. {
  359. if (errno != EINTR)
  360. return -1;
  361. else
  362. continue;
  363. }
  364. if (OUTPUT_BEING_FLUSHED (tiop))
  365. {
  366. #if defined (FLUSHO)
  367. _rl_errmsg ("warning: turning off output flushing");
  368. tiop->c_lflag &= ~FLUSHO;
  369. break;
  370. #else
  371. continue;
  372. #endif
  373. }
  374. break;
  375. }
  376. return 0;
  377. }
  378. static int
  379. get_tty_settings (tty, tiop)
  380. int tty;
  381. TIOTYPE *tiop;
  382. {
  383. set_winsize (tty);
  384. errno = 0;
  385. if (_get_tty_settings (tty, tiop) < 0)
  386. return -1;
  387. #if defined (_AIX)
  388. setopost(tiop);
  389. #endif
  390. return 0;
  391. }
  392. static int
  393. _set_tty_settings (tty, tiop)
  394. int tty;
  395. TIOTYPE *tiop;
  396. {
  397. while (SETATTR (tty, tiop) < 0)
  398. {
  399. if (errno != EINTR)
  400. return -1;
  401. errno = 0;
  402. }
  403. return 0;
  404. }
  405. static int
  406. set_tty_settings (tty, tiop)
  407. int tty;
  408. TIOTYPE *tiop;
  409. {
  410. if (_set_tty_settings (tty, tiop) < 0)
  411. return -1;
  412. #if 0
  413. #if defined (TERMIOS_TTY_DRIVER)
  414. # if defined (__ksr1__)
  415. if (ksrflow)
  416. {
  417. ksrflow = 0;
  418. tcflow (tty, TCOON);
  419. }
  420. # else /* !ksr1 */
  421. tcflow (tty, TCOON); /* Simulate a ^Q. */
  422. # endif /* !ksr1 */
  423. #else
  424. ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
  425. #endif /* !TERMIOS_TTY_DRIVER */
  426. #endif /* 0 */
  427. return 0;
  428. }
  429. static void
  430. prepare_terminal_settings (meta_flag, oldtio, tiop)
  431. int meta_flag;
  432. TIOTYPE oldtio, *tiop;
  433. {
  434. _rl_echoing_p = (oldtio.c_lflag & ECHO);
  435. #if defined (ECHOCTL)
  436. _rl_echoctl = (oldtio.c_lflag & ECHOCTL);
  437. #endif
  438. tiop->c_lflag &= ~(ICANON | ECHO);
  439. if ((unsigned char) oldtio.c_cc[VEOF] != (unsigned char) _POSIX_VDISABLE)
  440. _rl_eof_char = oldtio.c_cc[VEOF];
  441. #if defined (USE_XON_XOFF)
  442. #if defined (IXANY)
  443. tiop->c_iflag &= ~(IXON | IXOFF | IXANY);
  444. #else
  445. /* `strict' Posix systems do not define IXANY. */
  446. tiop->c_iflag &= ~(IXON | IXOFF);
  447. #endif /* IXANY */
  448. #endif /* USE_XON_XOFF */
  449. /* Only turn this off if we are using all 8 bits. */
  450. if (((tiop->c_cflag & CSIZE) == CS8) || meta_flag)
  451. tiop->c_iflag &= ~(ISTRIP | INPCK);
  452. /* Make sure we differentiate between CR and NL on input. */
  453. tiop->c_iflag &= ~(ICRNL | INLCR);
  454. #if !defined (HANDLE_SIGNALS)
  455. tiop->c_lflag &= ~ISIG;
  456. #else
  457. tiop->c_lflag |= ISIG;
  458. #endif
  459. tiop->c_cc[VMIN] = 1;
  460. tiop->c_cc[VTIME] = 0;
  461. #if defined (FLUSHO)
  462. if (OUTPUT_BEING_FLUSHED (tiop))
  463. {
  464. tiop->c_lflag &= ~FLUSHO;
  465. oldtio.c_lflag &= ~FLUSHO;
  466. }
  467. #endif
  468. /* Turn off characters that we need on Posix systems with job control,
  469. just to be sure. This includes ^Y and ^V. This should not really
  470. be necessary. */
  471. #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_VDISABLE)
  472. #if defined (VLNEXT)
  473. tiop->c_cc[VLNEXT] = _POSIX_VDISABLE;
  474. #endif
  475. #if defined (VDSUSP)
  476. tiop->c_cc[VDSUSP] = _POSIX_VDISABLE;
  477. #endif
  478. #endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */
  479. }
  480. #endif /* !NEW_TTY_DRIVER */
  481. /* Put the terminal in CBREAK mode so that we can detect key presses. */
  482. #if defined (NO_TTY_DRIVER)
  483. void
  484. rl_prep_terminal (meta_flag)
  485. int meta_flag;
  486. {
  487. _rl_echoing_p = 1;
  488. }
  489. void
  490. rl_deprep_terminal ()
  491. {
  492. }
  493. #else /* ! NO_TTY_DRIVER */
  494. void
  495. rl_prep_terminal (meta_flag)
  496. int meta_flag;
  497. {
  498. int tty;
  499. TIOTYPE tio;
  500. if (terminal_prepped)
  501. return;
  502. /* Try to keep this function from being INTerrupted. */
  503. _rl_block_sigint ();
  504. tty = rl_instream ? fileno (rl_instream) : fileno (stdin);
  505. if (get_tty_settings (tty, &tio) < 0)
  506. {
  507. #if defined (ENOTSUP)
  508. /* MacOS X and Linux, at least, lie about the value of errno if
  509. tcgetattr fails. */
  510. if (errno == ENOTTY || errno == EINVAL || errno == ENOTSUP)
  511. #else
  512. if (errno == ENOTTY || errno == EINVAL)
  513. #endif
  514. _rl_echoing_p = 1; /* XXX */
  515. _rl_release_sigint ();
  516. return;
  517. }
  518. otio = tio;
  519. if (_rl_bind_stty_chars)
  520. {
  521. #if defined (VI_MODE)
  522. /* If editing in vi mode, make sure we restore the bindings in the
  523. insertion keymap no matter what keymap we ended up in. */
  524. if (rl_editing_mode == vi_mode)
  525. rl_tty_unset_default_bindings (vi_insertion_keymap);
  526. else
  527. #endif
  528. rl_tty_unset_default_bindings (_rl_keymap);
  529. }
  530. save_tty_chars (&otio);
  531. RL_SETSTATE(RL_STATE_TTYCSAVED);
  532. if (_rl_bind_stty_chars)
  533. {
  534. #if defined (VI_MODE)
  535. /* If editing in vi mode, make sure we set the bindings in the
  536. insertion keymap no matter what keymap we ended up in. */
  537. if (rl_editing_mode == vi_mode)
  538. _rl_bind_tty_special_chars (vi_insertion_keymap, tio);
  539. else
  540. #endif
  541. _rl_bind_tty_special_chars (_rl_keymap, tio);
  542. }
  543. prepare_terminal_settings (meta_flag, otio, &tio);
  544. if (set_tty_settings (tty, &tio) < 0)
  545. {
  546. _rl_release_sigint ();
  547. return;
  548. }
  549. if (_rl_enable_keypad)
  550. _rl_control_keypad (1);
  551. fflush (rl_outstream);
  552. terminal_prepped = 1;
  553. RL_SETSTATE(RL_STATE_TERMPREPPED);
  554. _rl_release_sigint ();
  555. }
  556. /* Restore the terminal's normal settings and modes. */
  557. void
  558. rl_deprep_terminal ()
  559. {
  560. int tty;
  561. if (!terminal_prepped)
  562. return;
  563. /* Try to keep this function from being interrupted. */
  564. _rl_block_sigint ();
  565. tty = rl_instream ? fileno (rl_instream) : fileno (stdout);
  566. if (_rl_enable_keypad)
  567. _rl_control_keypad (0);
  568. fflush (rl_outstream);
  569. if (set_tty_settings (tty, &otio) < 0)
  570. {
  571. _rl_release_sigint ();
  572. return;
  573. }
  574. terminal_prepped = 0;
  575. RL_UNSETSTATE(RL_STATE_TERMPREPPED);
  576. _rl_release_sigint ();
  577. }
  578. #endif /* !NO_TTY_DRIVER */
  579. /* **************************************************************** */
  580. /* */
  581. /* Bogus Flow Control */
  582. /* */
  583. /* **************************************************************** */
  584. int
  585. rl_restart_output (count, key)
  586. int count, key;
  587. {
  588. #if defined (__MINGW32__)
  589. return 0;
  590. #else /* !__MING32__ */
  591. int fildes = fileno (rl_outstream);
  592. #if defined (TIOCSTART)
  593. #if defined (apollo)
  594. ioctl (&fildes, TIOCSTART, 0);
  595. #else
  596. ioctl (fildes, TIOCSTART, 0);
  597. #endif /* apollo */
  598. #else /* !TIOCSTART */
  599. # if defined (TERMIOS_TTY_DRIVER)
  600. # if defined (__ksr1__)
  601. if (ksrflow)
  602. {
  603. ksrflow = 0;
  604. tcflow (fildes, TCOON);
  605. }
  606. # else /* !ksr1 */
  607. tcflow (fildes, TCOON); /* Simulate a ^Q. */
  608. # endif /* !ksr1 */
  609. # else /* !TERMIOS_TTY_DRIVER */
  610. # if defined (TCXONC)
  611. ioctl (fildes, TCXONC, TCOON);
  612. # endif /* TCXONC */
  613. # endif /* !TERMIOS_TTY_DRIVER */
  614. #endif /* !TIOCSTART */
  615. return 0;
  616. #endif /* !__MINGW32__ */
  617. }
  618. int
  619. rl_stop_output (count, key)
  620. int count, key;
  621. {
  622. #if defined (__MINGW32__)
  623. return 0;
  624. #else
  625. int fildes = fileno (rl_instream);
  626. #if defined (TIOCSTOP)
  627. # if defined (apollo)
  628. ioctl (&fildes, TIOCSTOP, 0);
  629. # else
  630. ioctl (fildes, TIOCSTOP, 0);
  631. # endif /* apollo */
  632. #else /* !TIOCSTOP */
  633. # if defined (TERMIOS_TTY_DRIVER)
  634. # if defined (__ksr1__)
  635. ksrflow = 1;
  636. # endif /* ksr1 */
  637. tcflow (fildes, TCOOFF);
  638. # else
  639. # if defined (TCXONC)
  640. ioctl (fildes, TCXONC, TCOON);
  641. # endif /* TCXONC */
  642. # endif /* !TERMIOS_TTY_DRIVER */
  643. #endif /* !TIOCSTOP */
  644. return 0;
  645. #endif /* !__MINGW32__ */
  646. }
  647. /* **************************************************************** */
  648. /* */
  649. /* Default Key Bindings */
  650. /* */
  651. /* **************************************************************** */
  652. #if !defined (NO_TTY_DRIVER)
  653. #define SET_SPECIAL(sc, func) set_special_char(kmap, &ttybuff, sc, func)
  654. #endif
  655. #if defined (NO_TTY_DRIVER)
  656. #define SET_SPECIAL(sc, func)
  657. #define RESET_SPECIAL(c)
  658. #elif defined (NEW_TTY_DRIVER)
  659. static void
  660. set_special_char (kmap, tiop, sc, func)
  661. Keymap kmap;
  662. TIOTYPE *tiop;
  663. int sc;
  664. rl_command_func_t *func;
  665. {
  666. if (sc != -1 && kmap[(unsigned char)sc].type == ISFUNC)
  667. kmap[(unsigned char)sc].function = func;
  668. }
  669. #define RESET_SPECIAL(c) \
  670. if (c != -1 && kmap[(unsigned char)c].type == ISFUNC) \
  671. kmap[(unsigned char)c].function = rl_insert;
  672. static void
  673. _rl_bind_tty_special_chars (kmap, ttybuff)
  674. Keymap kmap;
  675. TIOTYPE ttybuff;
  676. {
  677. if (ttybuff.flags & SGTTY_SET)
  678. {
  679. SET_SPECIAL (ttybuff.sgttyb.sg_erase, rl_rubout);
  680. SET_SPECIAL (ttybuff.sgttyb.sg_kill, rl_unix_line_discard);
  681. }
  682. # if defined (TIOCGLTC)
  683. if (ttybuff.flags & LTCHARS_SET)
  684. {
  685. SET_SPECIAL (ttybuff.ltchars.t_werasc, rl_unix_word_rubout);
  686. SET_SPECIAL (ttybuff.ltchars.t_lnextc, rl_quoted_insert);
  687. }
  688. # endif /* TIOCGLTC */
  689. }
  690. #else /* !NEW_TTY_DRIVER */
  691. static void
  692. set_special_char (kmap, tiop, sc, func)
  693. Keymap kmap;
  694. TIOTYPE *tiop;
  695. int sc;
  696. rl_command_func_t *func;
  697. {
  698. unsigned char uc;
  699. uc = tiop->c_cc[sc];
  700. if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC)
  701. kmap[uc].function = func;
  702. }
  703. /* used later */
  704. #define RESET_SPECIAL(uc) \
  705. if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) \
  706. kmap[uc].function = rl_insert;
  707. static void
  708. _rl_bind_tty_special_chars (kmap, ttybuff)
  709. Keymap kmap;
  710. TIOTYPE ttybuff;
  711. {
  712. SET_SPECIAL (VERASE, rl_rubout);
  713. SET_SPECIAL (VKILL, rl_unix_line_discard);
  714. # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
  715. SET_SPECIAL (VLNEXT, rl_quoted_insert);
  716. # endif /* VLNEXT && TERMIOS_TTY_DRIVER */
  717. # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
  718. SET_SPECIAL (VWERASE, rl_unix_word_rubout);
  719. # endif /* VWERASE && TERMIOS_TTY_DRIVER */
  720. }
  721. #endif /* !NEW_TTY_DRIVER */
  722. /* Set the system's default editing characters to their readline equivalents
  723. in KMAP. Should be static, now that we have rl_tty_set_default_bindings. */
  724. void
  725. rltty_set_default_bindings (kmap)
  726. Keymap kmap;
  727. {
  728. #if !defined (NO_TTY_DRIVER)
  729. TIOTYPE ttybuff;
  730. int tty;
  731. tty = fileno (rl_instream);
  732. if (get_tty_settings (tty, &ttybuff) == 0)
  733. _rl_bind_tty_special_chars (kmap, ttybuff);
  734. #endif
  735. }
  736. /* New public way to set the system default editing chars to their readline
  737. equivalents. */
  738. void
  739. rl_tty_set_default_bindings (kmap)
  740. Keymap kmap;
  741. {
  742. rltty_set_default_bindings (kmap);
  743. }
  744. /* Rebind all of the tty special chars that readline worries about back
  745. to self-insert. Call this before saving the current terminal special
  746. chars with save_tty_chars(). This only works on POSIX termios or termio
  747. systems. */
  748. void
  749. rl_tty_unset_default_bindings (kmap)
  750. Keymap kmap;
  751. {
  752. /* Don't bother before we've saved the tty special chars at least once. */
  753. if (RL_ISSTATE(RL_STATE_TTYCSAVED) == 0)
  754. return;
  755. RESET_SPECIAL (_rl_tty_chars.t_erase);
  756. RESET_SPECIAL (_rl_tty_chars.t_kill);
  757. # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
  758. RESET_SPECIAL (_rl_tty_chars.t_lnext);
  759. # endif /* VLNEXT && TERMIOS_TTY_DRIVER */
  760. # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
  761. RESET_SPECIAL (_rl_tty_chars.t_werase);
  762. # endif /* VWERASE && TERMIOS_TTY_DRIVER */
  763. }
  764. #if defined (HANDLE_SIGNALS)
  765. #if defined (NEW_TTY_DRIVER) || defined (NO_TTY_DRIVER)
  766. int
  767. _rl_disable_tty_signals ()
  768. {
  769. return 0;
  770. }
  771. int
  772. _rl_restore_tty_signals ()
  773. {
  774. return 0;
  775. }
  776. #else
  777. static TIOTYPE sigstty, nosigstty;
  778. static int tty_sigs_disabled = 0;
  779. int
  780. _rl_disable_tty_signals ()
  781. {
  782. if (tty_sigs_disabled)
  783. return 0;
  784. if (_get_tty_settings (fileno (rl_instream), &sigstty) < 0)
  785. return -1;
  786. nosigstty = sigstty;
  787. nosigstty.c_lflag &= ~ISIG;
  788. nosigstty.c_iflag &= ~IXON;
  789. if (_set_tty_settings (fileno (rl_instream), &nosigstty) < 0)
  790. return (_set_tty_settings (fileno (rl_instream), &sigstty));
  791. tty_sigs_disabled = 1;
  792. return 0;
  793. }
  794. int
  795. _rl_restore_tty_signals ()
  796. {
  797. int r;
  798. if (tty_sigs_disabled == 0)
  799. return 0;
  800. r = _set_tty_settings (fileno (rl_instream), &sigstty);
  801. if (r == 0)
  802. tty_sigs_disabled = 0;
  803. return r;
  804. }
  805. #endif /* !NEW_TTY_DRIVER */
  806. #endif /* HANDLE_SIGNALS */