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>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, see <http://www.gnu.org/licenses/>.
20 Modified for uClibc by: Salvatore Cro <salvatore.cro at st.com>
23 /* This package emulates glibc `line_wrap_stream' semantics for systems that
24 don't have that. If the system does have it, it is just a wrapper for
25 that. This header file is only used internally while compiling argp, and
26 shouldn't be installed. */
28 #ifndef _ARGP_FMTSTREAM_H
29 #define _ARGP_FMTSTREAM_H
36 /* This feature is available in gcc versions 2.5 and later. */
37 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || defined(__STRICT_ANSI__)
38 # define __attribute__(Spec) /* empty */
40 /* The __-protected variants of `format' and `printf' attributes
41 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
42 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || defined(__STRICT_ANSI__)
43 # define __format__ format
44 # define __printf__ printf
48 #if 0 /* uClibc: disabled */
49 #if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
50 || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
51 /* line_wrap_stream is available, so use that. */
52 #define ARGP_FMTSTREAM_USE_LINEWRAP
55 /* line_wrap stream NOT available */
56 # undef ARGP_FMTSTREAM_USE_LINEWRAP
59 #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
60 /* Just be a simple wrapper for line_wrap_stream; the semantics are
61 *slightly* different, as line_wrap_stream doesn't actually make a new
62 object, it just modifies the given stream (reversibly) to do
63 line-wrapping. Since we control who uses this code, it doesn't matter. */
67 typedef FILE *argp_fmtstream_t
;
69 #define argp_make_fmtstream line_wrap_stream
70 #define __argp_make_fmtstream line_wrap_stream
71 #define argp_fmtstream_free line_unwrap_stream
72 #define __argp_fmtstream_free line_unwrap_stream
74 #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
75 #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
76 #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
77 #define argp_fmtstream_puts(fs,str) fputs(str,fs)
78 #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
79 #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
80 #define __argp_fmtstream_printf fprintf
81 #define argp_fmtstream_printf fprintf
83 #define __argp_fmtstream_lmargin line_wrap_lmargin
84 #define argp_fmtstream_lmargin line_wrap_lmargin
85 #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
86 #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
87 #define __argp_fmtstream_rmargin line_wrap_rmargin
88 #define argp_fmtstream_rmargin line_wrap_rmargin
89 #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
90 #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
91 #define __argp_fmtstream_wmargin line_wrap_wmargin
92 #define argp_fmtstream_wmargin line_wrap_wmargin
93 #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
94 #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
95 #define __argp_fmtstream_point line_wrap_point
96 #define argp_fmtstream_point line_wrap_point
98 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
99 /* Guess we have to define our own version. */
102 #define __const const
105 struct argp_fmtstream
107 FILE *stream
; /* The stream we're outputting to. */
109 size_t lmargin
, rmargin
; /* Left and right margins. */
110 ssize_t wmargin
; /* Margin to wrap to, or -1 to truncate. */
112 /* Point in buffer to which we've processed for wrapping, but not output. */
114 /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
117 char *buf
; /* Output buffer. */
118 char *p
; /* Current end of text in BUF. */
119 char *end
; /* Absolute end of BUF. */
122 typedef struct argp_fmtstream
*argp_fmtstream_t
;
124 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
125 written on it with LMARGIN spaces and limits them to RMARGIN columns
126 total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
127 replacing the whitespace before them with a newline and WMARGIN spaces.
128 Otherwise, chars beyond RMARGIN are simply dropped until a newline.
129 Returns NULL if there was an error. */
130 extern argp_fmtstream_t
__argp_make_fmtstream (FILE *__stream
,
134 extern argp_fmtstream_t
argp_make_fmtstream (FILE *__stream
,
139 /* Flush __FS to its stream, and free it (but don't close the stream). */
140 extern void __argp_fmtstream_free (argp_fmtstream_t __fs
);
141 extern void argp_fmtstream_free (argp_fmtstream_t __fs
);
143 extern ssize_t
__argp_fmtstream_printf (argp_fmtstream_t __fs
,
144 __const
char *__fmt
, ...)
145 __attribute__ ((__format__ (printf
, 2, 3)));
146 extern ssize_t
argp_fmtstream_printf (argp_fmtstream_t __fs
,
147 __const
char *__fmt
, ...)
148 __attribute__ ((__format__ (printf
, 2, 3)));
150 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs
, int __ch
);
151 extern int argp_fmtstream_putc (argp_fmtstream_t __fs
, int __ch
);
153 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs
, __const
char *__str
);
154 extern int argp_fmtstream_puts (argp_fmtstream_t __fs
, __const
char *__str
);
156 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs
,
157 __const
char *__str
, size_t __len
);
158 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs
,
159 __const
char *__str
, size_t __len
);
161 /* Access macros for various bits of state. */
162 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
163 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
164 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
165 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
166 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
167 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
169 /* Set __FS's left margin to LMARGIN and return the old value. */
170 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs
,
172 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs
,
175 /* Set __FS's right margin to __RMARGIN and return the old value. */
176 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs
,
178 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs
,
181 /* Set __FS's wrap margin to __WMARGIN and return the old value. */
182 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs
,
184 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs
,
187 /* Return the column number of the current output point in __FS. */
188 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs
);
189 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs
);
191 /* Internal routines. */
192 extern void _argp_fmtstream_update (argp_fmtstream_t __fs
);
193 extern void __argp_fmtstream_update (argp_fmtstream_t __fs
);
194 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs
, size_t __amount
);
195 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs
, size_t __amount
);
198 /* Inline versions of above routines. */
201 #define __argp_fmtstream_putc argp_fmtstream_putc
202 #define __argp_fmtstream_puts argp_fmtstream_puts
203 #define __argp_fmtstream_write argp_fmtstream_write
204 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
205 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
206 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
207 #define __argp_fmtstream_point argp_fmtstream_point
208 #define __argp_fmtstream_update _argp_fmtstream_update
209 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
213 #define ARGP_FS_EI __extern_inline
217 __argp_fmtstream_write (argp_fmtstream_t __fs
,
218 __const
char *__str
, size_t __len
)
220 if (__fs
->p
+ __len
<= __fs
->end
|| __argp_fmtstream_ensure (__fs
, __len
))
222 memcpy (__fs
->p
, __str
, __len
);
231 __argp_fmtstream_puts (argp_fmtstream_t __fs
, __const
char *__str
)
233 size_t __len
= strlen (__str
);
236 size_t __wrote
= __argp_fmtstream_write (__fs
, __str
, __len
);
237 return __wrote
== __len
? 0 : -1;
244 __argp_fmtstream_putc (argp_fmtstream_t __fs
, int __ch
)
246 if (__fs
->p
< __fs
->end
|| __argp_fmtstream_ensure (__fs
, 1))
247 return *__fs
->p
++ = __ch
;
252 /* Set __FS's left margin to __LMARGIN and return the old value. */
254 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs
, size_t __lmargin
)
257 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
258 __argp_fmtstream_update (__fs
);
259 __old
= __fs
->lmargin
;
260 __fs
->lmargin
= __lmargin
;
264 /* Set __FS's right margin to __RMARGIN and return the old value. */
266 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs
, size_t __rmargin
)
269 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
270 __argp_fmtstream_update (__fs
);
271 __old
= __fs
->rmargin
;
272 __fs
->rmargin
= __rmargin
;
276 /* Set FS's wrap margin to __WMARGIN and return the old value. */
278 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs
, size_t __wmargin
)
281 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
282 __argp_fmtstream_update (__fs
);
283 __old
= __fs
->wmargin
;
284 __fs
->wmargin
= __wmargin
;
288 /* Return the column number of the current output point in __FS. */
290 __argp_fmtstream_point (argp_fmtstream_t __fs
)
292 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
293 __argp_fmtstream_update (__fs
);
294 return __fs
->point_col
>= 0 ? __fs
->point_col
: 0;
298 #undef __argp_fmtstream_putc
299 #undef __argp_fmtstream_puts
300 #undef __argp_fmtstream_write
301 #undef __argp_fmtstream_set_lmargin
302 #undef __argp_fmtstream_set_rmargin
303 #undef __argp_fmtstream_set_wmargin
304 #undef __argp_fmtstream_point
305 #undef __argp_fmtstream_update
306 #undef __argp_fmtstream_ensure
309 #endif /* __OPTIMIZE__ */
311 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
313 #endif /* argp-fmtstream.h */