2017-04-17 18:08:24 -04:00
|
|
|
package ansiterm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-10-11 09:30:07 -04:00
|
|
|
"log"
|
2017-04-17 18:08:24 -04:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AnsiParser struct {
|
|
|
|
currState state
|
|
|
|
eventHandler AnsiEventHandler
|
|
|
|
context *ansiContext
|
|
|
|
csiEntry state
|
|
|
|
csiParam state
|
|
|
|
dcsEntry state
|
|
|
|
escape state
|
|
|
|
escapeIntermediate state
|
|
|
|
error state
|
|
|
|
ground state
|
|
|
|
oscString state
|
|
|
|
stateMap []state
|
2017-10-11 09:30:07 -04:00
|
|
|
|
|
|
|
logf func(string, ...interface{})
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2017-10-11 09:30:07 -04:00
|
|
|
type Option func(*AnsiParser)
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2017-10-11 09:30:07 -04:00
|
|
|
func WithLogf(f func(string, ...interface{})) Option {
|
|
|
|
return func(ap *AnsiParser) {
|
|
|
|
ap.logf = f
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2017-10-11 09:30:07 -04:00
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2017-10-11 09:30:07 -04:00
|
|
|
func CreateParser(initialState string, evtHandler AnsiEventHandler, opts ...Option) *AnsiParser {
|
|
|
|
ap := &AnsiParser{
|
2017-04-17 18:08:24 -04:00
|
|
|
eventHandler: evtHandler,
|
|
|
|
context: &ansiContext{},
|
|
|
|
}
|
2017-10-11 09:30:07 -04:00
|
|
|
for _, o := range opts {
|
|
|
|
o(ap)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDebugEnv := os.Getenv(LogEnv); isDebugEnv == "1" {
|
|
|
|
logFile, _ := os.Create("ansiParser.log")
|
|
|
|
logger := log.New(logFile, "", log.LstdFlags)
|
|
|
|
if ap.logf != nil {
|
|
|
|
l := ap.logf
|
|
|
|
ap.logf = func(s string, v ...interface{}) {
|
|
|
|
l(s, v...)
|
|
|
|
logger.Printf(s, v...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ap.logf = logger.Printf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ap.logf == nil {
|
|
|
|
ap.logf = func(string, ...interface{}) {}
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2017-10-11 09:30:07 -04:00
|
|
|
ap.csiEntry = csiEntryState{baseState{name: "CsiEntry", parser: ap}}
|
|
|
|
ap.csiParam = csiParamState{baseState{name: "CsiParam", parser: ap}}
|
|
|
|
ap.dcsEntry = dcsEntryState{baseState{name: "DcsEntry", parser: ap}}
|
|
|
|
ap.escape = escapeState{baseState{name: "Escape", parser: ap}}
|
|
|
|
ap.escapeIntermediate = escapeIntermediateState{baseState{name: "EscapeIntermediate", parser: ap}}
|
|
|
|
ap.error = errorState{baseState{name: "Error", parser: ap}}
|
|
|
|
ap.ground = groundState{baseState{name: "Ground", parser: ap}}
|
|
|
|
ap.oscString = oscStringState{baseState{name: "OscString", parser: ap}}
|
|
|
|
|
|
|
|
ap.stateMap = []state{
|
|
|
|
ap.csiEntry,
|
|
|
|
ap.csiParam,
|
|
|
|
ap.dcsEntry,
|
|
|
|
ap.escape,
|
|
|
|
ap.escapeIntermediate,
|
|
|
|
ap.error,
|
|
|
|
ap.ground,
|
|
|
|
ap.oscString,
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2017-10-11 09:30:07 -04:00
|
|
|
ap.currState = getState(initialState, ap.stateMap)
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2017-10-11 09:30:07 -04:00
|
|
|
ap.logf("CreateParser: parser %p", ap)
|
|
|
|
return ap
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getState(name string, states []state) state {
|
|
|
|
for _, el := range states {
|
|
|
|
if el.Name() == name {
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ap *AnsiParser) Parse(bytes []byte) (int, error) {
|
|
|
|
for i, b := range bytes {
|
|
|
|
if err := ap.handle(b); err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(bytes), ap.eventHandler.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ap *AnsiParser) handle(b byte) error {
|
|
|
|
ap.context.currentChar = b
|
|
|
|
newState, err := ap.currState.Handle(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if newState == nil {
|
2017-10-11 09:30:07 -04:00
|
|
|
ap.logf("WARNING: newState is nil")
|
2017-04-17 18:08:24 -04:00
|
|
|
return errors.New("New state of 'nil' is invalid.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if newState != ap.currState {
|
|
|
|
if err := ap.changeState(newState); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ap *AnsiParser) changeState(newState state) error {
|
2017-10-11 09:30:07 -04:00
|
|
|
ap.logf("ChangeState %s --> %s", ap.currState.Name(), newState.Name())
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
// Exit old state
|
|
|
|
if err := ap.currState.Exit(); err != nil {
|
2017-10-11 09:30:07 -04:00
|
|
|
ap.logf("Exit state '%s' failed with : '%v'", ap.currState.Name(), err)
|
2017-04-17 18:08:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform transition action
|
|
|
|
if err := ap.currState.Transition(newState); err != nil {
|
2017-10-11 09:30:07 -04:00
|
|
|
ap.logf("Transition from '%s' to '%s' failed with: '%v'", ap.currState.Name(), newState.Name, err)
|
2017-04-17 18:08:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter new state
|
|
|
|
if err := newState.Enter(); err != nil {
|
2017-10-11 09:30:07 -04:00
|
|
|
ap.logf("Enter state '%s' failed with: '%v'", newState.Name(), err)
|
2017-04-17 18:08:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ap.currState = newState
|
|
|
|
return nil
|
|
|
|
}
|