aboutsummaryrefslogtreecommitdiffstats
path: root/include/slibc-print.h
blob: 194e9cbf726f687ead3327e628aae16a3d17ed8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
 * slibc — Yet another C library
 * Copyright © 2015  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef _SLIBC_PRINT_H
#define _SLIBC_PRINT_H
#include <slibc/version.h>
#include <slibc/features.h>
#ifndef __PORTABLE



#define __NEED_size_t
#define __NEED_ssize_t
#define __NEED_wchar_t
#define __NEED_intmax_t
#define __NEED_va_list
#include <bits/types.h>



/**
 * Structure used by extensions to `generic_printf`-function
 * to request that additionally arguments be added before the
 * function is called again.
 * 
 * @etymology  (`generic_printf`)-subsystem: (ext)ension (queue).
 * 
 * @since  Always.
 */
struct generic_printf_ext_queue
{
  /**
   * Sizes, in bytes, of the missing arguments.
   * Only 1, 2, 4 and 8 are allowed.
   * 
   * @since  Always.
   */
  char sizes[4];
  
  /**
   * The indices of the missing arguments.
   * 
   * @since  Always.
   */
  size_t indices[4];
  
  /**
   * The amount of missing arguments.
   * `sizes` and `indices` can only hold up to
   * 4 arguments, the function will be later
   * again to retrieve information about the
   * omitted arguments. To avoid a deadlock,
   * add arguments in the order of their index
   * if there are more than 4 missing arguments.
   * 
   * @since  Always.
   */
  size_t count;
};


/**
 * Function-type used by `generic_printf` and `vgeneric_wprintf`
 * to write a string.
 * 
 * @etymology  (`generic_printf`)-subsystem: (write-func)tion, `(t)ypedef`.
 * 
 * @param   text    The text segment to print, it will only contain
 *                  a NUL byte if that NUL byte shall be printed.
 * @param   length  The length of `text`.
 * @param   data    The value passed to `generic_printf` or
 *                  `vgeneric_wprintf` via the parameter `data`.
 * @return          Zero on success, -1 on error. `errno` shall
 *                  be set on error.
 * 
 * @since  Always.
 */
typedef int (* generic_printf_write_func_t)(const char*, size_t, void*);

/**
 * Variant of `generic_printf_write_func_t` used for
 * `generic_wprintf` and `vgeneric_wprintf`.
 * 
 * @etymology  (`generic_wprintf`)-subsystem: (write-func)tion, `(t)ypedef`.
 * 
 * @since  Always.
 */
typedef int (* generic_wprintf_write_func_t)(const wchar_t*, size_t, void*);

/**
 * Function-type used by `generic_printf` and `vgeneric_wprintf`
 * to write a string if a custom formatting code was encountered.
 * 
 * @etymology  (`generic_printf`)-subsystem: (ext)ension(-func)tion, `(t)ypedef`.
 * 
 * @param   code        The %-code, excluding the %.
 * @param   args        Formatting arguments cased to `intmax`.
 * @param   argn        The number of formatting arguments in `args`.
 * @param   silent      Non-zero if the function must not perform a write.
 * @param   data        The value passed to `generic_printf` or
 *                      `vgeneric_wprintf` via the parameter `data`.
 * @param   queue_data  Used to send information to the calling function
 *                      about missing arguments.
 * @return              The number of written characters, -1 on error.
 *                      `errno` shall be set on error. -2 shall be returned
 *                      if argument index equal to or greater than the
 *                      value of `argn`, if `queue_data` must be set.
 * 
 * @throws  EINVAL  If `code` could not be recognised.
 * 
 * @since  Always.
 */
typedef ssize_t (* generic_printf_ext_func_t)(const char*, intmax_t*, size_t, int, void*,
					      struct generic_printf_ext_queue*);

/**
 * Variant of `generic_printf_ext_func_t` used for
 * `generic_wprintf` and `vgeneric_wprintf`.
 * 
 * @etymology  (`generic_wprintf`)-subsystem: (ext)ension(-func)tion, `(t)ypedef`.
 * 
 * @since  Always.
 */
typedef ssize_t (* generic_wprintf_ext_func_t)(const wchar_t*, intmax_t*, size_t, int, void*,
					       struct generic_printf_ext_queue*);


/**
 * An almost fully generic `printf`-function.
 * 
 * @etymology  (Generic) (`wprintf`)-function.
 * 
 * @param   write_function      Function used to write the string. `NULL` if
 *                              it shall not be printed but only measured.
 * @param   extension_function  Function used to extend the functions formatting codes.
 *                              `NULL` if not extensions are to be used.
 * @param   maximum_length      The maximum amount of bytes to write, including the
 *                              NUL byte, ignored if `limited_length` is zero.
 * @param   limited_length      Whether `maximum_length` shall be used.
 * @param   actual_length       Output parameter for the length of the printed string,
 *                              this includes any text that was truncated but not the
 *                              NUL character. Must not be `NULL`.
 * @param   terminate           Whether a NUL character shall be printed at the end.
 * @param   data                Data to pass to `write_function` and
 *                              `extension_function`, it should contain
 *                              the print-sink, and any state data neccessary
 *                              since the functions may be called multiple times.
 * @param   format              The formatting-string, see `fprintf` for details.
 * @param   ...                 The formatting-arguments.
 * @return                      Zero on success, -1 on error. On error, `errno`
 *                              is set to indicate the error.
 * 
 * @throws          Any error thrown by `write_function` or `extension_function`.
 * @throws  EINVAL  `format` contained unsupported formatting codes.
 * 
 * @since  Always.
 */
