/contrib/tcsh/tc.nls.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 143 lines · 97 code · 10 blank · 36 comment · 30 complexity · f67c8c23f5202bebd630f2a7fdc7dd66 MD5 · raw file

  1. /* $Header: /p/tcsh/cvsroot/tcsh/tc.nls.c,v 3.23 2010/02/12 22:17:20 christos Exp $ */
  2. /*
  3. * tc.nls.c: NLS handling
  4. */
  5. /*-
  6. * Copyright (c) 1980, 1991 The Regents of the University of California.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "sh.h"
  34. RCSID("$tcsh: tc.nls.c,v 3.23 2010/02/12 22:17:20 christos Exp $")
  35. #ifdef WIDE_STRINGS
  36. # ifdef HAVE_WCWIDTH
  37. # ifdef UTF16_STRINGS
  38. int
  39. xwcwidth (wint_t wchar)
  40. {
  41. wchar_t ws[2];
  42. if (wchar <= 0xffff)
  43. return wcwidth ((wchar_t) wchar);
  44. /* UTF-16 systems can't handle these values directly in calls to wcwidth.
  45. However, they can handle them as surrogate pairs in calls to wcswidth.
  46. What we do here is to convert UTF-32 values >= 0x10000 into surrogate
  47. pairs and compute the width by calling wcswidth. */
  48. wchar -= 0x10000;
  49. ws[0] = 0xd800 | (wchar >> 10);
  50. ws[1] = 0xdc00 | (wchar & 0x3ff);
  51. return wcswidth (ws, 2);
  52. }
  53. # else
  54. #define xwcwidth wcwidth
  55. # endif /* !UTF16_STRINGS */
  56. # endif /* HAVE_WCWIDTH */
  57. int
  58. NLSWidth(Char c)
  59. {
  60. # ifdef HAVE_WCWIDTH
  61. int l;
  62. if (c & INVALID_BYTE)
  63. return 1;
  64. l = xwcwidth((wchar_t) c);
  65. return l >= 0 ? l : 0;
  66. # else
  67. return iswprint(c) != 0;
  68. # endif
  69. }
  70. int
  71. NLSStringWidth(const Char *s)
  72. {
  73. int w = 0, l;
  74. Char c;
  75. while (*s) {
  76. c = *s++;
  77. #ifdef HAVE_WCWIDTH
  78. if ((l = xwcwidth((wchar_t) c)) < 0)
  79. l = 2;
  80. #else
  81. l = iswprint(c) != 0;
  82. #endif
  83. w += l;
  84. }
  85. return w;
  86. }
  87. #endif
  88. Char *
  89. NLSChangeCase(const Char *p, int mode)
  90. {
  91. Char c, *n, c2 = 0;
  92. const Char *op = p;
  93. for (; (c = *p) != 0; p++) {
  94. if (mode == 0 && Islower(c)) {
  95. c2 = Toupper(c);
  96. break;
  97. } else if (mode && Isupper(c)) {
  98. c2 = Tolower(c);
  99. break;
  100. }
  101. }
  102. if (!*p)
  103. return 0;
  104. n = Strsave(op);
  105. n[p - op] = c2;
  106. return n;
  107. }
  108. int
  109. NLSClassify(Char c, int nocomb)
  110. {
  111. int w;
  112. if (c & INVALID_BYTE)
  113. return NLSCLASS_ILLEGAL;
  114. w = NLSWidth(c);
  115. if ((w > 0 && !(Iscntrl(c) && (c & CHAR) < 0x100)) || (Isprint(c) && !nocomb))
  116. return w;
  117. if (Iscntrl(c) && (c & CHAR) < 0x100) {
  118. if (c == '\n')
  119. return NLSCLASS_NL;
  120. if (c == '\t')
  121. return NLSCLASS_TAB;
  122. return NLSCLASS_CTRL;
  123. }
  124. #ifdef WIDE_STRINGS
  125. if (c >= 0x1000000)
  126. return NLSCLASS_ILLEGAL4;
  127. if (c >= 0x10000)
  128. return NLSCLASS_ILLEGAL3;
  129. #endif
  130. if (c >= 0x100)
  131. return NLSCLASS_ILLEGAL2;
  132. return NLSCLASS_ILLEGAL;
  133. }