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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/* $Id: getoptargv.cpp $ */
/** @file
* IPRT - Command Line Parsing, Argument Vector.
*/
/*
* Copyright (C) 2010-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/getopt.h>
#include "internal/iprt.h"
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/err.h>
#include <iprt/mem.h>
#include <iprt/string.h>
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
/**
* Array indexed by the quoting type and 7-bit ASCII character.
*
* We include some extra stuff here that the corresponding shell would normally
* require quoting of.
*/
static uint8_t
#ifndef IPRT_REGENERATE_QUOTE_CHARS
const
#endif
g_abmQuoteChars[RTGETOPTARGV_CNV_QUOTE_MASK + 1][16] =
{
{ 0xfe, 0xff, 0xff, 0xff, 0x65, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 },
{ 0xfe, 0xff, 0xff, 0xff, 0xd7, 0x07, 0x00, 0xd8, 0x00, 0x00, 0x00, 0x18, 0x01, 0x00, 0x00, 0x50 },
};
#ifdef IPRT_REGENERATE_QUOTE_CHARS /* To re-generate the bitmaps. */
# include <stdio.h>
int main()
{
RT_ZERO(g_abmQuoteChars);
# define SET_ALL(ch) \
do { \
for (size_t iType = 0; iType <= RTGETOPTARGV_CNV_QUOTE_MASK; iType++) \
ASMBitSet(&g_abmQuoteChars[iType], (ch)); \
} while (0)
# define SET(ConstSuffix, ch) \
do { \
ASMBitSet(&g_abmQuoteChars[RTGETOPTARGV_CNV_QUOTE_##ConstSuffix], (ch)); \
printf(#ConstSuffix ": %#x %d %c\n", (ch), (ch), (ch)); \
} while (0)
/* just flag all the control chars as in need of quoting. */
for (char ch = 1; ch < 0x20; ch++)
SET_ALL(ch);
/* ... and space of course */
SET_ALL(' ');
/* MS CRT / CMD.EXE: */
SET(MS_CRT, '"');
SET(MS_CRT, '&');
SET(MS_CRT, '>');
SET(MS_CRT, '<');
SET(MS_CRT, '|');
SET(MS_CRT, '%');
/* Bourne shell: */
SET(BOURNE_SH, '!');
SET(BOURNE_SH, '"');
SET(BOURNE_SH, '$');
SET(BOURNE_SH, '&');
SET(BOURNE_SH, '(');
SET(BOURNE_SH, ')');
SET(BOURNE_SH, '*');
SET(BOURNE_SH, ';');
SET(BOURNE_SH, '<');
SET(BOURNE_SH, '>');
SET(BOURNE_SH, '?');
SET(BOURNE_SH, '[');
SET(BOURNE_SH, '\'');
SET(BOURNE_SH, '\\');
SET(BOURNE_SH, '`');
SET(BOURNE_SH, '|');
SET(BOURNE_SH, '~');
for (size_t iType = 0; iType <= RTGETOPTARGV_CNV_QUOTE_MASK; iType++)
{
printf(" {");
for (size_t iByte = 0; iByte < 16; iByte++)
printf(iByte == 0 ? " 0x%02x" : ", 0x%02x", g_abmQuoteChars[iType][iByte]);
printf(" },\n");
}
return 0;
}
#else /* !IPRT_REGENERATE_QUOTE_CHARS */
/**
* Look for an unicode code point in the separator string.
*
* @returns true if it's a separator, false if it isn't.
* @param Cp The code point.
* @param pszSeparators The separators.
*/
static bool rtGetOptIsUniCpInString(RTUNICP Cp, const char *pszSeparators)
{
/* This could be done in a more optimal fashion. Probably worth a
separate RTStr function at some point. */
for (;;)
{
RTUNICP CpSep;
int rc = RTStrGetCpEx(&pszSeparators, &CpSep);
AssertRCReturn(rc, false);
if (CpSep == Cp)
return true;
if (!CpSep)
return false;
}
}
/**
* Look for an 7-bit ASCII character in the separator string.
*
* @returns true if it's a separator, false if it isn't.
* @param ch The character.
* @param pszSeparators The separators.
* @param cchSeparators The number of separators chars.
*/
DECLINLINE(bool) rtGetOptIsAsciiInSet(char ch, const char *pszSeparators, size_t cchSeparators)
{
switch (cchSeparators)
{
case 8: if (ch == pszSeparators[7]) return true; RT_FALL_THRU();
case 7: if (ch == pszSeparators[6]) return true; RT_FALL_THRU();
case 6: if (ch == pszSeparators[5]) return true; RT_FALL_THRU();
case 5: if (ch == pszSeparators[4]) return true; RT_FALL_THRU();
case 4: if (ch == pszSeparators[3]) return true; RT_FALL_THRU();
case 3: if (ch == pszSeparators[2]) return true; RT_FALL_THRU();
case 2: if (ch == pszSeparators[1]) return true; RT_FALL_THRU();
case 1: if (ch == pszSeparators[0]) return true;
return false;
default:
return memchr(pszSeparators, ch, cchSeparators) != NULL;
}
}
/**
* Checks if the character is in the set of separators
*
* @returns true if it is, false if it isn't.
*
* @param Cp The code point.
* @param pszSeparators The separators.
* @param cchSeparators The length of @a pszSeparators.
*/
DECL_FORCE_INLINE(bool) rtGetOptIsCpInSet(RTUNICP Cp, const char *pszSeparators, size_t cchSeparators)
{
if (RT_LIKELY(Cp <= 127))
return rtGetOptIsAsciiInSet((char)Cp, pszSeparators, cchSeparators);
return rtGetOptIsUniCpInString(Cp, pszSeparators);
}
/**
* Skips any delimiters at the start of the string that is pointed to.
*
* @returns VINF_SUCCESS or RTStrGetCpEx status code.
* @param ppszSrc Where to get and return the string pointer.
* @param pszSeparators The separators.
* @param cchSeparators The length of @a pszSeparators.
*/
static int rtGetOptSkipDelimiters(const char **ppszSrc, const char *pszSeparators, size_t cchSeparators)
{
const char *pszSrc = *ppszSrc;
const char *pszRet;
for (;;)
{
pszRet = pszSrc;
RTUNICP Cp;
int rc = RTStrGetCpEx(&pszSrc, &Cp);
if (RT_FAILURE(rc))
{
*ppszSrc = pszRet;
return rc;
}
if ( !Cp
|| !rtGetOptIsCpInSet(Cp, pszSeparators, cchSeparators))
break;
}
*ppszSrc = pszRet;
return VINF_SUCCESS;
}
RTDECL(int) RTGetOptArgvFromString(char ***ppapszArgv, int *pcArgs, const char *pszCmdLine,
uint32_t fFlags, const char *pszSeparators)
{
/*
* Some input validation.
*/
AssertPtr(pszCmdLine);
AssertPtr(pcArgs);
AssertPtr(ppapszArgv);
AssertReturn( (fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) == RTGETOPTARGV_CNV_QUOTE_BOURNE_SH
|| (fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) == RTGETOPTARGV_CNV_QUOTE_MS_CRT, VERR_INVALID_FLAGS);
AssertReturn(~(fFlags & ~RTGETOPTARGV_CNV_VALID_MASK), VERR_INVALID_FLAGS);
if (!pszSeparators)
pszSeparators = " \t\n\r";
else
AssertPtr(pszSeparators);
size_t const cchSeparators = strlen(pszSeparators);
AssertReturn(cchSeparators > 0, VERR_INVALID_PARAMETER);
/*
* Parse the command line and chop off it into argv individual argv strings.
*/
const char *pszSrc = pszCmdLine;
char *pszDup = NULL;
char *pszDst;
if (fFlags & RTGETOPTARGV_CNV_MODIFY_INPUT)
pszDst = (char *)pszCmdLine;
else
{
pszDst = pszDup = (char *)RTMemAlloc(strlen(pszSrc) + 1);
if (!pszDup)
return VERR_NO_STR_MEMORY;
}
int rc = VINF_SUCCESS;
char **papszArgs = NULL;
unsigned iArg = 0;
while (*pszSrc)
{
/* Skip stuff */
rc = rtGetOptSkipDelimiters(&pszSrc, pszSeparators, cchSeparators);
if (RT_FAILURE(rc))
break;
if (!*pszSrc)
break;
/* Start a new entry. */
if ((iArg % 32) == 0)
{
void *pvNew = RTMemRealloc(papszArgs, (iArg + 33) * sizeof(char *));
if (!pvNew)
{
rc = VERR_NO_MEMORY;
break;
}
papszArgs = (char **)pvNew;
}
papszArgs[iArg++] = pszDst;
/*
* Parse and copy the string over.
*/
RTUNICP uc;
if ((fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) == RTGETOPTARGV_CNV_QUOTE_BOURNE_SH)
{
/*
* Bourne shell style.
*/
RTUNICP ucQuote = 0;
for (;;)
{
rc = RTStrGetCpEx(&pszSrc, &uc);
if (RT_FAILURE(rc) || !uc)
break;
if (!ucQuote)
{
if (uc == '"' || uc == '\'')
ucQuote = uc;
else if (rtGetOptIsCpInSet(uc, pszSeparators, cchSeparators))
break;
else if (uc != '\\')
pszDst = RTStrPutCp(pszDst, uc);
else
{
/* escaped char */
rc = RTStrGetCpEx(&pszSrc, &uc);
if (RT_FAILURE(rc) || !uc)
break;
pszDst = RTStrPutCp(pszDst, uc);
}
}
else if (ucQuote != uc)
{
if (uc != '\\' || ucQuote == '\'')
pszDst = RTStrPutCp(pszDst, uc);
else
{
/* escaped char */
rc = RTStrGetCpEx(&pszSrc, &uc);
if (RT_FAILURE(rc) || !uc)
break;
if ( uc != '"'
&& uc != '\\'
&& uc != '`'
&& uc != '$'
&& uc != '\n')
pszDst = RTStrPutCp(pszDst, ucQuote);
pszDst = RTStrPutCp(pszDst, uc);
}
}
else
ucQuote = 0;
}
}
else
{
/*
* Microsoft CRT style.
*/
Assert((fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) == RTGETOPTARGV_CNV_QUOTE_MS_CRT);
bool fInQuote = false;
for (;;)
{
rc = RTStrGetCpEx(&pszSrc, &uc);
if (RT_FAILURE(rc) || !uc)
break;
if (uc == '"')
{
/* Two double quotes insides a quoted string in an escape
sequence and we output one double quote char.
See http://www.daviddeley.com/autohotkey/parameters/parameters.htm */
if (!fInQuote)
fInQuote = true;
else if (*pszSrc != '"')
fInQuote = false;
else
{
pszDst = RTStrPutCp(pszDst, '"');
pszSrc++;
}
}
else if (!fInQuote && rtGetOptIsCpInSet(uc, pszSeparators, cchSeparators))
break;
else if (uc != '\\')
pszDst = RTStrPutCp(pszDst, uc);
else
{
/* A backslash sequence is only relevant if followed by
a double quote, then it will work like an escape char. */
size_t cSlashes = 1;
while (*pszSrc == '\\')
{
cSlashes++;
pszSrc++;
}
if (*pszSrc != '"')
/* Not an escape sequence. */
while (cSlashes-- > 0)
pszDst = RTStrPutCp(pszDst, '\\');
else
{
/* Escape sequence. Output half of the slashes. If odd
number, output the escaped double quote . */
while (cSlashes >= 2)
{
pszDst = RTStrPutCp(pszDst, '\\');
cSlashes -= 2;
}
if (cSlashes)
{
pszDst = RTStrPutCp(pszDst, '"');
pszSrc++;
}
}
}
}
}
*pszDst++ = '\0';
if (RT_FAILURE(rc) || !uc)
break;
}
if (RT_FAILURE(rc))
{
RTMemFree(pszDup);
RTMemFree(papszArgs);
return rc;
}
/*
* Terminate the array.
* Check for empty string to make sure we've got an array.
*/
if (iArg == 0)
{
RTMemFree(pszDup);
papszArgs = (char **)RTMemAlloc(1 * sizeof(char *));
if (!papszArgs)
return VERR_NO_MEMORY;
}
papszArgs[iArg] = NULL;
*pcArgs = iArg;
*ppapszArgv = papszArgs;
return VINF_SUCCESS;
}
RTDECL(void) RTGetOptArgvFree(char **papszArgv)
{
RTGetOptArgvFreeEx(papszArgv, 0);
}
RTDECL(void) RTGetOptArgvFreeEx(char **papszArgv, uint32_t fFlags)
{
Assert(~(fFlags & ~RTGETOPTARGV_CNV_VALID_MASK));
if (papszArgv)
{
/*
* We've really only _two_ allocations here. Check the code in
* RTGetOptArgvFromString for the particulars.
*/
if (!(fFlags & RTGETOPTARGV_CNV_MODIFY_INPUT))
RTMemFree(papszArgv[0]);
RTMemFree(papszArgv);
}
}
/**
* Checks if the argument needs quoting or not.
*
* @returns true if it needs, false if it don't.
* @param pszArg The argument.
* @param fFlags Quoting style.
* @param pcch Where to store the argument length when quoting
* is not required. (optimization)
*/
DECLINLINE(bool) rtGetOpArgvRequiresQuoting(const char *pszArg, uint32_t fFlags, size_t *pcch)
{
if ((fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) != RTGETOPTARGV_CNV_UNQUOTED)
{
char const *psz = pszArg;
unsigned char ch;
while ((ch = (unsigned char)*psz))
{
if ( ch < 128
&& ASMBitTest(&g_abmQuoteChars[fFlags & RTGETOPTARGV_CNV_QUOTE_MASK], ch))
return true;
psz++;
}
*pcch = psz - pszArg;
}
else
*pcch = strlen(pszArg);
return false;
}
/**
* Grows the command line string buffer.
*
* @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
* @param ppszCmdLine Pointer to the command line string pointer.
* @param pcbCmdLineAlloc Pointer to the allocation length variable.
* @param cchMin The minimum size to grow with, kind of.
*/
static int rtGetOptArgvToStringGrow(char **ppszCmdLine, size_t *pcbCmdLineAlloc, size_t cchMin)
{
size_t cb = *pcbCmdLineAlloc;
while (cb < cchMin)
cb *= 2;
cb *= 2;
*pcbCmdLineAlloc = cb;
return RTStrRealloc(ppszCmdLine, cb);
}
/**
* Checks if we have a sequence of DOS slashes followed by a double quote char.
*
* @returns true / false accordingly.
* @param psz The string.
*/
DECLINLINE(bool) rtGetOptArgvMsCrtIsSlashQuote(const char *psz)
{
while (*psz == '\\')
psz++;
return *psz == '"' || *psz == '\0';
}
RTDECL(int) RTGetOptArgvToString(char **ppszCmdLine, const char * const *papszArgv, uint32_t fFlags)
{
AssertReturn((fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) <= RTGETOPTARGV_CNV_UNQUOTED, VERR_INVALID_FLAGS);
AssertReturn(!(fFlags & (~RTGETOPTARGV_CNV_VALID_MASK | RTGETOPTARGV_CNV_MODIFY_INPUT)), VERR_INVALID_FLAGS);
#define PUT_CH(ch) \
if (RT_UNLIKELY(off + 1 >= cbCmdLineAlloc)) { \
rc = rtGetOptArgvToStringGrow(&pszCmdLine, &cbCmdLineAlloc, 1); \
if (RT_FAILURE(rc)) \
break; \
} \
pszCmdLine[off++] = (ch)
#define PUT_PSZ(psz, cch) \
if (RT_UNLIKELY(off + (cch) >= cbCmdLineAlloc)) { \
rc = rtGetOptArgvToStringGrow(&pszCmdLine, &cbCmdLineAlloc, (cch)); \
if (RT_FAILURE(rc)) \
break; \
} \
memcpy(&pszCmdLine[off], (psz), (cch)); \
off += (cch);
#define PUT_SZ(sz) PUT_PSZ(sz, sizeof(sz) - 1)
/*
* Take the realloc approach, it requires less code and is probably more
* efficient than figuring out the size first.
*/
int rc = VINF_SUCCESS;
size_t off = 0;
size_t cbCmdLineAlloc = 256;
char *pszCmdLine = RTStrAlloc(256);
if (!pszCmdLine)
return VERR_NO_STR_MEMORY;
for (size_t i = 0; papszArgv[i]; i++)
{
if (i > 0)
{
PUT_CH(' ');
}
/* does it need quoting? */
const char *pszArg = papszArgv[i];
size_t cchArg;
if (!rtGetOpArgvRequiresQuoting(pszArg, fFlags, &cchArg))
{
/* No quoting needed, just append the argument. */
PUT_PSZ(pszArg, cchArg);
}
else if ((fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) == RTGETOPTARGV_CNV_QUOTE_MS_CRT)
{
/*
* Microsoft CRT quoting. Quote the whole argument in double
* quotes to make it easier to read and code.
*/
PUT_CH('"');
char ch;
while ((ch = *pszArg++))
{
if ( ch == '\\'
&& rtGetOptArgvMsCrtIsSlashQuote(pszArg))
{
PUT_SZ("\\\\");
}
else if (ch == '"')
{
PUT_SZ("\\\"");
}
else
{
PUT_CH(ch);
}
}
PUT_CH('"');
}
else
{
/*
* Bourne Shell quoting. Quote the whole thing in single quotes
* and use double quotes for any single quote chars.
*/
PUT_CH('\'');
char ch;
while ((ch = *pszArg++))
{
if (ch == '\'')
{
PUT_SZ("'\"'\"'");
}
else
{
PUT_CH(ch);
}
}
PUT_CH('\'');
}
}
/* Set return value / cleanup. */
if (RT_SUCCESS(rc))
{
pszCmdLine[off] = '\0';
*ppszCmdLine = pszCmdLine;
}
else
RTStrFree(pszCmdLine);
#undef PUT_SZ
#undef PUT_PSZ
#undef PUT_CH
return rc;
}
RTDECL(int) RTGetOptArgvToUtf16String(PRTUTF16 *ppwszCmdLine, const char * const *papszArgv, uint32_t fFlags)
{
char *pszCmdLine;
int rc = RTGetOptArgvToString(&pszCmdLine, papszArgv, fFlags);
if (RT_SUCCESS(rc))
{
rc = RTStrToUtf16(pszCmdLine, ppwszCmdLine);
RTStrFree(pszCmdLine);
}
return rc;
}
#endif /* !IPRT_REGENERATE_QUOTE_CHARS */
|