Skip to content

Commit 716bfae

Browse files
committed
Quote rc commands if necessary
Surround an argument with quotes before assembling it into a command base on whether a command requires quoting to be parsed by rc as it was received through args. If the arg contains any of rc's special characters or whitespace as documented in rc(1), it is quoted and any ' characters are escaped to ''. The quoted command string is also appended to the dump command. Fixes #135
1 parent 96bdba9 commit 716bfae

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

acme/Watch/main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import (
4747
"9fans.net/go/acme"
4848
)
4949

50+
const rcspecial = "#;&|^$=`'{}()<> \t\n"
51+
5052
var args []string
5153
var win *acme.Win
5254
var needrun = make(chan bool, 1)
@@ -64,6 +66,8 @@ func main() {
6466
flag.Parse()
6567
args = flag.Args()
6668

69+
escaped := rcquote(args)
70+
6771
var err error
6872
win, err = acme.New()
6973
if err != nil {
@@ -80,7 +84,7 @@ func main() {
8084
}
8185
win.Ctl(cmd)
8286
win.Fprintf("tag", "Get Kill Quit ")
83-
win.Fprintf("body", "%% %s\n", strings.Join(args, " "))
87+
win.Fprintf("body", "%% %s\n", escaped)
8488

8589
needrun <- true
8690
go events()
@@ -329,3 +333,18 @@ func runBackground(id int, dir string) {
329333
// Continue loop with lock held
330334
}
331335
}
336+
337+
func rcquote(args []string) string {
338+
var b strings.Builder
339+
for i, arg := range args {
340+
if i != 0 {
341+
b.WriteRune(' ')
342+
}
343+
if strings.ContainsAny(arg, rcspecial) {
344+
fmt.Fprintf(&b, "'%s'", strings.ReplaceAll(arg, "'", "''"))
345+
} else {
346+
b.WriteString(arg)
347+
}
348+
}
349+
return b.String()
350+
}

0 commit comments

Comments
 (0)