2019-05-16 18:12:40 +00:00
|
|
|
package escapingio
|
|
|
|
|
|
|
|
import (
|
2019-05-17 14:23:40 +00:00
|
|
|
"bufio"
|
2019-05-16 18:12:40 +00:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler is a callback for handling an escaped char. Reader would skip
|
|
|
|
// the escape char and passed char if returns true; otherwise, it preserves them
|
|
|
|
// in output
|
|
|
|
type Handler func(c byte) bool
|
|
|
|
|
|
|
|
// NewReader returns a reader that escapes the c character (following new lines),
|
|
|
|
// in the same manner OpenSSH handling, which defaults to `~`.
|
|
|
|
//
|
|
|
|
// For illustrative purposes, we use `~` in documentation as a shorthand for escaping character.
|
|
|
|
//
|
|
|
|
// If following a new line, reader sees:
|
2022-08-16 14:06:30 +00:00
|
|
|
// - `~~`, only one is emitted
|
|
|
|
// - `~.` (or any character), the handler is invoked with the character.
|
2019-05-16 18:12:40 +00:00
|
|
|
// If handler returns true, `~.` will be skipped; otherwise, it's propagated.
|
2022-08-16 14:06:30 +00:00
|
|
|
// - `~` and it's the last character in stream, it's propagated
|
2019-05-16 18:12:40 +00:00
|
|
|
//
|
2019-05-16 21:06:03 +00:00
|
|
|
// Appearances of `~` when not preceded by a new line are propagated unmodified.
|
2019-05-16 18:12:40 +00:00
|
|
|
func NewReader(r io.Reader, c byte, h Handler) io.Reader {
|
2019-05-17 14:23:40 +00:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
reader := &reader{
|
2019-05-16 18:12:40 +00:00
|
|
|
impl: r,
|
|
|
|
escapeChar: c,
|
|
|
|
handler: h,
|
2019-05-17 14:23:40 +00:00
|
|
|
pr: pr,
|
|
|
|
pw: pw,
|
2019-05-16 18:12:40 +00:00
|
|
|
}
|
2019-05-17 14:23:40 +00:00
|
|
|
go reader.pipe()
|
|
|
|
return reader
|
2019-05-16 18:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lookState represents the state of reader for what character of `\n~.` sequence
|
|
|
|
// reader is looking for
|
|
|
|
type lookState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// sLookNewLine indicates that reader is looking for new line
|
|
|
|
sLookNewLine lookState = iota
|
|
|
|
|
|
|
|
// sLookEscapeChar indicates that reader is looking for ~
|
|
|
|
sLookEscapeChar
|
|
|
|
|
|
|
|
// sLookChar indicates that reader just read `~` is waiting for next character
|
|
|
|
// before acting
|
|
|
|
sLookChar
|
|
|
|
)
|
|
|
|
|
|
|
|
// to ease comments, i'll assume escape character to be `~`
|
|
|
|
type reader struct {
|
|
|
|
impl io.Reader
|
|
|
|
escapeChar uint8
|
|
|
|
handler Handler
|
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
// buffers
|
|
|
|
pw *io.PipeWriter
|
|
|
|
pr *io.PipeReader
|
2019-05-16 18:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *reader) Read(buf []byte) (int, error) {
|
2019-05-17 14:23:40 +00:00
|
|
|
return r.pr.Read(buf)
|
|
|
|
}
|
2019-05-16 18:12:40 +00:00
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
func (r *reader) pipe() {
|
|
|
|
rb := make([]byte, 4096)
|
|
|
|
bw := bufio.NewWriter(r.pw)
|
|
|
|
|
|
|
|
state := sLookEscapeChar
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err := r.impl.Read(rb)
|
|
|
|
|
|
|
|
if n > 0 {
|
|
|
|
state = r.processBuf(bw, rb, n, state)
|
|
|
|
bw.Flush()
|
|
|
|
if state == sLookChar {
|
|
|
|
// terminated with ~ - let's read one more character
|
|
|
|
n, err = r.impl.Read(rb[:1])
|
|
|
|
if n == 1 {
|
|
|
|
state = sLookNewLine
|
|
|
|
if rb[0] == r.escapeChar {
|
|
|
|
// only emit escape character once
|
|
|
|
bw.WriteByte(rb[0])
|
|
|
|
bw.Flush()
|
|
|
|
} else if r.handler(rb[0]) {
|
|
|
|
// skip if handled
|
|
|
|
} else {
|
|
|
|
bw.WriteByte(r.escapeChar)
|
|
|
|
bw.WriteByte(rb[0])
|
|
|
|
bw.Flush()
|
2020-09-01 00:31:44 +00:00
|
|
|
if rb[0] == '\n' || rb[0] == '\r' {
|
|
|
|
state = sLookEscapeChar
|
|
|
|
}
|
2019-05-17 14:23:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 18:12:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
if err != nil {
|
|
|
|
// write ~ if it's the last thing
|
|
|
|
if state == sLookChar {
|
|
|
|
bw.WriteByte(r.escapeChar)
|
2019-05-16 18:12:40 +00:00
|
|
|
}
|
2019-05-17 14:23:40 +00:00
|
|
|
bw.Flush()
|
|
|
|
r.pw.CloseWithError(err)
|
|
|
|
break
|
2019-05-16 18:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 14:23:40 +00:00
|
|
|
}
|
2019-05-16 18:12:40 +00:00
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
// processBuf process buffer and emits all output to writer
|
|
|
|
// if the last part of buffer is a new line followed by sequnce, it writes
|
|
|
|
// all output until the new line and returns sLookChar
|
|
|
|
func (r *reader) processBuf(bw io.Writer, buf []byte, n int, s lookState) lookState {
|
|
|
|
i := 0
|
2019-05-16 18:12:40 +00:00
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
wi := 0
|
2019-05-16 18:12:40 +00:00
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
START:
|
|
|
|
if s == sLookEscapeChar && buf[i] == r.escapeChar {
|
|
|
|
if i+1 >= n {
|
|
|
|
// buf terminates with ~ - write all before
|
|
|
|
bw.Write(buf[wi:i])
|
|
|
|
return sLookChar
|
|
|
|
}
|
2019-05-16 18:12:40 +00:00
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
nc := buf[i+1]
|
|
|
|
if nc == r.escapeChar {
|
|
|
|
// skip one escape char
|
|
|
|
bw.Write(buf[wi:i])
|
|
|
|
i++
|
|
|
|
wi = i
|
|
|
|
} else if r.handler(nc) {
|
|
|
|
// skip both characters
|
|
|
|
bw.Write(buf[wi:i])
|
|
|
|
i = i + 2
|
|
|
|
wi = i
|
2020-09-01 00:31:44 +00:00
|
|
|
} else if nc == '\n' || nc == '\r' {
|
|
|
|
i = i + 2
|
|
|
|
s = sLookEscapeChar
|
|
|
|
goto START
|
2019-05-17 14:23:40 +00:00
|
|
|
} else {
|
|
|
|
i = i + 2
|
|
|
|
// need to write everything keep going
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 18:12:40 +00:00
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
// search until we get \n~, or buf terminates
|
|
|
|
for {
|
|
|
|
if i >= n {
|
|
|
|
// got to end without new line, write and return
|
|
|
|
bw.Write(buf[wi:n])
|
|
|
|
return sLookNewLine
|
|
|
|
}
|
2019-05-16 18:12:40 +00:00
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
if buf[i] == '\n' || buf[i] == '\r' {
|
|
|
|
// buf terminated at new line
|
|
|
|
if i+1 >= n {
|
|
|
|
bw.Write(buf[wi:n])
|
|
|
|
return sLookEscapeChar
|
2019-05-16 18:12:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
// peek to see escape character go back to START if so
|
|
|
|
if buf[i+1] == r.escapeChar {
|
|
|
|
s = sLookEscapeChar
|
|
|
|
i++
|
|
|
|
goto START
|
2019-05-16 18:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:40 +00:00
|
|
|
i++
|
|
|
|
}
|
2019-05-16 18:12:40 +00:00
|
|
|
}
|