forked from racket/racket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_cmdl.inc
More file actions
128 lines (112 loc) · 2.78 KB
/
parse_cmdl.inc
File metadata and controls
128 lines (112 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Windows command-line parsing */
static char *wchar_to_char(wchar_t *wa, int len)
{
char *a;
int l;
l = scheme_utf8_encode((unsigned int *)wa, 0, len,
NULL, 0,
1 /* UTF-16 */);
a = (char *)malloc(l + 1);
scheme_utf8_encode((unsigned int *)wa, 0, len,
(unsigned char *)a, 0,
1 /* UTF-16 */);
a[l] = 0;
return a;
}
static int parse_command_line(char ***_command, char *buf)
{
GC_CAN_IGNORE unsigned char *parse, *created, *write;
int maxargs;
int findquote = 0;
char **command;
int count = 0;
maxargs = 49;
command = (char **)malloc((maxargs + 1) * sizeof(char *));
parse = created = write = (unsigned char *)buf;
while (*parse) {
int did_create = 0;
while (*parse && isspace(*parse)) { parse++; }
while (*parse && (!isspace(*parse) || findquote)) {
if (*parse== '"') {
findquote = !findquote;
did_create = 1;
} else if (*parse== '\\') {
GC_CAN_IGNORE unsigned char *next;
for (next = parse; *next == '\\'; next++) { }
if (*next == '"') {
/* Special handling: */
int count = (next - parse), i;
for (i = 1; i < count; i += 2) {
*(write++) = '\\';
}
parse += (count - 1);
if (count & 0x1) {
*(write++) = '\"';
parse++;
}
} else
*(write++) = *parse;
} else
*(write++) = *parse;
parse++;
}
if (*parse)
parse++;
*(write++) = 0;
if (*created || did_create) {
command[count++] = (char *)created;
if (count == maxargs) {
char **c2;
c2 = (char **)malloc(((2 * maxargs) + 1) * sizeof(char *));
memcpy(c2, command, maxargs * sizeof(char *));
maxargs *= 2;
}
}
created = write;
}
command[count] = NULL;
*_command = command;
return count;
}
static char **cmdline_to_argv(int *_argc, char **_normalized_path)
{
LPWSTR m_lpCmdLine;
int j, argc, l;
char *a, **argv, *normalized_path;
m_lpCmdLine = GetCommandLineW();
for (j = 0; m_lpCmdLine[j]; j++) {
}
a = wchar_to_char(m_lpCmdLine, j);
argc = parse_command_line(&argv, a);
/* argv[0] should be the name of the executable, but Windows doesn't
specify really where this name comes from, so we get it from
GetModuleFileName, just in case */
{
int name_len = 1024;
while (1) {
wchar_t *my_name;
my_name = (wchar_t *)malloc(sizeof(wchar_t) * name_len);
l = GetModuleFileNameW(NULL, my_name, name_len);
if (!l) {
name_len = GetLastError();
free(my_name);
my_name = NULL;
break;
} else if (l < name_len) {
a = wchar_to_char(my_name, l);
argv[0] = a;
CharLowerBuffW(my_name, l);
normalized_path = wchar_to_char(my_name, l);
free(my_name);
break;
} else {
free(my_name);
name_len = name_len * 2;
}
}
}
*_argc = argc;
if (_normalized_path)
*_normalized_path = normalized_path;
return argv;
}