]> git.pld-linux.org Git - packages/bsd-finger.git/blame - bsd-finger-wide-char-support5.patch
- store patches in decompressed files
[packages/bsd-finger.git] / bsd-finger-wide-char-support5.patch
CommitLineData
404c395e
JR
1--- bsd-finger-0.17/finger/finger.c.widechar 2005-12-15 09:14:18.000000000 +0100
2+++ bsd-finger-0.17/finger/finger.c 2005-12-15 09:14:18.000000000 +0100
3@@ -77,6 +77,7 @@
4 #include <limits.h>
5 #include <time.h>
6 #include <getopt.h>
7+#include <locale.h>
8 #include "finger.h"
9 #include "../version.h"
10
222b58b8
JR
11@@ -96,6 +96,9 @@
12 struct sockaddr_in sin;
13 socklen_t slen = sizeof(sin);
404c395e
JR
14
15+ if (setlocale (LC_ALL, "") != NULL)
16+ set_haslocale();
17+
222b58b8
JR
18 while ((ch = getopt(argc, argv, "lmps")) != EOF) {
19 switch(ch) {
20 case 'l':
404c395e
JR
21--- bsd-finger-0.17/finger/finger.h.widechar 2005-12-15 09:14:17.000000000 +0100
22+++ bsd-finger-0.17/finger/finger.h 2005-12-15 09:14:18.000000000 +0100
23@@ -117,3 +117,8 @@
24 /* terminal inquiries */
25 int is8bit(void);
26 int getscreenwidth(void);
27+
28+/* locale support */
3b58e503 29+extern int has_locale;
404c395e
JR
30+void set_haslocale(void);
31+
32--- bsd-finger-0.17/finger/display.c.widechar 1999-09-29 00:53:58.000000000 +0200
33+++ bsd-finger-0.17/finger/display.c 2005-12-15 10:05:40.000000000 +0100
34@@ -40,8 +40,19 @@
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdarg.h>
38+#include <inttypes.h>
39 #include "finger.h"
40
41+#define HAVE_WCHAR_H 1
42+#define HAVE_MBRTOWC 1
43+#define HAVE_WCWIDTH 1
44+
45+#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
46+#include <wchar.h>
47+#include <wctype.h>
48+#include <assert.h>
49+#endif
50+
51 int
52 getscreenwidth(void)
53 {
54@@ -147,9 +158,105 @@
55 fxputc(stdout, ch);
56 }
57
58+int has_locale = 0;
59+
60+void
61+set_haslocale (void)
62+{
63+ has_locale = 1;
64+}
65+
66+#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
67+static int verifymultibyte(const char *buf) {
68+ mbstate_t state;
69+ wchar_t nextchar;
70+ size_t bytesconsumed;
71+ char *eop, *op;
72+ (void)memset(&state, 0, sizeof(mbstate_t));
73+
74+ eop = (char *) (buf + strlen(buf));
75+ op = (char *) buf;
76+ while (op < eop) {
77+ bytesconsumed = mbrtowc(&nextchar, op, eop - op, &state);
78+ if (bytesconsumed == (size_t)(-1) ||
79+ bytesconsumed == (size_t)(-2)) {
80+ return 0;
81+ }
82+ op += bytesconsumed;
83+ }
84+
85+ return 1;
86+}
87+
88+#define OCTALIFY(n, o) \
89+ *(n)++ = '\\', \
90+ *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
91+ *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
92+ *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
93+ (o)++
94+
95+#endif
96+
97 static void fxputs(FILE *f, const char *buf) {
98- int i;
99- for (i=0; buf[i]; i++) fxputc(f, buf[i]);
100+ int widechars;
101+
102+#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
103+ if (has_locale)
104+ widechars = verifymultibyte (buf);
105+ else
106+ widechars = 0;
107+#else
108+ widechars = 0;
109+#endif
110+
111+ /* on 7-bit terminals, without wide-chars support, or string
112+ * isn't parseable, print char * by char */
113+ if (!is8bit() || !widechars) {
114+ unsigned int i;
115+ char ch;
116+ for (i = 0; i < strlen (buf); i++) {
117+ ch = buf[i];
118+ fxputc(f, ch);
119+ }
120+ return;
121+ }
122+
123+#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
124+ {
125+ mbstate_t state;
126+ wchar_t nextchar;
127+ size_t bytesconsumed;
128+ char *eop, *op, buffer[256];
129+ (void)memset(&state, 0, sizeof(mbstate_t));
130+ char* op1;
131+ eop = (char *) (buf + strlen(buf));
132+ op = (char *) buf;
133+ op1 = op;
134+ while (op < eop) {
135+ bytesconsumed = mbrtowc(&nextchar, op,
136+ eop - op, &state);
137+ /* This isn't supposed to happen as we verified the
138+ * string before hand */
139+ assert(bytesconsumed != (size_t)(-1) && bytesconsumed != (size_t)(-2));
140+
141+ if (iswprint(nextchar)) {
142+ (void)memcpy(buffer, op, bytesconsumed);
143+ buffer[bytesconsumed] = '\0';
144+ op += bytesconsumed;
145+ } else if (bytesconsumed == 1) {
146+ op++;
147+ } else {
148+ char *tmp;
149+ tmp = buffer;
150+ buffer[bytesconsumed] = '\0';
151+ while (bytesconsumed-- > 0) {
152+ OCTALIFY(tmp, op);
153+ }
154+ }
155+ }
156+ fprintf(f,"%s",op1);
157+ }
158+#endif
159 }
160
161 int xprintf(const char *fmt, ...) {
This page took 0.078547 seconds and 4 git commands to generate.