Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix operating system command sequence panic #168

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import (
"strings"
)

const (
bel = '\a' // bel (Bell)
st = '\\' // st (String Terminator)
)

var (
escapeSeqRegExp = regexp.MustCompile(`\x1b\[(\d+(;\d+)*)m`)
boldMarker = regexp.MustCompile(`\*([^*]+?)\*`)
Expand Down Expand Up @@ -93,15 +98,17 @@ func ParseStream(in io.Reader, opts ...ParseOption) (*String, error) {
panic("failed to parse ANSI sequence")
}

var skipUntil = func(end rune) {
var skipUntil = func(ends ...rune) {
for {
r, _, err := input.ReadRune()
if err == io.EOF {
panic("reached end of file before reaching end identifier")
}

if r == end {
return
for _, end := range ends {
if r == end {
return
}
}
}
}
Expand Down Expand Up @@ -169,7 +176,7 @@ func ParseStream(in io.Reader, opts ...ParseOption) (*String, error) {
}

case ']':
skipUntil('\a')
skipUntil(bel, st)
}
}

Expand Down
12 changes: 12 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ var _ = Describe("parse input string", func() {
Expect(err).ToNot(HaveOccurred())
Expect(result.String()).To(Equal("Hello, \x1b[1mWorld\x1b[0m!"))
})

It("should process Operating System Command sequences that terminate on bell", func() {
result, err := ParseStream(strings.NewReader("\x1b]2;title\a"))
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
})

It("should process Operating System Command sequences that terminate string terminator", func() {
result, err := ParseStream(strings.NewReader("\x1b]7;file://host/home/foobar/dir\x1b\\"))
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
})
})

Context("parse Select Graphic Rendition (SGR) based input", func() {
Expand Down
Loading