@@ -25,7 +25,6 @@ import (
2525 "os/exec"
2626 "path/filepath"
2727 "reflect"
28- "regexp"
2928 "runtime"
3029 "strconv"
3130 "strings"
@@ -1750,9 +1749,29 @@ func System(command string, returnVar *int) string {
17501749 var stdBuf bytes.Buffer
17511750 var err , err1 , err2 , err3 error
17521751
1753- // split command recommendations refer to Exec().
1754- r , _ := regexp .Compile (`[ ]+` )
1755- parts := r .Split (command , - 1 )
1752+ // split command
1753+ q := rune (0 )
1754+ parts := strings .FieldsFunc (command , func (r rune ) bool {
1755+ switch {
1756+ case r == q :
1757+ q = rune (0 )
1758+ return false
1759+ case q != rune (0 ):
1760+ return false
1761+ case unicode .In (r , unicode .Quotation_Mark ):
1762+ q = r
1763+ return false
1764+ default :
1765+ return unicode .IsSpace (r )
1766+ }
1767+ })
1768+ // remove the " and ' on both sides
1769+ for i , v := range parts {
1770+ f , l := v [0 ], len (v )
1771+ if l >= 2 && (f == '"' || f == '\'' ) {
1772+ parts [i ] = v [1 : l - 1 ]
1773+ }
1774+ }
17561775 cmd := exec .Command (parts [0 ], parts [1 :]... )
17571776 stdoutIn , _ := cmd .StdoutPipe ()
17581777 stderrIn , _ := cmd .StderrPipe ()
@@ -1799,9 +1818,28 @@ func System(command string, returnVar *int) string {
17991818// Passthru passthru()
18001819// returnVar, 0: succ; 1: fail
18011820func Passthru (command string , returnVar * int ) {
1802- // split command recommendations refer to Exec().
1803- r , _ := regexp .Compile (`[ ]+` )
1804- parts := r .Split (command , - 1 )
1821+ q := rune (0 )
1822+ parts := strings .FieldsFunc (command , func (r rune ) bool {
1823+ switch {
1824+ case r == q :
1825+ q = rune (0 )
1826+ return false
1827+ case q != rune (0 ):
1828+ return false
1829+ case unicode .In (r , unicode .Quotation_Mark ):
1830+ q = r
1831+ return false
1832+ default :
1833+ return unicode .IsSpace (r )
1834+ }
1835+ })
1836+ // remove the " and ' on both sides
1837+ for i , v := range parts {
1838+ f , l := v [0 ], len (v )
1839+ if l >= 2 && (f == '"' || f == '\'' ) {
1840+ parts [i ] = v [1 : l - 1 ]
1841+ }
1842+ }
18051843 cmd := exec .Command (parts [0 ], parts [1 :]... )
18061844 cmd .Stdout = os .Stdout
18071845 cmd .Stderr = os .Stderr
0 commit comments