wstdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 wcslen __wcslen
  43. #define wcsrtombs __wcsrtombs
  44. #define mbrtowc __mbrtowc
  45. #define _GNU_SOURCE
  46. #include <stdio.h>
  47. #include <wchar.h>
  48. #include <limits.h>
  49. #include <errno.h>
  50. #include <assert.h>
  51. #ifndef __UCLIBC_HAS_THREADS__
  52. #ifdef __BCC__
  53. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  54. asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
  55. RETURNTYPE NAME PARAMS
  56. #else
  57. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  58. strong_alias(NAME,NAME##_unlocked) \
  59. RETURNTYPE NAME PARAMS
  60. #endif
  61. #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
  62. UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
  63. #ifdef __BCC__
  64. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  65. asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
  66. void NAME PARAMS
  67. #else
  68. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  69. strong_alias(NAME,NAME##_unlocked) \
  70. void NAME PARAMS
  71. #endif
  72. #define __STDIO_THREADLOCK_OPENLIST
  73. #define __STDIO_THREADUNLOCK_OPENLIST
  74. #else /* __UCLIBC_HAS_THREADS__ */
  75. #include <pthread.h>
  76. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  77. RETURNTYPE NAME PARAMS \
  78. { \
  79. RETURNTYPE retval; \
  80. __STDIO_THREADLOCK(STREAM); \
  81. retval = NAME##_unlocked ARGS ; \
  82. __STDIO_THREADUNLOCK(STREAM); \
  83. return retval; \
  84. } \
  85. RETURNTYPE NAME##_unlocked PARAMS
  86. #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
  87. UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
  88. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  89. void NAME PARAMS \
  90. { \
  91. __STDIO_THREADLOCK(stream); \
  92. NAME##_unlocked ARGS ; \
  93. __STDIO_THREADUNLOCK(stream); \
  94. } \
  95. void NAME##_unlocked PARAMS
  96. #define __STDIO_THREADLOCK_OPENLIST \
  97. __pthread_mutex_lock(&_stdio_openlist_lock)
  98. #define __STDIO_THREADUNLOCK_OPENLIST \
  99. __pthread_mutex_unlock(&_stdio_openlist_lock)
  100. #define __STDIO_THREADTRYLOCK_OPENLIST \
  101. __pthread_mutex_trylock(&_stdio_openlist_lock)
  102. #endif /* __UCLIBC_HAS_THREADS__ */
  103. #ifndef __STDIO_BUFFERS
  104. #error stdio buffers are currently required for wide i/o
  105. #endif
  106. /**********************************************************************/
  107. #ifdef L_fwide
  108. /* TODO: According to SUSv3 should return EBADF if invalid stream. */
  109. int fwide(register FILE *stream, int mode)
  110. {
  111. __STDIO_THREADLOCK(stream);
  112. if (mode && !(stream->modeflags & (__FLAG_WIDE|__FLAG_NARROW))) {
  113. stream->modeflags |= ((mode > 0) ? __FLAG_WIDE : __FLAG_NARROW);
  114. }
  115. mode = (stream->modeflags & __FLAG_WIDE)
  116. - (stream->modeflags & __FLAG_NARROW);
  117. __STDIO_THREADUNLOCK(stream);
  118. return mode;
  119. }
  120. #endif
  121. /**********************************************************************/
  122. #ifdef L_fgetwc
  123. static void munge_stream(register FILE *stream, unsigned char *buf)
  124. {
  125. #ifdef __STDIO_GETC_MACRO
  126. stream->bufgetc =
  127. #endif
  128. #ifdef __STDIO_PUTC_MACRO
  129. stream->bufputc =
  130. #endif
  131. stream->bufpos = stream->bufread = stream->bufend = stream->bufstart = buf;
  132. }
  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. strong_alias(fgetwc_unlocked,getwc_unlocked);
  214. strong_alias(fgetwc,getwc);
  215. #endif
  216. /**********************************************************************/
  217. #ifdef L_getwchar
  218. UNLOCKED_STREAM(wint_t,getwchar,(void),(),stdin)
  219. {
  220. register FILE *stream = stdin; /* This helps bcc optimize. */
  221. return fgetwc_unlocked(stream);
  222. }
  223. #endif
  224. /**********************************************************************/
  225. #ifdef L_fgetws
  226. UNLOCKED(wchar_t *,fgetws,(wchar_t *__restrict ws, int n,
  227. FILE *__restrict stream),(ws, n, stream))
  228. {
  229. register wchar_t *p = ws;
  230. wint_t wi;
  231. while ((n > 1)
  232. && ((wi = fgetwc_unlocked(stream)) != WEOF)
  233. && ((*p++ = wi) != '\n')
  234. ) {
  235. --n;
  236. }
  237. if (p == ws) {
  238. /* TODO -- should we set errno? */
  239. /* if (n <= 0) { */
  240. /* errno = EINVAL; */
  241. /* } */
  242. return NULL;
  243. }
  244. *p = 0;
  245. return ws;
  246. }
  247. #endif
  248. /**********************************************************************/
  249. #ifdef L_fputwc
  250. UNLOCKED(wint_t,fputwc,(wchar_t wc, FILE *stream),(wc, stream))
  251. {
  252. #if 1
  253. return _wstdio_fwrite(&wc, 1, stream) ? wc : WEOF;
  254. #else
  255. size_t n;
  256. char buf[MB_LEN_MAX];
  257. if (stream->modeflags & __FLAG_NARROW) {
  258. stream->modeflags |= __FLAG_ERROR;
  259. __set_errno(EBADF);
  260. return WEOF;
  261. }
  262. stream->modeflags |= __FLAG_WIDE;
  263. return (((n = wcrtomb(buf, wc, &stream->state)) != ((size_t)-1)) /* !EILSEQ */
  264. && (_stdio_fwrite(buf, n, stream) == n))/* and wrote everything. */
  265. ? wc : WEOF;
  266. #endif
  267. }
  268. strong_alias(fputwc_unlocked,putwc_unlocked);
  269. strong_alias(fputwc,putwc);
  270. #endif
  271. /**********************************************************************/
  272. #ifdef L_putwchar
  273. UNLOCKED_STREAM(wint_t,putwchar,(wchar_t wc),(wc),stdout)
  274. {
  275. register FILE *stream = stdout; /* This helps bcc optimize. */
  276. return fputwc_unlocked(wc, stream);
  277. }
  278. #endif
  279. /**********************************************************************/
  280. #ifdef L_fputws
  281. UNLOCKED(int,fputws,(const wchar_t *__restrict ws,
  282. register FILE *__restrict stream),(ws, stream))
  283. {
  284. #if 1
  285. size_t n = wcslen(ws);
  286. return (_wstdio_fwrite(ws, n, stream) == n) ? 0 : -1;
  287. #else
  288. size_t n;
  289. char buf[64];
  290. if (stream->modeflags & __FLAG_NARROW) {
  291. stream->modeflags |= __FLAG_ERROR;
  292. __set_errno(EBADF);
  293. return -1;
  294. }
  295. stream->modeflags |= __FLAG_WIDE;
  296. while ((n = wcsrtombs(buf, &ws, sizeof(buf), &stream->state)) != 0) {
  297. /* Wasn't an empty wide string. */
  298. if ((n == ((size_t) -1))/* Encoding error! */
  299. || (_stdio_fwrite(buf, n, stream) != n)/* Didn't write everything. */
  300. ) {
  301. return -1;
  302. }
  303. if (!ws) { /* Done? */
  304. break;
  305. }
  306. }
  307. return 1;
  308. #endif
  309. }
  310. #endif
  311. /**********************************************************************/
  312. #ifdef L_ungetwc
  313. /*
  314. * Note: This is the application-callable ungetwc. If wscanf calls this, it
  315. * should also set stream->ungot[1] to 0 if this is the only ungot, as well
  316. * as reset stream->ungot_width[1] for use by _stdio_adjpos().
  317. */
  318. /* Reentrant. */
  319. wint_t ungetwc(wint_t c, register FILE *stream)
  320. {
  321. __STDIO_THREADLOCK(stream);
  322. __stdio_validate_FILE(stream); /* debugging only */
  323. if (stream->modeflags & __FLAG_NARROW) {
  324. stream->modeflags |= __FLAG_ERROR;
  325. c = WEOF;
  326. goto DONE;
  327. }
  328. stream->modeflags |= __FLAG_WIDE;
  329. /* If can't read or c == WEOF or ungot slots already filled, then fail. */
  330. if ((stream->modeflags
  331. & (__MASK_UNGOT2|__FLAG_WRITEONLY
  332. #ifndef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
  333. |__FLAG_WRITING /* Note: technically no, but yes in spirit */
  334. #endif /* __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__ */
  335. ))
  336. || ((stream->modeflags & __MASK_UNGOT1) && (stream->ungot[1]))
  337. || (c == WEOF) ) {
  338. c = WEOF;
  339. goto DONE;;
  340. }
  341. /* ungot_width */
  342. #ifdef __STDIO_BUFFERS
  343. #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
  344. if (stream->modeflags & __FLAG_WRITING) {
  345. fflush_unlocked(stream); /* Commit any write-buffered chars. */
  346. }
  347. #endif /* __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__ */
  348. #endif /* __STDIO_BUFFERS */
  349. /* Clear EOF and WRITING flags, and set READING FLAG */
  350. stream->modeflags &= ~(__FLAG_EOF|__FLAG_WRITING);
  351. #ifdef __UCLIBC_MJN3_ONLY__
  352. #warning CONSIDER: Is setting the reading flag after an ungetwc necessary?
  353. #endif /* __UCLIBC_MJN3_ONLY__ */
  354. stream->modeflags |= __FLAG_READING;
  355. stream->ungot[1] = 1; /* Flag as app ungetc call; wscanf fixes up. */
  356. stream->ungot[(stream->modeflags++) & __MASK_UNGOT] = c;
  357. __stdio_validate_FILE(stream); /* debugging only */
  358. DONE:
  359. __STDIO_THREADUNLOCK(stream);
  360. return c;
  361. }
  362. #endif
  363. /**********************************************************************/