argp-fmtstream.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Word-wrapping and line-truncating streams.
  2. Copyright (C) 1997 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Written by Miles Bader <miles at gnu.ai.mit.edu>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA.
  17. Modified for uClibc by: Salvatore Cro <salvatore.cro at st.com>
  18. */
  19. /* This package emulates glibc `line_wrap_stream' semantics for systems that
  20. don't have that. If the system does have it, it is just a wrapper for
  21. that. This header file is only used internally while compiling argp, and
  22. shouldn't be installed. */
  23. #ifndef _ARGP_FMTSTREAM_H
  24. #define _ARGP_FMTSTREAM_H
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #ifndef __attribute__
  29. /* This feature is available in gcc versions 2.5 and later. */
  30. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || defined(__STRICT_ANSI__)
  31. # define __attribute__(Spec) /* empty */
  32. # endif
  33. /* The __-protected variants of `format' and `printf' attributes
  34. are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
  35. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || defined(__STRICT_ANSI__)
  36. # define __format__ format
  37. # define __printf__ printf
  38. # endif
  39. #endif
  40. #if 0 /* uClibc: disabled */
  41. #if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
  42. || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
  43. /* line_wrap_stream is available, so use that. */
  44. #define ARGP_FMTSTREAM_USE_LINEWRAP
  45. #endif
  46. #else
  47. /* line_wrap stream NOT available */
  48. # undef ARGP_FMTSTREAM_USE_LINEWRAP
  49. #endif
  50. #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
  51. /* Just be a simple wrapper for line_wrap_stream; the semantics are
  52. *slightly* different, as line_wrap_stream doesn't actually make a new
  53. object, it just modifies the given stream (reversibly) to do
  54. line-wrapping. Since we control who uses this code, it doesn't matter. */
  55. #include <linewrap.h>
  56. typedef FILE *argp_fmtstream_t;
  57. #define argp_make_fmtstream line_wrap_stream
  58. #define __argp_make_fmtstream line_wrap_stream
  59. #define argp_fmtstream_free line_unwrap_stream
  60. #define __argp_fmtstream_free line_unwrap_stream
  61. #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
  62. #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
  63. #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
  64. #define argp_fmtstream_puts(fs,str) fputs(str,fs)
  65. #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
  66. #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
  67. #define __argp_fmtstream_printf fprintf
  68. #define argp_fmtstream_printf fprintf
  69. #define __argp_fmtstream_lmargin line_wrap_lmargin
  70. #define argp_fmtstream_lmargin line_wrap_lmargin
  71. #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
  72. #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
  73. #define __argp_fmtstream_rmargin line_wrap_rmargin
  74. #define argp_fmtstream_rmargin line_wrap_rmargin
  75. #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
  76. #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
  77. #define __argp_fmtstream_wmargin line_wrap_wmargin
  78. #define argp_fmtstream_wmargin line_wrap_wmargin
  79. #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
  80. #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
  81. #define __argp_fmtstream_point line_wrap_point
  82. #define argp_fmtstream_point line_wrap_point
  83. #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
  84. /* Guess we have to define our own version. */
  85. #ifndef __const
  86. #define __const const
  87. #endif
  88. struct argp_fmtstream
  89. {
  90. FILE *stream; /* The stream we're outputting to. */
  91. size_t lmargin, rmargin; /* Left and right margins. */
  92. ssize_t wmargin; /* Margin to wrap to, or -1 to truncate. */
  93. /* Point in buffer to which we've processed for wrapping, but not output. */
  94. size_t point_offs;
  95. /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
  96. ssize_t point_col;
  97. char *buf; /* Output buffer. */
  98. char *p; /* Current end of text in BUF. */
  99. char *end; /* Absolute end of BUF. */
  100. };
  101. typedef struct argp_fmtstream *argp_fmtstream_t;
  102. /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
  103. written on it with LMARGIN spaces and limits them to RMARGIN columns
  104. total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
  105. replacing the whitespace before them with a newline and WMARGIN spaces.
  106. Otherwise, chars beyond RMARGIN are simply dropped until a newline.
  107. Returns NULL if there was an error. */
  108. extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
  109. size_t __lmargin,
  110. size_t __rmargin,
  111. ssize_t __wmargin);
  112. extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
  113. size_t __lmargin,
  114. size_t __rmargin,
  115. ssize_t __wmargin);
  116. /* Flush __FS to its stream, and free it (but don't close the stream). */
  117. extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
  118. extern void argp_fmtstream_free (argp_fmtstream_t __fs);
  119. extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
  120. __const char *__fmt, ...)
  121. __attribute__ ((__format__ (printf, 2, 3)));
  122. extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
  123. __const char *__fmt, ...)
  124. __attribute__ ((__format__ (printf, 2, 3)));
  125. extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
  126. extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
  127. extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
  128. extern int argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
  129. extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
  130. __const char *__str, size_t __len);
  131. extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
  132. __const char *__str, size_t __len);
  133. /* Access macros for various bits of state. */
  134. #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
  135. #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
  136. #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
  137. #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
  138. #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
  139. #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
  140. /* Set __FS's left margin to LMARGIN and return the old value. */
  141. extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
  142. size_t __lmargin);
  143. extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
  144. size_t __lmargin);
  145. /* Set __FS's right margin to __RMARGIN and return the old value. */
  146. extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
  147. size_t __rmargin);
  148. extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
  149. size_t __rmargin);
  150. /* Set __FS's wrap margin to __WMARGIN and return the old value. */
  151. extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
  152. size_t __wmargin);
  153. extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
  154. size_t __wmargin);
  155. /* Return the column number of the current output point in __FS. */
  156. extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
  157. extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
  158. /* Internal routines. */
  159. extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
  160. extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
  161. extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
  162. extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
  163. #ifdef __OPTIMIZE__
  164. /* Inline versions of above routines. */
  165. #if !_LIBC
  166. #define __argp_fmtstream_putc argp_fmtstream_putc
  167. #define __argp_fmtstream_puts argp_fmtstream_puts
  168. #define __argp_fmtstream_write argp_fmtstream_write
  169. #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
  170. #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
  171. #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
  172. #define __argp_fmtstream_point argp_fmtstream_point
  173. #define __argp_fmtstream_update _argp_fmtstream_update
  174. #define __argp_fmtstream_ensure _argp_fmtstream_ensure
  175. #endif
  176. #ifndef ARGP_FS_EI
  177. #define ARGP_FS_EI __extern_inline
  178. #endif
  179. ARGP_FS_EI size_t
  180. __argp_fmtstream_write (argp_fmtstream_t __fs,
  181. __const char *__str, size_t __len)
  182. {
  183. if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
  184. {
  185. memcpy (__fs->p, __str, __len);
  186. __fs->p += __len;
  187. return __len;
  188. }
  189. else
  190. return 0;
  191. }
  192. ARGP_FS_EI int
  193. __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str)
  194. {
  195. size_t __len = strlen (__str);
  196. if (__len)
  197. {
  198. size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
  199. return __wrote == __len ? 0 : -1;
  200. }
  201. else
  202. return 0;
  203. }
  204. ARGP_FS_EI int
  205. __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
  206. {
  207. if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
  208. return *__fs->p++ = __ch;
  209. else
  210. return EOF;
  211. }
  212. /* Set __FS's left margin to __LMARGIN and return the old value. */
  213. ARGP_FS_EI size_t
  214. __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
  215. {
  216. size_t __old;
  217. if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
  218. __argp_fmtstream_update (__fs);
  219. __old = __fs->lmargin;
  220. __fs->lmargin = __lmargin;
  221. return __old;
  222. }
  223. /* Set __FS's right margin to __RMARGIN and return the old value. */
  224. ARGP_FS_EI size_t
  225. __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
  226. {
  227. size_t __old;
  228. if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
  229. __argp_fmtstream_update (__fs);
  230. __old = __fs->rmargin;
  231. __fs->rmargin = __rmargin;
  232. return __old;
  233. }
  234. /* Set FS's wrap margin to __WMARGIN and return the old value. */
  235. ARGP_FS_EI size_t
  236. __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
  237. {
  238. size_t __old;
  239. if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
  240. __argp_fmtstream_update (__fs);
  241. __old = __fs->wmargin;
  242. __fs->wmargin = __wmargin;
  243. return __old;
  244. }
  245. /* Return the column number of the current output point in __FS. */
  246. ARGP_FS_EI size_t
  247. __argp_fmtstream_point (argp_fmtstream_t __fs)
  248. {
  249. if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
  250. __argp_fmtstream_update (__fs);
  251. return __fs->point_col >= 0 ? __fs->point_col : 0;
  252. }
  253. #if !_LIBC
  254. #undef __argp_fmtstream_putc
  255. #undef __argp_fmtstream_puts
  256. #undef __argp_fmtstream_write
  257. #undef __argp_fmtstream_set_lmargin
  258. #undef __argp_fmtstream_set_rmargin
  259. #undef __argp_fmtstream_set_wmargin
  260. #undef __argp_fmtstream_point
  261. #undef __argp_fmtstream_update
  262. #undef __argp_fmtstream_ensure
  263. #endif
  264. #endif /* __OPTIMIZE__ */
  265. #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
  266. #endif /* argp-fmtstream.h */