Terminal.c 15.6 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
3 4 5 6 7 8
#include "kwsysPrivate.h"
#include KWSYS_HEADER(Terminal.h)

/* Work-around CMake dependency scanning limitation.  This must
   duplicate the above list of headers.  */
#if 0
9
#include "Terminal.h.in"
10 11 12 13 14
#endif

/*--------------------------------------------------------------------------*/
/* Configure support for this platform.  */
#if defined(_WIN32) || defined(__CYGWIN__)
15
#define KWSYS_TERMINAL_SUPPORT_CONSOLE
16 17
#endif
#if !defined(_WIN32)
18
#define KWSYS_TERMINAL_ISATTY_WORKS
19 20 21 22 23
#endif

/*--------------------------------------------------------------------------*/
/* Include needed system APIs.  */

24
#include <stdarg.h> /* va_list */
25
#include <stdlib.h> /* getenv */
26
#include <string.h> /* strcmp */
27 28

#if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
29 30
#include <io.h>      /* _get_osfhandle */
#include <windows.h> /* SetConsoleTextAttribute */
31 32 33
#endif

#if defined(KWSYS_TERMINAL_ISATTY_WORKS)
34
#include <unistd.h> /* isatty */
35
#else
36
#include <sys/stat.h> /* fstat */
37 38 39 40 41 42 43 44 45 46
#endif

/*--------------------------------------------------------------------------*/
static int kwsysTerminalStreamIsVT100(FILE* stream, int default_vt100,
                                      int default_tty);
static void kwsysTerminalSetVT100Color(FILE* stream, int color);
#if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
static HANDLE kwsysTerminalGetStreamHandle(FILE* stream);
static void kwsysTerminalSetConsoleColor(HANDLE hOut,
                                         CONSOLE_SCREEN_BUFFER_INFO* hOutInfo,
47
                                         FILE* stream, int color);
48 49 50 51 52 53
#endif

/*--------------------------------------------------------------------------*/
void kwsysTerminal_cfprintf(int color, FILE* stream, const char* format, ...)
{
  /* Setup the stream with the given color if possible.  */
54 55
  int pipeIsConsole = 0;
  int pipeIsVT100 = 0;
56
  int default_vt100 = color & kwsysTerminal_Color_AssumeVT100;
57 58 59 60
  int default_tty = color & kwsysTerminal_Color_AssumeTTY;
#if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
  CONSOLE_SCREEN_BUFFER_INFO hOutInfo;
  HANDLE hOut = kwsysTerminalGetStreamHandle(stream);
61
  if (GetConsoleScreenBufferInfo(hOut, &hOutInfo)) {
62
    pipeIsConsole = 1;
63
    kwsysTerminalSetConsoleColor(hOut, &hOutInfo, stream, color);
64
  }
65
#endif
66 67
  if (!pipeIsConsole &&
      kwsysTerminalStreamIsVT100(stream, default_vt100, default_tty)) {
68
    pipeIsVT100 = 1;
69
    kwsysTerminalSetVT100Color(stream, color);
70
  }
71 72 73

  /* Format the text into the stream.  */
  {
74 75 76 77
    va_list var_args;
    va_start(var_args, format);
    vfprintf(stream, format, var_args);
    va_end(var_args);
78 79
  }

80
/* Restore the normal color state for the stream.  */
81
#if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
82
  if (pipeIsConsole) {
83 84
    kwsysTerminalSetConsoleColor(hOut, &hOutInfo, stream,
                                 kwsysTerminal_Color_Normal);
85
  }
86
#endif
87
  if (pipeIsVT100) {
88
    kwsysTerminalSetVT100Color(stream, kwsysTerminal_Color_Normal);
89
  }
90 91 92
}

/*--------------------------------------------------------------------------*/
93
/* Detect cases when a stream is definitely not interactive.  */
94 95 96
#if !defined(KWSYS_TERMINAL_ISATTY_WORKS)
static int kwsysTerminalStreamIsNotInteractive(FILE* stream)
{
97
  /* The given stream is definitely not interactive if it is a regular
98 99
     file.  */
  struct stat stream_stat;
100 101
  if (fstat(fileno(stream), &stream_stat) == 0) {
    if (stream_stat.st_mode & S_IFREG) {
102 103
      return 1;
    }
104
  }
105 106 107 108 109 110
  return 0;
}
#endif

