2 * Routines for print streams.
4 * Gilbert Ramirez <gram@alumni.rice.edu>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
21 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
22 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
23 #endif /* ENABLE_VIRTUAL_TERMINAL_PROCESSING */
25 #include <stdlib.h> /* for getenv() */
26 #include <unistd.h> /* for isatty() */
31 #include <epan/print_stream.h>
35 #include <wsutil/file_util.h>
37 #define TERM_SGR_RESET "\x1B[0m" /* SGR - reset */
38 #define TERM_CSI_EL "\x1B[K" /* EL - Erase in Line (to end of line) */
52 const char *to_codeset
;
53 color_type_t color_type
;
62 * The classic Windows Console offers 1-bit color, so you can't set
63 * the red, green, or blue intensities, you can only set
64 * "{foreground, background} contains {red, green, blue}". So
65 * include red, green or blue if the numeric intensity is high
69 set_color_console(FILE *fh
, const color_t
*fg
, const color_t
*bg
)
71 /* default to white foreground, black background */
72 WORD win_fg_color
= FOREGROUND_RED
|FOREGROUND_BLUE
|FOREGROUND_GREEN
;
73 WORD win_bg_color
= 0;
76 if (((fg
->red
>> 8) & 0xff) >= 0x80)
78 win_fg_color
|= FOREGROUND_RED
;
82 win_fg_color
&= (~FOREGROUND_RED
);
84 if (((fg
->green
>> 8) & 0xff) >= 0x80)
86 win_fg_color
|= FOREGROUND_GREEN
;
90 win_fg_color
&= (~FOREGROUND_GREEN
);
92 if (((fg
->blue
>> 8) & 0xff) >= 0x80)
94 win_fg_color
|= FOREGROUND_BLUE
;
98 win_fg_color
&= (~FOREGROUND_BLUE
);
103 if (((bg
->red
>> 8) & 0xff) >= 0x80)
105 win_bg_color
|= BACKGROUND_RED
;
109 win_bg_color
&= (~BACKGROUND_RED
);
111 if (((bg
->green
>> 8) & 0xff) >= 0x80)
113 win_bg_color
|= BACKGROUND_GREEN
;
117 win_bg_color
&= (~BACKGROUND_GREEN
);
119 if (((bg
->blue
>> 8) & 0xff) >= 0x80)
121 win_bg_color
|= BACKGROUND_BLUE
;
125 win_bg_color
&= (~BACKGROUND_BLUE
);
129 SetConsoleTextAttribute((HANDLE
)_get_osfhandle(_fileno(fh
)), win_fg_color
|win_bg_color
);
134 * Use the SGR escape sequences to specify a 24-bit color.
137 set_color_24bit_escape(FILE *fh
, const color_t
*fg
, const color_t
*bg
)
140 * Use the "select character foreground colour" and "select character
141 * background colour" options to the Select Graphic Rendition control
142 * sequence; those are reserved in ECMA-48, and are specified in ISO
143 * standard 8613-6/ITU-T Recommendation T.416, "Open Document Architecture
144 * (ODA) and Interchange Format: Chararcter Content Architectures",
145 * section 13.1.8 "Select Graphic Rendition (SGR)". We use the
146 * "direct colour in RGB space" option, with a parameter value of 2.
148 * Those sequences are supported by some UN*X terminal emulators; some
149 * support either : or ; as a separator, others require a ;.
151 * For more than you ever wanted to know about all of this, see
153 * https://github.com/termstandard/colors
157 * https://gist.github.com/XVilka/8346728
159 * including the discussion following it, and
161 * https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
163 * They are also supported by versions of the Windows Console that
164 * allow setting the ENABLE_VIRTUAL_TERMINAL_PROCESSING mode; that
165 * mode tells the console to interpret escape sequences written
169 fprintf(fh
, "\x1B[38;2;%u;%u;%um",
170 (fg
->red
>> 8) & 0xff,
171 (fg
->green
>> 8) & 0xff,
172 (fg
->blue
>> 8) & 0xff);
176 fprintf(fh
, "\x1B[48;2;%u;%u;%um",
177 (bg
->red
>> 8) & 0xff,
178 (bg
->green
>> 8) & 0xff,
179 (bg
->blue
>> 8) & 0xff);
185 do_color_eol_console(print_stream_t
*self
)
187 output_text
*output
= (output_text
*)self
->data
;
188 FILE *fh
= output
->fh
;
190 SetConsoleTextAttribute((HANDLE
)_get_osfhandle(_fileno(fh
)), output
->csb_attrs
);
196 do_color_eol_24bit_escape(print_stream_t
*self
)
198 output_text
*output
= (output_text
*)self
->data
;
199 FILE *fh
= output
->fh
;
202 * Emit CSI EL to extend current background color all the way to EOL,
203 * otherwise we get a ragged right edge of color wherever the newline
204 * occurs. It's not perfect in every terminal emulator, but it generally
207 fprintf(fh
, "%s\n%s", TERM_CSI_EL
, TERM_SGR_RESET
);
211 open_print_dest(gboolean to_file
, const char *dest
)
215 /* Open the file or command for output */
217 fh
= ws_fopen(dest
, "w");
219 fh
= popen(dest
, "w");
225 close_print_dest(gboolean to_file
, FILE *fh
)
227 /* Close the file or command */
229 return (fclose(fh
) == 0);
231 return (pclose(fh
) == 0);
234 /* Some formats need stuff at the beginning of the output */
236 print_preamble(print_stream_t
*self
, gchar
*filename
, const char *version_string
)
238 return self
->ops
->print_preamble
? (self
->ops
->print_preamble
)(self
, filename
, version_string
) : TRUE
;
242 print_line(print_stream_t
*self
, int indent
, const char *line
)
244 return (self
->ops
->print_line
)(self
, indent
, line
);
248 print_line_color(print_stream_t
*self
, int indent
, const char *line
, const color_t
*fg
, const color_t
*bg
)
250 if (self
->ops
->print_line_color
)
251 return (self
->ops
->print_line_color
)(self
, indent
, line
, fg
, bg
);
253 return (self
->ops
->print_line
)(self
, indent
, line
);
256 /* Insert bookmark */
258 print_bookmark(print_stream_t
*self
, const gchar
*name
, const gchar
*title
)
260 return self
->ops
->print_bookmark
? (self
->ops
->print_bookmark
)(self
, name
, title
) : TRUE
;
264 new_page(print_stream_t
*self
)
266 return self
->ops
->new_page
? (self
->ops
->new_page
)(self
) : TRUE
;
269 /* Some formats need stuff at the end of the output */
271 print_finale(print_stream_t
*self
)
273 return self
->ops
->print_finale
? (self
->ops
->print_finale
)(self
) : TRUE
;
277 destroy_print_stream(print_stream_t
*self
)
279 return (self
&& self
->ops
&& self
->ops
->destroy
) ? (self
->ops
->destroy
)(self
) : TRUE
;
282 #define MAX_INDENT 160
284 /* returns TRUE if the print succeeded, FALSE if there was an error */
286 print_line_color_text(print_stream_t
*self
, int indent
, const char *line
, const color_t
*fg
, const color_t
*bg
)
288 static char spaces
[MAX_INDENT
];
290 output_text
*output
= (output_text
*)self
->data
;
291 unsigned int num_spaces
;
292 gboolean emit_color
= output
->isatty
&& (fg
!= NULL
|| bg
!= NULL
);
294 /* should be space, if NUL -> initialize */
296 memset(spaces
, ' ', sizeof(spaces
));
299 switch (output
->color_type
) {
306 set_color_console(output
->fh
, fg
, bg
);
310 case COLOR_24BIT_ESCAPE
:
311 set_color_24bit_escape(output
->fh
, fg
, bg
);
312 if (ferror(output
->fh
))
318 /* Prepare the tabs for printing, depending on tree level */
319 num_spaces
= indent
* 4;
320 if (num_spaces
> MAX_INDENT
)
321 num_spaces
= MAX_INDENT
;
323 ret
= fwrite(spaces
, 1, num_spaces
, output
->fh
);
324 if (ret
== num_spaces
) {
325 if (output
->isatty
&& output
->to_codeset
) {
326 /* XXX Allocating a fresh buffer every line probably isn't the
327 * most efficient way to do this. However, this has the side
328 * effect of scrubbing invalid output.
332 tty_out
= g_convert_with_fallback(line
, -1, output
->to_codeset
, "UTF-8", "?", NULL
, NULL
, NULL
);
337 * We mapped to little-endian UTF-16, so write to the
338 * console using the Unicode API.
340 DWORD out_len
= (DWORD
) wcslen((wchar_t *) tty_out
);
341 WriteConsoleW((HANDLE
)_get_osfhandle(_fileno(output
->fh
)), tty_out
, out_len
, &out_len
, NULL
);
343 fputs(tty_out
, output
->fh
);
347 fputs(line
, output
->fh
);
351 * Either we're not writing to a terminal/console or we are
352 * but we're just writing UTF-8 there.
354 fputs(line
, output
->fh
);
359 switch (output
->color_type
) {
362 putc('\n', output
->fh
);
367 do_color_eol_console(self
);
371 case COLOR_24BIT_ESCAPE
:
372 do_color_eol_24bit_escape(self
);
376 putc('\n', output
->fh
);
379 return !ferror(output
->fh
);
383 print_line_text(print_stream_t
*self
, int indent
, const char *line
)
385 return print_line_color_text(self
, indent
, line
, NULL
, NULL
);
389 new_page_text(print_stream_t
*self
)
391 output_text
*output
= (output_text
*)self
->data
;
393 fputs("\f", output
->fh
);
394 return !ferror(output
->fh
);
398 destroy_text(print_stream_t
*self
)
400 output_text
*output
= (output_text
*)self
->data
;
403 switch (output
->color_type
) {
410 /* Restore the default text attribute. */
411 SetConsoleTextAttribute((HANDLE
)_get_osfhandle(_fileno(output
->fh
)), output
->csb_attrs
);
415 case COLOR_24BIT_ESCAPE
:
416 /* Reset the color to the default */
417 fprintf(output
->fh
, "%s", TERM_SGR_RESET
);
422 * Restore the console mode before we changed it.
423 * We must do that *after* sending escape sequences,
424 * as this may disable escape sequence processing.
426 SetConsoleMode((HANDLE
)_get_osfhandle(_fileno(output
->fh
)), output
->console_mode
);
431 ret
= close_print_dest(output
->to_file
, output
->fh
);
437 static const print_stream_ops_t print_text_ops
= {
444 print_line_color_text
,
447 static print_stream_t
*
448 print_stream_text_alloc(gboolean to_file
, FILE *fh
)
450 print_stream_t
*stream
;
453 output
= (output_text
*)g_malloc(sizeof *output
);
454 output
->to_file
= to_file
;
459 * On Windows, "_isatty()", which is what ws_isatty() wraps,
460 * "determines whether fd is associated with a character device
461 * (a terminal, console, printer, or serial port)".
463 * We specifically want to know if it's assciated with a *console*,
464 * as, if it is, we'll be using console-specific APIs.
466 CONSOLE_SCREEN_BUFFER_INFO csb_info
;
469 if (GetConsoleScreenBufferInfo((HANDLE
)_get_osfhandle(_fileno(fh
)),
472 * The console-specific API GetConsoleScreenBufferInfo() succeeded,
473 * so we'll assume this is a console.
475 output
->isatty
= TRUE
;
476 output
->csb_attrs
= csb_info
.wAttributes
;
479 * Map to little-endian UTF-16; we'll be doing Unicode-API
480 * writes to the console, and that expects the standard flavor
481 * of Unicode on Windows, which is little-endian UTF-16.
483 output
->to_codeset
= "UTF-16LE";
486 * As indicated above, the classic Windows Console only offers
487 * 1-bit color, set through special console APIs.
489 * The console in Windows 10 version 1511 (TH2), build 10586, and
490 * later supports SGR escape sequences:
492 * http://www.nivot.org/blog/post/2016/02/04/Windows-10-TH2-(v1511)-Console-Host-Enhancements
494 * but only supports 16 colors. The "undocumented" 0x04 bit to
495 * which they refer is documented in the current version of the
496 * SetConsoleMode() documentation:
498 * https://docs.microsoft.com/en-us/windows/console/setconsolemode
500 * as ENABLE_VIRTUAL_TERMINAL_PROCESSING, saying
502 * When writing with WriteFile or WriteConsole, characters are
503 * parsed for VT100 and similar control character sequences that
504 * control cursor movement, color/font mode, and other operations
505 * that can also be performed via the existing Console APIs. For
506 * more information, see Console Virtual Terminal Sequences.
508 * Console Virtual Terminal Sequences:
510 * https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
512 * documents all the escape sequences the Console supports. It
513 * currently seems to indicate that the ODA versions with 24-bit
514 * color are supported but select the closest color from the
517 * The console in Windows 10 builds 14931 (a preview version of
518 * Windows 10 version 1703) and later supports SGR RGB sequences:
520 * https://devblogs.microsoft.com/commandline/24-bit-color-in-the-windows-console/
524 * Thanks to our ability to run Linux apps and scripts using our
525 * new Bash on Ubuntu on Windows environment atop the Windows
526 * Subsystem for Linux (WSL), we can use some Linux scripts and
527 * tools to demonstrate the Console's new 24-bit color support:
529 * which suggests that, with that version, whatever escape sequences
530 * work on UN*Xes also work on Windows, so maybe they support full
531 * 24-bit color with the ODA sequences.
533 * So, if ENABLE_VIRTUAL_TERMINAL_PROCESSING is already set on
534 * the console, or if it isn't but we can set it, we use the SGR
535 * sequences to set colors, otherwise, we just use the
536 * SetConsoleTextAttribute calls.
538 GetConsoleMode((HANDLE
)_get_osfhandle(_fileno(fh
)), &output
->console_mode
);
539 if (output
->console_mode
& ENABLE_VIRTUAL_TERMINAL_PROCESSING
) {
541 * It's already enabled; assume that means we can use the
542 * 24-bit color escape sequences (although the console might
543 * not support full 24-bit color, and would map the 24-bit
544 * color to the closest color in a smaller palette).
546 output
->color_type
= COLOR_24BIT_ESCAPE
;
549 * See if we can enable it.
551 console_mode
= output
->console_mode
| ENABLE_VIRTUAL_TERMINAL_PROCESSING
;
552 if (!SetConsoleMode((HANDLE
)_get_osfhandle(_fileno(fh
)), console_mode
)) {
554 * We can't - use console-mode color.
556 * It's not documented which error is returned if
557 * you try to set a mode bit that's not supported,
558 * but, at least on Windows 7, ERROR_INVALID_PARAMETER
559 * is returned if you try to set
560 * ENABLE_VIRTUAL_TERMINAL_PROCESSING.
562 * We could check for that error and report other
563 * errors as failures.
565 output
->color_type
= COLOR_CONSOLE
;
567 /* We can - use 24-bit color */
568 output
->color_type
= COLOR_24BIT_ESCAPE
;
573 * GetConsoleScreenBufferInfo() failed; it's not documented
574 * whether a particular error means "not a console", so we'll
575 * just assume this means it's not a console.
577 * The error we see on Windows 7 is ERROR_INVALID_HANDLE, but
578 * "invalid" is vague enough that I'm not sure we should
579 * treat that as meaning "not a console and everything else
580 * as being an error that we should report.
582 output
->isatty
= FALSE
;
585 * This is not used if we're not on a console, as we're not doing
588 output
->csb_attrs
= 0;
592 * On UN*X, isatty() tests "whether fildes, an open file descriptor,
593 * is associated with a terminal device", to quote the Single UNIX
594 * Specification, and the documentation for UN*Xes that haven't
595 * been tested against the SUS validation suite say similar things.
596 * It does *not* just test whether it's associated with a character
597 * device that may or may not be a terminal device, so it's what we
600 output
->isatty
= isatty(ws_fileno(fh
));
601 if (output
->isatty
) {
602 const gchar
*charset
;
605 /* Is there a more reliable way to do this? */
606 is_utf8
= g_get_charset(&charset
);
609 * The local character set isn't UTF-8, so arrange to
610 * map from UTF-8 to that character set before printing
613 output
->to_codeset
= charset
;
616 * The local character set is UTF-8, so no mapping is
619 output
->to_codeset
= NULL
;
623 * Not all UN*X terminal emulators support the 24-bit color SGR
624 * sequences (for example, macOS Terminal currently doesn't).
628 * https://github.com/termstandard/colors
630 * terminfo currently doesn't have a flag to indicate 24-bit
631 * color support - a future release will - so we can't use
632 * that to determine if the terminal emulator (or terminal)
635 * That page notes that some terminal emulators set the
636 * COLORTERM environment variable either to "truecolor"
637 * or "24bit" if 24-bit color is supported; we use that
640 * XXX - if there are terminal emulators that use the 24-bit
641 * color escape sequences but don't set COLORTERM, add code
642 * here to look at other environment variables to try to
645 * XXX - fall back on 8-color or 256-color support if we can
646 * somehow determine that 24-bit color support isn't available
647 * but 8-color or 256-color support is?
649 char *colorterm
= getenv("COLORTERM");
650 if (colorterm
!= NULL
&&
651 (strcmp(colorterm
, "truecolor") == 0 || strcmp(colorterm
, "24bit") == 0))
652 output
->color_type
= COLOR_24BIT_ESCAPE
;
654 output
->color_type
= COLOR_NONE
;
657 if (!output
->isatty
) {
659 * OK, this was determined *not* to be a terminal, so we won't
660 * be doing coloring or mapping from UTF-8 to a local character
663 output
->to_codeset
= NULL
;
664 output
->color_type
= COLOR_NONE
;
667 stream
= g_new(print_stream_t
, 1);
668 stream
->ops
= &print_text_ops
;
669 stream
->data
= output
;
675 print_stream_text_new(gboolean to_file
, const char *dest
)
679 fh
= open_print_dest(to_file
, dest
);
683 return print_stream_text_alloc(to_file
, fh
);
687 print_stream_text_stdio_new(FILE *fh
)
689 return print_stream_text_alloc(TRUE
, fh
);
697 #define MAX_PS_LINE_LENGTH 256
700 void ps_clean_string(char *out
, const char *in
, int outbuf_size
)
710 for (rd
= 0, wr
= 0 ; wr
< outbuf_size
; rd
++, wr
++ ) {
732 print_preamble_ps(print_stream_t
*self
, gchar
*filename
, const char *version_string
)
734 output_ps
*output
= (output_ps
*)self
->data
;
735 char psbuffer
[MAX_PS_LINE_LENGTH
]; /* static sized buffer! */
737 print_ps_preamble(output
->fh
);
739 fputs("%% the page title\n", output
->fh
);
740 ps_clean_string(psbuffer
, filename
, MAX_PS_LINE_LENGTH
);
741 fprintf(output
->fh
, "/ws_pagetitle (%s - Wireshark %s) def\n", psbuffer
, version_string
);
742 fputs("\n", output
->fh
);
743 return !ferror(output
->fh
);
747 print_line_ps(print_stream_t
*self
, int indent
, const char *line
)
749 output_ps
*output
= (output_ps
*)self
->data
;
750 char psbuffer
[MAX_PS_LINE_LENGTH
]; /* static sized buffer! */
752 ps_clean_string(psbuffer
, line
, MAX_PS_LINE_LENGTH
);
753 fprintf(output
->fh
, "%d (%s) putline\n", indent
, psbuffer
);
754 return !ferror(output
->fh
);
758 print_bookmark_ps(print_stream_t
*self
, const gchar
*name
, const gchar
*title
)
760 output_ps
*output
= (output_ps
*)self
->data
;
761 char psbuffer
[MAX_PS_LINE_LENGTH
]; /* static sized buffer! */
764 * See the Adobe "pdfmark reference":
766 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
768 * The pdfmark stuff tells code that turns PostScript into PDF
769 * things that it should do.
771 * The /OUT stuff creates a bookmark that goes to the
772 * destination with "name" as the name and "title" as the title.
774 * The "/DEST" creates the destination.
776 ps_clean_string(psbuffer
, title
, MAX_PS_LINE_LENGTH
);
777 fprintf(output
->fh
, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name
,
779 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
781 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
783 fprintf(output
->fh
, "/Dest /%s /DEST pdfmark\n", name
);
784 return !ferror(output
->fh
);
788 new_page_ps(print_stream_t
*self
)
790 output_ps
*output
= (output_ps
*)self
->data
;
792 fputs("formfeed\n", output
->fh
);
793 return !ferror(output
->fh
);
797 print_finale_ps(print_stream_t
*self
)
799 output_ps
*output
= (output_ps
*)self
->data
;
801 print_ps_finale(output
->fh
);
802 return !ferror(output
->fh
);
806 destroy_ps(print_stream_t
*self
)
808 output_ps
*output
= (output_ps
*)self
->data
;
811 ret
= close_print_dest(output
->to_file
, output
->fh
);
817 static const print_stream_ops_t print_ps_ops
= {
824 NULL
, /* print_line_color */
827 static print_stream_t
*
828 print_stream_ps_alloc(gboolean to_file
, FILE *fh
)
830 print_stream_t
*stream
;
833 output
= (output_ps
*)g_malloc(sizeof *output
);
834 output
->to_file
= to_file
;
837 stream
= g_new(print_stream_t
, 1);
838 stream
->ops
= &print_ps_ops
;
839 stream
->data
= output
;
845 print_stream_ps_new(gboolean to_file
, const char *dest
)
849 fh
= open_print_dest(to_file
, dest
);
853 return print_stream_ps_alloc(to_file
, fh
);
857 print_stream_ps_stdio_new(FILE *fh
)
859 return print_stream_ps_alloc(TRUE
, fh
);
863 * Editor modelines - https://www.wireshark.org/tools/modelines.html
868 * indent-tabs-mode: nil
871 * vi: set shiftwidth=4 tabstop=8 expandtab:
872 * :indentSize=4:tabSize=8:noTabs=true: