ReactOS 0.4.15-dev-7918-g2a2556c
__getmainargs.c File Reference
#include <apitest.h>
#include <stdio.h>
#include <string.h>
Include dependency graph for __getmainargs.c:

Go to the source code of this file.

Macros

#define winetest_ok_str(x, y)    winetest_ok(strcmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", y, x)
 
#define winetest_ok_wstr(x, y)    winetest_ok(wcscmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", wine_dbgstr_w(y), wine_dbgstr_w(x))
 
#define ok_argsA   (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_argsA_imp
 
#define ok_argsW   (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_argsW_imp
 

Functions

const char ** __p__acmdln (void)
 
void __getmainargs (int *argc, char ***argv, char ***env, int expand_wildcards, int *new_mode)
 
const wchar_t ** __p__wcmdln (void)
 
void __wgetmainargs (int *argc, wchar_t ***wargv, wchar_t ***wenv, int expand_wildcards, int *new_mode)
 
void ok_argsA_imp (const char *input_args, const char *arg1, const char *arg2, const char *arg3)
 
void ok_argsW_imp (const wchar_t *input_args, const wchar_t *arg1, const wchar_t *arg2, const wchar_t *arg3)
 
 START_TEST (__getmainargs)
 

Macro Definition Documentation

◆ ok_argsA

#define ok_argsA   (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_argsA_imp

Definition at line 23 of file __getmainargs.c.

◆ ok_argsW

#define ok_argsW   (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_argsW_imp

Definition at line 24 of file __getmainargs.c.

◆ winetest_ok_str

