1 | /* $Id: err.c 3192 2018-03-26 20:25:56Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * Override err.h so we get the program name right.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2005-2016 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 3 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
---|
23 | *
|
---|
24 | */
|
---|
25 |
|
---|
26 | /*******************************************************************************
|
---|
27 | * Header Files *
|
---|
28 | *******************************************************************************/
|
---|
29 | #ifdef HAVE_CONFIG_H
|
---|
30 | # include "config.h"
|
---|
31 | #else
|
---|
32 | # include <stdlib.h>
|
---|
33 | # define snprintf _snprintf
|
---|
34 | #endif
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <stdarg.h>
|
---|
37 | #include <string.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include "err.h"
|
---|
40 | #if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
|
---|
41 | # include "../output.h"
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #ifdef KBUILD_OS_WINDOWS
|
---|
45 | /* This is a trick to speed up console output on windows. */
|
---|
46 | # include "console.h"
|
---|
47 | # undef fwrite
|
---|
48 | # define fwrite maybe_con_fwrite
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | int err(PKMKBUILTINCTX pCtx, int eval, const char *fmt, ...)
|
---|
52 | {
|
---|
53 | /*
|
---|
54 | * We format into a buffer and pass that onto output.c or fwrite.
|
---|
55 | */
|
---|
56 | int error = errno;
|
---|
57 | char *pszToFree = NULL;
|
---|
58 | char szMsgStack[4096];
|
---|
59 | char *pszMsg = szMsgStack;
|
---|
60 | size_t cbMsg = sizeof(szMsgStack);
|
---|
61 | for (;;)
|
---|
62 | {
|
---|
63 | int cchMsg = snprintf(pszMsg, cbMsg, "%s: error: ", pCtx->pszProgName);
|
---|
64 | if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
|
---|
65 | {
|
---|
66 | int cchMsg2;
|
---|
67 | va_list va;
|
---|
68 | va_start(va, fmt);
|
---|
69 | cchMsg += cchMsg2 = vsnprintf(&pszMsg[cchMsg], cbMsg - cchMsg, fmt, va);
|
---|
70 | va_end(va);
|
---|
71 |
|
---|
72 | if ( cchMsg < (int)cbMsg - 1
|
---|
73 | && cchMsg2 >= 0)
|
---|
74 | {
|
---|
75 | cchMsg += cchMsg2 = snprintf(&pszMsg[cchMsg], cbMsg - cchMsg, ": %s\n", strerror(error));
|
---|
76 | if ( cchMsg < (int)cbMsg - 1
|
---|
77 | && cchMsg2 >= 0)
|
---|
78 | {
|
---|
79 | #if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
|
---|
80 | if (pCtx->pOut)
|
---|
81 | output_write_text(pCtx->pOut, 1 /*is_err*/, pszMsg, cchMsg);
|
---|
82 | else
|
---|
83 | #endif
|
---|
84 | {
|
---|
85 | fflush(stdout);
|
---|
86 | fwrite(pszMsg, cchMsg, 1, stderr);
|
---|
87 | fflush(stderr); /* paranoia */
|
---|
88 | }
|
---|
89 | if (pszToFree)
|
---|
90 | free(pszToFree);
|
---|
91 | errno = error;
|
---|
92 | return eval;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* double the buffer size and retry */
|
---|
98 | if (pszToFree)
|
---|
99 | free(pszToFree);
|
---|
100 | cbMsg *= 2;
|
---|
101 | pszToFree = malloc(cbMsg);
|
---|
102 | if (!pszToFree)
|
---|
103 | {
|
---|
104 | fprintf(stderr, "out of memory!\n");
|
---|
105 | errno = error;
|
---|
106 | return eval;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | int errx(PKMKBUILTINCTX pCtx, int eval, const char *fmt, ...)
|
---|
113 | {
|
---|
114 | /*
|
---|
115 | * We format into a buffer and pass that onto output.c or fwrite.
|
---|
116 | */
|
---|
117 | char *pszToFree = NULL;
|
---|
118 | char szMsgStack[4096];
|
---|
119 | char *pszMsg = szMsgStack;
|
---|
120 | size_t cbMsg = sizeof(szMsgStack);
|
---|
121 | for (;;)
|
---|
122 | {
|
---|
123 | int cchMsg = snprintf(pszMsg, cbMsg, "%s: error: ", pCtx->pszProgName);
|
---|
124 | if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
|
---|
125 | {
|
---|
126 | int cchMsg2;
|
---|
127 | va_list va;
|
---|
128 | va_start(va, fmt);
|
---|
129 | cchMsg += cchMsg2 = vsnprintf(&pszMsg[cchMsg], cbMsg - cchMsg, fmt, va);
|
---|
130 | va_end(va);
|
---|
131 |
|
---|
132 | if ( cchMsg < (int)cbMsg - 2
|
---|
133 | && cchMsg2 >= 0)
|
---|
134 | {
|
---|
135 | /* ensure newline */
|
---|
136 | if (pszMsg[cchMsg - 1] != '\n')
|
---|
137 | {
|
---|
138 | pszMsg[cchMsg++] = '\n';
|
---|
139 | pszMsg[cchMsg] = '\0';
|
---|
140 | }
|
---|
141 |
|
---|
142 | #if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
|
---|
143 | if (pCtx->pOut)
|
---|
144 | output_write_text(pCtx->pOut, 1 /*is_err*/, pszMsg, cchMsg);
|
---|
145 | else
|
---|
146 | #endif
|
---|
147 | {
|
---|
148 | fflush(stdout);
|
---|
149 | fwrite(pszMsg, cchMsg, 1, stderr);
|
---|
150 | fflush(stderr); /* paranoia */
|
---|
151 | }
|
---|
152 | if (pszToFree)
|
---|
153 | free(pszToFree);
|
---|
154 | return eval;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* double the buffer size and retry */
|
---|
159 | if (pszToFree)
|
---|
160 | free(pszToFree);
|
---|
161 | cbMsg *= 2;
|
---|
162 | pszToFree = malloc(cbMsg);
|
---|
163 | if (!pszToFree)
|
---|
164 | {
|
---|
165 | fprintf(stderr, "out of memory!\n");
|
---|
166 | return eval;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | void warn(PKMKBUILTINCTX pCtx, const char *fmt, ...)
|
---|
172 | {
|
---|
173 | /*
|
---|
174 | * We format into a buffer and pass that onto output.c or fwrite.
|
---|
175 | */
|
---|
176 | int error = errno;
|
---|
177 | char *pszToFree = NULL;
|
---|
178 | char szMsgStack[4096];
|
---|
179 | char *pszMsg = szMsgStack;
|
---|
180 | size_t cbMsg = sizeof(szMsgStack);
|
---|
181 | for (;;)
|
---|
182 | {
|
---|
183 | int cchMsg = snprintf(pszMsg, cbMsg, "%s: ", pCtx->pszProgName);
|
---|
184 | if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
|
---|
185 | {
|
---|
186 | int cchMsg2;
|
---|
187 | va_list va;
|
---|
188 | va_start(va, fmt);
|
---|
189 | cchMsg += cchMsg2 = vsnprintf(&pszMsg[cchMsg], cbMsg - cchMsg, fmt, va);
|
---|
190 | va_end(va);
|
---|
191 |
|
---|
192 | if ( cchMsg < (int)cbMsg - 1
|
---|
193 | && cchMsg2 >= 0)
|
---|
194 | {
|
---|
195 | cchMsg += cchMsg2 = snprintf(&pszMsg[cchMsg], cbMsg - cchMsg, ": %s\n", strerror(error));
|
---|
196 | if ( cchMsg < (int)cbMsg - 1
|
---|
197 | && cchMsg2 >= 0)
|
---|
198 | {
|
---|
199 | #if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
|
---|
200 | if (pCtx->pOut)
|
---|
201 | output_write_text(pCtx->pOut, 1 /*is_err*/, pszMsg, cchMsg);
|
---|
202 | else
|
---|
203 | #endif
|
---|
204 | {
|
---|
205 | fflush(stdout);
|
---|
206 | fwrite(pszMsg, cchMsg, 1, stderr);
|
---|
207 | fflush(stderr); /* paranoia */
|
---|
208 | }
|
---|
209 | if (pszToFree)
|
---|
210 | free(pszToFree);
|
---|
211 | errno = error;
|
---|
212 | return;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* double the buffer size and retry */
|
---|
218 | if (pszToFree)
|
---|
219 | free(pszToFree);
|
---|
220 | cbMsg *= 2;
|
---|
221 | pszToFree = malloc(cbMsg);
|
---|
222 | if (!pszToFree)
|
---|
223 | {
|
---|
224 | fprintf(stderr, "out of memory!\n");
|
---|
225 | errno = error;
|
---|
226 | return;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | void warnx(PKMKBUILTINCTX pCtx, const char *fmt, ...)
|
---|
232 | {
|
---|
233 | /*
|
---|
234 | * We format into a buffer and pass that onto output.c or fwrite.
|
---|
235 | */
|
---|
236 | char *pszToFree = NULL;
|
---|
237 | char szMsgStack[4096];
|
---|
238 | char *pszMsg = szMsgStack;
|
---|
239 | size_t cbMsg = sizeof(szMsgStack);
|
---|
240 | for (;;)
|
---|
241 | {
|
---|
242 | int cchMsg = snprintf(pszMsg, cbMsg, "%s: ", pCtx->pszProgName);
|
---|
243 | if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
|
---|
244 | {
|
---|
245 | int cchMsg2;
|
---|
246 | va_list va;
|
---|
247 | va_start(va, fmt);
|
---|
248 | cchMsg += cchMsg2 = vsnprintf(&pszMsg[cchMsg], cbMsg - cchMsg, fmt, va);
|
---|
249 | va_end(va);
|
---|
250 |
|
---|
251 | if ( cchMsg < (int)cbMsg - 2
|
---|
252 | && cchMsg2 >= 0)
|
---|
253 | {
|
---|
254 | /* ensure newline */
|
---|
255 | if (pszMsg[cchMsg - 1] != '\n')
|
---|
256 | {
|
---|
257 | pszMsg[cchMsg++] = '\n';
|
---|
258 | pszMsg[cchMsg] = '\0';
|
---|
259 | }
|
---|
260 |
|
---|
261 | #if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
|
---|
262 | if (pCtx->pOut)
|
---|
263 | output_write_text(pCtx->pOut, 1 /*is_err*/, pszMsg, cchMsg);
|
---|
264 | else
|
---|
265 | #endif
|
---|
266 | {
|
---|
267 | fflush(stdout);
|
---|
268 | fwrite(pszMsg, cchMsg, 1, stderr);
|
---|
269 | fflush(stderr); /* paranoia */
|
---|
270 | }
|
---|
271 | if (pszToFree)
|
---|
272 | free(pszToFree);
|
---|
273 | return;
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | /* double the buffer size and retry */
|
---|
278 | if (pszToFree)
|
---|
279 | free(pszToFree);
|
---|
280 | cbMsg *= 2;
|
---|
281 | pszToFree = malloc(cbMsg);
|
---|
282 | if (!pszToFree)
|
---|
283 | {
|
---|
284 | fprintf(stderr, "out of memory!\n");
|
---|
285 | return;
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | void kmk_builtin_ctx_printf(PKMKBUILTINCTX pCtx, int fIsErr, const char *pszFormat, ...)
|
---|
291 | {
|
---|
292 | /*
|
---|
293 | * We format into a buffer and pass that onto output.c or fwrite.
|
---|
294 | */
|
---|
295 | char *pszToFree = NULL;
|
---|
296 | char szMsgStack[4096];
|
---|
297 | char *pszMsg = szMsgStack;
|
---|
298 | size_t cbMsg = sizeof(szMsgStack);
|
---|
299 | for (;;)
|
---|
300 | {
|
---|
301 | int cchMsg;
|
---|
302 | va_list va;
|
---|
303 | va_start(va, pszFormat);
|
---|
304 | cchMsg = vsnprintf(pszMsg, cbMsg, pszFormat, va);
|
---|
305 | va_end(va);
|
---|
306 | if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
|
---|
307 | {
|
---|
308 | #if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
|
---|
309 | if (pCtx->pOut)
|
---|
310 | output_write_text(pCtx->pOut, fIsErr, pszMsg, cchMsg);
|
---|
311 | else
|
---|
312 | #endif
|
---|
313 | {
|
---|
314 | fwrite(pszMsg, cchMsg, 1, fIsErr ? stderr : stdout);
|
---|
315 | fflush(fIsErr ? stderr : stdout);
|
---|
316 | }
|
---|
317 | if (pszToFree)
|
---|
318 | free(pszToFree);
|
---|
319 | return;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /* double the buffer size and retry */
|
---|
323 | if (pszToFree)
|
---|
324 | free(pszToFree);
|
---|
325 | cbMsg *= 2;
|
---|
326 | pszToFree = malloc(cbMsg);
|
---|
327 | if (!pszToFree)
|
---|
328 | {
|
---|
329 | fprintf(stderr, "out of memory!\n");
|
---|
330 | return;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|