Subversion Repositories Kolibri OS

Rev

Rev 4872 | Rev 4921 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 1990, 2007 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  *      %W% (UofMD/Berkeley) %G%
  18.  */
  19.  
  20. /*
  21.  * Information local to this implementation of stdio,
  22.  * in particular, macros and private variables.
  23.  */
  24.  
  25. #include <_ansi.h>
  26. #include <reent.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <stdio.h>
  31. #ifdef __SCLE
  32. # include <io.h>
  33. #endif
  34.  
  35.  
  36. extern u_char *_EXFUN(__sccl, (char *, u_char *fmt));
  37. extern int    _EXFUN(__svfscanf_r,(struct _reent *,FILE *, _CONST char *,va_list));
  38. extern int    _EXFUN(__ssvfscanf_r,(struct _reent *,FILE *, _CONST char *,va_list));
  39. extern int    _EXFUN(__svfiscanf_r,(struct _reent *,FILE *, _CONST char *,va_list));
  40. extern int    _EXFUN(__ssvfiscanf_r,(struct _reent *,FILE *, _CONST char *,va_list));
  41. extern int    _EXFUN(__svfwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list));
  42. extern int    _EXFUN(__ssvfwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list));
  43. extern int    _EXFUN(__svfiwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list));
  44. extern int    _EXFUN(__ssvfiwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list));
  45. int           _EXFUN(_svfprintf_r,(struct _reent *, FILE *, const char *,
  46.                                   va_list)
  47.                                 _ATTRIBUTE ((__format__ (__printf__, 3, 0))));
  48. int           _EXFUN(_svfiprintf_r,(struct _reent *, FILE *, const char *,
  49.                                   va_list)
  50.                                 _ATTRIBUTE ((__format__ (__printf__, 3, 0))));
  51. int           _EXFUN(_svfwprintf_r,(struct _reent *, FILE *, const wchar_t *,
  52.                                   va_list));
  53. int           _EXFUN(_svfiwprintf_r,(struct _reent *, FILE *, const wchar_t *,
  54.                                   va_list));
  55. extern FILE  *_EXFUN(__sfp,(struct _reent *));
  56. extern int    _EXFUN(__sflags,(struct _reent *,_CONST char*, int*));
  57. extern int    _EXFUN(__sflush_r,(struct _reent *,FILE *));
  58. extern int    _EXFUN(__srefill_r,(struct _reent *,FILE *));
  59. extern _READ_WRITE_RETURN_TYPE _EXFUN(__sread,(struct _reent *, void *, char *,
  60.                                                int));
  61. extern _READ_WRITE_RETURN_TYPE _EXFUN(__seofread,(struct _reent *, void *,
  62.                                                   char *, int));
  63. extern _READ_WRITE_RETURN_TYPE _EXFUN(__swrite,(struct _reent *, void *,
  64.                                                 const char *, int));
  65. extern _fpos_t _EXFUN(__sseek,(struct _reent *, void *, _fpos_t, int));
  66. extern int    _EXFUN(__sclose,(struct _reent *, void *));
  67. extern int    _EXFUN(__stextmode,(int));
  68. extern _VOID   _EXFUN(__sinit,(struct _reent *));
  69. extern _VOID   _EXFUN(_cleanup_r,(struct _reent *));
  70. extern _VOID   _EXFUN(__smakebuf_r,(struct _reent *, FILE *));
  71. extern int    _EXFUN(_fwalk,(struct _reent *, int (*)(FILE *)));
  72. extern int    _EXFUN(_fwalk_reent,(struct _reent *, int (*)(struct _reent *, FILE *)));
  73. struct _glue * _EXFUN(__sfmoreglue,(struct _reent *,int n));
  74. extern int _EXFUN(__submore, (struct _reent *, FILE *));
  75.  
  76. #ifdef __LARGE64_FILES
  77. extern _fpos64_t _EXFUN(__sseek64,(struct _reent *, void *, _fpos64_t, int));
  78. extern _READ_WRITE_RETURN_TYPE _EXFUN(__swrite64,(struct _reent *, void *,
  79.                                                   const char *, int));
  80. #endif
  81.  
  82. /* Called by the main entry point fns to ensure stdio has been initialized.  */
  83.  
  84. #ifdef _REENT_SMALL
  85. #define CHECK_INIT(ptr, fp) \
  86.   do                                            \
  87.     {                                           \
  88.       if ((ptr) && !(ptr)->__sdidinit)          \
  89.         __sinit (ptr);                          \
  90.       if ((fp) == (FILE *)&__sf_fake_stdin)     \
  91.         (fp) = _stdin_r(ptr);                   \
  92.       else if ((fp) == (FILE *)&__sf_fake_stdout) \
  93.         (fp) = _stdout_r(ptr);                  \
  94.       else if ((fp) == (FILE *)&__sf_fake_stderr) \
  95.         (fp) = _stderr_r(ptr);                  \
  96.     }                                           \
  97.   while (0)
  98. #else /* !_REENT_SMALL   */
  99. #define CHECK_INIT(ptr, fp) \
  100.   do                                            \
  101.     {                                           \
  102.       if ((ptr) && !(ptr)->__sdidinit)          \
  103.         __sinit (ptr);                          \
  104.     }                                           \
  105.   while (0)
  106. #endif /* !_REENT_SMALL  */
  107.  
  108. #define CHECK_STD_INIT(ptr) \
  109.   do                                            \
  110.     {                                           \
  111.       if ((ptr) && !(ptr)->__sdidinit)          \
  112.         __sinit (ptr);                          \
  113.     }                                           \
  114.   while (0)
  115.  
  116. /* Return true and set errno and stream error flag iff the given FILE
  117.    cannot be written now.  */
  118.  
  119. #define cantwrite(ptr, fp)                                     \
  120.   ((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \
  121.    __swsetup_r(ptr, fp))
  122.  
  123. /* Test whether the given stdio file has an active ungetc buffer;
  124.    release such a buffer, without restoring ordinary unread data.  */
  125.  
  126. #define HASUB(fp) ((fp)->_ub._base != NULL)
  127. #define FREEUB(ptr, fp) {                    \
  128.         if ((fp)->_ub._base != (fp)->_ubuf) \
  129.                 _free_r(ptr, (char *)(fp)->_ub._base); \
  130.         (fp)->_ub._base = NULL; \
  131. }
  132.  
  133. /* Test for an fgetline() buffer.  */
  134.  
  135. #define HASLB(fp) ((fp)->_lb._base != NULL)
  136. #define FREELB(ptr, fp) { _free_r(ptr,(char *)(fp)->_lb._base); \
  137.       (fp)->_lb._base = NULL; }
  138.  
  139. /*
  140.  * Set the orientation for a stream. If o > 0, the stream has wide-
  141.  * orientation. If o < 0, the stream has byte-orientation.
  142.  */
  143. #define ORIENT(fp,ori)                                  \
  144.   do                                                            \
  145.     {                                                           \
  146.       if (!((fp)->_flags & __SORD))     \
  147.         {                                                       \
  148.           (fp)->_flags |= __SORD;                               \
  149.           if (ori > 0)                                          \
  150.             (fp)->_flags2 |= __SWID;                            \
  151.           else                                                  \
  152.             (fp)->_flags2 &= ~__SWID;                           \
  153.         }                                                       \
  154.     }                                                           \
  155.   while (0)
  156.  
  157. /* WARNING: _dcvt is defined in the stdlib directory, not here!  */
  158.  
  159. char *_EXFUN(_dcvt,(struct _reent *, char *, double, int, int, char, int));
  160. char *_EXFUN(_sicvt,(char *, short, char));
  161. char *_EXFUN(_icvt,(char *, int, char));
  162. char *_EXFUN(_licvt,(char *, long, char));
  163. #ifdef __GNUC__
  164. char *_EXFUN(_llicvt,(char *, long long, char));
  165. #endif
  166.  
  167. #define CVT_BUF_SIZE 128
  168.  
  169. #define NDYNAMIC 4      /* add four more whenever necessary */
  170.  
  171. #ifdef __SINGLE_THREAD__
  172. #define __sfp_lock_acquire()
  173. #define __sfp_lock_release()
  174. #define __sinit_lock_acquire()
  175. #define __sinit_lock_release()
  176. #else
  177. _VOID _EXFUN(__sfp_lock_acquire,(_VOID));
  178. _VOID _EXFUN(__sfp_lock_release,(_VOID));
  179. _VOID _EXFUN(__sinit_lock_acquire,(_VOID));
  180. _VOID _EXFUN(__sinit_lock_release,(_VOID));
  181. #endif
  182.  
  183. /* Types used in positional argument support in vfprinf/vfwprintf.
  184.    The implementation is char/wchar_t dependent but the class and state
  185.    tables are only defined once in vfprintf.c. */
  186. typedef enum {
  187.   ZERO,   /* '0' */
  188.   DIGIT,  /* '1-9' */
  189.   DOLLAR, /* '$' */
  190.   MODFR,  /* spec modifier */
  191.   SPEC,   /* format specifier */
  192.   DOT,    /* '.' */
  193.   STAR,   /* '*' */
  194.   FLAG,   /* format flag */
  195.   OTHER,  /* all other chars */
  196.   MAX_CH_CLASS /* place-holder */
  197. } __CH_CLASS;
  198.  
  199. typedef enum {
  200.   START,  /* start */
  201.   SFLAG,  /* seen a flag */
  202.   WDIG,   /* seen digits in width area */
  203.   WIDTH,  /* processed width */
  204.   SMOD,   /* seen spec modifier */
  205.   SDOT,   /* seen dot */
  206.   VARW,   /* have variable width specifier */
  207.   VARP,   /* have variable precision specifier */
  208.   PREC,   /* processed precision */
  209.   VWDIG,  /* have digits in variable width specification */
  210.   VPDIG,  /* have digits in variable precision specification */
  211.   DONE,   /* done */
  212.   MAX_STATE, /* place-holder */
  213. } __STATE;
  214.  
  215. typedef enum {
  216.   NOOP,  /* do nothing */
  217.   NUMBER, /* build a number from digits */
  218.   SKIPNUM, /* skip over digits */
  219.   GETMOD,  /* get and process format modifier */
  220.   GETARG,  /* get and process argument */
  221.   GETPW,   /* get variable precision or width */
  222.   GETPWB,  /* get variable precision or width and pushback fmt char */
  223.   GETPOS,  /* get positional parameter value */
  224.   PWPOS,   /* get positional parameter value for variable width or precision */
  225. } __ACTION;
  226.  
  227. extern _CONST __CH_CLASS __chclass[256];
  228. extern _CONST __STATE __state_table[MAX_STATE][MAX_CH_CLASS];
  229. extern _CONST __ACTION __action_table[MAX_STATE][MAX_CH_CLASS];
  230.