ui-style.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* Styling for ui_file
  2. Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "ui-style.h"
  16. #include "gdbsupport/gdb_regex.h"
  17. /* A regular expression that is used for matching ANSI terminal escape
  18. sequences. */
  19. static const char ansi_regex_text[] =
  20. /* Introduction. */
  21. "^\033\\["
  22. #define DATA_SUBEXP 1
  23. /* Capture parameter and intermediate bytes. */
  24. "("
  25. /* Parameter bytes. */
  26. "[\x30-\x3f]*"
  27. /* Intermediate bytes. */
  28. "[\x20-\x2f]*"
  29. /* End the first capture. */
  30. ")"
  31. /* The final byte. */
  32. #define FINAL_SUBEXP 2
  33. "([\x40-\x7e])";
  34. /* The number of subexpressions to allocate space for, including the
  35. "0th" whole match subexpression. */
  36. #define NUM_SUBEXPRESSIONS 3
  37. /* The compiled form of ansi_regex_text. */
  38. static regex_t ansi_regex;
  39. /* This maps bright colors to RGB triples. The index is the bright
  40. color index, starting with bright black. The values come from
  41. xterm. */
  42. static const uint8_t bright_colors[][3] = {
  43. { 127, 127, 127 }, /* Black. */
  44. { 255, 0, 0 }, /* Red. */
  45. { 0, 255, 0 }, /* Green. */
  46. { 255, 255, 0 }, /* Yellow. */
  47. { 92, 92, 255 }, /* Blue. */
  48. { 255, 0, 255 }, /* Magenta. */
  49. { 0, 255, 255 }, /* Cyan. */
  50. { 255, 255, 255 } /* White. */
  51. };
  52. /* See ui-style.h. */
  53. bool
  54. ui_file_style::color::append_ansi (bool is_fg, std::string *str) const
  55. {
  56. if (m_simple)
  57. {
  58. if (m_value >= BLACK && m_value <= WHITE)
  59. str->append (std::to_string (m_value + (is_fg ? 30 : 40)));
  60. else if (m_value > WHITE && m_value <= WHITE + 8)
  61. str->append (std::to_string (m_value - WHITE + (is_fg ? 90 : 100)));
  62. else if (m_value != -1)
  63. {
  64. str->append (is_fg ? "38;5;" : "48;5;");
  65. str->append (std::to_string (m_value));
  66. }
  67. else
  68. return false;
  69. }
  70. else
  71. {
  72. str->append (is_fg ? "38;2;" : "48;2;");
  73. str->append (std::to_string (m_red)
  74. + ";" + std::to_string (m_green)
  75. + ";" + std::to_string (m_blue));
  76. }
  77. return true;
  78. }
  79. /* See ui-style.h. */
  80. void
  81. ui_file_style::color::get_rgb (uint8_t *rgb) const
  82. {
  83. if (m_simple)
  84. {
  85. /* Can't call this for a basic color or NONE -- those will end
  86. up in the assert below. */
  87. if (m_value >= 8 && m_value <= 15)
  88. memcpy (rgb, bright_colors[m_value - 8], 3 * sizeof (uint8_t));
  89. else if (m_value >= 16 && m_value <= 231)
  90. {
  91. int value = m_value;
  92. value -= 16;
  93. /* This obscure formula seems to be what terminals actually
  94. do. */
  95. int component = value / 36;
  96. rgb[0] = component == 0 ? 0 : (55 + component * 40);
  97. value %= 36;
  98. component = value / 6;
  99. rgb[1] = component == 0 ? 0 : (55 + component * 40);
  100. value %= 6;
  101. rgb[2] = value == 0 ? 0 : (55 + value * 40);
  102. }
  103. else if (m_value >= 232)
  104. {
  105. uint8_t v = (m_value - 232) * 10 + 8;
  106. rgb[0] = v;
  107. rgb[1] = v;
  108. rgb[2] = v;
  109. }
  110. else
  111. gdb_assert_not_reached ("get_rgb called on invalid color");
  112. }
  113. else
  114. {
  115. rgb[0] = m_red;
  116. rgb[1] = m_green;
  117. rgb[2] = m_blue;
  118. }
  119. }
  120. /* See ui-style.h. */
  121. std::string
  122. ui_file_style::to_ansi () const
  123. {
  124. std::string result ("\033[");
  125. bool need_semi = m_foreground.append_ansi (true, &result);
  126. if (!m_background.is_none ())
  127. {
  128. if (need_semi)
  129. result.push_back (';');
  130. m_background.append_ansi (false, &result);
  131. need_semi = true;
  132. }
  133. if (m_intensity != NORMAL)
  134. {
  135. if (need_semi)
  136. result.push_back (';');
  137. result.append (std::to_string (m_intensity));
  138. need_semi = true;
  139. }
  140. if (m_reverse)
  141. {
  142. if (need_semi)
  143. result.push_back (';');
  144. result.push_back ('7');
  145. }
  146. result.push_back ('m');
  147. return result;
  148. }
  149. /* Read a ";" and a number from STRING. Return the number of
  150. characters read and put the number into *NUM. */
  151. static bool
  152. read_semi_number (const char *string, regoff_t *idx, long *num)
  153. {
  154. if (string[*idx] != ';')
  155. return false;
  156. ++*idx;
  157. if (string[*idx] < '0' || string[*idx] > '9')
  158. return false;
  159. char *tail;
  160. *num = strtol (string + *idx, &tail, 10);
  161. *idx = tail - string;
  162. return true;
  163. }
  164. /* A helper for ui_file_style::parse that reads an extended color
  165. sequence; that is, and 8- or 24- bit color. */
  166. static bool
  167. extended_color (const char *str, regoff_t *idx, ui_file_style::color *color)
  168. {
  169. long value;
  170. if (!read_semi_number (str, idx, &value))
  171. return false;
  172. if (value == 5)
  173. {
  174. /* 8-bit color. */
  175. if (!read_semi_number (str, idx, &value))
  176. return false;
  177. if (value >= 0 && value <= 255)
  178. *color = ui_file_style::color (value);
  179. else
  180. return false;
  181. }
  182. else if (value == 2)
  183. {
  184. /* 24-bit color. */
  185. long r, g, b;
  186. if (!read_semi_number (str, idx, &r)
  187. || r > 255
  188. || !read_semi_number (str, idx, &g)
  189. || g > 255
  190. || !read_semi_number (str, idx, &b)
  191. || b > 255)
  192. return false;
  193. *color = ui_file_style::color (r, g, b);
  194. }
  195. else
  196. {
  197. /* Unrecognized sequence. */
  198. return false;
  199. }
  200. return true;
  201. }
  202. /* See ui-style.h. */
  203. bool
  204. ui_file_style::parse (const char *buf, size_t *n_read)
  205. {
  206. regmatch_t subexps[NUM_SUBEXPRESSIONS];
  207. int match = regexec (&ansi_regex, buf, ARRAY_SIZE (subexps), subexps, 0);
  208. if (match == REG_NOMATCH)
  209. {
  210. *n_read = 0;
  211. return false;
  212. }
  213. /* Other failures mean the regexp is broken. */
  214. gdb_assert (match == 0);
  215. /* The regexp is anchored. */
  216. gdb_assert (subexps[0].rm_so == 0);
  217. /* The final character exists. */
  218. gdb_assert (subexps[FINAL_SUBEXP].rm_eo - subexps[FINAL_SUBEXP].rm_so == 1);
  219. if (buf[subexps[FINAL_SUBEXP].rm_so] != 'm')
  220. {
  221. /* We don't handle this sequence, so just drop it. */
  222. *n_read = subexps[0].rm_eo;
  223. return false;
  224. }
  225. /* Examine each setting in the match and apply it to the result.
  226. See the Select Graphic Rendition section of
  227. https://en.wikipedia.org/wiki/ANSI_escape_code. In essence each
  228. code is just a number, separated by ";"; there are some more
  229. wrinkles but we don't support them all.. */
  230. /* "\033[m" means the same thing as "\033[0m", so handle that
  231. specially here. */
  232. if (subexps[DATA_SUBEXP].rm_so == subexps[DATA_SUBEXP].rm_eo)
  233. *this = ui_file_style ();
  234. for (regoff_t i = subexps[DATA_SUBEXP].rm_so;
  235. i < subexps[DATA_SUBEXP].rm_eo;
  236. ++i)
  237. {
  238. if (buf[i] == ';')
  239. {
  240. /* Skip. */
  241. }
  242. else if (buf[i] >= '0' && buf[i] <= '9')
  243. {
  244. char *tail;
  245. long value = strtol (buf + i, &tail, 10);
  246. i = tail - buf;
  247. switch (value)
  248. {
  249. case 0:
  250. /* Reset. */
  251. *this = ui_file_style ();
  252. break;
  253. case 1:
  254. /* Bold. */
  255. m_intensity = BOLD;
  256. break;
  257. case 2:
  258. /* Dim. */
  259. m_intensity = DIM;
  260. break;
  261. case 7:
  262. /* Reverse. */
  263. m_reverse = true;
  264. break;
  265. case 21:
  266. m_intensity = NORMAL;
  267. break;
  268. case 22:
  269. /* Normal. */
  270. m_intensity = NORMAL;
  271. break;
  272. case 27:
  273. /* Inverse off. */
  274. m_reverse = false;
  275. break;
  276. case 30:
  277. case 31:
  278. case 32:
  279. case 33:
  280. case 34:
  281. case 35:
  282. case 36:
  283. case 37:
  284. /* Note: not 38. */
  285. case 39:
  286. m_foreground = color (value - 30);
  287. break;
  288. case 40:
  289. case 41:
  290. case 42:
  291. case 43:
  292. case 44:
  293. case 45:
  294. case 46:
  295. case 47:
  296. /* Note: not 48. */
  297. case 49:
  298. m_background = color (value - 40);
  299. break;
  300. case 90:
  301. case 91:
  302. case 92:
  303. case 93:
  304. case 94:
  305. case 95:
  306. case 96:
  307. case 97:
  308. m_foreground = color (value - 90 + 8);
  309. break;
  310. case 100:
  311. case 101:
  312. case 102:
  313. case 103:
  314. case 104:
  315. case 105:
  316. case 106:
  317. case 107:
  318. m_background = color (value - 100 + 8);
  319. break;
  320. case 38:
  321. /* If we can't parse the extended color, fail. */
  322. if (!extended_color (buf, &i, &m_foreground))
  323. {
  324. *n_read = subexps[0].rm_eo;
  325. return false;
  326. }
  327. break;
  328. case 48:
  329. /* If we can't parse the extended color, fail. */
  330. if (!extended_color (buf, &i, &m_background))
  331. {
  332. *n_read = subexps[0].rm_eo;
  333. return false;
  334. }
  335. break;
  336. default:
  337. /* Ignore everything else. */
  338. break;
  339. }
  340. }
  341. else
  342. {
  343. /* Unknown, let's just ignore. */
  344. }
  345. }
  346. *n_read = subexps[0].rm_eo;
  347. return true;
  348. }
  349. /* See ui-style.h. */
  350. bool
  351. skip_ansi_escape (const char *buf, int *n_read)
  352. {
  353. regmatch_t subexps[NUM_SUBEXPRESSIONS];
  354. int match = regexec (&ansi_regex, buf, ARRAY_SIZE (subexps), subexps, 0);
  355. if (match == REG_NOMATCH || buf[subexps[FINAL_SUBEXP].rm_so] != 'm')
  356. return false;
  357. *n_read = subexps[FINAL_SUBEXP].rm_eo;
  358. return true;
  359. }
  360. void _initialize_ui_style ();
  361. void
  362. _initialize_ui_style ()
  363. {
  364. int code = regcomp (&ansi_regex, ansi_regex_text, REG_EXTENDED);
  365. /* If the regular expression was incorrect, it was a programming
  366. error. */
  367. gdb_assert (code == 0);
  368. }