aboutsummaryrefslogtreecommitdiff
path: root/lib/cli/optparse.c
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-10-28 23:43:31 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-10-28 23:43:31 +0100
commit3f7c3d8baa64fecbfb1a30db853731897035fd0b (patch)
treee254627c27fd7504a57b67fc381323c37a0c5d10 /lib/cli/optparse.c
parentf3178067bb6963aa913dfd9a024e960729056760 (diff)
Only use UTF-8 when the locale permits it
Diffstat (limited to 'lib/cli/optparse.c')
-rw-r--r--lib/cli/optparse.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/cli/optparse.c b/lib/cli/optparse.c
index 6544b06..f4034a9 100644
--- a/lib/cli/optparse.c
+++ b/lib/cli/optparse.c
@@ -1,6 +1,8 @@
+#include <langinfo.h>
#include <stdio.h>
#include <string.h>
+#include "_attrs.h"
#include "cli.h"
#include "macros.h"
#include "mbstring.h"
@@ -141,11 +143,19 @@ out:
return ch;
}
+[[_mlib_inline]]
+static inline bool
+utf8p(void)
+{
+ return streq("UTF-8", nl_langinfo(CODESET));
+}
+
rune
error_s(optparser_t *st, const char *msg, u8view_t s)
{
- snprintf(st->errmsg, sizeof(st->errmsg), u8"%s — ‘%.*s’", msg,
- SV_PRI_ARGS(s));
+ snprintf(st->errmsg, sizeof(st->errmsg),
+ utf8p() ? u8"%s — ‘%.*s’" : u8"%s -- `%.*s'",
+ msg, SV_PRI_ARGS(s));
return -1;
}
@@ -153,7 +163,8 @@ rune
error_r(optparser_t *st, const char *msg, rune ch)
{
char buf[U8_LEN_MAX + 1] = {};
- snprintf(st->errmsg, sizeof(st->errmsg), u8"%s — ‘%.*s’", msg,
- rtou8(buf, sizeof(buf), ch), buf);
+ snprintf(st->errmsg, sizeof(st->errmsg),
+ utf8p() ? u8"%s — ‘%.*s’" : u8"%s -- `%.*s'",
+ msg, rtou8(buf, sizeof(buf), ch), buf);
return -1;
}