Skip to content

Commit

Permalink
Delete SegmentCommandLine from utilcode (#71111)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas authored Jun 22, 2022
1 parent 604681a commit 4dc676f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 266 deletions.
4 changes: 0 additions & 4 deletions src/coreclr/ilasm/assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@

#define dwUniBuf 16384

#ifdef TARGET_UNIX
extern char *g_pszExeFile;
#endif

extern WCHAR wzUniBuf[]; // Unicode conversion global buffer (assem.cpp)

class Class;
Expand Down
4 changes: 0 additions & 4 deletions src/coreclr/ilasm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ static DWORD g_dwSubsystem=(DWORD)-1,g_dwComImageFlags=(DWORD)-1,g_dwFileAlig
static ULONGLONG g_stBaseAddress=0;
static size_t g_stSizeOfStackReserve=0;
extern unsigned int g_uConsoleCP;
#ifdef TARGET_UNIX
char * g_pszExeFile;
#endif

void MakeTestFile(_In_ __nullterminated char* szFileName)
{
Expand Down Expand Up @@ -855,7 +852,6 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
#ifdef TARGET_UNIX
int main(int argc, char* str[])
{
g_pszExeFile = str[0];
if (0 != PAL_Initialize(argc, str))
{
fprintf(stderr,"Error: Fail to PAL_Initialize\n");
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/ildasm/dasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ BOOL g_fCustomInstructionEncodingSystem = FALSE;
COR_FIELD_OFFSET *g_rFieldOffset = NULL;
ULONG g_cFieldsMax, g_cFieldOffsets;

char* g_pszExeFile;
char g_szInputFile[MAX_FILENAME_LENGTH]; // in UTF-8
WCHAR g_wszFullInputFile[MAX_PATH + 1]; // in UTF-16
char g_szOutputFile[MAX_FILENAME_LENGTH]; // in UTF-8
Expand Down
91 changes: 23 additions & 68 deletions src/coreclr/ildasm/windasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ extern char g_szAsmCodeIndent[];

extern DWORD g_Mode;

extern char* g_pszExeFile;
extern char g_szInputFile[]; // in UTF-8
extern WCHAR g_wszFullInputFile[]; // in UTF-16
extern char g_szOutputFile[]; // in UTF-8
Expand Down Expand Up @@ -424,37 +423,10 @@ int ProcessOneArg(_In_ __nullterminated char* szArg, _Out_ char** ppszObjFileNam
return 0;
}

char* UTF8toANSI(_In_ __nullterminated char* szUTF)
{
ULONG32 L = (ULONG32) strlen(szUTF)+16;
WCHAR* wzUnicode = new WCHAR[L];
memset(wzUnicode,0,L*sizeof(WCHAR));
WszMultiByteToWideChar(CP_UTF8,0,szUTF,-1,wzUnicode,L);
L <<= 2;
char* szANSI = new char[L];
memset(szANSI,0,L);
WszWideCharToMultiByte(g_uConsoleCP,0,wzUnicode,-1,szANSI,L,NULL,NULL);
VDELETE(wzUnicode);
return szANSI;
}
char* ANSItoUTF8(_In_ __nullterminated char* szANSI)
{
ULONG32 L = (ULONG32) strlen(szANSI)+16;
WCHAR* wzUnicode = new WCHAR[L];
memset(wzUnicode,0,L*sizeof(WCHAR));
WszMultiByteToWideChar(g_uConsoleCP,0,szANSI,-1,wzUnicode,L);
L *= 3;
char* szUTF = new char[L];
memset(szUTF,0,L);
WszWideCharToMultiByte(CP_UTF8,0,wzUnicode,-1,szUTF,L,NULL,NULL);
VDELETE(wzUnicode);
return szUTF;
}

int ParseCmdLineW(_In_ __nullterminated WCHAR* wzCmdLine, _Out_ char** ppszObjFileName)
#ifdef HOST_WINDOWS
int ParseCmdLineW(int argc, _In_ __nullterminated wchar_t* argv[], _Out_ char** ppszObjFileName)
{
int argc,ret=0;
LPWSTR* argv= SegmentCommandLine(wzCmdLine, (DWORD*)&argc);
int ret=0;
char* szArg = new char[2048];
for(int i=1; i < argc; i++)
{
Expand All @@ -465,54 +437,32 @@ int ParseCmdLineW(_In_ __nullterminated WCHAR* wzCmdLine, _Out_ char** ppszObjFi
VDELETE(szArg);
return ret;
}

int ParseCmdLineA(_In_ __nullterminated char* szCmdLine, _Out_ char** ppszObjFileName)
#else
int ParseCmdLine(int argc, _In_ __nullterminated char* argv[], _Out_ char** ppszObjFileName)
{
if((szCmdLine == NULL)||(*szCmdLine == 0)) return 0;

// ANSI to UTF-8
char* szCmdLineUTF = ANSItoUTF8(szCmdLine);

// Split into argv[]
int argc=0, ret = 0;
DynamicArray<char*> argv;
char* pch;
char* pchend;
bool bUnquoted = true;

pch = szCmdLineUTF;
pchend = pch+strlen(szCmdLineUTF);
while(pch)
int ret = 0;
char* szArg = new char[2048];
for (int i = 1; i < argc; i++)
{
for(; *pch == ' '; pch++); // skip the blanks
argv[argc++] = pch;
for(; pch < pchend; pch++)
{
if(*pch == '"') bUnquoted = !bUnquoted;
else if((*pch == ' ')&&bUnquoted) break;
}

if(pch < pchend) *pch++ = 0;
else break;
if ((ret = ProcessOneArg(argv[i], ppszObjFileName)) != 0) break;
}

for(int i=1; i < argc; i++)
{
if((ret = ProcessOneArg(argv[i],ppszObjFileName)) != 0) break;
}
VDELETE(szCmdLineUTF);
VDELETE(szArg);
return ret;
}
#endif

int __cdecl main(int nCmdShow, char* lpCmdLine[])
#ifdef HOST_WINDOWS
int __cdecl wmain(int argc, wchar_t* argv[])
#else
int main(int argc, char* argv[])
#endif
{
#if defined(TARGET_UNIX)
if (0 != PAL_Initialize(nCmdShow, lpCmdLine))
if (0 != PAL_Initialize(argc, argv))
{
printError(g_pFile, "Error: Fail to PAL_Initialize\n");
exit(1);
}
g_pszExeFile = lpCmdLine[0];
#endif

#ifdef HOST_WINDOWS
Expand Down Expand Up @@ -552,7 +502,12 @@ int __cdecl main(int nCmdShow, char* lpCmdLine[])
g_hResources = WszGetModuleHandle(NULL);
#endif

iCommandLineParsed = ParseCmdLineW((wzCommandLine = GetCommandLineW()),&g_pszObjFileName);

#ifdef HOST_WINDOWS
iCommandLineParsed = ParseCmdLineW(argc, argv, &g_pszObjFileName);
#else
iCommandLineParsed = ParseCmdLine(argc, argv, &g_pszObjFileName);
#endif

if(!g_fLimitedVisibility)
{
Expand Down
34 changes: 0 additions & 34 deletions src/coreclr/inc/utilcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -3808,14 +3808,6 @@ inline bool FitsInRel28(INT64 val64)
return (val64 >= -0x08000000LL) && (val64 < 0x08000000LL);
}

//*****************************************************************************
// Splits a command line into argc/argv lists, using the VC7 parsing rules.
// This functions interface mimics the CommandLineToArgvW api.
// If function fails, returns NULL.
// If function suceeds, call delete [] on return pointer when done.
//*****************************************************************************
LPWSTR *SegmentCommandLine(LPCWSTR lpCmdLine, DWORD *pNumArgs);

//
// TEB access can be dangerous when using fibers because a fiber may
// run on multiple threads. If the TEB pointer is retrieved and saved
Expand All @@ -3840,11 +3832,6 @@ class ClrTeb
return (void *)(size_t)GetCurrentThreadId();
}

static void* InvalidFiberPtrId()
{
return NULL;
}

static void* GetStackBase()
{
return PAL_GetStackBase();
Expand Down Expand Up @@ -3877,33 +3864,12 @@ class ClrTeb
return NtCurrentTeb()->NtTib.StackLimit;
}

// Please don't start to use this method unless you absolutely have to.
// The reason why this is added is for WIN64 to support LEGACY PE-style TLS
// variables. On X86 it is supported by the JIT compilers themselves. On
// WIN64 we build more logic into the JIT helper for accessing fields.
static void* GetLegacyThreadLocalStoragePointer()
{
LIMITED_METHOD_CONTRACT;
return NtCurrentTeb()->ThreadLocalStoragePointer;
}

static void* GetOleReservedPtr()
{
LIMITED_METHOD_CONTRACT;
return NtCurrentTeb()->ReservedForOle;
}

static void* GetProcessEnvironmentBlock()
{
LIMITED_METHOD_CONTRACT;
return NtCurrentTeb()->ProcessEnvironmentBlock;
}


static void* InvalidFiberPtrId()
{
return (void*) 1;
}
#endif // HOST_UNIX
};

Expand Down
155 changes: 0 additions & 155 deletions src/coreclr/utilcode/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2600,161 +2600,6 @@ void PutArm64Rel12(UINT32 * pCode, INT32 imm12)
_ASSERTE(GetArm64Rel12(pCode) == imm12);
}

//---------------------------------------------------------------------
// Splits a command line into argc/argv lists, using the VC7 parsing rules.
//
// This functions interface mimics the CommandLineToArgvW api.
//
// If function fails, returns NULL.
//
// If function suceeds, call delete [] on return pointer when done.
//
//---------------------------------------------------------------------
// NOTE: Implementation-wise, once every few years it would be a good idea to
// compare this code with the C runtime library's parse_cmdline method,
// which is in vctools\crt\crtw32\startup\stdargv.c. (Note we don't
// support wild cards, and we use Unicode characters exclusively.)
// We are up to date as of ~6/2005.
//---------------------------------------------------------------------
LPWSTR *SegmentCommandLine(LPCWSTR lpCmdLine, DWORD *pNumArgs)
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FAULT;


*pNumArgs = 0;

int nch = (int)wcslen(lpCmdLine);

// Calculate the worstcase storage requirement. (One pointer for
// each argument, plus storage for the arguments themselves.)
int cbAlloc = (nch+1)*sizeof(LPWSTR) + sizeof(WCHAR)*(nch + 1);
LPWSTR pAlloc = new (nothrow) WCHAR[cbAlloc / sizeof(WCHAR)];
if (!pAlloc)
return NULL;

LPWSTR *argv = (LPWSTR*) pAlloc; // We store the argv pointers in the first halt
LPWSTR pdst = (LPWSTR)( ((BYTE*)pAlloc) + sizeof(LPWSTR)*(nch+1) ); // A running pointer to second half to store arguments
LPCWSTR psrc = lpCmdLine;
WCHAR c;
BOOL inquote;
BOOL copychar;
int numslash;

// First, parse the program name (argv[0]). Argv[0] is parsed under
// special rules. Anything up to the first whitespace outside a quoted
// subtring is accepted. Backslashes are treated as normal characters.
argv[ (*pNumArgs)++ ] = pdst;
inquote = FALSE;
do {
if (*psrc == W('"') )
{
inquote = !inquote;
c = *psrc++;
continue;
}
*pdst++ = *psrc;

c = *psrc++;

} while ( (c != W('\0') && (inquote || (c != W(' ') && c != W('\t')))) );

if ( c == W('\0') ) {
psrc--;
} else {
*(pdst-1) = W('\0');
}

inquote = FALSE;



/* loop on each argument */
for(;;)
{
if ( *psrc )
{
while (*psrc == W(' ') || *psrc == W('\t'))
{
++psrc;
}
}

if (*psrc == W('\0'))
break; /* end of args */

/* scan an argument */
argv[ (*pNumArgs)++ ] = pdst;

/* loop through scanning one argument */
for (;;)
{
copychar = 1;
/* Rules: 2N backslashes + " ==> N backslashes and begin/end quote
2N+1 backslashes + " ==> N backslashes + literal "
N backslashes ==> N backslashes */
numslash = 0;
while (*psrc == W('\\'))
{
/* count number of backslashes for use below */
++psrc;
++numslash;
}
if (*psrc == W('"'))
{
/* if 2N backslashes before, start/end quote, otherwise
copy literally */
if (numslash % 2 == 0)
{
if (inquote && psrc[1] == W('"'))
{
psrc++; /* Double quote inside quoted string */
}
else
{
/* skip first quote char and copy second */
copychar = 0; /* don't copy quote */
inquote = !inquote;
}
}
numslash /= 2; /* divide numslash by two */
}

/* copy slashes */
while (numslash--)
{
*pdst++ = W('\\');
}

/* if at end of arg, break loop */
if (*psrc == W('\0') || (!inquote && (*psrc == W(' ') || *psrc == W('\t'))))
break;

/* copy character into argument */
if (copychar)
{
*pdst++ = *psrc;
}
++psrc;
}

/* null-terminate the argument */

*pdst++ = W('\0'); /* terminate string */
}

/* We put one last argument in -- a null ptr */
argv[ (*pNumArgs) ] = NULL;

// If we hit this assert, we overwrote our destination buffer.
// Since we're supposed to allocate for the worst
// case, either the parsing rules have changed or our worse case
// formula is wrong.
_ASSERTE((BYTE*)pdst <= (BYTE*)pAlloc + cbAlloc);
return argv;
}

//======================================================================
// This function returns true, if it can determine that the instruction pointer
// refers to a code address that belongs in the range of the given image.
Expand Down

0 comments on commit 4dc676f

Please sign in to comment.