catch SIGTERM meant for running process
This commit is contained in:
parent
3c2dde3665
commit
d17e975cc8
3 changed files with 36 additions and 5 deletions
110
cmd/repl.go
110
cmd/repl.go
|
|
@ -1,110 +0,0 @@
|
|||
/* 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"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"github.com/chzyer/readline"
|
||||
"git.callpipe.com/aidan/shs/ast"
|
||||
"git.callpipe.com/aidan/shs/log"
|
||||
"git.callpipe.com/aidan/shs/stdlib"
|
||||
)
|
||||
|
||||
const (
|
||||
def_prompt string = "λ "
|
||||
)
|
||||
|
||||
func setLogLvl() {
|
||||
loglvl := os.Getenv("SH_LOGGING")
|
||||
if loglvl != "" {
|
||||
llvl, err := strconv.ParseInt(loglvl, 10, 8)
|
||||
if err != nil {
|
||||
log.Log(log.ERR,
|
||||
"couldnt parse log level: " + err.Error(),
|
||||
"init")
|
||||
} else {
|
||||
log.SetLogLvl(llvl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
debug := os.Getenv("SH_DEBUG_MODE")
|
||||
hist := os.Getenv("SH_HIST_FILE")
|
||||
prompt := os.Getenv("SHS_SH_PROMPT")
|
||||
|
||||
var vars ast.VarTable
|
||||
var funcs ast.FuncTable
|
||||
|
||||
funcs = stdlib.GenFuncTable()
|
||||
vars = &map[string]*ast.Token{}
|
||||
ast.InitVarTable(vars)
|
||||
ast.SyncTablesWithOSEnviron = true
|
||||
|
||||
var err error
|
||||
|
||||
if prompt == "" {
|
||||
prompt = def_prompt
|
||||
}
|
||||
|
||||
rl, err := readline.NewEx(&readline.Config{
|
||||
Prompt: prompt,
|
||||
HistoryFile: hist,
|
||||
InterruptPrompt: "^C",
|
||||
})
|
||||
|
||||
defer rl.Close()
|
||||
if err != nil {
|
||||
log.Log(log.ERR, "Couldnt initialize readline: " + err.Error(), "repl")
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
setLogLvl()
|
||||
text, err := rl.Readline()
|
||||
if err != nil {
|
||||
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
|
||||
}
|
||||
|
||||
userInput := ast.Lex(text)
|
||||
if userInput == nil {
|
||||
// errors handled in Lex
|
||||
// TODO: return an error instead
|
||||
continue
|
||||
}
|
||||
|
||||
if debug != "" {
|
||||
ast.PrintSExprsIndividually(userInput)
|
||||
}
|
||||
|
||||
result, unwrap := userInput.Eval(funcs, vars)
|
||||
if result != nil {
|
||||
if result.Tag == ast.LIST && unwrap {
|
||||
result = result.Inner.(*ast.Token)
|
||||
}
|
||||
|
||||
for i := result; i != nil; i = i.Next {
|
||||
fmt.Printf(i.String() + " ")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue