wstdio.c 11 KB

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