Terminal.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #include "kwsysPrivate.h"
  4. #include KWSYS_HEADER(Terminal.h)
  5. /* Work-around CMake dependency scanning limitation. This must
  6. duplicate the above list of headers. */
  7. #if 0
  8. # include "Terminal.h.in"
  9. #endif
  10. /* Configure support for this platform. */
  11. #if defined(_WIN32)
  12. # define KWSYS_TERMINAL_SUPPORT_CONSOLE
  13. #endif
  14. #if !defined(_WIN32)
  15. # define KWSYS_TERMINAL_ISATTY_WORKS
  16. #endif
  17. /* Include needed system APIs. */
  18. #include <stdarg.h> /* va_list */
  19. #include <stdlib.h> /* getenv */
  20. #include <string.h> /* strcmp */
  21. #if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
  22. # include <io.h> /* _get_osfhandle */
  23. # include <windows.h> /* SetConsoleTextAttribute */
  24. #endif
  25. #if defined(KWSYS_TERMINAL_ISATTY_WORKS)
  26. # include <unistd.h> /* isatty */
  27. #else
  28. # include <sys/stat.h> /* fstat */
  29. #endif
  30. static int kwsysTerminalStreamIsVT100(FILE* stream, int default_vt100,
  31. int default_tty);
  32. static void kwsysTerminalSetVT100Color(FILE* stream, int color);
  33. #if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
  34. static HANDLE kwsysTerminalGetStreamHandle(FILE* stream);
  35. static void kwsysTerminalSetConsoleColor(HANDLE hOut,
  36. CONSOLE_SCREEN_BUFFER_INFO* hOutInfo,
  37. FILE* stream, int color);
  38. #endif
  39. void kwsysTerminal_cfprintf(int color, FILE* stream, const char* format, ...)
  40. {
  41. /* Setup the stream with the given color if possible. */
  42. int pipeIsConsole = 0;
  43. int pipeIsVT100 = 0;
  44. int default_vt100 = color & kwsysTerminal_Color_AssumeVT100;
  45. int default_tty = color & kwsysTerminal_Color_AssumeTTY;
  46. #if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
  47. CONSOLE_SCREEN_BUFFER_INFO hOutInfo;
  48. HANDLE hOut = kwsysTerminalGetStreamHandle(stream);
  49. if (GetConsoleScreenBufferInfo(hOut, &hOutInfo)) {
  50. pipeIsConsole = 1;
  51. kwsysTerminalSetConsoleColor(hOut, &hOutInfo, stream, color);
  52. }
  53. #endif
  54. if (!pipeIsConsole &&
  55. kwsysTerminalStreamIsVT100(stream, default_vt100, default_tty)) {
  56. pipeIsVT100 = 1;
  57. kwsysTerminalSetVT100Color(stream, color);
  58. }
  59. /* Format the text into the stream. */
  60. {
  61. va_list var_args;
  62. va_start(var_args, format);
  63. vfprintf(stream, format, var_args);
  64. va_end(var_args);
  65. }
  66. /* Restore the normal color state for the stream. */
  67. #if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
  68. if (pipeIsConsole) {
  69. kwsysTerminalSetConsoleColor(hOut, &hOutInfo, stream,
  70. kwsysTerminal_Color_Normal);
  71. }
  72. #endif
  73. if (pipeIsVT100) {
  74. kwsysTerminalSetVT100Color(stream, kwsysTerminal_Color_Normal);
  75. }
  76. }
  77. /* Detect cases when a stream is definitely not interactive. */
  78. #if !defined(KWSYS_TERMINAL_ISATTY_WORKS)
  79. static int kwsysTerminalStreamIsNotInteractive(FILE* stream)
  80. {
  81. /* The given stream is definitely not interactive if it is a regular
  82. file. */
  83. struct stat stream_stat;
  84. if (fstat(fileno(stream), &stream_stat) == 0) {
  85. if (stream_stat.st_mode & S_IFREG) {
  86. return 1;
  87. }
  88. }
  89. return 0;
  90. }
  91. #endif
  92. /* List of terminal names known to support VT100 color escape sequences. */
  93. static const char* kwsysTerminalVT100Names[] = { "Eterm",
  94. "alacritty",
  95. "alacritty-direct",
  96. "ansi",
  97. "color-xterm",
  98. "con132x25",
  99. "con132x30",
  100. "con132x43",
  101. "con132x60",
  102. "con80x25",
  103. "con80x28",
  104. "con80x30",
  105. "con80x43",
  106. "con80x50",
  107. "con80x60",
  108. "cons25",
  109. "console",
  110. "cygwin",
  111. "dtterm",
  112. "eterm-color",
  113. "gnome",
  114. "gnome-256color",
  115. "konsole",
  116. "konsole-256color",
  117. "kterm",
  118. "linux",
  119. "msys",
  120. "linux-c",
  121. "mach-color",
  122. "mlterm",
  123. "putty",
  124. "putty-256color",
  125. "rxvt",
  126. "rxvt-256color",
  127. "rxvt-cygwin",
  128. "rxvt-cygwin-native",
  129. "rxvt-unicode",
  130. "rxvt-unicode-256color",
  131. "screen",
  132. "screen-256color",
  133. "screen-256color-bce",
  134. "screen-bce",
  135. "screen-w",
  136. "screen.linux",
  137. "st-256color",
  138. "tmux",
  139. "tmux-256color",
  140. "vt100",
  141. "xterm",
  142. "xterm-16color",
  143. "xterm-256color",
  144. "xterm-88color",
  145. "xterm-color",
  146. "xterm-debian",
  147. "xterm-kitty",
  148. "xterm-termite",
  149. 0 };
  150. /* Detect whether a stream is displayed in a VT100-compatible terminal. */
  151. static int kwsysTerminalStreamIsVT100(FILE* stream, int default_vt100,
  152. int default_tty)
  153. {
  154. /* Force color according to http://bixense.com/clicolors/ convention. */
  155. {
  156. const char* clicolor_force = getenv("CLICOLOR_FORCE");
  157. if (clicolor_force && strcmp(clicolor_force, "0") != 0) {
  158. return 1;
  159. }
  160. }
  161. /* Disable color according to http://bixense.com/clicolors/ convention. */
  162. {
  163. const char* clicolor = getenv("CLICOLOR");
  164. if (clicolor && strcmp(clicolor, "0") == 0) {
  165. return 0;
  166. }
  167. }
  168. /* GNU make 4.1+ may tell us that its output is destined for a TTY. */
  169. {
  170. const char* termout = getenv("MAKE_TERMOUT");
  171. if (termout && *termout != '\0') {
  172. return 1;
  173. }
  174. }
  175. /* If running inside emacs the terminal is not VT100. Some emacs
  176. seem to claim the TERM is xterm even though they do not support
  177. VT100 escapes. */
  178. {
  179. const char* emacs = getenv("EMACS");
  180. if (emacs && *emacs == 't') {
  181. return 0;
  182. }
  183. }
  184. /* Check for a valid terminal. */
  185. if (!default_vt100) {
  186. const char** t = 0;
  187. const char* term = getenv("TERM");
  188. if (term) {
  189. for (t = kwsysTerminalVT100Names; *t && strcmp(term, *t) != 0; ++t) {
  190. }
  191. }
  192. if (!(t && *t)) {
  193. return 0;
  194. }
  195. }
  196. #if defined(KWSYS_TERMINAL_ISATTY_WORKS)
  197. /* Make sure the stream is a tty. */
  198. (void)default_tty;
  199. return isatty(fileno(stream)) ? 1 : 0;
  200. #else
  201. /* Check for cases in which the stream is definitely not a tty. */
  202. if (kwsysTerminalStreamIsNotInteractive(stream)) {
  203. return 0;
  204. }
  205. /* Use the provided default for whether this is a tty. */
  206. return default_tty;
  207. #endif
  208. }
  209. /* VT100 escape sequence strings. */
  210. #if defined(__MVS__)
  211. /* if building on z/OS (aka MVS), assume we are using EBCDIC */
  212. # define ESCAPE_CHAR "\47"
  213. #else
  214. # define ESCAPE_CHAR "\33"
  215. #endif
  216. #define KWSYS_TERMINAL_VT100_NORMAL ESCAPE_CHAR "[0m"
  217. #define KWSYS_TERMINAL_VT100_BOLD ESCAPE_CHAR "[1m"
  218. #define KWSYS_TERMINAL_VT100_UNDERLINE ESCAPE_CHAR "[4m"
  219. #define KWSYS_TERMINAL_VT100_BLINK ESCAPE_CHAR "[5m"
  220. #define KWSYS_TERMINAL_VT100_INVERSE ESCAPE_CHAR "[7m"
  221. #define KWSYS_TERMINAL_VT100_FOREGROUND_BLACK ESCAPE_CHAR "[30m"
  222. #define KWSYS_TERMINAL_VT100_FOREGROUND_RED ESCAPE_CHAR "[31m"
  223. #define KWSYS_TERMINAL_VT100_FOREGROUND_GREEN ESCAPE_CHAR "[32m"
  224. #define KWSYS_TERMINAL_VT100_FOREGROUND_YELLOW ESCAPE_CHAR "[33m"
  225. #define KWSYS_TERMINAL_VT100_FOREGROUND_BLUE ESCAPE_CHAR "[34m"
  226. #define KWSYS_TERMINAL_VT100_FOREGROUND_MAGENTA ESCAPE_CHAR "[35m"
  227. #define KWSYS_TERMINAL_VT100_FOREGROUND_CYAN ESCAPE_CHAR "[36m"
  228. #define KWSYS_TERMINAL_VT100_FOREGROUND_WHITE ESCAPE_CHAR "[37m"
  229. #define KWSYS_TERMINAL_VT100_BACKGROUND_BLACK ESCAPE_CHAR "[40m"
  230. #define KWSYS_TERMINAL_VT100_BACKGROUND_RED ESCAPE_CHAR "[41m"
  231. #define KWSYS_TERMINAL_VT100_BACKGROUND_GREEN ESCAPE_CHAR "[42m"
  232. #define KWSYS_TERMINAL_VT100_BACKGROUND_YELLOW ESCAPE_CHAR "[43m"
  233. #define KWSYS_TERMINAL_VT100_BACKGROUND_BLUE ESCAPE_CHAR "[44m"
  234. #define KWSYS_TERMINAL_VT100_BACKGROUND_MAGENTA ESCAPE_CHAR "[45m"
  235. #define KWSYS_TERMINAL_VT100_BACKGROUND_CYAN ESCAPE_CHAR "[46m"
  236. #define KWSYS_TERMINAL_VT100_BACKGROUND_WHITE ESCAPE_CHAR "[47m"
  237. /* Write VT100 escape sequences to the stream for the given color. */
  238. static void kwsysTerminalSetVT100Color(FILE* stream, int color)
  239. {
  240. if (color == kwsysTerminal_Color_Normal) {
  241. fprintf(stream, KWSYS_TERMINAL_VT100_NORMAL);
  242. return;
  243. }
  244. switch (color & kwsysTerminal_Color_ForegroundMask) {
  245. case kwsysTerminal_Color_Normal:
  246. fprintf(stream, KWSYS_TERMINAL_VT100_NORMAL);
  247. break;
  248. case kwsysTerminal_Color_ForegroundBlack:
  249. fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_BLACK);
  250. break;
  251. case kwsysTerminal_Color_ForegroundRed:
  252. fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_RED);
  253. break;
  254. case kwsysTerminal_Color_ForegroundGreen:
  255. fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_GREEN);
  256. break;
  257. case kwsysTerminal_Color_ForegroundYellow:
  258. fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_YELLOW);
  259. break;
  260. case kwsysTerminal_Color_ForegroundBlue:
  261. fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_BLUE);
  262. break;
  263. case kwsysTerminal_Color_ForegroundMagenta:
  264. fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_MAGENTA);
  265. break;
  266. case kwsysTerminal_Color_ForegroundCyan:
  267. fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_CYAN);
  268. break;
  269. case kwsysTerminal_Color_ForegroundWhite:
  270. fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_WHITE);
  271. break;
  272. }
  273. switch (color & kwsysTerminal_Color_BackgroundMask) {
  274. case kwsysTerminal_Color_BackgroundBlack:
  275. fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_BLACK);
  276. break;
  277. case kwsysTerminal_Color_BackgroundRed:
  278. fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_RED);
  279. break;
  280. case kwsysTerminal_Color_BackgroundGreen:
  281. fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_GREEN);
  282. break;
  283. case kwsysTerminal_Color_BackgroundYellow:
  284. fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_YELLOW);
  285. break;
  286. case kwsysTerminal_Color_BackgroundBlue:
  287. fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_BLUE);
  288. break;
  289. case kwsysTerminal_Color_BackgroundMagenta:
  290. fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_MAGENTA);
  291. break;
  292. case kwsysTerminal_Color_BackgroundCyan:
  293. fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_CYAN);
  294. break;
  295. case kwsysTerminal_Color_BackgroundWhite:
  296. fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_WHITE);
  297. break;
  298. }
  299. if (color & kwsysTerminal_Color_ForegroundBold) {
  300. fprintf(stream, KWSYS_TERMINAL_VT100_BOLD);
  301. }
  302. }
  303. #if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
  304. # define KWSYS_TERMINAL_MASK_FOREGROUND \
  305. (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | \
  306. FOREGROUND_INTENSITY)
  307. # define KWSYS_TERMINAL_MASK_BACKGROUND \
  308. (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | \
  309. BACKGROUND_INTENSITY)
  310. /* Get the Windows handle for a FILE stream. */
  311. static HANDLE kwsysTerminalGetStreamHandle(FILE* stream)
  312. {
  313. /* Get the C-library file descriptor from the stream. */
  314. int fd = fileno(stream);
  315. # if defined(__CYGWIN__)
  316. /* Cygwin seems to have an extra pipe level. If the file descriptor
  317. corresponds to stdout or stderr then obtain the matching windows
  318. handle directly. */
  319. if (fd == fileno(stdout)) {
  320. return GetStdHandle(STD_OUTPUT_HANDLE);
  321. } else if (fd == fileno(stderr)) {
  322. return GetStdHandle(STD_ERROR_HANDLE);
  323. }
  324. # endif
  325. /* Get the underlying Windows handle for the descriptor. */
  326. return (HANDLE)_get_osfhandle(fd);
  327. }
  328. /* Set color attributes in a Windows console. */
  329. static void kwsysTerminalSetConsoleColor(HANDLE hOut,
  330. CONSOLE_SCREEN_BUFFER_INFO* hOutInfo,
  331. FILE* stream, int color)
  332. {
  333. WORD attributes = 0;
  334. switch (color & kwsysTerminal_Color_ForegroundMask) {
  335. case kwsysTerminal_Color_Normal:
  336. attributes |= hOutInfo->wAttributes & KWSYS_TERMINAL_MASK_FOREGROUND;
  337. break;
  338. case kwsysTerminal_Color_ForegroundBlack:
  339. attributes |= 0;
  340. break;
  341. case kwsysTerminal_Color_ForegroundRed:
  342. attributes |= FOREGROUND_RED;
  343. break;
  344. case kwsysTerminal_Color_ForegroundGreen:
  345. attributes |= FOREGROUND_GREEN;
  346. break;
  347. case kwsysTerminal_Color_ForegroundYellow:
  348. attributes |= FOREGROUND_RED | FOREGROUND_GREEN;
  349. break;
  350. case kwsysTerminal_Color_ForegroundBlue:
  351. attributes |= FOREGROUND_BLUE;
  352. break;
  353. case kwsysTerminal_Color_ForegroundMagenta:
  354. attributes |= FOREGROUND_RED | FOREGROUND_BLUE;
  355. break;
  356. case kwsysTerminal_Color_ForegroundCyan:
  357. attributes |= FOREGROUND_BLUE | FOREGROUND_GREEN;
  358. break;
  359. case kwsysTerminal_Color_ForegroundWhite:
  360. attributes |= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
  361. break;
  362. }
  363. switch (color & kwsysTerminal_Color_BackgroundMask) {
  364. case kwsysTerminal_Color_Normal:
  365. attributes |= hOutInfo->wAttributes & KWSYS_TERMINAL_MASK_BACKGROUND;
  366. break;
  367. case kwsysTerminal_Color_BackgroundBlack:
  368. attributes |= 0;
  369. break;
  370. case kwsysTerminal_Color_BackgroundRed:
  371. attributes |= BACKGROUND_RED;
  372. break;
  373. case kwsysTerminal_Color_BackgroundGreen:
  374. attributes |= BACKGROUND_GREEN;
  375. break;
  376. case kwsysTerminal_Color_BackgroundYellow:
  377. attributes |= BACKGROUND_RED | BACKGROUND_GREEN;
  378. break;
  379. case kwsysTerminal_Color_BackgroundBlue:
  380. attributes |= BACKGROUND_BLUE;
  381. break;
  382. case kwsysTerminal_Color_BackgroundMagenta:
  383. attributes |= BACKGROUND_RED | BACKGROUND_BLUE;
  384. break;
  385. case kwsysTerminal_Color_BackgroundCyan:
  386. attributes |= BACKGROUND_BLUE | BACKGROUND_GREEN;
  387. break;
  388. case kwsysTerminal_Color_BackgroundWhite:
  389. attributes |= BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
  390. break;
  391. }
  392. if (color & kwsysTerminal_Color_ForegroundBold) {
  393. attributes |= FOREGROUND_INTENSITY;
  394. }
  395. if (color & kwsysTerminal_Color_BackgroundBold) {
  396. attributes |= BACKGROUND_INTENSITY;
  397. }
  398. fflush(stream);
  399. SetConsoleTextAttribute(hOut, attributes);
  400. }
  401. #endif