#define winetest_ok_str (   x,
  y 
)     winetest_ok(strcmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", y, x)

Definition at line 19 of file __getmainargs.c.

◆ winetest_ok_wstr

#define winetest_ok_wstr (   x,
  y 
)     winetest_ok(wcscmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", wine_dbgstr_w(y), wine_dbgstr_w(x))

Definition at line 21 of file __getmainargs.c.

Function Documentation

◆ __getmainargs()

void __getmainargs ( int argc,
char ***  argv,
char ***  env,
int  expand_wildcards,
int new_mode 
)

Definition at line 182 of file getargs.c.

183{
184 int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex, anyLetter;
185 size_t len;
186 char* buffer;
187
188 /* missing threading init */
189
190 i = 0;
191 doexpand = expand_wildcards;
192 escapedQuote = FALSE;
193 anyLetter = FALSE;
194 slashesAdded = 0;
195 inQuotes = 0;
196 bufferIndex = 0;
197
198 if (__argv && _environ)
199 {
200 *argv = __argv;
201 *env = _environ;
202 *argc = __argc;
203 return;
204 }
205
206 __argc = 0;
207
208 len = strlen(_acmdln);
209 buffer = malloc(sizeof(char) * len);
210
211 // Reference: https://msdn.microsoft.com/en-us/library/a1y7w461.aspx
212 while (TRUE)
213 {
214 // Arguments are delimited by white space, which is either a space or a tab.
215 if (i >= len || ((_acmdln[i] == ' ' || _acmdln[i] == '\t') && !inQuotes))
216 {
217 // Handle the case when empty spaces are in the end of the cmdline
218 if (anyLetter)
219 {
220 aexpand(strndup(buffer, bufferIndex), doexpand);
221 }
222 // Copy the last element from buffer and quit the loop
223 if (i >= len)
224 {
225 break;
226 }
227
228 while (_acmdln[i] == ' ' || _acmdln[i] == '\t')
229 ++i;
230 anyLetter = FALSE;
231 bufferIndex = 0;
232 slashesAdded = 0;
233 escapedQuote = FALSE;
234 continue;
235 }
236
237 anyLetter = TRUE;
238
239 if (_acmdln[i] == '\\')
240 {
242 ++slashesAdded;
243 ++i;
244 escapedQuote = FALSE;
245 continue;
246 }
247
248 if (_acmdln[i] == '\"')
249 {
250 if (slashesAdded > 0)
251 {
252 if (slashesAdded % 2 == 0)
253 {
254 // If an even number of backslashes is followed by a double quotation mark, then one backslash (\‍)
255 // is placed in the argv array for every pair of backslashes (\\‍), and the double quotation mark (")
256 // is interpreted as a string delimiter.
257 bufferIndex -= slashesAdded / 2;
258 }
259 else
260 {
261 // If an odd number of backslashes is followed by a double quotation mark, then one backslash (\‍)
262 // is placed in the argv array for every pair of backslashes (\\‍) and the double quotation mark is
263 // interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark (")
264 // to be placed in argv.
265 bufferIndex -= slashesAdded / 2 + 1;
266 buffer[bufferIndex++] = '\"';
267 slashesAdded = 0;
268 escapedQuote = TRUE;
269 ++i;
270 continue;
271 }
272 slashesAdded = 0;
273 }
274 else if (!inQuotes && i > 0 && _acmdln[i - 1] == '\"' && !escapedQuote)
275 {
276 buffer[bufferIndex++] = '\"';
277 ++i;
278 escapedQuote = TRUE;
279 continue;
280 }
281 slashesAdded = 0;
282 escapedQuote = FALSE;
283 inQuotes = !inQuotes;
284 doexpand = inQuotes ? FALSE : expand_wildcards;
285 ++i;
286 continue;
287 }
288
290 slashesAdded = 0;
291 escapedQuote = FALSE;
292 ++i;
293 }
294
295 /* Free the temporary buffer. */
296 free(buffer);
297
298 *argc = __argc;
299 if (__argv == NULL)
300 {
301 __argv = (char**)malloc(sizeof(char*));
302 __argv[0] = 0;
303 }
304 *argv = __argv;
305 *env = _environ;
306
307 _pgmptr = malloc(MAX_PATH * sizeof(char));
308 if (_pgmptr)
309 {
311 _pgmptr[0] = '\0';
312 else
313 _pgmptr[MAX_PATH - 1] = '\0';
314 }
315 else
316 {
317 _pgmptr = _strdup(__argv[0]);
318 }
319
321
322 // if (new_mode) _set_new_mode(*new_mode);
323}
static int argc
Definition: ServiceArgs.c:12
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
static LPCWSTR LPCWSTR LPCWSTR env
Definition: db.cpp:170
#define free
Definition: debug_ros.c:5
#define _strdup
Definition: debug_ros.c:7
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define GetProcessHeap()
Definition: compat.h:736
#define MAX_PATH
Definition: compat.h:34
DWORD WINAPI GetModuleFileNameA(HINSTANCE hModule, LPSTR lpFilename, DWORD nSize)
Definition: loader.c:539
GLuint buffer
Definition: glext.h:5915
GLuint bufferIndex
Definition: glext.h:7857
GLenum GLsizei len
Definition: glext.h:6722
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
BOOL WINAPI HeapValidate(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem)
Definition: heapmem.c:156
char * _acmdln
Definition: environ.c:18
char ** __argv
Definition: getargs.c:18
int aexpand(char *name, int expand_wildcards)
Definition: getargs.c:132
int __argc
Definition: getargs.c:21
char * strndup(char const *name, size_t len)
Definition: getargs.c:25
char * _pgmptr
Definition: environ.c:27
char ** _environ
Definition: environ.c:22
#define argv
Definition: mplay32.c:18

Referenced by ok_argsA_imp().

◆ __p__acmdln()

const char ** __p__acmdln ( void  )

Definition at line 368 of file environ.c.

369{
370 return &_acmdln;
371}
char * _acmdln
Definition: environ.c:18

Referenced by ok_argsA_imp(), and Test__acmdln().

◆ __p__wcmdln()

const wchar_t ** __p__wcmdln ( void  )

Definition at line 376 of file environ.c.

377{
378 return &_wcmdln;
379}
wchar_t * _wcmdln
Definition: environ.c:19

Referenced by ok_argsW_imp(), and Test__wcmdln().

◆ __wgetmainargs()

void __wgetmainargs ( int argc,
wchar_t ***  wargv,
wchar_t ***  wenv,
int  expand_wildcards,
int new_mode 
)

Definition at line 328 of file getargs.c.

330{
331 int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex, anyLetter;
332 size_t len;
333 wchar_t* buffer;
334
335 /* missing threading init */
336
337 i = 0;
338 doexpand = expand_wildcards;
339 escapedQuote = FALSE;
340 anyLetter = TRUE;
341 slashesAdded = 0;
342 inQuotes = 0;
343 bufferIndex = 0;
344
345 if (__wargv && __winitenv)
346 {
347 *wargv = __wargv;
348 *wenv = __winitenv;
349 *argc = __argc;
350 return;
351 }
352
353 __argc = 0;
354
355 len = wcslen(_wcmdln);
356 buffer = malloc(sizeof(wchar_t) * len);
357
358 // Reference: https://msdn.microsoft.com/en-us/library/a1y7w461.aspx
359 while (TRUE)
360 {
361 // Arguments are delimited by white space, which is either a space or a tab.
362 if (i >= len || ((_wcmdln[i] == ' ' || _wcmdln[i] == '\t') && !inQuotes))
363 {
364 // Handle the case when empty spaces are in the end of the cmdline
365 if (anyLetter)
366 {
367 wexpand(wcsndup(buffer, bufferIndex), doexpand);
368 }
369 // Copy the last element from buffer and quit the loop
370 if (i >= len)
371 {
372 break;
373 }
374
375 while (_wcmdln[i] == ' ' || _wcmdln[i] == '\t')
376 ++i;
377 anyLetter = FALSE;
378 bufferIndex = 0;
379 slashesAdded = 0;
380 escapedQuote = FALSE;
381 continue;
382 }
383
384 anyLetter = TRUE;
385
386 if (_wcmdln[i] == '\\')
387 {
389 ++slashesAdded;
390 ++i;
391 escapedQuote = FALSE;
392 continue;
393 }
394
395 if (_wcmdln[i] == '\"')
396 {
397 if (slashesAdded > 0)
398 {
399 if (slashesAdded % 2 == 0)
400 {
401 // If an even number of backslashes is followed by a double quotation mark, then one backslash (\‍)
402 // is placed in the argv array for every pair of backslashes (\\‍), and the double quotation mark (")
403 // is interpreted as a string delimiter.
404 bufferIndex -= slashesAdded / 2;
405 }
406 else
407 {
408 // If an odd number of backslashes is followed by a double quotation mark, then one backslash (\‍)
409 // is placed in the argv array for every pair of backslashes (\\‍) and the double quotation mark is
410 // interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark (")
411 // to be placed in argv.
412 bufferIndex -= slashesAdded / 2 + 1;
413 buffer[bufferIndex++] = '\"';
414 slashesAdded = 0;
415 escapedQuote = TRUE;
416 ++i;
417 continue;
418 }
419 slashesAdded = 0;
420 }
421 else if (!inQuotes && i > 0 && _wcmdln[i - 1] == '\"' && !escapedQuote)
422 {
423 buffer[bufferIndex++] = '\"';
424 ++i;
425 escapedQuote = TRUE;
426 continue;
427 }
428 slashesAdded = 0;
429 escapedQuote = FALSE;
430 inQuotes = !inQuotes;
431 doexpand = inQuotes ? FALSE : expand_wildcards;
432 ++i;
433 continue;
434 }
435
437 slashesAdded = 0;
438 escapedQuote = FALSE;
439 ++i;
440 }
441
442 /* Free the temporary buffer. */
443 free(buffer);
444
445 *argc = __argc;
446 if (__wargv == NULL)
447 {
448 __wargv = (wchar_t**)malloc(sizeof(wchar_t*));
449 __wargv[0] = 0;
450 }
451 *wargv = __wargv;
452 *wenv = __winitenv;
453
454 _wpgmptr = malloc(MAX_PATH * sizeof(wchar_t));
455 if (_wpgmptr)
456 {
458 _wpgmptr[0] = '\0';
459 else
460 _wpgmptr[MAX_PATH - 1] = '\0';
461 }
462 else
463 {
465 }
466
468
469 // if (new_mode) _set_new_mode(*new_mode);
470}
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
wchar_t * _wpgmptr
Definition: environ.c:29
int wexpand(wchar_t *name, int expand_wildcards)
Definition: getargs.c:67
wchar_t ** __winitenv
Definition: environ.c:25
wchar_t * _wcmdln
Definition: environ.c:19
wchar_t * wcsndup(wchar_t *name, size_t len)
Definition: getargs.c:36
wchar_t ** __wargv
Definition: getargs.c:20
_Check_return_ _CRTIMP wchar_t *__cdecl _wcsdup(_In_z_ const wchar_t *_Str)

Referenced by ok_argsW_imp().

◆ ok_argsA_imp()

void ok_argsA_imp ( const char input_args,
const char arg1,
const char arg2,
const char arg3 
)

Definition at line 28 of file __getmainargs.c.

29{
30 int argc = 0, mode = 0;
31 int expect_count = arg3 == NULL ? (arg2 == NULL ? 2 : 3) : 4;
32 char** argv, **env;
33
34 /* Remove cached argv, setup our input as program argument. */
35 *__p___argv() = NULL;
36 *__p__acmdln() = input_args;
37
38 /* Process the commandline stored in _acmdln */
39 __getmainargs(&argc, &argv, &env, 0, &mode);
40
41 winetest_ok(argc == expect_count, "Wrong value for argc, expected: %d, got: %d\n", expect_count, argc);
42 if(argc != expect_count)
43 return;
44
45 winetest_ok_str(argv[0], "test.exe");
47 if (expect_count > 2)
48 {
50 if (expect_count > 3)
52 }
53}
#define winetest_ok_str(x, y)
Definition: __getmainargs.c:19
void __getmainargs(int *argc, char ***argv, char ***env, int expand_wildcards, int *new_mode)
Definition: getargs.c:182
const char ** __p__acmdln(void)
Definition: environ.c:368
GLuint GLuint GLuint GLuint arg1
Definition: glext.h:9513
GLuint GLuint GLuint GLuint GLuint GLuint GLuint GLuint GLuint GLuint arg3
Definition: glext.h:9515
GLuint GLuint GLuint GLuint GLuint GLuint GLuint arg2
Definition: glext.h:9514
GLenum mode
Definition: glext.h:6217
_CRTIMP char ***__cdecl __p___argv()
Definition: getargs.c:483
void __winetest_cdecl winetest_ok(int condition, const char *msg,...)

◆ ok_argsW_imp()

void ok_argsW_imp ( const wchar_t input_args,
const wchar_t arg1,
const wchar_t arg2,
const wchar_t arg3 
)

Definition at line 56 of file __getmainargs.c.

57{
58 int argc = 0, mode = 0;
59 int expect_count = arg3 == NULL ? (arg2 == NULL ? 2 : 3) : 4;
60 wchar_t** argv, **env;
61
62 /* Remove cached wargv, setup our input as program argument. */
63 *__p___wargv() = NULL;
64 *__p__wcmdln() = input_args;
65
66 /* Process the commandline stored in _wcmdln */
67 __wgetmainargs(&argc, &argv, &env, 0, &mode);
68
69 winetest_ok(argc == expect_count, "Wrong value for argc, expected: %d, got: %d\n", expect_count, argc);
70 if(argc != expect_count)
71 return;
72
73 winetest_ok_wstr(argv[0], L"test.exe");
75 if (expect_count > 2)
76 {
78 if (expect_count > 3)
80 }
81}
#define winetest_ok_wstr(x, y)
Definition: __getmainargs.c:21
void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenv, int expand_wildcards, int *new_mode)
Definition: getargs.c:328
const wchar_t ** __p__wcmdln(void)
Definition: environ.c:376
_CRTIMP wchar_t ***__cdecl __p___wargv()
Definition: getargs.c:491
#define L(x)
Definition: ntvdm.h:50

