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

kDep*: no globals; dir-nt-bird.c: only main thread

File:
1 edited

Legend:

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

    r3141 r3167  
    165165/** @} */
    166166
    167 
    168 /*******************************************************************************
    169 *   Global Variables                                                           *
    170 *******************************************************************************/
    171 /** the executable name. */
    172 static const char *argv0 = "";
    173 static const char *g_pszFile = NULL;
     167/**
     168 * Globals.
     169 */
     170typedef struct KDEPOBJGLOBALS
     171{
     172    /** Core instance. */
     173    DEPGLOBALS Core;
     174    /** the executable name. */
     175    const char *argv0;
     176    /** The file.    */
     177    const char *pszFile;
     178} KDEPOBJGLOBALS;
     179/** Pointer to kDepObj globals. */
     180typedef KDEPOBJGLOBALS *PKDEPOBJGLOBALS;
     181
    174182
    175183
     
    178186 *
    179187 * @returns rc.
     188 * @param   pThis       kObjDep instance data.
    180189 * @param   rc          The return code, for making one line return statements.
    181190 * @param   pszFormat   The message format string.
     
    183192 * @todo    Promote this to kDep.c.
    184193 */
    185 static int kDepErr(int rc, const char *pszFormat, ...)
     194static int kDepErr(PKDEPOBJGLOBALS pThis, int rc, const char *pszFormat, ...)
    186195{
    187196    va_list va;
    188197    const char *psz;
    189     const char *pszName = argv0;
     198    const char *pszName = pThis->argv0;
    190199
    191200    fflush(stdout);
     
    195204        pszName = psz + 1;
    196205
    197     if (g_pszFile)
    198         fprintf(stderr, "%s: %s: error: ", pszName, g_pszFile);
     206    if (pThis->pszFile)
     207        fprintf(stderr, "%s: %s: error: ", pszName, pThis->pszFile);
    199208    else
    200209        fprintf(stderr, "%s: error: ", pszName);
     
    245254 *
    246255 * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
     256 * @param   pThis       The kDepObj instance data.
    247257 * @param   pbFile      The start of the file.
    248258 * @param   cbFile      The file size.
    249259 */
    250 int kDepObjOMFParse(const KU8 *pbFile, KSIZE cbFile)
     260int kDepObjOMFParse(PKDEPOBJGLOBALS pThis, const KU8 *pbFile, KSIZE cbFile)
    251261{
    252262    PCKDEPOMFHDR    pHdr        = (PCKDEPOMFHDR)pbFile;
     
    284294                PCKDEPOMFTHEADR pTHeadr = (PCKDEPOMFTHEADR)pHdr;
    285295                if (1 + pTHeadr->Name.cch + 1 != pHdr->cbRec)
    286                     return kDepErr(1, "%#07x - Bad %cHEADR record, length mismatch.\n",
     296                    return kDepErr(pThis, 1, "%#07x - Bad %cHEADR record, length mismatch.\n",
    287297                                   (const KU8*)pHdr - pbFile, pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L');
    288298                if (    (   pTHeadr->Name.cch > 2
     
    303313                {
    304314                    dprintf(("%cHEADR: %.*s\n", pHdr->bType == KDEPOMF_THEADR ? 'T' : 'L', pTHeadr->Name.cch, pTHeadr->Name.ach));
    305                     depAdd(pTHeadr->Name.ach, pTHeadr->Name.cch);
     315                    depAdd(&pThis->Core, pTHeadr->Name.ach, pTHeadr->Name.cch);
    306316                    iMaybeSrc++;
    307317                }
     
    315325
    316326                if (pHdr->cbRec < 2 + 1)
    317                     return kDepErr(1, "%#07x - Bad COMMENT record, too small.\n", (const KU8*)pHdr - pbFile);
     327                    return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, too small.\n", (const KU8*)pHdr - pbFile);
    318328                if (uData.pb[0] & 0x3f)
    319                     return kDepErr(1, "%#07x - Bad COMMENT record, reserved flags set.\n", (const KU8*)pHdr - pbFile);
     329                    return kDepErr(pThis, 1, "%#07x - Bad COMMENT record, reserved flags set.\n", (const KU8*)pHdr - pbFile);
    320330                uClass = uData.pb[1];
    321331                uData.pb += 2;
     
    334344                            if (pHdr->cbRec == 2 + 1)
    335345                                return 0;
    336                             return kDepErr(1, "%#07lx - Bad DEPENDENCY FILE record, length mismatch. (%u/%u)\n",
     346                            return kDepErr(pThis, 1, "%#07lx - Bad DEPENDENCY FILE record, length mismatch. (%u/%u)\n",
    337347                                           (long)((const KU8 *)pHdr - pbFile),
    338348                                           K_OFFSETOF(KDEPOMFDEPFILE, Name.ach[pDep->Name.cch]) + 1,
    339349                                           (unsigned)(pHdr->cbRec + sizeof(*pHdr)));
    340350                        }
    341                         depAdd(pDep->Name.ach, pDep->Name.cch);
     351                        depAdd(&pThis->Core, pDep->Name.ach, pDep->Name.cch);
    342352                        iSrc++;
    343353                        break;
     
    388398                    KU16 uSeg = kDepObjOMFGetIndex(&uData, &cbRecLeft);
    389399                    if (uSeg == KU16_MAX)
    390                         return kDepErr(1, "%#07lx - Bad LINNUM32 record\n", (long)((const KU8 *)pHdr - pbFile));
     400                        return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record\n", (long)((const KU8 *)pHdr - pbFile));
    391401                    K_NOREF(uGrp);
    392402
     
    405415
    406416                        if (cbRecLeft < 2+1+1+2+2+4)
    407                             return kDepErr(1, "%#07lx - Bad LINNUM32 record, too short\n", (long)((const KU8 *)pHdr - pbFile));
     417                            return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too short\n", (long)((const KU8 *)pHdr - pbFile));
    408418                        cbRecLeft  -= 2+1+1+2+2+4;
    409419                        uLine       = *uData.pu16++;
     
    421431
    422432                        if (uLine != 0)
    423                             return kDepErr(1, "%#07lx - Bad LINNUM32 record, line %#x (MBZ)\n", (long)((const KU8 *)pHdr - pbFile), uLine);
     433                            return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, line %#x (MBZ)\n", (long)((const KU8 *)pHdr - pbFile), uLine);
    424434                        cLinFiles = iLinFile = KU32_MAX;
    425435                        if (   uLinNumType == 3 /* file names table */
     
    427437                            cLinNums = 0; /* no line numbers */
    428438                        else if (uLinNumType > 4)
    429                             return kDepErr(1, "%#07lx - Bad LINNUM32 record, type %#x unknown\n", (long)((const KU8 *)pHdr - pbFile), uLinNumType);
     439                            return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, type %#x unknown\n", (long)((const KU8 *)pHdr - pbFile), uLinNumType);
    430440                    }
    431441                    else
     
    442452                        {
    443453                            if (cbRecLeft < cbEntry)
    444                                 return kDepErr(1, "%#07lx - Bad LINNUM32 record, incomplete line entry\n", (long)((const KU8 *)pHdr - pbFile));
     454                                return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete line entry\n", (long)((const KU8 *)pHdr - pbFile));
    445455
    446456                            switch (uLinNumType)
     
    482492
    483493                            if (cbRecLeft < 4+4+4)
    484                                 return kDepErr(1, "%#07lx - Bad LINNUM32 record, incomplete file/path table header\n", (long)((const KU8 *)pHdr - pbFile));
     494                                return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, incomplete file/path table header\n", (long)((const KU8 *)pHdr - pbFile));
    485495                            cbRecLeft -= 4+4+4;
    486496
     
    491501                                     uLinNumType == 3 ? "file names" : "path", cLinFiles, cLinFiles, iFirstCol, cCols));
    492502                            if (cLinFiles == KU32_MAX)
    493                                 return kDepErr(1, "%#07lx - Bad LINNUM32 record, too many file/path table entries.\n", (long)((const KU8 *)pHdr - pbFile));
     503                                return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, too many file/path table entries.\n", (long)((const KU8 *)pHdr - pbFile));
    494504                            iLinFile = 0;
    495505                        }
     
    500510                            int cbName = *uData.pb++;
    501511                            if (cbRecLeft < 1 + cbName)
    502                                 return kDepErr(1, "%#07lx - Bad LINNUM32 record, file/path table entry too long.\n", (long)((const KU8 *)pHdr - pbFile));
     512                                return kDepErr(pThis, 1, "%#07lx - Bad LINNUM32 record, file/path table entry too long.\n", (long)((const KU8 *)pHdr - pbFile));
    503513                            iLinFile++;
    504514                            dprintf(("#%" KU32_PRI": %.*s\n", iLinFile, cbName, uData.pch));
    505515                            if (uLinNumType == 3)
    506516                            {
    507                                 depAdd(uData.pch, cbName);
     517                                depAdd(&pThis->Core, uData.pch, cbName);
    508518                                iSrc++;
    509519                            }
     
    532542
    533543    if (cbLeft)
    534         return kDepErr(1, "%#07x - Unexpected EOF. cbLeft=%#x\n", (const KU8*)pHdr - pbFile, cbLeft);
     544        return kDepErr(pThis, 1, "%#07x - Unexpected EOF. cbLeft=%#x\n", (const KU8*)pHdr - pbFile, cbLeft);
    535545
    536546    if (iSrc == 0 && iMaybeSrc <= 1)
     
    574584 *
    575585 * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
     586 * @param   pThis       The kDepObj instance data.
    576587 * @param   pbSyms      Pointer to the start of the symbol section.
    577588 * @param   cbSyms      Size of the symbol section.
    578589 */
    579 int kDepObjCOFFParseCV8SymbolSection(const KU8 *pbSyms, KU32 cbSyms)
     590int kDepObjCOFFParseCV8SymbolSection(PKDEPOBJGLOBALS pThis, const KU8 *pbSyms, KU32 cbSyms)
    580591{
    581592    char const *    pchStrTab  = NULL;
     
    602613        {
    603614            fprintf(stderr, "%s: CV symbol table entry at %08" KX32_PRI " is too long; cbSyms=%#" KX32_PRI "\n",
    604                     argv0, off, cbSyms);
     615                    pThis->argv0, off, cbSyms);
    605616            return 1; /* FIXME */
    606617        }
     
    610621        {
    611622            fprintf(stderr, "%s: CV symbol table entry at %08" KX32_PRI " is too long; cbData=%#" KX32_PRI " cbSyms=%#" KX32_PRI "\n",
    612                     argv0, off, cbData, cbSyms);
     623                    pThis->argv0, off, cbData, cbSyms);
    613624            return 1; /* FIXME */
    614625        }
     
    634645                dprintf(("%06" KX32_PRI " %06" KX32_PRI ": String table\n", off, cbData));
    635646                if (pchStrTab)
    636                     fprintf(stderr, "%s: warning: Found yet another string table!\n", argv0);
     647                    fprintf(stderr, "%s: warning: Found yet another string table!\n", pThis->argv0);
    637648                pchStrTab = uData.pch;
    638649                cbStrTab = cbData;
     
    643654                dprintf(("%06" KX32_PRI " %06" KX32_PRI ": Source files\n", off, cbData));
    644655                if (uSrcFiles.pb)
    645                     fprintf(stderr, "%s: warning: Found yet another source files table!\n", argv0);
     656                    fprintf(stderr, "%s: warning: Found yet another source files table!\n", pThis->argv0);
    646657                uSrcFiles = uData;
    647658                cbSrcFiles = cbData;
     
    694705        {
    695706            fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is too long; cbSrcFiles=%#" KX32_PRI "\n",
    696                     argv0, off, cbSrcFiles);
     707                    pThis->argv0, off, cbSrcFiles);
    697708            return 1;
    698709        }
     
    703714        {
    704715            fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is too long; cbSrc=%#" KX32_PRI " cbSrcFiles=%#" KX32_PRI "\n",
    705                     argv0, off, cbSrc, cbSrcFiles);
     716                    pThis->argv0, off, cbSrc, cbSrcFiles);
    706717            return 1;
    707718        }
     
    711722        {
    712723            fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " is out side the string table; offFile=%#" KX32_PRI " cbStrTab=%#" KX32_PRI "\n",
    713                     argv0, off, offFile, cbStrTab);
     724                    pThis->argv0, off, offFile, cbStrTab);
    714725            return 1;
    715726        }
     
    719730        {
    720731            fprintf(stderr, "%s: CV source file entry at %08" KX32_PRI " has an empty file name; offFile=%#x" KX32_PRI "\n",
    721                     argv0, off, offFile);
     732                    pThis->argv0, off, offFile);
    722733            return 1;
    723734        }
     
    726737         * Display the result and add it to the dependency database.
    727738         */
    728         depAdd(pszFile, cchFile);
     739        depAdd(&pThis->Core, pszFile, cchFile);
    729740        if (u16Type == 0x0110)
    730741            dprintf(("#%03" KU32_PRI ": {todo-md5-todo} '%s'\n",
     
    753764 *
    754765 * @returns 0 on success, 1 on failure, 2 if no dependencies was found.
     766 * @param   pThis       The kDepObj instance data.
    755767 * @param   pbFile      The start of the file.
    756768 * @param   cbFile      The file size.
    757769 */
    758 int kDepObjCOFFParse(const KU8 *pbFile, KSIZE cbFile)
     770int kDepObjCOFFParse(PKDEPOBJGLOBALS pThis, const KU8 *pbFile, KSIZE cbFile)
    759771{
    760772    IMAGE_FILE_HEADER const            *pFileHdr   = (IMAGE_FILE_HEADER const *)pbFile;
     
    790802            dprintf(("CV symbol table: version=%x\n", *u.pu32));
    791803            if (*u.pu32 == 0x000000004)
    792                 rc = kDepObjCOFFParseCV8SymbolSection(u.pb, paSHdrs[iSHdr].SizeOfRawData);
     804                rc = kDepObjCOFFParseCV8SymbolSection(pThis, u.pb, paSHdrs[iSHdr].SizeOfRawData);
    793805            else
    794806                rc = 2;
     
    946958 * Read the file into memory and parse it.
    947959 */
    948 static int kDepObjProcessFile(FILE *pInput)
     960static int kDepObjProcessFile(PKDEPOBJGLOBALS pThis, FILE *pInput)
    949961{
    950962    size_t      cbFile;
     
    964976     */
    965977    if (kDepObjOMFTest(pbFile, cbFile))
    966         rc = kDepObjOMFParse(pbFile, cbFile);
     978        rc = kDepObjOMFParse(pThis, pbFile, cbFile);
    967979    else if (kDepObjCOFFTest(pbFile, cbFile))
    968         rc = kDepObjCOFFParse(pbFile, cbFile);
     980        rc = kDepObjCOFFParse(pThis, pbFile, cbFile);
    969981    else
    970982    {
    971         fprintf(stderr, "%s: error: Doesn't recognize the header of the OMF/COFF file.\n", argv0);
     983        fprintf(stderr, "%s: error: Doesn't recognize the header of the OMF/COFF file.\n", pThis->argv0);
    972984        rc = 1;
    973985    }
     
    978990
    979991
    980 static void usage(const char *a_argv0)
     992static void kDebObjUsage(const char *a_argv0)
    981993{
    982994    printf("usage: %s -o <output> -t <target> [-fqs] [-e <ignore-ext>] <OMF or COFF file>\n"
     
    9891001int kmk_builtin_kDepObj(int argc, char *argv[], char **envp)
    9901002{
    991     int         i;
     1003    int             i;
     1004    KDEPOBJGLOBALS  This;
    9921005
    9931006    /* Arguments. */
     
    10031016    int         fQuiet = 0;
    10041017
    1005     argv0 = argv[0];
     1018    /* Init instance data.   */
     1019    This.argv0  = argv[0];
     1020    This.pszFile = NULL;
    10061021
    10071022    /*
     
    10101025    if (argc <= 1)
    10111026    {
    1012         usage(argv[0]);
     1027        kDebObjUsage(argv[0]);
    10131028        return 1;
    10141029    }
     
    10331048                {
    10341049                    fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);
    1035                     usage(argv[0]);
     1050                    kDebObjUsage(argv[0]);
    10361051                    return 2;
    10371052                }
     
    11481163                 */
    11491164                case '?':
    1150                     usage(argv[0]);
     1165                    kDebObjUsage(argv[0]);
    11511166                    return 0;
    11521167                case 'V':
     
    11591174                default:
    11601175                    fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);
    1161                     usage(argv[0]);
     1176                    kDebObjUsage(argv[0]);
    11621177                    return 2;
    11631178            }
     
    12101225     * Do the parsing.
    12111226     */
    1212     i = kDepObjProcessFile(pInput);
     1227    depInit(&This.Core);
     1228    i = kDepObjProcessFile(&This, pInput);
    12131229    fclose(pInput);
    12141230
     
    12181234    if (!i)
    12191235    {
    1220         depOptimize(fFixCase, fQuiet, pszIgnoreExt);
     1236        depOptimize(&This.Core, fFixCase, fQuiet, pszIgnoreExt);
    12211237        fprintf(pOutput, "%s:", pszTarget);
    1222         depPrint(pOutput);
     1238        depPrint(&This.Core, pOutput);
    12231239        if (fStubs)
    1224             depPrintStubs(pOutput);
     1240            depPrintStubs(&This.Core, pOutput);
    12251241    }
    12261242
     
    12401256    }
    12411257
    1242     depCleanup();
     1258    depCleanup(&This.Core);
    12431259    return i;
    12441260}
Note: See TracChangeset for help on using the changeset viewer.