summaryrefslogtreecommitdiffstats
path: root/contrib/tcsh/tc.nls.c
diff options
context:
space:
mode:
authormp <mp@FreeBSD.org>2007-03-11 22:33:41 +0000
committermp <mp@FreeBSD.org>2007-03-11 22:33:41 +0000
commita40980339b13e3b506c2317b5b4864127039eb2c (patch)
tree34aefea92d30b614247ef1f2671f2362f4761785 /contrib/tcsh/tc.nls.c
parent32837fb336d4709f0a121130a3a78f29be0db5ed (diff)
downloadFreeBSD-src-a40980339b13e3b506c2317b5b4864127039eb2c.zip
FreeBSD-src-a40980339b13e3b506c2317b5b4864127039eb2c.tar.gz
Import of tcsh-6.15.00
Diffstat (limited to 'contrib/tcsh/tc.nls.c')
-rw-r--r--contrib/tcsh/tc.nls.c257
1 files changed, 36 insertions, 221 deletions
diff --git a/contrib/tcsh/tc.nls.c b/contrib/tcsh/tc.nls.c
index 3b1209e..5a75f68 100644
--- a/contrib/tcsh/tc.nls.c
+++ b/contrib/tcsh/tc.nls.c
@@ -1,4 +1,4 @@
-/* $Header: /src/pub/tcsh/tc.nls.c,v 3.6 2005/02/15 21:09:02 christos Exp $ */
+/* $Header: /p/tcsh/cvsroot/tcsh/tc.nls.c,v 3.21 2006/09/26 16:45:30 christos Exp $ */
/*
* tc.nls.c: NLS handling
*/
@@ -32,30 +32,49 @@
*/
#include "sh.h"
-RCSID("$Id: tc.nls.c,v 3.6 2005/02/15 21:09:02 christos Exp $")
+RCSID("$tcsh: tc.nls.c,v 3.21 2006/09/26 16:45:30 christos Exp $")
-#ifdef SHORT_STRINGS
+#ifdef WIDE_STRINGS
int
-NLSWidth(c)
- NLSChar c;
+NLSWidth(Char c)
{
# ifdef HAVE_WCWIDTH
- int l;
- if (c & NLS_ILLEGAL)
+ int l;
+ if (c & INVALID_BYTE)
return 1;
l = wcwidth(c);
return l >= 0 ? l : 0;
# else
- return c != 0;
+ return iswprint(c) != 0;
# endif
}
+
+int
+NLSStringWidth(const Char *s)
+{
+ int w = 0, l;
+ Char c;
+
+ while (*s) {
+ c = *s++;
+#ifdef HAVE_WCWIDTH
+ if ((l = wcwidth(c)) < 0)
+ l = 2;
+#else
+ l = iswprint(c) != 0;
+#endif
+ w += l;
+ }
+ return w;
+}
#endif
-#if defined (WIDE_STRINGS) || !defined (SHORT_STRINGS)
Char *
-NLSChangeCase(Char *p, int mode)
+NLSChangeCase(const Char *p, int mode)
{
- Char c, *op = p, *n, c2 = 0;
+ Char c, *n, c2 = 0;
+ const Char *op = p;
+
for (; (c = *p) != 0; p++) {
if (mode == 0 && Islower(c)) {
c2 = Toupper(c);
@@ -73,231 +92,27 @@ NLSChangeCase(Char *p, int mode)
}
int
-NLSExtend(Char *from, int max, int num)
-{
- (void)from;
- num = abs (num);
- if (num > max)
- num = max;
- return num;
-}
-#endif
-
-#ifdef WIDE_STRINGS
-
-int
-NLSStringWidth(Char *s)
-{
- int w = 0;
- while (*s)
- w += wcwidth(*s++);
- return w;
-}
-
-#elif defined (SHORT_STRINGS)
-
-int
-NLSFrom(const Char *p, size_t l, NLSChar *cp)
-{
- size_t i;
- int len;
- wchar_t c;
- char b[MB_LEN_MAX];
-
- if (l == NLSZEROT) {
- for (i = 0; i < MB_CUR_MAX && *p; i++)
- b[i] = p[i] & CHAR;
- } else {
- for (i = 0; i < MB_CUR_MAX && i < l; i++)
- b[i] = p[i] & CHAR;
- }
- mbtowc(0, 0, 0);
- len = rt_mbtowc(&c, b, i);
- if (len <= 0) {
- if (cp)
- *cp = *p ? *p | NLS_ILLEGAL : 0;
- return 1;
- }
- if (cp)
- *cp = (int)c;
- return len;
-}
-
-int
-NLSFinished(Char *p, size_t l, eChar extra)
-{
- size_t i, r;
- wchar_t c;
- char b[MB_LEN_MAX + 1], back[MB_LEN_MAX];
- mbstate_t state;
- for (i = 0; i < MB_CUR_MAX && i < l; i++)
- b[i] = p[i];
- if (extra != CHAR_ERR)
- b[i++] = extra;
- memset(&state, 0, sizeof(state));
- r = mbrtowc((wchar_t *)&c, b, i, (mbstate_t *)&state);
- if (r == (size_t)-2)
- return 0;
- if (r == (size_t)-1 || (size_t)wctomb(back, c) != r ||
- memcmp(b, back, r) != 0)
- return -1;
- return r == i ? 1 : 2;
-}
-
-int
-NLSChars(Char *s)
-{
- int l;
- for (l = 0; *s; l++)
- s += NLSSize(s, -1);
- return l;
-}
-
-int
-NLSStringWidth(Char *s)
-{
- int w = 0;
- NLSChar c;
- while (*s) {
- s += NLSFrom(s, NLSZEROT, &c);
- w += NLSWidth(c);
- }
- return w;
-}
-
-int
-NLSTo(Char *p, NLSChar c)
-{
- char b[MB_LEN_MAX];
- int i, j;
-
- if (c & NLS_ILLEGAL) {
- if (p)
- *p = c;
- return 1;
- }
- i = wctomb(b, (wchar_t)c);
- if (i == -1)
- return 0;
- if (p)
- for (j = 0; j < i; j++)
- p[j] = b[j];
- return i;
-}
-
-
-int
-NLSExtend(Char *from, int max, int num)
-{
- int l, n, i;
- Char *p;
-
- if (num == 0)
- return 0;
- if (num > 0) {
- n = 0;
- while (num > 0 && max > 0) {
- l = NLSSize(from, max);
- n += l;
- from += l;
- max -= l;
- num--;
- }
- return n;
- }
- from -= max;
- p = from;
- i = max;
- n = 0;
- while (i > 0) {
- l = NLSSize(p, i);
- p += l;
- i -= l;
- n++;
- }
- if (n >= -num)
- n += num;
- else
- n = 0;
- i = max;
- while (n > 0) {
- l = NLSSize(from, max);
- from += l;
- max -= l;
- i -= l;
- n--;
- }
- return i;
-}
-
-void
-NLSQuote(Char *cp)
-{
- int l;
- while (*cp) {
- l = NLSSize(cp, -1);
- cp++;
- while (l-- > 1)
- *cp++ |= QUOTE;
- }
-}
-
-Char *
-NLSChangeCase(Char *p, int mode)
-{
- Char *n, *op = p;
- NLSChar c, c2 = 0;
- int l, l2;
-
- while (*p) {
- l = NLSFrom(p, NLSZEROT, &c);
- if (mode == 0 && iswlower((wint_t)c)) {
- c2 = (int)towupper((wint_t)c);
- break;
- } else if (mode && iswupper((wint_t)c)) {
- c2 = (int)towlower((wint_t)c);
- break;
- }
- p += l;
- }
- if (!*p)
- return 0;
- l2 = NLSTo((Char *)0, c2);
- n = (Char *)xmalloc((size_t)((op - p + l2 + Strlen(p + l) + 1) * sizeof(Char)));
- if (p != op)
- memcpy(n, op, (p - op) * sizeof(Char));
- NLSTo(n + (p - op), c2);
- memcpy(n + (p - op + l2), p + l, (Strlen(p + l) + 1) * sizeof(Char));
- return n;
-}
-#endif
-
-int
-NLSClassify(c, nocomb)
- NLSChar c;
- int nocomb;
+NLSClassify(Char c, int nocomb)
{
int w;
- if (c & NLS_ILLEGAL)
+ if (c & INVALID_BYTE)
return NLSCLASS_ILLEGAL;
w = NLSWidth(c);
- if (w > 0 || (Iswprint(c) && !nocomb))
+ if ((w > 0 && !(Iscntrl(c) && (c & CHAR) < 0x100)) || (Isprint(c) && !nocomb))
return w;
- if (Iswcntrl(c) && c < 0x100) {
+ if (Iscntrl(c) && (c & CHAR) < 0x100) {
if (c == '\n')
return NLSCLASS_NL;
if (c == '\t')
return NLSCLASS_TAB;
-#ifndef ASCII
- if (!Isupper(_toebcdic[_toascii[c]|0100]) && !strchr("@[\\]^_", _toebcdic[_toascii[c]|0100]))
- return NLSCLASS_ILLEGAL;
-#endif
return NLSCLASS_CTRL;
}
+#ifdef WIDE_STRINGS
if (c >= 0x1000000)
return NLSCLASS_ILLEGAL4;
if (c >= 0x10000)
return NLSCLASS_ILLEGAL3;
+#endif
if (c >= 0x100)
return NLSCLASS_ILLEGAL2;
return NLSCLASS_ILLEGAL;
OpenPOWER on IntegriCloud