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/kDepIDB.c

    r3167 r3192  
    4646#include "k/kTypes.h"
    4747#include "kDep.h"
     48#include "err.h"
    4849#include "kmkbuiltin.h"
    4950
     
    6667typedef struct KDEPIDBGLOBALS
    6768{
     69    PKMKBUILTINCTX pCtx;
    6870    DEPGLOBALS  Core;
    69     const char *argv0;
    7071} KDEPIDBGLOBALS;
    7172typedef KDEPIDBGLOBALS *PKDEPIDBGLOBALS;
     
    197198{
    198199    if (pHdr->cbPage * pHdr->cPages != cbFile)
    199     {
    200         fprintf(stderr, "%s: error: Bad PDB 2.0 header - cbPage * cPages != cbFile.\n", pThis->argv0);
    201         return 1;
    202     }
     200        return errx(pThis->pCtx, 1, "Bad PDB 2.0 header - cbPage * cPages != cbFile.");
    203201    if (pHdr->iStartPage >= pHdr->cPages && pHdr->iStartPage <= 0)
    204     {
    205         fprintf(stderr, "%s: error: Bad PDB 2.0 header - iStartPage=%u cPages=%u.\n", pThis->argv0,
    206                 pHdr->iStartPage, pHdr->cPages);
    207         return 1;
    208     }
     202        return errx(pThis->pCtx, 1, "Bad PDB 2.0 header - iStartPage=%u cPages=%u.",
     203                    pHdr->iStartPage, pHdr->cPages);
    209204    if (pHdr->iRootPages >= pHdr->cPages && pHdr->iRootPages <= 0)
    210     {
    211         fprintf(stderr, "%s: error: Bad PDB 2.0 header - iRootPages=%u cPage=%u.\n", pThis->argv0,
    212                 pHdr->iStartPage, pHdr->cPages);
    213         return 1;
    214     }
     205        return errx(pThis->pCtx, 1, "Bad PDB 2.0 header - iRootPages=%u cPage=%u.",
     206                    pHdr->iStartPage, pHdr->cPages);
    215207    return 0;
    216208}
     
    251243            else
    252244            {
    253                 fprintf(stderr, "%s: warning: Invalid page index %u (max %u)!\n", pThis->argv0,
    254                         (unsigned)off, pHdr->cPages);
     245                warnx(pThis->pCtx, "warning: Invalid page index %u (max %u)!\n", (unsigned)off, pHdr->cPages);
    255246                memset(pbBuf + iPage * cbPage, 0, cbPage);
    256247            }
     
    261252    }
    262253    else
    263         fprintf(stderr, "%s: error: failed to allocate %lu bytes\n", pThis->argv0, (unsigned long)(cPages * cbPage + 1));
     254    {
     255        errx(pThis->pCtx, 1, "failed to allocate %lu bytes", (unsigned long)(cPages * cbPage + 1));
     256        return NULL;
     257    }
    264258    return pbBuf;
    265259}
     
    311305        ||  cbStream == ~(KU32)0)
    312306    {
    313         fprintf(stderr, "%s: error: Invalid stream %d\n", pThis->argv0, iStream);
     307        errx(pThis->pCtx, 1, "Invalid stream %d", iStream);
    314308        return NULL;
    315309    }
     
    493487{
    494488    if (pHdr->cbPage * pHdr->cPages != cbFile)
    495     {
    496         fprintf(stderr, "%s: error: Bad PDB 2.0 header - cbPage * cPages != cbFile.\n", pThis->argv0);
    497         return 1;
    498     }
     489        return errx(pThis->pCtx, 1, "Bad PDB 2.0 header - cbPage * cPages != cbFile.");
    499490    if (pHdr->iStartPage >= pHdr->cPages && pHdr->iStartPage <= 0)
    500     {
    501         fprintf(stderr, "%s: error: Bad PDB 2.0 header - cbPage * cPages != cbFile.\n", pThis->argv0);
    502         return 1;
    503     }
     491        return errx(pThis->pCtx, 1, "Bad PDB 2.0 header - cbPage * cPages != cbFile.");
    504492    return 0;
    505493}
     
    529517    }
    530518    else
    531         fprintf(stderr, "%s: error: failed to allocate %lu bytes\n", pThis->argv0, (unsigned long)(cPages * pHdr->cbPage + 1));
     519        errx(pThis->pCtx, 1, "failed to allocate %lu bytes", (unsigned long)(cPages * pHdr->cbPage + 1));
    532520    return pbBuf;
    533521}
     
    573561        ||  cbStream == ~(KU32)0)
    574562    {
    575         fprintf(stderr, "%s: error: Invalid stream %d\n", pThis->argv0, iStream);
     563        errx(pThis->pCtx, 1, "Invalid stream %d", iStream);
    576564        return NULL;
    577565    }
     
    653641        rc = Pdb20Process(pThis, pbFile, cbFile);
    654642    else
    655     {
    656         fprintf(stderr, "%s: error: Doesn't recognize the header of the Visual C++ IDB file.\n", pThis->argv0);
    657         rc = 1;
    658     }
     643        rc = errx(pThis->pCtx, 1, "Doesn't recognize the header of the Visual C++ IDB file.");
    659644
    660645    depFreeFileMemory(pbFile, pvOpaque);
     
    663648
    664649
    665 static void kDepIDBUsage(const char *a_argv0)
    666 {
    667     printf("usage: %s -o <output> -t <target> [-fqs] <vc idb-file>\n"
    668            "   or: %s --help\n"
    669            "   or: %s --version\n",
    670            a_argv0, a_argv0, a_argv0);
    671 }
    672 
    673 
    674 int kmk_builtin_kDepIDB(int argc, char *argv[], char **envp)
     650static void kDepIDBUsage(PKMKBUILTINCTX pCtx, int fIsErr)
     651{
     652    kmk_builtin_ctx_printf(pCtx, fIsErr,
     653                           "usage: %s -o <output> -t <target> [-fqs] <vc idb-file>\n"
     654                           "   or: %s --help\n"
     655                           "   or: %s --version\n",
     656                           pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
     657}
     658
     659
     660int kmk_builtin_kDepIDB(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
    675661{
    676662    int             i;
     
    689675
    690676    /* Init the instance data. */
    691     This.argv0 = argv[0];
     677    This.pCtx = pCtx;
    692678
    693679    /*
     
    696682    if (argc <= 1)
    697683    {
    698         kDepIDBUsage(This.argv0);
     684        kDepIDBUsage(pCtx, 0);
    699685        return 1;
    700686    }
     
    723709                    pszOutput = &argv[i][2];
    724710                    if (pOutput)
    725                     {
    726                         fprintf(stderr, "%s: syntax error: only one output file!\n", This.argv0);
    727                         return 1;
    728                     }
     711                        return errx(pCtx, 2, "only one output file!");
    729712                    if (!*pszOutput)
    730713                    {
    731714                        if (++i >= argc)
    732                         {
    733                             fprintf(stderr, "%s: syntax error: The '-o' argument is missing the filename.\n", This.argv0);
    734                             return 1;
    735                         }
     715                            return errx(pCtx, 2, "The '-o' argument is missing the filename.");
    736716                        pszOutput = argv[i];
    737717                    }
     
    741721                        pOutput = fopen(pszOutput, "w" KMK_FOPEN_NO_INHERIT_MODE);
    742722                    if (!pOutput)
    743                     {
    744                         fprintf(stderr, "%s: error: Failed to create output file '%s'.\n", This.argv0, pszOutput);
    745                         return 1;
    746                     }
     723                        return err(pCtx, 1, "Failed to create output file '%s'", pszOutput);
    747724                    break;
    748725                }
     
    754731                {
    755732                    if (pszTarget)
    756                     {
    757                         fprintf(stderr, "%s: syntax error: only one target!\n", This.argv0);
    758                         return 1;
    759                     }
     733                        return errx(pCtx, 2, "only one target!");
    760734                    pszTarget = &argv[i][2];
    761735                    if (!*pszTarget)
    762736                    {
    763737                        if (++i >= argc)
    764                         {
    765                             fprintf(stderr, "%s: syntax error: The '-t' argument is missing the target name.\n", This.argv0);
    766                             return 1;
    767                         }
     738                            return errx(pCtx, 2, "The '-t' argument is missing the target name.");
    768739                        pszTarget = argv[i];
    769740                    }
     
    802773                 */
    803774                case '?':
    804                     kDepIDBUsage(This.argv0);
     775                    kDepIDBUsage(pCtx, 0);
    805776                    return 0;
    806777                case 'V':
    807778                case 'v':
    808                     return kbuild_version(This.argv0);
     779                    return kbuild_version(pCtx->pszProgName);
    809780
    810781                /*
     
    812783                 */
    813784                default:
    814                     fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", This.argv0, argv[i]);
    815                     kDepIDBUsage(This.argv0);
    816                     return 1;
     785                    errx(pCtx, 2, "Invalid argument '%s.'", argv[i]);
     786                    kDepIDBUsage(pCtx, 1);
     787                    return 2;
    817788            }
    818789        }
     
    821792            pInput = fopen(argv[i], "rb" KMK_FOPEN_NO_INHERIT_MODE);
    822793            if (!pInput)
    823             {
    824                 fprintf(stderr, "%s: error: Failed to open input file '%s'.\n", This.argv0, argv[i]);
    825                 return 1;
    826             }
     794                return err(pCtx, 1, "Failed to open input file '%s'", argv[i]);
    827795            fInput = 1;
    828796        }
     
    834802        {
    835803            if (++i < argc)
    836             {
    837                 fprintf(stderr, "%s: syntax error: No arguments shall follow the input spec.\n", This.argv0);
    838                 return 1;
    839             }
     804                return errx(pCtx, 2, "No arguments shall follow the input spec.");
    840805            break;
    841806        }
     
    846811     */
    847812    if (!pInput)
    848     {
    849         fprintf(stderr, "%s: syntax error: No input!\n", This.argv0);
    850         return 1;
    851     }
     813        return errx(pCtx, 2, "No input!");
    852814    if (!pOutput)
    853     {
    854         fprintf(stderr, "%s: syntax error: No output!\n", This.argv0);
    855         return 1;
    856     }
     815        return errx(pCtx, 2, "No output!");
    857816    if (!pszTarget)
    858     {
    859         fprintf(stderr, "%s: syntax error: No target!\n", This.argv0);
    860         return 1;
    861     }
     817        return errx(pCtx, 2, "No target!");
    862818
    863819    /*
     
    884840     */
    885841    if (!i && ferror(pOutput))
    886     {
    887         i = 1;
    888         fprintf(stderr, "%s: error: Error writing to '%s'.\n", This.argv0, pszOutput);
    889     }
     842        i = errx(pCtx, 1, "Error writing to '%s'.", pszOutput);
    890843    fclose(pOutput);
    891844    if (i)
    892845    {
    893846        if (unlink(pszOutput))
    894             fprintf(stderr, "%s: warning: failed to remove output file '%s' on failure.\n", This.argv0, pszOutput);
     847            warnx(pCtx, "warning: failed to remove output file '%s' on failure.", pszOutput);
    895848    }
    896849
     
    899852}
    900853
     854#ifdef KMK_BUILTIN_STANDALONE
     855int main(int argc, char **argv, char **envp)
     856{
     857    KMKBUILTINCTX Ctx = { "kmk_kDepIDB", NULL };
     858    return kmk_builtin_kDepIDB(argc, argv, envp, &Ctx);
     859}
     860#endif
     861
Note: See TracChangeset for help on using the changeset viewer.