int generic_printf(generic_printf_write_func_t, generic_printf_ext_func_t,
		   size_t, int, size_t* restrict, int, void*, const char*, ...)
  __GCC_ONLY(__attribute__((__nonnull__(5, 8), __format__(__slibc_printf__, 8, 9), __warn_unused_result__)));

/**
 * Variant of `generic_printf` that uses `va_list`
 * instead of variadic arguments.
 * 
 * @etymology  (V)ariadic version of (`generic_printf`).
 * 
 * @param   write_function      Function used to write the string. `NULL` if
 *                              it shall not be printed but only measured.
 * @param   extension_function  Function used to extend the functions formatting codes.
 *                              `NULL` if not extensions are to be used.
 * @param   maximum_length      The maximum amount of bytes to write, including the
 *                              NUL byte, ignored if `limited_length` is zero.
 * @param   limited_length      Whether `maximum_length` shall be used.
 * @param   actual_length       Output parameter for the length of the printed string,
 *                              this includes any text that was truncated but not the
 *                              NUL character. Must not be `NULL`.
 * @param   terminate           Whether a NUL character shall be printed at the end.
 * @param   data                Data to pass to `write_function` and
 *                              `extension_function`, it should contain
 *                              the print-sink, and any state data neccessary
 *                              since the functions may be called multiple times.
 * @param   format              The formatting-string, see `fprintf` for details.
 * @param   args                The formatting-arguments.
 * @return                      Zero on success, -1 on error. On error, `errno`
 *                              is set to indicate the error.
 * 
 * @throws          Any error thrown by `write_function` or `extension_function`.
 * @throws  EINVAL  `format` contained unsupported formatting codes.
 * 
 * @since  Always.
 */
int vgeneric_printf(generic_printf_write_func_t, generic_printf_ext_func_t,
		    size_t, int, size_t* restrict, int, void*, const char*, va_list)
  __GCC_ONLY(__attribute__((__nonnull__(5, 8), __warn_unused_result__)));

/**
 * Variant of `generic_printf` uses `wchar_t` instead of `char`;
 * 
 * @etymology  (Generic) (`printf`)-function.
 * 
 * @param   write_function      Function used to write the string. `NULL` if
 *                              it shall not be printed but only measured.
 * @param   extension_function  Function used to extend the functions formatting codes.
 *                              `NULL` if not extensions are to be used.
 * @param   maximum_length      The maximum amount of wide characters to write,
 *                              including the NUL wide character, ignored if
 *                              `limited_length` is zero.
 * @param   limited_length      Whether `maximum_length` shall be used.
 * @param   actual_length       Output parameter for the length of the printed string,
 *                              this includes any text that was truncated but not the
 *                              NUL character. Must not be `NULL`.
 * @param   terminate           Whether a NUL character shall be printed at the end.
 * @param   data                Data to pass to `write_function` and
 *                              `extension_function`, it should contain
 *                              the print-sink, and any state data neccessary
 *                              since the functions may be called multiple times.
 * @param   format              The formatting-string, see `fprintf` for details.
 * @param   ...                 The formatting-arguments.
 * @return                      Zero on success, -1 on error. On error, `errno`
 *                              is set to indicate the error.
 * 
 * @throws          Any error thrown by `write_function` or `extension_function`.
 * @throws  EINVAL  `format` contained unsupported formatting codes.
 * 
 * @since  Always.
 */
int generic_wprintf(generic_wprintf_write_func_t, generic_wprintf_ext_func_t,
		    size_t, int, size_t* restrict, int, void*, const wchar_t*, ...)
  __GCC_ONLY(__attribute__((__nonnull__(5, 8), __warn_unused_result__)));

/**
 * Variant of `generic_wprintf` that uses `va_list`
 * instead of variadic arguments.
 * 
 * @etymology  (V)ariadic version of (`generic_wprintf`).
 * 
 * @param   write_function      Function used to write the string. `NULL` if
 *                              it shall not be printed but only measured.
 * @param   extension_function  Function used to extend the functions formatting codes.
 *                              `NULL` if not extensions are to be used.
 * @param   maximum_length      The maximum amount of wide characters to write,
 *                              including the NUL wide character, ignored if
 *                              `limited_length` is zero.
 * @param   limited_length      Whether `maximum_length` shall be used.
 * @param   actual_length       Output parameter for the length of the printed string,
 *                              this includes any text that was truncated but not the
 *                              NUL character. Must not be `NULL`.
 * @param   terminate           Whether a NUL character shall be printed at the end.
 * @param   data                Data to pass to `write_function` and
 *                              `extension_function`, it should contain
 *                              the print-sink, and any state data neccessary
 *                              since the functions may be called multiple times.
 * @param   format              The formatting-string, see `fprintf` for details.
 * @param   ...                 The formatting-arguments.
 * @return                      Zero on success, -1 on error. On error, `errno`
 *                              is set to indicate the error.
 * 
 * @throws          Any error thrown by `write_function` or `extension_function`.
 * @throws  EINVAL  `format` contained unsupported formatting codes.
 * 
 * @since  Always.
 */
int vgeneric_wprintf(generic_wprintf_write_func_t, generic_wprintf_ext_func_t,
		     size_t, int, size_t* restrict, int, void*, const wchar_t*, va_list)
  __GCC_ONLY(__attribute__((__nonnull__(5, 8), __warn_unused_result__)));



#endif
#endif