wstdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  18. *
  19. * Besides uClibc, I'm using this code in my libc for elks, which is
  20. * a 16-bit environment with a fairly limited compiler. It would make
  21. * things much easier for me if this file isn't modified unnecessarily.
  22. * In particular, please put any new or replacement functions somewhere
  23. * else, and modify the makefile to use your version instead.
  24. * Thanks. Manuel
  25. *
  26. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  27. /* Nov 21, 2002
  28. *
  29. * Reimplement fputwc and fputws in terms of internal function _wstdio_fwrite.
  30. */
  31. /*
  32. * ANSI/ISO C99 says
  33. 9 Although both text and binary wide­oriented streams are conceptually sequences of wide
  34. characters, the external file associated with a wide­oriented stream is a sequence of
  35. multibyte characters, generalized as follows:
  36. --- Multibyte encodings within files may contain embedded null bytes (unlike multibyte
  37. encodings valid for use internal to the program).
  38. --- A file need not begin nor end in the initial shift state. 225)
  39. * How do we deal with this?
  40. * Should auto_wr_transition init the mbstate object?
  41. */
  42. #define _GNU_SOURCE
  43. #include <stdio.h>
  44. #include <wchar.h>
  45. #include <limits.h>
  46. #include <errno.h>
  47. #include <assert.h>
  48. libc_hidden_proto(fgetwc_unlocked)
  49. libc_hidden_proto(fputwc_unlocked)
  50. #ifndef __UCLIBC_HAS_THREADS__
  51. #ifdef __BCC__
  52. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  53. asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
  54. RETURNTYPE NAME PARAMS
  55. #else
  56. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  57. strong_alias(NAME,NAME##_unlocked) \
  58. RETURNTYPE NAME PARAMS
  59. #endif
  60. #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
  61. UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
  62. #ifdef __BCC__
  63. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  64. asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
  65. void NAME PARAMS
  66. #else
  67. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  68. strong_alias(NAME,NAME##_unlocked) \
  69. void NAME PARAMS
  70. #endif
  71. #define __STDIO_THREADLOCK_OPENLIST
  72. #define __STDIO_THREADUNLOCK_OPENLIST
  73. #else /* __UCLIBC_HAS_THREADS__ */
  74. #include <pthread.h>
  75. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  76. RETURNTYPE NAME PARAMS \
  77. { \
  78. RETURNTYPE retval; \
  79. __STDIO_THREADLOCK(STREAM); \
  80. retval = NAME##_unlocked ARGS ; \
  81. __STDIO_THREADUNLOCK(STREAM); \
  82. return retval; \
  83. } \
  84. RETURNTYPE NAME##_unlocked PARAMS
  85. #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
  86. UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
  87. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  88. void NAME PARAMS \
  89. { \
  90. __STDIO_THREADLOCK(stream); \
  91. NAME##_unlocked ARGS ; \
  92. __STDIO_THREADUNLOCK(stream); \
  93. } \
  94. void NAME##_unlocked PARAMS
  95. #define __STDIO_THREADLOCK_OPENLIST \
  96. __pthread_mutex_lock(&_stdio_openlist_lock)
  97. #define __STDIO_THREADUNLOCK_OPENLIST \
  98. __pthread_mutex_unlock(&_stdio_openlist_lock)
  99. #define __STDIO_THREADTRYLOCK_OPENLIST \
  100. __pthread_mutex_trylock(&_stdio_openlist_lock)
  101. #endif /* __UCLIBC_HAS_THREADS__ */
  102. #ifndef __STDIO_BUFFERS
  103. #error stdio buffers are currently required for wide i/o
  104. #endif
  105. /**********************************************************************/
  106. #ifdef L_fwide
  107. /* TODO: According to SUSv3 should return EBADF if invalid stream. */
  108. int fwide(register FILE *stream, int mode)
  109. {
  110. __STDIO_THREADLOCK(stream);
  111. if (mode && !(stream->modeflags & (__FLAG_WIDE|__FLAG_NARROW))) {
  112. stream->modeflags |= ((mode > 0) ? __FLAG_WIDE : __FLAG_NARROW);
  113. }
  114. mode = (stream->modeflags & __FLAG_WIDE)
  115. - (stream->modeflags & __FLAG_NARROW);
  116. __STDIO_THREADUNLOCK(stream);
  117. return mode;
  118. }
  119. #endif
  120. /**********************************************************************/
  121. #ifdef L_fgetwc
  122. static void munge_stream(register FILE *stream, unsigned char *buf)
  123. {
  124. #ifdef __STDIO_GETC_MACRO
  125. stream->bufgetc =
  126. #endif
  127. #ifdef __STDIO_PUTC_MACRO
  128. stream->bufputc =
  129. #endif
  130. stream->bufpos = stream->bufread = stream->bufend = stream->bufstart = buf;
  131. }
  132. libc_hidden_proto(mbrtowc)
  133. UNLOCKED(wint_t,fgetwc,(register FILE *stream),(stream))
  134. {
  135. wint_t wi;
  136. wchar_t wc[1];
  137. int n;
  138. size_t r;
  139. unsigned char c[1];
  140. unsigned char sbuf[1];
  141. wi = WEOF; /* Prepare for failure. */
  142. if (stream->modeflags & __FLAG_NARROW) {
  143. stream->modeflags |= __FLAG_ERROR;
  144. __set_errno(EBADF);
  145. goto DONE;
  146. }
  147. stream->modeflags |= __FLAG_WIDE;
  148. if (stream->modeflags & __MASK_UNGOT) {/* Any ungetwc()s? */
  149. assert(stream->modeflags & __FLAG_READING);
  150. /* assert( (stream->modeflags & (__FLAG_READING|__FLAG_ERROR)) */
  151. /* == __FLAG_READING); */
  152. if ((((stream->modeflags & __MASK_UNGOT) > 1) || stream->ungot[1])) {
  153. stream->ungot_width[0] = 0; /* Application ungot... */
  154. } else {
  155. stream->ungot_width[0] = stream->ungot_width[1]; /* scanf ungot */
  156. }
  157. wi = stream->ungot[(--stream->modeflags) & __MASK_UNGOT];
  158. stream->ungot[1] = 0;
  159. goto DONE;
  160. }
  161. if (!stream->bufstart) { /* Ugh... stream isn't buffered! */
  162. /* Munge the stream temporarily to use a 1-byte buffer. */
  163. munge_stream(stream, sbuf);
  164. ++stream->bufend;
  165. }
  166. if (stream->state.mask == 0) { /* If last was a complete char */
  167. stream->ungot_width[0] = 0; /* then reset the width. */
  168. }
  169. LOOP:
  170. if ((n = stream->bufread - stream->bufpos) == 0) {
  171. goto FILL_BUFFER;
  172. }
  173. r = mbrtowc(wc, stream->bufpos, n, &stream->state);
  174. if (((ssize_t) r) >= 0) { /* Success... */
  175. if (r == 0) { /* Nul wide char... means 0 byte for us so */
  176. ++r; /* increment r and handle below as single. */
  177. }
  178. stream->bufpos += r;
  179. stream->ungot_width[0] += r;
  180. wi = *wc;
  181. goto DONE;
  182. }
  183. if (r == ((size_t) -2)) {
  184. /* Potentially valid but incomplete and no more buffered. */
  185. stream->bufpos += n; /* Update bufpos for stream. */
  186. stream->ungot_width[0] += n;
  187. FILL_BUFFER:
  188. if (_stdio_fread(c, (size_t) 1, stream) > 0) {
  189. assert(stream->bufpos == stream->bufstart + 1);
  190. *--stream->bufpos = *c; /* Insert byte into buffer. */
  191. goto LOOP;
  192. }
  193. if (!__FERROR(stream)) { /* EOF with no error. */
  194. if (!stream->state.mask) { /* No partially complete wchar. */
  195. goto DONE;
  196. }
  197. /* EOF but partially complete wchar. */
  198. /* TODO: should EILSEQ be set? */
  199. __set_errno(EILSEQ);
  200. }
  201. }
  202. /* If we reach here, either r == ((size_t)-1) and mbrtowc set errno
  203. * to EILSEQ, or r == ((size_t)-2) and stream is in an error state
  204. * or at EOF with a partially complete wchar. Make sure stream's
  205. * error indicator is set. */
  206. stream->modeflags |= __FLAG_ERROR;
  207. DONE:
  208. if (stream->bufstart == sbuf) { /* Need to un-munge the stream. */
  209. munge_stream(stream, NULL);
  210. }
  211. return wi;
  212. }
  213. libc_hidden_def(fgetwc_unlocked)
  214. strong_alias(fgetwc_unlocked,getwc_unlocked)
  215. strong_alias(fgetwc,getwc)
  216. #endif
  217. /**********************************************************************/
  218. #ifdef L_getwchar
  219. UNLOCKED_STREAM(wint_t,getwchar,(void),(),stdin)
  220. {
  221. register FILE *stream = stdin; /* This helps bcc optimize. */
  222. return fgetwc_unlocked(stream);
  223. }
  224. #endif
  225. /**********************************************************************/
  226. #ifdef L_fgetws
  227. UNLOCKED(wchar_t *,fgetws,(wchar_t *__restrict ws, int n,
  228. FILE *__restrict stream),(ws, n, stream))
  229. {
  230. register wchar_t *p = ws;
  231. wint_t wi;
  232. while ((n > 1)
  233. && ((wi = fgetwc_unlocked(stream)) != WEOF)
  234. && ((*p++ = wi) != '\n')
  235. ) {
  236. --n;
  237. }
  238. if (p == ws) {
  239. /* TODO -- should we set errno? */
  240. /* if (n <= 0) { */
  241. /* errno = EINVAL; */
  242. /* } */
  243. return NULL;
  244. }
  245. *p = 0;
  246. return ws;
  247. }
  248. #endif
  249. /**********************************************************************/
  250. #ifdef L_fputwc
  251. /* libc_hidden_proto(wcrtomb) */
  252. UNLOCKED(wint_t,fputwc,(wchar_t wc, FILE *stream),(wc, stream))
  253. {
  254. #if 1
  255. return _wstdio_fwrite(&wc, 1, stream) ? wc : WEOF;
  256. #else
  257. size_t n;
  258. char buf[MB_LEN_MAX];
  259. if (stream->modeflags & __FLAG_NARROW) {
  260. stream->modeflags |= __FLAG_ERROR;
  261. __set_errno(EBADF);
  262. return WEOF;
  263. }
  264. stream->modeflags |= __FLAG_WIDE;
  265. return (((n = wcrtomb(buf, wc, &stream->state)) != ((size_t)-1)) /* !EILSEQ */
  266. && (_stdio_fwrite(buf, n, stream) == n))/* and wrote everything. */
  267. ? wc : WEOF;
  268. #endif
  269. }
  270. libc_hidden_def(fputwc_unlocked)
  271. strong_alias(fputwc_unlocked,putwc_unlocked)
  272. strong_alias(fputwc,putwc)
  273. #endif
  274. /**********************************************************************/
  275. #ifdef L_putwchar
  276. UNLOCKED_STREAM(wint_t,putwchar,(wchar_t wc),(wc),stdout)
  277. {
  278. register FILE *stream = stdout; /* This helps bcc optimize. */
  279. return fputwc_unlocked(wc, stream);
  280. }
  281. #endif
  282. /**********************************************************************/
  283. #ifdef L_fputws
  284. libc_hidden_proto(wcslen)
  285. /* libc_hidden_proto(wcsrtombs) */
  286. UNLOCKED(int,fputws,(const wchar_t *__restrict ws,
  287. register FILE *__restrict stream),(ws, stream))
  288. {
  289. #if 1
  290. size_t n = wcslen(ws);
  291. return (_wstdio_fwrite(ws, n, stream) == n) ? 0 : -1;
  292. #else
  293. size_t n;
  294. char buf[64];
  295. if (stream->modeflags & __FLAG_NARROW) {
  296. stream->modeflags |= __FLAG_ERROR;
  297. __set_errno(EBADF);
  298. return -1;
  299. }
  300. stream->modeflags |= __FLAG_WIDE;
  301. while ((n = wcsrtombs(buf, &ws, sizeof(buf), &stream->state)) != 0) {
  302. /* Wasn't an empty wide string. */
  303. if ((n == ((size_t) -1))/* Encoding error! */
  304. || (_stdio_fwrite(buf, n, stream) != n)/* Didn't write everything. */
  305. ) {
  306. return -1;
  307. }
  308. if (!ws) { /* Done? */
  309. break;
  310. }
  311. }
  312. return 1;
  313. #endif
  314. }
  315. #endif
  316. /**********************************************************************/
  317. #ifdef L_ungetwc
  318. /*
  319. * Note: This is the application-callable ungetwc. If wscanf calls this, it
  320. * should also set stream->ungot[1] to 0 if this is the only ungot, as well
  321. * as reset stream->ungot_width[1] for use by _stdio_adjpos().
  322. */
  323. /* Reentrant. */
  324. libc_hidden_proto(fflush_unlocked)
  325. wint_t ungetwc(wint_t c, register FILE *stream)
  326. {
  327. __STDIO_THREADLOCK(stream);
  328. __stdio_validate_FILE(stream); /* debugging only */
  329. if (stream->modeflags & __FLAG_NARROW) {
  330. stream->modeflags |= __FLAG_ERROR;
  331. c = WEOF;
  332. goto DONE;
  333. }
  334. stream->modeflags |= __FLAG_WIDE;
  335. /* If can't read or c == WEOF or ungot slots already filled, then fail. */
  336. if ((stream->modeflags
  337. & (__MASK_UNGOT2|__FLAG_WRITEONLY
  338. #ifndef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
  339. |__FLAG_WRITING /* Note: technically no, but yes in spirit */
  340. #endif /* __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__ */
  341. ))
  342. || ((stream->modeflags & __MASK_UNGOT1) && (stream->ungot[1]))
  343. || (c == WEOF) ) {
  344. c = WEOF;
  345. goto DONE;;
  346. }
  347. /* ungot_width */
  348. #ifdef __STDIO_BUFFERS
  349. #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
  350. if (stream->modeflags & __FLAG_WRITING) {
  351. fflush_unlocked(stream); /* Commit any write-buffered chars. */
  352. }
  353. #endif /* __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__ */
  354. #endif /* __STDIO_BUFFERS */
  355. /* Clear EOF and WRITING flags, and set READING FLAG */
  356. stream->modeflags &= ~(__FLAG_EOF|__FLAG_WRITING);
  357. #ifdef __UCLIBC_MJN3_ONLY__
  358. #warning CONSIDER: Is setting the reading flag after an ungetwc necessary?
  359. #endif /* __UCLIBC_MJN3_ONLY__ */
  360. stream->modeflags |= __FLAG_READING;
  361. stream->ungot[1] = 1; /* Flag as app ungetc call; wscanf fixes up. */
  362. stream->ungot[(stream->modeflags++) & __MASK_UNGOT] = c;
  363. __stdio_validate_FILE(stream); /* debugging only */
  364. DONE:
  365. __STDIO_THREADUNLOCK(stream);
  366. return c;
  367. }
  368. #endif
  369. /**********************************************************************/