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@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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This package emulates glibc `line_wrap_stream' semantics for systems that
22 don't have that. If the system does have it, it is just a wrapper for
23 that. This header file is only used internally while compiling argp, and
24 shouldn't be installed. */
26 #ifndef _ARGP_FMTSTREAM_H
27 #define _ARGP_FMTSTREAM_H
38 # if ! (defined (HAVE_FLOCKFILE) && defined(HAVE_PUTC_UNLOCKED) \
39 && defined (HAVE_FPUTS_UNLOCKED) && defined (HAVE_FWRITE_UNLOCKED) )
41 /* Don't use stdio locking */
44 # define funlockfile(f)
45 # define putc_unlocked(c, f) putc((c), (f))
46 # define fputs_unlocked(s, f) fputs((s), (f))
47 # define fwrite_unlocked(b, s, n, f) fwrite((b), (s), (n), (f))
48 # endif /* No thread safe i/o */
51 #if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
52 || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
53 /* line_wrap_stream is available, so use that. */
54 #define ARGP_FMTSTREAM_USE_LINEWRAP
57 #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
58 /* Just be a simple wrapper for line_wrap_stream; the semantics are
59 *slightly* different, as line_wrap_stream doesn't actually make a new
60 object, it just modifies the given stream (reversibly) to do
61 line-wrapping. Since we control who uses this code, it doesn't matter. */
65 typedef FILE *argp_fmtstream_t
;
67 #define argp_make_fmtstream line_wrap_stream
68 #define __argp_make_fmtstream line_wrap_stream
69 #define argp_fmtstream_free line_unwrap_stream
70 #define __argp_fmtstream_free line_unwrap_stream
72 #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
73 #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
74 #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
75 #define argp_fmtstream_puts(fs,str) fputs(str,fs)
76 #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
77 #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
78 #define __argp_fmtstream_printf fprintf
79 #define argp_fmtstream_printf fprintf
81 #define __argp_fmtstream_lmargin line_wrap_lmargin
82 #define argp_fmtstream_lmargin line_wrap_lmargin
83 #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
84 #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
85 #define __argp_fmtstream_rmargin line_wrap_rmargin
86 #define argp_fmtstream_rmargin line_wrap_rmargin
87 #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
88 #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
89 #define __argp_fmtstream_wmargin line_wrap_wmargin
90 #define argp_fmtstream_wmargin line_wrap_wmargin
91 #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
92 #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
93 #define __argp_fmtstream_point line_wrap_point
94 #define argp_fmtstream_point line_wrap_point
96 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
97 /* Guess we have to define our own version. */
100 #define __const const
103 /* FIXME: We could use a configure test to check for __attribute__,
104 * just like lsh does. */
107 # define PRINTF_STYLE(f, a) __attribute__ ((__format__ (__printf__, f, a)))
109 # define PRINTF_STYLE(f, a)
114 struct argp_fmtstream
116 FILE *stream
; /* The stream we're outputting to. */
118 size_t lmargin
, rmargin
; /* Left and right margins. */
119 ssize_t wmargin
; /* Margin to wrap to, or -1 to truncate. */
121 /* Point in buffer to which we've processed for wrapping, but not output. */
123 /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
126 char *buf
; /* Output buffer. */
127 char *p
; /* Current end of text in BUF. */
128 char *end
; /* Absolute end of BUF. */
131 typedef struct argp_fmtstream
*argp_fmtstream_t
;
133 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
134 written on it with LMARGIN spaces and limits them to RMARGIN columns
135 total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
136 replacing the whitespace before them with a newline and WMARGIN spaces.
137 Otherwise, chars beyond RMARGIN are simply dropped until a newline.
138 Returns NULL if there was an error. */
139 extern argp_fmtstream_t
__argp_make_fmtstream (FILE *__stream
,
143 extern argp_fmtstream_t
argp_make_fmtstream (FILE *__stream
,
148 /* Flush __FS to its stream, and free it (but don't close the stream). */
149 extern void __argp_fmtstream_free (argp_fmtstream_t __fs
);
150 extern void argp_fmtstream_free (argp_fmtstream_t __fs
);
152 extern ssize_t
__argp_fmtstream_printf (argp_fmtstream_t __fs
,
153 __const
char *__fmt
, ...)
155 extern ssize_t
argp_fmtstream_printf (argp_fmtstream_t __fs
,
156 __const
char *__fmt
, ...)
159 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs
, int __ch
);
160 extern int argp_fmtstream_putc (argp_fmtstream_t __fs
, int __ch
);
162 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs
, __const
char *__str
);
163 extern int argp_fmtstream_puts (argp_fmtstream_t __fs
, __const
char *__str
);
165 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs
,
166 __const
char *__str
, size_t __len
);
167 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs
,
168 __const
char *__str
, size_t __len
);
170 /* Access macros for various bits of state. */
171 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
172 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
173 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
174 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
175 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
176 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
178 /* Set __FS's left margin to LMARGIN and return the old value. */
179 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs
,
181 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs
,
184 /* Set __FS's right margin to __RMARGIN and return the old value. */
185 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs
,
187 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs
,
190 /* Set __FS's wrap margin to __WMARGIN and return the old value. */
191 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs
,
193 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs
,
196 /* Return the column number of the current output point in __FS. */
197 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs
);
198 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs
);
200 /* Internal routines. */
201 extern void _argp_fmtstream_update (argp_fmtstream_t __fs
);
202 extern void __argp_fmtstream_update (argp_fmtstream_t __fs
);
203 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs
, size_t __amount
);
204 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs
, size_t __amount
);
207 /* Inline versions of above routines. */
210 #define __argp_fmtstream_putc argp_fmtstream_putc
211 #define __argp_fmtstream_puts argp_fmtstream_puts
212 #define __argp_fmtstream_write argp_fmtstream_write
213 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
214 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
215 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
216 #define __argp_fmtstream_point argp_fmtstream_point
217 #define __argp_fmtstream_update _argp_fmtstream_update
218 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
222 #define ARGP_FS_EI extern inline
226 __argp_fmtstream_write (argp_fmtstream_t __fs
,
227 __const
char *__str
, size_t __len
)
229 if (__fs
->p
+ __len
<= __fs
->end
|| __argp_fmtstream_ensure (__fs
, __len
))
231 memcpy (__fs
->p
, __str
, __len
);
240 __argp_fmtstream_puts (argp_fmtstream_t __fs
, __const
char *__str
)
242 size_t __len
= strlen (__str
);
245 size_t __wrote
= __argp_fmtstream_write (__fs
, __str
, __len
);
246 return __wrote
== __len
? 0 : -1;
253 __argp_fmtstream_putc (argp_fmtstream_t __fs
, int __ch
)
255 if (__fs
->p
< __fs
->end
|| __argp_fmtstream_ensure (__fs
, 1))
256 return *__fs
->p
++ = __ch
;
261 /* Set __FS's left margin to __LMARGIN and return the old value. */
263 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs
, size_t __lmargin
)
266 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
267 __argp_fmtstream_update (__fs
);
268 __old
= __fs
->lmargin
;
269 __fs
->lmargin
= __lmargin
;
273 /* Set __FS's right margin to __RMARGIN and return the old value. */
275 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs
, size_t __rmargin
)
278 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
279 __argp_fmtstream_update (__fs
);
280 __old
= __fs
->rmargin
;
281 __fs
->rmargin
= __rmargin
;
285 /* Set FS's wrap margin to __WMARGIN and return the old value. */
287 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs
, size_t __wmargin
)
290 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
291 __argp_fmtstream_update (__fs
);
292 __old
= __fs
->wmargin
;
293 __fs
->wmargin
= __wmargin
;
297 /* Return the column number of the current output point in __FS. */
299 __argp_fmtstream_point (argp_fmtstream_t __fs
)
301 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
302 __argp_fmtstream_update (__fs
);
303 return __fs
->point_col
>= 0 ? __fs
->point_col
: 0;
307 #undef __argp_fmtstream_putc
308 #undef __argp_fmtstream_puts
309 #undef __argp_fmtstream_write
310 #undef __argp_fmtstream_set_lmargin
311 #undef __argp_fmtstream_set_rmargin
312 #undef __argp_fmtstream_set_wmargin
313 #undef __argp_fmtstream_point
314 #undef __argp_fmtstream_update
315 #undef __argp_fmtstream_ensure
318 #endif /* __OPTIMIZE__ */
320 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
322 #endif /* argp-fmtstream.h */