NewPattern creates a new Pattern for matching hosts. NewPattern("*") creates a Pattern that matches all hosts. From the manpage, a pattern consists of zero or more non-whitespace characters, `*' (a wildcard that matches zero or more characters), or `?' (a wildcard that matches exactly one character
(s string)
| 463 | // |
| 464 | // Host 192.168.0.? |
| 465 | func NewPattern(s string) (*Pattern, error) { |
| 466 | if s == "" { |
| 467 | return nil, errors.New("ssh_config: empty pattern") |
| 468 | } |
| 469 | negated := false |
| 470 | if s[0] == '!' { |
| 471 | negated = true |
| 472 | s = s[1:] |
| 473 | } |
| 474 | var buf bytes.Buffer |
| 475 | buf.WriteByte('^') |
| 476 | for i := 0; i < len(s); i++ { |
| 477 | // A byte loop is correct because all metacharacters are ASCII. |
| 478 | switch b := s[i]; b { |
| 479 | case '*': |
| 480 | buf.WriteString(".*") |
| 481 | case '?': |
| 482 | buf.WriteString(".?") |
| 483 | default: |
| 484 | // borrowing from QuoteMeta here. |
| 485 | if special(b) { |
| 486 | buf.WriteByte('\\') |
| 487 | } |
| 488 | buf.WriteByte(b) |
| 489 | } |
| 490 | } |
| 491 | buf.WriteByte('$') |
| 492 | r, err := regexp.Compile(buf.String()) |
| 493 | if err != nil { |
| 494 | return nil, err |
| 495 | } |
| 496 | return &Pattern{str: s, regex: r, not: negated}, nil |
| 497 | } |
| 498 | |
| 499 | // Host describes a Host directive and the keywords that follow it. |
| 500 | type Host struct { |