-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathargs.cpp
287 lines (257 loc) · 6.93 KB
/
args.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "header.h"
BOOL ParseCommandlineW(LPARGS args)
{
PWCHAR lpToken;
PFILEINFO fi;
//
// init defaults
//
args->bHelp = FALSE; // /?
args->bAnsi = FALSE; // /A
args->bContinue = FALSE; // /C
args->bAddDate = FALSE; // /D
args->bIntermediate = FALSE; // /I
args->bAddTime = FALSE; // /T
args->bUnicode = FALSE; // /U
args->bFwdExitCode = FALSE; // /E
args->bElapsedTime = FALSE; // /ET
args->dwBufSize = 0x4000;
args->dwPeekTimeout = 50;
//
// point to first fileinfo and init. This will contain the entry for the output file that is the console, unless redirected.
//
fi = &args->fi;
fi->hFile = NULL;
fi->lpFileName = NULL;
fi->fiNext = NULL;
fi->bAppend = FALSE;
while(GetCommandLineTokenW(&lpToken))
{
if(!lstrcmpiW(lpToken, L"/?")) // help
args->bHelp = TRUE;
else if(!lstrcmpiW(lpToken, L"/A")) // ansi
args->bAnsi = TRUE;
else if(!lstrcmpiW(lpToken, L"/C")) // continue
args->bContinue = TRUE;
else if(!lstrcmpiW(lpToken, L"/D")) // add date
args->bAddDate = TRUE;
else if(!lstrcmpiW(lpToken, L"/I")) // create intermediate dirs
args->bIntermediate = TRUE;
else if(!lstrcmpiW(lpToken, L"/E")) // forward exit code
args->bFwdExitCode = TRUE;
else if(!lstrcmpiW(lpToken, L"/T")) // add time
args->bAddTime = TRUE;
else if(!lstrcmpiW(lpToken, L"/U")) // unicode
args->bUnicode = TRUE;
else if(!lstrcmpiW(lpToken, L"/ET")) // elapsed time
args->bElapsedTime = TRUE;
else if(!lstrcmpiW(lpToken, L"/+")) // append
{
//
// next token should be a filename, load it and create a new fileinfo struct
//
if(!GetCommandLineTokenW(&lpToken))
{
Verbose(TEXT("The /+ switch must be followed by a filename.\r\n"));
return FALSE;
}
if(!CreateFileInfoStruct(&fi)) ExitProcess(Perror((DWORD)NULL));
fi->bAppend = TRUE;
if(!CheckFileName(lpToken)) ExitProcess(Perror(ERROR_INVALID_PARAMETER));
if(!StringAllocW(&fi->lpFileName, lpToken)) ExitProcess(Perror((DWORD)NULL));
}
else
{
// unknown token so assume a filename
if(!CreateFileInfoStruct(&fi)) ExitProcess(Perror((DWORD)NULL));
if(!CheckFileName(lpToken)) ExitProcess(Perror(ERROR_INVALID_PARAMETER));
if(!StringAllocW(&fi->lpFileName, lpToken)) ExitProcess(Perror((DWORD)NULL));
}
}
//
// validate the options
//
if((args->bAnsi + args->bUnicode) > 1)
{
Verbose(TEXT("The /A and /U switches cannot be used together.\r\n"));
return FALSE;
}
return TRUE;
}
BOOL CheckFileName(PWCHAR lpToken)
{
PWCHAR p = lpToken;
WCHAR strBadChars[] = L"<>|\"/";
PWCHAR lpBadChars;
if(!lstrcmpW(lpToken, L"")) return FALSE;
while(*p)
{
lpBadChars = strBadChars;
while(*lpBadChars) if(*p == *lpBadChars++) return FALSE;
p++;
}
return TRUE;
}
PFILEINFO CreateFileInfoStruct(PFILEINFO *fi)
{
(*fi)->fiNext = (PFILEINFO) HeapAlloc(GetProcessHeap(), 0, sizeof(FILEINFO));
if(!(*fi)->fiNext) return NULL;
//
// init and point to new struct
//
(*fi) = (*fi)->fiNext;
(*fi)->hFile = NULL;
(*fi)->lpFileName = NULL;
(*fi)->bAppend = FALSE;
(*fi)->fiNext = NULL;
return *fi;
}
VOID FreeFileInfoStructs(PFILEINFO fi)
{
while(fi)
{
HeapFree(GetProcessHeap(), 0, fi->lpFileName);
HeapFree(GetProcessHeap(), 0, fi = fi->fiNext);
}
}
LPWSTR StringAllocW(PWCHAR *dest, LPCWSTR src)
{
*dest = (WCHAR *) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src) * sizeof(WCHAR) + sizeof(WCHAR)));
if(!*dest) return NULL;
else return lstrcpyW(*dest, src);
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Function: GetCommandLineToken()
//
// Synopsis: Parses a commandline, returning one token at a time
//
// Arguments: [lpToken] Pointer to a pointer. Points to the found token
//
// Returns: TRUE if a token is found, otherwise FALSE (end of commandline
// reached
//
// Notes: Each call sets lpToken to point to each token in turn until no
// more found
//
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
BOOL GetCommandLineTokenW(PWCHAR *lpToken)
{
static PWCHAR p = NULL; // keeps track of where we've got to on windows cmdline
static PWCHAR lpTokenBuf = NULL; // buffer to hold each token in turn
PWCHAR t; // keeps track of current token
DWORD cQuote = 0; // keeps tracks of quotes
BOOL bOutQuotes = TRUE; // true if outside of quotes
BOOL bInToken = FALSE; // true if inside a token
//
// if p is NULL, then I'm being called for first time
//
if(!p)
{
//
// get a pointer to windows commandline
//
p = GetCommandLineW();
//
// allocate enough memory to hold any tokens in the commandline
//
lpTokenBuf = (PWCHAR) HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (lstrlenW(p) + sizeof(WCHAR)));
if(!lpTokenBuf) ExitProcess(Perror((DWORD)NULL));
//
// skip over name of exe to get to start arguments
// windows always removes leading whitespace and adds a trailing space
// if not already present. [; ; prog/s] --> [prog /s]
//
if(*p == L'"')
{
//
// if exe name begins with a quote(s), then skip over all beginning quotes
//
while(*p == L'"') p++;
//
// now skip to the ending quote(s)
//
while(*p != L'"') p++;
//
// finally skip any further trailing quotes
//
while(*p == L'"') p++;
}
//
// didn't start with a quote, so skip to first whitespace
//
else
{
while((*p) && (*p != L' ') && (*p != L'\t')) p++;
}
//
// skip over any whitespace to first token or end of commandline
//
while(*p && (*p == L' ' || *p == L'\t')) p++;
}
//
// initialise token pointers and token buffer
//
*lpToken = t = lstrcpyW(lpTokenBuf, L"");
//
// now start to copy characters from windows commandline to token buffer
//
if(*p)
{
while(*p)
{
//
// skip over whitespace if not inside a quoted token
//
if((*p == L' ' || *p == L'\t') && bOutQuotes)
{
break;
}
else if(*p == L'"')
{
bOutQuotes = !bOutQuotes;
p++;
//
// three consecutive quotes make one 'real' quote
//
if(++cQuote == 3)
{
*t++ = L'"';
cQuote = 0;
bOutQuotes = !bOutQuotes;
}
continue;
}
//
// if forward slash found then reset cQuote and toggle bInToken flag
//
else if(*p == L'/')
{
cQuote = 0;
bInToken = !bInToken;
//
// if not a token and not in quotes then break and l
//
if(!bInToken && bOutQuotes) break;
}
cQuote = 0;
bInToken = TRUE;
//
// add the character to the token
//
*t++ = *p++;
} // while(*p)
//
// terminate the token
//
*t = L'\0';
//
// set p to point to next argument ready for next call
//
while(*p && (*p == L' ' || *p == L'\t')) p++;
return TRUE;
}
//
// reached the end of windows commandline
//
else return FALSE;
}