1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
|
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
|
-
+
|
Tcl_DString nameBuf, ds;
const TCHAR *nativeName;
TCHAR nativeFullPath[MAX_PATH];
static const char extensions[][5] = {"", ".com", ".exe", ".bat", ".cmd"};
/*
* Look for the program as an external program. First try the name as it
* is, then try adding .com, .exe, and .bat, in that order, to the name,
* is, then try adding .com, .exe, .bat and .cmd, in that order, to the name,
* looking for an executable.
*
* Using the raw SearchPath() function doesn't do quite what is necessary.
* If the name of the executable already contains a '.' character, it will
* not try appending the specified extension when searching (in other
* words, SearchPath will not find the program "a.b.exe" if the arguments
* specified "a.b" and ".exe"). So, first look for the file as it is
|
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
|
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
+
+
+
+
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static const char *
BuildCmdLineBypassBS(
const char *current,
const char **bspos
) {
/* mark first backslash possition */
if (!*bspos) {
*bspos = current;
}
do {
current++;
} while (*current == '\\');
return current;
}
static void
QuoteCmdLineBackslash(
Tcl_DString *dsPtr,
const char *start,
const char *current,
const char *bspos
) {
if (!bspos) {
if (current > start) { /* part before current (special) */
Tcl_DStringAppend(dsPtr, start, (int) (current - start));
}
} else {
if (bspos > start) { /* part before first backslash */
Tcl_DStringAppend(dsPtr, start, (int) (bspos - start));
}
while (bspos++ < current) { /* each backslash twice */
TclDStringAppendLiteral(dsPtr, "\\\\");
}
}
}
static const char *
QuoteCmdLinePart(
Tcl_DString *dsPtr,
const char *start,
const char *special,
const char *specMetaChars,
const char **bspos
) {
if (!*bspos) {
/* rest before special (before quote) */
QuoteCmdLineBackslash(dsPtr, start, special, NULL);
start = special;
} else {
/* rest before first backslash and backslashes into new quoted block */
QuoteCmdLineBackslash(dsPtr, start, *bspos, NULL);
start = *bspos;
}
/*
* escape all special chars enclosed in quotes like `"..."`, note that here we
* don't must escape `\` (with `\`), because it's outside of the main quotes,
* so `\` remains `\`, but important - not at end of part, because results as
* before the quote, so `%\%\` should be escaped as `"%\%"\\`).
*/
TclDStringAppendLiteral(dsPtr, "\""); /* opening escape quote-char */
do {
*bspos = NULL;
special++;
if (*special == '\\') {
/* bypass backslashes (and mark first backslash possition)*/
special = BuildCmdLineBypassBS(special, bspos);
if (*special == '\0') break;
}
} while (*special && strchr(specMetaChars, *special));
if (!*bspos) {
/* unescaped rest before quote */
QuoteCmdLineBackslash(dsPtr, start, special, NULL);
} else {
/* unescaped rest before first backslash (rather belongs to the main block) */
QuoteCmdLineBackslash(dsPtr, start, *bspos, NULL);
}
TclDStringAppendLiteral(dsPtr, "\""); /* closing escape quote-char */
return special;
}
static void
BuildCommandLine(
const char *executable, /* Full path of executable (including
* extension). Replacement for argv[0]. */
int argc, /* Number of arguments. */
const char **argv, /* Argument strings in UTF. */
Tcl_DString *linePtr) /* Initialized Tcl_DString that receives the
* command line (TCHAR). */
{
const char *arg, *start, *special;
int quote, i;
const char *arg, *start, *special, *bspos;
int quote = 0, i;
Tcl_DString ds;
/* characters to enclose in quotes if unpaired quote flag set */
const static char *specMetaChars = "&|^<>!()%";
/* characters to enclose in quotes in any case (regardless unpaired-flag) */
const static char *specMetaChars2 = "%";
/* Quote flags:
* CL_ESCAPE - escape argument;
* CL_QUOTE - enclose in quotes;
* CL_UNPAIRED - previous arguments chain contains unpaired quote-char;
*/
enum {CL_ESCAPE = 1, CL_QUOTE = 2, CL_UNPAIRED = 4};
Tcl_DStringInit(&ds);
/*
* Prime the path. Add a space separator if we were primed with something.
*/
TclDStringAppendDString(&ds, linePtr);
if (Tcl_DStringLength(linePtr) > 0) {
TclDStringAppendLiteral(&ds, " ");
}
for (i = 0; i < argc; i++) {
if (i == 0) {
arg = executable;
} else {
arg = argv[i];
TclDStringAppendLiteral(&ds, " ");
}
quote = 0;
quote &= ~(CL_ESCAPE|CL_QUOTE); /* reset escape flags */
bspos = NULL;
if (arg[0] == '\0') {
quote = 1;
quote = CL_QUOTE;
} else {
int count;
Tcl_UniChar ch = 0;
for (start = arg; *start != '\0'; start += count) {
count = TclUtfToUniChar(start, &ch);
if (Tcl_UniCharIsSpace(ch)) { /* INTL: ISO space. */
quote = 1;
break;
}
}
}
if (quote) {
Tcl_UniChar ch;
for (start = arg;
*start != '\0' &&
(quote & (CL_ESCAPE|CL_QUOTE)) != (CL_ESCAPE|CL_QUOTE);
start += count
) {
count = Tcl_UtfToUniChar(start, &ch);
if (count > 1) continue;
if (Tcl_UniCharIsSpace(ch)) {
quote |= CL_QUOTE; /* quote only */
if (bspos) { /* if backslash found - escape & quote */
quote |= CL_ESCAPE;
break;
}
continue;
}
if (strchr(specMetaChars, *start)) {
quote |= (CL_ESCAPE|CL_QUOTE); /*escape & quote */
break;
}
if (*start == '"') {
quote |= CL_ESCAPE; /* escape only */
continue;
}
if (*start == '\\') {
bspos = start;
if (quote & CL_QUOTE) { /* if quote - escape & quote */
quote |= CL_ESCAPE;
break;
}
continue;
}
}
bspos = NULL;
}
if (quote & CL_QUOTE) {
/* start of argument (main opening quote-char) */
TclDStringAppendLiteral(&ds, "\"");
}
start = arg;
for (special = arg; ; ) {
if ((*special == '\\') && (special[1] == '\\' ||
special[1] == '"' || (quote && special[1] == '\0'))) {
Tcl_DStringAppend(&ds, start, (int) (special - start));
start = special;
while (1) {
special++;
if (*special == '"' || (quote && *special == '\0')) {
/*
* N backslashes followed a quote -> insert N * 2 + 1
* backslashes then a quote.
if (!(quote & CL_ESCAPE)) {
*/
Tcl_DStringAppend(&ds, start,
(int) (special - start));
/* nothing to escape */
Tcl_DStringAppend(&ds, arg, -1);
} else {
start = arg;
for (special = arg; *special != '\0'; ) {
break;
}
if (*special != '\\') {
/* position of `\` is important before quote or at end (equal `\"` because quoted) */
if (*special == '\\') {
break;
}
}
Tcl_DStringAppend(&ds, start, (int) (special - start));
start = special;
}
if (*special == '"') {
Tcl_DStringAppend(&ds, start, (int) (special - start));
TclDStringAppendLiteral(&ds, "\\\"");
start = special + 1;
}
if (*special == '\0') {
break;
}
special++;
}
Tcl_DStringAppend(&ds, start, (int) (special - start));
if (quote) {
/* bypass backslashes (and mark first backslash possition)*/
special = BuildCmdLineBypassBS(special, &bspos);
if (*special == '\0') break;
}
/* ["] */
if (*special == '"') {
quote ^= CL_UNPAIRED; /* invert unpaired flag - observe unpaired quotes */
/* add part before (and escape backslashes before quote) */
QuoteCmdLineBackslash(&ds, start, special, bspos);
bspos = NULL;
/* escape using backslash */
TclDStringAppendLiteral(&ds, "\\\"");
start = ++special;
continue;
}
/* unpaired (escaped) quote causes special handling on meta-chars */
if ((quote & CL_UNPAIRED) && strchr(specMetaChars, *special)) {
special = QuoteCmdLinePart(&ds, start, special, specMetaChars, &bspos);
/* start to current or first backslash */
start = !bspos ? special : bspos;
continue;
}
/* special case for % - should be enclosed always (paired also) */
if (strchr(specMetaChars2, *special)) {
special = QuoteCmdLinePart(&ds, start, special, specMetaChars2, &bspos);
/* start to current or first backslash */
start = !bspos ? special : bspos;
continue;
}
/* other not special (and not meta) character */
bspos = NULL; /* reset last backslash possition (not interesting) */
special++;
}
/* rest of argument (and escape backslashes before closing main quote) */
QuoteCmdLineBackslash(&ds, start, special,
(quote & CL_QUOTE) ? bspos : NULL);
}
if (quote & CL_QUOTE) {
/* end of argument (main closing quote-char) */
TclDStringAppendLiteral(&ds, "\"");
}
}
Tcl_DStringFree(linePtr);
Tcl_WinUtfToTChar(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds), linePtr);
Tcl_DStringFree(&ds);
}
|