jm

Odin for scripts: small packages and a runner, on core: only
Log | Files | Refs | README

sh_unix.odin (1045B)


      1 #+build !windows
      2 package sh
      3 
      4 import "core:slice"
      5 import "core:strings"
      6 
      7 shell_argv :: proc(cmd: string, shell: Shell, allocator := context.allocator) -> []string {
      8 	switch shell {
      9 	case .Pwsh:
     10 		return slice.clone([]string{"pwsh", "-NoProfile", "-Command", cmd}, allocator)
     11 	case .Default, .Sh, .Cmd:
     12 		return slice.clone([]string{"/bin/sh", "-c", cmd}, allocator)
     13 	}
     14 	return slice.clone([]string{"/bin/sh", "-c", cmd}, allocator)
     15 }
     16 
     17 executable_extensions :: proc(allocator := context.temp_allocator) -> []string {
     18 	return slice.clone([]string{""}, allocator)
     19 }
     20 
     21 // quote makes s safe as one word in a sh command line.
     22 quote :: proc(s: string, allocator := context.allocator) -> string {
     23 	if s != "" && strings.index_any(s, " \t\n'\"\\$`!*?[]{}()<>|&;#~") < 0 {
     24 		return strings.clone(s, allocator)
     25 	}
     26 	b := strings.builder_make(allocator)
     27 	strings.write_byte(&b, '\'')
     28 	for c in s {
     29 		if c == '\'' {
     30 			strings.write_string(&b, `'\''`)
     31 		} else {
     32 			strings.write_rune(&b, c)
     33 		}
     34 	}
     35 	strings.write_byte(&b, '\'')
     36 	return strings.to_string(b)
     37 }