/*--------------------------------------------------------------------------*/
/* List of terminal names known to support VT100 color escape sequences.  */
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
static const char* kwsysTerminalVT100Names[] = { "Eterm",
                                                 "ansi",
                                                 "color-xterm",
                                                 "con132x25",
                                                 "con132x30",
                                                 "con132x43",
                                                 "con132x60",
                                                 "con80x25",
                                                 "con80x28",
                                                 "con80x30",
                                                 "con80x43",
                                                 "con80x50",
                                                 "con80x60",
                                                 "cons25",
                                                 "console",
                                                 "cygwin",
                                                 "dtterm",
                                                 "eterm-color",
                                                 "gnome",
                                                 "gnome-256color",
                                                 "konsole",
                                                 "konsole-256color",
                                                 "kterm",
                                                 "linux",
                                                 "msys",
                                                 "linux-c",
                                                 "mach-color",
                                                 "mlterm",
                                                 "putty",
                                                 "putty-256color",
                                                 "rxvt",
                                                 "rxvt-256color",
                                                 "rxvt-cygwin",
                                                 "rxvt-cygwin-native",
                                                 "rxvt-unicode",
                                                 "rxvt-unicode-256color",
                                                 "screen",
                                                 "screen-256color",
                                                 "screen-256color-bce",
                                                 "screen-bce",
                                                 "screen-w",
                                                 "screen.linux",
153 154
                                                 "tmux",
                                                 "tmux-256color",
155 156 157 158 159 160 161 162 163
                                                 "vt100",
                                                 "xterm",
                                                 "xterm-16color",
                                                 "xterm-256color",
                                                 "xterm-88color",
                                                 "xterm-color",
                                                 "xterm-debian",
                                                 "xterm-termite",
                                                 0 };
164 165 166 167 168 169

/*--------------------------------------------------------------------------*/
/* Detect whether a stream is displayed in a VT100-compatible terminal.  */
static int kwsysTerminalStreamIsVT100(FILE* stream, int default_vt100,
                                      int default_tty)
{
170 171
  /* Force color according to http://bixense.com/clicolors/ convention.  */
  {
172 173 174 175
    const char* clicolor_force = getenv("CLICOLOR_FORCE");
    if (clicolor_force && *clicolor_force &&
        strcmp(clicolor_force, "0") != 0) {
      return 1;
176 177 178
    }
  }

179 180 181
  /* If running inside emacs the terminal is not VT100.  Some emacs
     seem to claim the TERM is xterm even though they do not support
     VT100 escapes.  */
182
  {
183 184 185
    const char* emacs = getenv("EMACS");
    if (emacs && *emacs == 't') {
      return 0;
186
    }
187
  }
188

189
  /* Check for a valid terminal.  */
190
  if (!default_vt100) {
191 192
    const char** t = 0;
    const char* term = getenv("TERM");
193 194
    if (term) {
      for (t = kwsysTerminalVT100Names; *t && strcmp(term, *t) != 0; ++t) {
195
      }
196 197
    }
    if (!(t && *t)) {
198 199
      return 0;
    }
200
  }
201 202 203 204

#if defined(KWSYS_TERMINAL_ISATTY_WORKS)
  /* Make sure the stream is a tty. */
  (void)default_tty;
205
  return isatty(fileno(stream)) ? 1 : 0;
206
#else
207
  /* Check for cases in which the stream is definitely not a tty.  */
208
  if (kwsysTerminalStreamIsNotInteractive(stream)) {
209
    return 0;
210
  }
211 212 213 214 215 216 217 218

  /* Use the provided default for whether this is a tty.  */
  return default_tty;
#endif
}

