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