2015-04-16 17:01:37 -04:00
|
|
|
package ansiterm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-10-02 03:46:20 -04:00
|
|
|
"log"
|
2015-04-16 17:01:37 -04:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AnsiParser struct {
|
2016-04-25 18:55:12 -04:00
|
|
|
currState state
|
2015-04-16 17:01:37 -04:00
|
|
|
eventHandler AnsiEventHandler
|
2016-04-25 18:55:12 -04:00
|
|
|
context *ansiContext
|
|
|
|
csiEntry state
|
|
|
|
csiParam state
|
|
|
|
dcsEntry state
|
|
|
|
escape state
|
|
|
|
escapeIntermediate state
|
|
|
|
error state
|
|
|
|
ground state
|
|
|
|
oscString state
|
|
|
|
stateMap []state
|
2017-10-02 03:46:20 -04:00
|
|
|
|
|
|
|
logf func(string, ...interface{})
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2017-10-02 03:46:20 -04:00
|
|
|
type Option func(*AnsiParser)
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2017-10-02 03:46:20 -04:00
|
|
|
func WithLogf(f func(string, ...interface{})) Option {
|
|
|
|
return func(ap *AnsiParser) {
|
|
|
|
ap.logf = f
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
2017-10-02 03:46:20 -04:00
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2017-10-02 03:46:20 -04:00
|
|
|
func CreateParser(initialState string, evtHandler AnsiEventHandler, opts ...Option) *AnsiParser {
|
|
|
|
ap := &AnsiParser{
|
2015-04-16 17:01:37 -04:00
|
|
|
eventHandler: evtHandler,
|
2016-04-25 18:55:12 -04:00
|
|
|
context: &ansiContext{},
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
2017-10-02 03:46:20 -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{}) {}
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2017-10-02 03:46:20 -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,
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2017-10-02 03:46:20 -04:00
|
|
|
ap.currState = getState(initialState, ap.stateMap)
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2017-10-02 03:46:20 -04:00
|
|
|
ap.logf("CreateParser: parser %p", ap)
|
|
|
|
return ap
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func getState(name string, states []state) state {
|
2015-04-16 17:01:37 -04:00
|
|
|
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-02 03:46:20 -04:00
|
|
|
ap.logf("WARNING: newState is nil")
|
2016-04-25 18:55:12 -04:00
|
|
|
return errors.New("New state of 'nil' is invalid.")
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if newState != ap.currState {
|
|
|
|
if err := ap.changeState(newState); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (ap *AnsiParser) changeState(newState state) error {
|
2017-10-02 03:46:20 -04:00
|
|
|
ap.logf("ChangeState %s --> %s", ap.currState.Name(), newState.Name())
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
// Exit old state
|
|
|
|
if err := ap.currState.Exit(); err != nil {
|
2017-10-02 03:46:20 -04:00
|
|
|
ap.logf("Exit state '%s' failed with : '%v'", ap.currState.Name(), err)
|
2015-04-16 17:01:37 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform transition action
|
|
|
|
if err := ap.currState.Transition(newState); err != nil {
|
2017-10-02 03:46:20 -04:00
|
|
|
ap.logf("Transition from '%s' to '%s' failed with: '%v'", ap.currState.Name(), newState.Name, err)
|
2015-04-16 17:01:37 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter new state
|
|
|
|
if err := newState.Enter(); err != nil {
|
2017-10-02 03:46:20 -04:00
|
|
|
ap.logf("Enter state '%s' failed with: '%v'", newState.Name(), err)
|
2015-04-16 17:01:37 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ap.currState = newState
|
|
|
|
return nil
|
|
|
|
}
|