Diff
Not logged in

Differences From Artifact [16aa21e37d]:

To Artifact [f7f7c8fd48]:


1536
1537
1538
1539
1540
1541
1542


1543
1544
1545
1546
1547
1548
1549
    Tcl_DString *linePtr)	/* Initialized Tcl_DString that receives the
				 * command line (TCHAR). */
{
    const char *arg, *start, *special;
    int quote, i;
    Tcl_DString ds;



    Tcl_DStringInit(&ds);

    /*
     * Prime the path. Add a space separator if we were primed with something.
     */

    Tcl_DStringAppend(&ds, Tcl_DStringValue(linePtr), -1);







>
>







1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
    Tcl_DString *linePtr)	/* Initialized Tcl_DString that receives the
				 * command line (TCHAR). */
{
    const char *arg, *start, *special;
    int quote, i;
    Tcl_DString ds;

    const static char *specMetaChars = "&|^<>!%()";

    Tcl_DStringInit(&ds);

    /*
     * Prime the path. Add a space separator if we were primed with something.
     */

    Tcl_DStringAppend(&ds, Tcl_DStringValue(linePtr), -1);
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
	if (arg[0] == '\0') {
	    quote = 1;
	} else {
	    int count;
	    Tcl_UniChar ch;
	    for (start = arg; *start != '\0'; start += count) {
		count = Tcl_UtfToUniChar(start, &ch);
		if (Tcl_UniCharIsSpace(ch)) {	/* INTL: ISO space. */



		    quote = 1;
		    break;
		}
	    }
	}
	if (quote) {
	    Tcl_DStringAppend(&ds, "\"", 1);
	}

	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.
			 */



			Tcl_DStringAppend(&ds, start,
				(int) (special - start));
			break;
		    }


		    if (*special != '\\') {
			break;

		    }
		}

		Tcl_DStringAppend(&ds, start, (int) (special - start));
		start = special;
	    }

	    if (*special == '"') {

		Tcl_DStringAppend(&ds, start, (int) (special - start));
		Tcl_DStringAppend(&ds, "\\\"", 2);
		start = special + 1;
	    }
	    if (*special == '\0') {
		break;
	    }
	    special++;
	}

	Tcl_DStringAppend(&ds, start, (int) (special - start));
	if (quote) {

	    Tcl_DStringAppend(&ds, "\"", 1);
	}
    }
    Tcl_DStringFree(linePtr);
    Tcl_WinUtfToTChar(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds), linePtr);
    Tcl_DStringFree(&ds);
}







|
>
>
>





|
|
|
>
|
|
|
|
|
>
>
|
<
|
>
|
<
<
|
<
>
>
|
|
|
|
|
>
>
|
<
>

<
>
|
|
<
>
|
>
|
|
|
<
<
|
|
|
|
>
|
<
>







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
	if (arg[0] == '\0') {
	    quote = 1;
	} else {
	    int count;
	    Tcl_UniChar ch;
	    for (start = arg; *start != '\0'; start += count) {
		count = Tcl_UtfToUniChar(start, &ch);
		if (Tcl_UniCharIsSpace(ch) ||
		   (count == 1 && (*start=='"' || strchr(specMetaChars, *start)))
		) {
		    /* must be quoted */
		    quote = 1;
		    break;
		}
	    }
	}
	if (!quote) {
	    Tcl_DStringAppend(&ds, arg, -1);
	} else {
	    Tcl_DStringAppend(&ds, "\"", 1);
	    start = arg;
	    for (special = arg; *special != '\0'; ) {
		if (*special == '\\' && (special[1] == '\\' || special[1] == '"' || special[1] == '\0')) {
		    if (special > start) {
			Tcl_DStringAppend(&ds, start, (int) (special - start));
		    }
		    Tcl_DStringAppend(&ds, "\\\\", 2);
		    start = ++special;

		    continue;
		}
		if (*special == '"') {


		    quote ^= 2; /* observe unpaired quotes */

		    if (special > start) {
			Tcl_DStringAppend(&ds, start, (int) (special - start));
		    }
		    Tcl_DStringAppend(&ds, "\\\"", 2);
		    start = ++special;
		    continue;
		}
		/* unpaired (escaped) quote causes special handling on meta-chars */
		if ((quote & 2) && strchr(specMetaChars, *special)) {
		    if (special > start) {

			Tcl_DStringAppend(&ds, start, (int) (special - start));
		    }

		    /* unpaired - escape all special chars inside quotes "..." */
		    Tcl_DStringAppend(&ds, "\"", 1);
		    start = special;

		    do {
			special++;
		    } while(*special && strchr(specMetaChars, *special));
		    Tcl_DStringAppend(&ds, start, (int) (special - start));
		    Tcl_DStringAppend(&ds, "\"", 1);
		    start = special;


		    continue;
		}
		special++;
	    }
	    if (special > start) {
		Tcl_DStringAppend(&ds, start, (int) (special - start));

	    }
	    Tcl_DStringAppend(&ds, "\"", 1);
	}
    }
    Tcl_DStringFree(linePtr);
    Tcl_WinUtfToTChar(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds), linePtr);
    Tcl_DStringFree(&ds);
}