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