| 1 |
package terminfo |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"os/exec" |
| 8 |
"path/filepath" |
| 9 |
"reflect" |
| 10 |
"regexp" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"golang.org/x/exp/maps" |
| 17 |
"golang.org/x/exp/slices" |
| 18 |
) |
| 19 |
|
| 20 |
func TestLoad(t *testing.T) { |
| 21 |
keys := maps.Keys(terms(t)) |
| 22 |
slices.Sort(keys) |
| 23 |
for _, ts := range keys { |
| 24 |
term := ts |
| 25 |
t.Run(term, func(t *testing.T) { |
| 26 |
if err := os.Setenv("TERM", term); err != nil { |
| 27 |
t.Fatalf("could not set TERM environment variable, got: %v", err) |
| 28 |
} |
| 29 |
|
| 30 |
info, err := LoadFromEnv() |
| 31 |
if err != nil { |
| 32 |
t.Fatalf("term %s expected no error, got: %v", term, err) |
| 33 |
} |
| 34 |
|
| 35 |
if len(info.Names) < 1 { |
| 36 |
t.Errorf("term %s expected names to have at least one value", term) |
| 37 |
} |
| 38 |
}) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestOpen(t *testing.T) { |
| 43 |
for ts, n := range terms(t) { |
| 44 |
term, filename := ts, n |
| 45 |
t.Run(strings.TrimPrefix(filename, "/"), func(t *testing.T) { |
| 46 |
|
| 47 |
ti, err := Open(filepath.Dir(filepath.Dir(filename)), term) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("term %s expected no error, got: %v", term, err) |
| 50 |
} |
| 51 |
if ti.File != filename { |
| 52 |
t.Errorf("term %s should have file %s, got: %s", term, filename, ti.File) |
| 53 |
} |
| 54 |
|
| 55 |
if len(ti.Names) < 1 { |
| 56 |
t.Errorf("term %s expected names to have at least one value", term) |
| 57 |
} |
| 58 |
}) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestValues(t *testing.T) { |
| 63 |
for ts, n := range terms(t) { |
| 64 |
term, filename := ts, n |
| 65 |
t.Run(filepath.Base(filename), func(t *testing.T) { |
| 66 |
t.Parallel() |
| 67 |
ic, err := getInfocmpData(t, term) |
| 68 |
if err != nil { |
| 69 |
t.Fatalf("expected no error, got: %s", err) |
| 70 |
} |
| 71 |
|
| 72 |
ti, err := Load(term) |
| 73 |
if err != nil { |
| 74 |
t.Fatalf("expected no error, got: %v", err) |
| 75 |
} |
| 76 |
|
| 77 |
if !reflect.DeepEqual(ic.names, ti.Names) { |
| 78 |
t.Errorf("names do not match") |
| 79 |
} |
| 80 |
|
| 81 |
for i, v := range ic.boolCaps { |
| 82 |
if v == nil { |
| 83 |
if _, ok := ti.BoolsM[i]; !ok { |
| 84 |
t.Errorf("expected bool cap %d (%s) to be missing", i, BoolCapName(i)) |
| 85 |
} |
| 86 |
} else if v.(bool) != ti.Bools[i] { |
| 87 |
t.Errorf("bool cap %d (%s) should be %t", i, BoolCapName(i), v) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if len(ic.extBoolCaps) != len(ti.ExtBools) { |
| 92 |
t.Errorf("should have same number of extended bools (%d, %d)", len(ic.extBoolCaps), len(ti.ExtBools)) |
| 93 |
} |
| 94 |
for i, v := range ic.extBoolCaps { |
| 95 |
z, ok := ti.ExtBools[i] |
| 96 |
if !ok { |
| 97 |
t.Errorf("should have extended bool %d", i) |
| 98 |
} |
| 99 |
if v.(bool) != z { |
| 100 |
t.Errorf("extended bool cap %d (%s) should be %t", i, ic.extBoolNames[i], v) |
| 101 |
} |
| 102 |
n, ok := ti.ExtBoolNames[i] |
| 103 |
if !ok { |
| 104 |
t.Errorf("missing extended bool %d name", i) |
| 105 |
} |
| 106 |
if string(n) != ic.extBoolNames[i] { |
| 107 |
t.Errorf("extended bool %d name should be '%s', got: '%s'", i, ic.extBoolNames[i], string(n)) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
for i, v := range ic.numCaps { |
| 112 |
if v == nil { |
| 113 |
if _, ok := ti.NumsM[i]; !ok { |
| 114 |
|
| 115 |
} |
| 116 |
} else if v.(int) != ti.Nums[i] { |
| 117 |
t.Errorf("num cap %d (%s) should be %d", i, NumCapName(i), v) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if len(ic.extNumCaps) != len(ti.ExtNums) { |
| 122 |
t.Errorf("should have same number of extended nums (%d, %d)", len(ic.extNumCaps), len(ti.ExtNums)) |
| 123 |
} |
| 124 |
for i, v := range ic.extNumCaps { |
| 125 |
z, ok := ti.ExtNums[i] |
| 126 |
if !ok { |
| 127 |
t.Errorf("should have extended num %d", i) |
| 128 |
} |
| 129 |
if v.(int) != z { |
| 130 |
t.Errorf("extended num cap %d (%s) should be %t", i, ic.extNumNames[i], v) |
| 131 |
} |
| 132 |
n, ok := ti.ExtNumNames[i] |
| 133 |
if !ok { |
| 134 |
t.Errorf("missing extended num %d name", i) |
| 135 |
} |
| 136 |
if string(n) != ic.extNumNames[i] { |
| 137 |
t.Errorf("extended num %d name should be '%s', got: '%s'", i, ic.extNumNames[i], string(n)) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
for i, v := range ic.stringCaps { |
| 142 |
if v == nil { |
| 143 |
if _, ok := ti.StringsM[i]; !ok { |
| 144 |
|
| 145 |
} |
| 146 |
} else if v.(string) != string(ti.Strings[i]) { |
| 147 |
t.Errorf("string cap %d (%s) is invalid:", i, StringCapName(i)) |
| 148 |
t.Errorf("got: %#v", string(ti.Strings[i])) |
| 149 |
t.Errorf("want: %#v", v) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if len(ic.extStringCaps) != len(ti.ExtStrings) { |
| 154 |
t.Errorf("should have same number of extended strings (%d, %d)", len(ic.extStringCaps), len(ti.ExtStrings)) |
| 155 |
} |
| 156 |
for i, v := range ic.extStringCaps { |
| 157 |
z, ok := ti.ExtStrings[i] |
| 158 |
if !ok { |
| 159 |
t.Errorf("should have extended string %d", i) |
| 160 |
} |
| 161 |
if v.(string) != string(z) { |
| 162 |
t.Errorf("extended string cap %d (%s) should be %t", i, ic.extStringNames[i], v) |
| 163 |
} |
| 164 |
n, ok := ti.ExtStringNames[i] |
| 165 |
if !ok { |
| 166 |
t.Errorf("missing extended string %d name", i) |
| 167 |
} |
| 168 |
if string(n) != ic.extStringNames[i] { |
| 169 |
t.Errorf("extended string %d name should be '%s', got: '%s'", i, ic.extStringNames[i], string(n)) |
| 170 |
} |
| 171 |
} |
| 172 |
}) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
var shortCapNameMap map[string]int |
| 177 |
|
| 178 |
type infocmp struct { |
| 179 |
names []string |
| 180 |
boolCaps map[int]interface{} |
| 181 |
numCaps map[int]interface{} |
| 182 |
stringCaps map[int]interface{} |
| 183 |
extBoolCaps map[int]interface{} |
| 184 |
extNumCaps map[int]interface{} |
| 185 |
extStringCaps map[int]interface{} |
| 186 |
extBoolNames map[int]string |
| 187 |
extNumNames map[int]string |
| 188 |
extStringNames map[int]string |
| 189 |
} |
| 190 |
|
| 191 |
func init() { |
| 192 |
shortCapNameMap = make(map[string]int) |
| 193 |
for _, z := range [][]string{boolCapNames[:], numCapNames[:], stringCapNames[:]} { |
| 194 |
for i := 0; i < len(z); i += 2 { |
| 195 |
shortCapNameMap[z[i+1]] = i / 2 |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
var staticCharRE = regexp.MustCompile(`(?m)^static\s+char\s+(.*)\s*\[\]\s*=\s*(".*");$`) |
| 201 |
|
| 202 |
func getInfocmpData(t *testing.T, term string) (*infocmp, error) { |
| 203 |
t.Logf("loading infocmp data for term %s", term) |
| 204 |
c := exec.Command("/usr/bin/infocmp", "-E", "-x") |
| 205 |
c.Env = []string{"TERM=" + term} |
| 206 |
buf, err := c.CombinedOutput() |
| 207 |
if err != nil { |
| 208 |
t.Logf("shell error (TERM=%s):\n%s\n", term, string(buf)) |
| 209 |
return nil, err |
| 210 |
} |
| 211 |
|
| 212 |
m := staticCharRE.FindAllStringSubmatch(string(buf), -1) |
| 213 |
if !strings.HasSuffix(strings.TrimSpace(m[0][1]), "_alias_data") { |
| 214 |
return nil, errors.New("missing _alias_data") |
| 215 |
} |
| 216 |
|
| 217 |
names, err := strconv.Unquote(`"` + strings.Replace(m[0][2][1:len(m[0][2])-1], `"`, `\"`, -1) + `"`) |
| 218 |
if err != nil { |
| 219 |
return nil, fmt.Errorf("could not unquote _alias_data: %v", err) |
| 220 |
} |
| 221 |
ic := &infocmp{ |
| 222 |
names: strings.Split(names, "|"), |
| 223 |
boolCaps: make(map[int]interface{}), |
| 224 |
numCaps: make(map[int]interface{}), |
| 225 |
stringCaps: make(map[int]interface{}), |
| 226 |
extBoolCaps: make(map[int]interface{}), |
| 227 |
extNumCaps: make(map[int]interface{}), |
| 228 |
extStringCaps: make(map[int]interface{}), |
| 229 |
extBoolNames: make(map[int]string), |
| 230 |
extNumNames: make(map[int]string), |
| 231 |
extStringNames: make(map[int]string), |
| 232 |
} |
| 233 |
|
| 234 |
caps := make(map[string]string, len(m)) |
| 235 |
for i, s := range m[1:] { |
| 236 |
k := strings.TrimSpace(s[1]) |
| 237 |
idx := strings.LastIndex(k, "_s_") |
| 238 |
if idx == -1 { |
| 239 |
return nil, fmt.Errorf("string cap %d (%s) does not contain _s_", i, k) |
| 240 |
} |
| 241 |
v, err := strconv.Unquote(s[2]) |
| 242 |
if err != nil { |
| 243 |
return nil, fmt.Errorf("could not unquote %d: %v", i, err) |
| 244 |
} |
| 245 |
caps[k] = v |
| 246 |
} |
| 247 |
|
| 248 |
for _, err := range []error{ |
| 249 |
processSect(buf, caps, ic.boolCaps, ic.extBoolCaps, ic.extBoolNames, boolSectRE), |
| 250 |
processSect(buf, caps, ic.numCaps, ic.extNumCaps, ic.extNumNames, numSectRE), |
| 251 |
processSect(buf, caps, ic.stringCaps, ic.extStringCaps, ic.extStringNames, stringSectRE), |
| 252 |
} { |
| 253 |
if err != nil { |
| 254 |
return nil, err |
| 255 |
} |
| 256 |
} |
| 257 |
return ic, nil |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
var ( |
| 262 |
boolSectRE = regexp.MustCompile(`_bool_data\[\]\s*=\s*{`) |
| 263 |
numSectRE = regexp.MustCompile(`_number_data\[\]\s*=\s*{`) |
| 264 |
stringSectRE = regexp.MustCompile(`_string_data\[\]\s*=\s*{`) |
| 265 |
endSectRE = regexp.MustCompile(`(?m)^};$`) |
| 266 |
capValuesRE = regexp.MustCompile(`(?m)^\s+/\*\s+[0-9]+:\s+([^\s]+)\s+\*/\s+(.*),$`) |
| 267 |
numRE = regexp.MustCompile(`^[0-9]+$`) |
| 268 |
absCanRE = regexp.MustCompile(`^(ABSENT|CANCELLED)_(BOOLEAN|NUMERIC|STRING)$`) |
| 269 |
) |
| 270 |
|
| 271 |
|
| 272 |
func processSect(buf []byte, caps map[string]string, xx, yy map[int]interface{}, extn map[int]string, sectRE *regexp.Regexp) error { |
| 273 |
var err error |
| 274 |
|
| 275 |
start := sectRE.FindIndex(buf) |
| 276 |
if start == nil || len(start) != 2 { |
| 277 |
return fmt.Errorf("could not find sect (%s)", sectRE) |
| 278 |
} |
| 279 |
end := endSectRE.FindIndex(buf[start[1]:]) |
| 280 |
if end == nil || len(end) != 2 { |
| 281 |
return fmt.Errorf("could not find end of section (%s)", sectRE) |
| 282 |
} |
| 283 |
buf = buf[start[1] : start[1]+end[0]] |
| 284 |
|
| 285 |
m := capValuesRE.FindAllStringSubmatch(string(buf), -1) |
| 286 |
var extc int |
| 287 |
for i, s := range m { |
| 288 |
var skip bool |
| 289 |
|
| 290 |
target := xx |
| 291 |
k, ok := shortCapNameMap[s[1]] |
| 292 |
if !ok { |
| 293 |
target, k, extn[extc] = yy, extc, s[1] |
| 294 |
extc++ |
| 295 |
} |
| 296 |
|
| 297 |
var v interface{} |
| 298 |
switch { |
| 299 |
case s[2] == "TRUE" || s[2] == "FALSE": |
| 300 |
v = s[2] == "TRUE" |
| 301 |
case numRE.MatchString(s[2]): |
| 302 |
var j int64 |
| 303 |
j, err = strconv.ParseInt(s[2], 10, 32) |
| 304 |
if err != nil { |
| 305 |
return fmt.Errorf("line %d could not parse: %v", i, err) |
| 306 |
} |
| 307 |
v = int(j) |
| 308 |
case absCanRE.MatchString(s[2]): |
| 309 |
if !ok { |
| 310 |
if strings.HasSuffix(s[2], "NUMERIC") { |
| 311 |
v = -1 |
| 312 |
} else { |
| 313 |
skip = true |
| 314 |
} |
| 315 |
} |
| 316 |
default: |
| 317 |
v, ok = caps[s[2]] |
| 318 |
if !ok { |
| 319 |
return fmt.Errorf("cap '%s' not defined in cap table", s[2]) |
| 320 |
} |
| 321 |
} |
| 322 |
if !skip { |
| 323 |
target[k] = v |
| 324 |
} |
| 325 |
} |
| 326 |
return nil |
| 327 |
} |
| 328 |
|
| 329 |
var termNameCache = struct { |
| 330 |
names map[string]string |
| 331 |
sync.Mutex |
| 332 |
}{} |
| 333 |
|
| 334 |
var fileRE = regexp.MustCompile("^([0-9]+|[a-zA-Z])/") |
| 335 |
|
| 336 |
func terms(t *testing.T) map[string]string { |
| 337 |
termNameCache.Lock() |
| 338 |
defer termNameCache.Unlock() |
| 339 |
if termNameCache.names == nil { |
| 340 |
termNameCache.names = make(map[string]string) |
| 341 |
for _, dir := range termDirs() { |
| 342 |
err := filepath.Walk(dir, func(n string, fi os.FileInfo, err error) error { |
| 343 |
switch { |
| 344 |
case err != nil: |
| 345 |
return err |
| 346 |
case fi.IsDir(), dir == n, !fileRE.MatchString(n[len(dir)+1:]), fi.Mode()&os.ModeSymlink != 0: |
| 347 |
return nil |
| 348 |
} |
| 349 |
term := filepath.Base(n) |
| 350 |
if term == "xterm-old" { |
| 351 |
return nil |
| 352 |
} |
| 353 |
termNameCache.names[term] = n |
| 354 |
return nil |
| 355 |
}) |
| 356 |
if err != nil { |
| 357 |
t.Fatalf("could not walk directory, got: %v", err) |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
return termNameCache.names |
| 362 |
} |
| 363 |
|
| 364 |
func termDirs() []string { |
| 365 |
var dirs []string |
| 366 |
for _, dir := range []string{"/lib/terminfo", "/usr/share/terminfo"} { |
| 367 |
fi, err := os.Stat(dir) |
| 368 |
if err != nil && os.IsNotExist(err) { |
| 369 |
continue |
| 370 |
} else if err != nil { |
| 371 |
panic(err) |
| 372 |
} |
| 373 |
if fi.IsDir() { |
| 374 |
dirs = append(dirs, dir) |
| 375 |
} |
| 376 |
} |
| 377 |
return dirs |
| 378 |
} |
| 379 |
|
| 380 |
func TestCapSizes(t *testing.T) { |
| 381 |
if CapCountBool*2 != len(boolCapNames) { |
| 382 |
t.Fatalf("boolCapNames should have same length as twice CapCountBool") |
| 383 |
} |
| 384 |
if CapCountNum*2 != len(numCapNames) { |
| 385 |
t.Fatalf("numCapNames should have same length as twice CapCountNum") |
| 386 |
} |
| 387 |
if CapCountString*2 != len(stringCapNames) { |
| 388 |
t.Fatalf("stringCapNames should have same length as twice CapCountString") |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
func TestCapNames(t *testing.T) { |
| 393 |
for i := 0; i < CapCountBool; i++ { |
| 394 |
n, s := BoolCapName(i), BoolCapNameShort(i) |
| 395 |
if n == "" { |
| 396 |
t.Errorf("Bool cap %d should have name", i) |
| 397 |
} |
| 398 |
if s == "" { |
| 399 |
t.Errorf("Bool cap %d should have short name", i) |
| 400 |
} |
| 401 |
if n == s { |
| 402 |
t.Errorf("Bool cap %d name and short name should not equal (%s==%s)", i, n, s) |
| 403 |
} |
| 404 |
} |
| 405 |
for i := 0; i < CapCountNum; i++ { |
| 406 |
n, s := NumCapName(i), NumCapNameShort(i) |
| 407 |
if n == "" { |
| 408 |
t.Errorf("Num cap %d should have name", i) |
| 409 |
} |
| 410 |
if s == "" { |
| 411 |
t.Errorf("Num cap %d should have short name", i) |
| 412 |
} |
| 413 |
if n == s && n != "lines" { |
| 414 |
t.Errorf("Num cap %d name and short name should not equal (%s==%s)", i, n, s) |
| 415 |
} |
| 416 |
} |
| 417 |
for i := 0; i < CapCountString; i++ { |
| 418 |
n, s := StringCapName(i), StringCapNameShort(i) |
| 419 |
if n == "" { |
| 420 |
t.Errorf("String cap %d should have name", i) |
| 421 |
} |
| 422 |
if s == "" { |
| 423 |
t.Errorf("String cap %d should have short name", i) |
| 424 |
} |
| 425 |
if n == s && n != "tone" && n != "pulse" { |
| 426 |
t.Errorf("String cap %d name and short name should not equal (%s==%s)", i, n, s) |
| 427 |
} |
| 428 |
} |
| 429 |
} |