◆ START_TEST()

START_TEST ( __getmainargs  )

Definition at line 83 of file __getmainargs.c.

84{
85 ok_argsA("test.exe \"a b c\" d e", "a b c", "d", "e");
86 ok_argsA("test.exe \"ab\\\"c\" \"\\\\\" d", "ab\"c", "\\", "d");
87 ok_argsA("test.exe a\\\\\\b d\"e f\"g h", "a\\\\\\b", "de fg", "h");
88 ok_argsA("test.exe a\\\\\\\"b c d", "a\\\"b", "c", "d");
89 ok_argsA("test.exe a\\\\\\\\\"b c\" d e", "a\\\\b c", "d", "e");
90 ok_argsA("test.exe a b \"\"", "a", "b", "");
91 ok_argsA("test.exe a \"\" b", "a", "", "b");
92 ok_argsA("test.exe a \"b\"\" c", "a", "b\"", "c");
93 ok_argsA("test.exe a \"b\\\"\" c", "a", "b\"", "c");
94 ok_argsA("test.exe a \" b\\ \"\" c", "a", " b\\ \"", "c");
95 ok_argsA("test.exe a \"b\\ \"\"\" c\" d", "a", "b\\ \" c", "d");
96 ok_argsA("test.exe a \"b\\ \"\"\" \"c \"\"\"\" d", "a", "b\\ \" c", "\" d");
97 ok_argsA("test.exe a b c ", "a", "b", "c");
98 ok_argsA("test.exe \"a b c\"", "a b c", NULL, NULL);
99
100 ok_argsW(L"test.exe \"a b c\" d e", L"a b c", L"d", L"e");
101 ok_argsW(L"test.exe \"ab\\\"c\" \"\\\\\" d", L"ab\"c", L"\\", L"d");
102 ok_argsW(L"test.exe a\\\\\\b d\"e f\"g h", L"a\\\\\\b", L"de fg", L"h");
103 ok_argsW(L"test.exe a\\\\\\\"b c d", L"a\\\"b", L"c", L"d");
104 ok_argsW(L"test.exe a\\\\\\\\\"b c\" d e", L"a\\\\b c", L"d", L"e");
105 ok_argsW(L"test.exe a b \"\"", L"a", L"b", L"");
106 ok_argsW(L"test.exe a \"\" b", L"a", L"", L"b");
107 ok_argsW(L"test.exe a \"b\"\" c", L"a", L"b\"", L"c");
108 ok_argsW(L"test.exe a \"b\\\"\" c", L"a", L"b\"", L"c");
109 ok_argsW(L"test.exe a \" b\\ \"\" c", L"a", L" b\\ \"", L"c");
110 ok_argsW(L"test.exe a \"b\\ \"\"\" c\" d", L"a", L"b\\ \" c", L"d");
111 ok_argsW(L"test.exe a \"b\\ \"\"\" \"c \"\"\"\" d", L"a", L"b\\ \" c", L"\" d");
112 ok_argsW(L"test.exe a b c ", L"a", L"b", L"c");
113 ok_argsW(L"test.exe \"a b c\"", L"a b c", NULL, NULL);
114}
#define ok_argsA
Definition: __getmainargs.c:23
#define ok_argsW
Definition: __getmainargs.c:24