tui-io.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /* TUI support I/O functions.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Hewlett-Packard Company.
  4. This file is part of GDB.
  5. This program 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. This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "target.h"
  17. #include "gdbsupport/event-loop.h"
  18. #include "event-top.h"
  19. #include "command.h"
  20. #include "top.h"
  21. #include "tui/tui.h"
  22. #include "tui/tui-data.h"
  23. #include "tui/tui-io.h"
  24. #include "tui/tui-command.h"
  25. #include "tui/tui-win.h"
  26. #include "tui/tui-wingeneral.h"
  27. #include "tui/tui-file.h"
  28. #include "tui/tui-out.h"
  29. #include "ui-out.h"
  30. #include "cli-out.h"
  31. #include <fcntl.h>
  32. #include <signal.h>
  33. #ifdef __MINGW32__
  34. #include <windows.h>
  35. #endif
  36. #include "gdbsupport/filestuff.h"
  37. #include "completer.h"
  38. #include "gdb_curses.h"
  39. #include <map>
  40. #include "pager.h"
  41. /* This redefines CTRL if it is not already defined, so it must come
  42. after terminal state releated include files like <term.h> and
  43. "gdb_curses.h". */
  44. #include "readline/readline.h"
  45. #ifdef __MINGW32__
  46. static SHORT ncurses_norm_attr;
  47. #endif
  48. static int tui_getc (FILE *fp);
  49. static int
  50. key_is_start_sequence (int ch)
  51. {
  52. return (ch == 27);
  53. }
  54. /* Use definition from readline 4.3. */
  55. #undef CTRL_CHAR
  56. #define CTRL_CHAR(c) \
  57. ((c) < control_character_threshold && (((c) & 0x80) == 0))
  58. /* This file controls the IO interactions between gdb and curses.
  59. When the TUI is enabled, gdb has two modes a curses and a standard
  60. mode.
  61. In curses mode, the gdb outputs are made in a curses command
  62. window. For this, the gdb_stdout and gdb_stderr are redirected to
  63. the specific ui_file implemented by TUI. The output is handled by
  64. tui_puts(). The input is also controlled by curses with
  65. tui_getc(). The readline library uses this function to get its
  66. input. Several readline hooks are installed to redirect readline
  67. output to the TUI (see also the note below).
  68. In normal mode, the gdb outputs are restored to their origin, that
  69. is as if TUI is not used. Readline also uses its original getc()
  70. function with stdin.
  71. Note SCz/2001-07-21: the current readline is not clean in its
  72. management of the output. Even if we install a redisplay handler,
  73. it sometimes writes on a stdout file. It is important to redirect
  74. every output produced by readline, otherwise the curses window will
  75. be garbled. This is implemented with a pipe that TUI reads and
  76. readline writes to. A gdb input handler is created so that reading
  77. the pipe is handled automatically. This will probably not work on
  78. non-Unix platforms. The best fix is to make readline clean enough
  79. so that is never write on stdout.
  80. Note SCz/2002-09-01: we now use more readline hooks and it seems
  81. that with them we don't need the pipe anymore (verified by creating
  82. the pipe and closing its end so that write causes a SIGPIPE). The
  83. old pipe code is still there and can be conditionally removed by
  84. #undef TUI_USE_PIPE_FOR_READLINE. */
  85. /* For gdb 5.3, prefer to continue the pipe hack as a backup wheel. */
  86. #ifdef HAVE_PIPE
  87. #define TUI_USE_PIPE_FOR_READLINE
  88. #endif
  89. /* #undef TUI_USE_PIPE_FOR_READLINE */
  90. /* TUI output files. */
  91. static struct ui_file *tui_stdout;
  92. static struct ui_file *tui_stderr;
  93. static struct ui_file *tui_stdlog;
  94. struct ui_out *tui_out;
  95. /* GDB output files in non-curses mode. */
  96. static struct ui_file *tui_old_stdout;
  97. static struct ui_file *tui_old_stderr;
  98. static struct ui_file *tui_old_stdlog;
  99. cli_ui_out *tui_old_uiout;
  100. /* Readline previous hooks. */
  101. static rl_getc_func_t *tui_old_rl_getc_function;
  102. static rl_voidfunc_t *tui_old_rl_redisplay_function;
  103. static rl_vintfunc_t *tui_old_rl_prep_terminal;
  104. static rl_voidfunc_t *tui_old_rl_deprep_terminal;
  105. static rl_compdisp_func_t *tui_old_rl_display_matches_hook;
  106. static int tui_old_rl_echoing_p;
  107. /* Readline output stream.
  108. Should be removed when readline is clean. */
  109. static FILE *tui_rl_outstream;
  110. static FILE *tui_old_rl_outstream;
  111. #ifdef TUI_USE_PIPE_FOR_READLINE
  112. static int tui_readline_pipe[2];
  113. #endif
  114. /* Print a character in the curses command window. The output is
  115. buffered. It is up to the caller to refresh the screen if
  116. necessary. */
  117. static void
  118. do_tui_putc (WINDOW *w, char c)
  119. {
  120. /* Expand TABs, since ncurses on MS-Windows doesn't. */
  121. if (c == '\t')
  122. {
  123. int col;
  124. col = getcurx (w);
  125. do
  126. {
  127. waddch (w, ' ');
  128. col++;
  129. }
  130. while ((col % 8) != 0);
  131. }
  132. else
  133. waddch (w, c);
  134. }
  135. /* Update the cached value of the command window's start line based on
  136. the window's current Y coordinate. */
  137. static void
  138. update_cmdwin_start_line ()
  139. {
  140. TUI_CMD_WIN->start_line = getcury (TUI_CMD_WIN->handle.get ());
  141. }
  142. /* Print a character in the curses command window. The output is
  143. buffered. It is up to the caller to refresh the screen if
  144. necessary. */
  145. static void
  146. tui_putc (char c)
  147. {
  148. do_tui_putc (TUI_CMD_WIN->handle.get (), c);
  149. update_cmdwin_start_line ();
  150. }
  151. /* This maps colors to their corresponding color index. */
  152. static std::map<ui_file_style::color, int> color_map;
  153. /* This holds a pair of colors and is used to track the mapping
  154. between a color pair index and the actual colors. */
  155. struct color_pair
  156. {
  157. int fg;
  158. int bg;
  159. bool operator< (const color_pair &o) const
  160. {
  161. return fg < o.fg || (fg == o.fg && bg < o.bg);
  162. }
  163. };
  164. /* This maps pairs of colors to their corresponding color pair
  165. index. */
  166. static std::map<color_pair, int> color_pair_map;
  167. /* This is indexed by ANSI color offset from the base color, and holds
  168. the corresponding curses color constant. */
  169. static const int curses_colors[] = {
  170. COLOR_BLACK,
  171. COLOR_RED,
  172. COLOR_GREEN,
  173. COLOR_YELLOW,
  174. COLOR_BLUE,
  175. COLOR_MAGENTA,
  176. COLOR_CYAN,
  177. COLOR_WHITE
  178. };
  179. /* Given a color, find its index. */
  180. static bool
  181. get_color (const ui_file_style::color &color, int *result)
  182. {
  183. if (color.is_none ())
  184. *result = -1;
  185. else if (color.is_basic ())
  186. *result = curses_colors[color.get_value ()];
  187. else
  188. {
  189. auto it = color_map.find (color);
  190. if (it == color_map.end ())
  191. {
  192. /* The first 8 colors are standard. */
  193. int next = color_map.size () + 8;
  194. if (next >= COLORS)
  195. return false;
  196. uint8_t rgb[3];
  197. color.get_rgb (rgb);
  198. /* We store RGB as 0..255, but curses wants 0..1000. */
  199. if (init_color (next, rgb[0] * 1000 / 255, rgb[1] * 1000 / 255,
  200. rgb[2] * 1000 / 255) == ERR)
  201. return false;
  202. color_map[color] = next;
  203. *result = next;
  204. }
  205. else
  206. *result = it->second;
  207. }
  208. return true;
  209. }
  210. /* The most recently emitted color pair. */
  211. static int last_color_pair = -1;
  212. /* The most recently applied style. */
  213. static ui_file_style last_style;
  214. /* If true, we're highlighting the current source line in reverse
  215. video mode. */
  216. static bool reverse_mode_p = false;
  217. /* The background/foreground colors before we entered reverse
  218. mode. */
  219. static ui_file_style::color reverse_save_bg (ui_file_style::NONE);
  220. static ui_file_style::color reverse_save_fg (ui_file_style::NONE);
  221. /* Given two colors, return their color pair index; making a new one
  222. if necessary. */
  223. static int
  224. get_color_pair (int fg, int bg)
  225. {
  226. color_pair c = { fg, bg };
  227. auto it = color_pair_map.find (c);
  228. if (it == color_pair_map.end ())
  229. {
  230. /* Color pair 0 is our default color, so new colors start at
  231. 1. */
  232. int next = color_pair_map.size () + 1;
  233. /* Curses has a limited number of available color pairs. Fall
  234. back to the default if we've used too many. */
  235. if (next >= COLOR_PAIRS)
  236. return 0;
  237. init_pair (next, fg, bg);
  238. color_pair_map[c] = next;
  239. return next;
  240. }
  241. return it->second;
  242. }
  243. /* Apply STYLE to W. */
  244. void
  245. tui_apply_style (WINDOW *w, ui_file_style style)
  246. {
  247. /* Reset. */
  248. wattron (w, A_NORMAL);
  249. wattroff (w, A_BOLD);
  250. wattroff (w, A_DIM);
  251. wattroff (w, A_REVERSE);
  252. if (last_color_pair != -1)
  253. wattroff (w, COLOR_PAIR (last_color_pair));
  254. wattron (w, COLOR_PAIR (0));
  255. const ui_file_style::color &fg = style.get_foreground ();
  256. const ui_file_style::color &bg = style.get_background ();
  257. if (!fg.is_none () || !bg.is_none ())
  258. {
  259. int fgi, bgi;
  260. if (get_color (fg, &fgi) && get_color (bg, &bgi))
  261. {
  262. #ifdef __MINGW32__
  263. /* MS-Windows port of ncurses doesn't support implicit
  264. default foreground and background colors, so we must
  265. specify them explicitly when needed, using the colors we
  266. saw at startup. */
  267. if (fgi == -1)
  268. fgi = ncurses_norm_attr & 15;
  269. if (bgi == -1)
  270. bgi = (ncurses_norm_attr >> 4) & 15;
  271. #endif
  272. int pair = get_color_pair (fgi, bgi);
  273. if (last_color_pair != -1)
  274. wattroff (w, COLOR_PAIR (last_color_pair));
  275. wattron (w, COLOR_PAIR (pair));
  276. last_color_pair = pair;
  277. }
  278. }
  279. switch (style.get_intensity ())
  280. {
  281. case ui_file_style::NORMAL:
  282. break;
  283. case ui_file_style::BOLD:
  284. wattron (w, A_BOLD);
  285. break;
  286. case ui_file_style::DIM:
  287. wattron (w, A_DIM);
  288. break;
  289. default:
  290. gdb_assert_not_reached ("invalid intensity");
  291. }
  292. if (style.is_reverse ())
  293. wattron (w, A_REVERSE);
  294. last_style = style;
  295. }
  296. /* Apply an ANSI escape sequence from BUF to W. BUF must start with
  297. the ESC character. If BUF does not start with an ANSI escape,
  298. return 0. Otherwise, apply the sequence if it is recognized, or
  299. simply ignore it if not. In this case, the number of bytes read
  300. from BUF is returned. */
  301. static size_t
  302. apply_ansi_escape (WINDOW *w, const char *buf)
  303. {
  304. ui_file_style style = last_style;
  305. size_t n_read;
  306. if (!style.parse (buf, &n_read))
  307. return n_read;
  308. if (reverse_mode_p)
  309. {
  310. /* We want to reverse _only_ the default foreground/background
  311. colors. If the foreground color is not the default (because
  312. the text was styled), we want to leave it as is. If e.g.,
  313. the terminal is fg=BLACK, and bg=WHITE, and the style wants
  314. to print text in RED, we want to reverse the background color
  315. (print in BLACK), but still print the text in RED. To do
  316. that, we enable the A_REVERSE attribute, and re-reverse the
  317. parsed-style's fb/bg colors.
  318. Notes on the approach:
  319. - there's no portable way to know which colors the default
  320. fb/bg colors map to.
  321. - this approach does the right thing even if you change the
  322. terminal colors while GDB is running -- the reversed
  323. colors automatically adapt.
  324. */
  325. if (!style.is_default ())
  326. {
  327. ui_file_style::color bg = style.get_background ();
  328. ui_file_style::color fg = style.get_foreground ();
  329. style.set_fg (bg);
  330. style.set_bg (fg);
  331. }
  332. /* Enable A_REVERSE. */
  333. style.set_reverse (true);
  334. }
  335. tui_apply_style (w, style);
  336. return n_read;
  337. }
  338. /* See tui.io.h. */
  339. void
  340. tui_set_reverse_mode (WINDOW *w, bool reverse)
  341. {
  342. ui_file_style style = last_style;
  343. reverse_mode_p = reverse;
  344. style.set_reverse (reverse);
  345. if (reverse)
  346. {
  347. reverse_save_bg = style.get_background ();
  348. reverse_save_fg = style.get_foreground ();
  349. }
  350. else
  351. {
  352. style.set_bg (reverse_save_bg);
  353. style.set_fg (reverse_save_fg);
  354. }
  355. tui_apply_style (w, style);
  356. }
  357. /* Print LENGTH characters from the buffer pointed to by BUF to the
  358. curses command window. The output is buffered. It is up to the
  359. caller to refresh the screen if necessary. */
  360. void
  361. tui_write (const char *buf, size_t length)
  362. {
  363. /* We need this to be \0-terminated for the regexp matching. */
  364. std::string copy (buf, length);
  365. tui_puts (copy.c_str ());
  366. }
  367. /* Print a string in the curses command window. The output is
  368. buffered. It is up to the caller to refresh the screen if
  369. necessary. */
  370. void
  371. tui_puts (const char *string, WINDOW *w)
  372. {
  373. if (w == nullptr)
  374. w = TUI_CMD_WIN->handle.get ();
  375. while (true)
  376. {
  377. const char *next = strpbrk (string, "\n\1\2\033\t");
  378. /* Print the plain text prefix. */
  379. size_t n_chars = next == nullptr ? strlen (string) : next - string;
  380. if (n_chars > 0)
  381. waddnstr (w, string, n_chars);
  382. /* We finished. */
  383. if (next == nullptr)
  384. break;
  385. char c = *next;
  386. switch (c)
  387. {
  388. case '\1':
  389. case '\2':
  390. /* Ignore these, they are readline escape-marking
  391. sequences. */
  392. ++next;
  393. break;
  394. case '\n':
  395. case '\t':
  396. do_tui_putc (w, c);
  397. ++next;
  398. break;
  399. case '\033':
  400. {
  401. size_t bytes_read = apply_ansi_escape (w, next);
  402. if (bytes_read > 0)
  403. next += bytes_read;
  404. else
  405. {
  406. /* Just drop the escape. */
  407. ++next;
  408. }
  409. }
  410. break;
  411. default:
  412. gdb_assert_not_reached ("missing case in tui_puts");
  413. }
  414. string = next;
  415. }
  416. if (TUI_CMD_WIN != nullptr && w == TUI_CMD_WIN->handle.get ())
  417. update_cmdwin_start_line ();
  418. }
  419. static void
  420. tui_puts_internal (WINDOW *w, const char *string, int *height)
  421. {
  422. char c;
  423. int prev_col = 0;
  424. bool saw_nl = false;
  425. while ((c = *string++) != 0)
  426. {
  427. if (c == '\n')
  428. saw_nl = true;
  429. if (c == '\1' || c == '\2')
  430. {
  431. /* Ignore these, they are readline escape-marking
  432. sequences. */
  433. }
  434. else
  435. {
  436. if (c == '\033')
  437. {
  438. size_t bytes_read = apply_ansi_escape (w, string - 1);
  439. if (bytes_read > 0)
  440. {
  441. string = string + bytes_read - 1;
  442. continue;
  443. }
  444. }
  445. do_tui_putc (w, c);
  446. if (height != nullptr)
  447. {
  448. int col = getcurx (w);
  449. if (col <= prev_col)
  450. ++*height;
  451. prev_col = col;
  452. }
  453. }
  454. }
  455. if (TUI_CMD_WIN != nullptr && w == TUI_CMD_WIN->handle.get ())
  456. update_cmdwin_start_line ();
  457. if (saw_nl)
  458. wrefresh (w);
  459. }
  460. /* Readline callback.
  461. Redisplay the command line with its prompt after readline has
  462. changed the edited text. */
  463. void
  464. tui_redisplay_readline (void)
  465. {
  466. int prev_col;
  467. int height;
  468. int col;
  469. int c_pos;
  470. int c_line;
  471. int in;
  472. WINDOW *w;
  473. const char *prompt;
  474. int start_line;
  475. /* Detect when we temporarily left SingleKey and now the readline
  476. edit buffer is empty, automatically restore the SingleKey
  477. mode. The restore must only be done if the command has finished.
  478. The command could call prompt_for_continue and we must not
  479. restore SingleKey so that the prompt and normal keymap are used. */
  480. if (tui_current_key_mode == TUI_ONE_COMMAND_MODE && rl_end == 0
  481. && !gdb_in_secondary_prompt_p (current_ui))
  482. tui_set_key_mode (TUI_SINGLE_KEY_MODE);
  483. if (tui_current_key_mode == TUI_SINGLE_KEY_MODE)
  484. prompt = "";
  485. else
  486. prompt = rl_display_prompt;
  487. c_pos = -1;
  488. c_line = -1;
  489. w = TUI_CMD_WIN->handle.get ();
  490. start_line = TUI_CMD_WIN->start_line;
  491. wmove (w, start_line, 0);
  492. prev_col = 0;
  493. height = 1;
  494. if (prompt != nullptr)
  495. tui_puts_internal (w, prompt, &height);
  496. prev_col = getcurx (w);
  497. for (in = 0; in <= rl_end; in++)
  498. {
  499. unsigned char c;
  500. if (in == rl_point)
  501. {
  502. getyx (w, c_line, c_pos);
  503. }
  504. if (in == rl_end)
  505. break;
  506. c = (unsigned char) rl_line_buffer[in];
  507. if (CTRL_CHAR (c) || c == RUBOUT)
  508. {
  509. waddch (w, '^');
  510. waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?');
  511. }
  512. else if (c == '\t')
  513. {
  514. /* Expand TABs, since ncurses on MS-Windows doesn't. */
  515. col = getcurx (w);
  516. do
  517. {
  518. waddch (w, ' ');
  519. col++;
  520. } while ((col % 8) != 0);
  521. }
  522. else
  523. {
  524. waddch (w, c);
  525. }
  526. if (c == '\n')
  527. TUI_CMD_WIN->start_line = getcury (w);
  528. col = getcurx (w);
  529. if (col < prev_col)
  530. height++;
  531. prev_col = col;
  532. }
  533. wclrtobot (w);
  534. TUI_CMD_WIN->start_line = getcury (w);
  535. if (c_line >= 0)
  536. wmove (w, c_line, c_pos);
  537. TUI_CMD_WIN->start_line -= height - 1;
  538. wrefresh (w);
  539. fflush(stdout);
  540. }
  541. /* Readline callback to prepare the terminal. It is called once each
  542. time we enter readline. Terminal is already setup in curses
  543. mode. */
  544. static void
  545. tui_prep_terminal (int notused1)
  546. {
  547. #ifdef NCURSES_MOUSE_VERSION
  548. mousemask (ALL_MOUSE_EVENTS, NULL);
  549. #endif
  550. }
  551. /* Readline callback to restore the terminal. It is called once each
  552. time we leave readline. There is nothing to do in curses mode. */
  553. static void
  554. tui_deprep_terminal (void)
  555. {
  556. #ifdef NCURSES_MOUSE_VERSION
  557. mousemask (0, NULL);
  558. #endif
  559. }
  560. #ifdef TUI_USE_PIPE_FOR_READLINE
  561. /* Read readline output pipe and feed the command window with it.
  562. Should be removed when readline is clean. */
  563. static void
  564. tui_readline_output (int error, gdb_client_data data)
  565. {
  566. int size;
  567. char buf[256];
  568. size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1);
  569. if (size > 0 && tui_active)
  570. {
  571. buf[size] = 0;
  572. tui_puts (buf);
  573. }
  574. }
  575. #endif
  576. /* TUI version of displayer.crlf. */
  577. static void
  578. tui_mld_crlf (const struct match_list_displayer *displayer)
  579. {
  580. tui_putc ('\n');
  581. }
  582. /* TUI version of displayer.putch. */
  583. static void
  584. tui_mld_putch (const struct match_list_displayer *displayer, int ch)
  585. {
  586. tui_putc (ch);
  587. }
  588. /* TUI version of displayer.puts. */
  589. static void
  590. tui_mld_puts (const struct match_list_displayer *displayer, const char *s)
  591. {
  592. tui_puts (s);
  593. }
  594. /* TUI version of displayer.flush. */
  595. static void
  596. tui_mld_flush (const struct match_list_displayer *displayer)
  597. {
  598. wrefresh (TUI_CMD_WIN->handle.get ());
  599. }
  600. /* TUI version of displayer.erase_entire_line. */
  601. static void
  602. tui_mld_erase_entire_line (const struct match_list_displayer *displayer)
  603. {
  604. WINDOW *w = TUI_CMD_WIN->handle.get ();
  605. int cur_y = getcury (w);
  606. wmove (w, cur_y, 0);
  607. wclrtoeol (w);
  608. wmove (w, cur_y, 0);
  609. }
  610. /* TUI version of displayer.beep. */
  611. static void
  612. tui_mld_beep (const struct match_list_displayer *displayer)
  613. {
  614. beep ();
  615. }
  616. /* A wrapper for wgetch that enters nonl mode. We We normally want
  617. curses' "nl" mode, but when reading from the user, we'd like to
  618. differentiate between C-j and C-m, because some users bind these
  619. keys differently in their .inputrc. So, put curses into nonl mode
  620. just when reading from the user. See PR tui/20819. */
  621. static int
  622. gdb_wgetch (WINDOW *win)
  623. {
  624. nonl ();
  625. int r = wgetch (win);
  626. nl ();
  627. return r;
  628. }
  629. /* Helper function for tui_mld_read_key.
  630. This temporarily replaces tui_getc for use during tab-completion
  631. match list display. */
  632. static int
  633. tui_mld_getc (FILE *fp)
  634. {
  635. WINDOW *w = TUI_CMD_WIN->handle.get ();
  636. int c = gdb_wgetch (w);
  637. return c;
  638. }
  639. /* TUI version of displayer.read_key. */
  640. static int
  641. tui_mld_read_key (const struct match_list_displayer *displayer)
  642. {
  643. rl_getc_func_t *prev = rl_getc_function;
  644. int c;
  645. /* We can't use tui_getc as we need NEWLINE to not get emitted. */
  646. rl_getc_function = tui_mld_getc;
  647. c = rl_read_key ();
  648. rl_getc_function = prev;
  649. return c;
  650. }
  651. /* TUI version of rl_completion_display_matches_hook.
  652. See gdb_display_match_list for a description of the arguments. */
  653. static void
  654. tui_rl_display_match_list (char **matches, int len, int max)
  655. {
  656. struct match_list_displayer displayer;
  657. rl_get_screen_size (&displayer.height, &displayer.width);
  658. displayer.crlf = tui_mld_crlf;
  659. displayer.putch = tui_mld_putch;
  660. displayer.puts = tui_mld_puts;
  661. displayer.flush = tui_mld_flush;
  662. displayer.erase_entire_line = tui_mld_erase_entire_line;
  663. displayer.beep = tui_mld_beep;
  664. displayer.read_key = tui_mld_read_key;
  665. gdb_display_match_list (matches, len, max, &displayer);
  666. }
  667. /* Setup the IO for curses or non-curses mode.
  668. - In non-curses mode, readline and gdb use the standard input and
  669. standard output/error directly.
  670. - In curses mode, the standard output/error is controlled by TUI
  671. with the tui_stdout and tui_stderr. The output is redirected in
  672. the curses command window. Several readline callbacks are installed
  673. so that readline asks for its input to the curses command window
  674. with wgetch(). */
  675. void
  676. tui_setup_io (int mode)
  677. {
  678. extern int _rl_echoing_p;
  679. if (mode)
  680. {
  681. /* Ensure that readline has been initialized before saving any
  682. of its variables. */
  683. tui_ensure_readline_initialized ();
  684. /* Redirect readline to TUI. */
  685. tui_old_rl_redisplay_function = rl_redisplay_function;
  686. tui_old_rl_deprep_terminal = rl_deprep_term_function;
  687. tui_old_rl_prep_terminal = rl_prep_term_function;
  688. tui_old_rl_getc_function = rl_getc_function;
  689. tui_old_rl_display_matches_hook = rl_completion_display_matches_hook;
  690. tui_old_rl_outstream = rl_outstream;
  691. tui_old_rl_echoing_p = _rl_echoing_p;
  692. rl_redisplay_function = tui_redisplay_readline;
  693. rl_deprep_term_function = tui_deprep_terminal;
  694. rl_prep_term_function = tui_prep_terminal;
  695. rl_getc_function = tui_getc;
  696. _rl_echoing_p = 0;
  697. rl_outstream = tui_rl_outstream;
  698. rl_prompt = 0;
  699. rl_completion_display_matches_hook = tui_rl_display_match_list;
  700. rl_already_prompted = 0;
  701. /* Keep track of previous gdb output. */
  702. tui_old_stdout = gdb_stdout;
  703. tui_old_stderr = gdb_stderr;
  704. tui_old_stdlog = gdb_stdlog;
  705. tui_old_uiout = dynamic_cast<cli_ui_out *> (current_uiout);
  706. gdb_assert (tui_old_uiout != nullptr);
  707. /* Reconfigure gdb output. */
  708. gdb_stdout = tui_stdout;
  709. gdb_stderr = tui_stderr;
  710. gdb_stdlog = tui_stdlog;
  711. gdb_stdtarg = gdb_stderr; /* for moment */
  712. gdb_stdtargerr = gdb_stderr; /* for moment */
  713. current_uiout = tui_out;
  714. /* Save tty for SIGCONT. */
  715. savetty ();
  716. }
  717. else
  718. {
  719. /* Restore gdb output. */
  720. gdb_stdout = tui_old_stdout;
  721. gdb_stderr = tui_old_stderr;
  722. gdb_stdlog = tui_old_stdlog;
  723. gdb_stdtarg = gdb_stderr; /* for moment */
  724. gdb_stdtargerr = gdb_stderr; /* for moment */
  725. current_uiout = tui_old_uiout;
  726. /* Restore readline. */
  727. rl_redisplay_function = tui_old_rl_redisplay_function;
  728. rl_deprep_term_function = tui_old_rl_deprep_terminal;
  729. rl_prep_term_function = tui_old_rl_prep_terminal;
  730. rl_getc_function = tui_old_rl_getc_function;
  731. rl_completion_display_matches_hook = tui_old_rl_display_matches_hook;
  732. rl_outstream = tui_old_rl_outstream;
  733. _rl_echoing_p = tui_old_rl_echoing_p;
  734. rl_already_prompted = 0;
  735. /* Save tty for SIGCONT. */
  736. savetty ();
  737. /* Clean up color information. */
  738. last_color_pair = -1;
  739. last_style = ui_file_style ();
  740. color_map.clear ();
  741. color_pair_map.clear ();
  742. }
  743. }
  744. #ifdef SIGCONT
  745. /* Catch SIGCONT to restore the terminal and refresh the screen. */
  746. static void
  747. tui_cont_sig (int sig)
  748. {
  749. if (tui_active)
  750. {
  751. /* Restore the terminal setting because another process (shell)
  752. might have changed it. */
  753. resetty ();
  754. /* Force a refresh of the screen. */
  755. tui_refresh_all_win ();
  756. }
  757. signal (sig, tui_cont_sig);
  758. }
  759. #endif
  760. /* Initialize the IO for gdb in curses mode. */
  761. void
  762. tui_initialize_io (void)
  763. {
  764. #ifdef SIGCONT
  765. signal (SIGCONT, tui_cont_sig);
  766. #endif
  767. /* Create tui output streams. */
  768. tui_stdout = new pager_file (new tui_file (stdout));
  769. tui_stderr = new tui_file (stderr);
  770. tui_stdlog = new timestamped_file (tui_stderr);
  771. tui_out = tui_out_new (tui_stdout);
  772. /* Create the default UI. */
  773. tui_old_uiout = cli_out_new (gdb_stdout);
  774. #ifdef TUI_USE_PIPE_FOR_READLINE
  775. /* Temporary solution for readline writing to stdout: redirect
  776. readline output in a pipe, read that pipe and output the content
  777. in the curses command window. */
  778. if (gdb_pipe_cloexec (tui_readline_pipe) != 0)
  779. error (_("Cannot create pipe for readline"));
  780. tui_rl_outstream = fdopen (tui_readline_pipe[1], "w");
  781. if (tui_rl_outstream == 0)
  782. error (_("Cannot redirect readline output"));
  783. setvbuf (tui_rl_outstream, NULL, _IOLBF, 0);
  784. #ifdef O_NONBLOCK
  785. (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
  786. #else
  787. #ifdef O_NDELAY
  788. (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY);
  789. #endif
  790. #endif
  791. add_file_handler (tui_readline_pipe[0], tui_readline_output, 0, "tui");
  792. #else
  793. tui_rl_outstream = stdout;
  794. #endif
  795. #ifdef __MINGW32__
  796. /* MS-Windows port of ncurses doesn't support default foreground and
  797. background colors, so we must record the default colors at startup. */
  798. HANDLE hstdout = (HANDLE)_get_osfhandle (fileno (stdout));
  799. DWORD cmode;
  800. CONSOLE_SCREEN_BUFFER_INFO csbi;
  801. if (hstdout != INVALID_HANDLE_VALUE
  802. && GetConsoleMode (hstdout, &cmode) != 0
  803. && GetConsoleScreenBufferInfo (hstdout, &csbi))
  804. ncurses_norm_attr = csbi.wAttributes;
  805. #endif
  806. }
  807. /* Dispatch the correct tui function based upon the mouse event. */
  808. #ifdef NCURSES_MOUSE_VERSION
  809. static void
  810. tui_dispatch_mouse_event ()
  811. {
  812. MEVENT mev;
  813. if (getmouse (&mev) != OK)
  814. return;
  815. for (tui_win_info *wi : all_tui_windows ())
  816. if (mev.x > wi->x && mev.x < wi->x + wi->width - 1
  817. && mev.y > wi->y && mev.y < wi->y + wi->height - 1)
  818. {
  819. if ((mev.bstate & BUTTON1_CLICKED) != 0
  820. || (mev.bstate & BUTTON2_CLICKED) != 0
  821. || (mev.bstate & BUTTON3_CLICKED) != 0)
  822. {
  823. int button = (mev.bstate & BUTTON1_CLICKED) != 0 ? 1
  824. : ((mev.bstate & BUTTON2_CLICKED) != 0 ? 2
  825. : 3);
  826. wi->click (mev.x - wi->x - 1, mev.y - wi->y - 1, button);
  827. }
  828. #ifdef BUTTON5_PRESSED
  829. else if ((mev.bstate & BUTTON4_PRESSED) != 0)
  830. wi->backward_scroll (3);
  831. else if ((mev.bstate & BUTTON5_PRESSED) != 0)
  832. wi->forward_scroll (3);
  833. #endif
  834. break;
  835. }
  836. }
  837. #endif
  838. /* Dispatch the correct tui function based upon the control
  839. character. */
  840. static unsigned int
  841. tui_dispatch_ctrl_char (unsigned int ch)
  842. {
  843. struct tui_win_info *win_info = tui_win_with_focus ();
  844. /* If no window has the focus, or if the focus window can't scroll,
  845. just pass the character through. */
  846. if (win_info == NULL || !win_info->can_scroll ())
  847. return ch;
  848. switch (ch)
  849. {
  850. case KEY_NPAGE:
  851. win_info->forward_scroll (0);
  852. break;
  853. case KEY_PPAGE:
  854. win_info->backward_scroll (0);
  855. break;
  856. case KEY_DOWN:
  857. case KEY_SF:
  858. win_info->forward_scroll (1);
  859. break;
  860. case KEY_UP:
  861. case KEY_SR:
  862. win_info->backward_scroll (1);
  863. break;
  864. case KEY_RIGHT:
  865. win_info->left_scroll (1);
  866. break;
  867. case KEY_LEFT:
  868. win_info->right_scroll (1);
  869. break;
  870. default:
  871. /* We didn't recognize the character as a control character, so pass it
  872. through. */
  873. return ch;
  874. }
  875. /* We intercepted the control character, so return 0 (which readline
  876. will interpret as a no-op). */
  877. return 0;
  878. }
  879. /* See tui-io.h. */
  880. void
  881. tui_inject_newline_into_command_window ()
  882. {
  883. gdb_assert (tui_active);
  884. WINDOW *w = TUI_CMD_WIN->handle.get ();
  885. /* When hitting return with an empty input, gdb executes the last
  886. command. If we emit a newline, this fills up the command window
  887. with empty lines with gdb prompt at beginning. Instead of that,
  888. stay on the same line but provide a visual effect to show the
  889. user we recognized the command. */
  890. if (rl_end == 0 && !gdb_in_secondary_prompt_p (current_ui))
  891. {
  892. wmove (w, getcury (w), 0);
  893. /* Clear the line. This will blink the gdb prompt since
  894. it will be redrawn at the same line. */
  895. wclrtoeol (w);
  896. wrefresh (w);
  897. napms (20);
  898. }
  899. else
  900. {
  901. /* Move cursor to the end of the command line before emitting the
  902. newline. We need to do so because when ncurses outputs a newline
  903. it truncates any text that appears past the end of the cursor. */
  904. int px, py;
  905. getyx (w, py, px);
  906. px += rl_end - rl_point;
  907. py += px / TUI_CMD_WIN->width;
  908. px %= TUI_CMD_WIN->width;
  909. wmove (w, py, px);
  910. tui_putc ('\n');
  911. }
  912. }
  913. /* If we're passing an escape sequence to readline, this points to a
  914. string holding the remaining characters of the sequence to pass.
  915. We advance the pointer one character at a time until '\0' is
  916. reached. */
  917. static const char *cur_seq = nullptr;
  918. /* Set CUR_SEQ to point at the current sequence to pass to readline,
  919. setup to call the input handler again so we complete the sequence
  920. shortly, and return the first character to start the sequence. */
  921. static int
  922. start_sequence (const char *seq)
  923. {
  924. call_stdin_event_handler_again_p = 1;
  925. cur_seq = seq + 1;
  926. return seq[0];
  927. }
  928. /* Main worker for tui_getc. Get a character from the command window.
  929. This is called from the readline package, but wrapped in a
  930. try/catch by tui_getc. */
  931. static int
  932. tui_getc_1 (FILE *fp)
  933. {
  934. int ch;
  935. WINDOW *w;
  936. w = TUI_CMD_WIN->handle.get ();
  937. #ifdef TUI_USE_PIPE_FOR_READLINE
  938. /* Flush readline output. */
  939. tui_readline_output (0, 0);
  940. #endif
  941. /* We enable keypad mode so that curses's wgetch processes mouse
  942. escape sequences. In keypad mode, wgetch also processes the
  943. escape sequences for keys such as up/down etc. and returns KEY_UP
  944. / KEY_DOWN etc. When we have the focus on the command window
  945. though, we want to pass the raw up/down etc. escape codes to
  946. readline so readline understands them. */
  947. if (cur_seq != nullptr)
  948. {
  949. ch = *cur_seq++;
  950. /* If we've reached the end of the string, we're done with the
  951. sequence. Otherwise, setup to get back here again for
  952. another character. */
  953. if (*cur_seq == '\0')
  954. cur_seq = nullptr;
  955. else
  956. call_stdin_event_handler_again_p = 1;
  957. return ch;
  958. }
  959. else
  960. ch = gdb_wgetch (w);
  961. /* Handle prev/next/up/down here. */
  962. ch = tui_dispatch_ctrl_char (ch);
  963. #ifdef NCURSES_MOUSE_VERSION
  964. if (ch == KEY_MOUSE)
  965. {
  966. tui_dispatch_mouse_event ();
  967. return 0;
  968. }
  969. #endif
  970. /* Translate curses keys back to escape sequences so that readline
  971. can understand them. We do this irrespective of which window has
  972. the focus. If e.g., we're focused on a non-command window, then
  973. the up/down keys will already have been filtered by
  974. tui_dispatch_ctrl_char. Keys that haven't been intercepted will
  975. be passed down to readline. */
  976. if (current_ui->command_editing)
  977. {
  978. /* For the standard arrow keys + home/end, hardcode sequences
  979. readline understands. See bind_arrow_keys_internal in
  980. readline/readline.c. */
  981. switch (ch)
  982. {
  983. case KEY_UP:
  984. return start_sequence ("\033[A");
  985. case KEY_DOWN:
  986. return start_sequence ("\033[B");
  987. case KEY_RIGHT:
  988. return start_sequence ("\033[C");
  989. case KEY_LEFT:
  990. return start_sequence ("\033[D");
  991. case KEY_HOME:
  992. return start_sequence ("\033[H");
  993. case KEY_END:
  994. return start_sequence ("\033[F");
  995. /* del and ins are unfortunately not hardcoded in readline for
  996. all systems. */
  997. case KEY_DC: /* del */
  998. #ifdef __MINGW32__
  999. return start_sequence ("\340S");
  1000. #else
  1001. return start_sequence ("\033[3~");
  1002. #endif
  1003. case KEY_IC: /* ins */
  1004. #if defined __MINGW32__
  1005. return start_sequence ("\340R");
  1006. #else
  1007. return start_sequence ("\033[2~");
  1008. #endif
  1009. }
  1010. /* Keycodes above KEY_MAX are not garanteed to be stable.
  1011. Compare keyname instead. */
  1012. if (ch >= KEY_MAX)
  1013. {
  1014. auto name = gdb::string_view (keyname (ch));
  1015. /* The following sequences are hardcoded in readline as
  1016. well. */
  1017. /* ctrl-arrow keys */
  1018. if (name == "kLFT5") /* ctrl-left */
  1019. return start_sequence ("\033[1;5D");
  1020. else if (name == "kRIT5") /* ctrl-right */
  1021. return start_sequence ("\033[1;5C");
  1022. else if (name == "kDC5") /* ctrl-del */
  1023. return start_sequence ("\033[3;5~");
  1024. /* alt-arrow keys */
  1025. else if (name == "kLFT3") /* alt-left */
  1026. return start_sequence ("\033[1;3D");
  1027. else if (name == "kRIT3") /* alt-right */
  1028. return start_sequence ("\033[1;3C");
  1029. }
  1030. }
  1031. /* Handle the CTRL-L refresh for each window. */
  1032. if (ch == '\f')
  1033. {
  1034. tui_refresh_all_win ();
  1035. return ch;
  1036. }
  1037. if (ch == KEY_BACKSPACE)
  1038. return '\b';
  1039. if (current_ui->command_editing && key_is_start_sequence (ch))
  1040. {
  1041. int ch_pending;
  1042. nodelay (w, TRUE);
  1043. ch_pending = gdb_wgetch (w);
  1044. nodelay (w, FALSE);
  1045. /* If we have pending input following a start sequence, call the stdin
  1046. event handler again because ncurses may have already read and stored
  1047. the input into its internal buffer, meaning that we won't get an stdin
  1048. event for it. If we don't compensate for this missed stdin event, key
  1049. sequences as Alt_F (^[f) will not behave promptly.
  1050. (We only compensates for the missed 2nd byte of a key sequence because
  1051. 2-byte sequences are by far the most commonly used. ncurses may have
  1052. buffered a larger, 3+-byte key sequence though it remains to be seen
  1053. whether it is useful to compensate for all the bytes of such
  1054. sequences.) */
  1055. if (ch_pending != ERR)
  1056. {
  1057. ungetch (ch_pending);
  1058. call_stdin_event_handler_again_p = 1;
  1059. }
  1060. }
  1061. if (ch > 0xff)
  1062. {
  1063. /* Readline doesn't understand non-8-bit curses keys, filter
  1064. them out. */
  1065. return 0;
  1066. }
  1067. return ch;
  1068. }
  1069. /* Get a character from the command window. This is called from the
  1070. readline package. */
  1071. static int
  1072. tui_getc (FILE *fp)
  1073. {
  1074. try
  1075. {
  1076. return tui_getc_1 (fp);
  1077. }
  1078. catch (const gdb_exception &ex)
  1079. {
  1080. /* Just in case, don't ever let an exception escape to readline.
  1081. This shouldn't ever happen, but if it does, print the
  1082. exception instead of just crashing GDB. */
  1083. exception_print (gdb_stderr, ex);
  1084. /* If we threw an exception, it's because we recognized the
  1085. character. */
  1086. return 0;
  1087. }
  1088. }