github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/cmd/bpf2go/tools.go (about) 1 package main 2 3 import ( 4 "errors" 5 "fmt" 6 "runtime/debug" 7 "strings" 8 "unicode" 9 "unicode/utf8" 10 ) 11 12 func splitCFlagsFromArgs(in []string) (args, cflags []string) { 13 for i, arg := range in { 14 if arg == "--" { 15 return in[:i], in[i+1:] 16 } 17 } 18 19 return in, nil 20 } 21 22 func splitArguments(in string) ([]string, error) { 23 var ( 24 result []string 25 builder strings.Builder 26 escaped bool 27 delim = ' ' 28 ) 29 30 for _, r := range strings.TrimSpace(in) { 31 if escaped { 32 builder.WriteRune(r) 33 escaped = false 34 continue 35 } 36 37 switch r { 38 case '\\': 39 escaped = true 40 41 case delim: 42 current := builder.String() 43 builder.Reset() 44 45 if current != "" || delim != ' ' { 46 // Only append empty words if they are not 47 // delimited by spaces 48 result = append(result, current) 49 } 50 delim = ' ' 51 52 case '"', '\'', ' ': 53 if delim == ' ' { 54 delim = r 55 continue 56 } 57 58 fallthrough 59 60 default: 61 builder.WriteRune(r) 62 } 63 } 64 65 if delim != ' ' { 66 return nil, fmt.Errorf("missing `%c`", delim) 67 } 68 69 if escaped { 70 return nil, errors.New("unfinished escape") 71 } 72 73 // Add the last word 74 if builder.Len() > 0 { 75 result = append(result, builder.String()) 76 } 77 78 return result, nil 79 } 80 81 func toUpperFirst(str string) string { 82 first, n := utf8.DecodeRuneInString(str) 83 return string(unicode.ToUpper(first)) + str[n:] 84 } 85 86 func currentModule() string { 87 bi, ok := debug.ReadBuildInfo() 88 if !ok { 89 // Fall back to constant since bazel doesn't support BuildInfo. 90 return "github.com/cilium/ebpf" 91 } 92 93 return bi.Main.Path 94 }