/*--------------------------------------------------------------------------*/
/* VT100 escape sequence strings.  */
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
#define KWSYS_TERMINAL_VT100_NORMAL "\33[0m"
#define KWSYS_TERMINAL_VT100_BOLD "\33[1m"
#define KWSYS_TERMINAL_VT100_UNDERLINE "\33[4m"
#define KWSYS_TERMINAL_VT100_BLINK "\33[5m"
#define KWSYS_TERMINAL_VT100_INVERSE "\33[7m"
#define KWSYS_TERMINAL_VT100_FOREGROUND_BLACK "\33[30m"
#define KWSYS_TERMINAL_VT100_FOREGROUND_RED "\33[31m"
#define KWSYS_TERMINAL_VT100_FOREGROUND_GREEN "\33[32m"
#define KWSYS_TERMINAL_VT100_FOREGROUND_YELLOW "\33[33m"
#define KWSYS_TERMINAL_VT100_FOREGROUND_BLUE "\33[34m"
#define KWSYS_TERMINAL_VT100_FOREGROUND_MAGENTA "\33[35m"
#define KWSYS_TERMINAL_VT100_FOREGROUND_CYAN "\33[36m"
#define KWSYS_TERMINAL_VT100_FOREGROUND_WHITE "\33[37m"
#define KWSYS_TERMINAL_VT100_BACKGROUND_BLACK "\33[40m"
#define KWSYS_TERMINAL_VT100_BACKGROUND_RED "\33[41m"
#define KWSYS_TERMINAL_VT100_BACKGROUND_GREEN "\33[42m"
#define KWSYS_TERMINAL_VT100_BACKGROUND_YELLOW "\33[43m"
#define KWSYS_TERMINAL_VT100_BACKGROUND_BLUE "\33[44m"
#define KWSYS_TERMINAL_VT100_BACKGROUND_MAGENTA "\33[45m"
#define KWSYS_TERMINAL_VT100_BACKGROUND_CYAN "\33[46m"
#define KWSYS_TERMINAL_VT100_BACKGROUND_WHITE "\33[47m"
240 241 242 243 244

/*--------------------------------------------------------------------------*/
/* Write VT100 escape sequences to the stream for the given color.  */
static void kwsysTerminalSetVT100Color(FILE* stream, int color)
{
245
  if (color == kwsysTerminal_Color_Normal) {
246 247
    fprintf(stream, KWSYS_TERMINAL_VT100_NORMAL);
    return;
248
  }
249

250
  switch (color & kwsysTerminal_Color_ForegroundMask) {
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    case kwsysTerminal_Color_Normal:
      fprintf(stream, KWSYS_TERMINAL_VT100_NORMAL);
      break;
    case kwsysTerminal_Color_ForegroundBlack:
      fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_BLACK);
      break;
    case kwsysTerminal_Color_ForegroundRed:
      fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_RED);
      break;
    case kwsysTerminal_Color_ForegroundGreen:
      fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_GREEN);
      break;
    case kwsysTerminal_Color_ForegroundYellow:
      fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_YELLOW);
      break;
    case kwsysTerminal_Color_ForegroundBlue:
      fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_BLUE);
      break;
    case kwsysTerminal_Color_ForegroundMagenta:
      fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_MAGENTA);
      break;
    case kwsysTerminal_Color_ForegroundCyan:
      fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_CYAN);
      break;
    case kwsysTerminal_Color_ForegroundWhite:
      fprintf(stream, KWSYS_TERMINAL_VT100_FOREGROUND_WHITE);
      break;
278 279
  }
  switch (color & kwsysTerminal_Color_BackgroundMask) {
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    case kwsysTerminal_Color_BackgroundBlack:
      fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_BLACK);
      break;
    case kwsysTerminal_Color_BackgroundRed:
      fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_RED);
      break;
    case kwsysTerminal_Color_BackgroundGreen:
      fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_GREEN);
      break;
    case kwsysTerminal_Color_BackgroundYellow:
      fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_YELLOW);
      break;
    case kwsysTerminal_Color_BackgroundBlue:
      fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_BLUE);
      break;
    case kwsysTerminal_Color_BackgroundMagenta:
      fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_MAGENTA);
      break;
    case kwsysTerminal_Color_BackgroundCyan:
      fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_CYAN);
      break;
    case kwsysTerminal_Color_BackgroundWhite:
      fprintf(stream, KWSYS_TERMINAL_VT100_BACKGROUND_WHITE);
      break;
304 305
  }
  if (color & kwsysTerminal_Color_ForegroundBold) {
306
    fprintf(stream, KWSYS_TERMINAL_VT100_BOLD);
307
  }
308 309 310 311 312
}

/*--------------------------------------------------------------------------*/
#if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)

313
#define KWSYS_TERMINAL_MASK_FOREGROUND                                        \
314
  (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY)
