Ignore:
Timestamp:
Mar 26, 2018, 10:25:56 PM (7 years ago)
Author:
bird
Message:

kmkbuiltin: funnel output thru output.c (usually via err.c).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/err.c

    r3188 r3192  
    2727*   Header Files                                                               *
    2828*******************************************************************************/
    29 #include "config.h"
     29#ifdef HAVE_CONFIG_H
     30# include "config.h"
     31#else
     32# include <stdlib.h>
     33# define snprintf _snprintf
     34#endif
    3035#include <stdio.h>
    3136#include <stdarg.h>
     
    3338#include <errno.h>
    3439#include "err.h"
     40#if !defined(KMK_BUILTIN_STANDALONE) && !defined(KWORKER)
     41# include "../output.h"
     42#endif
    3543
    3644#ifdef KBUILD_OS_WINDOWS
     
    4149#endif
    4250
    43 
    44 /** The current program name. */
    45 const char *g_progname = "kmk";
    46 
    47 
    48 int err(int eval, const char *fmt, ...)
    49 {
    50     va_list args;
    51     int error = errno;
    52 
    53     /* stderr is unbuffered, so try format the whole message and print it in
    54        one go so it won't be split by other output. */
    55     char szMsg[4096];
    56     int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
    57     if (cchMsg < (int)sizeof(szMsg) - 1 && cchMsg > 0)
    58     {
    59         int cchMsg2;
    60         va_start(args, fmt);
    61         cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
    62         va_end(args);
    63 
    64         if (   cchMsg < (int)sizeof(szMsg) - 1
    65             && cchMsg2 >= 0)
    66         {
    67             cchMsg += cchMsg2 = snprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, ": %s\n", strerror(error));
    68             if (   cchMsg < (int)sizeof(szMsg) - 1
     51int 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
    6973                && cchMsg2 >= 0)
    7074            {
    71                 fwrite(szMsg, cchMsg, 1, stderr);
     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
     112int 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);
    72154                return eval;
    73155            }
    74 
    75         }
    76 
    77     }
    78 
    79     /* fallback */
    80     fprintf(stderr, "%s: ", g_progname);
    81     va_start(args, fmt);
    82     vfprintf(stderr, fmt, args);
    83     va_end(args);
    84     fprintf(stderr, ": %s\n", strerror(error));
    85 
    86     return eval;
    87 }
    88 
    89 
    90 int errx(int eval, const char *fmt, ...)
    91 {
    92     va_list args;
    93 
    94     /* stderr is unbuffered, so try format the whole message and print it in
    95        one go so it won't be split by other output. */
    96     char szMsg[4096];
    97     int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
    98     if (cchMsg < (int)sizeof(szMsg) - 1 && cchMsg > 0)
    99     {
    100         int cchMsg2;
    101         va_start(args, fmt);
    102         cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
    103         va_end(args);
    104 
    105         if (   cchMsg < (int)sizeof(szMsg) - 1
    106             && cchMsg2 >= 0)
    107         {
    108             szMsg[cchMsg++] = '\n';
    109             fwrite(szMsg, cchMsg, 1, stderr);
     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");
    110166            return eval;
    111167        }
    112 
    113     }
    114 
    115     /* fallback */
    116     fprintf(stderr, "%s: ", g_progname);
    117     va_start(args, fmt);
    118     vfprintf(stderr, fmt, args);
    119     va_end(args);
    120     fprintf(stderr, "\n");
    121 
    122     return eval;
    123 }
    124 
    125 void warn(const char *fmt, ...)
    126 {
    127     int error = errno;
    128     va_list args;
    129 
    130     /* stderr is unbuffered, so try format the whole message and print it in
    131        one go so it won't be split by other output. */
    132     char szMsg[4096];
    133     int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
    134     if (cchMsg < (int)sizeof(szMsg) - 1 && cchMsg > 0)
    135     {
    136         int cchMsg2;
    137         va_start(args, fmt);
    138         cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
    139         va_end(args);
    140 
    141         if (   cchMsg < (int)sizeof(szMsg) - 1
    142             && cchMsg2 >= 0)
    143         {
    144             cchMsg += cchMsg2 = snprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, ": %s\n", strerror(error));
    145             if (   cchMsg < (int)sizeof(szMsg) - 1
     168    }
     169}
     170
     171void 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
    146193                && cchMsg2 >= 0)
    147194            {
    148                 fwrite(szMsg, cchMsg, 1, stderr);
     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
     231void 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);
    149273                return;
    150274            }
    151 
    152         }
    153     }
    154 
    155     /* fallback */
    156     fprintf(stderr, "%s: ", g_progname);
    157     va_start(args, fmt);
    158     vfprintf(stderr, fmt, args);
    159     va_end(args);
    160     fprintf(stderr, ": %s\n", strerror(error));
    161 }
    162 
    163 void warnx(const char *fmt, ...)
    164 {
    165     va_list args;
    166 
    167     /* stderr is unbuffered, so try format the whole message and print it in
    168        one go so it won't be split by other output. */
    169     char szMsg[4096];
    170     int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
    171     if (cchMsg < (int)sizeof(szMsg) - 1 && cchMsg > 0)
    172     {
    173         int cchMsg2;
    174         va_start(args, fmt);
    175         cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
    176         va_end(args);
    177 
    178         if (   cchMsg < (int)sizeof(szMsg) - 1
    179             && cchMsg2 >= 0)
    180         {
    181             szMsg[cchMsg++] = '\n';
    182             fwrite(szMsg, cchMsg, 1, stderr);
     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");
    183285            return;
    184286        }
    185 
    186     }
    187 
    188     /* fallback */
    189     fprintf(stderr, "%s: ", g_progname);
    190     va_start(args, fmt);
    191     vfprintf(stderr, fmt, args);
    192     va_end(args);
    193     fprintf(stderr, "\n");
    194 }
    195 
     287    }
     288}
     289
     290void 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
Note: See TracChangeset for help on using the changeset viewer.