1275970Scy/* $Header: /p/tcsh/cvsroot/tcsh/tc.nls.c,v 3.23 2010/02/12 22:17:20 christos Exp $ */
2275970Scy/*
3275970Scy * tc.nls.c: NLS handling
4275970Scy */
5275970Scy/*-
6275970Scy * Copyright (c) 1980, 1991 The Regents of the University of California.
7275970Scy * All rights reserved.
8275970Scy *
9275970Scy * Redistribution and use in source and binary forms, with or without
10275970Scy * modification, are permitted provided that the following conditions
11275970Scy * are met:
12275970Scy * 1. Redistributions of source code must retain the above copyright
13275970Scy *    notice, this list of conditions and the following disclaimer.
14275970Scy * 2. Redistributions in binary form must reproduce the above copyright
15275970Scy *    notice, this list of conditions and the following disclaimer in the
16275970Scy *    documentation and/or other materials provided with the distribution.
17275970Scy * 3. Neither the name of the University nor the names of its contributors
18275970Scy *    may be used to endorse or promote products derived from this software
19275970Scy *    without specific prior written permission.
20275970Scy *
21275970Scy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22275970Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23275970Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24275970Scy * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25275970Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26275970Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27275970Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28275970Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29275970Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30275970Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31275970Scy * SUCH DAMAGE.
32275970Scy */
33275970Scy#include "sh.h"
34275970Scy
35275970ScyRCSID("$tcsh: tc.nls.c,v 3.23 2010/02/12 22:17:20 christos Exp $")
36294569Sdelphij
37275970Scy
38275970Scy#ifdef WIDE_STRINGS
39275970Scy# ifdef HAVE_WCWIDTH
40275970Scy#  ifdef UTF16_STRINGS
41275970Scyint
42275970Scyxwcwidth (wint_t wchar)
43275970Scy{
44275970Scy  wchar_t ws[2];
45275970Scy
46275970Scy  if (wchar <= 0xffff)
47275970Scy    return wcwidth ((wchar_t) wchar);
48275970Scy  /* UTF-16 systems can't handle these values directly in calls to wcwidth.
49275970Scy     However, they can handle them as surrogate pairs in calls to wcswidth.
50275970Scy     What we do here is to convert UTF-32 values >= 0x10000 into surrogate
51275970Scy     pairs and compute the width by calling wcswidth. */
52275970Scy  wchar -= 0x10000;
53275970Scy  ws[0] = 0xd800 | (wchar >> 10);
54275970Scy  ws[1] = 0xdc00 | (wchar & 0x3ff);
55275970Scy  return wcswidth (ws, 2);
56275970Scy}
57275970Scy#  else
58275970Scy#define xwcwidth wcwidth
59275970Scy#  endif /* !UTF16_STRINGS */
60275970Scy# endif /* HAVE_WCWIDTH */
61275970Scy
62275970Scyint
63275970ScyNLSWidth(Char c)
64275970Scy{
65275970Scy# ifdef HAVE_WCWIDTH
66275970Scy    int l;
67275970Scy    if (c & INVALID_BYTE)
68275970Scy	return 1;
69275970Scy    l = xwcwidth((wchar_t) c);
70275970Scy    return l >= 0 ? l : 0;
71275970Scy# else
72275970Scy    return iswprint(c) != 0;
73275970Scy# endif
74275970Scy}
75275970Scy
76275970Scyint
77275970ScyNLSStringWidth(const Char *s)
78275970Scy{
79275970Scy    int w = 0, l;
80275970Scy    Char c;
81275970Scy
82275970Scy    while (*s) {
83275970Scy	c = *s++;
84275970Scy#ifdef HAVE_WCWIDTH
85275970Scy	if ((l = xwcwidth((wchar_t) c)) < 0)
86275970Scy		l = 2;
87275970Scy#else
88275970Scy	l = iswprint(c) != 0;
89275970Scy#endif
90275970Scy	w += l;
91275970Scy    }
92275970Scy    return w;
93275970Scy}
94275970Scy#endif
95275970Scy
96294569SdelphijChar *
97275970ScyNLSChangeCase(const Char *p, int mode)
98275970Scy{
99275970Scy    Char c, *n, c2 = 0;
100275970Scy    const Char *op = p;
101275970Scy
102275970Scy    for (; (c = *p) != 0; p++) {
103275970Scy        if (mode == 0 && Islower(c)) {
104275970Scy	    c2 = Toupper(c);
105294569Sdelphij	    break;
106294569Sdelphij        } else if (mode && Isupper(c)) {
107294569Sdelphij	    c2 = Tolower(c);
108294569Sdelphij	    break;
109294569Sdelphij	}
110294569Sdelphij    }
111294569Sdelphij    if (!*p)
112294569Sdelphij	return 0;
113294569Sdelphij    n = Strsave(op);
114275970Scy    n[p - op] = c2;
115275970Scy    return n;
116275970Scy}
117275970Scy
118275970Scyint
119275970ScyNLSClassify(Char c, int nocomb)
120275970Scy{
121275970Scy    int w;
122275970Scy    if (c & INVALID_BYTE)
123275970Scy	return NLSCLASS_ILLEGAL;
124275970Scy    w = NLSWidth(c);
125275970Scy    if ((w > 0 && !(Iscntrl(c) && (c & CHAR) < 0x100)) || (Isprint(c) && !nocomb))
126275970Scy	return w;
127275970Scy    if (Iscntrl(c) && (c & CHAR) < 0x100) {
128275970Scy	if (c == '\n')
129275970Scy	    return NLSCLASS_NL;
130275970Scy	if (c == '\t')
131275970Scy	    return NLSCLASS_TAB;
132275970Scy	return NLSCLASS_CTRL;
133275970Scy    }
134275970Scy#ifdef WIDE_STRINGS
135275970Scy    if (c >= 0x1000000)
136275970Scy	return NLSCLASS_ILLEGAL4;
137275970Scy    if (c >= 0x10000)
138275970Scy	return NLSCLASS_ILLEGAL3;
139275970Scy#endif
140275970Scy    if (c >= 0x100)
141275970Scy	return NLSCLASS_ILLEGAL2;
142275970Scy    return NLSCLASS_ILLEGAL;
143275970Scy}
144275970Scy