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