wstdio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. #ifndef __STDIO_THREADSAFE
  49. #ifdef __BCC__
  50. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  51. asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
  52. RETURNTYPE NAME PARAMS
  53. #else
  54. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  55. strong_alias(NAME,NAME##_unlocked) \
  56. RETURNTYPE NAME PARAMS
  57. #endif
  58. #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
  59. UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
  60. #ifdef __BCC__
  61. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  62. asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
  63. void NAME PARAMS
  64. #else
  65. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  66. strong_alias(NAME,NAME##_unlocked) \
  67. void NAME PARAMS
  68. #endif
  69. #define __STDIO_THREADLOCK_OPENLIST
  70. #define __STDIO_THREADUNLOCK_OPENLIST
  71. #else /* __STDIO_THREADSAFE */
  72. #include <pthread.h>
  73. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  74. RETURNTYPE NAME PARAMS \
  75. { \
  76. RETURNTYPE retval; \
  77. __STDIO_THREADLOCK(STREAM); \
  78. retval = NAME##_unlocked ARGS ; \
  79. __STDIO_THREADUNLOCK(STREAM); \
  80. return retval; \
  81. } \
  82. RETURNTYPE NAME##_unlocked PARAMS
  83. #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
  84. UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
  85. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  86. void NAME PARAMS \
  87. { \
  88. __STDIO_THREADLOCK(stream); \
  89. NAME##_unlocked ARGS ; \
  90. __STDIO_THREADUNLOCK(stream); \
  91. } \
  92. void NAME##_unlocked PARAMS
  93. #define __STDIO_THREADLOCK_OPENLIST \
  94. pthread_mutex_lock(&_stdio_openlist_lock)
  95. #define __STDIO_THREADUNLOCK_OPENLIST \
  96. pthread_mutex_unlock(&_stdio_openlist_lock)
  97. #define __STDIO_THREADTRYLOCK_OPENLIST \
  98. pthread_mutex_trylock(&_stdio_openlist_lock)
  99. #endif /* __STDIO_THREADSAFE */
  100. #ifndef __STDIO_BUFFERS
  101. #error stdio buffers are currently required for wide i/o
  102. #endif
  103. /**********************************************************************/
  104. #ifdef L_fwide
  105. /* TODO: According to SUSv3 should return EBADF if invalid stream. */
  106. int fwide(register FILE *stream, int mode)
  107. {
  108. __STDIO_THREADLOCK(stream);
  109. if (mode && !(stream->modeflags & (__FLAG_WIDE|__FLAG_NARROW))) {
  110. stream->modeflags |= ((mode > 0) ? __FLAG_WIDE : __FLAG_NARROW);
  111. }
  112. mode = (stream->modeflags & __FLAG_WIDE)
  113. - (stream->modeflags & __FLAG_NARROW);
  114. __STDIO_THREADUNLOCK(stream);
  115. return mode;
  116. }
  117. #endif
  118. /**********************************************************************/
  119. #ifdef L_fgetwc
  120. static void munge_stream(register FILE *stream, unsigned char *buf)
  121. {
  122. #ifdef __STDIO_GETC_MACRO
  123. stream->bufgetc =
  124. #endif
  125. #ifdef __STDIO_PUTC_MACRO
  126. stream->bufputc =
  127. #endif
  128. stream->bufpos = stream->bufread = stream->bufend = stream->bufstart = buf;
  129. }
  130. UNLOCKED(wint_t,fgetwc,(register FILE *stream),(stream))
  131. {
  132. wint_t wi;
  133. wchar_t wc[1];
  134. int n;
  135. size_t r;
  136. unsigned char c[1];
  137. unsigned char sbuf[1];
  138. wi = WEOF; /* Prepare for failure. */
  139. if (stream->modeflags & __FLAG_NARROW) {
  140. stream->modeflags |= __FLAG_ERROR;
  141. __set_errno(EBADF);
  142. goto DONE;
  143. }
  144. stream->modeflags |= __FLAG_WIDE;
  145. if (stream->modeflags & __MASK_UNGOT) {/* Any ungetwc()s? */
  146. assert(stream->modeflags & __FLAG_READING);
  147. /* assert( (stream->modeflags & (__FLAG_READING|__FLAG_ERROR)) */
  148. /* == __FLAG_READING); */
  149. if ((((stream->modeflags & __MASK_UNGOT) > 1) || stream->ungot[1])) {
  150. stream->ungot_width[0] = 0; /* Application ungot... */
  151. } else {
  152. stream->ungot_width[0] = stream->ungot_width[1]; /* scanf ungot */
  153. }
  154. wi = stream->ungot[(--stream->modeflags) & __MASK_UNGOT];
  155. stream->ungot[1] = 0;
  156. goto DONE;
  157. }
  158. if (!stream->bufstart) { /* Ugh... stream isn't buffered! */
  159. /* Munge the stream temporarily to use a 1-byte buffer. */
  160. munge_stream(stream, sbuf);
  161. ++stream->bufend;
  162. }
  163. if (stream->state.mask == 0) { /* If last was a complete char */
  164. stream->ungot_width[0] = 0; /* then reset the width. */
  165. }
  166. LOOP:
  167. if ((n = stream->bufread - stream->bufpos) == 0) {
  168. goto FILL_BUFFER;
  169. }
  170. r = mbrtowc(wc, stream->bufpos, n, &stream->state);
  171. if (((ssize_t) r) >= 0) { /* Success... */
  172. if (r == 0) { /* Nul wide char... means 0 byte for us so */
  173. ++r; /* increment r and handle below as single. */
  174. }
  175. stream->bufpos += r;
  176. stream->ungot_width[0] += r;
  177. wi = *wc;
  178. goto DONE;
  179. }
  180. if (r == ((size_t) -2)) {
  181. /* Potentially valid but incomplete and no more buffered. */
  182. stream->bufpos += n; /* Update bufpos for stream. */
  183. stream->ungot_width[0] += n;
  184. FILL_BUFFER:
  185. if (_stdio_fread(c, (size_t) 1, stream) > 0) {
  186. assert(stream->bufpos == stream->bufstart + 1);
  187. *--stream->bufpos = *c; /* Insert byte into buffer. */
  188. goto LOOP;
  189. }
  190. if (!__FERROR(stream)) { /* EOF with no error. */
  191. if (!stream->state.mask) { /* No partially complete wchar. */
  192. goto DONE;
  193. }
  194. /* EOF but partially complete wchar. */
  195. /* TODO: should EILSEQ be set? */
  196. __set_errno(EILSEQ);
  197. }
  198. }
  199. /* If we reach here, either r == ((size_t)-1) and mbrtowc set errno
  200. * to EILSEQ, or r == ((size_t)-2) and stream is in an error state
  201. * or at EOF with a partially complete wchar. Make sure stream's
  202. * error indicator is set. */
  203. stream->modeflags |= __FLAG_ERROR;
  204. DONE:
  205. if (stream->bufstart == sbuf) { /* Need to un-munge the stream. */
  206. munge_stream(stream, NULL);
  207. }
  208. return wi;
  209. }
  210. strong_alias(fgetwc_unlocked,getwc_unlocked);
  211. strong_alias(fgetwc,getwc);
  212. #endif
  213. /**********************************************************************/
  214. #ifdef L_getwchar
  215. UNLOCKED_STREAM(wint_t,getwchar,(void),(),stdin)
  216. {
  217. register FILE *stream = stdin; /* This helps bcc optimize. */
  218. return fgetwc_unlocked(stream);
  219. }
  220. #endif
  221. /**********************************************************************/
  222. #ifdef L_fgetws
  223. UNLOCKED(wchar_t *,fgetws,(wchar_t *__restrict ws, int n,
  224. FILE *__restrict stream),(ws, n, stream))
  225. {
  226. register wchar_t *p = ws;
  227. wint_t wi;
  228. while ((n > 1)
  229. && ((wi = fgetwc_unlocked(stream)) != WEOF)
  230. && ((*p++ = wi) != '\n')
  231. ) {
  232. --n;
  233. }
  234. if (p == ws) {
  235. /* TODO -- should we set errno? */
  236. /* if (n <= 0) { */
  237. /* errno = EINVAL; */
  238. /* } */
  239. return NULL;
  240. }
  241. *p = 0;
  242. return ws;
  243. }
  244. #endif
  245. /**********************************************************************/
  246. #ifdef L_fputwc
  247. UNLOCKED(wint_t,fputwc,(wchar_t wc, FILE *stream),(wc, stream))
  248. {
  249. #if 1
  250. return _wstdio_fwrite(&wc, 1, stream) ? wc : WEOF;
  251. #else
  252. size_t n;
  253. char buf[MB_LEN_MAX];
  254. if (stream->modeflags & __FLAG_NARROW) {
  255. stream->modeflags |= __FLAG_ERROR;
  256. __set_errno(EBADF);
  257. return WEOF;
  258. }
  259. stream->modeflags |= __FLAG_WIDE;
  260. return (((n = wcrtomb(buf, wc, &stream->state)) != ((size_t)-1)) /* !EILSEQ */
  261. && (_stdio_fwrite(buf, n, stream) == n))/* and wrote everything. */
  262. ? wc : WEOF;
  263. #endif
  264. }
  265. strong_alias(fputwc_unlocked,putwc_unlocked);
  266. strong_alias(fputwc,putwc);
  267. #endif
  268. /**********************************************************************/
  269. #ifdef L_putwchar
  270. UNLOCKED_STREAM(wint_t,putwchar,(wchar_t wc),(wc),stdout)
  271. {
  272. register FILE *stream = stdout; /* This helps bcc optimize. */
  273. return fputwc_unlocked(wc, stream);
  274. }
  275. #endif
  276. /**********************************************************************/
  277. #ifdef L_fputws
  278. UNLOCKED(int,fputws,(const wchar_t *__restrict ws,
  279. register FILE *__restrict stream),(ws, stream))
  280. {
  281. #if 1
  282. size_t n = wcslen(ws);
  283. return (_wstdio_fwrite(ws, n, stream) == n) ? 0 : -1;
  284. #else
  285. size_t n;
  286. char buf[64];
  287. if (stream->modeflags & __FLAG_NARROW) {
  288. stream->modeflags |= __FLAG_ERROR;
  289. __set_errno(EBADF);
  290. return -1;
  291. }
  292. stream->modeflags |= __FLAG_WIDE;
  293. while ((n = wcsrtombs(buf, &ws, sizeof(buf), &stream->state)) != 0) {
  294. /* Wasn't an empty wide string. */
  295. if ((n == ((size_t) -1))/* Encoding error! */
  296. || (_stdio_fwrite(buf, n, stream) != n)/* Didn't write everything. */
  297. ) {
  298. return -1;
  299. }
  300. if (!ws) { /* Done? */
  301. break;
  302. }
  303. }
  304. return 1;
  305. #endif
  306. }
  307. #endif
  308. /**********************************************************************/
  309. #ifdef L_ungetwc
  310. /*
  311. * Note: This is the application-callable ungetwc. If wscanf calls this, it
  312. * should also set stream->ungot[1] to 0 if this is the only ungot, as well
  313. * as reset stream->ungot_width[1] for use by _stdio_adjpos().
  314. */
  315. /* Reentrant. */
  316. wint_t ungetwc(wint_t c, register FILE *stream)
  317. {
  318. __STDIO_THREADLOCK(stream);
  319. __stdio_validate_FILE(stream); /* debugging only */
  320. if (stream->modeflags & __FLAG_NARROW) {
  321. stream->modeflags |= __FLAG_ERROR;
  322. c = WEOF;
  323. goto DONE;
  324. }
  325. stream->modeflags |= __FLAG_WIDE;
  326. /* If can't read or c == WEOF or ungot slots already filled, then fail. */
  327. if ((stream->modeflags
  328. & (__MASK_UNGOT2|__FLAG_WRITEONLY
  329. #ifndef __STDIO_AUTO_RW_TRANSITION
  330. |__FLAG_WRITING /* Note: technically no, but yes in spirit */
  331. #endif /* __STDIO_AUTO_RW_TRANSITION */
  332. ))
  333. || ((stream->modeflags & __MASK_UNGOT1) && (stream->ungot[1]))
  334. || (c == WEOF) ) {
  335. c = WEOF;
  336. goto DONE;;
  337. }
  338. /* ungot_width */
  339. #ifdef __STDIO_BUFFERS
  340. #ifdef __STDIO_AUTO_RW_TRANSITION
  341. if (stream->modeflags & __FLAG_WRITING) {
  342. fflush_unlocked(stream); /* Commit any write-buffered chars. */
  343. }
  344. #endif /* __STDIO_AUTO_RW_TRANSITION */
  345. #endif /* __STDIO_BUFFERS */
  346. /* Clear EOF and WRITING flags, and set READING FLAG */
  347. stream->modeflags &= ~(__FLAG_EOF|__FLAG_WRITING);
  348. #ifdef __UCLIBC_MJN3_ONLY__
  349. #warning CONSIDER: Is setting the reading flag after an ungetwc necessary?
  350. #endif /* __UCLIBC_MJN3_ONLY__ */
  351. stream->modeflags |= __FLAG_READING;
  352. stream->ungot[1] = 1; /* Flag as app ungetc call; wscanf fixes up. */
  353. stream->ungot[(stream->modeflags++) & __MASK_UNGOT] = c;
  354. __stdio_validate_FILE(stream); /* debugging only */
  355. DONE:
  356. __STDIO_THREADUNLOCK(stream);
  357. return c;
  358. }
  359. #endif
  360. /**********************************************************************/