mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
[os2/process]: Don't quote arguments unless needed
This commit is contained in:
@@ -638,6 +638,19 @@ _parse_command_line :: proc(cmd_line_w: [^]u16, allocator: runtime.Allocator) ->
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
QUOTED_CHARS :: "()[]{}^=;!'+,`~\" "
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
_char_needs_escape :: proc(ch: u8) -> bool {
|
||||||
|
for r in transmute([]byte) string(QUOTED_CHARS) {
|
||||||
|
if ch == r {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
_build_command_line :: proc(command: []string, allocator: runtime.Allocator) -> string {
|
_build_command_line :: proc(command: []string, allocator: runtime.Allocator) -> string {
|
||||||
_write_byte_n_times :: #force_inline proc(builder: ^strings.Builder, b: byte, n: int) {
|
_write_byte_n_times :: #force_inline proc(builder: ^strings.Builder, b: byte, n: int) {
|
||||||
for _ in 0 ..< n {
|
for _ in 0 ..< n {
|
||||||
@@ -650,26 +663,35 @@ _build_command_line :: proc(command: []string, allocator: runtime.Allocator) ->
|
|||||||
strings.write_byte(&builder, ' ')
|
strings.write_byte(&builder, ' ')
|
||||||
}
|
}
|
||||||
j := 0
|
j := 0
|
||||||
strings.write_byte(&builder, '"')
|
arg_needs_quoting := false
|
||||||
for j < len(arg) {
|
// Note(flysand): From documentation of `cmd.exe`, run "cmd /?"
|
||||||
backslashes := 0
|
if strings.contains_any(arg, QUOTED_CHARS) {
|
||||||
for j < len(arg) && arg[j] == '\\' {
|
arg_needs_quoting = true
|
||||||
backslashes += 1
|
}
|
||||||
|
if !arg_needs_quoting {
|
||||||
|
strings.write_string(&builder, arg)
|
||||||
|
} else {
|
||||||
|
strings.write_byte(&builder, '"')
|
||||||
|
for j < len(arg) {
|
||||||
|
backslashes := 0
|
||||||
|
for j < len(arg) && arg[j] == '\\' {
|
||||||
|
backslashes += 1
|
||||||
|
j += 1
|
||||||
|
}
|
||||||
|
if j == len(arg) {
|
||||||
|
_write_byte_n_times(&builder, '\\', 2*backslashes)
|
||||||
|
break
|
||||||
|
} else if arg[j] == '"' {
|
||||||
|
_write_byte_n_times(&builder, '\\', 2*backslashes+1)
|
||||||
|
strings.write_byte(&builder, arg[j])
|
||||||
|
} else {
|
||||||
|
_write_byte_n_times(&builder, '\\', backslashes)
|
||||||
|
strings.write_byte(&builder, arg[j])
|
||||||
|
}
|
||||||
j += 1
|
j += 1
|
||||||
}
|
}
|
||||||
if j == len(arg) {
|
strings.write_byte(&builder, '"')
|
||||||
_write_byte_n_times(&builder, '\\', 2*backslashes)
|
|
||||||
break
|
|
||||||
} else if arg[j] == '"' {
|
|
||||||
_write_byte_n_times(&builder, '\\', 2*backslashes+1)
|
|
||||||
strings.write_byte(&builder, '"')
|
|
||||||
} else {
|
|
||||||
_write_byte_n_times(&builder, '\\', backslashes)
|
|
||||||
strings.write_byte(&builder, arg[j])
|
|
||||||
}
|
|
||||||
j += 1
|
|
||||||
}
|
}
|
||||||
strings.write_byte(&builder, '"')
|
|
||||||
}
|
}
|
||||||
return strings.to_string(builder)
|
return strings.to_string(builder)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user