cli-out.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* Output generating routines for GDB CLI.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions.
  4. Written by Fernando Nasser for Cygnus.
  5. This file is part of GDB.
  6. This program 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. This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include "ui-out.h"
  18. #include "cli-out.h"
  19. #include "completer.h"
  20. #include "readline/readline.h"
  21. #include "cli/cli-style.h"
  22. /* These are the CLI output functions */
  23. /* Mark beginning of a table */
  24. void
  25. cli_ui_out::do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
  26. {
  27. if (nr_rows == 0)
  28. m_suppress_output = true;
  29. else
  30. /* Only the table suppresses the output and, fortunately, a table
  31. is not a recursive data structure. */
  32. gdb_assert (!m_suppress_output);
  33. }
  34. /* Mark beginning of a table body */
  35. void
  36. cli_ui_out::do_table_body ()
  37. {
  38. if (m_suppress_output)
  39. return;
  40. /* first, close the table header line */
  41. text ("\n");
  42. }
  43. /* Mark end of a table */
  44. void
  45. cli_ui_out::do_table_end ()
  46. {
  47. m_suppress_output = false;
  48. }
  49. /* Specify table header */
  50. void
  51. cli_ui_out::do_table_header (int width, ui_align alignment,
  52. const std::string &col_name,
  53. const std::string &col_hdr)
  54. {
  55. if (m_suppress_output)
  56. return;
  57. do_field_string (0, width, alignment, 0, col_hdr.c_str (),
  58. ui_file_style ());
  59. }
  60. /* Mark beginning of a list */
  61. void
  62. cli_ui_out::do_begin (ui_out_type type, const char *id)
  63. {
  64. }
  65. /* Mark end of a list */
  66. void
  67. cli_ui_out::do_end (ui_out_type type)
  68. {
  69. }
  70. /* output an int field */
  71. void
  72. cli_ui_out::do_field_signed (int fldno, int width, ui_align alignment,
  73. const char *fldname, LONGEST value)
  74. {
  75. if (m_suppress_output)
  76. return;
  77. do_field_string (fldno, width, alignment, fldname, plongest (value),
  78. ui_file_style ());
  79. }
  80. /* output an unsigned field */
  81. void
  82. cli_ui_out::do_field_unsigned (int fldno, int width, ui_align alignment,
  83. const char *fldname, ULONGEST value)
  84. {
  85. if (m_suppress_output)
  86. return;
  87. do_field_string (fldno, width, alignment, fldname, pulongest (value),
  88. ui_file_style ());
  89. }
  90. /* used to omit a field */
  91. void
  92. cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
  93. const char *fldname)
  94. {
  95. if (m_suppress_output)
  96. return;
  97. do_field_string (fldno, width, alignment, fldname, "",
  98. ui_file_style ());
  99. }
  100. /* other specific cli_field_* end up here so alignment and field
  101. separators are both handled by cli_field_string */
  102. void
  103. cli_ui_out::do_field_string (int fldno, int width, ui_align align,
  104. const char *fldname, const char *string,
  105. const ui_file_style &style)
  106. {
  107. int before = 0;
  108. int after = 0;
  109. if (m_suppress_output)
  110. return;
  111. if ((align != ui_noalign) && string)
  112. {
  113. before = width - strlen (string);
  114. if (before <= 0)
  115. before = 0;
  116. else
  117. {
  118. if (align == ui_right)
  119. after = 0;
  120. else if (align == ui_left)
  121. {
  122. after = before;
  123. before = 0;
  124. }
  125. else
  126. /* ui_center */
  127. {
  128. after = before / 2;
  129. before -= after;
  130. }
  131. }
  132. }
  133. if (before)
  134. spaces (before);
  135. if (string)
  136. {
  137. ui_file *stream = m_streams.back ();
  138. stream->emit_style_escape (style);
  139. stream->puts (string);
  140. stream->emit_style_escape (ui_file_style ());
  141. }
  142. if (after)
  143. spaces (after);
  144. if (align != ui_noalign)
  145. field_separator ();
  146. }
  147. /* Output field containing ARGS using printf formatting in FORMAT. */
  148. void
  149. cli_ui_out::do_field_fmt (int fldno, int width, ui_align align,
  150. const char *fldname, const ui_file_style &style,
  151. const char *format, va_list args)
  152. {
  153. if (m_suppress_output)
  154. return;
  155. std::string str = string_vprintf (format, args);
  156. do_field_string (fldno, width, align, fldname, str.c_str (), style);
  157. }
  158. void
  159. cli_ui_out::do_spaces (int numspaces)
  160. {
  161. if (m_suppress_output)
  162. return;
  163. print_spaces (numspaces, m_streams.back ());
  164. }
  165. void
  166. cli_ui_out::do_text (const char *string)
  167. {
  168. if (m_suppress_output)
  169. return;
  170. gdb_puts (string, m_streams.back ());
  171. }
  172. void
  173. cli_ui_out::do_message (const ui_file_style &style,
  174. const char *format, va_list args)
  175. {
  176. if (m_suppress_output)
  177. return;
  178. std::string str = string_vprintf (format, args);
  179. if (!str.empty ())
  180. {
  181. ui_file *stream = m_streams.back ();
  182. stream->emit_style_escape (style);
  183. stream->puts (str.c_str ());
  184. stream->emit_style_escape (ui_file_style ());
  185. }
  186. }
  187. void
  188. cli_ui_out::do_wrap_hint (int indent)
  189. {
  190. if (m_suppress_output)
  191. return;
  192. m_streams.back ()->wrap_here (indent);
  193. }
  194. void
  195. cli_ui_out::do_flush ()
  196. {
  197. gdb_flush (m_streams.back ());
  198. }
  199. /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
  200. and make it therefore active. OUTSTREAM as NULL will pop the last pushed
  201. output stream; it is an internal error if it does not exist. */
  202. void
  203. cli_ui_out::do_redirect (ui_file *outstream)
  204. {
  205. if (outstream != NULL)
  206. m_streams.push_back (outstream);
  207. else
  208. m_streams.pop_back ();
  209. }
  210. /* The cli_ui_out::do_progress_* functions result in the following:
  211. - printed for tty, SHOULD_PRINT == true:
  212. <NAME
  213. [##### ]\r>
  214. - printed for tty, SHOULD_PRINT == false:
  215. <>
  216. - printed for not-a-tty:
  217. <NAME...
  218. >
  219. */
  220. void
  221. cli_ui_out::do_progress_start (const std::string &name, bool should_print)
  222. {
  223. struct ui_file *stream = m_streams.back ();
  224. cli_progress_info meter;
  225. meter.last_value = 0;
  226. meter.name = name;
  227. if (!stream->isatty ())
  228. {
  229. gdb_printf (stream, "%s...", meter.name.c_str ());
  230. gdb_flush (stream);
  231. meter.printing = WORKING;
  232. }
  233. else
  234. {
  235. /* Don't actually emit anything until the first call notifies us
  236. of progress. This makes it so a second progress message can
  237. be started before the first one has been notified, without
  238. messy output. */
  239. meter.printing = should_print ? START : NO_PRINT;
  240. }
  241. m_meters.push_back (std::move (meter));
  242. }
  243. void
  244. cli_ui_out::do_progress_notify (double howmuch)
  245. {
  246. struct ui_file *stream = m_streams.back ();
  247. cli_progress_info &meter (m_meters.back ());
  248. if (meter.printing == NO_PRINT)
  249. return;
  250. if (meter.printing == START)
  251. {
  252. gdb_printf (stream, "%s\n", meter.name.c_str ());
  253. gdb_flush (stream);
  254. meter.printing = WORKING;
  255. }
  256. if (meter.printing == WORKING && howmuch >= 1.0)
  257. return;
  258. if (!stream->isatty ())
  259. return;
  260. int chars_per_line = get_chars_per_line ();
  261. if (chars_per_line > 0)
  262. {
  263. int i, max;
  264. int width = chars_per_line - 3;
  265. max = width * howmuch;
  266. gdb_printf (stream, "\r[");
  267. for (i = 0; i < width; ++i)
  268. gdb_printf (stream, i < max ? "#" : " ");
  269. gdb_printf (stream, "]");
  270. gdb_flush (stream);
  271. meter.printing = PROGRESS;
  272. }
  273. }
  274. void
  275. cli_ui_out::do_progress_end ()
  276. {
  277. struct ui_file *stream = m_streams.back ();
  278. cli_progress_info &meter = m_meters.back ();
  279. if (!stream->isatty ())
  280. {
  281. gdb_printf (stream, "\n");
  282. gdb_flush (stream);
  283. }
  284. else if (meter.printing == PROGRESS)
  285. {
  286. int i;
  287. int width = get_chars_per_line () - 3;
  288. gdb_printf (stream, "\r");
  289. for (i = 0; i < width + 2; ++i)
  290. gdb_printf (stream, " ");
  291. gdb_printf (stream, "\r");
  292. gdb_flush (stream);
  293. }
  294. m_meters.pop_back ();
  295. }
  296. /* local functions */
  297. void
  298. cli_ui_out::field_separator ()
  299. {
  300. gdb_putc (' ', m_streams.back ());
  301. }
  302. /* Constructor for cli_ui_out. */
  303. cli_ui_out::cli_ui_out (ui_file *stream, ui_out_flags flags)
  304. : ui_out (flags),
  305. m_suppress_output (false)
  306. {
  307. gdb_assert (stream != NULL);
  308. m_streams.push_back (stream);
  309. }
  310. cli_ui_out::~cli_ui_out ()
  311. {
  312. }
  313. /* Initialize private members at startup. */
  314. cli_ui_out *
  315. cli_out_new (struct ui_file *stream)
  316. {
  317. return new cli_ui_out (stream, ui_source_list);
  318. }
  319. ui_file *
  320. cli_ui_out::set_stream (struct ui_file *stream)
  321. {
  322. ui_file *old;
  323. old = m_streams.back ();
  324. m_streams.back () = stream;
  325. return old;
  326. }
  327. bool
  328. cli_ui_out::can_emit_style_escape () const
  329. {
  330. return m_streams.back ()->can_emit_style_escape ();
  331. }
  332. /* CLI interface to display tab-completion matches. */
  333. /* CLI version of displayer.crlf. */
  334. static void
  335. cli_mld_crlf (const struct match_list_displayer *displayer)
  336. {
  337. rl_crlf ();
  338. }
  339. /* CLI version of displayer.putch. */
  340. static void
  341. cli_mld_putch (const struct match_list_displayer *displayer, int ch)
  342. {
  343. putc (ch, rl_outstream);
  344. }
  345. /* CLI version of displayer.puts. */
  346. static void
  347. cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
  348. {
  349. fputs (s, rl_outstream);
  350. }
  351. /* CLI version of displayer.flush. */
  352. static void
  353. cli_mld_flush (const struct match_list_displayer *displayer)
  354. {
  355. fflush (rl_outstream);
  356. }
  357. EXTERN_C void _rl_erase_entire_line (void);
  358. /* CLI version of displayer.erase_entire_line. */
  359. static void
  360. cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
  361. {
  362. _rl_erase_entire_line ();
  363. }
  364. /* CLI version of displayer.beep. */
  365. static void
  366. cli_mld_beep (const struct match_list_displayer *displayer)
  367. {
  368. rl_ding ();
  369. }
  370. /* CLI version of displayer.read_key. */
  371. static int
  372. cli_mld_read_key (const struct match_list_displayer *displayer)
  373. {
  374. return rl_read_key ();
  375. }
  376. /* CLI version of rl_completion_display_matches_hook.
  377. See gdb_display_match_list for a description of the arguments. */
  378. void
  379. cli_display_match_list (char **matches, int len, int max)
  380. {
  381. struct match_list_displayer displayer;
  382. rl_get_screen_size (&displayer.height, &displayer.width);
  383. displayer.crlf = cli_mld_crlf;
  384. displayer.putch = cli_mld_putch;
  385. displayer.puts = cli_mld_puts;
  386. displayer.flush = cli_mld_flush;
  387. displayer.erase_entire_line = cli_mld_erase_entire_line;
  388. displayer.beep = cli_mld_beep;
  389. displayer.read_key = cli_mld_read_key;
  390. gdb_display_match_list (matches, len, max, &displayer);
  391. rl_forced_update_display ();
  392. }