Ignore:
Timestamp:
Oct 3, 2000, 7:42:41 AM (25 years ago)
Author:
bird
Message:

Implemented .Def to WLINK directives/options converter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/common/kFileDef.cpp

    r2759 r4402  
    1818#include <string.h>
    1919#include <stdlib.h>
    20 
     20#include <assert.h>
     21
     22#include "kFile.h"
    2123#include "kFileFormatBase.h"
    2224#include "kFileDef.h"
     
    2628*   Internal Functions                                                         *
    2729*******************************************************************************/
    28 static char *dupeString(const char *psz);
     30static char *dupeString(const char *psz, BOOL fSkipFirstWord = FALSE);
    2931static char *trim(char *psz);
     32static char *ltrim(const char *psz);
    3033static char *removeFnutts(char *pszStr);
     34inline char  upcase(char ch);
     35static char *stristr(const char *pszStr, const char *pszSubStr);
     36
    3137
    3238/**
    3339 * Duplicates a string.
    34  * @returns   Pointer to stringcopy. Remeber to delete this!
    35  * @param     psz  Pointer to string to duplicate.
    36  */
    37 static char *dupeString(const char *psz)
     40 * @returns Pointer to stringcopy. Remeber to delete this!
     41 * @param   psz             Pointer to string to duplicate.
     42 * @param   fSkipFirstWord  Skips the first word before duplicating the string.
     43 */
     44static char *dupeString(const char *psz, BOOL fSkipFirstWord/* = FALSE*/)
    3845{
    3946    char *pszDupe;
    4047    if (psz == NULL)
    4148        return NULL;
     49
     50    if (fSkipFirstWord)
     51    {
     52        while (*psz != ' ' && *psz != '\t' && *psz != '\n' && *psz != '\r' && *psz != '\0')
     53            psz++;
     54        psz = ltrim(psz);
     55    }
     56
    4257    pszDupe = new char[strlen(psz)+1];
    43     return strcpy(pszDupe, psz);
     58    strcpy(pszDupe, psz);
     59    if (fSkipFirstWord)
     60        return removeFnutts(pszDupe);
     61    return pszDupe;
    4462}
    4563
     
    5674    if (psz == NULL)
    5775        return NULL;
    58     while (*psz == ' ')
     76    while (*psz == ' ' || *psz == '\t')
    5977        psz++;
    6078    i = strlen(psz) - 1;
    61     while (i >= 0 && psz[i] == ' ')
     79    while (i >= 0 && (psz[i] == ' ' || psz[i] == '\t'))
    6280        i--;
    6381    psz[i+1] = '\0';
     
    6684
    6785
    68 kFileDef::kFileDef(FILE *phFile) throw(int)
    69     :pszType(NULL), pszBase(NULL), pszCode(NULL), pszData(NULL), pszDescription(NULL),
     86/**
     87 * Trims a string, that is removing blank spaces at start and end.
     88 * @returns   Pointer to first non-blank char.
     89 * @param     psz  Pointer to string.
     90 * @result    Blank at end of string is removed. ('\0' is moved to the left.)
     91 */
     92static char *ltrim(const char *psz)
     93{
     94    if (psz == NULL)
     95        return NULL;
     96
     97    while (*psz == ' ' || *psz == '\t')
     98        psz++;
     99    return (char *)psz;
     100}
     101
     102
     103
     104kFileDef::kFileDef(kFile *pFile) throw(int)
     105    :pszType(NULL), pszModName(NULL), pszBase(NULL), pszCode(NULL), pszData(NULL), pszDescription(NULL),
    70106    pszExeType(NULL), pszHeapSize(NULL), pszOld(NULL), pszProtmode(NULL), pszStackSize(NULL),
    71     pszStub(NULL), pSegments(NULL), pImports(NULL), pExports(NULL)
     107    pszStub(NULL), pSegments(NULL), pImports(NULL), pExports(NULL),
     108    fProgram(FALSE), fLibrary(FALSE), fPhysicalDevice(FALSE), fVirtualDevice(FALSE),
     109    fInitInstance(FALSE), fTermInstance(FALSE), fInitGlobal(FALSE), fTermGlobal(FALSE),
     110    chAppType(kFileDef::unknown)
    72111{
    73112    /* determin file size */
    74     if (!fseek(phFile, 0, SEEK_SET))
    75     {
    76         this->read(phFile);
     113
     114    if (pFile->start())
     115    {
     116        this->read(pFile);
    77117    }
    78118    else
     
    126166/**
    127167 * Read/parse the Definition file.
    128  * @param     phFile  Handle to file.
    129  * @remark    throws errorcode on error (TODO: errorhandling)
    130  */
    131 void kFileDef::read(FILE *phFile) throw (int)
     168 * @param   pFile   Pointer to fileobject.
     169 * @remark  throws errorcode on error (TODO: errorhandling)
     170 */
     171void kFileDef::read(kFile *pFile) throw (int)
    132172{
    133173    char *pszTmp;
     
    136176
    137177    /* readloop */
    138     psz = readln(phFile, &szBuffer[0], sizeof(szBuffer));
     178    psz = readln(pFile, &szBuffer[0], sizeof(szBuffer));
    139179    while (psz != NULL)
    140180    {
     
    146186            if (pszType != NULL) throw (0x101);
    147187            pszType = dupeString(psz);
     188            fLibrary = TRUE;
     189            if (!setModuleName())
     190                throw (0x107);
     191            fInitInstance = stristr(pszType, "INITINSTANCE") != NULL;
     192            fInitGlobal   = stristr(pszType, "INITGLOBAL")   != NULL || !fInitInstance;
     193            fTermInstance = stristr(pszType, "TERMINSTANCE") != NULL;
     194            fTermGlobal   = stristr(pszType, "TERMGLOBAL")   != NULL || !fTermInstance;
    148195        }
    149196        else if (StringCase(psz, "NAME"))
     
    151198            if (pszType != NULL) throw (0x101);
    152199            pszType = dupeString(psz);
     200            fProgram = TRUE;
     201            setModuleName();
     202            if (stristr(pszType, "WINDOWAPI"))
     203                chAppType = kFileDef::pm;
     204            else if (stristr(pszType, "NOTWINDOWCOMPAT"))
     205                chAppType = kFileDef::fullscreen;
     206            else if (stristr(pszType, "WINDOWCOMPAT"))
     207                chAppType = kFileDef::pmvio;
     208            else
     209                chAppType = kFileDef::unknown;
    153210        }
    154211        else if (StringCase(psz, "PHYSICAL DEVICE")) //gap is fixed to one space, this may be fixed in readln.
     
    156213            if (pszType != NULL) throw (0x101);
    157214            pszType = dupeString(psz);
     215            fPhysicalDevice = TRUE;
     216            setModuleName();
    158217        }
    159218        else if (StringCase(psz, "VIRTUAL DEVICE")) //gap is fixed to one space, this may be fixed in readln.
     
    161220            if (pszType != NULL) throw (0x101);
    162221            pszType = dupeString(psz);
     222            fVirtualDevice = TRUE;
     223            setModuleName();
    163224        }
    164225        else if (StringCase(psz, "BASE"))
    165             pszBase = dupeString(psz);
     226            pszBase = dupeString(psz, TRUE);
    166227        else if (StringCase(psz, "CODE"))
    167             pszCode = dupeString(psz);
     228            pszCode = dupeString(psz, TRUE);
    168229        else if (StringCase(psz, "DATA"))
    169             pszData = dupeString(psz);
     230            pszData = dupeString(psz, TRUE);
    170231        else if (StringCase(psz, "DESCRIPTION"))
    171             pszDescription = dupeString(psz);
     232            pszDescription = dupeString(psz, TRUE);
    172233        else if (StringCase(psz, "EXETYPE"))
    173             pszExeType = dupeString(psz);
     234            pszExeType = dupeString(psz, TRUE);
    174235        else if (StringCase(psz, "HEAPSIZE"))
    175             pszHeapSize = dupeString(psz);
     236            pszHeapSize = dupeString(psz, TRUE);
    176237        else if (StringCase(psz, "OLD"))
    177             pszOld = dupeString(psz);
     238            pszOld = dupeString(psz, TRUE);
    178239        else if (StringCase(psz, "PROTMODE"))
    179             pszProtmode = dupeString(psz);
     240            pszProtmode = dupeString(psz, TRUE);
    180241        else if (StringCase(psz, "STACKSIZE"))
    181             pszStackSize = dupeString(psz);
     242            pszStackSize = dupeString(psz, TRUE);
    182243        else if (StringCase(psz, "STUB"))
    183             pszStub = dupeString(psz);
     244            pszStub = dupeString(psz, TRUE);
    184245        else if (StringCase(psz, "SEGMENTS"))
    185246        {
    186247            PDEFSEGMENT *pps = &pSegments;
    187             while (!isKeyword(psz = readln(phFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL)
     248            while (!isKeyword(psz = readln(pFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL)
    188249            {
    189250                *pps = new DEFSEGMENT; memset(*pps, 0, sizeof(**pps));
     
    196257        {
    197258            PDEFIMPORT *ppi = &pImports;
    198             while (!isKeyword(psz = readln(phFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL)
     259            while (!isKeyword(psz = readln(pFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL)
    199260            {
    200261                //DOSCALL1.154 or DosQueryHeaderInfo = DOSCALL1.154
     
    229290        {
    230291            PDEFEXPORT *ppe = &pExports;
    231             while (!isKeyword(psz = readln(phFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL)
     292            while (!isKeyword(psz = readln(pFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL)
    232293            {
    233294                /* CloseHandle = CloseHandle@4 @1234 RESIDENTNAME 2 */
     
    312373        /* next ? */
    313374        if (fNext)
    314             psz = readln(phFile, &szBuffer[0], sizeof(szBuffer));
     375            psz = readln(pFile, &szBuffer[0], sizeof(szBuffer));
    315376    }
    316377
    317378    /* sanity check */
    318379    if (pszType == NULL)
    319         throw (0x106);
     380        throw ((int)0x106);
    320381}
    321382
     
    324385 * Reads first meaning full line from a file into a buffer.
    325386 * @returns   Pointer to buffer on success;  NULL on error.
    326  * @param     phFile     Filea handle.
     387 * @param     pFile      Pointer to fileobject to read line from.
    327388 * @param     pszBuffer  Pointer to buffer.
    328389 * @param     cbBuffer   Size of buffer.
    329390 * @remark    tabs are expanded. string is trimmed. comments removed.
    330391 */
    331 char *kFileDef::readln(FILE *phFile, char *pszBuffer, int cbBuffer) throw (int)
     392char *kFileDef::readln(kFile *pFile, char *pszBuffer, int cbBuffer) throw (int)
    332393{
    333394    int i;
     
    337398    {
    338399        /* read line */
    339         if (!fgets(pszBuffer, cbBuffer, phFile))
    340         {
    341             if (feof(phFile))
    342                 return FALSE;
    343             else
     400        if (!pFile->readln(pszBuffer, cbBuffer))
     401        {
     402            if (!pFile->isEOF())
    344403                throw (0x201);
     404            return FALSE;
    345405        }
    346406
     
    378438                memmove(pszBuffer, &pszBuffer[i], cch + 1);
    379439        }
    380     } while ((*pszBuffer == ';' || cch == 0) && !feof(phFile));
     440    } while ((*pszBuffer == ';' || cch == 0) && !pFile->isEOF());
    381441
    382442    return !(*pszBuffer == ';' || cch == 0) ? pszBuffer : NULL;
     
    416476
    417477/**
    418  * Queries the module name.
     478 * Extracts the module name from the pszType string.
    419479 * @returns   Success indicator.
    420480 * @param     pszBuffer  Pointer to string buffer which is to hold the module name upon return.
    421481 * @remark    Assumes that pszBuffer is large enough.
    422482 */
     483BOOL  kFileDef::setModuleName(void)
     484{
     485    char *pszEnd;
     486    char *pszStart;
     487
     488    assert(pszType);
     489
     490    /* skip the first word */
     491    pszStart = strpbrk(pszType, " \t");
     492    if (pszStart != NULL)
     493    {
     494        pszStart = ltrim(pszStart);
     495        pszEnd = strpbrk(pszStart, " \t");
     496        if (pszEnd == NULL)
     497            pszEnd = pszStart + strlen(pszEnd);
     498        pszModName = new char[pszEnd - pszStart + 1];
     499        memcpy(pszModName, pszStart, pszEnd - pszStart);
     500        pszModName[pszEnd - pszStart] = '\0';
     501    }
     502    else
     503        return !StringCase(pszType, "LIBRARY");
     504    return TRUE;
     505}
     506
     507
     508/**
     509 * Query for the module name.
     510 * @returns   Success indicator. TRUE / FALSE.
     511 * @param     pszBuffer  Pointer to buffer which to put the name into.
     512 */
    423513BOOL  kFileDef::queryModuleName(char *pszBuffer)
    424514{
    425     char *psz;
    426 
    427     if (pszType != NULL && StringCase(pszType, "LIBRARY"))
    428     {
    429         psz = pszType + sizeof("LIBRARY") - 1;
    430         while (*psz == ' ')
    431             psz++;
    432         while (*psz != '\0' && *psz != ' ')
    433             *pszBuffer++ = *psz++;
    434         *pszBuffer = '\0';
    435     }
    436     else
     515    if (pszModName == NULL)
    437516        return FALSE;
     517
     518    strcpy(pszBuffer, pszModName);
     519
    438520    return TRUE;
    439521}
     
    497579
    498580/**
     581 * Make a Watcom Linker parameter file addtition of this definition file.
     582 * @returns Success indicator.
     583 * @param   pFile   File which we're to write to (append).
     584 *                  Appends at current posistion.
     585 * @sketch
     586 * @status
     587 * @author  knut st. osmundsen (knut.stange.osmundsen@mynd.no)
     588 * @remark
     589 */
     590BOOL  kFileDef::makeWatcomLinkFileAddtion(kFile *pFile) throw(int)
     591{
     592    PDEFSEGMENT pSeg;
     593    PDEFIMPORT  pImp;
     594    PDEFEXPORT  pExp;
     595    pFile->setThrowOnErrors();
     596
     597    /*
     598     * Write a little remark first to tell that converted stuff starts here.
     599     */
     600    pFile->printf("#\n# Directives generated from .DEF-file.\n#\n");
     601
     602    /* Format - Module type */
     603    pFile->printf("FORMAT OS2 LX %s %s %s\n",
     604                  fLibrary                               ? "DLL" :
     605                    (fProgram ? (chAppType == pm         ? "PM"
     606                              : (chAppType == fullscreen ? "FULLSCREEN"
     607                                                         : "PMCOMPATIBLE"))
     608                    : (fVirtualDevice                    ? "VIRTDEVICE"
     609                                                         : "PHYSDEVICE" )),
     610                  fLibrary ? (fInitGlobal ? "INITGLOBAL" : "INITINSTANCE") : "",
     611                  fLibrary ? (fTermGlobal ? "TERMGLOBAL" : "TERMINSTANCE") : "");
     612
     613
     614    /* Module name */
     615    if (pszModName)
     616        pFile->printf("OPTION MODNAME=%s\n", pszModName);
     617
     618    /* Description */
     619    if (pszDescription)
     620        pFile->printf("OPTION DESCRIPTION '%s'\n", pszDescription);
     621
     622    /* Base */
     623    if (pszBase)
     624        pFile->printf("OPTION OFFSET=%s\n", pszBase);
     625
     626    /* Stub */
     627    if (pszStub)
     628        pFile->printf("OPTION STUB='%s'\n", pszStub);
     629
     630    /* Old */
     631    if (pszOld)
     632        pFile->printf("OPTION OLDLIBRARY=%s\n", pszOld);
     633
     634    /* Protected mode */
     635    if (pszProtmode)
     636        pFile->printf("OPTION PROTMODE\n", pszProtmode);
     637
     638    /* Stacksize */
     639    if (pszStackSize)
     640        pFile->printf("OPTION STACK=%s\n", pszStackSize);
     641
     642    /* HeapSize */
     643    if (pszHeapSize)
     644        pFile->printf("OPTION HEAPSIZE=%s\n", pszHeapSize);
     645
     646    /* Code  -  not supported */
     647
     648    /* Data  -  not supported */
     649
     650    /*
     651     * Segments.
     652     */
     653    pSeg = pSegments;
     654    while (pSeg != NULL)
     655    {
     656        pFile->printf("SEGMENT %s\n", pSeg->psz);
     657        pSeg = pSeg->pNext;
     658    }
     659
     660    /*
     661     * Imports.
     662     */
     663    pImp = pImports;
     664    while (pImp != NULL)
     665    {
     666        if (pImp->pszName == NULL)
     667            pFile->printf("IMPORT '%s' '%s'.%d\n", pImp->pszIntName, pImp->pszDll, pImp->ulOrdinal);
     668        else
     669            pFile->printf("IMPORT '%s' '%s'.'%s'\n", pImp->pszIntName, pImp->pszDll, pImp->pszName);
     670        pImp = pImp->pNext;
     671    }
     672
     673    /*
     674     * Exports.
     675     */
     676    pExp = pExports;
     677    while (pExp != NULL)
     678    {
     679        pFile->printf("EXPORT '%s'", pExp->pszName);
     680        if (pExp->ulOrdinal != ~0UL)
     681            pFile->printf(".%d", pExp->ulOrdinal);
     682        if (pExp->pszIntName)
     683            pFile->printf("='%s'", pExp->pszIntName);
     684        if (pExp->fResident)
     685            pFile->printf(" RESIDENT");
     686        if (pExp->cParam != ~0UL)
     687            pFile->printf(" %d", pExp->cParam * 2); /* .DEFs this is number of words. Watcom should have bytes. */
     688        pFile->printf("\n");
     689        pExp = pExp->pNext;
     690    }
     691
     692    return TRUE;
     693}
     694
     695
     696
     697
     698/**
    499699 * Removes '"' and ''' at start and end of the string.
    500700 * @returns     pszStr!
     
    514714    return pszStr;
    515715}
     716
     717
     718/**
     719 * Upcases a char.
     720 * @returns   Upper case of the char given in ch.
     721 * @param     ch  Char to capitalize.
     722 */
     723inline char upcase(char ch)
     724{
     725    return ch >= 'a' && ch <= 'z' ? (char)(ch - ('a' - 'A')) : ch;
     726}
     727
     728
     729/**
     730 * Searches for a substring in a string.
     731 * @returns   Pointer to start of substring when found, NULL when not found.
     732 * @param     pszStr     String to be searched.
     733 * @param     pszSubStr  String to be searched.
     734 * @remark    Depends on the upcase function.
     735 */
     736static char *stristr(const char *pszStr, const char *pszSubStr)
     737{
     738    int cchSubStr = strlen(pszSubStr);
     739    int i = 0;
     740
     741    while (*pszStr != '\0' && i < cchSubStr)
     742    {
     743        i = 0;
     744        while (i < cchSubStr && pszStr[i] != '\0' &&
     745               (upcase(pszStr[i]) == upcase(pszSubStr[i])))
     746            i++;
     747        pszStr++;
     748    }
     749
     750    return (char*)(*pszStr != '\0' ? pszStr - 1 : NULL);
     751}
     752
Note: See TracChangeset for help on using the changeset viewer.