-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvssWindows.c++
134 lines (122 loc) · 2.76 KB
/
vssWindows.c++
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
129
130
131
132
133
#include "platform.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <ctype.h>
/* Convert a string into an argv style of array
* This honors both single and double quotes. The quote
* characters can be escaped with a preceding backslash.
* Inputs: cmd Optional string to use as argv[0]. Ignored if NULL.
* sz Pointer to the string to convert
* Outputs: argc Pointer to a location for the argument count.
* argv Pointer to a location to hold the argv pointer.
* Note: new/delete cause a link error, so I'm using malloc/free.
*/
static int ScanArgv(char *cmd, const char *sz, int *argc, char ***argv)
{
char **wargv = NULL;
int wargc;
int len;
const char *pSrc;
char* pDst;
const char *pSrc2;
char *pDst2;
char quote;
if (!sz)
return 0;
// Count the number of fields, including the command if applicable
wargc = cmd ? 1 : 0;
for (pSrc=sz; *pSrc; )
{
while ((*pSrc) && isspace(*pSrc))
pSrc++;
if (*pSrc)
{
quote = (*pSrc == '"' || *pSrc == '\'') ? *pSrc++ : '\0';
if (quote)
{
for (pSrc2=pSrc; (*pSrc2) && (*pSrc2 != quote); )
{
if (*pSrc2 == '\\')
{
switch (*(pSrc2+1))
{
case '\'':
case '"':
pSrc2++;
break;
default:
break;
}
}
pSrc2++;
}
}
else
{
for (pSrc2=pSrc; (*pSrc2) && !isspace(*pSrc2); )
pSrc2++;
}
wargc++;
pSrc = pSrc2;
}
}
// Get a buffer large enough to hold the argv and the string
len = strlen(sz) + 1;
if (cmd)
len += strlen(cmd) + 1;
len += (sizeof(char *)) * wargc;
// wargv = (char**) new char[len];
wargv = (char**)malloc(len);
if (!wargv)
{
fprintf(stderr, "vss error: out of memory.\n");
return 0;
}
// Move the caller's string to the buffer
pDst = ((char*)wargv) + ((sizeof(char*)) * wargc);
strcpy(pDst, sz);
// If the caller provided a command, move it in also
wargc = 0;
if (cmd)
{
pDst2 = pDst + strlen(pDst) + 1;
strcpy(pDst2, cmd);
*(wargv+(wargc++)) = pDst2;
}
// Parse the string, splitting into the component fields
while (*pDst)
{
while (*pDst && isspace(*pDst))
pDst++;
if (*pDst)
{
quote = (*pDst == '"' || *pDst == '\'') ? *pDst++ : '\0';
if (quote)
for (pDst2=pDst; (*pDst2) && (*pDst2 != quote); )
pDst2++;
else
for (pDst2=pDst; (*pDst2) && (! isspace(*pDst2)); )
pDst2++;
if (*pDst2)
*pDst2++ = '\0';
*(wargv+(wargc++)) = pDst;
pDst = pDst2;
}
}
*argc = wargc;
*argv = wargv;
return 1;
}
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
int argc = 0;
char **argv = NULL;
if (!ScanArgv("vss", lpCmdLine, &argc, &argv))
return -1;
int w = VSS_main(argc, argv /*, hInstance*/);
// delete [] argv;
free(argv);
return w;
}