terminal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /* terminal.c -- controlling the terminal with termcap. */
  2. /* Copyright (C) 1996-2009 Free Software Foundation, Inc.
  3. This file is part of the GNU Readline Library (Readline), a library
  4. for reading lines of text with interactive input and history editing.
  5. Readline is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Readline is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define READLINE_LIBRARY
  17. #if defined (HAVE_CONFIG_H)
  18. # include <config.h>
  19. #endif
  20. #include <sys/types.h>
  21. #include "posixstat.h"
  22. #include <fcntl.h>
  23. #if defined (HAVE_SYS_FILE_H)
  24. # include <sys/file.h>
  25. #endif /* HAVE_SYS_FILE_H */
  26. #if defined (HAVE_UNISTD_H)
  27. # include <unistd.h>
  28. #endif /* HAVE_UNISTD_H */
  29. #if defined (HAVE_STDLIB_H)
  30. # include <stdlib.h>
  31. #else
  32. # include "ansi_stdlib.h"
  33. #endif /* HAVE_STDLIB_H */
  34. #if defined (HAVE_LOCALE_H)
  35. # include <locale.h>
  36. #endif
  37. #include <stdio.h>
  38. /* System-specific feature definitions and include files. */
  39. #include "rldefs.h"
  40. #if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ)
  41. # include <sys/ioctl.h>
  42. #endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */
  43. #ifdef __MSDOS__
  44. # include <pc.h>
  45. #endif
  46. #include "rltty.h"
  47. #include "tcap.h"
  48. /* Some standard library routines. */
  49. #include "readline.h"
  50. #include "history.h"
  51. #include "rlprivate.h"
  52. #include "rlshell.h"
  53. #include "xmalloc.h"
  54. #if defined (__MINGW32__)
  55. # include <windows.h>
  56. # include <wincon.h>
  57. static void _win_get_screensize PARAMS((int *, int *));
  58. #endif
  59. #if defined (__EMX__)
  60. static void _emx_get_screensize PARAMS((int *, int *));
  61. #endif
  62. #define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
  63. #define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
  64. /* If the calling application sets this to a non-zero value, readline will
  65. use the $LINES and $COLUMNS environment variables to set its idea of the
  66. window size before interrogating the kernel. */
  67. int rl_prefer_env_winsize = 0;
  68. /* **************************************************************** */
  69. /* */
  70. /* Terminal and Termcap */
  71. /* */
  72. /* **************************************************************** */
  73. #ifndef __MSDOS__
  74. static char *term_buffer = (char *)NULL;
  75. static char *term_string_buffer = (char *)NULL;
  76. #endif /* !__MSDOS__ */
  77. static int tcap_initialized;
  78. #if !defined (__linux__) && !defined (NCURSES_VERSION)
  79. # if defined (__EMX__) || defined (NEED_EXTERN_PC)
  80. extern
  81. # endif /* __EMX__ || NEED_EXTERN_PC */
  82. char PC, *BC, *UP;
  83. #endif /* !__linux__ && !NCURSES_VERSION */
  84. /* Some strings to control terminal actions. These are output by tputs (). */
  85. char *_rl_term_clreol;
  86. char *_rl_term_clrpag;
  87. char *_rl_term_cr;
  88. char *_rl_term_backspace;
  89. char *_rl_term_goto;
  90. char *_rl_term_pc;
  91. /* Non-zero if we determine that the terminal can do character insertion. */
  92. int _rl_terminal_can_insert = 0;
  93. /* How to insert characters. */
  94. char *_rl_term_im;
  95. char *_rl_term_ei;
  96. char *_rl_term_ic;
  97. char *_rl_term_ip;
  98. char *_rl_term_IC;
  99. /* How to delete characters. */
  100. char *_rl_term_dc;
  101. char *_rl_term_DC;
  102. char *_rl_term_forward_char;
  103. /* How to go up a line. */
  104. char *_rl_term_up;
  105. /* A visible bell; char if the terminal can be made to flash the screen. */
  106. static char *_rl_visible_bell;
  107. /* Non-zero means the terminal can auto-wrap lines. */
  108. int _rl_term_autowrap = -1;
  109. /* Non-zero means that this terminal has a meta key. */
  110. static int term_has_meta;
  111. /* The sequences to write to turn on and off the meta key, if this
  112. terminal has one. */
  113. static char *_rl_term_mm;
  114. static char *_rl_term_mo;
  115. /* The key sequences output by the arrow keys, if this terminal has any. */
  116. static char *_rl_term_ku;
  117. static char *_rl_term_kd;
  118. static char *_rl_term_kr;
  119. static char *_rl_term_kl;
  120. /* How to initialize and reset the arrow keys, if this terminal has any. */
  121. static char *_rl_term_ks;
  122. static char *_rl_term_ke;
  123. /* The key sequences sent by the Home and End keys, if any. */
  124. static char *_rl_term_kh;
  125. static char *_rl_term_kH;
  126. static char *_rl_term_at7; /* @7 */
  127. /* Delete key */
  128. static char *_rl_term_kD;
  129. /* Insert key */
  130. static char *_rl_term_kI;
  131. /* Cursor control */
  132. static char *_rl_term_vs; /* very visible */
  133. static char *_rl_term_ve; /* normal */
  134. static void bind_termcap_arrow_keys PARAMS((Keymap));
  135. /* Variables that hold the screen dimensions, used by the display code. */
  136. int _rl_screenwidth, _rl_screenheight, _rl_screenchars;
  137. /* Non-zero means the user wants to enable the keypad. */
  138. int _rl_enable_keypad;
  139. /* Non-zero means the user wants to enable a meta key. */
  140. int _rl_enable_meta = 1;
  141. #if defined (__EMX__)
  142. static void
  143. _emx_get_screensize (swp, shp)
  144. int *swp, *shp;
  145. {
  146. int sz[2];
  147. _scrsize (sz);
  148. if (swp)
  149. *swp = sz[0];
  150. if (shp)
  151. *shp = sz[1];
  152. }
  153. #endif
  154. #if defined (__MINGW32__)
  155. static void
  156. _win_get_screensize (swp, shp)
  157. int *swp, *shp;
  158. {
  159. HANDLE hConOut;
  160. CONSOLE_SCREEN_BUFFER_INFO scr;
  161. hConOut = GetStdHandle (STD_OUTPUT_HANDLE);
  162. if (hConOut != INVALID_HANDLE_VALUE)
  163. {
  164. if (GetConsoleScreenBufferInfo (hConOut, &scr))
  165. {
  166. *swp = scr.dwSize.X;
  167. *shp = scr.srWindow.Bottom - scr.srWindow.Top + 1;
  168. }
  169. }
  170. }
  171. #endif
  172. /* Get readline's idea of the screen size. TTY is a file descriptor open
  173. to the terminal. If IGNORE_ENV is true, we do not pay attention to the
  174. values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
  175. non-null serve to check whether or not we have initialized termcap. */
  176. void
  177. _rl_get_screen_size (tty, ignore_env)
  178. int tty, ignore_env;
  179. {
  180. char *ss;
  181. #if defined (TIOCGWINSZ)
  182. struct winsize window_size;
  183. #endif /* TIOCGWINSZ */
  184. int wr, wc;
  185. wr = wc = -1;
  186. #if defined (TIOCGWINSZ)
  187. if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
  188. {
  189. wc = (int) window_size.ws_col;
  190. wr = (int) window_size.ws_row;
  191. }
  192. #endif /* TIOCGWINSZ */
  193. #if defined (__EMX__)
  194. _emx_get_screensize (&wc, &wr);
  195. #elif defined (__MINGW32__)
  196. _win_get_screensize (&wc, &wr);
  197. #endif
  198. if (ignore_env || rl_prefer_env_winsize == 0)
  199. {
  200. _rl_screenwidth = wc;
  201. _rl_screenheight = wr;
  202. }
  203. else
  204. _rl_screenwidth = _rl_screenheight = -1;
  205. /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
  206. is unset. If we prefer the environment, check it first before
  207. assigning the value returned by the kernel. */
  208. if (_rl_screenwidth <= 0)
  209. {
  210. if (ignore_env == 0 && (ss = sh_get_env_value ("COLUMNS")))
  211. _rl_screenwidth = atoi (ss);
  212. if (_rl_screenwidth <= 0)
  213. _rl_screenwidth = wc;
  214. #if defined (__DJGPP__)
  215. if (_rl_screenwidth <= 0)
  216. _rl_screenwidth = ScreenCols ();
  217. #else
  218. if (_rl_screenwidth <= 0 && term_string_buffer)
  219. _rl_screenwidth = tgetnum ("co");
  220. #endif
  221. }
  222. /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
  223. is unset. */
  224. if (_rl_screenheight <= 0)
  225. {
  226. if (ignore_env == 0 && (ss = sh_get_env_value ("LINES")))
  227. _rl_screenheight = atoi (ss);
  228. if (_rl_screenheight <= 0)
  229. _rl_screenheight = wr;
  230. #if defined (__DJGPP__)
  231. if (_rl_screenheight <= 0)
  232. _rl_screenheight = ScreenRows ();
  233. #else
  234. if (_rl_screenheight <= 0 && term_string_buffer)
  235. _rl_screenheight = tgetnum ("li");
  236. #endif
  237. }
  238. /* If all else fails, default to 80x24 terminal. */
  239. if (_rl_screenwidth <= 1)
  240. _rl_screenwidth = 80;
  241. if (_rl_screenheight <= 0)
  242. _rl_screenheight = 24;
  243. /* If we're being compiled as part of bash, set the environment
  244. variables $LINES and $COLUMNS to new values. Otherwise, just
  245. do a pair of putenv () or setenv () calls. */
  246. sh_set_lines_and_columns (_rl_screenheight, _rl_screenwidth);
  247. if (_rl_term_autowrap == 0)
  248. _rl_screenwidth--;
  249. _rl_screenchars = _rl_screenwidth * _rl_screenheight;
  250. }
  251. void
  252. _rl_set_screen_size (rows, cols)
  253. int rows, cols;
  254. {
  255. if (_rl_term_autowrap == -1)
  256. _rl_init_terminal_io (rl_terminal_name);
  257. if (rows > 0)
  258. _rl_screenheight = rows;
  259. if (cols > 0)
  260. {
  261. _rl_screenwidth = cols;
  262. if (_rl_term_autowrap == 0)
  263. _rl_screenwidth--;
  264. }
  265. if (rows > 0 || cols > 0)
  266. _rl_screenchars = _rl_screenwidth * _rl_screenheight;
  267. }
  268. void
  269. rl_set_screen_size (rows, cols)
  270. int rows, cols;
  271. {
  272. _rl_set_screen_size (rows, cols);
  273. }
  274. void
  275. rl_get_screen_size (rows, cols)
  276. int *rows, *cols;
  277. {
  278. if (rows)
  279. *rows = _rl_screenheight;
  280. if (cols)
  281. *cols = _rl_screenwidth;
  282. }
  283. void
  284. rl_reset_screen_size ()
  285. {
  286. _rl_get_screen_size (fileno (rl_instream), 0);
  287. }
  288. void
  289. rl_resize_terminal ()
  290. {
  291. _rl_get_screen_size (fileno (rl_instream), 1);
  292. if (_rl_echoing_p)
  293. {
  294. if (CUSTOM_REDISPLAY_FUNC ())
  295. rl_forced_update_display ();
  296. else if (RL_ISSTATE(RL_STATE_REDISPLAYING) == 0)
  297. _rl_redisplay_after_sigwinch ();
  298. }
  299. }
  300. struct _tc_string {
  301. const char * const tc_var;
  302. char **tc_value;
  303. };
  304. /* This should be kept sorted, just in case we decide to change the
  305. search algorithm to something smarter. */
  306. static const struct _tc_string tc_strings[] =
  307. {
  308. { "@7", &_rl_term_at7 },
  309. { "DC", &_rl_term_DC },
  310. { "IC", &_rl_term_IC },
  311. { "ce", &_rl_term_clreol },
  312. { "cl", &_rl_term_clrpag },
  313. { "cr", &_rl_term_cr },
  314. { "dc", &_rl_term_dc },
  315. { "ei", &_rl_term_ei },
  316. { "ic", &_rl_term_ic },
  317. { "im", &_rl_term_im },
  318. { "kD", &_rl_term_kD }, /* delete */
  319. { "kH", &_rl_term_kH }, /* home down ?? */
  320. { "kI", &_rl_term_kI }, /* insert */
  321. { "kd", &_rl_term_kd },
  322. { "ke", &_rl_term_ke }, /* end keypad mode */
  323. { "kh", &_rl_term_kh }, /* home */
  324. { "kl", &_rl_term_kl },
  325. { "kr", &_rl_term_kr },
  326. { "ks", &_rl_term_ks }, /* start keypad mode */
  327. { "ku", &_rl_term_ku },
  328. { "le", &_rl_term_backspace },
  329. { "mm", &_rl_term_mm },
  330. { "mo", &_rl_term_mo },
  331. { "nd", &_rl_term_forward_char },
  332. { "pc", &_rl_term_pc },
  333. { "up", &_rl_term_up },
  334. { "vb", &_rl_visible_bell },
  335. { "vs", &_rl_term_vs },
  336. { "ve", &_rl_term_ve },
  337. };
  338. #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
  339. /* Read the desired terminal capability strings into BP. The capabilities
  340. are described in the TC_STRINGS table. */
  341. static void
  342. get_term_capabilities (bp)
  343. char **bp;
  344. {
  345. #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
  346. register int i;
  347. for (i = 0; i < NUM_TC_STRINGS; i++)
  348. *(tc_strings[i].tc_value) = tgetstr ((char *)tc_strings[i].tc_var, bp);
  349. #endif
  350. tcap_initialized = 1;
  351. }
  352. int
  353. _rl_init_terminal_io (terminal_name)
  354. const char *terminal_name;
  355. {
  356. const char *term;
  357. char *buffer;
  358. int tty, tgetent_ret;
  359. term = terminal_name ? terminal_name : sh_get_env_value ("TERM");
  360. _rl_term_clrpag = _rl_term_cr = _rl_term_clreol = (char *)NULL;
  361. tty = rl_instream ? fileno (rl_instream) : 0;
  362. if (term == 0)
  363. term = "dumb";
  364. #ifdef __MSDOS__
  365. _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
  366. _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
  367. _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
  368. _rl_term_mm = _rl_term_mo = (char *)NULL;
  369. _rl_terminal_can_insert = term_has_meta = _rl_term_autowrap = 0;
  370. _rl_term_cr = "\r";
  371. _rl_term_clreol = _rl_term_clrpag = _rl_term_backspace = (char *)NULL;
  372. _rl_term_goto = _rl_term_pc = _rl_term_ip = (char *)NULL;
  373. _rl_term_ks = _rl_term_ke =_rl_term_vs = _rl_term_ve = (char *)NULL;
  374. _rl_term_kh = _rl_term_kH = _rl_term_at7 = _rl_term_kI = (char *)NULL;
  375. #if defined(HACK_TERMCAP_MOTION)
  376. _rl_term_forward_char = (char *)NULL;
  377. #endif
  378. _rl_get_screen_size (tty, 0);
  379. #else /* !__MSDOS__ */
  380. /* I've separated this out for later work on not calling tgetent at all
  381. if the calling application has supplied a custom redisplay function,
  382. (and possibly if the application has supplied a custom input function). */
  383. if (CUSTOM_REDISPLAY_FUNC())
  384. {
  385. tgetent_ret = -1;
  386. }
  387. else
  388. {
  389. if (term_string_buffer == 0)
  390. term_string_buffer = (char *)xmalloc(2032);
  391. if (term_buffer == 0)
  392. term_buffer = (char *)xmalloc(4080);
  393. buffer = term_string_buffer;
  394. tgetent_ret = tgetent (term_buffer, term);
  395. }
  396. if (tgetent_ret <= 0)
  397. {
  398. FREE (term_string_buffer);
  399. FREE (term_buffer);
  400. buffer = term_buffer = term_string_buffer = (char *)NULL;
  401. _rl_term_autowrap = 0; /* used by _rl_get_screen_size */
  402. /* Allow calling application to set default height and width, using
  403. rl_set_screen_size */
  404. if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
  405. {
  406. #if defined (__EMX__)
  407. _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
  408. _rl_screenwidth--;
  409. #else /* !__EMX__ */
  410. _rl_get_screen_size (tty, 0);
  411. #endif /* !__EMX__ */
  412. }
  413. /* Defaults. */
  414. if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
  415. {
  416. _rl_screenwidth = 79;
  417. _rl_screenheight = 24;
  418. }
  419. /* Everything below here is used by the redisplay code (tputs). */
  420. _rl_screenchars = _rl_screenwidth * _rl_screenheight;
  421. _rl_term_cr = "\r";
  422. _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
  423. _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
  424. _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
  425. _rl_term_kh = _rl_term_kH = _rl_term_kI = _rl_term_kD = (char *)NULL;
  426. _rl_term_ks = _rl_term_ke = _rl_term_at7 = (char *)NULL;
  427. _rl_term_mm = _rl_term_mo = (char *)NULL;
  428. _rl_term_ve = _rl_term_vs = (char *)NULL;
  429. _rl_term_forward_char = (char *)NULL;
  430. _rl_terminal_can_insert = term_has_meta = 0;
  431. /* Reasonable defaults for tgoto(). Readline currently only uses
  432. tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
  433. change that later... */
  434. PC = '\0';
  435. BC = _rl_term_backspace = "\b";
  436. UP = _rl_term_up;
  437. return 0;
  438. }
  439. get_term_capabilities (&buffer);
  440. /* Set up the variables that the termcap library expects the application
  441. to provide. */
  442. PC = _rl_term_pc ? *_rl_term_pc : 0;
  443. BC = _rl_term_backspace;
  444. UP = _rl_term_up;
  445. if (!_rl_term_cr)
  446. _rl_term_cr = "\r";
  447. _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
  448. /* Allow calling application to set default height and width, using
  449. rl_set_screen_size */
  450. if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
  451. _rl_get_screen_size (tty, 0);
  452. /* "An application program can assume that the terminal can do
  453. character insertion if *any one of* the capabilities `IC',
  454. `im', `ic' or `ip' is provided." But we can't do anything if
  455. only `ip' is provided, so... */
  456. _rl_terminal_can_insert = (_rl_term_IC || _rl_term_im || _rl_term_ic);
  457. /* Check to see if this terminal has a meta key and clear the capability
  458. variables if there is none. */
  459. term_has_meta = tgetflag ("km") != 0;
  460. if (term_has_meta == 0)
  461. _rl_term_mm = _rl_term_mo = (char *)NULL;
  462. #endif /* !__MSDOS__ */
  463. /* Attempt to find and bind the arrow keys. Do not override already
  464. bound keys in an overzealous attempt, however. */
  465. bind_termcap_arrow_keys (emacs_standard_keymap);
  466. #if defined (VI_MODE)
  467. bind_termcap_arrow_keys (vi_movement_keymap);
  468. bind_termcap_arrow_keys (vi_insertion_keymap);
  469. #endif /* VI_MODE */
  470. return 0;
  471. }
  472. /* Bind the arrow key sequences from the termcap description in MAP. */
  473. static void
  474. bind_termcap_arrow_keys (map)
  475. Keymap map;
  476. {
  477. Keymap xkeymap;
  478. xkeymap = _rl_keymap;
  479. _rl_keymap = map;
  480. rl_bind_keyseq_if_unbound (_rl_term_ku, rl_get_previous_history);
  481. rl_bind_keyseq_if_unbound (_rl_term_kd, rl_get_next_history);
  482. rl_bind_keyseq_if_unbound (_rl_term_kr, rl_forward_char);
  483. rl_bind_keyseq_if_unbound (_rl_term_kl, rl_backward_char);
  484. rl_bind_keyseq_if_unbound (_rl_term_kh, rl_beg_of_line); /* Home */
  485. rl_bind_keyseq_if_unbound (_rl_term_at7, rl_end_of_line); /* End */
  486. rl_bind_keyseq_if_unbound (_rl_term_kD, rl_delete);
  487. _rl_keymap = xkeymap;
  488. }
  489. char *
  490. rl_get_termcap (cap)
  491. const char *cap;
  492. {
  493. register int i;
  494. if (tcap_initialized == 0)
  495. return ((char *)NULL);
  496. for (i = 0; i < NUM_TC_STRINGS; i++)
  497. {
  498. if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
  499. return *(tc_strings[i].tc_value);
  500. }
  501. return ((char *)NULL);
  502. }
  503. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  504. has changed. */
  505. int
  506. rl_reset_terminal (terminal_name)
  507. const char *terminal_name;
  508. {
  509. _rl_screenwidth = _rl_screenheight = 0;
  510. _rl_init_terminal_io (terminal_name);
  511. return 0;
  512. }
  513. /* A function for the use of tputs () */
  514. #ifdef _MINIX
  515. void
  516. _rl_output_character_function (c)
  517. int c;
  518. {
  519. putc (c, _rl_out_stream);
  520. }
  521. #else /* !_MINIX */
  522. int
  523. _rl_output_character_function (c)
  524. int c;
  525. {
  526. return putc (c, _rl_out_stream);
  527. }
  528. #endif /* !_MINIX */
  529. /* Write COUNT characters from STRING to the output stream. */
  530. void
  531. _rl_output_some_chars (string, count)
  532. const char *string;
  533. int count;
  534. {
  535. fwrite (string, 1, count, _rl_out_stream);
  536. }
  537. /* Move the cursor back. */
  538. int
  539. _rl_backspace (count)
  540. int count;
  541. {
  542. register int i;
  543. #ifndef __MSDOS__
  544. if (_rl_term_backspace)
  545. for (i = 0; i < count; i++)
  546. tputs (_rl_term_backspace, 1, _rl_output_character_function);
  547. else
  548. #endif
  549. for (i = 0; i < count; i++)
  550. putc ('\b', _rl_out_stream);
  551. return 0;
  552. }
  553. /* Move to the start of the next line. */
  554. int
  555. rl_crlf ()
  556. {
  557. #if defined (NEW_TTY_DRIVER) || defined (__MINT__)
  558. if (_rl_term_cr)
  559. tputs (_rl_term_cr, 1, _rl_output_character_function);
  560. #endif /* NEW_TTY_DRIVER || __MINT__ */
  561. putc ('\n', _rl_out_stream);
  562. return 0;
  563. }
  564. /* Ring the terminal bell. */
  565. int
  566. rl_ding ()
  567. {
  568. if (_rl_echoing_p)
  569. {
  570. switch (_rl_bell_preference)
  571. {
  572. case NO_BELL:
  573. default:
  574. break;
  575. case VISIBLE_BELL:
  576. #ifdef __MSDOS__
  577. ScreenVisualBell ();
  578. break;
  579. #else
  580. if (_rl_visible_bell)
  581. {
  582. tputs (_rl_visible_bell, 1, _rl_output_character_function);
  583. break;
  584. }
  585. /* FALLTHROUGH */
  586. #endif
  587. case AUDIBLE_BELL:
  588. fprintf (stderr, "\007");
  589. fflush (stderr);
  590. break;
  591. }
  592. return (0);
  593. }
  594. return (-1);
  595. }
  596. /* **************************************************************** */
  597. /* */
  598. /* Controlling the Meta Key and Keypad */
  599. /* */
  600. /* **************************************************************** */
  601. void
  602. _rl_enable_meta_key ()
  603. {
  604. #if !defined (__DJGPP__)
  605. if (term_has_meta && _rl_term_mm)
  606. tputs (_rl_term_mm, 1, _rl_output_character_function);
  607. #endif
  608. }
  609. void
  610. _rl_control_keypad (on)
  611. int on;
  612. {
  613. #if !defined (__DJGPP__)
  614. if (on && _rl_term_ks)
  615. tputs (_rl_term_ks, 1, _rl_output_character_function);
  616. else if (!on && _rl_term_ke)
  617. tputs (_rl_term_ke, 1, _rl_output_character_function);
  618. #endif
  619. }
  620. /* **************************************************************** */
  621. /* */
  622. /* Controlling the Cursor */
  623. /* */
  624. /* **************************************************************** */
  625. /* Set the cursor appropriately depending on IM, which is one of the
  626. insert modes (insert or overwrite). Insert mode gets the normal
  627. cursor. Overwrite mode gets a very visible cursor. Only does
  628. anything if we have both capabilities. */
  629. void
  630. _rl_set_cursor (im, force)
  631. int im, force;
  632. {
  633. #ifndef __MSDOS__
  634. if (_rl_term_ve && _rl_term_vs)
  635. {
  636. if (force || im != rl_insert_mode)
  637. {
  638. if (im == RL_IM_OVERWRITE)
  639. tputs (_rl_term_vs, 1, _rl_output_character_function);
  640. else
  641. tputs (_rl_term_ve, 1, _rl_output_character_function);
  642. }
  643. }
  644. #endif
  645. }