315
#define KWSYS_TERMINAL_MASK_BACKGROUND                                        \
316 317 318 319 320 321 322 323
  (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY)

/* Get the Windows handle for a FILE stream.  */
static HANDLE kwsysTerminalGetStreamHandle(FILE* stream)
{
  /* Get the C-library file descriptor from the stream.  */
  int fd = fileno(stream);

324
#if defined(__CYGWIN__)
325 326 327
  /* Cygwin seems to have an extra pipe level.  If the file descriptor
     corresponds to stdout or stderr then obtain the matching windows
     handle directly.  */
328
  if (fd == fileno(stdout)) {
329
    return GetStdHandle(STD_OUTPUT_HANDLE);
330
  } else if (fd == fileno(stderr)) {
331
    return GetStdHandle(STD_ERROR_HANDLE);
332 333
  }
#endif
334 335 336 337 338 339 340 341

  /* Get the underlying Windows handle for the descriptor.  */
  return (HANDLE)_get_osfhandle(fd);
}

/* Set color attributes in a Windows console.  */
static void kwsysTerminalSetConsoleColor(HANDLE hOut,
                                         CONSOLE_SCREEN_BUFFER_INFO* hOutInfo,
342
                                         FILE* stream, int color)
343 344
{
  WORD attributes = 0;
345
  switch (color & kwsysTerminal_Color_ForegroundMask) {
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    case kwsysTerminal_Color_Normal:
      attributes |= hOutInfo->wAttributes & KWSYS_TERMINAL_MASK_FOREGROUND;
      break;
    case kwsysTerminal_Color_ForegroundBlack:
      attributes |= 0;
      break;
    case kwsysTerminal_Color_ForegroundRed:
      attributes |= FOREGROUND_RED;
      break;
    case kwsysTerminal_Color_ForegroundGreen:
      attributes |= FOREGROUND_GREEN;
      break;
    case kwsysTerminal_Color_ForegroundYellow:
      attributes |= FOREGROUND_RED | FOREGROUND_GREEN;
      break;
    case kwsysTerminal_Color_ForegroundBlue:
      attributes |= FOREGROUND_BLUE;
      break;
    case kwsysTerminal_Color_ForegroundMagenta:
      attributes |= FOREGROUND_RED | FOREGROUND_BLUE;
      break;
    case kwsysTerminal_Color_ForegroundCyan:
      attributes |= FOREGROUND_BLUE | FOREGROUND_GREEN;
      break;
    case kwsysTerminal_Color_ForegroundWhite:
      attributes |= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
      break;
373 374
  }
  switch (color & kwsysTerminal_Color_BackgroundMask) {
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    case kwsysTerminal_Color_Normal:
      attributes |= hOutInfo->wAttributes & KWSYS_TERMINAL_MASK_BACKGROUND;
      break;
    case kwsysTerminal_Color_BackgroundBlack:
      attributes |= 0;
      break;
    case kwsysTerminal_Color_BackgroundRed:
      attributes |= BACKGROUND_RED;
      break;
    case kwsysTerminal_Color_BackgroundGreen:
      attributes |= BACKGROUND_GREEN;
      break;
    case kwsysTerminal_Color_BackgroundYellow:
      attributes |= BACKGROUND_RED | BACKGROUND_GREEN;
      break;
    case kwsysTerminal_Color_BackgroundBlue:
      attributes |= BACKGROUND_BLUE;
      break;
    case kwsysTerminal_Color_BackgroundMagenta:
      attributes |= BACKGROUND_RED | BACKGROUND_BLUE;
      break;
    case kwsysTerminal_Color_BackgroundCyan:
      attributes |= BACKGROUND_BLUE | BACKGROUND_GREEN;
      break;
    case kwsysTerminal_Color_BackgroundWhite:
      attributes |= BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
      break;
402 403
  }
  if (color & kwsysTerminal_Color_ForegroundBold) {
404
    attributes |= FOREGROUND_INTENSITY;
405 406
  }
  if (color & kwsysTerminal_Color_BackgroundBold) {
407
    attributes |= BACKGROUND_INTENSITY;
408
  }
409 410 411 412
  fflush(stream);
  SetConsoleTextAttribute(hOut, attributes);
}
#endif