tc.nls.c revision 167465
11556Srgrimes/* $Header: /p/tcsh/cvsroot/tcsh/tc.nls.c,v 3.21 2006/09/26 16:45:30 christos Exp $ */
21556Srgrimes/*
31556Srgrimes * tc.nls.c: NLS handling
41556Srgrimes */
51556Srgrimes/*-
61556Srgrimes * Copyright (c) 1980, 1991 The Regents of the University of California.
71556Srgrimes * All rights reserved.
81556Srgrimes *
91556Srgrimes * Redistribution and use in source and binary forms, with or without
101556Srgrimes * modification, are permitted provided that the following conditions
111556Srgrimes * are met:
121556Srgrimes * 1. Redistributions of source code must retain the above copyright
131556Srgrimes *    notice, this list of conditions and the following disclaimer.
141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151556Srgrimes *    notice, this list of conditions and the following disclaimer in the
161556Srgrimes *    documentation and/or other materials provided with the distribution.
171556Srgrimes * 3. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
321556Srgrimes */
331556Srgrimes#include "sh.h"
341556Srgrimes
351556SrgrimesRCSID("$tcsh: tc.nls.c,v 3.21 2006/09/26 16:45:30 christos Exp $")
361556Srgrimes
371556Srgrimes#ifdef WIDE_STRINGS
3836150Scharnierint
3936150ScharnierNLSWidth(Char c)
4036150Scharnier{
411556Srgrimes# ifdef HAVE_WCWIDTH
4299110Sobrien    int l;
4399110Sobrien    if (c & INVALID_BYTE)
441556Srgrimes	return 1;
4517987Speter    l = wcwidth(c);
4617987Speter    return l >= 0 ? l : 0;
4717987Speter# else
4899762Stjr    return iswprint(c) != 0;
4917987Speter# endif
5017987Speter}
5117987Speter
5217987Speterint
5317987SpeterNLSStringWidth(const Char *s)
5417987Speter{
5517987Speter    int w = 0, l;
5669793Sobrien    Char c;
5717987Speter
5818018Speter    while (*s) {
5917987Speter	c = *s++;
601556Srgrimes#ifdef HAVE_WCWIDTH
611556Srgrimes	if ((l = wcwidth(c)) < 0)
6217987Speter		l = 2;
631556Srgrimes#else
641556Srgrimes	l = iswprint(c) != 0;
6517987Speter#endif
6617987Speter	w += l;
671556Srgrimes    }
681556Srgrimes    return w;
691556Srgrimes}
701556Srgrimes#endif
711556Srgrimes
721556SrgrimesChar *
731556SrgrimesNLSChangeCase(const Char *p, int mode)
741556Srgrimes{
751556Srgrimes    Char c, *n, c2 = 0;
761556Srgrimes    const Char *op = p;
771556Srgrimes
781556Srgrimes    for (; (c = *p) != 0; p++) {
791556Srgrimes        if (mode == 0 && Islower(c)) {
801556Srgrimes	    c2 = Toupper(c);
811556Srgrimes	    break;
821556Srgrimes        } else if (mode && Isupper(c)) {
8328346Ssteve	    c2 = Tolower(c);
841556Srgrimes	    break;
8597659Stjr	}
861556Srgrimes    }
871556Srgrimes    if (!*p)
8838536Scracauer	return 0;
8938950Scracauer    n = Strsave(op);
9038536Scracauer    n[p - op] = c2;
9199762Stjr    return n;
921556Srgrimes}
9320425Ssteve
9490111Simpint
9520425SsteveNLSClassify(Char c, int nocomb)
9690111Simp{
9790111Simp    int w;
9890111Simp    if (c & INVALID_BYTE)
9920425Ssteve	return NLSCLASS_ILLEGAL;
10090111Simp    w = NLSWidth(c);
10120425Ssteve    if ((w > 0 && !(Iscntrl(c) && (c & CHAR) < 0x100)) || (Isprint(c) && !nocomb))
10290111Simp	return w;
10390111Simp    if (Iscntrl(c) && (c & CHAR) < 0x100) {
10490111Simp	if (c == '\n')
10597659Stjr	    return NLSCLASS_NL;
10697659Stjr	if (c == '\t')
10797659Stjr	    return NLSCLASS_TAB;
10897659Stjr	return NLSCLASS_CTRL;
10997659Stjr    }
11097822Stjr#ifdef WIDE_STRINGS
1111556Srgrimes    if (c >= 0x1000000)
1121556Srgrimes	return NLSCLASS_ILLEGAL4;
1131556Srgrimes    if (c >= 0x10000)
1141556Srgrimes	return NLSCLASS_ILLEGAL3;
1151556Srgrimes#endif
1161556Srgrimes    if (c >= 0x100)
1171556Srgrimes	return NLSCLASS_ILLEGAL2;
1181556Srgrimes    return NLSCLASS_ILLEGAL;
1191556Srgrimes}
1201556Srgrimes