Changeset 4236 for trunk/src


Ignore:
Timestamp:
Sep 12, 2000, 6:29:59 AM (25 years ago)
Author:
bird
Message:

Command line string rewrite.

Location:
trunk/src/kernel32
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/winexebase.cpp

    r3483 r4236  
    1 /* $Id: winexebase.cpp,v 1.9 2000-05-02 20:53:14 sandervl Exp $ */
     1/* $Id: winexebase.cpp,v 1.10 2000-09-12 04:29:58 bird Exp $ */
    22
    33/*
     
    4848//******************************************************************************
    4949//******************************************************************************
    50 Win32ExeBase::Win32ExeBase(HINSTANCE hInstance) 
     50Win32ExeBase::Win32ExeBase(HINSTANCE hInstance)
    5151                 : Win32ImageBase(hInstance),
    5252                   fConsoleApp(FALSE),
     
    6262 Win32DllBase *dll;
    6363
    64   //First delete all dlls loaded by LoadLibrary 
     64  //First delete all dlls loaded by LoadLibrary
    6565  //Then delete all dlls that were loaded by the exe
    6666  //(NOTE: This is what NT does; first delete loadlib dlls in LIFO order and
     
    138138//******************************************************************************
    139139//******************************************************************************
    140 void Win32ExeBase::setCommandLine(char *cline)
    141 {
    142  ULONG cmdlength = strlen(cline) + 1;
    143 
    144   cmdLineA = (LPSTR)malloc(cmdlength);
    145   strcpy(cmdLineA, cline);
    146   cmdLineW = (LPWSTR)malloc(cmdlength*sizeof(WCHAR));
    147   AsciiToUnicode(cmdLineA, cmdLineW);
    148 }
    149 //******************************************************************************
    150 //******************************************************************************
  • trunk/src/kernel32/winexebase.h

    r3059 r4236  
    1 /* $Id: winexebase.h,v 1.1 2000-03-09 19:03:22 sandervl Exp $ */
     1/* $Id: winexebase.h,v 1.2 2000-09-12 04:29:58 bird Exp $ */
    22
    33/*
     
    2424virtual ~Win32ExeBase();
    2525
    26         void   setCommandLine(char *cline);
    27         LPSTR  getCommandLineA()                { return cmdLineA;  };
    28         LPWSTR getCommandLineW()                { return cmdLineW;  };
    29 
    3026        BOOL  isConsoleApp()                   { return fConsoleApp; };
    3127
  • trunk/src/kernel32/winexelx.cpp

    r3375 r4236  
    1 /* $Id: winexelx.cpp,v 1.6 2000-04-14 22:35:27 sandervl Exp $ */
     1/* $Id: winexelx.cpp,v 1.7 2000-09-12 04:29:58 bird Exp $ */
    22
    33/*
     
    6666
    6767  if(winexe) {
    68         char *cmdline;
    69         int   cmdlen = strlen(ppib->pib_pchcmd);
    70 
    71         cmdlen += strlen(ppib->pib_pchcmd+cmdlen+1);
    72         cmdline = (char *)malloc(cmdlen+2); //term. 0 + space
    73         strcpy(cmdline, ppib->pib_pchcmd);
    74         strcat(cmdline, " ");
    75         strcat(cmdline, ppib->pib_pchcmd+strlen(ppib->pib_pchcmd)+1);
    76         winexe->setCommandLine(cmdline);
    77         free(cmdline);
     68    InitCommandLine(FALSE);
    7869        winexe->setEntryPoint((ULONG)EntryPoint);
    7970        winexe->start();
     
    8879//******************************************************************************
    8980//******************************************************************************
    90 Win32LxExe::Win32LxExe(HINSTANCE hInstance, PVOID pResData) 
     81Win32LxExe::Win32LxExe(HINSTANCE hInstance, PVOID pResData)
    9182                 : Win32ImageBase(hInstance),
    9283                   Win32LxImage(hInstance, pResData),
  • trunk/src/kernel32/winexepeldr.cpp

    r3501 r4236  
    1 /* $Id: winexepeldr.cpp,v 1.9 2000-05-09 18:56:09 sandervl Exp $ */
     1/* $Id: winexepeldr.cpp,v 1.10 2000-09-12 04:29:59 bird Exp $ */
    22
    33/*
     
    4747//Called by ring 3 pe loader to create win32 executable
    4848//******************************************************************************
    49 BOOL WIN32API CreateWin32PeLdrExe(char *szFileName, char *szCmdLine, ULONG reservedMem) 
     49BOOL WIN32API CreateWin32PeLdrExe(char *szFileName, char *szCmdLine, ULONG reservedMem)
    5050{
    5151 APIRET  rc;
     
    6767  }
    6868
    69   //exe length + space + (possibly) 2x'"' + cmd line length + 0 terminator
    70   szFullCmdLine = (char *)malloc(strlen(szFileName) + 3 + strlen(szCmdLine) + 1);
    71   //Enclose executable name in quotes if it (or it's directory) contains spaces
    72   if(strchr(szFileName, ' ') != NULL) {
    73         sprintf(szFullCmdLine, "\"%s\"", szFileName);
    74   }
    75   else  strcpy(szFullCmdLine, szFileName);
    76   strcat(szFullCmdLine, " ");
    77   strcat(szFullCmdLine, szCmdLine);
    78   WinExe->setCommandLine(szFullCmdLine);
    79   dprintf(("Cmd line: %s", szFullCmdLine));
    80   free(szFullCmdLine);
     69  /* Initiate the commandline.
     70   * (Since PE.EXE takes no options we'll currently just skip the
     71   * first parameter and used the passed in executable name.)
     72   */
     73  szCmdLine = szCmdLine; /* ignored */
     74  InitCommandLine(szFileName);
    8175
    8276  if(getenv("WIN32_IOPL2")) {
     
    9589        return FALSE;
    9690  }
     91
    9792  OS2UnsetExceptionHandler(&exceptFrame);
    9893  if(WinExe->isConsoleApp()) {
     
    112107//******************************************************************************
    113108//******************************************************************************
    114 Win32PeLdrExe::Win32PeLdrExe(char *szFileName) : 
    115                    Win32ImageBase(-1), 
     109Win32PeLdrExe::Win32PeLdrExe(char *szFileName) :
     110                   Win32ImageBase(-1),
    116111                   Win32ExeBase(-1),
    117112                   Win32PeLdrImage(szFileName, TRUE)
  • trunk/src/kernel32/wprocess.cpp

    r4229 r4236  
    1 /* $Id: wprocess.cpp,v 1.90 2000-09-09 08:59:55 sandervl Exp $ */
     1/* $Id: wprocess.cpp,v 1.91 2000-09-12 04:29:59 bird Exp $ */
    22
    33/*
     
    3535#include "odin32validate.h"
    3636#include "exceptutil.h"
     37#include "oslibdos.h"
    3738#include "oslibmisc.h"
    3839#include "oslibdebug.h"
     
    5051
    5152
    52 //******************************************************************************
    53 //******************************************************************************
    54 BOOL      fIsOS2Image = FALSE;  //TRUE  -> Odin32 OS/2 application (not converted!)
    55                                 //FALSE -> otherwise
    56 BOOL      fExitProcess = FALSE;
     53/*******************************************************************************
     54*   Global Variables                                                           *
     55*******************************************************************************/
     56BOOL    fIsOS2Image = FALSE;            /* TRUE  -> Odin32 OS/2 application (not converted!) */
     57                                        /* FALSE -> otherwise */
     58BOOL    fExitProcess = FALSE;
     59
     60//Commandlines
     61PCSTR   pszCmdLineA;                    /* ASCII/ANSII commandline. */
     62PCWSTR  pszCmdLineW;                    /* Unicode commandline. */
    5763
    5864//Process database
    59 PDB       ProcessPDB = {0};
    60 USHORT    ProcessTIBSel = 0;
    61 DWORD    *TIBFlatPtr    = 0;
     65PDB     ProcessPDB = {0};
     66USHORT  ProcessTIBSel = 0;
     67DWORD  *TIBFlatPtr    = 0;
    6268
    6369//list of thread database structures
     
    7884                                                 PSID *pSid);
    7985static HINSTANCE hInstNTDll = 0;
     86
    8087//******************************************************************************
    8188//******************************************************************************
     
    233240    RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &thdb->threadinfo.SidUser.User.Sid);
    234241    thdb->threadinfo.SidUser.User.Attributes = 0; //?????????
    235  
     242
    236243    thdb->threadinfo.pTokenGroups = (TOKEN_GROUPS*)malloc(sizeof(TOKEN_GROUPS));
    237244    thdb->threadinfo.pTokenGroups->GroupCount = 1;
     
    786793            if(pModule)
    787794            {
    788                
     795
    789796                if(pModule->isLxDll())
    790797                {
     
    10471054    return 0;
    10481055}
    1049 //******************************************************************************
    1050 //******************************************************************************
    1051 LPCSTR WIN32API GetCommandLineA()
    1052 {
    1053  LPTSTR cmdline = NULL;
    1054 
    1055   if(WinExe) {
    1056     cmdline = WinExe->getCommandLineA();
    1057   }
    1058   if(cmdline == NULL) //not used for converted exes
    1059     cmdline = O32_GetCommandLine();
    1060 
    1061   dprintf(("KERNEL32:  GetCommandLine %s\n", cmdline));
    1062   dprintf(("KERNEL32:  FS = %x\n", GetFS()));
    1063   return(cmdline);
    1064 }
    1065 //******************************************************************************
    1066 //******************************************************************************
     1056
     1057
     1058/**
     1059 * Internal function which gets the commandline.
     1060 * @returns     OS/2 / Windows return code
     1061 *              On successful return (NO_ERROR) the global variables
     1062 *              pszCmdLineA and pszCmdLineW are set.
     1063 *
     1064 * @param       pszPeExe    Pass in the name of the PE exe of this process. We'll
     1065 *                          us this as exename and skip the first argument (ie. argv[1]).
     1066 *                          If NULL we'll use the commandline from OS/2 as it is.
     1067 * @status      Completely implemented and tested.
     1068 * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no)
     1069 */
     1070ULONG InitCommandLine(const char *pszPeExe)
     1071{
     1072    PCHAR   pib_pchcmd;                 /* PIB pointer to commandline. */
     1073    CHAR    szFilename[CCHMAXPATH];     /* Filename buffer used to get the exe filename in. */
     1074    ULONG   cch;                        /* Commandline string length. (including terminator) */
     1075    PSZ     psz;                        /* Temporary string pointer. */
     1076    PSZ     psz2;                       /* Temporary string pointer. */
     1077    APIRET  rc;                         /* OS/2 return code. */
     1078    BOOL    fQuotes;                    /* Flag used to remember if the exe filename should be in quotes. */
     1079
     1080    /** @sketch
     1081     * Get commandline from the PIB.
     1082     */
     1083    pib_pchcmd = (PCHAR)OSLibGetPIB(PIB_PCHCMD);
     1084
     1085    /** @sketch
     1086     * Two methods of making the commandline:
     1087     *  (1) The first argument is skipped and the second is used as exe filname.
     1088     *      This applies to PE.EXE launched processes only.
     1089     *  (2) No skipping. First argument is the exe filename.
     1090     *      This applies to all but PE.EXE launched processes.
     1091     *
     1092     *  Note: We could do some code size optimization here. Much of the code for
     1093     *        the two methods are nearly identical.
     1094     *
     1095     */
     1096    if (pszPeExe)
     1097    {
     1098        PSZ     pszArg2;                /* Pointer to into pib_pchcmd to the second argument. */
     1099
     1100        /** @sketch Method (1):
     1101         * First we'll have to determin the size of the commandline.
     1102         *
     1103         * If the commandline string don't have any arguments we'll fail.
     1104         * Skip argument pointer to the second argument (ie. argv[2]).
     1105         */
     1106        if (pib_pchcmd == NULL                                  /* CPREF states that if may be NULL. */
     1107            || pib_pchcmd[cch = strlen(pib_pchcmd) + 1] == '\0' /* No arguments */
     1108            )
     1109        {
     1110            dprintf(("KERNEL32: InitCommandLine(%p): No arguments!\n", pszPeExe));
     1111            return ERROR_BAD_ARGUMENTS;
     1112        }
     1113
     1114        psz2 = pib_pchcmd + cch;        /* skip exe name (PE.EXE) */
     1115        while (*psz2 == ' ')
     1116            psz2++;
     1117        fQuotes = *psz2 == '"';
     1118        if (fQuotes)
     1119        {
     1120            psz2++;
     1121            psz = strchr(psz2, '"');
     1122        }
     1123        else
     1124            psz = strchr(psz2, ' ');
     1125        if (psz != NULL)
     1126        {
     1127            pszArg2 = psz2 += psz - psz2 + 1;   /* + 1: space or quote. */
     1128            while (*psz2 == ' ')            /* skip blanks after argv[1]. */
     1129                psz2++;
     1130        }
     1131        else
     1132            pszArg2 = psz2 += strlen(psz2) + 1; /* no arguments, skip to next string. */
     1133
     1134        /** @sketch
     1135         * Use passed in executable name.
     1136         * Check if the exe filename needs to be put in quotes.
     1137         * Determin the length of the executable name including quotes and '\0'-terminator.
     1138         * Count the length of the arguments. (We here count's all argument strings.)
     1139         */
     1140        #ifdef DEBUG
     1141        if (pszPeExe[0] == '\0')
     1142        {
     1143            dprintf(("KERNEL32: InitCommandLine(%p): pszPeExe was empty string\n", pszPeExe));
     1144            return ERROR_BAD_ARGUMENTS;
     1145        }
     1146        #endif
     1147
     1148        fQuotes = strchr(pszPeExe, ' ') != NULL;
     1149        cch = strlen(pszPeExe) + fQuotes*2 + 1;
     1150
     1151        while (*psz2 != '\0')
     1152        {
     1153            register int cchTmp = strlen(psz2) + 1; /* + 1 is for terminator (psz2) and space (cch). */
     1154            psz2 += cchTmp;
     1155            cch += cchTmp;
     1156        }
     1157
     1158        /** @sketch
     1159         * Allocate memory for the commandline.
     1160         * Build commandline:
     1161         *  Copy exe filename.
     1162         *  Add arguments.
     1163         */
     1164        pszCmdLineA = psz = (PSZ)malloc(cch);
     1165        if (psz == NULL)
     1166        {
     1167            dprintf(("KERNEL32: InitCommandLine(%p): malloc(%d) failed\n", pszPeExe, cch));
     1168            return ERROR_NOT_ENOUGH_MEMORY;
     1169        }
     1170
     1171        if (fQuotes)
     1172            *psz++ = '"';
     1173        strcpy(psz, pszPeExe);
     1174        psz += strlen(psz);
     1175        if (fQuotes)
     1176        {
     1177            *psz++ = '"';
     1178            *psz = '\0';
     1179        }
     1180
     1181        psz2 = pszArg2;
     1182        while (*psz2 != '\0')
     1183        {
     1184            register int cchTmp = strlen(psz2) + 1; /* + 1 is for terminator (psz). */
     1185            *psz++ = ' ';           /* add space */
     1186            memcpy(psz, psz2, cchTmp);
     1187            psz2 += cchTmp;
     1188            psz += cchTmp - 1;
     1189        }
     1190        rc = NO_ERROR;
     1191    }
     1192    else
     1193    {
     1194        /** @sketch Method (2):
     1195         * First we'll have to determin the size of the commandline.
     1196         *
     1197         * As we don't assume that OS/2 allways puts a fully qualified EXE name
     1198         * as the first string, we'll check if it's empty - and get the modulename
     1199         * in that case - and allways get the fully qualified filename.
     1200         */
     1201        if (pib_pchcmd == NULL || pib_pchcmd[0] == '\0')
     1202        {
     1203            rc = OSLibDosQueryModuleName(OSLibGetPIB(PIB_HMTE), sizeof(szFilename), szFilename);
     1204            if (rc != NO_ERROR)
     1205            {
     1206                dprintf(("KERNEL32: InitCommandLine(%p): OSLibQueryModuleName(0x%x,...) failed with rc=%d\n",
     1207                         pszPeExe, OSLibGetPIB(PIB_HMTE), rc));
     1208                return rc;
     1209            }
     1210        }
     1211        else
     1212        {
     1213            rc = OSLibDosQueryPathInfo(pib_pchcmd, FIL_QUERYFULLNAME, szFilename, sizeof(szFilename));
     1214            if (rc != NO_ERROR)
     1215            {
     1216                dprintf(("KERNEL32: InitCommandLine(%p): (info) OSLibDosQueryPathInfo failed with rc=%d\n", pszPeExe, rc));
     1217                strcpy(szFilename, pib_pchcmd);
     1218                rc = NO_ERROR;
     1219            }
     1220        }
     1221
     1222        /** @sketch
     1223         * We're still measuring the size of the commandline:
     1224         *  Check if we have to quote the exe filename.
     1225         *  Determin the length of the executable name including quotes and '\0'-terminator.
     1226         *  Count the length of the arguments. (We here count's all argument strings.)
     1227         */
     1228        fQuotes = strchr(szFilename, ' ') != NULL;
     1229        cch = strlen(szFilename) + fQuotes*2 + 1;
     1230        if (pib_pchcmd != NULL)
     1231        {
     1232            psz2 = pib_pchcmd + strlen(pib_pchcmd) + 1;
     1233            while (*psz2 != '\0')
     1234            {
     1235                register int cchTmp = strlen(psz2) + 1; /* + 1 is for terminator (psz2) and space (cch). */
     1236                psz2 += cchTmp;
     1237                cch += cchTmp;
     1238            }
     1239        }
     1240
     1241        /** @sketch
     1242         * Allocate memory for the commandline.
     1243         * Build commandline:
     1244         *  Copy exe filename.
     1245         *  Add arguments.
     1246         */
     1247        pszCmdLineA = psz = (PSZ)malloc(cch);
     1248        if (psz == NULL)
     1249        {
     1250            dprintf(("KERNEL32: InitCommandLine(%p): malloc(%d) failed\n", pszPeExe, cch));
     1251            return ERROR_NOT_ENOUGH_MEMORY;
     1252        }
     1253
     1254        if (fQuotes)
     1255            *psz++ = '"';
     1256        strcpy(psz, szFilename);
     1257        psz += strlen(psz);
     1258        if (fQuotes)
     1259        {
     1260            *psz++ = '"';
     1261            *psz = '\0';
     1262        }
     1263
     1264        if (pib_pchcmd != NULL)
     1265        {
     1266            psz2 = pib_pchcmd + strlen(pib_pchcmd) + 1;
     1267            while (*psz2 != '\0')
     1268            {
     1269                register int cchTmp = strlen(psz2) + 1; /* + 1 is for terminator (psz). */
     1270                *psz++ = ' ';           /* add space */
     1271                memcpy(psz, psz2, cchTmp);
     1272                psz2 += cchTmp;
     1273                psz += cchTmp - 1;
     1274            }
     1275        }
     1276    }
     1277
     1278    /** @sketch
     1279     * If successfully build ASCII commandline then convert it to UniCode.
     1280     */
     1281    if (rc == NO_ERROR)
     1282    {
     1283        pszCmdLineW = (WCHAR*)malloc(cch * 2);
     1284        if (pszCmdLineW != NULL)
     1285            AsciiToUnicode(pszCmdLineA, (WCHAR*)pszCmdLineW);
     1286        else
     1287        {
     1288            dprintf(("KERNEL32: InitCommandLine(%p): malloc(%d) failed (2)\n", pszPeExe, cch));
     1289            rc = ERROR_NOT_ENOUGH_MEMORY;
     1290        }
     1291    }
     1292
     1293    return rc;
     1294}
     1295
     1296/**
     1297 * Gets the command line of the current process.
     1298 * @returns     On success:
     1299 *                  Command line of the current process. One single string.
     1300 *                  The first part of the command line string is the executable filename
     1301 *                  of the current process. It might be in quotes if it contains spaces.
     1302 *                  The rest of the string is arguments.
     1303 *
     1304 *              On error:
     1305 *                  NULL. Last error set. (does Win32 set last error this?)
     1306 * @sketch      IF not inited THEN
     1307 *                  Init commandline assuming !PE.EXE
     1308 *                  IF init failes THEN set last error.
     1309 *              ENDIF
     1310 *              return ASCII/ANSI commandline.
     1311 * @status      Completely implemented and tested.
     1312 * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no)
     1313 * @remark      The Ring-3 PeLdr is resposible for calling InitCommandLine before anyone
     1314 *              is able to call this function.
     1315 */
     1316LPCSTR WIN32API GetCommandLineA(VOID)
     1317{
     1318    /*
     1319     * Check if the commandline is initiated.
     1320     * If not we'll have to do it.
     1321     * ASSUMES that if not inited this isn't a PE.EXE lauched process.
     1322     */
     1323    if (pszCmdLineA == NULL)
     1324    {
     1325        APIRET rc;
     1326        rc = InitCommandLine(NULL);
     1327        if (rc != NULL)
     1328            SetLastError(rc);
     1329    }
     1330
     1331    dprintf(("KERNEL32:  GetCommandLineA: %s\n", pszCmdLineA));
     1332    return pszCmdLineA;
     1333}
     1334
     1335
     1336/**
     1337 * Gets the command line of the current process.
     1338 * @returns     On success:
     1339 *                  Command line of the current process. One single string.
     1340 *                  The first part of the command line string is the executable filename
     1341 *                  of the current process. It might be in quotes if it contains spaces.
     1342 *                  The rest of the string is arguments.
     1343 *
     1344 *              On error:
     1345 *                  NULL. Last error set. (does Win32 set last error this?)
     1346 * @sketch      IF not inited THEN
     1347 *                  Init commandline assuming !PE.EXE
     1348 *                  IF init failes THEN set last error.
     1349 *              ENDIF
     1350 *              return Unicode commandline.
     1351 * @status      Completely implemented and tested.
     1352 * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no)
     1353 * @remark      The Ring-3 PeLdr is resposible for calling InitCommandLine before anyone
     1354 *              is able to call this function.
     1355 */
    10671356LPCWSTR WIN32API GetCommandLineW(void)
    10681357{
    1069  static WCHAR *UnicodeCmdLine = NULL;
    1070          char *asciicmdline = NULL;
    1071 
    1072     dprintf(("KERNEL32:  FS = %x\n", GetFS()));
    1073 
    1074     if(UnicodeCmdLine)
    1075         return(UnicodeCmdLine); //already called before
    1076 
    1077     if(WinExe) {
    1078         if(WinExe->getCommandLineW())
    1079              return WinExe->getCommandLineW();
    1080     }
    1081     if(asciicmdline == NULL) //not used for converted exes
    1082         asciicmdline = O32_GetCommandLine();
    1083 
    1084     if(asciicmdline) {
    1085         UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
    1086         AsciiToUnicode(asciicmdline, UnicodeCmdLine);
    1087         dprintf(("KERNEL32:  OS2GetCommandLineW: %s\n", asciicmdline));
    1088         return(UnicodeCmdLine);
    1089     }
    1090     dprintf(("KERNEL32:  OS2GetCommandLineW: asciicmdline == NULL\n"));
    1091     return NULL;
    1092 }
    1093 //******************************************************************************
    1094 //******************************************************************************
     1358    /*
     1359     * Check if the commandline is initiated.
     1360     * If not we'll have to do it.
     1361     * ASSUMES that if not inited this isn't a PE.EXE lauched process.
     1362     */
     1363    if (pszCmdLineW == NULL)
     1364    {
     1365        APIRET rc;
     1366        rc = InitCommandLine(NULL);
     1367        if (rc != NULL)
     1368            SetLastError(rc);
     1369    }
     1370
     1371    dprintf(("KERNEL32:  GetCommandLineW: %s\n", pszCmdLineA));
     1372    return pszCmdLineW;
     1373}
     1374
     1375
    10951376DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
    10961377{
     
    11541435    else    hMod = -1;
    11551436  }
    1156   else 
     1437  else
    11571438  {
    11581439    strcpy(szModule, OSLibStripPath((char *)lpszModule));
Note: See TracChangeset for help on using the changeset viewer.