gbranden pushed a commit to branch master
in repository groff.
commit 2db473fe5eb150b1241428941f39258876c22e33
Author: G. Branden Robinson <[email protected]>
AuthorDate: Wed Nov 11 06:47:02 2026 -0600
src/preproc/tbl/main.cpp: Improve error messages.
...by clearly detailing how the expected input differs from the actual
input.
* src/preproc/tbl/main.cpp (describe_character): New function populates
a static buffer with a description of an input character, robustly
handling unprintable characters by reporting their character codes in
decimal.
(process_format): Recast diagnostics and call the new function to
detail input received.
Before:
$ printf '.TS\nL.L\nfoo\n.TE\n' | ./build/tbl > /dev/null
./build/tbl:<standard input>:2: error: '.' is not the last character of the
table description
./build/tbl:<standard input>:2: error: giving up on this table region
$ printf '.TS\nL\nfoo\n.TE\n' | ./build/tbl > /dev/null
./build/tbl:<standard input>:3: error: expected column classifier, got 'f'
./build/tbl:<standard input>:3: error: giving up on this table region
$ printf '.TS\n\002\nfoo\n.TE\n' | ./build/tbl > /dev/null
./build/tbl:<standard input>:2: error: expected column classifier, got ''
./build/tbl:<standard input>:2: error: giving up on this table region
$ printf '.TS\n\377\nfoo\n.TE\n' | ./build/tbl > /dev/null
./build/tbl:<standard input>:2: error: expected column classifier, got '�'
./build/tbl:<standard input>:2: error: giving up on this table region
After:
$ printf '.TS\nL.L\nfoo\n.TE\n' | ./build/tbl > /dev/null
./build/tbl:<standard input>:2: error: expected newline after '.'
terminating table description; got 'L'
./build/tbl:<standard input>:2: error: giving up on this table region
$ printf '.TS\nL\nfoo\n.TE\n' | ./build/tbl > /dev/null
./build/tbl:<standard input>:3: error: expected column classifier, got 'f'
./build/tbl:<standard input>:3: error: giving up on this table region
$ printf '.TS\n\002\nfoo\n.TE\n' | ./build/tbl > /dev/null
./build/tbl:<standard input>:2: error: expected column classifier, got
character code 2
./build/tbl:<standard input>:2: error: giving up on this table region
$ printf '.TS\n\377\nfoo\n.TE\n' | ./build/tbl > /dev/null
./build/tbl:<standard input>:2: error: expected column classifier, got
character code 255
./build/tbl:<standard input>:2: error: giving up on this table region
---
ChangeLog | 11 +++++++++++
src/preproc/tbl/main.cpp | 33 +++++++++++++++++++++++++++++++--
2 files changed, 42 insertions(+), 2 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 9f23f89dc..9ca62d89f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2026-11-11 G. Branden Robinson <[email protected]>
+
+ * src/preproc/tbl/main.cpp: Improve error diagnostics by clearly
+ detailing how the expected input differs from the actual input.
+ (describe_character): New function populates a static buffer
+ with a description of an input character, robustly handling
+ unprintable characters by reporting their character codes in
+ decimal.
+ (process_format): Recast diagnostics and call the new function
+ to detail input received.
+
2026-11-11 G. Branden Robinson <[email protected]>
* src/preproc/tbl/main.cpp (process_data): Clarify error
diff --git a/src/preproc/tbl/main.cpp b/src/preproc/tbl/main.cpp
index 0edba7dbc..c15c9e653 100644
--- a/src/preproc/tbl/main.cpp
+++ b/src/preproc/tbl/main.cpp
@@ -33,6 +33,9 @@ along with this program. If not, see
<http://www.gnu.org/licenses/>. */
// GNU extensions to C standard library
#include <getopt.h> // getopt_long()
+// libgroff
+#include "lib.h" // INT_DIGITS
+
#include "table.h"
#define MAX_POINT_SIZE 99
@@ -784,6 +787,30 @@ void input_entry_format::debug_print()
putc(',', stderr);
}
+// Describe an input character.
+// TODO: Can we usefully pull this into libgroff? If so, we might want
+// to convert it to use dynamic storage, to make it flexible enough for
+// use in reëntrant or multithreaded code.
+// TODO: Describe an unprintable character in Unicode notation?
+static const char *describe_character(int c)
+{
+ // This buffer is larger than `'X'` for any X.
+ // constexpr // C++11
+ static const char cc[] = "character code";
+ const size_t descbufsize = strlen(cc) + 1 /* ' ' */ + INT_DIGITS
+ + 1 /* '\0' */;
+ static char descbuf[descbufsize];
+ if (csprint(c)) {
+ char quote = '\'';
+ if ('\'' == c)
+ quote = '"';
+ (void) snprintf(descbuf, descbufsize, "%c%c%c", quote, c, quote);
+ }
+ else
+ (void) snprintf(descbuf, descbufsize, "%s %d", cc, c);
+ return descbuf;
+}
+
// Interpret a table description, like "CC,LR.". Return null pointer if
// we should give up on this table. If this is a continued description,
// `current_format` will be the current description.
@@ -875,7 +902,8 @@ static format *process_format(table_input &in, options *opt,
default:
if (c == opt->tab_char)
break;
- error("expected column classifier, got '%1'", char(c));
+ error("expected column classifier, got %1",
+ describe_character(c));
free_input_entry_format_list(list);
list = 0 /* nullptr */;
return 0 /* nullptr */;
@@ -1200,7 +1228,8 @@ static format *process_format(table_input &in, options
*opt,
c = in.get();
} while (c == ' ' || c == '\t');
if (c != '\n') {
- error("'.' is not the last character of the table description");
+ error("expected newline after '.' terminating table description;"
+ " got %1", describe_character(c));
free_input_entry_format_list(list);
list = 0 /* nullptr */;
return 0 /* nullptr */;
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit