SHS/cmd/shs.go

183 lines
4.5 KiB
Go
Raw Normal View History

2020-06-21 01:30:54 -07:00
/* SHS: Syntactically Homogeneous Shell
* Copyright (C) 2019 Aidan Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"os"
2020-06-21 01:30:54 -07:00
"fmt"
"strconv"
"github.com/candid82/liner"
2020-06-28 13:47:44 -07:00
"gitlab.com/whom/shs/ast"
"gitlab.com/whom/shs/log"
2020-07-18 10:44:34 -07:00
"gitlab.com/whom/shs/util"
2020-07-22 00:22:39 -07:00
"gitlab.com/whom/shs/stdlib"
2020-07-03 02:02:12 -07:00
"gitlab.com/whom/shs/config"
2020-06-21 01:30:54 -07:00
)
const (
2020-06-27 21:27:33 -07:00
def_prompt string = "λ "
2020-06-21 01:30:54 -07:00
)
// useful for when input contains escape sequences
// not checking delims cause thats up to the user who defines their prompts
func parseString(in string) string {
in = "\"" + in + "\""
out, err := strconv.Unquote(in)
if err != nil {
log.Log(log.ERR,
"Couldnt parse (pre?)prompt",
"init")
return ""
}
return out
}
2020-07-03 02:02:12 -07:00
func setLogLvl(vars ast.VarTable) {
var loglvl string
loglvl_t := ast.GetVar("SH_LOGGING", vars)
if loglvl_t != nil {
loglvl = loglvl_t.Value()
}
2020-06-21 01:30:54 -07:00
if loglvl != "" {
2020-06-21 11:11:57 -07:00
llvl, err := strconv.ParseInt(loglvl, 10, 8)
if err != nil {
log.Log(log.ERR,
"couldnt parse log level: " + err.Error(),
2020-06-21 01:30:54 -07:00
"init")
} else {
log.SetLogLvl(llvl)
}
}
}
func main() {
2020-07-03 02:02:12 -07:00
var prompt string
var debug string
var hist string
no_hist := false
2020-06-21 01:30:54 -07:00
ast.SyncTablesWithOSEnviron = true
ast.ExecWhenFuncUndef = true
2020-07-22 21:56:31 -07:00
stdlib.InitShellFeatures()
defer stdlib.TeardownShell()
2020-07-03 02:02:12 -07:00
vars, funcs := config.InitFromConfig(".shsrc")
debug_t := ast.GetVar("SH_DEBUG_MODE", vars)
if debug_t != nil {
debug = debug_t.Value()
}
hist_t := ast.GetVar("SH_HIST_FILE", vars)
if hist_t != nil {
hist = hist_t.Value()
} else {
no_hist = true
}
dyn_prompt := ast.GetFunction("_SH_PROMPT", funcs)
if dyn_prompt == nil || dyn_prompt.Args != 0 {
dyn_prompt = nil
2020-07-03 02:02:12 -07:00
}
2020-06-21 01:30:54 -07:00
prompt_t := ast.GetVar("SHS_STATIC_PROMPT", vars)
2020-07-03 02:02:12 -07:00
if prompt_t != nil {
prompt = parseString(prompt_t.Value())
2020-07-03 02:02:12 -07:00
} else {
2020-06-21 01:30:54 -07:00
prompt = def_prompt
}
line := liner.NewLiner()
defer line.Close()
if !liner.TerminalSupported() {
log.Log(log.ERR,
"Terminal unsupported, continuing in dummy mode!",
"init")
}
line.SetCtrlCAborts(true)
2020-07-18 10:44:34 -07:00
line.SetCompleter(func(line string) (c []string) {
2020-07-18 14:19:58 -07:00
return util.ShellCompleter(line, vars, funcs)
2020-07-18 10:44:34 -07:00
})
2020-06-27 21:27:33 -07:00
var histFile *os.File
var err error
if !no_hist {
histFile, err = os.Open(hist)
if err == nil {
line.ReadHistory(histFile)
} else {
log.Log(log.ERR,
"couldnt read history: " + err.Error(),
"repl")
}
defer histFile.Close()
2020-06-27 21:27:33 -07:00
}
2020-06-21 01:30:54 -07:00
for {
2020-07-03 02:02:12 -07:00
setLogLvl(vars)
2020-07-23 19:54:36 -07:00
stdlib.CheckBGProcs()
var prePrompt string
if dyn_prompt != nil {
p_tok := dyn_prompt.CallFunction(nil, vars, funcs)
if p_tok != nil && p_tok.Tag == ast.STRING {
prePrompt = parseString(p_tok.Value())
}
}
fmt.Printf(prePrompt)
text, err := line.Prompt(prompt)
if err != nil && err != liner.ErrPromptAborted{
2020-07-24 20:37:46 -07:00
// must be a better way to do this check
if err.Error() == "EOF" {
return
}
2020-06-21 11:11:57 -07:00
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
2020-07-18 10:44:34 -07:00
continue
2020-06-21 01:30:54 -07:00
}
2020-07-18 10:44:34 -07:00
line.AppendHistory(text)
2020-06-21 01:30:54 -07:00
userInput := ast.Lex(text)
if userInput == nil {
// errors handled in Lex
// TODO: return an error instead
continue
}
if debug != "" {
2020-06-21 11:11:57 -07:00
ast.PrintSExprsIndividually(userInput)
2020-06-21 01:30:54 -07:00
}
result := userInput.Eval(funcs, vars, false)
if result != nil {
for i := result; i != nil; i = i.Next {
fmt.Printf(i.String() + " ")
}
}
fmt.Printf("\n")
2020-06-21 01:30:54 -07:00
}
}