diff --git a/cmd/shs_repl.go b/cmd/shs_repl.go index 2f32bca..ea6007c 100644 --- a/cmd/shs_repl.go +++ b/cmd/shs_repl.go @@ -18,21 +18,26 @@ package main import ( - "os" "fmt" "strconv" "github.com/chzyer/readline" "gitlab.com/whom/shs/ast" "gitlab.com/whom/shs/log" - "gitlab.com/whom/shs/stdlib" + "gitlab.com/whom/shs/config" ) const ( def_prompt string = "λ " ) -func setLogLvl() { - loglvl := os.Getenv("SH_LOGGING") +func setLogLvl(vars ast.VarTable) { + var loglvl string + + loglvl_t := ast.GetVar("SH_LOGGING", vars) + if loglvl_t != nil { + loglvl = loglvl_t.Value() + } + if loglvl != "" { llvl, err := strconv.ParseInt(loglvl, 10, 8) if err != nil { @@ -46,23 +51,28 @@ func setLogLvl() { } func main() { - debug := os.Getenv("SH_DEBUG_MODE") - hist := os.Getenv("SH_HIST_FILE") - prompt := os.Getenv("SHS_SH_PROMPT") + var prompt string + var debug string + var hist string - var vars ast.VarTable - var funcs ast.FuncTable - - funcs = stdlib.GenFuncTable() - vars = &map[string]*ast.Token{} - - ast.InitVarTable(vars) ast.SyncTablesWithOSEnviron = true ast.ExecWhenFuncUndef = true - var err error + vars, funcs := config.InitFromConfig(".shsrc") + debug_t := ast.GetVar("SH_DEBUG_MODE", vars) + if debug_t != nil { + debug = debug_t.Value() + } - if prompt == "" { + hist_t := ast.GetVar("SH_HIST_FILE", vars) + if hist_t != nil { + hist = hist_t.Value() + } + + prompt_t := ast.GetVar("SHS_SH_PROMPT", vars) + if prompt_t != nil { + prompt = prompt_t.Value() + } else { prompt = def_prompt } @@ -79,7 +89,7 @@ func main() { } for { - setLogLvl() + setLogLvl(vars) text, err := rl.Readline() if err != nil { log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl") diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..6ce7d51 --- /dev/null +++ b/config/config.go @@ -0,0 +1,66 @@ +/* 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 . + */ + +package config + +import ( + "os" + "io" + "bufio" + "gitlab.com/whom/shs/log" + "gitlab.com/whom/shs/ast" + "gitlab.com/whom/shs/stdlib" +) + +func InitFromConfig(configFile string) (ast.VarTable, ast.FuncTable) { + funcs := stdlib.GenFuncTable() + vars := &map[string]*ast.Token{} + + ast.InitVarTable(vars) + + p := ast.GetVar("HOME", vars) + configFile = p.Value() + "/" + configFile + + cfile, err := os.Open(configFile) + if err != nil { + log.Log(log.DEBUG, + "unable to open config file: " + err.Error(), + "config") + return vars, funcs + } + + r := bufio.NewReader(cfile) + text, err := r.ReadString('\n') + for err != io.EOF { + if err != nil { + log.Log(log.ERR, + "unable to read from config file: " + err.Error(), + "config") + break + } + + // Eval lines in config + ast.Lex(text).Eval(funcs, vars, false) + text, err = r.ReadString('\n') + } + + log.Log(log.DEBUG, + "config file fully evaluated", + "config") + cfile.Close() + return vars, funcs +}