From patchwork Sun Mar 3 15:42:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eli Zaretskii X-Patchwork-Id: 31706 Received: (qmail 127561 invoked by alias); 3 Mar 2019 15:42:47 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 127547 invoked by uid 89); 3 Mar 2019 15:42:47 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-9.2 required=5.0 tests=BAYES_50, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.2 spammy=gray, collecting, sk:backgro, bgcolor X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (209.51.188.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 03 Mar 2019 15:42:44 +0000 Received: from fencepost.gnu.org ([2001:470:142:3::e]:59427) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h0TGF-0006U1-3t; Sun, 03 Mar 2019 10:42:43 -0500 Received: from [176.228.60.248] (port=2092 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1h0TGE-0001h5-Mh; Sun, 03 Mar 2019 10:42:43 -0500 Date: Sun, 03 Mar 2019 17:42:35 +0200 Message-Id: <83ef7oezh0.fsf@gnu.org> From: Eli Zaretskii To: Tom Tromey CC: gdb-patches@sourceware.org In-reply-to: <87mumeb935.fsf@tromey.com> (message from Tom Tromey on Fri, 01 Mar 2019 14:03:58 -0700) Subject: Re: [PATCH 00/16] Add styling to the gdb CLI and TUI References: <20181128001435.12703-1-tom@tromey.com> <83k1kxfzwo.fsf@gnu.org> <8736rja4i8.fsf@tromey.com> <83r2brhw8k.fsf@gnu.org> <87h8cmh1wg.fsf@tromey.com> <83va12gz8j.fsf@gnu.org> <87mumeb935.fsf@tromey.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-IsSubscribed: yes > From: Tom Tromey > Cc: Tom Tromey , gdb-patches@sourceware.org > Date: Fri, 01 Mar 2019 14:03:58 -0700 > > Another way would be to just make a new function and implement it > differently in mingw-hdep.c and in posix-hdep.c. Here's a working prototype I'm currently testing. If this implementation strategy is acceptable, I will post a proper patch soon. Thanks for the guidance. --- ./gdb/cli/cli-style.c~1 2019-02-27 06:51:50.000000000 +0200 +++ ./gdb/cli/cli-style.c 2019-03-03 17:26:34.122234300 +0200 @@ -24,7 +24,7 @@ /* True if styling is enabled. */ -#if defined(_WIN32) || defined (__CYGWIN__) +#if defined (__MSDOS__) || defined (__CYGWIN__) int cli_styling = 0; #else int cli_styling = 1; --- ./gdb/mingw-hdep.c~1 2019-02-27 06:51:50.000000000 +0200 +++ ./gdb/mingw-hdep.c 2019-03-03 17:15:24.608418300 +0200 @@ -177,3 +177,166 @@ gdb_select (int n, fd_set *readfds, fd_s return num_ready; } + +/* Zero if not yet initialized, 1 if stdout is a console device, else -1. */ +static int mingw_console_initialized; + +/* Handle to stdout . */ +static HANDLE hstdout = INVALID_HANDLE_VALUE; + +/* Text attribute to use for normal text (the "none" pseudo-color). */ +static SHORT norm_attr; + +/* The most recently applied style. */ +static ui_file_style last_style; + +/* Alternative for the libc 'fputs' which handles embedded SGR + sequences in support of styling. */ + +int +gdb_console_fputs (const char *linebuf, FILE *fstream) +{ + if (!mingw_console_initialized) + { + hstdout = (HANDLE)_get_osfhandle (fileno (fstream)); + DWORD cmode; + CONSOLE_SCREEN_BUFFER_INFO csbi; + + if (hstdout != INVALID_HANDLE_VALUE + && GetConsoleMode (hstdout, &cmode) != 0 + && GetConsoleScreenBufferInfo (hstdout, &csbi)) + { + norm_attr = csbi.wAttributes; + mingw_console_initialized = 1; + } + else if (hstdout != INVALID_HANDLE_VALUE) + mingw_console_initialized = -1; /* valid, but not a console device */ + } + /* If our stdout is not a console device, let the default 'fputs' + handle the task. */ + if (mingw_console_initialized <= 0) + return 0; + + /* Mapping between 8 ANSI colors and Windows console attributes. */ + static int fg_color[] = { + 0, /* black */ + FOREGROUND_RED, /* red */ + FOREGROUND_GREEN, /* green */ + FOREGROUND_GREEN | FOREGROUND_RED, /* yellow */ + FOREGROUND_BLUE, /* blue */ + FOREGROUND_BLUE | FOREGROUND_RED, /* magenta */ + FOREGROUND_BLUE | FOREGROUND_GREEN, /* cyan */ + FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* gray */ + }; + static int bg_color[] = { + 0, /* black */ + BACKGROUND_RED, /* red */ + BACKGROUND_GREEN, /* green */ + BACKGROUND_GREEN | BACKGROUND_RED, /* yellow */ + BACKGROUND_BLUE, /* blue */ + BACKGROUND_BLUE | BACKGROUND_RED, /* magenta */ + BACKGROUND_BLUE | BACKGROUND_GREEN, /* cyan */ + BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE /* gray */ + }; + + ui_file_style style = last_style; + unsigned char c; + size_t n_read; + + for ( ; (c = *linebuf) != 0; linebuf += n_read) + { + if (c == '\033') + { + fflush (fstream); + bool parsed = style.parse (linebuf, &n_read); + if (n_read <= 0) /* should never happen */ + n_read = 1; + if (!parsed) + { + /* This means we silently swallow SGR sequences we + cannot parse. */ + continue; + } + /* Colors. */ + const ui_file_style::color &fg = style.get_foreground (); + const ui_file_style::color &bg = style.get_background (); + int fgcolor, bgcolor, bright, inverse; + if (fg.is_none ()) + fgcolor = norm_attr & 15; + else + fgcolor = fg_color[fg.get_value () & 15]; + if (bg.is_none ()) + bgcolor = norm_attr & (15 << 4); + else + bgcolor = bg_color[bg.get_value () & 15]; + + /* Intensity. */ + switch (style.get_intensity ()) + { + case ui_file_style::NORMAL: + case ui_file_style::DIM: + bright = 0; + break; + case ui_file_style::BOLD: + bright = 1; + break; + default: + gdb_assert_not_reached ("invalid intensity"); + } + + /* Inverse video. */ + if (style.is_reverse ()) + inverse = 1; + else + inverse = 0; + + /* Construct the attribute. */ + if (inverse) + { + int t = fgcolor; + fgcolor = (bgcolor >> 4); + bgcolor = (t << 4); + } + if (bright) + fgcolor |= FOREGROUND_INTENSITY; + + SHORT attr = (bgcolor & (15 << 4)) | (fgcolor & 15); + + /* Apply the attribute. */ + SetConsoleTextAttribute (hstdout, attr); + } + else + { + /* When we are about to write newline, we need to clear to + EOL with the normal attribute, to avoid spilling the + colors to the next screen line. We assume here that no + non-default attribute extends beyond the newline. */ + if (c == '\n') + { + DWORD nchars; + COORD start_pos; + DWORD written; + CONSOLE_SCREEN_BUFFER_INFO csbi; + + fflush (fstream); + GetConsoleScreenBufferInfo (hstdout, &csbi); + + if (csbi.wAttributes != norm_attr) + { + start_pos = csbi.dwCursorPosition; + nchars = csbi.dwSize.X - start_pos.X; + + FillConsoleOutputAttribute (hstdout, norm_attr, nchars, + start_pos, &written); + FillConsoleOutputCharacter (hstdout, ' ', nchars, + start_pos, &written); + } + } + fputc (c, fstream); + n_read = 1; + } + } + + last_style = style; + return 1; +} --- ./gdb/posix-hdep.c~1 2019-02-27 06:51:50.000000000 +0200 +++ ./gdb/posix-hdep.c 2019-03-03 17:34:32.870539500 +0200 @@ -30,3 +30,11 @@ gdb_select (int n, fd_set *readfds, fd_s { return select (n, readfds, writefds, exceptfds, timeout); } + +/* Host-dependent console fputs method. POSIX platforms always return + zero, to use the default C 'fputs'. */ +int +gdb_console_fputs (const char *buf, FILE *f) +{ + return 0; +} --- ./gdb/ui-file.c~1 2019-02-27 06:51:50.000000000 +0200 +++ ./gdb/ui-file.c 2019-03-03 17:32:02.526701100 +0200 @@ -236,6 +236,12 @@ stdio_file::write_async_safe (const char void stdio_file::puts (const char *linebuffer) { + /* This host-dependent function (with implementations in + posix-hdep.c and mingw-hdep.c) is given the opportunity to + process the output first in host-dependent way. If it does, it + should return non-zero, to avoid calling fputs below. */ + if (gdb_console_fputs (linebuffer, m_file)) + return; /* Calling error crashes when we are called from the exception framework. */ if (fputs (linebuffer, m_file)) { --- ./gdb/ui-file.h~1 2019-02-27 06:51:50.000000000 +0200 +++ ./gdb/ui-file.h 2019-03-03 17:40:00.605222700 +0200 @@ -101,6 +101,8 @@ extern long ui_file_read (struct ui_file *file, char *buf, long length_buf); +extern int gdb_console_fputs (const char *, FILE *); + /* A std::string-based ui_file. Can be used as a scratch buffer for collecting output. */ --- ./gdb/utils.c~1 2019-02-27 06:51:50.000000000 +0200 +++ ./gdb/utils.c 2019-03-03 17:39:30.222675900 +0200 @@ -1456,8 +1456,18 @@ can_emit_style_escape (struct ui_file *s || !ui_file_isatty (stream)) return false; const char *term = getenv ("TERM"); + /* Windows doesn't by default define $TERM, but can support styles + regardless. */ +#ifndef _WIN32 if (term == nullptr || !strcmp (term, "dumb")) return false; +#else + /* But if they do define $TERM, let us behave the same as on Posix + platforms, for the benefit of programs which invoke GDB as their + back-end. */ + if (term && !strcmp (term, "dumb")) + return false; +#endif return true; }