tty-slang.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. Interface to the terminal controlling library.
  3. Slang wrapper.
  4. Copyright (C) 2005-2019
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Andrew Borodin <aborodin@vmail.ru>, 2009
  8. Egmont Koblinger <egmont@gmail.com>, 2010
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /** \file
  22. * \brief Source: S-Lang-based tty layer of Midnight Commander
  23. */
  24. #include <config.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/types.h> /* size_t */
  29. #include <unistd.h>
  30. #ifdef HAVE_SYS_IOCTL_H
  31. #include <sys/ioctl.h>
  32. #endif
  33. #include <termios.h>
  34. #include "lib/global.h"
  35. #include "lib/strutil.h" /* str_term_form */
  36. #include "lib/util.h" /* is_printable() */
  37. #include "tty-internal.h" /* mc_tty_normalize_from_utf8() */
  38. #include "tty.h"
  39. #include "color.h"
  40. #include "color-slang.h"
  41. #include "color-internal.h"
  42. #include "mouse.h" /* Gpm_Event is required in key.h */
  43. #include "key.h" /* define_sequence */
  44. #include "win.h"
  45. /*** global variables ****************************************************************************/
  46. /* If true program softkeys (HP terminals only) on startup and after every
  47. command ran in the subshell to the description found in the termcap/terminfo
  48. database */
  49. int reset_hp_softkeys = 0;
  50. /*** file scope macro definitions ****************************************************************/
  51. #ifndef SLTT_MAX_SCREEN_COLS
  52. #define SLTT_MAX_SCREEN_COLS 512
  53. #endif
  54. #ifndef SLTT_MAX_SCREEN_ROWS
  55. #define SLTT_MAX_SCREEN_ROWS 512
  56. #endif
  57. /*** file scope type declarations ****************************************************************/
  58. /*** file scope variables ************************************************************************/
  59. /* Various saved termios settings that we control here */
  60. static struct termios boot_mode;
  61. static struct termios new_mode;
  62. /* Controls whether we should wait for input in tty_lowlevel_getch */
  63. static gboolean no_slang_delay;
  64. static gboolean slsmg_active = FALSE;
  65. /* This table describes which capabilities we want and which values we
  66. * assign to them.
  67. */
  68. static const struct
  69. {
  70. int key_code;
  71. const char *key_name;
  72. } key_table[] =
  73. {
  74. /* *INDENT-OFF* */
  75. { KEY_F (0), "k0" },
  76. { KEY_F (1), "k1" },
  77. { KEY_F (2), "k2" },
  78. { KEY_F (3), "k3" },
  79. { KEY_F (4), "k4" },
  80. { KEY_F (5), "k5" },
  81. { KEY_F (6), "k6" },
  82. { KEY_F (7), "k7" },
  83. { KEY_F (8), "k8" },
  84. { KEY_F (9), "k9" },
  85. { KEY_F (10), "k;" },
  86. { KEY_F (11), "F1" },
  87. { KEY_F (12), "F2" },
  88. { KEY_F (13), "F3" },
  89. { KEY_F (14), "F4" },
  90. { KEY_F (15), "F5" },
  91. { KEY_F (16), "F6" },
  92. { KEY_F (17), "F7" },
  93. { KEY_F (18), "F8" },
  94. { KEY_F (19), "F9" },
  95. { KEY_F (20), "FA" },
  96. { KEY_IC, "kI" },
  97. { KEY_NPAGE, "kN" },
  98. { KEY_PPAGE, "kP" },
  99. { KEY_LEFT, "kl" },
  100. { KEY_RIGHT, "kr" },
  101. { KEY_UP, "ku" },
  102. { KEY_DOWN, "kd" },
  103. { KEY_DC, "kD" },
  104. { KEY_BACKSPACE, "kb" },
  105. { KEY_HOME, "kh" },
  106. { KEY_END, "@7" },
  107. { 0, NULL }
  108. /* *INDENT-ON* */
  109. };
  110. /* --------------------------------------------------------------------------------------------- */
  111. /*** file scope functions ************************************************************************/
  112. /* --------------------------------------------------------------------------------------------- */
  113. static void
  114. tty_setup_sigwinch (void (*handler) (int))
  115. {
  116. (void) SLsignal (SIGWINCH, handler);
  117. tty_create_winch_pipe ();
  118. }
  119. /* --------------------------------------------------------------------------------------------- */
  120. static void
  121. sigwinch_handler (int dummy)
  122. {
  123. ssize_t n = 0;
  124. (void) dummy;
  125. n = write (sigwinch_pipe[1], "", 1);
  126. (void) n;
  127. (void) SLsignal (SIGWINCH, sigwinch_handler);
  128. }
  129. /* --------------------------------------------------------------------------------------------- */
  130. /* HP Terminals have capabilities (pfkey, pfloc, pfx) to program function keys.
  131. elm 2.4pl15 invoked with the -K option utilizes these softkeys and the
  132. consequence is that function keys don't work in MC sometimes...
  133. Unfortunately I don't now the one and only escape sequence to turn off.
  134. softkeys (elm uses three different capabilities to turn on softkeys and two.
  135. capabilities to turn them off)..
  136. Among other things elm uses the pair we already use in slang_keypad. That's.
  137. the reason why I call slang_reset_softkeys from slang_keypad. In lack of
  138. something better the softkeys are programmed to their defaults from the
  139. termcap/terminfo database.
  140. The escape sequence to program the softkeys is taken from elm and it is.
  141. hardcoded because neither slang nor ncurses 4.1 know how to 'printf' this.
  142. sequence. -- Norbert
  143. */
  144. static void
  145. slang_reset_softkeys (void)
  146. {
  147. int key;
  148. static const char display[] = " ";
  149. char tmp[BUF_SMALL];
  150. for (key = 1; key < 9; key++)
  151. {
  152. char *send;
  153. g_snprintf (tmp, sizeof (tmp), "k%d", key);
  154. send = SLtt_tgetstr (tmp);
  155. if (send != NULL)
  156. {
  157. g_snprintf (tmp, sizeof (tmp), ESC_STR "&f%dk%dd%dL%s%s", key,
  158. (int) (sizeof (display) - 1), (int) strlen (send), display, send);
  159. SLtt_write_string (tmp);
  160. }
  161. }
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. static void
  165. do_define_key (int code, const char *strcap)
  166. {
  167. char *seq;
  168. seq = SLtt_tgetstr ((SLFUTURE_CONST char *) strcap);
  169. if (seq != NULL)
  170. define_sequence (code, seq, MCKEY_NOACTION);
  171. }
  172. /* --------------------------------------------------------------------------------------------- */
  173. static void
  174. load_terminfo_keys (void)
  175. {
  176. int i;
  177. for (i = 0; key_table[i].key_code; i++)
  178. do_define_key (key_table[i].key_code, key_table[i].key_name);
  179. }
  180. /* --------------------------------------------------------------------------------------------- */
  181. /*** public functions ****************************************************************************/
  182. /* --------------------------------------------------------------------------------------------- */
  183. int
  184. mc_tty_normalize_lines_char (const char *str)
  185. {
  186. char *str2;
  187. int res;
  188. struct mc_tty_lines_struct
  189. {
  190. const char *line;
  191. int line_code;
  192. } const lines_codes[] = {
  193. {"\342\224\214", SLSMG_ULCORN_CHAR},
  194. {"\342\224\220", SLSMG_URCORN_CHAR},
  195. {"\342\224\224", SLSMG_LLCORN_CHAR},
  196. {"\342\224\230", SLSMG_LRCORN_CHAR},
  197. {"\342\224\234", SLSMG_LTEE_CHAR},
  198. {"\342\224\244", SLSMG_RTEE_CHAR},
  199. {"\342\224\254", SLSMG_UTEE_CHAR},
  200. {"\342\224\264", SLSMG_DTEE_CHAR},
  201. {"\342\224\200", SLSMG_HLINE_CHAR},
  202. {"\342\224\202", SLSMG_VLINE_CHAR},
  203. {"\342\224\274", SLSMG_PLUS_CHAR},
  204. {NULL, 0}
  205. };
  206. if (!str)
  207. return (int) ' ';
  208. for (res = 0; lines_codes[res].line; res++)
  209. {
  210. if (strcmp (str, lines_codes[res].line) == 0)
  211. return lines_codes[res].line_code;
  212. }
  213. str2 = mc_tty_normalize_from_utf8 (str);
  214. res = g_utf8_get_char_validated (str2, -1);
  215. if (res < 0)
  216. res = (unsigned char) str2[0];
  217. g_free (str2);
  218. return res;
  219. }
  220. /* --------------------------------------------------------------------------------------------- */
  221. void
  222. tty_init (gboolean mouse_enable, gboolean is_xterm)
  223. {
  224. SLtt_Ignore_Beep = 1;
  225. SLutf8_enable (-1); /* has to be called first before any of the other functions. */
  226. SLtt_get_terminfo ();
  227. /*
  228. * If the terminal in not in terminfo but begins with a well-known
  229. * string such as "linux" or "xterm" S-Lang will go on, but the
  230. * terminal size and several other variables won't be initialized
  231. * (as of S-Lang 1.4.4). Detect it and abort. Also detect extremely
  232. * small screen dimensions.
  233. */
  234. if ((COLS < 10) || (LINES < 5)
  235. #if SLANG_VERSION < 20303
  236. /* Beginning from pre2.3.3-8 (55f58798c267d76a1b93d0d916027b71a10ac1ee),
  237. these limitations were eliminated. */
  238. || (COLS > SLTT_MAX_SCREEN_COLS) || (LINES > SLTT_MAX_SCREEN_ROWS)
  239. #endif
  240. )
  241. {
  242. fprintf (stderr,
  243. _("Screen size %dx%d is not supported.\n"
  244. "Check the TERM environment variable.\n"), COLS, LINES);
  245. exit (EXIT_FAILURE);
  246. }
  247. tcgetattr (fileno (stdin), &boot_mode);
  248. /* 255 = ignore abort char; XCTRL('g') for abort char = ^g */
  249. SLang_init_tty (XCTRL ('g'), 1, 0);
  250. if (mc_global.tty.ugly_line_drawing)
  251. SLtt_Has_Alt_Charset = 0;
  252. tcgetattr (SLang_TT_Read_FD, &new_mode);
  253. tty_reset_prog_mode ();
  254. load_terminfo_keys ();
  255. SLtt_Blink_Mode = (tty_use_256colors () || tty_use_truecolors (NULL)) ? 1 : 0;
  256. tty_start_interrupt_key ();
  257. /* It's the small part from the previous init_key() */
  258. init_key_input_fd ();
  259. /* For 8-bit locales, NCurses handles 154 (0x9A) symbol properly, while S-Lang
  260. * requires SLsmg_Display_Eight_Bit >= 154 (OR manual filtering if xterm display
  261. * detected - but checking TERM would fail under screen, OR running xterm
  262. * with allowC1Printable).
  263. */
  264. tty_display_8bit (FALSE);
  265. SLsmg_init_smg ();
  266. slsmg_active = TRUE;
  267. if (!mouse_enable)
  268. use_mouse_p = MOUSE_DISABLED;
  269. tty_init_xterm_support (is_xterm); /* do it before tty_enter_ca_mode() call */
  270. tty_enter_ca_mode ();
  271. tty_keypad (TRUE);
  272. tty_nodelay (FALSE);
  273. tty_setup_sigwinch (sigwinch_handler);
  274. }
  275. /* --------------------------------------------------------------------------------------------- */
  276. void
  277. tty_shutdown (void)
  278. {
  279. char *op_cap;
  280. tty_reset_shell_mode ();
  281. tty_noraw_mode ();
  282. tty_keypad (FALSE);
  283. tty_reset_screen ();
  284. tty_exit_ca_mode ();
  285. SLang_reset_tty ();
  286. slsmg_active = FALSE;
  287. /* Load the op capability to reset the colors to those that were
  288. * active when the program was started up
  289. */
  290. op_cap = SLtt_tgetstr ((SLFUTURE_CONST char *) "op");
  291. if (op_cap != NULL)
  292. {
  293. fputs (op_cap, stdout);
  294. fflush (stdout);
  295. }
  296. }
  297. /* --------------------------------------------------------------------------------------------- */
  298. void
  299. tty_enter_ca_mode (void)
  300. {
  301. /* S-Lang handles alternate screen switching and cursor position saving */
  302. }
  303. /* --------------------------------------------------------------------------------------------- */
  304. void
  305. tty_exit_ca_mode (void)
  306. {
  307. /* S-Lang handles alternate screen switching and cursor position restoring */
  308. }
  309. /* --------------------------------------------------------------------------------------------- */
  310. void
  311. tty_change_screen_size (void)
  312. {
  313. SLtt_get_screen_size ();
  314. if (slsmg_active)
  315. SLsmg_reinit_smg ();
  316. #ifdef ENABLE_SUBSHELL
  317. if (mc_global.tty.use_subshell)
  318. tty_resize (mc_global.tty.subshell_pty);
  319. #endif
  320. }
  321. /* --------------------------------------------------------------------------------------------- */
  322. /* Done each time we come back from done mode */
  323. void
  324. tty_reset_prog_mode (void)
  325. {
  326. tcsetattr (SLang_TT_Read_FD, TCSANOW, &new_mode);
  327. SLsmg_init_smg ();
  328. slsmg_active = TRUE;
  329. SLsmg_touch_lines (0, LINES);
  330. }
  331. /* --------------------------------------------------------------------------------------------- */
  332. /* Called each time we want to shutdown slang screen manager */
  333. void
  334. tty_reset_shell_mode (void)
  335. {
  336. tcsetattr (SLang_TT_Read_FD, TCSANOW, &boot_mode);
  337. }
  338. /* --------------------------------------------------------------------------------------------- */
  339. void
  340. tty_raw_mode (void)
  341. {
  342. tcsetattr (SLang_TT_Read_FD, TCSANOW, &new_mode);
  343. }
  344. /* --------------------------------------------------------------------------------------------- */
  345. void
  346. tty_noraw_mode (void)
  347. {
  348. }
  349. /* --------------------------------------------------------------------------------------------- */
  350. void
  351. tty_noecho (void)
  352. {
  353. }
  354. /* --------------------------------------------------------------------------------------------- */
  355. int
  356. tty_flush_input (void)
  357. {
  358. return 0; /* OK */
  359. }
  360. /* --------------------------------------------------------------------------------------------- */
  361. void
  362. tty_keypad (gboolean set)
  363. {
  364. char *keypad_string;
  365. keypad_string = SLtt_tgetstr ((SLFUTURE_CONST char *) (set ? "ks" : "ke"));
  366. if (keypad_string != NULL)
  367. SLtt_write_string (keypad_string);
  368. if (set && reset_hp_softkeys)
  369. slang_reset_softkeys ();
  370. }
  371. /* --------------------------------------------------------------------------------------------- */
  372. void
  373. tty_nodelay (gboolean set)
  374. {
  375. no_slang_delay = set;
  376. }
  377. /* --------------------------------------------------------------------------------------------- */
  378. int
  379. tty_baudrate (void)
  380. {
  381. return SLang_TT_Baud_Rate;
  382. }
  383. /* --------------------------------------------------------------------------------------------- */
  384. int
  385. tty_lowlevel_getch (void)
  386. {
  387. int c;
  388. if (no_slang_delay && (SLang_input_pending (0) == 0))
  389. return -1;
  390. c = SLang_getkey ();
  391. if (c == SLANG_GETKEY_ERROR)
  392. {
  393. fprintf (stderr,
  394. "SLang_getkey returned SLANG_GETKEY_ERROR\n"
  395. "Assuming EOF on stdin and exiting\n");
  396. exit (EXIT_FAILURE);
  397. }
  398. return c;
  399. }
  400. /* --------------------------------------------------------------------------------------------- */
  401. int
  402. tty_reset_screen (void)
  403. {
  404. SLsmg_reset_smg ();
  405. slsmg_active = FALSE;
  406. return 0; /* OK */
  407. }
  408. /* --------------------------------------------------------------------------------------------- */
  409. void
  410. tty_touch_screen (void)
  411. {
  412. SLsmg_touch_lines (0, LINES);
  413. }
  414. /* --------------------------------------------------------------------------------------------- */
  415. void
  416. tty_gotoyx (int y, int x)
  417. {
  418. SLsmg_gotorc (y, x);
  419. }
  420. /* --------------------------------------------------------------------------------------------- */
  421. void
  422. tty_getyx (int *py, int *px)
  423. {
  424. *py = SLsmg_get_row ();
  425. *px = SLsmg_get_column ();
  426. }
  427. /* --------------------------------------------------------------------------------------------- */
  428. void
  429. tty_draw_hline (int y, int x, int ch, int len)
  430. {
  431. int x1;
  432. if (y < 0 || y >= LINES || x >= COLS)
  433. return;
  434. x1 = x;
  435. if (x < 0)
  436. {
  437. len += x;
  438. if (len <= 0)
  439. return;
  440. x = 0;
  441. }
  442. if (ch == ACS_HLINE)
  443. ch = mc_tty_frm[MC_TTY_FRM_HORIZ];
  444. if (ch == 0)
  445. ch = ACS_HLINE;
  446. SLsmg_gotorc (y, x);
  447. if (ch == ACS_HLINE)
  448. SLsmg_draw_hline (len);
  449. else
  450. while (len-- != 0)
  451. tty_print_char (ch);
  452. SLsmg_gotorc (y, x1);
  453. }
  454. /* --------------------------------------------------------------------------------------------- */
  455. void
  456. tty_draw_vline (int y, int x, int ch, int len)
  457. {
  458. int y1;
  459. if (x < 0 || x >= COLS || y >= LINES)
  460. return;
  461. y1 = y;
  462. if (y < 0)
  463. {
  464. len += y;
  465. if (len <= 0)
  466. return;
  467. y = 0;
  468. }
  469. if (ch == ACS_VLINE)
  470. ch = mc_tty_frm[MC_TTY_FRM_VERT];
  471. if (ch == 0)
  472. ch = ACS_VLINE;
  473. SLsmg_gotorc (y, x);
  474. if (ch == ACS_VLINE)
  475. SLsmg_draw_vline (len);
  476. else
  477. {
  478. int pos = 0;
  479. while (len-- != 0)
  480. {
  481. SLsmg_gotorc (y + pos, x);
  482. tty_print_char (ch);
  483. pos++;
  484. }
  485. }
  486. SLsmg_gotorc (y1, x);
  487. }
  488. /* --------------------------------------------------------------------------------------------- */
  489. void
  490. tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
  491. {
  492. SLsmg_fill_region (y, x, rows, cols, ch);
  493. }
  494. /* --------------------------------------------------------------------------------------------- */
  495. void
  496. tty_set_alt_charset (gboolean alt_charset)
  497. {
  498. SLsmg_set_char_set ((int) alt_charset);
  499. }
  500. /* --------------------------------------------------------------------------------------------- */
  501. void
  502. tty_display_8bit (gboolean what)
  503. {
  504. SLsmg_Display_Eight_Bit = what ? 128 : 160;
  505. }
  506. /* --------------------------------------------------------------------------------------------- */
  507. void
  508. tty_print_char (int c)
  509. {
  510. SLsmg_write_char ((SLwchar_Type) ((unsigned int) c));
  511. }
  512. /* --------------------------------------------------------------------------------------------- */
  513. void
  514. tty_print_alt_char (int c, gboolean single)
  515. {
  516. #define DRAW(x, y) (x == y) \
  517. ? SLsmg_draw_object (SLsmg_get_row(), SLsmg_get_column(), x) \
  518. : SLsmg_write_char ((unsigned int) y)
  519. switch (c)
  520. {
  521. case ACS_VLINE:
  522. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT]);
  523. break;
  524. case ACS_HLINE:
  525. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ]);
  526. break;
  527. case ACS_LTEE:
  528. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_LEFTMIDDLE : MC_TTY_FRM_DLEFTMIDDLE]);
  529. break;
  530. case ACS_RTEE:
  531. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_RIGHTMIDDLE : MC_TTY_FRM_DRIGHTMIDDLE]);
  532. break;
  533. case ACS_TTEE:
  534. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_TOPMIDDLE : MC_TTY_FRM_DTOPMIDDLE]);
  535. break;
  536. case ACS_BTEE:
  537. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_BOTTOMMIDDLE : MC_TTY_FRM_DBOTTOMMIDDLE]);
  538. break;
  539. case ACS_ULCORNER:
  540. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_LEFTTOP : MC_TTY_FRM_DLEFTTOP]);
  541. break;
  542. case ACS_LLCORNER:
  543. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_LEFTBOTTOM : MC_TTY_FRM_DLEFTBOTTOM]);
  544. break;
  545. case ACS_URCORNER:
  546. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_RIGHTTOP : MC_TTY_FRM_DRIGHTTOP]);
  547. break;
  548. case ACS_LRCORNER:
  549. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_RIGHTBOTTOM : MC_TTY_FRM_DRIGHTBOTTOM]);
  550. break;
  551. case ACS_PLUS:
  552. DRAW (c, mc_tty_frm[MC_TTY_FRM_CROSS]);
  553. break;
  554. default:
  555. SLsmg_write_char ((unsigned int) c);
  556. }
  557. #undef DRAW
  558. }
  559. /* --------------------------------------------------------------------------------------------- */
  560. void
  561. tty_print_anychar (int c)
  562. {
  563. if (c > 255)
  564. {
  565. char str[UTF8_CHAR_LEN + 1];
  566. int res;
  567. res = g_unichar_to_utf8 (c, str);
  568. if (res == 0)
  569. {
  570. str[0] = '.';
  571. str[1] = '\0';
  572. }
  573. else
  574. {
  575. str[res] = '\0';
  576. }
  577. SLsmg_write_string ((char *) str_term_form (str));
  578. }
  579. else
  580. {
  581. if (!is_printable (c))
  582. c = '.';
  583. SLsmg_write_char ((SLwchar_Type) ((unsigned int) c));
  584. }
  585. }
  586. /* --------------------------------------------------------------------------------------------- */
  587. void
  588. tty_print_string (const char *s)
  589. {
  590. SLsmg_write_string ((char *) str_term_form (s));
  591. }
  592. /* --------------------------------------------------------------------------------------------- */
  593. void
  594. tty_printf (const char *fmt, ...)
  595. {
  596. va_list args;
  597. va_start (args, fmt);
  598. SLsmg_vprintf ((char *) fmt, args);
  599. va_end (args);
  600. }
  601. /* --------------------------------------------------------------------------------------------- */
  602. char *
  603. tty_tgetstr (const char *cap)
  604. {
  605. return SLtt_tgetstr ((SLFUTURE_CONST char *) cap);
  606. }
  607. /* --------------------------------------------------------------------------------------------- */
  608. void
  609. tty_refresh (void)
  610. {
  611. SLsmg_refresh ();
  612. }
  613. /* --------------------------------------------------------------------------------------------- */
  614. void
  615. tty_beep (void)
  616. {
  617. SLtt_beep ();
  618. }
  619. /* --------------------------------------------------------------------------------------------- */