argp-fmtstream.h 11 KB

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