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

    r3167 r3192  
    3939#else
    4040# include <io.h>
     41typedef intptr_t ssize_t;
    4142#endif
    4243#include "k/kDefs.h"
     
    4445#include "k/kLdrFmts/pe.h"
    4546#include "kDep.h"
     47#include "err.h"
    4648#include "kmkbuiltin.h"
    4749
     
    170172typedef struct KDEPOBJGLOBALS
    171173{
     174    /** The command execution context. */
     175    PKMKBUILTINCTX pCtx;
    172176    /** Core instance. */
    173177    DEPGLOBALS Core;
    174     /** the executable name. */
    175     const char *argv0;
    176178    /** The file.    */
    177179    const char *pszFile;
     
    194196static int kDepErr(PKDEPOBJGLOBALS pThis, int rc, const char *pszFormat, ...)
    195197{
     198    char szMsg[2048];
    196199    va_list va;
    197     const char *psz;
    198     const char *pszName = pThis->argv0;
    199 
    200     fflush(stdout);
    201 
    202     /* The message prefix. */
    203     while ((psz = strpbrk(pszName, "/\\:")) != NULL)
    204         pszName = psz + 1;
     200    va_start(va, pszFormat);
     201    vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, va);
     202    va_end(va);
     203    szMsg[sizeof(szMsg) - 1] = '\0';
    205204
    206205    if (pThis->pszFile)
    207         fprintf(stderr, "%s: %s: error: ", pszName, pThis->pszFile);
     206        warnx(pThis->pCtx, "%s: error: %s", pThis->pszFile, szMsg);
    208207    else
    209         fprintf(stderr, "%s: error: ", pszName);
    210 
    211     /* The message. */
    212     va_start(va, pszFormat);
    213     vfprintf(stderr, pszFormat, va);
    214     va_end(va);
    215 
     208        errx(pThis->pCtx, rc, "%s", szMsg);
    216209    return rc;
    217210}
     
    294287                PCKDEPOMFTHEADR pTHeadr = (PCKDEPOMFTHEADR)pHdr;
    295288                if (1 + pTHeadr->Name.cch + 1 != pHdr->cbRec)
    296                     return kDepErr(pThis, 1, "%#07x - Bad %cHEADR record, length mismatch.\n",
     289                    return kDepErr(pThis, 1, "%#07x - Bad %cHEADR record, length mismatch.",
    297290                                   (const KU8*)pHdr - pbFile, pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L');
    298291                if (    (   pTHeadr->Name.cch > 2
     
    325318
    326319                if (pHdr->cbRec < 2 + 1)
    327                     return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, too small.\n", (const KU8*)pHdr - pbFile);
     320                    return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, too small.", (const KU8*)pHdr - pbFile);
    328321                if (uData.pb[0] & 0x3f)
    329                     return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, reserved flags set.\n", (const KU8*)pHdr - pbFile);
     322                    return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, reserved flags set.", (const KU8*)pHdr - pbFile);
    330323                uClass = uData.pb[1];
    331324                uData.pb += 2;
     
    344337                            if (pHdr->cbRec == 2 + 1)
    345338                                return 0;
    346                             return kDepErr(pThis, 1, "%#07lx - Bad DEPENDENCY FILE record, length mismatch. (%u/%u)\n",
     339                            return kDepErr(pThis, 1, "%#07lx - Bad DEPENDENCY FILE record, length mismatch. (%u/%u)",
    347340                                           (long)((const KU8 *)pHdr - pbFile),
    348341                                           K_OFFSETOF(KDEPOMFDEPFILE, Name.ach[pDep->Name.cch]) + 1,
     
    398391                    KU16 uSeg = kDepObjOMFGetIndex(&uData, &cbRecLeft);
    399392                    if (uSeg == KU16_MAX)
    400                         return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record\n", (long)((const KU8 *)pHdr - pbFile));
     393                        return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record", (long)((const KU8 *)pHdr - pbFile));
    401394                    K_NOREF(uGrp);
    402395
     
    415408
    416409                        if (cbRecLeft < 2+1+1+2+2+4)
    417                             return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too short\n", (long)((const KU8 *)pHdr - pbFile));
     410                            return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too short", (long)((const KU8 *)pHdr - pbFile));
    418411                        cbRecLeft  -= 2+1+1+2+2+4;
    419412                        uLine       = *uData.pu16++;
     
    431424
    432425                        if (uLine != 0)
    433                             return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, line %#x (MBZ)\n", (long)((const KU8 *)pHdr - pbFile), uLine);
     426                            return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, line %#x (MBZ)", (long)((const KU8 *)pHdr - pbFile), uLine);
    434427                        cLinFiles = iLinFile = KU32_MAX;
    435428                        if (   uLinNumType == 3 /* file names table */
     
    437430                            cLinNums = 0; /* no line numbers */
    438431                        else if (uLinNumType > 4)
    439                             return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, type %#x unknown\n", (long)((const KU8 *)pHdr - pbFile), uLinNumType);
     432                            return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, type %#x unknown", (long)((const KU8 *)pHdr - pbFile), uLinNumType);
    440433                    }
    441434                    else
     
    452445                        {
    453446                            if (cbRecLeft < cbEntry)
    454                                 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete line entry\n", (long)((const KU8 *)pHdr - pbFile));
     447                                return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete line entry", (long)((const KU8 *)pHdr - pbFile));
    455448
    456449                            switch (uLinNumType)
     
    492485
    493486                            if (cbRecLeft < 4+4+4)
    494                                 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete file/path table header\n", (long)((const KU8 *)pHdr - pbFile));
     487                                return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete file/path table header", (long)((const KU8 *)pHdr - pbFile));
    495488                            cbRecLeft -= 4+4+4;
    496489
     
    501494                                     uLinNumType == 3 ? "file names" : "path", cLinFiles, cLinFiles, iFirstCol, cCols));
    502495                            if (cLinFiles == KU32_MAX)
    503                                 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too many file/path table entries.\n", (long)((const KU8 *)pHdr - pbFile));
     496                                return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too many file/path table entries.", (long)((const KU8 *)pHdr - pbFile));
    504497                            iLinFile = 0;
    505498                        }
     
    510503                            int cbName = *uData.pb++;
    511504                            if (cbRecLeft < 1 + cbName)
    512                                 return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, file/path table entry too long.\n", (long)((const KU8 *)pHdr - pbFile));
     505                                return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, file/path table entry too long.", (long)((const KU8 *)pHdr - pbFile));
    513506                            iLinFile++;
    514507                            dprintf(("#%" KU32_PRI": %.*s\n", iLinFile, cbName, uData.pch));
     
    542535
    543536    if (cbLeft)
    544         return kDepErr(pThis, 1, "%#07x - Unexpected EOF. cbLeft=%#x\n", (const KU8*)pHdr - pbFile, cbLeft);
     537        return kDepErr(pThis, 1, "%#07x - Unexpected EOF. cbLeft=%#x", (const KU8*)pHdr - pbFile, cbLeft);
    545538
    546539    if (iSrc == 0 && iMaybeSrc <= 1)
     
    612605        if (off + sizeof(*pHdr) >= cbSyms)
    613606        {
    614             fprintf(stderr, "%s: CV symbol table entry at %08" KX32_PRI " is too long; cbSyms=%#" KX32_PRI "\n",
    615                     pThis->argv0, off, cbSyms);
     607            kDepErr(pThis, 1, "CV symbol table entry at %08" KX32_PRI " is too long; cbSyms=%#" KX32_PRI "",
     608                    off, cbSyms);
    616609            return 1; /* FIXME */
    617610        }
     
    620613        if (off + cbData + sizeof(*pHdr) > cbSyms)
    621614        {
    622             fprintf(stderr, "%s: CV symbol table entry at %08" KX32_PRI " is too long; cbData=%#" KX32_PRI " cbSyms=%#" KX32_PRI "\n",
    623                     pThis->argv0, off, cbData, cbSyms);
     615            kDepErr(pThis, 1, "CV symbol table entry at %08" KX32_PRI " is too long; cbData=%#" KX32_PRI " cbSyms=%#" KX32_PRI,
     616                    off, cbData, cbSyms);
    624617            return 1; /* FIXME */
    625618        }
     
    645638                dprintf(("%06" KX32_PRI " %06" KX32_PRI ": String table\n", off, cbData));
    646639                if (pchStrTab)
    647                     fprintf(stderr, "%s: warning: Found yet another string table!\n", pThis->argv0);
     640                    warnx(pThis->pCtx, "%s: warning: Found yet another string table!", pThis->pszFile);
    648641                pchStrTab = uData.pch;
    649642                cbStrTab = cbData;
     
    654647                dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Source files\n", off, cbData));
    655648                if (uSrcFiles.pb)
    656                     fprintf(stderr, "%s: warning: Found yet another source files table!\n", pThis->argv0);
     649                    warnx(pThis->pCtx, "%s: warning: Found yet another source files table!", pThis->pszFile);
    657650                uSrcFiles = uData;
    658651                cbSrcFiles = cbData;
     
    703696         */
    704697        if (off + 8 > cbSrcFiles)
    705         {
    706             fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is too long; cbSrcFiles=%#" KX32_PRI "\n",
    707                     pThis->argv0, off, cbSrcFiles);
    708             return 1;
    709         }
     698            return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " is too long; cbSrcFiles=%#" KX32_PRI,
     699                           off, cbSrcFiles);
    710700        uSrc.pb = uSrcFiles.pb + off;
    711701        u16Type = uSrc.pu16[2];
    712702        cbSrc = u16Type == 0x0110 ? 6 + 16 + 2 : 6 + 2;
    713703        if (off + cbSrc > cbSrcFiles)
    714         {
    715             fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is too long; cbSrc=%#" KX32_PRI " cbSrcFiles=%#" KX32_PRI "\n",
    716                     pThis->argv0, off, cbSrc, cbSrcFiles);
    717             return 1;
    718         }
     704            return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " is too long; cbSrc=%#" KX32_PRI " cbSrcFiles=%#" KX32_PRI,
     705                           off, cbSrc, cbSrcFiles);
    719706
    720707        offFile = *uSrc.pu32;
    721708        if (offFile > cbStrTab)
    722         {
    723             fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is out side the string table; offFile=%#" KX32_PRI " cbStrTab=%#" KX32_PRI "\n",
    724                     pThis->argv0, off, offFile, cbStrTab);
    725             return 1;
    726         }
     709            return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " is out side the string table; offFile=%#" KX32_PRI " cbStrTab=%#" KX32_PRI,
     710                    off, offFile, cbStrTab);
    727711        pszFile = pchStrTab + offFile;
    728712        cchFile = strlen(pszFile);
    729713        if (cchFile == 0)
    730         {
    731             fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " has an empty file name; offFile=%#x" KX32_PRI "\n",
    732                     pThis->argv0, off, offFile);
    733             return 1;
    734         }
     714            return kDepErr(pThis, 1, "CV source file entry at %08" KX32_PRI " has an empty file name; offFile=%#x" KX32_PRI,
     715                           off, offFile);
    735716
    736717        /*
     
    822803 * @returns K_TRUE if it's COFF, K_FALSE otherwise.
    823804 *
     805 * @param   pThis   The kDepObj instance data.
    824806 * @param   pb      The start of the file.
    825807 * @param   cb      The file size.
    826808 */
    827 KBOOL kDepObjCOFFTest(const KU8 *pbFile, KSIZE cbFile)
     809KBOOL kDepObjCOFFTest(PKDEPOBJGLOBALS pThis, const KU8 *pbFile, KSIZE cbFile)
    828810{
    829811    IMAGE_FILE_HEADER const         *pFileHdr   = (IMAGE_FILE_HEADER const *)pbFile;
     
    864846            && pBigObjHdr->Machine != IMAGE_FILE_MACHINE_EBC)
    865847        {
    866             fprintf(stderr, "kDepObj: error: bigobj Machine not supported: %#x\n", pBigObjHdr->Machine);
     848            kDepErr(pThis, 1, "bigobj Machine not supported: %#x", pBigObjHdr->Machine);
    867849            return K_FALSE;
    868850        }
    869851        if (pBigObjHdr->Flags != 0)
    870852        {
    871             fprintf(stderr, "kDepObj: error: bigobj Flags field is non-zero: %#x\n", pBigObjHdr->Flags);
     853            kDepErr(pThis, 1, "bigobj Flags field is non-zero: %#x", pBigObjHdr->Flags);
    872854            return K_FALSE;
    873855        }
    874856        if (pBigObjHdr->SizeOfData != 0)
    875857        {
    876             fprintf(stderr, "kDepObj: error: bigobj SizeOfData field is non-zero: %#x\n", pBigObjHdr->SizeOfData);
     858            kDepErr(pThis, 1, "bigobj SizeOfData field is non-zero: %#x", pBigObjHdr->SizeOfData);
    877859            return K_FALSE;
    878860        }
     
    977959    if (kDepObjOMFTest(pbFile, cbFile))
    978960        rc = kDepObjOMFParse(pThis, pbFile, cbFile);
    979     else if (kDepObjCOFFTest(pbFile, cbFile))
     961    else if (kDepObjCOFFTest(pThis, pbFile, cbFile))
    980962        rc = kDepObjCOFFParse(pThis, pbFile, cbFile);
    981963    else
    982     {
    983         fprintf(stderr, "%s: error: Doesn't recognize the header of the OMF/COFF file.\n", pThis->argv0);
    984         rc = 1;
    985     }
     964        rc = kDepErr(pThis, 1, "Doesn't recognize the header of the OMF/COFF file.");
    986965
    987966    depFreeFileMemory(pbFile, pvOpaque);
     
    990969
    991970
    992 static void kDebObjUsage(const char *a_argv0)
    993 {
    994     printf("usage: %s -o <output> -t <target> [-fqs] [-e <ignore-ext>] <OMF or COFF file>\n"
    995            "   or: %s --help\n"
    996            "   or: %s --version\n",
    997            a_argv0, a_argv0, a_argv0);
     971static void kDebObjUsage(PKMKBUILTINCTX pCtx, int fIsErr)
     972{
     973    kmk_builtin_ctx_printf(pCtx, fIsErr,
     974                           "usage: %s -o <output> -t <target> [-fqs] [-e <ignore-ext>] <OMF or COFF file>\n"
     975                           "   or: %s --help\n"
     976                           "   or: %s --version\n",
     977                           pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
    998978}
    999979
    1000980
    1001 int kmk_builtin_kDepObj(int argc, char *argv[], char **envp)
     981int kmk_builtin_kDepObj(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
    1002982{
    1003983    int             i;
     
    1017997
    1018998    /* Init instance data.   */
    1019     This.argv0  = argv[0];
     999    This.pCtx = pCtx;
    10201000    This.pszFile = NULL;
    10211001
     
    10251005    if (argc <= 1)
    10261006    {
    1027         kDebObjUsage(argv[0]);
     1007        kDebObjUsage(pCtx, 0);
    10281008        return 1;
    10291009    }
     
    10471027                else
    10481028                {
    1049                     fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);
    1050                     kDebObjUsage(argv[0]);
     1029                    errx(pCtx, 2, "Invalid argument '%s'.", argv[i]);
     1030                    kDebObjUsage(pCtx, 1);
    10511031                    return 2;
    10521032                }
     
    10671047                        pszValue = argv[i];
    10681048                    else
    1069                     {
    1070                         fprintf(stderr, "%s: syntax error: The '-%c' option takes a value.\n", argv[0], chOpt);
    1071                         return 2;
    1072                     }
     1049                        return errx(pCtx, 2, "The '-%c' option takes a value.", chOpt);
    10731050                    break;
    10741051
     
    10871064                {
    10881065                    if (pOutput)
    1089                     {
    1090                         fprintf(stderr, "%s: syntax error: only one output file!\n", argv[0]);
    1091                         return 2;
    1092                     }
     1066                        return errx(pCtx, 2, "only one output file!");
    10931067                    pszOutput = pszValue;
    10941068                    if (pszOutput[0] == '-' && !pszOutput[1])
     
    10971071                        pOutput = fopen(pszOutput, "w");
    10981072                    if (!pOutput)
    1099                     {
    1100                         fprintf(stderr, "%s: error: Failed to create output file '%s'.\n", argv[0], pszOutput);
    1101                         return 1;
    1102                     }
     1073                        return err(pCtx, 1, "Failed to create output file '%s'", pszOutput);
    11031074                    break;
    11041075                }
     
    11101081                {
    11111082                    if (pszTarget)
    1112                     {
    1113                         fprintf(stderr, "%s: syntax error: only one target!\n", argv[0]);
    1114                         return 1;
    1115                     }
     1083                        return errx(pCtx, 2, "only one target!");
    11161084                    pszTarget = pszValue;
    11171085                    break;
     
    11511119                {
    11521120                    if (pszIgnoreExt)
    1153                     {
    1154                         fprintf(stderr, "%s: syntax error: The '-e' option can only be used once!\n", argv[0]);
    1155                         return 2;
    1156                     }
     1121                        return errx(pCtx, 2, "The '-e' option can only be used once!");
    11571122                    pszIgnoreExt = pszValue;
    11581123                    break;
     
    11631128                 */
    11641129                case '?':
    1165                     kDebObjUsage(argv[0]);
     1130                    kDebObjUsage(pCtx, 0);
    11661131                    return 0;
    11671132                case 'V':
     
    11731138                 */
    11741139                default:
    1175                     fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);
    1176                     kDebObjUsage(argv[0]);
     1140                    errx(pCtx, 2, "Invalid argument '%s'.", argv[i]);
     1141                    kDebObjUsage(pCtx, 1);
    11771142                    return 2;
    11781143            }
     
    11821147            pInput = fopen(argv[i], "rb");
    11831148            if (!pInput)
    1184             {
    1185                 fprintf(stderr, "%s: error: Failed to open input file '%s'.\n", argv[0], argv[i]);
    1186                 return 1;
    1187             }
     1149                return err(pCtx, 1, "Failed to open input file '%s'", argv[i]);
    11881150            fInput = 1;
    11891151        }
     
    11951157        {
    11961158            if (++i < argc)
    1197             {
    1198                 fprintf(stderr, "%s: syntax error: No arguments shall follow the input spec.\n", argv[0]);
    1199                 return 1;
    1200             }
     1159                return errx(pCtx, 2, "No arguments shall follow the input spec.");
    12011160            break;
    12021161        }
     
    12071166     */
    12081167    if (!pInput)
    1209     {
    1210         fprintf(stderr, "%s: syntax error: No input!\n", argv[0]);
    1211         return 1;
    1212     }
     1168        return errx(pCtx, 2, "No input!");
    12131169    if (!pOutput)
    1214     {
    1215         fprintf(stderr, "%s: syntax error: No output!\n", argv[0]);
    1216         return 1;
    1217     }
     1170        return errx(pCtx, 2, "No output!");
    12181171    if (!pszTarget)
    1219     {
    1220         fprintf(stderr, "%s: syntax error: No target!\n", argv[0]);
    1221         return 1;
    1222     }
     1172        return errx(pCtx, 2, "No target!");
    12231173
    12241174    /*
     
    12451195     */
    12461196    if (!i && ferror(pOutput))
    1247     {
    1248         i = 1;
    1249         fprintf(stderr, "%s: error: Error writing to '%s'.\n", argv[0], pszOutput);
    1250     }
     1197        i = errx(pCtx, 1, "Error writing to '%s'", pszOutput);
    12511198    fclose(pOutput);
    12521199    if (i)
    12531200    {
    12541201        if (unlink(pszOutput))
    1255             fprintf(stderr, "%s: warning: failed to remove output file '%s' on failure.\n", argv[0], pszOutput);
     1202            warn(pCtx, "warning: failed to remove output file '%s' on failure.", pszOutput);
    12561203    }
    12571204
     
    12601207}
    12611208
     1209#ifdef KMK_BUILTIN_STANDALONE
     1210int main(int argc, char **argv, char **envp)
     1211{
     1212    KMKBUILTINCTX Ctx = { "kDepObj", NULL };
     1213    return kmk_builtin_kDepObj(argc, argv, envp, &Ctx);
     1214}
     1215#endif
     1216
Note: See TracChangeset for help on using the changeset viewer.