VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/err.c@ 3387

Last change on this file since 3387 was 3237, checked in by bird, 5 years ago

kmk: linux build fixing...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1/* $Id: err.c 3237 2018-12-25 04:11:26Z 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 <bird-kBuild-spamx@anduin.net>
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# ifdef HAVE_STDLIB_H
32# include <stdlib.h>
33# endif
34# ifdef HAVE_STDINT_H
35# include <stdint.h>
36# endif
37#else
38# include <stdlib.h>
39# define snprintf _snprintf
40#endif
41#include <stdio.h>
42#include <stdarg.h>
43#include <string.h>
44#include <errno.h>
45#include "err.h"
46#if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
47# include "../output.h"
48#endif
49
50#ifdef KBUILD_OS_WINDOWS
51/* This is a trick to speed up console output on windows. */
52# include "console.h"
53# undef fwrite
54# define fwrite maybe_con_fwrite
55#endif
56
57int err(PKMKBUILTINCTX pCtx, int eval, const char *fmt, ...)
58{
59 /*
60 * We format into a buffer and pass that onto output.c or fwrite.
61 */
62 int error = errno;
63 char *pszToFree = NULL;
64 char szMsgStack[4096];
65 char *pszMsg = szMsgStack;
66 size_t cbMsg = sizeof(szMsgStack);
67 for (;;)
68 {
69 int cchMsg = snprintf(pszMsg, cbMsg, "%s: error: ", pCtx->pszProgName);
70 if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
71 {
72 int cchMsg2;
73 va_list va;
74 va_start(va, fmt);
75 cchMsg += cchMsg2 = vsnprintf(&pszMsg[cchMsg], cbMsg - cchMsg, fmt, va);
76 va_end(va);
77
78 if ( cchMsg < (int)cbMsg - 1
79 && cchMsg2 >= 0)
80 {
81 cchMsg += cchMsg2 = snprintf(&pszMsg[cchMsg], cbMsg - cchMsg, ": %s\n", strerror(error));
82 if ( cchMsg < (int)cbMsg - 1
83 && cchMsg2 >= 0)
84 {
85#if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
86 if (pCtx->pOut)
87 output_write_text(pCtx->pOut, 1 /*is_err*/, pszMsg, cchMsg);
88 else
89#endif
90 {
91 fflush(stdout);
92 fwrite(pszMsg, cchMsg, 1, stderr);
93 fflush(stderr); /* paranoia */
94 }
95 if (pszToFree)
96 free(pszToFree);
97 errno = error;
98 return eval;
99 }
100 }
101 }
102
103 /* double the buffer size and retry */
104 if (pszToFree)
105 free(pszToFree);
106 cbMsg *= 2;
107 pszToFree = malloc(cbMsg);
108 if (!pszToFree)
109 {
110 fprintf(stderr, "out of memory!\n");
111 errno = error;
112 return eval;
113 }
114 }
115}
116
117
118int errx(PKMKBUILTINCTX pCtx, int eval, const char *fmt, ...)
119{
120 /*
121 * We format into a buffer and pass that onto output.c or fwrite.
122 */
123 char *pszToFree = NULL;
124 char szMsgStack[4096];
125 char *pszMsg = szMsgStack;
126 size_t cbMsg = sizeof(szMsgStack);
127 for (;;)
128 {
129 int cchMsg = snprintf(pszMsg, cbMsg, "%s: error: ", pCtx->pszProgName);
130 if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
131 {
132 int cchMsg2;
133 va_list va;
134 va_start(va, fmt);
135 cchMsg += cchMsg2 = vsnprintf(&pszMsg[cchMsg], cbMsg - cchMsg, fmt, va);
136 va_end(va);
137
138 if ( cchMsg < (int)cbMsg - 2
139 && cchMsg2 >= 0)
140 {
141 /* ensure newline */
142 if (pszMsg[cchMsg - 1] != '\n')
143 {
144 pszMsg[cchMsg++] = '\n';
145 pszMsg[cchMsg] = '\0';
146 }
147
148#if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
149 if (pCtx->pOut)
150 output_write_text(pCtx->pOut, 1 /*is_err*/, pszMsg, cchMsg);
151 else
152#endif
153 {
154 fflush(stdout);
155 fwrite(pszMsg, cchMsg, 1, stderr);
156 fflush(stderr); /* paranoia */
157 }
158 if (pszToFree)
159 free(pszToFree);
160 return eval;
161 }
162 }
163
164 /* double the buffer size and retry */
165 if (pszToFree)
166 free(pszToFree);
167 cbMsg *= 2;
168 pszToFree = malloc(cbMsg);
169 if (!pszToFree)
170 {
171 fprintf(stderr, "out of memory!\n");
172 return eval;
173 }
174 }
175}
176
177void warn(PKMKBUILTINCTX pCtx, const char *fmt, ...)
178{
179 /*
180 * We format into a buffer and pass that onto output.c or fwrite.
181 */
182 int error = errno;
183 char *pszToFree = NULL;
184 char szMsgStack[4096];
185 char *pszMsg = szMsgStack;
186 size_t cbMsg = sizeof(szMsgStack);
187 for (;;)
188 {
189 int cchMsg = snprintf(pszMsg, cbMsg, "%s: ", pCtx->pszProgName);
190 if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
191 {
192 int cchMsg2;
193 va_list va;
194 va_start(va, fmt);
195 cchMsg += cchMsg2 = vsnprintf(&pszMsg[cchMsg], cbMsg - cchMsg, fmt, va);
196 va_end(va);
197
198 if ( cchMsg < (int)cbMsg - 1
199 && cchMsg2 >= 0)
200 {
201 cchMsg += cchMsg2 = snprintf(&pszMsg[cchMsg], cbMsg - cchMsg, ": %s\n", strerror(error));
202 if ( cchMsg < (int)cbMsg - 1
203 && cchMsg2 >= 0)
204 {
205#if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
206 if (pCtx->pOut)
207 output_write_text(pCtx->pOut, 1 /*is_err*/, pszMsg, cchMsg);
208 else
209#endif
210 {
211 fflush(stdout);
212 fwrite(pszMsg, cchMsg, 1, stderr);
213 fflush(stderr); /* paranoia */
214 }
215 if (pszToFree)
216 free(pszToFree);
217 errno = error;
218 return;
219 }
220 }
221 }
222
223 /* double the buffer size and retry */
224 if (pszToFree)
225 free(pszToFree);
226 cbMsg *= 2;
227 pszToFree = malloc(cbMsg);
228 if (!pszToFree)
229 {
230 fprintf(stderr, "out of memory!\n");
231 errno = error;
232 return;
233 }
234 }
235}
236
237void warnx(PKMKBUILTINCTX pCtx, const char *fmt, ...)
238{
239 /*
240 * We format into a buffer and pass that onto output.c or fwrite.
241 */
242 char *pszToFree = NULL;
243 char szMsgStack[4096];
244 char *pszMsg = szMsgStack;
245 size_t cbMsg = sizeof(szMsgStack);
246 for (;;)
247 {
248 int cchMsg = snprintf(pszMsg, cbMsg, "%s: ", pCtx->pszProgName);
249 if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
250 {
251 int cchMsg2;
252 va_list va;
253 va_start(va, fmt);
254 cchMsg += cchMsg2 = vsnprintf(&pszMsg[cchMsg], cbMsg - cchMsg, fmt, va);
255 va_end(va);
256
257 if ( cchMsg < (int)cbMsg - 2
258 && cchMsg2 >= 0)
259 {
260 /* ensure newline */
261 if (pszMsg[cchMsg - 1] != '\n')
262 {
263 pszMsg[cchMsg++] = '\n';
264 pszMsg[cchMsg] = '\0';
265 }
266
267#if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
268 if (pCtx->pOut)
269 output_write_text(pCtx->pOut, 1 /*is_err*/, pszMsg, cchMsg);
270 else
271#endif
272 {
273 fflush(stdout);
274 fwrite(pszMsg, cchMsg, 1, stderr);
275 fflush(stderr); /* paranoia */
276 }
277 if (pszToFree)
278 free(pszToFree);
279 return;
280 }
281 }
282
283 /* double the buffer size and retry */
284 if (pszToFree)
285 free(pszToFree);
286 cbMsg *= 2;
287 pszToFree = malloc(cbMsg);
288 if (!pszToFree)
289 {
290 fprintf(stderr, "out of memory!\n");
291 return;
292 }
293 }
294}
295
296void kmk_builtin_ctx_printf(PKMKBUILTINCTX pCtx, int fIsErr, const char *pszFormat, ...)
297{
298 /*
299 * We format into a buffer and pass that onto output.c or fwrite.
300 */
301 char *pszToFree = NULL;
302 char szMsgStack[4096];
303 char *pszMsg = szMsgStack;
304 size_t cbMsg = sizeof(szMsgStack);
305 for (;;)
306 {
307 int cchMsg;
308 va_list va;
309 va_start(va, pszFormat);
310 cchMsg = vsnprintf(pszMsg, cbMsg, pszFormat, va);
311 va_end(va);
312 if (cchMsg < (int)cbMsg - 1 && cchMsg > 0)
313 {
314#if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
315 if (pCtx->pOut)
316 output_write_text(pCtx->pOut, fIsErr, pszMsg, cchMsg);
317 else
318#endif
319 {
320 fwrite(pszMsg, cchMsg, 1, fIsErr ? stderr : stdout);
321 fflush(fIsErr ? stderr : stdout);
322 }
323 if (pszToFree)
324 free(pszToFree);
325 return;
326 }
327
328 /* double the buffer size and retry */
329 if (pszToFree)
330 free(pszToFree);
331 cbMsg *= 2;
332 pszToFree = malloc(cbMsg);
333 if (!pszToFree)
334 {
335 fprintf(stderr, "out of memory!\n");
336 return;
337 }
338 }
339}
340
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use