aboutsummaryrefslogtreecommitdiffstats
path: root/libsimple/printf.h
blob: 9b6253c4efbe45bfeabfe525b61075cc7a16c57f (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* See LICENSE file for copyright and license details. */


/**
 * Version of `printf` that allocates, on the heap, a
 * sufficiently large string and writes the output to it
 * 
 * @param   strp    Output pointer for string, will be set to `NULL`
 *                  on failure, however portable applications should
 *                  assume that it may also be unmodified or a recently
 *                  freed pointer if `asprintf` is used rather than
 *                  `libsimple_asprintf` explicitly
 * @param   fmt     The format string
 * @param   ...     The format argument
 * @return          The length of the output on success, -1 on error
 * @throws  EMFILE  {FOPEN_MAX} streams are currently open in the calling process
 * @throws  ENOMEM  Could not allocate enough memory
 * @throws          Any error specified for `fprintf`
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1, 2), __format__(__printf__, 2, 3))))
int libsimple_asprintf(char **, const char *, ...);
#ifndef asprintf
# define asprintf libsimple_asprintf
#endif


/**
 * Version of `vprintf` that allocates, on the heap, a
 * sufficiently large string and writes the output to it
 * 
 * @param   strp    Output pointer for string, will be set to `NULL`
 *                  on failure, however portable applications should
 *                  assume that it may also be unmodified or a recently
 *                  freed pointer if `asprintf` is used rather than
 *                  `libsimple_asprintf` explicitly
 * @param   fmt     The format string
 * @param   ap      The format argument
 * @return          The length of the output on success, -1 on error
 * @throws  EMFILE  {FOPEN_MAX} streams are currently open in the calling process
 * @throws  ENOMEM  Could not allocate enough memory
 * @throws          Any error specified for `fprintf`
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1, 2))))
int libsimple_vasprintf(char **, const char *, va_list);
#ifndef vasprintf
# define vasprintf libsimple_vasprintf
#endif


/**
 * Version of `printf` that allocates, on the stack, a
 * sufficiently large string and writes the output to it
 * 
 * This macro does not check whether it can allocate
 * enough memory, if it cannot, the kernel may kill the
 * thread, and possibly the process, with a SIGSEGV signal
 * 
 * @param   fmt  The format string
 * @param   ...  The format argument
 * @return       The formatted string, `NULL` on error
 * @throws       Any error specified for `snprintf`
 */
#if defined(__GNUC__) || defined(__clang__)
# define libsimple_asprintfa(__fmt, ...)\
	({\
		const char *__f = (__fmt);\
		char *__ret = NULL;\
		int __r = snprintf(NULL, 0, __f, __VA_ARGS__);\
		if (__r >= 0) {\
			__ret = alloca((size_t)__r + 1);\
			sprintf(__ret, __f, __VA_ARGS__);\
		}\
		__ret;\
	})
# ifndef asprintfa
#  define asprintfa(...) libsimple_asprintfa(__VA_ARGS__)
# endif
#endif


/**
 * Version of `vprintf` that allocates, on the stack, a
 * sufficiently large string and writes the output to it
 * 
 * This macro does not check whether it can allocate
 * enough memory, if it cannot, the kernel may kill the
 * thread, and possibly the process, with a SIGSEGV signal
 * 
 * @param   fmt  The format string
 * @param   ap   The format argument
 * @return       The formatted string, `NULL` on error
 * @throws       Any error specified for `snprintf`
 */
#if defined(__GNUC__) || defined(__clang__)
# define libsimple_vasprintfa(__fmt, __ap)\
	({\
		const char *__f = (__fmt);\
		va_list __a1;\
		va_list __a2;\
		char *__ret = NULL;\
		int __r;\
		va_copy(__a1, __ap);\
		va_copy(__a2, __a1);\
		__r = vsnprintf(NULL, 0, __f, __a1);\
		if (__r >= 0) {\
			__ret = alloca((size_t)__r + 1);\
			vsprintf(__ret, __f, __a2);\
		}\
		va_end(__a2);\
		va_end(__a1);\
		__ret;\
	})
# ifndef vasprintfa
#  define vasprintfa(fmt, ap) libsimple_vasprintfa(fmt, ap)
# endif
#endif


/**
 * Version of `vprintf` for printing error message;
 * it prints to standard error (rather than standard
 * output) and, unless `fmt` starts with "usage: "
 * and unless `argv0` (global `char *`), prefixes
 * the output with `"%s: ", argv0`; additionally, if
 * `fmt` ends with ':', the output is suffixed with
 * `" %s\n", strerror(errno)`, if `fmt` ends with
 * neither ':' nor '\n', the outpt is suffixed with
 * `\n`
 * 
 * NB! This function uses `strerror` which is not
 * thread-safe
 * 
 * @param  fmt  The format string
 * @param  ap   The format argument
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1))))
void libsimple_vweprintf(const char *, va_list);
#ifndef vweprintf
# define vweprintf libsimple_vweprintf
#endif


/**
 * Version of `vprintf` for printing error message;
 * it prints to standard error (rather than standard
 * output) and, unless `fmt` starts with "usage: "
 * and unless `argv0` (global `char *`), prefixes
 * the output with `"%s: ", argv0`; additionally, if
 * `fmt` ends with ':', the output is suffixed with
 * `" %s\n", strerror(errno)`, if `fmt` ends with
 * neither ':' nor '\n', the outpt is suffixed with
 * `\n`
 * 
 * NB! This function uses `strerror` which is not
 * thread-safe
 * 
 * @param  fmt  The format string
 * @param  ...  The format argument
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1), __format__(__printf__, 1, 2))))
static inline void
libsimple_weprintf(const char *__fmt, ...)
{
	va_list __ap;
	va_start(__ap, __fmt);
	libsimple_vweprintf(__fmt, __ap);
	va_end(__ap);
}
#ifndef weprintf
# define weprintf libsimple_weprintf
#endif


/**
 * Version of `vprintf` for printing error message;
 * it prints to standard error (rather than standard
 * output) and, unless `fmt` starts with "usage: "
 * and unless `argv0` (global `char *`), prefixes
 * the output with `"%s: ", argv0`; additionally, if
 * `fmt` ends with ':', the output is suffixed with
 * `" %s\n", strerror(errno)`, if `fmt` ends with
 * neither ':' nor '\n', the outpt is suffixed with
 * `\n`
 * 
 * This function will exit the process
 * 
 * NB! This function uses `strerror` which is not
 * thread-safe
 * 
 * @param  status  Exit value for the process
 * @param  fmt     The format string
 * @param  ap      The format argument
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2), __noreturn__)))
static inline void
libsimple_venprintf(int __status, const char *__fmt, va_list __ap)
{
	libsimple_vweprintf(__fmt, __ap);
	exit(__status);
}
#ifndef venprintf
# define venprintf libsimple_venprintf
#endif


/**
 * Version of `vprintf` for printing error message;
 * it prints to standard error (rather than standard
 * output) and, unless `fmt` starts with "usage: "
 * and unless `argv0` (global `char *`), prefixes
 * the output with `"%s: ", argv0`; additionally, if
 * `fmt` ends with ':', the output is suffixed with
 * `" %s\n", strerror(errno)`, if `fmt` ends with
 * neither ':' nor '\n', the outpt is suffixed with
 * `\n`
 * 
 * This function will exit the process
 * 
 * NB! This function uses `strerror` which is not
 * thread-safe
 * 
 * @param  status  Exit value for the process
 * @param  fmt     The format string
 * @param  ...     The format argument
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2), __format__(__printf__, 2, 3), __noreturn__)))
static inline void
libsimple_enprintf(int __status, const char *__fmt, ...)
{
	va_list __ap;
	va_start(__ap, __fmt);
	libsimple_venprintf(__status, __fmt, __ap);
	va_end(__ap);
}
#ifndef enprintf
# define enprintf libsimple_enprintf
#endif


/**
 * Version of `vprintf` for printing error message;
 * it prints to standard error (rather than standard
 * output) and, unless `fmt` starts with "usage: "
 * and unless `argv0` (global `char *`), prefixes
 * the output with `"%s: ", argv0`; additionally, if
 * `fmt` ends with ':', the output is suffixed with
 * `" %s\n", strerror(errno)`, if `fmt` ends with
 * neither ':' nor '\n', the outpt is suffixed with
 * `\n`
 * 
 * This function will exit the process with the
 * value `libsimple_default_failure_exit`
 * 
 * NB! This function uses `strerror` which is not
 * thread-safe
 * 
 * @param  fmt  The format string
 * @param  ap   The format argument
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1), __noreturn__)))
static inline void
libsimple_veprintf(const char *__fmt, va_list __ap)
{
	libsimple_vweprintf(__fmt, __ap);
	exit(libsimple_default_failure_exit);
}
#ifndef veprintf
# define veprintf libsimple_veprintf
#endif


/**
 * Version of `vprintf` for printing error message;
 * it prints to standard error (rather than standard
 * output) and, unless `fmt` starts with "usage: "
 * and unless `argv0` (global `char *`), prefixes
 * the output with `"%s: ", argv0`; additionally, if
 * `fmt` ends with ':', the output is suffixed with
 * `" %s\n", strerror(errno)`, if `fmt` ends with
 * neither ':' nor '\n', the outpt is suffixed with
 * `\n`
 * 
 * This function will exit the process with the
 * value `libsimple_default_failure_exit`
 * 
 * NB! This function uses `strerror` which is not
 * thread-safe
 * 
 * @param  fmt  The format string
 * @param  ...  The format argument
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1), __format__(__printf__, 1, 2), __noreturn__)))
static inline void
libsimple_eprintf(const char *__fmt, ...)
{
	va_list __ap;
	va_start(__ap, __fmt);
	libsimple_veprintf(__fmt, __ap);
	va_end(__ap);
}
#ifndef eprintf
# define eprintf libsimple_eprintf
#endif