util.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. util
  5. ----
  6. ### Synopsis
  7. char *unctrl(chtype c);
  8. void filter(void);
  9. void use_env(bool x);
  10. int delay_output(int ms);
  11. int getcchar(const cchar_t *wcval, wchar_t *wch, attr_t *attrs,
  12. short *color_pair, void *opts);
  13. int setcchar(cchar_t *wcval, const wchar_t *wch, const attr_t attrs,
  14. short color_pair, const void *opts);
  15. wchar_t *wunctrl(cchar_t *wc);
  16. int PDC_mbtowc(wchar_t *pwc, const char *s, size_t n);
  17. size_t PDC_mbstowcs(wchar_t *dest, const char *src, size_t n);
  18. size_t PDC_wcstombs(char *dest, const wchar_t *src, size_t n);
  19. ### Description
  20. unctrl() expands the text portion of the chtype c into a printable
  21. string. Control characters are changed to the "^X" notation; others
  22. are passed through. wunctrl() is the wide-character version of the
  23. function.
  24. filter() and use_env() are no-ops in PDCurses.
  25. delay_output() inserts an ms millisecond pause in output.
  26. getcchar() works in two modes: When wch is not NULL, it reads the
  27. cchar_t pointed to by wcval and stores the attributes in attrs, the
  28. color pair in color_pair, and the text in the wide-character string
  29. wch. When wch is NULL, getcchar() merely returns the number of wide
  30. characters in wcval. In either mode, the opts argument is unused.
  31. setcchar constructs a cchar_t at wcval from the wide-character text
  32. at wch, the attributes in attr and the color pair in color_pair. The
  33. opts argument is unused.
  34. Currently, the length returned by getcchar() is always 1 or 0.
  35. Similarly, setcchar() will only take the first wide character from
  36. wch, and ignore any others that it "should" take (i.e., combining
  37. characters). Nor will it correctly handle any character outside the
  38. basic multilingual plane (UCS-2).
  39. ### Return Value
  40. wunctrl() returns NULL on failure. delay_output() always returns OK.
  41. getcchar() returns the number of wide characters wcval points to when
  42. wch is NULL; when it's not, getcchar() returns OK or ERR.
  43. setcchar() returns OK or ERR.
  44. ### Portability
  45. X/Open ncurses NetBSD
  46. unctrl Y Y Y
  47. filter Y Y Y
  48. use_env Y Y Y
  49. delay_output Y Y Y
  50. getcchar Y Y Y
  51. setcchar Y Y Y
  52. wunctrl Y Y Y
  53. PDC_mbtowc - - -
  54. PDC_mbstowcs - - -
  55. PDC_wcstombs - - -
  56. **man-end****************************************************************/
  57. #include <stdlib.h>
  58. #include <string.h>
  59. char *unctrl(chtype c)
  60. {
  61. static char strbuf[3] = {0, 0, 0};
  62. chtype ic;
  63. PDC_LOG(("unctrl() - called\n"));
  64. ic = c & A_CHARTEXT;
  65. if (ic >= 0x20 && ic != 0x7f) /* normal characters */
  66. {
  67. strbuf[0] = (char)ic;
  68. strbuf[1] = '\0';
  69. return strbuf;
  70. }
  71. strbuf[0] = '^'; /* '^' prefix */
  72. if (ic == 0x7f) /* 0x7f == DEL */
  73. strbuf[1] = '?';
  74. else /* other control */
  75. strbuf[1] = (char)(ic + '@');
  76. return strbuf;
  77. }
  78. void filter(void)
  79. {
  80. PDC_LOG(("filter() - called\n"));
  81. }
  82. void use_env(bool x)
  83. {
  84. PDC_LOG(("use_env() - called: x %d\n", x));
  85. }
  86. int delay_output(int ms)
  87. {
  88. PDC_LOG(("delay_output() - called: ms %d\n", ms));
  89. return napms(ms);
  90. }
  91. #ifdef PDC_WIDE
  92. int getcchar(const cchar_t *wcval, wchar_t *wch, attr_t *attrs,
  93. short *color_pair, void *opts)
  94. {
  95. if (!wcval)
  96. return ERR;
  97. if (wch)
  98. {
  99. if (!attrs || !color_pair)
  100. return ERR;
  101. *wch = (*wcval & A_CHARTEXT);
  102. *attrs = (*wcval & (A_ATTRIBUTES & ~A_COLOR));
  103. *color_pair = PAIR_NUMBER(*wcval & A_COLOR);
  104. if (*wch)
  105. *++wch = L'\0';
  106. return OK;
  107. }
  108. else
  109. return ((*wcval & A_CHARTEXT) != L'\0');
  110. }
  111. int setcchar(cchar_t *wcval, const wchar_t *wch, const attr_t attrs,
  112. short color_pair, const void *opts)
  113. {
  114. if (!wcval || !wch)
  115. return ERR;
  116. *wcval = *wch | attrs | COLOR_PAIR(color_pair);
  117. return OK;
  118. }
  119. wchar_t *wunctrl(cchar_t *wc)
  120. {
  121. static wchar_t strbuf[3] = {0, 0, 0};
  122. cchar_t ic;
  123. PDC_LOG(("wunctrl() - called\n"));
  124. if (!wc)
  125. return NULL;
  126. ic = *wc & A_CHARTEXT;
  127. if (ic >= 0x20 && ic != 0x7f) /* normal characters */
  128. {
  129. strbuf[0] = (wchar_t)ic;
  130. strbuf[1] = L'\0';
  131. return strbuf;
  132. }
  133. strbuf[0] = '^'; /* '^' prefix */
  134. if (ic == 0x7f) /* 0x7f == DEL */
  135. strbuf[1] = '?';
  136. else /* other control */
  137. strbuf[1] = (wchar_t)(ic + '@');
  138. return strbuf;
  139. }
  140. int PDC_mbtowc(wchar_t *pwc, const char *s, size_t n)
  141. {
  142. # ifdef PDC_FORCE_UTF8
  143. wchar_t key;
  144. int i = -1;
  145. const unsigned char *string;
  146. if (!s || (n < 1))
  147. return -1;
  148. if (!*s)
  149. return 0;
  150. string = (const unsigned char *)s;
  151. key = string[0];
  152. /* Simplistic UTF-8 decoder -- only does the BMP, minimal validation */
  153. if (key & 0x80)
  154. {
  155. if ((key & 0xe0) == 0xc0)
  156. {
  157. if (1 < n)
  158. {
  159. key = ((key & 0x1f) << 6) | (string[1] & 0x3f);
  160. i = 2;
  161. }
  162. }
  163. else if ((key & 0xe0) == 0xe0)
  164. {
  165. if (2 < n)
  166. {
  167. key = ((key & 0x0f) << 12) | ((string[1] & 0x3f) << 6) |
  168. (string[2] & 0x3f);
  169. i = 3;
  170. }
  171. }
  172. }
  173. else
  174. i = 1;
  175. if (i)
  176. *pwc = key;
  177. return i;
  178. # else
  179. return mbtowc(pwc, s, n);
  180. # endif
  181. }
  182. size_t PDC_mbstowcs(wchar_t *dest, const char *src, size_t n)
  183. {
  184. # ifdef PDC_FORCE_UTF8
  185. size_t i = 0, len;
  186. if (!src || !dest)
  187. return 0;
  188. len = strlen(src);
  189. while (*src && i < n)
  190. {
  191. int retval = PDC_mbtowc(dest + i, src, len);
  192. if (retval < 1)
  193. return -1;
  194. src += retval;
  195. len -= retval;
  196. i++;
  197. }
  198. # else
  199. size_t i = mbstowcs(dest, src, n);
  200. # endif
  201. dest[i] = 0;
  202. return i;
  203. }
  204. size_t PDC_wcstombs(char *dest, const wchar_t *src, size_t n)
  205. {
  206. # ifdef PDC_FORCE_UTF8
  207. size_t i = 0;
  208. if (!src || !dest)
  209. return 0;
  210. while (*src && i < n)
  211. {
  212. chtype code = *src++;
  213. if (code < 0x80)
  214. {
  215. dest[i] = code;
  216. i++;
  217. }
  218. else
  219. if (code < 0x800)
  220. {
  221. dest[i] = ((code & 0x07c0) >> 6) | 0xc0;
  222. dest[i + 1] = (code & 0x003f) | 0x80;
  223. i += 2;
  224. }
  225. else
  226. {
  227. dest[i] = ((code & 0xf000) >> 12) | 0xe0;
  228. dest[i + 1] = ((code & 0x0fc0) >> 6) | 0x80;
  229. dest[i + 2] = (code & 0x003f) | 0x80;
  230. i += 3;
  231. }
  232. }
  233. # else
  234. size_t i = wcstombs(dest, src, n);
  235. # endif
  236. dest[i] = '\0';
  237. return i;
  238. }
  239. #endif