| 1 | |
| 2 | |
| 3 | |
| 4 | package env |
| 5 | |
| 6 | import ( |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | |
| 13 | func SQEscape(s string) string { |
| 14 | return strings.ReplaceAll(s, "'", `'"'"'`) |
| 15 | } |
| 16 | |
| 17 | |
| 18 | const ( |
| 19 | Found = 0 |
| 20 | NotFound = 1 |
| 21 | |
| 22 | |
| 23 | Refused = 3 |
| 24 | ) |
| 25 | |
| 26 | |
| 27 | |
| 28 | func records(content string) []string { |
| 29 | if content == "" { |
| 30 | return nil |
| 31 | } |
| 32 | return strings.Split(strings.TrimSuffix(content, "\n"), "\n") |
| 33 | } |
| 34 | |
| 35 | var ( |
| 36 | leadingBlank = regexp.MustCompile(`^[ \t]+`) |
| 37 | exportPrefix = regexp.MustCompile(`^export[ \t]+`) |
| 38 | assignment = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*=`) |
| 39 | blankOrNote = regexp.MustCompile(`^(#.*)?$`) |
| 40 | restOK = regexp.MustCompile(`^[ \t]+(#.*)?$`) |
| 41 | ) |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | func Lookup(content, key string) (string, int) { |
| 53 | |
| 54 | |
| 55 | lines := records(StripCR(content)) |
| 56 | found := false |
| 57 | val := "" |
| 58 | for i := 0; i < len(lines); i++ { |
| 59 | line := leadingBlank.ReplaceAllString(lines[i], "") |
| 60 | if blankOrNote.MatchString(line) { |
| 61 | continue |
| 62 | } |
| 63 | line = exportPrefix.ReplaceAllString(line, "") |
| 64 | if !assignment.MatchString(line) { |
| 65 | return "", Refused |
| 66 | } |
| 67 | eq := strings.IndexByte(line, '=') |
| 68 | v, st, used := parseValue(line[eq+1:], lines[i+1:]) |
| 69 | i += used |
| 70 | if st != Found { |
| 71 | return "", Refused |
| 72 | } |
| 73 | if line[:eq] == key { |
| 74 | found, val = true, v |
| 75 | } |
| 76 | } |
| 77 | if !found { |
| 78 | return "", NotFound |
| 79 | } |
| 80 | return val, Found |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | func parseValue(s string, more []string) (v string, st, used int) { |
| 86 | for s != "" { |
| 87 | switch c := s[0]; { |
| 88 | case c == '\'': |
| 89 | s = s[1:] |
| 90 | for { |
| 91 | e := strings.IndexByte(s, '\'') |
| 92 | if e >= 0 { |
| 93 | v += s[:e] |
| 94 | s = s[e+1:] |
| 95 | break |
| 96 | } |
| 97 | if used == len(more) { |
| 98 | return v, Refused, used |
| 99 | } |
| 100 | v += s + "\n" |
| 101 | s = more[used] |
| 102 | used++ |
| 103 | } |
| 104 | case c == '"': |
| 105 | s = s[1:] |
| 106 | e := strings.IndexByte(s, '"') |
| 107 | if e < 0 || strings.ContainsAny(s[:e], "$`\\") { |
| 108 | return v, Refused, used |
| 109 | } |
| 110 | v += s[:e] |
| 111 | s = s[e+1:] |
| 112 | case c == ' ' || c == '\t': |
| 113 | if restOK.MatchString(s) { |
| 114 | return v, Found, used |
| 115 | } |
| 116 | return v, Refused, used |
| 117 | case strings.IndexByte("$`\\;&|<>()~\r", c) >= 0: |
| 118 | return v, Refused, used |
| 119 | default: |
| 120 | v += s[:1] |
| 121 | s = s[1:] |
| 122 | } |
| 123 | } |
| 124 | return v, Found, used |
| 125 | } |
| 126 | |
| 127 | var nameAssign = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*=`) |
| 128 | |
| 129 | |
| 130 | func Names(content string) []string { |
| 131 | var names []string |
| 132 | walk(content, func(_ string, name string) { names = append(names, name) }, nil) |
| 133 | return names |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | func Drop(content, key string) string { |
| 139 | var b strings.Builder |
| 140 | walk(content, nil, func(line, name string) { |
| 141 | if name != key { |
| 142 | b.WriteString(line + "\n") |
| 143 | } |
| 144 | }) |
| 145 | return b.String() |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | func walk(content string, name func(line, name string), line func(line, name string)) { |
| 152 | q := byte(0) |
| 153 | cur := "" |
| 154 | scan := func(s string) { |
| 155 | for i := 0; i < len(s); i++ { |
| 156 | c := s[i] |
| 157 | switch q { |
| 158 | case '\'': |
| 159 | if c == '\'' { |
| 160 | q = 0 |
| 161 | } |
| 162 | continue |
| 163 | case '$': |
| 164 | if c == '\\' { |
| 165 | i++ |
| 166 | } else if c == '\'' { |
| 167 | q = 0 |
| 168 | } |
| 169 | continue |
| 170 | case '"': |
| 171 | if c == '\\' { |
| 172 | i++ |
| 173 | } else if c == '"' { |
| 174 | q = 0 |
| 175 | } |
| 176 | continue |
| 177 | } |
| 178 | switch { |
| 179 | case c == '\\': |
| 180 | i++ |
| 181 | case c == '$' && i+1 < len(s) && s[i+1] == '\'': |
| 182 | q = '$' |
| 183 | i++ |
| 184 | case c == '\'' || c == '"': |
| 185 | q = c |
| 186 | case c == '#' && i > 0 && (s[i-1] == ' ' || s[i-1] == '\t'): |
| 187 | return |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | for _, l := range records(content) { |
| 192 | if q != 0 { |
| 193 | scan(l) |
| 194 | if line != nil { |
| 195 | line(l, cur) |
| 196 | } |
| 197 | continue |
| 198 | } |
| 199 | cur = "" |
| 200 | t := exportPrefix.ReplaceAllString(leadingBlank.ReplaceAllString(l, ""), "") |
| 201 | if m := nameAssign.FindString(t); m != "" { |
| 202 | cur = m[:len(m)-1] |
| 203 | if name != nil { |
| 204 | name(l, cur) |
| 205 | } |
| 206 | scan(t[len(m):]) |
| 207 | } |
| 208 | if line != nil { |
| 209 | line(l, cur) |
| 210 | } |
| 211 | } |
| 212 | } |