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+
24 #include <epan/print_stream.h>
28 #include <wsutil/file_util.h>
30 #define TERM_SGR_RESET "\x1B[0m" /* SGR - reset */
31 #define TERM_CSI_EL "\x1B[K" /* EL - Erase in Line (to end of line) */
39 print_color_escape(FILE *fh, const color_t *fg, const color_t *bg)
42 /* default to white foreground, black background */
43 WORD win_fg_color = FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN;
44 WORD win_bg_color = 0;
46 /* The classic Windows Console offers 1-bit color, so you can't set
47 * the red, green, or blue intensities, you can only set
48 * "{foreground, background} contains {red, green, blue}". So
49 * include red, green or blue if the numeric intensity is high
52 * The console in Windows 10 builds 14931 and later supports SGR RGB
55 * https://blogs.msdn.microsoft.com/commandline/2016/09/22/24-bit-color-in-the-windows-console/
57 * We might want to print those instead depending on the version of
58 * Windows or just remove the SetConsoleTextAttribute calls and only
59 * print SGR sequences.
62 if (((fg->red >> 8) & 0xff) >= 0x80)
64 win_fg_color |= FOREGROUND_RED;
68 win_fg_color &= (~FOREGROUND_RED);
70 if (((fg->green >> 8) & 0xff) >= 0x80)
72 win_fg_color |= FOREGROUND_GREEN;
76 win_fg_color &= (~FOREGROUND_GREEN);
78 if (((fg->blue >> 8) & 0xff) >= 0x80)
80 win_fg_color |= FOREGROUND_BLUE;
84 win_fg_color &= (~FOREGROUND_BLUE);
89 if (((bg->red >> 8) & 0xff) >= 0x80)
91 win_bg_color |= BACKGROUND_RED;
95 win_bg_color &= (~BACKGROUND_RED);
97 if (((bg->green >> 8) & 0xff) >= 0x80)
99 win_bg_color |= BACKGROUND_GREEN;
103 win_bg_color &= (~BACKGROUND_GREEN);
105 if (((bg->blue >> 8) & 0xff) >= 0x80)
107 win_bg_color |= BACKGROUND_BLUE;
111 win_bg_color &= (~BACKGROUND_BLUE);
115 SetConsoleTextAttribute((HANDLE)_get_osfhandle(_fileno(fh)), win_fg_color|win_bg_color);
120 * Use the "select character foreground colour" and "select character
121 * background colour" options to the Select Graphic Rendition control
122 * sequence; those are reserved in ECMA-48, and are specified in ISO
123 * standard 8613-6/ITU-T Recommendation T.416, "Open Document Architecture
124 * (ODA) and Interchange Format: Chararcter Content Architectures",
125 * section 13.1.8 "Select Graphic Rendition (SGR)". We use the
126 * "direct colour in RGB space" option, with a parameter value of 2.
128 * Those sequences are supported by some UN*X terminal emulators; some
129 * support either : or ; as a separator, others require a ;.
131 * For more than you ever wanted to know about all of this, see
133 * https://gist.github.com/XVilka/8346728
135 * including the discussion following it.
137 * XXX - this isn't always treated correctly; macOS Terminal currently
138 * doesn't handle this correctly - it gives weird colors. Sadly, as
139 * per various other discussions mentioned in the discussion cited above,
140 * there's nothing in terminfo to indicate the presence of 24-bit color
141 * support, so there's no good way to decide whether to use this or not.
143 * XXX - fall back on 8-color or 256-color support if we can somehow
144 * determine that 24-bit color support isn't available but 8-color or
145 * 256-color support is?
148 fprintf(fh, "\x1B[38;2;%u;%u;%um",
149 (fg->red >> 8) & 0xff,
150 (fg->green >> 8) & 0xff,
151 (fg->blue >> 8) & 0xff);
155 fprintf(fh, "\x1B[48;2;%u;%u;%um",
156 (bg->red >> 8) & 0xff,
157 (bg->green >> 8) & 0xff,
158 (bg->blue >> 8) & 0xff);
164 print_color_eol(print_stream_t *self)
166 output_text *output = (output_text *)self->data;
167 FILE *fh = output->fh;
169 SetConsoleTextAttribute((HANDLE)_get_osfhandle(_fileno(fh)), self->csb_attrs);
174 * Emit CSI EL to extend current background color all the way to EOL,
175 * otherwise we get a ragged right edge of color wherever the newline
176 * occurs. It's not perfect in every terminal emulator, but it generally
179 fprintf(fh, "%s\n%s", TERM_CSI_EL, TERM_SGR_RESET);
184 open_print_dest(gboolean to_file, const char *dest)
188 /* Open the file or command for output */
190 fh = ws_fopen(dest, "w");
192 fh = popen(dest, "w");
198 close_print_dest(gboolean to_file, FILE *fh)
200 /* Close the file or command */
202 return (fclose(fh) == 0);
204 return (pclose(fh) == 0);
207 /* Some formats need stuff at the beginning of the output */
209 print_preamble(print_stream_t *self, gchar *filename, const char *version_string)
211 return self->ops->print_preamble ? (self->ops->print_preamble)(self, filename, version_string) : TRUE;
215 print_line(print_stream_t *self, int indent, const char *line)
217 return (self->ops->print_line)(self, indent, line);
221 print_line_color(print_stream_t *self, int indent, const char *line, const color_t *fg, const color_t *bg)
223 if (self->ops->print_line_color)
224 return (self->ops->print_line_color)(self, indent, line, fg, bg);
226 return (self->ops->print_line)(self, indent, line);
229 /* Insert bookmark */
231 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
233 return self->ops->print_bookmark ? (self->ops->print_bookmark)(self, name, title) : TRUE;
237 new_page(print_stream_t *self)
239 return self->ops->new_page ? (self->ops->new_page)(self) : TRUE;
242 /* Some formats need stuff at the end of the output */
244 print_finale(print_stream_t *self)
246 return self->ops->print_finale ? (self->ops->print_finale)(self) : TRUE;
250 destroy_print_stream(print_stream_t *self)
252 return (self && self->ops && self->ops->destroy) ? (self->ops->destroy)(self) : TRUE;
255 #define MAX_INDENT 160
257 /* returns TRUE if the print succeeded, FALSE if there was an error */
259 print_line_color_text(print_stream_t *self, int indent, const char *line, const color_t *fg, const color_t *bg)
261 static char spaces[MAX_INDENT];
263 output_text *output = (output_text *)self->data;
264 unsigned int num_spaces;
265 gboolean emit_color = self->isatty && (fg != NULL || bg != NULL);
267 /* should be space, if NUL -> initialize */
269 memset(spaces, ' ', sizeof(spaces));
272 print_color_escape(output->fh, fg, bg);
273 if (ferror(output->fh))
277 /* Prepare the tabs for printing, depending on tree level */
278 num_spaces = indent * 4;
279 if (num_spaces > MAX_INDENT)
280 num_spaces = MAX_INDENT;
282 ret = fwrite(spaces, 1, num_spaces, output->fh);
283 if (ret == num_spaces) {
284 gchar *tty_out = NULL;
286 if (self->isatty && self->to_codeset) {
287 /* XXX Allocating a fresh buffer every line probably isn't the
288 * most efficient way to do this. However, this has the side
289 * effect of scrubbing invalid output.
291 tty_out = g_convert_with_fallback(line, -1, self->to_codeset, "UTF-8", "?", NULL, NULL, NULL);
296 DWORD out_len = (DWORD) wcslen((wchar_t *) tty_out);
297 WriteConsoleW((HANDLE)_get_osfhandle(_fileno(output->fh)), tty_out, out_len, &out_len, NULL);
299 fputs(tty_out, output->fh);
303 fputs(line, output->fh);
307 print_color_eol(self);
309 putc('\n', output->fh);
312 return !ferror(output->fh);
316 print_line_text(print_stream_t *self, int indent, const char *line)
318 return print_line_color_text(self, indent, line, NULL, NULL);
322 new_page_text(print_stream_t *self)
324 output_text *output = (output_text *)self->data;
326 fputs("\f", output->fh);
327 return !ferror(output->fh);
331 destroy_text(print_stream_t *self)
333 output_text *output = (output_text *)self->data;
336 ret = close_print_dest(output->to_file, output->fh);
342 static const print_stream_ops_t print_text_ops = {
349 print_line_color_text,
352 static print_stream_t *
353 print_stream_text_alloc(gboolean to_file, FILE *fh)
355 print_stream_t *stream;
358 const gchar *charset;
362 output = (output_text *)g_malloc(sizeof *output);
363 output->to_file = to_file;
365 stream = (print_stream_t *)g_malloc0(sizeof (print_stream_t));
366 stream->ops = &print_text_ops;
367 stream->isatty = ws_isatty(ws_fileno(fh));
368 stream->data = output;
371 /* Is there a more reliable way to do this? */
372 is_utf8 = g_get_charset(&charset);
374 stream->to_codeset = charset;
377 CONSOLE_SCREEN_BUFFER_INFO csb_info;
378 GetConsoleScreenBufferInfo((HANDLE)_get_osfhandle(_fileno(fh)), &csb_info);
379 stream->csb_attrs = csb_info.wAttributes;
381 stream->to_codeset = "UTF-16LE";
388 print_stream_text_new(gboolean to_file, const char *dest)
392 fh = open_print_dest(to_file, dest);
396 return print_stream_text_alloc(to_file, fh);
400 print_stream_text_stdio_new(FILE *fh)
402 return print_stream_text_alloc(TRUE, fh);
410 #define MAX_PS_LINE_LENGTH 256
413 void ps_clean_string(char *out, const char *in, int outbuf_size)
423 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
445 print_preamble_ps(print_stream_t *self, gchar *filename, const char *version_string)
447 output_ps *output = (output_ps *)self->data;
448 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
450 print_ps_preamble(output->fh);
452 fputs("%% the page title\n", output->fh);
453 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
454 fprintf(output->fh, "/ws_pagetitle (%s - Wireshark %s) def\n", psbuffer, version_string);
455 fputs("\n", output->fh);
456 return !ferror(output->fh);
460 print_line_ps(print_stream_t *self, int indent, const char *line)
462 output_ps *output = (output_ps *)self->data;
463 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
465 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
466 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
467 return !ferror(output->fh);
471 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
473 output_ps *output = (output_ps *)self->data;
474 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
477 * See the Adobe "pdfmark reference":
479 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
481 * The pdfmark stuff tells code that turns PostScript into PDF
482 * things that it should do.
484 * The /OUT stuff creates a bookmark that goes to the
485 * destination with "name" as the name and "title" as the title.
487 * The "/DEST" creates the destination.
489 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
490 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
492 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
494 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
496 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
497 return !ferror(output->fh);
501 new_page_ps(print_stream_t *self)
503 output_ps *output = (output_ps *)self->data;
505 fputs("formfeed\n", output->fh);
506 return !ferror(output->fh);
510 print_finale_ps(print_stream_t *self)
512 output_ps *output = (output_ps *)self->data;
514 print_ps_finale(output->fh);
515 return !ferror(output->fh);
519 destroy_ps(print_stream_t *self)
521 output_ps *output = (output_ps *)self->data;
524 ret = close_print_dest(output->to_file, output->fh);
530 static const print_stream_ops_t print_ps_ops = {
537 NULL, /* print_line_color */
540 static print_stream_t *
541 print_stream_ps_alloc(gboolean to_file, FILE *fh)
543 print_stream_t *stream;
546 output = (output_ps *)g_malloc(sizeof *output);
547 output->to_file = to_file;
549 stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
550 stream->ops = &print_ps_ops;
551 stream->data = output;
557 print_stream_ps_new(gboolean to_file, const char *dest)
561 fh = open_print_dest(to_file, dest);
565 return print_stream_ps_alloc(to_file, fh);
569 print_stream_ps_stdio_new(FILE *fh)
571 return print_stream_ps_alloc(TRUE, fh);
575 * Editor modelines - http://www.wireshark.org/tools/modelines.html
580 * indent-tabs-mode: nil
583 * vi: set shiftwidth=4 tabstop=8 expandtab:
584 * :indentSize=4:tabSize=8:noTabs=true: