2022-07-13 13:03:35 +02:00
|
|
|
From c80065fe6900be5e794941e29b32440e9969b1c3 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Christos Zoulas <christos@zoulas.com>
|
|
|
|
Date: Mon, 4 Jul 2022 19:44:35 +0000
|
2022-07-21 16:10:28 +02:00
|
|
|
Subject: [PATCH 1/5] PR/362: ro-ee: fix wide char printing
|
2022-07-13 13:03:35 +02:00
|
|
|
|
|
|
|
---
|
2022-07-21 16:10:28 +02:00
|
|
|
src/file.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++-------------
|
|
|
|
src/file.h | 4 +-
|
|
|
|
src/funcs.c | 4 +-
|
|
|
|
3 files changed, 81 insertions(+), 24 deletions(-)
|
2022-07-13 13:03:35 +02:00
|
|
|
|
|
|
|
--- src/file.c
|
2022-07-21 16:10:28 +02:00
|
|
|
+++ src/file.c 2022-07-21 13:56:33.280169243 +0000
|
|
|
|
@@ -60,6 +60,12 @@ FILE_RCSID("@(#)$File: file.c,v 1.195 20
|
2022-07-13 13:03:35 +02:00
|
|
|
#ifdef HAVE_WCTYPE_H
|
|
|
|
#include <wctype.h>
|
|
|
|
#endif
|
|
|
|
+#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) && \
|
|
|
|
+ defined(HAVE_WCTYPE_H)
|
|
|
|
+#define FILE_WIDE_SUPPORT
|
|
|
|
+#else
|
|
|
|
+#include <ctype.h>
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
|
|
|
|
# include <getopt.h>
|
2022-07-21 16:10:28 +02:00
|
|
|
@@ -544,6 +550,58 @@ unwrap(struct magic_set *ms, const char
|
2022-07-13 13:03:35 +02:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
+private void
|
|
|
|
+file_octal(unsigned char c)
|
|
|
|
+{
|
|
|
|
+ putc('\\', stdout);
|
|
|
|
+ putc(((c >> 6) & 7) + '0', stdout);
|
|
|
|
+ putc(((c >> 3) & 7) + '0', stdout);
|
|
|
|
+ putc(((c >> 0) & 7) + '0', stdout);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+private void
|
|
|
|
+fname_print(const char *inname)
|
|
|
|
+{
|
|
|
|
+ size_t n = strlen(inname);
|
|
|
|
+#ifdef FILE_WIDE_SUPPORT
|
|
|
|
+ mbstate_t state;
|
|
|
|
+ wchar_t nextchar;
|
|
|
|
+ size_t bytesconsumed;
|
|
|
|
+
|
|
|
|
+
|
2022-07-21 16:10:28 +02:00
|
|
|
+ (void)memset(&state, 0, sizeof(state));
|
2022-07-13 13:03:35 +02:00
|
|
|
+ while (n > 0) {
|
|
|
|
+ bytesconsumed = mbrtowc(&nextchar, inname, n, &state);
|
|
|
|
+ if (bytesconsumed == CAST(size_t, -1) ||
|
|
|
|
+ bytesconsumed == CAST(size_t, -2)) {
|
2022-07-21 16:10:28 +02:00
|
|
|
+ nextchar = *inname++;
|
|
|
|
+ n--;
|
|
|
|
+ (void)memset(&state, 0, sizeof(state));
|
|
|
|
+ file_octal(CAST(unsigned char, nextchar));
|
|
|
|
+ continue;
|
2022-07-13 13:03:35 +02:00
|
|
|
+ }
|
|
|
|
+ inname += bytesconsumed;
|
|
|
|
+ n -= bytesconsumed;
|
|
|
|
+ if (iswprint(nextchar)) {
|
2022-07-21 16:10:28 +02:00
|
|
|
+ printf("%lc", nextchar);
|
2022-07-13 13:03:35 +02:00
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ /* XXX: What if it is > 255? */
|
|
|
|
+ file_octal(CAST(unsigned char, nextchar));
|
|
|
|
+ }
|
|
|
|
+#else
|
|
|
|
+ size_t i;
|
|
|
|
+ for (i = 0; i < n; i++) {
|
|
|
|
+ unsigned char c = CAST(unsigned char, inname[i]);
|
|
|
|
+ if (isprint(c)) {
|
|
|
|
+ putc(c);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ file_octal(c);
|
|
|
|
+ }
|
|
|
|
+#endif
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
/*
|
|
|
|
* Called for each input file on the command line (or in a list of files)
|
|
|
|
*/
|
2022-07-21 16:10:28 +02:00
|
|
|
@@ -553,15 +611,13 @@ process(struct magic_set *ms, const char
|
2022-07-13 13:03:35 +02:00
|
|
|
const char *type, c = nulsep > 1 ? '\0' : '\n';
|
|
|
|
int std_in = strcmp(inname, "-") == 0;
|
|
|
|
int haderror = 0;
|
|
|
|
- size_t plen = 4 * wid + 1;
|
|
|
|
- char *pbuf, *pname;
|
|
|
|
-
|
|
|
|
- if ((pbuf = CAST(char *, malloc(plen))) == NULL)
|
|
|
|
- file_err(EXIT_FAILURE, "Can't allocate %zu bytes", plen);
|
|
|
|
|
|
|
|
if (wid > 0 && !bflag) {
|
|
|
|
- pname = file_printable(ms, pbuf, plen, inname, wid);
|
|
|
|
- (void)printf("%s", std_in ? "/dev/stdin" : pname);
|
|
|
|
+ const char *pname = std_in ? "/dev/stdin" : inname;
|
|
|
|
+ if ((ms->flags & MAGIC_RAW) == 0)
|
|
|
|
+ fname_print(pname);
|
|
|
|
+ else
|
|
|
|
+ (void)printf("%s", pname);
|
|
|
|
if (nulsep)
|
|
|
|
(void)putc('\0', stdout);
|
|
|
|
if (nulsep < 2) {
|
2022-07-21 16:10:28 +02:00
|
|
|
@@ -580,7 +636,6 @@ process(struct magic_set *ms, const char
|
2022-07-13 13:03:35 +02:00
|
|
|
}
|
|
|
|
if (nobuffer)
|
|
|
|
haderror |= fflush(stdout) != 0;
|
|
|
|
- free(pbuf);
|
|
|
|
return haderror || type == NULL;
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:10:28 +02:00
|
|
|
@@ -588,35 +643,37 @@ protected size_t
|
2022-07-13 13:03:35 +02:00
|
|
|
file_mbswidth(struct magic_set *ms, const char *s)
|
|
|
|
{
|
|
|
|
size_t width = 0;
|
|
|
|
-#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) && \
|
|
|
|
- defined(HAVE_WCTYPE_H)
|
|
|
|
- size_t bytesconsumed, old_n, n;
|
|
|
|
+#ifdef FILE_WIDE_SUPPORT
|
|
|
|
+ size_t bytesconsumed, n;
|
|
|
|
mbstate_t state;
|
|
|
|
wchar_t nextchar;
|
|
|
|
- (void)memset(&state, 0, sizeof(mbstate_t));
|
|
|
|
- old_n = n = strlen(s);
|
|
|
|
+
|
2022-07-21 16:10:28 +02:00
|
|
|
+ (void)memset(&state, 0, sizeof(state));
|
2022-07-13 13:03:35 +02:00
|
|
|
+ n = strlen(s);
|
|
|
|
|
|
|
|
while (n > 0) {
|
|
|
|
bytesconsumed = mbrtowc(&nextchar, s, n, &state);
|
|
|
|
if (bytesconsumed == CAST(size_t, -1) ||
|
|
|
|
bytesconsumed == CAST(size_t, -2)) {
|
|
|
|
- /* Something went wrong, return something reasonable */
|
|
|
|
- return old_n;
|
|
|
|
+ nextchar = *s;
|
|
|
|
+ bytesconsumed = 1;
|
2022-07-21 16:10:28 +02:00
|
|
|
+ (void)memset(&state, 0, sizeof(state));
|
|
|
|
+ width += 4;
|
|
|
|
+ } else {
|
|
|
|
+ int w = wcwidth(nextchar);
|
|
|
|
+ width += ((ms->flags & MAGIC_RAW) != 0
|
|
|
|
+ || iswprint(nextchar)) ? (w > 0 ? w : 1) : 4;
|
2022-07-13 13:03:35 +02:00
|
|
|
}
|
2022-07-21 16:10:28 +02:00
|
|
|
- width += ((ms->flags & MAGIC_RAW) != 0
|
|
|
|
- || iswprint(nextchar)) ? wcwidth(nextchar) : 4;
|
2022-07-13 13:03:35 +02:00
|
|
|
|
|
|
|
s += bytesconsumed, n -= bytesconsumed;
|
|
|
|
}
|
|
|
|
- return width;
|
|
|
|
#else
|
|
|
|
while (*s) {
|
|
|
|
width += (ms->flags & MAGIC_RAW) != 0
|
|
|
|
|| isprint(CAST(unsigned char, *s)) ? 1 : 4;
|
|
|
|
}
|
|
|
|
-
|
|
|
|
- return strlen(s);
|
|
|
|
#endif
|
|
|
|
+ return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void
|
|
|
|
--- src/file.h
|
2022-07-21 16:10:28 +02:00
|
|
|
+++ src/file.h 2022-07-21 13:56:33.280169243 +0000
|
|
|
|
@@ -575,7 +575,7 @@ protected size_t file_pstring_length_siz
|
2022-07-13 13:03:35 +02:00
|
|
|
const struct magic *);
|
|
|
|
protected size_t file_pstring_get_length(struct magic_set *,
|
|
|
|
const struct magic *, const char *);
|
|
|
|
-public char * file_printable(struct magic_set *, char *, size_t,
|
|
|
|
+protected char * file_printable(struct magic_set *, char *, size_t,
|
|
|
|
const char *, size_t);
|
|
|
|
#ifdef __EMX__
|
|
|
|
protected int file_os2_apptype(struct magic_set *, const char *, const void *,
|
|
|
|
--- src/funcs.c
|
2022-07-21 16:10:28 +02:00
|
|
|
+++ src/funcs.c 2022-07-21 13:56:33.280169243 +0000
|
|
|
|
@@ -763,7 +763,7 @@ file_pop_buffer(struct magic_set *ms, fi
|
2022-07-13 13:03:35 +02:00
|
|
|
/*
|
|
|
|
* convert string to ascii printable format.
|
|
|
|
*/
|
|
|
|
-public char *
|
|
|
|
+protected char *
|
|
|
|
file_printable(struct magic_set *ms, char *buf, size_t bufsiz,
|
|
|
|
const char *str, size_t slen)
|
|
|
|
{
|