[370] | 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.
|
---|
[2019] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[2911] | 7 | * Copyright (c) 2005-2016 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
---|
[370] | 8 | *
|
---|
[2019] | 9 | * This file is part of kBuild.
|
---|
[370] | 10 | *
|
---|
[2019] | 11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
[370] | 12 | * it under the terms of the GNU General Public License as published by
|
---|
[2019] | 13 | * the Free Software Foundation; either version 3 of the License, or
|
---|
[370] | 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
[2019] | 16 | * kBuild is distributed in the hope that it will be useful,
|
---|
[370] | 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
|
---|
[2019] | 22 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
---|
[370] | 23 | *
|
---|
| 24 | */
|
---|
| 25 |
|
---|
[2019] | 26 | /*******************************************************************************
|
---|
| 27 | * Header Files *
|
---|
| 28 | *******************************************************************************/
|
---|
[3192] | 29 | #ifdef HAVE_CONFIG_H
|
---|
| 30 | # include "config.h"
|
---|
| 31 | #else
|
---|
| 32 | # include <stdlib.h>
|
---|
| 33 | # define snprintf _snprintf
|
---|
| 34 | #endif
|
---|
[370] | 35 | #include <stdio.h>
|
---|
| 36 | #include <stdarg.h>
|
---|
| 37 | #include <string.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include "err.h"
|
---|
[3192] | 40 | #if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
|
---|
| 41 | # include "../output.h"
|
---|
| 42 | #endif
|
---|
[370] | 43 |
|
---|
[2911] | 44 | #ifdef KBUILD_OS_WINDOWS
|
---|
| 45 | /* This is a trick to speed up console output on windows. */
|
---|
[3188] | 46 | # include "console.h"
|
---|
[2911] | 47 | # undef fwrite
|
---|
| 48 | # define fwrite maybe_con_fwrite
|
---|
| 49 | #endif
|
---|
[370] | 50 |
|
---|
[3192] | 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);
|
---|
[2911] | 71 |
|
---|
[3192] | 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 | }
|
---|
[370] | 96 |
|
---|
[3192] | 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 | }
|
---|
[370] | 110 |
|
---|
[3192] | 111 |
|
---|
| 112 | int errx(PKMKBUILTINCTX pCtx, int eval, const char *fmt, ...)
|
---|
[370] | 113 | {
|
---|
[3192] | 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 (;;)
|
---|
[2911] | 122 | {
|
---|
[3192] | 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);
|
---|
[2911] | 131 |
|
---|
[3192] | 132 | if ( cchMsg < (int)cbMsg - 2
|
---|
[2911] | 133 | && cchMsg2 >= 0)
|
---|
| 134 | {
|
---|
[3192] | 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);
|
---|
[2911] | 154 | return eval;
|
---|
| 155 | }
|
---|
[3192] | 156 | }
|
---|
[2911] | 157 |
|
---|
[3192] | 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;
|
---|
[2911] | 167 | }
|
---|
| 168 | }
|
---|
[370] | 169 | }
|
---|
| 170 |
|
---|
[3192] | 171 | void warn(PKMKBUILTINCTX pCtx, const char *fmt, ...)
|
---|
[370] | 172 | {
|
---|
[3192] | 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 (;;)
|
---|
[2911] | 182 | {
|
---|
[3192] | 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);
|
---|
[2911] | 191 |
|
---|
[3192] | 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)
|
---|
[2911] | 223 | {
|
---|
[3192] | 224 | fprintf(stderr, "out of memory!\n");
|
---|
| 225 | errno = error;
|
---|
| 226 | return;
|
---|
[2911] | 227 | }
|
---|
| 228 | }
|
---|
[370] | 229 | }
|
---|
| 230 |
|
---|
[3192] | 231 | void warnx(PKMKBUILTINCTX pCtx, const char *fmt, ...)
|
---|
[370] | 232 | {
|
---|
[3192] | 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 (;;)
|
---|
[2911] | 241 | {
|
---|
[3192] | 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);
|
---|
[2911] | 250 |
|
---|
[3192] | 251 | if ( cchMsg < (int)cbMsg - 2
|
---|
[2911] | 252 | && cchMsg2 >= 0)
|
---|
| 253 | {
|
---|
[3192] | 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);
|
---|
[2911] | 273 | return;
|
---|
| 274 | }
|
---|
[3192] | 275 | }
|
---|
[2911] | 276 |
|
---|
[3192] | 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;
|
---|
[2911] | 286 | }
|
---|
| 287 | }
|
---|
[370] | 288 | }
|
---|
| 289 |
|
---|
[3192] | 290 | void kmk_builtin_ctx_printf(PKMKBUILTINCTX pCtx, int fIsErr, const char *pszFormat, ...)
|
---|
[370] | 291 | {
|
---|
[3192] | 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 (;;)
|
---|
[2911] | 300 | {
|
---|
[3192] | 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 | }
|
---|
[2911] | 321 |
|
---|
[3192] | 322 | /* double the buffer size and retry */
|
---|
| 323 | if (pszToFree)
|
---|
| 324 | free(pszToFree);
|
---|
| 325 | cbMsg *= 2;
|
---|
| 326 | pszToFree = malloc(cbMsg);
|
---|
| 327 | if (!pszToFree)
|
---|
[2911] | 328 | {
|
---|
[3192] | 329 | fprintf(stderr, "out of memory!\n");
|
---|
[2911] | 330 | return;
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
[370] | 333 | }
|
---|
| 334 |
|
---|