gbranden pushed a commit to branch master
in repository groff.
commit b0c2b82d751e7401ccd77c244684ed7a22a57ed5
Author: G. Branden Robinson <[email protected]>
AuthorDate: Wed Jun 4 00:24:17 2025 -0500
[grotty]: Refactor and fix code style nits.
* src/devices/grotty/tty.cpp: Convert manifest constant
`DEFAULT_COLOR_IDX` from preprocessor macro to global variable of type
`const int`.
(tty_printer::put_color): Reorder equality comparisons to avoid
inadvertent lvalue assignment. Compute buffer length for formatted
string using `sizeof()` on string literal representing expected output
of maximal length. Drop `static` qualifier from same buffer; the
buffer's contents do not need to outlive the function call, and
allocating ~18 bytes from the stack should never be a problem.
Convert `sprintf()` call to `snprintf()` and save its return value
instead of discarding it. Use `assert()` to check for truncation of
formatted string. This is merely an `assert()` because we don't
expect truncation to ever occur in a production environment, because
the buffer is sized for output of maximal length.
---
ChangeLog | 18 ++++++++++++++++++
src/devices/grotty/tty.cpp | 20 +++++++++++++-------
2 files changed, 31 insertions(+), 7 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index e82fc50d8..b703ed9c0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2025-06-04 G. Branden Robinson <[email protected]>
+
+ * src/devices/grotty/tty.cpp: Refactor and fix code style nits.
+ Convert manifest constant `DEFAULT_COLOR_IDX` from preprocessor
+ macro to global variable of type `const int`.
+ (tty_printer::put_color): Reorder equality comparisons to avoid
+ inadvertent lvalue assignment. Compute buffer length for
+ formatted string using `sizeof()` on string literal representing
+ expected output of maximal length. Drop `static` qualifier from
+ same buffer; the buffer's contents do not need to outlive the
+ function call, and allocating ~18 bytes from the stack should
+ never be a problem. Convert `sprintf()` call to `snprintf()`
+ and save its return value instead of discarding it. Use
+ `assert()` to check for truncation of formatted string. This
+ is merely an `assert()` because we don't expect truncation to
+ ever occur in a production environment, because the buffer is
+ sized for output of maximal length.
+
2025-06-03 G. Branden Robinson <[email protected]>
* src/devices/grotty/tty.cpp (tty_printer::put_color): Fix
diff --git a/src/devices/grotty/tty.cpp b/src/devices/grotty/tty.cpp
index 1354b230a..772ae95a3 100644
--- a/src/devices/grotty/tty.cpp
+++ b/src/devices/grotty/tty.cpp
@@ -108,7 +108,7 @@ static unsigned char bold_underline_mode;
// 'CSI 0 m' exclusively
#define SGR_DEFAULT CSI "0m"
-#define DEFAULT_COLOR_IDX -1
+const int DEFAULT_COLOR_IDX = -1;
class tty_font : public font {
tty_font(const char *);
@@ -710,7 +710,7 @@ void tty_printer::put_char(output_character wc)
void tty_printer::put_color(long color_index, int back)
{
if (!want_sgr_truecolor) {
- if (color_index == DEFAULT_COLOR_IDX) {
+ if (DEFAULT_COLOR_IDX == color_index) {
putstring(SGR_DEFAULT);
// set bold and underline again
if (is_boldfacing)
@@ -738,16 +738,22 @@ void tty_printer::put_color(long color_index, int back)
}
}
else {
- if (color_index == DEFAULT_COLOR_IDX) {
+ if (DEFAULT_COLOR_IDX == color_index) {
putstring(SGR_DEFAULT);
back = !back;
color_index = back ? curr_back_idx : curr_fore_idx;
- if (color_index == DEFAULT_COLOR_IDX) {return;}
+ if (DEFAULT_COLOR_IDX == color_index)
+ return;
}
putstring(CSI);
int fb = back ? 48 : 38;
- static char buf[24];
- sprintf(buf,"%d;2;%lu;%lu;%lum",fb, color_index>>16, (color_index>>8) &
0xff, color_index & 0xff);
+ const size_t buflen = sizeof "48;2;255;255;255m";
+ char buf[buflen];
+ size_t written = snprintf(buf, buflen, "%d;2;%lu;%lu;%lum", fb,
+ (color_index >> 16),
+ ((color_index >> 8) & 0xff),
+ (color_index & 0xff));
+ assert(written < buflen);
putstring(buf);
}
}
@@ -1036,7 +1042,7 @@ int main(int argc, char **argv)
allow_drawing_commands = false;
break;
case 't':
- // TRUECOLOR
+ // Use SGR 38 and 48 sequences instead of SGR 30-37 and 40-47.
want_sgr_truecolor = true;
break;
case CHAR_MAX + 1: // --help
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit