Doxyfy ircd_snprintf.h and ircd_snprintf.c.
[ircu2.10.12-pk.git] / include / ircd_snprintf.h
1 #ifndef INCLUDED_ircd_snprintf_h
2 #define INCLUDED_ircd_snprintf_h
3 /*
4  * IRC - Internet Relay Chat, include/ircd_snprintf.h
5  * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * $Id$
22  */
23 #ifndef INCLUDED_sys_types_h
24 #include <sys/types.h>
25 #define INCLUDED_sys_types_h
26 #endif
27 #ifndef INCLUDED_stdarg_h
28 #include <stdarg.h>
29 #define INCLUDED_stdarg_h
30 #endif
31
32 struct Client;
33
34 /** structure passed as argument for %v conversion */
35 struct VarData {
36   size_t        vd_chars;       /**< number of characters inserted */
37   size_t        vd_overflow;    /**< number of characters that couldn't be */
38   const char   *vd_format;      /**< format string */
39   va_list       vd_args;        /**< arguments for %v */
40 };
41
42 #ifndef HAVE_VA_COPY
43 #if HAVE___VA_COPY
44 #define va_copy(DEST, SRC) __va_copy(DEST, SRC)
45 #else
46 /** Fallback macro to copy to \a DEST from \a SRC. */
47 #define va_copy(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof(DEST))
48 #endif
49 #endif
50
51 extern int ircd_snprintf(struct Client *dest, char *buf, size_t buf_len,
52                          const char *format, ...);
53 extern int ircd_vsnprintf(struct Client *dest, char *buf, size_t buf_len,
54                           const char *format, va_list args);
55
56 /** @fn int ircd_snprintf(struct Client *dest, char *buf, size_t
57                          buf_len, const char *format, ...)
58 **
59 **   These functions are intended to be a complete replacement for
60 ** sprintf and sprintf_irc.  They are a (nearly) complete
61 ** reimplementation, and of course they're snprintf clones, making it
62 ** more difficult for accidental buffer overflows to crop up.
63 **
64 **   First off, what's missing?  These functions support all ANSI C
65 ** conversion specifiers and selected ones from ISO 9x, with the
66 ** exception of all floating-point conversions.  The floating-point
67 ** conversions are tricky, and will likely be dependent on the
68 ** representation of a floating-point number on a particular
69 ** architecture.  While that representation is likely to conform to
70 ** some standard, it is not currently used in ircu, so seemed like a
71 ** good thing to omit, given the difficulty of implementing it.
72 **
73 **   There are two more things missing from this implementation that
74 ** would be required by ANSI; the first is support for multibyte
75 ** character strings, and the second is support for locales, neither
76 ** of which have any relevance for ircu, so again omission seemed to
77 ** be a good policy.  Additionally, %#x always causes '0x' (or '0X')
78 ** to be printed, even if the number is zero.
79 **
80 **   These functions also have some extensions not seen in a
81 ** standards-compliant implementation; technically, the ISO 9x
82 ** extensions fall into this category, for instance.  The ISO 9x
83 ** extensions supported are type extensions--%ju, %tu, and %zu, for
84 ** instance; %qu and %hhu are also supported.  The extensions added
85 ** for use in ircu are %Tu, which takes a time_t, and the new %C
86 ** conversion, which inserts either a numeric or a nick, dependant on
87 ** the &lt;dest> parameter.  The GNU %m extension, which inserts the
88 ** strerror() string corresponding to the current value of errno, is
89 ** also supported, as is a special %v extension, which essentially
90 ** does a recursive call to ircd_snprintf.
91 **
92 **   The following description is descended from the Linux manpage for
93 ** the printf family of functions.
94 **
95 **   The format string is composed of zero or more directives:
96 ** ordinary characters (not %), which are copied unchanged to the
97 ** output stream; and conversion specifications, each of which results
98 ** in fetching zero or more subsequent arguments.  Each conversion
99 ** specification is introduced by the character %.  The arguments must
100 ** correspond properly (after type promotion) with the conversion
101 ** specifier.  After the %, the following appear in sequence:
102 **
103 ** <ul><li>Zero or more of the following flags:<dl>
104 **
105 ** <dt>#</dt>
106 **      <dd>specifying that the value should be converted to an
107 **      "alternate form."  For c, d, i, n, p, s, and u conversions,
108 **      this option has no effect.  For o conversions, the precision
109 **      of the number is increased to force the first character of the
110 **      output string to a zero (except if a zero value is printed
111 **      with an explicit precision of zero).  For x and X conversions,
112 **      the string '0x' (or '0X' for X conversions) is prepended to
113 **      it.  For e, E, f, g, and G conversions, the result will always
114 **      contain a decimal point, even if no digits follow it
115 **      (normally, a decimal point appears in the results of those
116 **      conversions only if a digit follows).  For g and G
117 **      conversions, trailing zeros are not removed from the result as
118 **      they would otherwise be.  For C conversions, if the
119 **      destination is local and the origin is a user, the
120 **      nick!user\@host form is used.</dd>
121 **
122 ** <dt>0</dt>
123 **      <dd> specifying zero padding.  For all conversions except n, the
124 **      converted value is padded on the left with zeros rather than
125 **      blanks.  If a precision is given with a numeric conversion (d,
126 **      i, o, u, i, x, and X), the 0 flag is ignored.</dd>
127 **
128 ** <dt>-</dt>
129 **      <dd>(a negative field width flag) indicates the converted value is
130 **      to be left adjusted on the field boundary.  Except for n
131 **      conversions, the converted value is padded on the right with
132 **      blanks, rather than on the left with blanks or zeros.  A -
133 **      overrides a 0 if both are given.</dd>
134 **
135 ** <dt>' ' (a space)</dt>
136 **      <dd>specifying that a blank should be left before a
137 **      positive number produced by a signed conversion (d, e, E, f,
138 **      g, G, or i).</dd>
139 **
140 ** <dt>+</dt>
141 **      <dd>specifying that a sign always be placed before a number
142 **      produced by a signed conversion.  A + overrides a space if
143 **      both are used.</dd>
144 **
145 ** <dt>:</dt>
146 **      <dd>specifying that a struct Client name should be preceded by a
147 **      ':' character if the destination is a user.</dd>
148 ** </dl></li>
149 **
150 ** <li>An optional decimal digit string specifying a minimum field
151 **   width.  If the converted value has fewer characters than the
152 **   field width, it will be padded with spaces on the left (or right,
153 **   if the left-adjustment flag has been given) to fill out the field
154 **   width.</li>
155 **
156 ** <li>An optional precision, in the form of a period (`.') followed by
157 **   an optional digit string.  If  the digit string is omitted, the
158 **   precision is taken as zero.  This gives the minimum number of
159 **   digits to appear for d, i, o, u, x, and X conversions, the number
160 **   of digits to appear after the decimal-point for e, E, and f
161 **   conversions, the maximum number of significant digits for g and G
162 **   conversions, or the maximum number of characters to be printed
163 **   from a string for s conversions.</li>
164 **
165 ** <li>The optional character h, specifying that a following d, i, o, u,
166 **   x, or X conversion corresponds to a short int or unsigned short
167 **   int argument, or that a following n conversion corresponds to a
168 **   pointer to a short int argument.  If the h character is given
169 **   again, char is used instead of short int.</li>
170 **
171 ** <li>The optional character l (ell) specifying that a following d, i,
172 **   o, u, x, or X conversion applies to a pointer to a long int or
173 **   unsigned long int argument, or that a following n conversion
174 **   corresponds to a pointer to a long int argument.</li>
175 **
176 ** <li>The character L specifying that a following e, E, f, g, or G
177 **   conversion corresponds to a long double argument, or a following
178 **   d, i, o, u, x, or X conversion corresponds to a long long
179 **   argument.  Note that long long is not specified in ANSI C and
180 **   therefore not portable to all architectures.</li>
181 **
182 ** <li>The optional character q.  This is equivalent to L.</li>
183 **
184 ** <li>A j character specifying that the following integer (d, i, o, u,
185 **   x, or X) conversion corresponds to an intmax_t argument.</li>
186 **
187 ** <li>A t character specifying that the following integer (d, i, o, u,
188 **   x, or X) conversion corresponds to a ptrdiff_t argument.</li>
189 **
190 ** <li>A z character specifying that the following integer (d, i, o, u,
191 **   x, or X) conversion corresponds to a size_t argument.</li>
192 **
193 ** <li>A T character specifying that the following integer (d, i, o, u,
194 **   x, or X) conversion corresponds to a time_t argument.</li>
195 **
196 ** <li>A character that specifies the type of conversion to be applied.</li>
197 ** </ul>
198 **
199 **   A field width or precision, or both, may be indicated by an
200 ** asterisk `*' instead of a digit string.  In this case, an int
201 ** argument supplies the field width or precision.  A negative field
202 ** width is treated as a left adjustment flag followed by a positive
203 ** field width; a negative precision is treated as though it were
204 ** missing.
205 **
206 ** The conversion specifiers and their meanings are:
207 **
208 ** <dl>
209 ** <dt>diouxX</dt>
210 **              <dd>The int (or appropriate variant) argument is converted
211 **              to signed decimal (d and i), unsigned octal (o),
212 **              unsigned decimal (u), or unsigned hexadecimal (x and
213 **              X) notation.  The letters abcdef are used for x
214 **              conversions; the letters ABCDEF are used for X
215 **              conversions.  The precision, if any, gives the minimum
216 **              number of digits that must appear; if the converted
217 **              value requires fewer digits, it is padded on the left
218 **              with zeros.</dd>
219 **
220 ** <dt>eE [NOT IMPLEMENTED]</dt>
221 **              <dd>The double argument is rounded and
222 **              converted in the style [-]d.dddedd where there is one
223 **              digit before the decimal-point character and the
224 **              number of digits after it is equal to the precision;
225 **              if the precision is missing, it is taken as 6; if the
226 **              precision is zero, no decimal-point character appears.
227 **              An E conversion uses the letter E (rather than e) to
228 **              introduce the exponent.  The exponent always contains
229 **              at least two digits; if the value is zero, the
230 **              exponent is 00.</dd>
231 **
232 ** <dt>f [NOT IMPLEMENTED]</dt>
233 **              <dd>The double argument is rounded and
234 **              converted to  decimal notation in the style
235 **              [-]ddd.ddd, where the number of digits after the
236 **              decimal-point character is equal to the precision
237 **              specification.  If the precision is missing, it is
238 **              taken as 6; if the precision is explicitly zero, no
239 **              decimal-point character appears.  If a decimal point
240 **              appears, at least one digit appears before it.</dd>
241 **
242 ** <dt>g [NOT IMPLEMENTED]</dt>
243 **              <dd>The double argument is converted in
244 **              style f or e (or E for G conversions).  The precision
245 **              specifies the number of significant digits.  If the
246 **              precision is missing, 6 digits are given; if the
247 **              precision is zero, it is treated as 1.  Style e is
248 **              used if the exponent from its conversion is less than
249 **              -4 or greater than or equal to the precision.
250 **              Trailing zeros are removed from the fractional part of
251 **              the result; a decimal point appears only if it is
252 **              followed by at least one digit.</dd>
253 **
254 ** <dt>c</dt>
255 **              <dd>The int argument is converted to an unsigned char, and
256 **              the resulting character is written.</dd>
257 **
258 ** <dt>s</dt>
259 **              <dd>The "char *" argument is expected to be a pointer to
260 **              an array of character type (pointer to a string).
261 **              Characters from the array are written up to (but not
262 **              including) a terminating NUL character; if a precision
263 **              is specified, no more than the number specified are
264 **              written.  If a precision is given, no null character
265 **              need be present; if the precision is not specified, or
266 **              is greater than the size of the array, the array must
267 **              contain a terminating NUL character.</dd>
268 **
269 ** <dt>p</dt>
270 **              <dd>The "void *" pointer argument is printed in
271 **              hexadecimal (as if by %#x or %#lx).</dd>
272 **
273 ** <dt>n</dt>
274 **              <dd>The number of characters written so far is stored into
275 **              the integer indicated by the ``int *'' (or variant)
276 **              pointer argument.  No argument is converted.</dd>
277 **
278 ** <dt>m</dt>
279 **              <dd>The error message associated with the current value of
280 **              errno is printed as if by %s.</dd>
281 **
282 ** <dt>C</dt>
283 **              <dd>The client argument identifier is printed under the
284 **              control of the &lt;dest> argument; if &lt;dest> is NULL or
285 **              is a user, the client's name (nickname or server name)
286 **              is printed; otherwise, the client's network numeric is
287 **              printed.</dd>
288 **
289 ** <dt>H</dt>
290 **              <dd>The channel argument identifier (channel name) is
291 **              printed.</dd>
292 **
293 ** <dt>v</dt>
294 **              <dd>The argument given must be a pointer to a struct
295 **              VarData with vd_format and vd_args must be initialized
296 **              appropriately.  On return, vd_chars will contain the
297 **              number of characters added to the buffer, and
298 **              vd_overflow will contain the number of characters that
299 **              could not be added due to buffer overflow or due to a
300 **              precision.</dd>
301 **
302 ** <dt>%<dt>
303 **              <dd>A `%' is written.  No argument is converted.  The
304 **              complete conversion specification is `%%'.</dd>
305 ** </dl>
306 **
307 **   In no case does a non-existent or small field width cause
308 ** truncation of a field; if the result of a conversion is wider than
309 ** the field width, the field is expanded to contain the conversion
310 ** result.
311 **
312 ** @param[in] dest Client receiving of message.
313 ** @param[out] buf Output buffer for formatted message.
314 ** @param[in] buf_len Number of bytes that can be written to \a buf.
315 ** @param[in] format Format string for message.
316 ** @return Number of bytes that would be written to \a buf without truncation.
317 */
318
319 #endif /* INCLUDED_ircd_snprintf_h */