-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathcommands.go
More file actions
725 lines (676 loc) · 19.7 KB
/
commands.go
File metadata and controls
725 lines (676 loc) · 19.7 KB
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package sqlcmd
import (
"flag"
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
"github.com/microsoft/go-sqlcmd/internal/color"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
// Command defines a sqlcmd action which can be intermixed with the SQL batch
// Commands for sqlcmd are defined at https://docs.microsoft.com/sql/tools/sqlcmd-utility#sqlcmd-commands
type Command struct {
// regex must include at least one group if it has parameters
// Will be matched using FindStringSubmatch
regex *regexp.Regexp
// The function that implements the command. Third parameter is the line number
action func(*Sqlcmd, []string, uint) error
// Name of the command
name string
// whether the command is a system command
isSystem bool
}
// Commands is the set of sqlcmd command implementations
type Commands map[string]*Command
func newCommands() Commands {
// Commands is the set of Command implementations
return map[string]*Command{
"EXIT": {
regex: regexp.MustCompile(`(?im)^[\t ]*?:?EXIT([\( \t]+.*\)*$|$)`),
action: exitCommand,
name: "EXIT",
},
"QUIT": {
regex: regexp.MustCompile(`(?im)^[\t ]*?:?QUIT(?:[ \t]+(.*$)|$)`),
action: quitCommand,
name: "QUIT",
},
"GO": {
regex: regexp.MustCompile(batchTerminatorRegex("GO")),
action: goCommand,
name: "GO",
},
"OUT": {
regex: regexp.MustCompile(`(?im)^[ \t]*:OUT(?:[ \t]+(.*$)|$)`),
action: outCommand,
name: "OUT",
},
"ERROR": {
regex: regexp.MustCompile(`(?im)^[ \t]*:ERROR(?:[ \t]+(.*$)|$)`),
action: errorCommand,
name: "ERROR",
}, "READFILE": {
regex: regexp.MustCompile(`(?im)^[ \t]*:R(?:[ \t]+(.*$)|$)`),
action: readFileCommand,
name: "READFILE",
},
"SETVAR": {
regex: regexp.MustCompile(`(?im)^[ \t]*:SETVAR(?:[ \t]+(.*$)|$)`),
action: setVarCommand,
name: "SETVAR",
},
"LISTVAR": {
regex: regexp.MustCompile(`(?im)^[\t ]*?:LISTVAR(?:[ \t]+(.*$)|$)`),
action: listVarCommand,
name: "LISTVAR",
},
"RESET": {
regex: regexp.MustCompile(`(?im)^[ \t]*?:?RESET(?:[ \t]+(.*$)|$)`),
action: resetCommand,
name: "RESET",
},
"LIST": {
regex: regexp.MustCompile(`(?im)^[ \t]*:LIST(?:[ \t]+(.*$)|$)`),
action: listCommand,
name: "LIST",
},
"CONNECT": {
regex: regexp.MustCompile(`(?im)^[ \t]*:CONNECT(?:[ \t]+(.*$)|$)`),
action: connectCommand,
name: "CONNECT",
},
"EXEC": {
regex: regexp.MustCompile(`(?im)^[ \t]*?:?!!(.*$)`),
action: execCommand,
name: "EXEC",
isSystem: true,
},
"EDIT": {
regex: regexp.MustCompile(`(?im)^[\t ]*?:?ED(?:[ \t]+(.*$)|$)`),
action: editCommand,
name: "EDIT",
isSystem: true,
},
"ONERROR": {
regex: regexp.MustCompile(`(?im)^[\t ]*?:?ON ERROR(?:[ \t]+(.*$)|$)`),
action: onerrorCommand,
name: "ONERROR",
},
"XML": {
regex: regexp.MustCompile(`(?im)^[\t ]*?:XML(?:[ \t]+(.*$)|$)`),
action: xmlCommand,
name: "XML",
},
"HELP": {
regex: regexp.MustCompile(`(?im)^[ \t]*:HELP(?:[ \t]+(.*$)|$)`),
action: helpCommand,
name: "HELP",
},
"PERFTRACE": {
regex: regexp.MustCompile(`(?im)^[ \t]*:PERFTRACE(?:[ \t]+(.*$)|$)`),
action: perftraceCommand,
name: "PERFTRACE",
},
}
}
// DisableSysCommands disables the ED and :!! commands.
// When exitOnCall is true, running those commands will exit the process.
func (c Commands) DisableSysCommands(exitOnCall bool) {
f := warnDisabled
if exitOnCall {
f = errorDisabled
}
for _, cmd := range c {
if cmd.isSystem {
cmd.action = f
}
}
}
func (c Commands) matchCommand(line string) (*Command, []string) {
for _, cmd := range c {
matchedCommand := cmd.regex.FindStringSubmatch(line)
if matchedCommand != nil {
return cmd, removeComments(matchedCommand[1:])
}
}
return nil, nil
}
func removeComments(args []string) []string {
var pos int
quote := false
for i := range args {
pos, quote = commentStart([]rune(args[i]), quote)
if pos > -1 {
out := make([]string, i+1)
if i > 0 {
copy(out, args[:i])
}
out[i] = args[i][:pos]
return out
}
}
return args
}
func commentStart(arg []rune, quote bool) (int, bool) {
var i int
space := true
for ; i < len(arg); i++ {
c, next := arg[i], grab(arg, i+1, len(arg))
switch {
case quote && c == '"' && next != '"':
quote = false
case quote && c == '"' && next == '"':
i++
case c == '\t' || c == ' ':
space = true
// Note we assume none of the regexes would split arguments on non-whitespace boundaries such that "text -- comment" would get split into "text -" and "- comment"
case !quote && space && c == '-' && next == '-':
return i, false
case !quote && c == '"':
quote = true
default:
space = false
}
}
return -1, quote
}
func warnDisabled(s *Sqlcmd, args []string, line uint) error {
s.WriteError(s.GetError(), ErrCommandsDisabled)
return nil
}
func errorDisabled(s *Sqlcmd, args []string, line uint) error {
s.WriteError(s.GetError(), ErrCommandsDisabled)
s.Exitcode = 1
return ErrExitRequested
}
func batchTerminatorRegex(terminator string) string {
return fmt.Sprintf(`(?im)^[\t ]*?%s(?:[ ]+(.*$)|$)`, regexp.QuoteMeta(terminator))
}
// SetBatchTerminator attempts to set the batch terminator to the given value
// Returns an error if the new value is not usable in the regex
func (c Commands) SetBatchTerminator(terminator string) error {
cmd := c["GO"]
regex, err := regexp.Compile(batchTerminatorRegex(terminator))
if err != nil {
return err
}
cmd.regex = regex
return nil
}
// exitCommand has 3 modes.
// With no (), it just exits without running any query
// With () it runs whatever batch is in the buffer then exits
// With any text between () it runs the text as a query then exits
func exitCommand(s *Sqlcmd, args []string, line uint) error {
if len(args) == 0 {
return ErrExitRequested
}
params := strings.TrimSpace(args[0])
if params == "" {
return ErrExitRequested
}
if !strings.HasPrefix(params, "(") || !strings.HasSuffix(params, ")") {
return InvalidCommandError("EXIT", line)
}
// First we save the current batch
query1 := s.batch.String()
if len(query1) > 0 {
query1 = s.getRunnableQuery(query1)
}
// Now parse the params of EXIT as a batch without commands
cmd := s.batch.cmd
s.batch.cmd = nil
defer func() {
s.batch.cmd = cmd
}()
query2 := strings.TrimSpace(params[1 : len(params)-1])
if len(query2) > 0 {
s.batch.Reset([]rune(query2))
_, _, err := s.batch.Next()
if err != nil {
return err
}
query2 = s.batch.String()
if len(query2) > 0 {
query2 = s.getRunnableQuery(query2)
}
}
if len(query1) > 0 || len(query2) > 0 {
query := query1 + SqlcmdEol + query2
s.Exitcode, _ = s.runQuery(query)
}
return ErrExitRequested
}
// quitCommand immediately exits the program without running any more batches
func quitCommand(s *Sqlcmd, args []string, line uint) error {
if args != nil && strings.TrimSpace(args[0]) != "" {
return InvalidCommandError("QUIT", line)
}
return ErrExitRequested
}
// goCommand runs the current batch the number of times specified
func goCommand(s *Sqlcmd, args []string, line uint) error {
// default to 1 execution
n := 1
var err error
if len(args) > 0 {
cnt := strings.TrimSpace(args[0])
if cnt != "" {
if cnt, err = resolveArgumentVariables(s, []rune(cnt), true); err != nil {
return err
}
_, err = fmt.Sscanf(cnt, "%d", &n)
}
}
if err != nil || n < 1 {
return InvalidCommandError("GO", line)
}
if s.EchoInput {
err = listCommand(s, []string{}, line)
}
if err != nil {
return InvalidCommandError("GO", line)
}
query := s.batch.String()
if query == "" {
return nil
}
query = s.getRunnableQuery(query)
for i := 0; i < n; i++ {
if retcode, err := s.runQuery(query); err != nil {
s.Exitcode = retcode
return err
}
}
s.batch.Reset(nil)
return nil
}
// outCommand changes the output writer to use a file
func outCommand(s *Sqlcmd, args []string, line uint) error {
if len(args) == 0 || args[0] == "" {
return InvalidCommandError("OUT", line)
}
filePath, err := resolveArgumentVariables(s, []rune(args[0]), true)
if err != nil {
return err
}
switch {
case strings.EqualFold(filePath, "stdout"):
s.SetOutput(os.Stdout)
case strings.EqualFold(filePath, "stderr"):
s.SetOutput(os.Stderr)
default:
o, err := os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return InvalidFileError(err, args[0])
}
if s.UnicodeOutputFile {
// ODBC sqlcmd doesn't write a BOM but we will.
// Maybe the endian-ness should be configurable.
win16le := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM)
encoder := transform.NewWriter(o, win16le.NewEncoder())
s.SetOutput(encoder)
} else {
s.SetOutput(o)
}
}
return nil
}
// errorCommand changes the error writer to use a file
func errorCommand(s *Sqlcmd, args []string, line uint) error {
if len(args) == 0 || args[0] == "" {
return InvalidCommandError("ERROR", line)
}
filePath, err := resolveArgumentVariables(s, []rune(args[0]), true)
if err != nil {
return err
}
switch {
case strings.EqualFold(filePath, "stderr"):
s.SetError(os.Stderr)
case strings.EqualFold(filePath, "stdout"):
s.SetError(os.Stdout)
default:
o, err := os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return InvalidFileError(err, args[0])
}
s.SetError(o)
}
return nil
}
func readFileCommand(s *Sqlcmd, args []string, line uint) error {
if args == nil || len(args) != 1 {
return InvalidCommandError(":R", line)
}
fileName, _ := resolveArgumentVariables(s, []rune(args[0]), false)
return s.IncludeFile(fileName, false)
}
// setVarCommand parses a variable setting and applies it to the current Sqlcmd variables
func setVarCommand(s *Sqlcmd, args []string, line uint) error {
if args == nil || len(args) != 1 || args[0] == "" {
return InvalidCommandError(":SETVAR", line)
}
varname := args[0]
val := ""
// The prior incarnation of sqlcmd doesn't require a space between the variable name and its value
// in some very unexpected cases. This version will require the space.
sp := strings.IndexRune(args[0], ' ')
if sp > -1 {
val = strings.TrimSpace(varname[sp:])
varname = varname[:sp]
}
if err := s.vars.Setvar(varname, val); err != nil {
switch e := err.(type) {
case *VariableError:
return e
default:
return InvalidCommandError(":SETVAR", line)
}
}
return nil
}
// listVarCommand prints the set of Sqlcmd scripting variables.
// Builtin values are printed first, followed by user-set values in sorted order.
func listVarCommand(s *Sqlcmd, args []string, line uint) error {
if args != nil && strings.TrimSpace(args[0]) != "" {
return InvalidCommandError("LISTVAR", line)
}
vars := s.vars.All()
keys := make([]string, 0, len(vars))
for k := range vars {
if !contains(builtinVariables, k) {
keys = append(keys, k)
}
}
sort.Strings(keys)
keys = append(builtinVariables, keys...)
for _, k := range keys {
fmt.Fprintf(s.GetOutput(), `%s = "%s"%s`, k, vars[k], SqlcmdEol)
}
return nil
}
// resetCommand resets the statement cache
func resetCommand(s *Sqlcmd, args []string, line uint) error {
if s.batch != nil {
s.batch.Reset(nil)
}
return nil
}
// listCommand displays statements currently in the statement cache
func listCommand(s *Sqlcmd, args []string, line uint) (err error) {
cmd := ""
if args != nil {
if len(args) > 0 {
cmd = strings.ToLower(strings.TrimSpace(args[0]))
if len(args) > 1 || (cmd != "color" && cmd != "") {
return InvalidCommandError("LIST", line)
}
}
}
output := s.GetOutput()
if cmd == "color" {
sample := "select 'literal' as literal, 100 as number from [sys].[tables]"
clr := color.TextTypeTSql
if s.Format.IsXmlMode() {
sample = `<node att="attValue"/><node>value</node>`
clr = color.TextTypeXml
}
// ignoring errors since it's not critical output
for _, style := range s.colorizer.Styles() {
_, _ = output.Write([]byte(style + ": "))
_ = s.colorizer.Write(output, sample, style, clr)
_, _ = output.Write([]byte(SqlcmdEol))
}
return
}
if s.batch == nil || s.batch.String() == "" {
return
}
if err = s.colorizer.Write(output, s.batch.String(), s.vars.ColorScheme(), color.TextTypeTSql); err == nil {
_, err = output.Write([]byte(SqlcmdEol))
}
return
}
func connectCommand(s *Sqlcmd, args []string, line uint) error {
if len(args) == 0 {
return InvalidCommandError("CONNECT", line)
}
commandArgs := strings.Fields(args[0])
// Parse flags
flags := flag.NewFlagSet("connect", flag.ContinueOnError)
database := flags.String("D", "", "database name")
username := flags.String("U", "", "user name")
password := flags.String("P", "", "password")
loginTimeout := flags.String("l", "", "login timeout")
authenticationMethod := flags.String("G", "", "authentication method")
err := flags.Parse(commandArgs[1:])
//err := flags.Parse(args[1:])
if err != nil {
return InvalidCommandError("CONNECT", line)
}
connect := *s.Connect
connect.UserName, _ = resolveArgumentVariables(s, []rune(*username), false)
connect.Password, _ = resolveArgumentVariables(s, []rune(*password), false)
connect.Database, _ = resolveArgumentVariables(s, []rune(*database), false)
timeout, _ := resolveArgumentVariables(s, []rune(*loginTimeout), false)
if timeout != "" {
if timeoutSeconds, err := strconv.ParseInt(timeout, 10, 32); err == nil {
if timeoutSeconds < 0 {
return InvalidCommandError("CONNECT", line)
}
connect.LoginTimeoutSeconds = int(timeoutSeconds)
}
}
connect.AuthenticationMethod = *authenticationMethod
// Set server name as the first positional argument
if len(commandArgs) > 0 {
connect.ServerName, _ = resolveArgumentVariables(s, []rune(commandArgs[0]), false)
}
// If no user name is provided we switch to integrated auth
_ = s.ConnectDb(&connect, s.lineIo == nil)
// ConnectDb prints connection errors already, and failure to connect is not fatal even with -b option
return nil
}
func execCommand(s *Sqlcmd, args []string, line uint) error {
if len(args) == 0 {
return InvalidCommandError("EXEC", line)
}
cmdLine := strings.TrimSpace(args[0])
if cmdLine == "" {
return InvalidCommandError("EXEC", line)
}
if cmdLine, err := resolveArgumentVariables(s, []rune(cmdLine), true); err != nil {
return err
} else {
cmd := sysCommand(cmdLine)
cmd.Stderr = s.GetError()
cmd.Stdout = s.GetOutput()
_ = cmd.Run()
}
return nil
}
func editCommand(s *Sqlcmd, args []string, line uint) error {
if args != nil && strings.TrimSpace(args[0]) != "" {
return InvalidCommandError("ED", line)
}
file, err := os.CreateTemp("", "sq*.sql")
if err != nil {
return err
}
fileName := file.Name()
defer os.Remove(fileName)
text := s.batch.String()
if s.batch.State() == "-" {
text = fmt.Sprintf("%s%s", text, SqlcmdEol)
}
_, err = file.WriteString(text)
if err != nil {
return err
}
file.Close()
cmd := sysCommand(s.vars.TextEditor() + " " + `"` + fileName + `"`)
cmd.Stderr = s.GetError()
cmd.Stdout = s.GetOutput()
err = cmd.Run()
if err != nil {
return err
}
wasEcho := s.echoFileLines
s.echoFileLines = true
s.batch.Reset(nil)
_ = s.IncludeFile(fileName, false)
s.echoFileLines = wasEcho
return nil
}
func onerrorCommand(s *Sqlcmd, args []string, line uint) error {
if len(args) == 0 || args[0] == "" {
return InvalidCommandError("ON ERROR", line)
}
params := strings.TrimSpace(args[0])
if strings.EqualFold(strings.ToLower(params), "exit") {
s.Connect.ExitOnError = true
} else if strings.EqualFold(strings.ToLower(params), "ignore") {
s.Connect.IgnoreError = true
s.Connect.ExitOnError = false
} else {
return InvalidCommandError("ON ERROR", line)
}
return nil
}
func xmlCommand(s *Sqlcmd, args []string, line uint) error {
if len(args) != 1 || args[0] == "" {
return InvalidCommandError("XML", line)
}
params := strings.TrimSpace(args[0])
// "OFF" and "ON" are documented as the allowed values.
// ODBC sqlcmd treats any value other than "ON" the same as "OFF".
// So we will too.
if strings.EqualFold(params, "on") {
s.Format.XmlMode(true)
} else {
s.Format.XmlMode(false)
}
return nil
}
// helpCommand displays the list of available sqlcmd commands
func helpCommand(s *Sqlcmd, args []string, line uint) error {
helpText := `:!! [<command>]
- Executes a command in the operating system shell.
:connect server[\instance] [-l timeout] [-U user [-P password]]
- Connects to a SQL Server instance.
:ed
- Edits the current or last executed statement cache.
:error <dest>
- Redirects error output to a file, stderr, or stdout.
:exit
- Quits sqlcmd immediately.
:exit()
- Execute statement cache; quit with no return value.
:exit(<query>)
- Execute the specified query; returns numeric result.
go [<n>]
- Executes the statement cache (n times).
:help
- Shows this list of commands.
:list
- Prints the content of the statement cache.
:listvar
- Lists the set sqlcmd scripting variables.
:on error [exit|ignore]
- Action for batch or sqlcmd command errors.
:out <filename>|stderr|stdout
- Redirects query output to a file, stderr, or stdout.
:perftrace <filename>|stderr|stdout
- Redirects timing output to a file, stderr, or stdout.
:quit
- Quits sqlcmd immediately.
:r <filename>
- Append file contents to the statement cache.
:reset
- Discards the statement cache.
:setvar {variable}
- Removes a sqlcmd scripting variable.
:setvar <variable> <value>
- Sets a sqlcmd scripting variable.
:xml [on|off]
- Sets XML output mode.
`
_, err := s.GetOutput().Write([]byte(helpText))
return err
}
// perftraceCommand changes the performance statistics writer to use a file
func perftraceCommand(s *Sqlcmd, args []string, line uint) error {
if len(args) == 0 || args[0] == "" {
return InvalidCommandError("PERFTRACE", line)
}
filePath, err := resolveArgumentVariables(s, []rune(args[0]), true)
if err != nil {
return err
}
switch {
case strings.EqualFold(filePath, "stderr"):
s.SetStat(os.Stderr)
case strings.EqualFold(filePath, "stdout"):
s.SetStat(os.Stdout)
default:
o, err := os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return InvalidFileError(err, args[0])
}
s.SetStat(o)
}
return nil
}
func resolveArgumentVariables(s *Sqlcmd, arg []rune, failOnUnresolved bool) (string, error) {
var b *strings.Builder
end := len(arg)
for i := 0; i < end && !s.Connect.DisableVariableSubstitution; {
c, next := arg[i], grab(arg, i+1, end)
switch {
case c == '$' && next == '(':
vl, ok := readVariableReference(arg, i+2, end)
if ok {
varName := string(arg[i+2 : vl])
val, ok := s.resolveVariable(varName)
if ok {
if b == nil {
b = new(strings.Builder)
b.Grow(len(arg))
b.WriteString(string(arg[0:i]))
}
b.WriteString(val)
} else {
if failOnUnresolved {
return "", UndefinedVariable(varName)
}
s.WriteError(s.GetError(), UndefinedVariable(varName))
if b != nil {
b.WriteString(string(arg[i : vl+1]))
}
}
i += ((vl - i) + 1)
} else {
if b != nil {
b.WriteString("$(")
}
i += 2
}
default:
if b != nil {
b.WriteRune(c)
}
i++
}
}
if b == nil {
return string(arg), nil
}
return b.String(), nil
}