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

    r3159 r3167  
    2424 */
    2525
    26 /*******************************************************************************
    27 *   Header Files                                                               *
    28 *******************************************************************************/
     26
     27/*********************************************************************************************************************************
     28*   Header Files                                                                                                                 *
     29*********************************************************************************************************************************/
    2930#include "config.h"
    3031#include <stdio.h>
     
    4849
    4950
    50 /*******************************************************************************
    51 *   Defined Constants And Macros                                               *
    52 *******************************************************************************/
     51/*********************************************************************************************************************************
     52*   Defined Constants And Macros                                                                                                 *
     53*********************************************************************************************************************************/
    5354/*#define DEBUG*/
    5455#ifdef DEBUG
     
    6061#endif
    6162
    62 
    63 /*******************************************************************************
    64 *   Global Variables                                                           *
    65 *******************************************************************************/
    66 /** the executable name. */
    67 static const char *argv0 = "";
     63/*********************************************************************************************************************************
     64*   Structures and Typedefs                                                                                                      *
     65*********************************************************************************************************************************/
     66typedef struct KDEPIDBGLOBALS
     67{
     68    DEPGLOBALS  Core;
     69    const char *argv0;
     70} KDEPIDBGLOBALS;
     71typedef KDEPIDBGLOBALS *PKDEPIDBGLOBALS;
    6872
    6973
     
    7377 * @returns 0 on success.
    7478 * @returns !0 on failure.
     79 * @param   pThis           The kDepIDB instance.
    7580 * @param   pbStream        The stream bits.
    7681 * @param   cbStream        The size of the stream.
     
    7883 * @param   cchPrefix       The size of the prefix.
    7984 */
    80 static int ScanStream(KU8 *pbStream, size_t cbStream, const char *pszPrefix, size_t cchPrefix)
     85static int ScanStream(PKDEPIDBGLOBALS pThis, KU8 *pbStream, size_t cbStream, const char *pszPrefix, size_t cchPrefix)
    8186{
    8287    const KU8      *pbCur = pbStream;
     
    96101            pbCur += cchPrefix;
    97102            cchDep = strlen((const char *)pbCur);
    98             depAdd((const char *)pbCur, cchDep);
     103            depAdd(&pThis->Core, (const char *) pbCur, cchDep);
    99104            dprintf(("%05x: '%s'\n", pbCur - pbStream, pbCur));
    100105
     
    189194
    190195
    191 static int Pdb70ValidateHeader(PPDB70HDR pHdr, size_t cbFile)
     196static int Pdb70ValidateHeader(PKDEPIDBGLOBALS pThis, PPDB70HDR pHdr, size_t cbFile)
    192197{
    193198    if (pHdr->cbPage * pHdr->cPages != cbFile)
    194199    {
    195         fprintf(stderr, "%s: error: Bad PDB 2.0 header - cbPage * cPages != cbFile.\n", argv0);
     200        fprintf(stderr, "%s: error: Bad PDB 2.0 header - cbPage * cPages != cbFile.\n", pThis->argv0);
    196201        return 1;
    197202    }
    198203    if (pHdr->iStartPage >= pHdr->cPages && pHdr->iStartPage <= 0)
    199204    {
    200         fprintf(stderr, "%s: error: Bad PDB 2.0 header - iStartPage=%u cPages=%u.\n", argv0,
     205        fprintf(stderr, "%s: error: Bad PDB 2.0 header - iStartPage=%u cPages=%u.\n", pThis->argv0,
    201206                pHdr->iStartPage, pHdr->cPages);
    202207        return 1;
     
    204209    if (pHdr->iRootPages >= pHdr->cPages && pHdr->iRootPages <= 0)
    205210    {
    206         fprintf(stderr, "%s: error: Bad PDB 2.0 header - iRootPages=%u cPage=%u.\n", argv0,
     211        fprintf(stderr, "%s: error: Bad PDB 2.0 header - iRootPages=%u cPage=%u.\n", pThis->argv0,
    207212                pHdr->iStartPage, pHdr->cPages);
    208213        return 1;
     
    227232}
    228233
    229 static void *Pdb70AllocAndRead(PPDB70HDR pHdr, size_t cb, PPDB70PAGE paiPageMap)
     234static void *Pdb70AllocAndRead(PKDEPIDBGLOBALS pThis, PPDB70HDR pHdr, size_t cb, PPDB70PAGE paiPageMap)
    230235{
    231236    const size_t    cbPage = pHdr->cbPage;
     
    246251            else
    247252            {
    248                 fprintf(stderr, "%s: warning: Invalid page index %u (max %u)!\n", argv0,
     253                fprintf(stderr, "%s: warning: Invalid page index %u (max %u)!\n", pThis->argv0,
    249254                        (unsigned)off, pHdr->cPages);
    250255                memset(pbBuf + iPage * cbPage, 0, cbPage);
     
    256261    }
    257262    else
    258         fprintf(stderr, "%s: error: failed to allocate %lu bytes\n", argv0, (unsigned long)(cPages * cbPage + 1));
     263        fprintf(stderr, "%s: error: failed to allocate %lu bytes\n", pThis->argv0, (unsigned long)(cPages * cbPage + 1));
    259264    return pbBuf;
    260265}
    261266
    262 static PPDB70ROOT Pdb70AllocAndReadRoot(PPDB70HDR pHdr)
     267static PPDB70ROOT Pdb70AllocAndReadRoot(PKDEPIDBGLOBALS pThis, PPDB70HDR pHdr)
    263268{
    264269    /*
     
    267272     */
    268273    PPDB70PAGE piPageMap = (KU32 *)((KU8 *)pHdr + pHdr->iRootPages * pHdr->cbPage);
    269     PPDB70ROOT pRoot = Pdb70AllocAndRead(pHdr, pHdr->cbRoot, piPageMap);
     274    PPDB70ROOT pRoot = Pdb70AllocAndRead(pThis, pHdr, pHdr->cbRoot, piPageMap);
    270275    if (pRoot)
    271276    {
     
    275280        size_t cb = K_OFFSETOF(PDB70ROOT, aStreams[pRoot->cStreams]);
    276281        free(pRoot);
    277         pRoot = Pdb70AllocAndRead(pHdr, cb, piPageMap);
     282        pRoot = Pdb70AllocAndRead(pThis, pHdr, cb, piPageMap);
    278283        if (pRoot)
    279284        {
     
    284289                    cb += Pdb70Pages(pHdr, pRoot->aStreams[iStream].cbStream) * sizeof(PDB70PAGE);
    285290            free(pRoot);
    286             pRoot = Pdb70AllocAndRead(pHdr, cb, piPageMap);
     291            pRoot = Pdb70AllocAndRead(pThis, pHdr, cb, piPageMap);
    287292            if (pRoot)
    288293            {
     
    299304}
    300305
    301 static void *Pdb70AllocAndReadStream(PPDB70HDR pHdr, PPDB70ROOT pRoot, unsigned iStream, size_t *pcbStream)
     306static void *Pdb70AllocAndReadStream(PKDEPIDBGLOBALS pThis, PPDB70HDR pHdr, PPDB70ROOT pRoot, unsigned iStream, size_t *pcbStream)
    302307{
    303308    const size_t    cbStream = pRoot->aStreams[iStream].cbStream;
     
    306311        ||  cbStream == ~(KU32)0)
    307312    {
    308         fprintf(stderr, "%s: error: Invalid stream %d\n", argv0, iStream);
     313        fprintf(stderr, "%s: error: Invalid stream %d\n", pThis->argv0, iStream);
    309314        return NULL;
    310315    }
     
    317322    if (pcbStream)
    318323        *pcbStream = cbStream;
    319     return Pdb70AllocAndRead(pHdr, cbStream, paiPageMap);
    320 }
    321 
    322 static int Pdb70Process(KU8 *pbFile, size_t cbFile)
     324    return Pdb70AllocAndRead(pThis, pHdr, cbStream, paiPageMap);
     325}
     326
     327static int Pdb70Process(PKDEPIDBGLOBALS pThis, KU8 *pbFile, size_t cbFile)
    323328{
    324329    PPDB70HDR   pHdr = (PPDB70HDR)pbFile;
     
    334339     * Validate the header and read the root stream.
    335340     */
    336     if (Pdb70ValidateHeader(pHdr, cbFile))
    337         return 1;
    338     pRoot = Pdb70AllocAndReadRoot(pHdr);
     341    if (Pdb70ValidateHeader(pThis, pHdr, cbFile))
     342        return 1;
     343    pRoot = Pdb70AllocAndReadRoot(pThis, pHdr);
    339344    if (!pRoot)
    340345        return 1;
     
    344349     */
    345350    dprintf(("Reading the names stream....\n"));
    346     pNames = Pdb70AllocAndReadStream(pHdr, pRoot, 1, &cbStream);
     351    pNames = Pdb70AllocAndReadStream(pThis, pHdr, pRoot, 1, &cbStream);
    347352    if (pNames)
    348353    {
     
    366371                    && !memcmp(psz, "/mr/inversedeps/", sizeof("/mr/inversedeps/") - 1))
    367372                {
    368                     depAdd(psz + sizeof("/mr/inversedeps/") - 1, cch - (sizeof("/mr/inversedeps/") - 1));
     373                    depAdd(&pThis->Core, psz + sizeof("/mr/inversedeps/") - 1, cch - (sizeof("/mr/inversedeps/") - 1));
    369374                    fAdded = 1;
    370375                }
     
    408413            dprintf(("Stream #%d: %#x bytes (%#x aligned)\n", iStream, pRoot->aStreams[iStream].cbStream,
    409414                     Pdb70Align(pHdr, pRoot->aStreams[iStream].cbStream)));
    410             pbStream = (KU8 *)Pdb70AllocAndReadStream(pHdr, pRoot, iStream, &cbStream);
     415            pbStream = (KU8 *)Pdb70AllocAndReadStream(pThis, pHdr, pRoot, iStream, &cbStream);
    411416            if (pbStream)
    412417            {
    413                 rc = ScanStream(pbStream, cbStream, "/mr/inversedeps/", sizeof("/mr/inversedeps/") - 1);
     418                rc = ScanStream(pThis, pbStream, cbStream, "/mr/inversedeps/", sizeof("/mr/inversedeps/") - 1);
    414419                free(pbStream);
    415420            }
     
    485490
    486491
    487 static int Pdb20ValidateHeader(PPDB20HDR pHdr, size_t cbFile)
     492static int Pdb20ValidateHeader(PKDEPIDBGLOBALS pThis, PPDB20HDR pHdr, size_t cbFile)
    488493{
    489494    if (pHdr->cbPage * pHdr->cPages != cbFile)
    490495    {
    491         fprintf(stderr, "%s: error: Bad PDB 2.0 header - cbPage * cPages != cbFile.\n", argv0);
     496        fprintf(stderr, "%s: error: Bad PDB 2.0 header - cbPage * cPages != cbFile.\n", pThis->argv0);
    492497        return 1;
    493498    }
    494499    if (pHdr->iStartPage >= pHdr->cPages && pHdr->iStartPage <= 0)
    495500    {
    496         fprintf(stderr, "%s: error: Bad PDB 2.0 header - cbPage * cPages != cbFile.\n", argv0);
     501        fprintf(stderr, "%s: error: Bad PDB 2.0 header - cbPage * cPages != cbFile.\n", pThis->argv0);
    497502        return 1;
    498503    }
     
    507512}
    508513
    509 static void *Pdb20AllocAndRead(PPDB20HDR pHdr, size_t cb, PPDB20PAGE paiPageMap)
     514static void *Pdb20AllocAndRead(PKDEPIDBGLOBALS pThis, PPDB20HDR pHdr, size_t cb, PPDB20PAGE paiPageMap)
    510515{
    511516    size_t cPages = Pdb20Pages(pHdr, cb);
     
    524529    }
    525530    else
    526         fprintf(stderr, "%s: error: failed to allocate %lu bytes\n", argv0, (unsigned long)(cPages * pHdr->cbPage + 1));
     531        fprintf(stderr, "%s: error: failed to allocate %lu bytes\n", pThis->argv0, (unsigned long)(cPages * pHdr->cbPage + 1));
    527532    return pbBuf;
    528533}
    529534
    530 static PPDB20ROOT Pdb20AllocAndReadRoot(PPDB20HDR pHdr)
     535static PPDB20ROOT Pdb20AllocAndReadRoot(PKDEPIDBGLOBALS pThis, PPDB20HDR pHdr)
    531536{
    532537    /*
     
    534539     * (Todo: Check if we can just use the stream size..)
    535540     */
    536     PPDB20ROOT pRoot = Pdb20AllocAndRead(pHdr, sizeof(*pRoot), &pHdr->aiRootPageMap[0]);
     541    PPDB20ROOT pRoot = Pdb20AllocAndRead(pThis, pHdr, sizeof(*pRoot), &pHdr->aiRootPageMap[0]);
    537542    if (pRoot)
    538543    {
     
    540545        size_t cb = K_OFFSETOF(PDB20ROOT, aStreams[pRoot->cStreams]);
    541546        free(pRoot);
    542         pRoot = Pdb20AllocAndRead(pHdr, cb, &pHdr->aiRootPageMap[0]);
     547        pRoot = Pdb20AllocAndRead(pThis, pHdr, cb, &pHdr->aiRootPageMap[0]);
    543548        if (pRoot)
    544549        {
     
    549554                    cb += Pdb20Pages(pHdr, pRoot->aStreams[iStream].cbStream) * sizeof(PDB20PAGE);
    550555            free(pRoot);
    551             pRoot = Pdb20AllocAndRead(pHdr, cb, &pHdr->aiRootPageMap[0]);
     556            pRoot = Pdb20AllocAndRead(pThis, pHdr, cb, &pHdr->aiRootPageMap[0]);
    552557            if (pRoot)
    553558            {
     
    561566}
    562567
    563 static void *Pdb20AllocAndReadStream(PPDB20HDR pHdr, PPDB20ROOT pRoot, unsigned iStream, size_t *pcbStream)
     568static void *Pdb20AllocAndReadStream(PKDEPIDBGLOBALS pThis, PPDB20HDR pHdr, PPDB20ROOT pRoot, unsigned iStream, size_t *pcbStream)
    564569{
    565570    size_t      cbStream = pRoot->aStreams[iStream].cbStream;
     
    568573        ||  cbStream == ~(KU32)0)
    569574    {
    570         fprintf(stderr, "%s: error: Invalid stream %d\n", argv0, iStream);
     575        fprintf(stderr, "%s: error: Invalid stream %d\n", pThis->argv0, iStream);
    571576        return NULL;
    572577    }
     
    579584    if (pcbStream)
    580585        *pcbStream = cbStream;
    581     return Pdb20AllocAndRead(pHdr, cbStream, paiPageMap);
    582 }
    583 
    584 static int Pdb20Process(KU8 *pbFile, size_t cbFile)
     586    return Pdb20AllocAndRead(pThis, pHdr, cbStream, paiPageMap);
     587}
     588
     589static int Pdb20Process(PKDEPIDBGLOBALS pThis, KU8 *pbFile, size_t cbFile)
    585590{
    586591    PPDB20HDR   pHdr = (PPDB20HDR)pbFile;
     
    592597     * Validate the header and read the root stream.
    593598     */
    594     if (Pdb20ValidateHeader(pHdr, cbFile))
    595         return 1;
    596     pRoot = Pdb20AllocAndReadRoot(pHdr);
     599    if (Pdb20ValidateHeader(pThis, pHdr, cbFile))
     600        return 1;
     601    pRoot = Pdb20AllocAndReadRoot(pThis, pHdr);
    597602    if (!pRoot)
    598603        return 1;
     
    608613        if (pRoot->aStreams[iStream].cbStream == ~(KU32)0)
    609614            continue;
    610         pbStream = (KU8 *)Pdb20AllocAndReadStream(pHdr, pRoot, iStream, NULL);
     615        pbStream = (KU8 *)Pdb20AllocAndReadStream(pThis, pHdr, pRoot, iStream, NULL);
    611616        if (pbStream)
    612617        {
    613             rc = ScanStream(pbStream, pRoot->aStreams[iStream].cbStream, "/ipm/header/", sizeof("/ipm/header/") - 1);
     618            rc = ScanStream(pThis, pbStream, pRoot->aStreams[iStream].cbStream, "/ipm/header/", sizeof("/ipm/header/") - 1);
    614619            free(pbStream);
    615620        }
     
    626631 * Make an attempt at parsing a Visual C++ IDB file.
    627632 */
    628 static int ProcessIDB(FILE *pInput)
     633static int ProcessIDB(PKDEPIDBGLOBALS pThis, FILE *pInput)
    629634{
    630635    size_t      cbFile;
     
    644649     */
    645650    if (!memcmp(pbFile, PDB_SIGNATURE_700, sizeof(PDB_SIGNATURE_700)))
    646         rc = Pdb70Process(pbFile, cbFile);
     651        rc = Pdb70Process(pThis, pbFile, cbFile);
    647652    else if (!memcmp(pbFile, PDB_SIGNATURE_200, sizeof(PDB_SIGNATURE_200)))
    648         rc = Pdb20Process(pbFile, cbFile);
     653        rc = Pdb20Process(pThis, pbFile, cbFile);
    649654    else
    650655    {
    651         fprintf(stderr, "%s: error: Doesn't recognize the header of the Visual C++ IDB file.\n", argv0);
     656        fprintf(stderr, "%s: error: Doesn't recognize the header of the Visual C++ IDB file.\n", pThis->argv0);
    652657        rc = 1;
    653658    }
     
    658663
    659664
    660 static void usage(const char *a_argv0)
     665static void kDepIDBUsage(const char *a_argv0)
    661666{
    662667    printf("usage: %s -o <output> -t <target> [-fqs] <vc idb-file>\n"
     
    669674int kmk_builtin_kDepIDB(int argc, char *argv[], char **envp)
    670675{
    671     int         i;
     676    int             i;
     677    KDEPIDBGLOBALS  This;
    672678
    673679    /* Arguments. */
     
    682688    int         fQuiet = 0;
    683689
    684     argv0 = argv[0];
     690    /* Init the instance data. */
     691    This.argv0 = argv[0];
    685692
    686693    /*
     
    689696    if (argc <= 1)
    690697    {
    691         usage(argv[0]);
     698        kDepIDBUsage(This.argv0);
    692699        return 1;
    693700    }
     
    717724                    if (pOutput)
    718725                    {
    719                         fprintf(stderr, "%s: syntax error: only one output file!\n", argv[0]);
     726                        fprintf(stderr, "%s: syntax error: only one output file!\n", This.argv0);
    720727                        return 1;
    721728                    }
     
    724731                        if (++i >= argc)
    725732                        {
    726                             fprintf(stderr, "%s: syntax error: The '-o' argument is missing the filename.\n", argv[0]);
     733                            fprintf(stderr, "%s: syntax error: The '-o' argument is missing the filename.\n", This.argv0);
    727734                            return 1;
    728735                        }
     
    735742                    if (!pOutput)
    736743                    {
    737                         fprintf(stderr, "%s: error: Failed to create output file '%s'.\n", argv[0], pszOutput);
     744                        fprintf(stderr, "%s: error: Failed to create output file '%s'.\n", This.argv0, pszOutput);
    738745                        return 1;
    739746                    }
     
    748755                    if (pszTarget)
    749756                    {
    750                         fprintf(stderr, "%s: syntax error: only one target!\n", argv[0]);
     757                        fprintf(stderr, "%s: syntax error: only one target!\n", This.argv0);
    751758                        return 1;
    752759                    }
     
    756763                        if (++i >= argc)
    757764                        {
    758                             fprintf(stderr, "%s: syntax error: The '-t' argument is missing the target name.\n", argv[0]);
     765                            fprintf(stderr, "%s: syntax error: The '-t' argument is missing the target name.\n", This.argv0);
    759766                            return 1;
    760767                        }
     
    795802                 */
    796803                case '?':
    797                     usage(argv[0]);
     804                    kDepIDBUsage(This.argv0);
    798805                    return 0;
    799806                case 'V':
    800807                case 'v':
    801                     return kbuild_version(argv[0]);
     808                    return kbuild_version(This.argv0);
    802809
    803810                /*
     
    805812                 */
    806813                default:
    807                     fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);
    808                     usage(argv[0]);
     814                    fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", This.argv0, argv[i]);
     815                    kDepIDBUsage(This.argv0);
    809816                    return 1;
    810817            }
     
    815822            if (!pInput)
    816823            {
    817                 fprintf(stderr, "%s: error: Failed to open input file '%s'.\n", argv[0], argv[i]);
     824                fprintf(stderr, "%s: error: Failed to open input file '%s'.\n", This.argv0, argv[i]);
    818825                return 1;
    819826            }
     
    828835            if (++i < argc)
    829836            {
    830                 fprintf(stderr, "%s: syntax error: No arguments shall follow the input spec.\n", argv[0]);
     837                fprintf(stderr, "%s: syntax error: No arguments shall follow the input spec.\n", This.argv0);
    831838                return 1;
    832839            }
     
    840847    if (!pInput)
    841848    {
    842         fprintf(stderr, "%s: syntax error: No input!\n", argv[0]);
     849        fprintf(stderr, "%s: syntax error: No input!\n", This.argv0);
    843850        return 1;
    844851    }
    845852    if (!pOutput)
    846853    {
    847         fprintf(stderr, "%s: syntax error: No output!\n", argv[0]);
     854        fprintf(stderr, "%s: syntax error: No output!\n", This.argv0);
    848855        return 1;
    849856    }
    850857    if (!pszTarget)
    851858    {
    852         fprintf(stderr, "%s: syntax error: No target!\n", argv[0]);
     859        fprintf(stderr, "%s: syntax error: No target!\n", This.argv0);
    853860        return 1;
    854861    }
     
    857864     * Do the parsing.
    858865     */
    859     i = ProcessIDB(pInput);
     866    depInit(&This.Core);
     867    i = ProcessIDB(&This, pInput);
    860868    fclose(pInput);
    861869
     
    865873    if (!i)
    866874    {
    867         depOptimize(fFixCase, fQuiet, NULL /*pszIgnoredExt*/);
     875        depOptimize(&This.Core, fFixCase, fQuiet, NULL /*pszIgnoredExt*/);
    868876        fprintf(pOutput, "%s:", pszTarget);
    869         depPrint(pOutput);
     877        depPrint(&This.Core, pOutput);
    870878        if (fStubs)
    871             depPrintStubs(pOutput);
     879            depPrintStubs(&This.Core, pOutput);
    872880    }
    873881
     
    878886    {
    879887        i = 1;
    880         fprintf(stderr, "%s: error: Error writing to '%s'.\n", argv[0], pszOutput);
     888        fprintf(stderr, "%s: error: Error writing to '%s'.\n", This.argv0, pszOutput);
    881889    }
    882890    fclose(pOutput);
     
    884892    {
    885893        if (unlink(pszOutput))
    886             fprintf(stderr, "%s: warning: failed to remove output file '%s' on failure.\n", argv[0], pszOutput);
    887     }
    888 
    889     depCleanup();
     894            fprintf(stderr, "%s: warning: failed to remove output file '%s' on failure.\n", This.argv0, pszOutput);
     895    }
     896
     897    depCleanup(&This.Core);
    890898    return i;
    891899}
Note: See TracChangeset for help on using the changeset viewer.