Changeset 3167 for trunk/src


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

Location:
trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kDepPre/kDepPre.c

    r3065 r3167  
    5656 * @returns 0 on success.
    5757 * @returns 1 or other approriate exit code on failure.
     58 * @param   pThis       Pointer to the 'dep' instance.
    5859 * @param   pInput      Input stream. (probably not seekable)
    5960 */
    60 static int ParseCPrecompiler(FILE *pInput)
     61static int ParseCPrecompiler(PDEPGLOBALS pThis, FILE *pInput)
    6162{
    6263    enum
     
    186187                            ||  pDep->cchFilename != cchFilename
    187188                            ||  memcmp(pDep->szFilename, szBuf, cchFilename))
    188                             pDep = depAdd(szBuf, cchFilename);
     189                            pDep = depAdd(pThis, szBuf, cchFilename);
    189190                        break;
    190191                    }
     
    225226{
    226227    int         i;
     228    DEPGLOBALS  This;
    227229
    228230    /* Arguments. */
     
    450452     * Do the parsing.
    451453     */
    452     i = ParseCPrecompiler(pInput);
     454    depInit(&This);
     455    i = ParseCPrecompiler(&This, pInput);
    453456
    454457    /*
     
    465468    if (!i)
    466469    {
    467         depOptimize(fFixCase, 0 /* fQuiet */, NULL /*pszIgnoredExt*/);
     470        depOptimize(&This, fFixCase, 0 /* fQuiet */, NULL /*pszIgnoredExt*/);
    468471        fprintf(pOutput, "%s:", pszTarget);
    469         depPrint(pOutput);
     472        depPrint(&This, pOutput);
    470473        if (fStubs)
    471             depPrintStubs(pOutput);
     474            depPrintStubs(&This, pOutput);
    472475    }
    473476
     
    487490    }
    488491
     492    depCleanup(&This);
     493
    489494    return i;
    490495}
  • trunk/src/kObjCache/kObjCache.c

    r3110 r3167  
    807807    /** The current dependency file. */
    808808    PDEP pCurDep;
     809    /** The core dependency collector state. */
     810    DEPGLOBALS Core;
    809811} KOCDEP;
    810812/** Pointer to a KOCDEP.  */
     
    824826    pDepState->pszFilename = NULL;
    825827    pDepState->pCurDep = NULL;
     828    depInit(&pDepState->Core);
    826829}
    827830
     
    837840    free(pDepState->pszFilename);
    838841    pDepState->pszFilename = NULL;
    839     depCleanup();
     842    depCleanup(&pDepState->Core);
    840843}
    841844
     
    908911        || cchFilename != pDepState->pCurDep->cchFilename
    909912        || strcmp(pDepState->pszFilename, pDepState->pCurDep->szFilename))
    910         pDepState->pCurDep = depAdd(pDepState->pszFilename, cchFilename);
     913        pDepState->pCurDep = depAdd(&pDepState->Core, pDepState->pszFilename, cchFilename);
    911914}
    912915
     
    10701073                            || cchFilename != pDepState->pCurDep->cchFilename
    10711074                            || strcmp(pDepState->pszFilename, pDepState->pCurDep->szFilename))
    1072                             pDepState->pCurDep = depAdd(pDepState->pszFilename, cchFilename);
     1075                            pDepState->pCurDep = depAdd(&pDepState->Core, pDepState->pszFilename, cchFilename);
    10731076                        pDepState->offFilename = 0;
    10741077                        break;
     
    11121115        FatalMsg("Failed to open dependency file '%s': %s\n", pszFilename, strerror(errno));
    11131116
    1114     depOptimize(fFixCase, fQuiet, NULL /*pszIgnoredExt*/);
     1117    depOptimize(&pDepState->Core, fFixCase, fQuiet, NULL /*pszIgnoredExt*/);
    11151118
    11161119    /* Make object file name with unix slashes. */
     
    11221125    fprintf(pFile, "%s:", pszObjFileAbs);
    11231126    free(pszObjFileAbs);
    1124     depPrint(pFile);
     1127    depPrint(&pDepState->Core, pFile);
    11251128    if (fGenStubs)
    1126         depPrintStubs(pFile);
     1129        depPrintStubs(&pDepState->Core, pFile);
    11271130
    11281131    if (fclose(pFile) != 0)
  • trunk/src/kmk/dir-nt-bird.c

    r3140 r3167  
    3838# include <glob.h>
    3939#endif
    40 
     40#include <assert.h>
    4141
    4242#include "nt_fullpath.h" /* for the time being - will be implemented here later on. */
     
    8080 * from the sources.  */
    8181static KBOOL g_fFsCacheIsUsingCustomRevision = K_FALSE;
     82/** The ID of the main thread.  We currently only let it access the cache. */
     83static DWORD g_idMainThread = 0;
    8284
    8385
    8486void hash_init_directories(void)
    8587{
     88    g_idMainThread = GetCurrentThreadId();
    8689    g_pFsCache = kFsCacheCreate(0);
    8790    if (g_pFsCache)
     
    110113    KFSLOOKUPERROR  enmError;
    111114    PKFSOBJ         pDirObj = kFsCacheLookupA(g_pFsCache, pszDir, &enmError);
     115    assert(GetCurrentThreadId() == g_idMainThread);
    112116    if (pDirObj)
    113117    {
     118
    114119        if (pDirObj->bObjType == KFSOBJ_TYPE_DIR)
    115120        {
     
    145150int file_exists_p(const char *pszPath)
    146151{
     152    int fRc;
     153    if (GetCurrentThreadId() == g_idMainThread)
     154    {
     155        KFSLOOKUPERROR  enmError;
     156        PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
     157        if (pPathObj)
     158        {
     159            fRc = pPathObj->bObjType != KFSOBJ_TYPE_MISSING;
     160            kFsCacheObjRelease(g_pFsCache, pPathObj);
     161        }
     162        else
     163            fRc = 0;
     164    }
     165    else
     166        fRc = GetFileAttributesA(pszPath) != INVALID_FILE_ATTRIBUTES;
     167    return fRc;
     168}
     169
     170
     171/**
     172 * Just a way for vpath.c to get a correctly cased path, I think.
     173 *
     174 * @returns Directory path in string cache.
     175 * @param   pszDir      The directory.
     176 */
     177const char *dir_name(const char *pszDir)
     178{
     179    char szTmp[MAX_PATH];
     180    assert(GetCurrentThreadId() == g_idMainThread);
     181    nt_fullpath(pszDir, szTmp, sizeof(szTmp));
     182    return strcache_add(szTmp);
     183}
     184
     185
     186/**
     187 * Makes future file_impossible_p calls return 1 for pszPath.
     188 */
     189void file_impossible(const char *pszPath)
     190{
     191    KFSLOOKUPERROR  enmError;
     192    PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
     193    assert(GetCurrentThreadId() == g_idMainThread);
     194    if (pPathObj)
     195    {
     196        kFsCacheObjAddUserData(g_pFsCache, pPathObj, KMK_DIR_NT_IMPOSSIBLE_KEY, sizeof(KFSUSERDATA));
     197        kFsCacheObjRelease(g_pFsCache, pPathObj);
     198    }
     199}
     200
     201/**
     202 * Makes future file_impossible_p calls return 1 for pszPath.
     203 */
     204int file_impossible_p(const char *pszPath)
     205{
    147206    int             fRc;
    148207    KFSLOOKUPERROR  enmError;
    149208    PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
     209    assert(GetCurrentThreadId() == g_idMainThread);
    150210    if (pPathObj)
    151211    {
    152         fRc = pPathObj->bObjType != KFSOBJ_TYPE_MISSING;
     212        fRc = kFsCacheObjGetUserData(g_pFsCache, pPathObj, KMK_DIR_NT_IMPOSSIBLE_KEY) != NULL;
    153213        kFsCacheObjRelease(g_pFsCache, pPathObj);
    154214    }
     
    160220
    161221/**
    162  * Just a way for vpath.c to get a correctly cased path, I think.
    163  *
    164  * @returns Directory path in string cache.
    165  * @param   pszDir      The directory.
    166  */
    167 const char *dir_name(const char *pszDir)
    168 {
    169     char szTmp[MAX_PATH];
    170     nt_fullpath(pszDir, szTmp, sizeof(szTmp));
    171     return strcache_add(szTmp);
    172 }
    173 
    174 
    175 /**
    176  * Makes future file_impossible_p calls return 1 for pszPath.
    177  */
    178 void file_impossible(const char *pszPath)
    179 {
    180     KFSLOOKUPERROR  enmError;
    181     PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
    182     if (pPathObj)
    183     {
    184         kFsCacheObjAddUserData(g_pFsCache, pPathObj, KMK_DIR_NT_IMPOSSIBLE_KEY, sizeof(KFSUSERDATA));
    185         kFsCacheObjRelease(g_pFsCache, pPathObj);
    186     }
    187 }
    188 
    189 /**
    190  * Makes future file_impossible_p calls return 1 for pszPath.
    191  */
    192 int file_impossible_p(const char *pszPath)
    193 {
    194     int             fRc;
    195     KFSLOOKUPERROR  enmError;
    196     PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
    197     if (pPathObj)
    198     {
    199         fRc = kFsCacheObjGetUserData(g_pFsCache, pPathObj, KMK_DIR_NT_IMPOSSIBLE_KEY) != NULL;
    200         kFsCacheObjRelease(g_pFsCache, pPathObj);
    201     }
    202     else
    203         fRc = 0;
    204     return fRc;
    205 }
    206 
    207 
    208 /**
    209222 * opendir for glob.
    210223 *
     
    216229    KFSLOOKUPERROR  enmError;
    217230    PKFSOBJ         pDirObj = kFsCacheLookupA(g_pFsCache, pszDir, &enmError);
     231    assert(GetCurrentThreadId() == g_idMainThread);
    218232    if (pDirObj)
    219233    {
     
    244258    KMKNTOPENDIR *pDir = (KMKNTOPENDIR *)pvDir;
    245259    KU32 const    cChildren = pDir->pDir->cChildren;
     260    assert(GetCurrentThreadId() == g_idMainThread);
    246261    while (pDir->idxNext < cChildren)
    247262    {
     
    307322{
    308323    KMKNTOPENDIR *pDir = (KMKNTOPENDIR *)pvDir;
     324    assert(GetCurrentThreadId() == g_idMainThread);
    309325    kFsCacheObjRelease(g_pFsCache, &pDir->pDir->Obj);
    310326    pDir->pDir = NULL;
     
    324340    KFSLOOKUPERROR  enmError;
    325341    PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
     342    assert(GetCurrentThreadId() == g_idMainThread);
    326343/** @todo follow symlinks vs. on symlink!   */
    327344    if (pPathObj)
     
    352369    KFSLOOKUPERROR  enmError;
    353370    PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
     371    assert(GetCurrentThreadId() == g_idMainThread);
    354372    if (pPathObj)
    355373    {
     
    384402    KFSLOOKUPERROR  enmError;
    385403    PKFSOBJ         pDirObj = kFsCacheLookupA(g_pFsCache, pszDir, &enmError);
     404    assert(GetCurrentThreadId() == g_idMainThread);
    386405    if (pDirObj)
    387406    {
     
    403422void dir_setup_glob(glob_t *pGlob)
    404423{
     424    assert(GetCurrentThreadId() == g_idMainThread);
    405425    pGlob->gl_opendir   = dir_glob_opendir;
    406426    pGlob->gl_readdir   = dir_glob_readdir;
     
    465485    KFSLOOKUPERROR  enmError;
    466486    PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
     487    assert(GetCurrentThreadId() == g_idMainThread);
    467488    if (pPathObj)
    468489    {
     
    558579    /* Currently a little expensive, so just hit the file system once the
    559580       jobs starts comming in. */
     581    assert(GetCurrentThreadId() == g_idMainThread);
    560582    if (g_cInvalidates == 0)
    561583    {
     
    589611void dir_cache_invalid_after_job(void)
    590612{
     613    assert(GetCurrentThreadId() == g_idMainThread);
    591614    g_cInvalidates++;
    592615    if (g_fFsCacheIsUsingCustomRevision)
     
    603626void dir_cache_invalid_all(void)
    604627{
     628    assert(GetCurrentThreadId() == g_idMainThread);
    605629    g_cInvalidates++;
    606630    kFsCacheInvalidateAll(g_pFsCache);
     
    614638void dir_cache_invalid_missing(void)
    615639{
     640    assert(GetCurrentThreadId() == g_idMainThread);
    616641    g_cInvalidates++;
    617642    kFsCacheInvalidateAll(g_pFsCache);
     
    625650void dir_cache_invalid_volatile(void)
    626651{
     652    assert(GetCurrentThreadId() == g_idMainThread);
    627653    g_cInvalidates++;
    628654    if (g_fFsCacheIsUsingCustomRevision)
     
    644670    KFSLOOKUPERROR enmError;
    645671    PKFSOBJ pObj = kFsCacheLookupA(g_pFsCache, pszDir, &enmError);
     672    assert(GetCurrentThreadId() == g_idMainThread);
    646673    if (pObj)
    647674    {
     
    670697int dir_cache_deleted_directory(const char *pszDir)
    671698{
     699    assert(GetCurrentThreadId() == g_idMainThread);
    672700    if (kFsCacheInvalidateDeletedDirectoryA(g_pFsCache, pszDir))
    673701        return 0;
     
    678706int kmk_builtin_dircache(int argc, char **argv, char **envp)
    679707{
     708    assert(GetCurrentThreadId() == g_idMainThread);
    680709    if (argc >= 2)
    681710    {
  • 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}
  • 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}
  • trunk/src/lib/kDep.c

    r3140 r3167  
    7373
    7474
    75 /*******************************************************************************
    76 *   Global Variables                                                           *
    77 *******************************************************************************/
    78 /** List of dependencies. */
    79 static PDEP g_pDeps = NULL;
     75/**
     76 * Initializes the dep instance.
     77 *
     78 * @param   pThis       The dep instance to init.
     79 */
     80void depInit(PDEPGLOBALS pThis)
     81{
     82    pThis->pDeps = NULL;
     83}
     84
     85
     86/**
     87 * Cleans up the dep instance (frees resources).
     88 *
     89 * @param   pThis       The dep instance to cleanup.
     90 */
     91void depCleanup(PDEPGLOBALS pThis)
     92{
     93    PDEP pDep = pThis->pDeps;
     94    pThis->pDeps = NULL;
     95    while (pDep)
     96    {
     97        PDEP pFree = pDep;
     98        pDep = pDep->pNext;
     99        free(pFree);
     100    }
     101}
    80102
    81103
     
    196218 * 'Optimizes' and corrects the dependencies.
    197219 */
    198 void depOptimize(int fFixCase, int fQuiet, const char *pszIgnoredExt)
     220void depOptimize(PDEPGLOBALS pThis, int fFixCase, int fQuiet, const char *pszIgnoredExt)
    199221{
    200222    /*
     
    202224     */
    203225    size_t  cchIgnoredExt = pszIgnoredExt ? strlen(pszIgnoredExt) : 0;
    204     PDEP    pDepOrg = g_pDeps;
    205     PDEP    pDep = g_pDeps;
    206     g_pDeps = NULL;
     226    PDEP    pDepOrg = pThis->pDeps;
     227    PDEP    pDep = pThis->pDeps;
     228    pThis->pDeps = NULL;
    207229    for (; pDep; pDep = pDep->pNext)
    208230    {
     
    287309         * Insert the corrected dependency.
    288310         */
    289         depAdd(pszFilename, strlen(pszFilename));
     311        depAdd(pThis, pszFilename, strlen(pszFilename));
    290312    }
    291313
     
    305327 * Prints the dependency chain.
    306328 *
    307  * @returns Pointer to the allocated dependency.
     329 * @param   pThis       The 'dep' instance.
    308330 * @param   pOutput     Output stream.
    309331 */
    310 void depPrint(FILE *pOutput)
     332void depPrint(PDEPGLOBALS pThis, FILE *pOutput)
    311333{
    312334    PDEP pDep;
    313     for (pDep = g_pDeps; pDep; pDep = pDep->pNext)
     335    for (pDep = pThis->pDeps; pDep; pDep = pDep->pNext)
    314336        fprintf(pOutput, " \\\n\t%s", pDep->szFilename);
    315337    fprintf(pOutput, "\n\n");
     
    319341/**
    320342 * Prints empty dependency stubs for all dependencies.
    321  */
    322 void depPrintStubs(FILE *pOutput)
     343 *
     344 * @param   pThis       The 'dep' instance.
     345 * @param   pOutput     Output stream.
     346 */
     347void depPrintStubs(PDEPGLOBALS pThis, FILE *pOutput)
    323348{
    324349    PDEP pDep;
    325     for (pDep = g_pDeps; pDep; pDep = pDep->pNext)
     350    for (pDep = pThis->pDeps; pDep; pDep = pDep->pNext)
    326351        fprintf(pOutput, "%s:\n\n", pDep->szFilename);
    327352}
     
    355380 *
    356381 * @returns Pointer to the allocated dependency.
     382 * @param   pThis       The 'dep' instance.
    357383 * @param   pszFilename     The filename. Does not need to be terminated.
    358384 * @param   cchFilename     The length of the filename.
    359385 */
    360 PDEP depAdd(const char *pszFilename, size_t cchFilename)
     386PDEP depAdd(PDEPGLOBALS pThis, const char *pszFilename, size_t cchFilename)
    361387{
    362388    unsigned    uHash = sdbm(pszFilename, cchFilename);
     
    368394     */
    369395    pDepPrev = NULL;
    370     for (pDep = g_pDeps; pDep; pDepPrev = pDep, pDep = pDep->pNext)
     396    for (pDep = pThis->pDeps; pDep; pDepPrev = pDep, pDep = pDep->pNext)
    371397        if (    pDep->uHash == uHash
    372398            &&  pDep->cchFilename == cchFilename
     
    397423    else
    398424    {
    399         pDep->pNext = g_pDeps;
    400         g_pDeps = pDep;
     425        pDep->pNext = pThis->pDeps;
     426        pThis->pDeps = pDep;
    401427    }
    402428    return pDep;
    403 }
    404 
    405 
    406 /**
    407  * Frees the current dependency chain.
    408  */
    409 void depCleanup(void)
    410 {
    411     PDEP pDep = g_pDeps;
    412     g_pDeps = NULL;
    413     while (pDep)
    414     {
    415         PDEP pFree = pDep;
    416         pDep = pDep->pNext;
    417         free(pFree);
    418     }
    419429}
    420430
  • trunk/src/lib/kDep.h

    r2955 r3167  
    4646} DEP, *PDEP;
    4747
     48typedef struct DEPGLOBALS
     49{
     50    /** List of dependencies. */
     51    PDEP pDeps;
    4852
    49 extern PDEP depAdd(const char *pszFilename, size_t cchFilename);
    50 extern void depOptimize(int fFixCase, int fQuiet, const char *pszIgnoredExt);
    51 extern void depPrint(FILE *pOutput);
    52 extern void depPrintStubs(FILE *pOutput);
    53 extern void depCleanup(void);
     53} DEPGLOBALS;
     54typedef DEPGLOBALS *PDEPGLOBALS;
     55
     56extern void depInit(PDEPGLOBALS pThis);
     57extern void depCleanup(PDEPGLOBALS pThis);
     58extern PDEP depAdd(PDEPGLOBALS pThis, const char *pszFilename, size_t cchFilename);
     59extern void depOptimize(PDEPGLOBALS pThis, int fFixCase, int fQuiet, const char *pszIgnoredExt);
     60extern void depPrint(PDEPGLOBALS pThis, FILE *pOutput);
     61extern void depPrintStubs(PDEPGLOBALS pThis, FILE *pOutput);
     62
    5463extern void *depReadFileIntoMemory(FILE *pInput, size_t *pcbFile, void **ppvOpaque);
    5564extern void depFreeFileMemory(void *pvFile, void *pvOpaque);
Note: See TracChangeset for help on using the changeset viewer.