load from config file
This commit is contained in:
parent
0d7eb4139b
commit
f98cd75160
2 changed files with 93 additions and 17 deletions
|
|
@ -18,21 +18,26 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"github.com/chzyer/readline"
|
"github.com/chzyer/readline"
|
||||||
"gitlab.com/whom/shs/ast"
|
"gitlab.com/whom/shs/ast"
|
||||||
"gitlab.com/whom/shs/log"
|
"gitlab.com/whom/shs/log"
|
||||||
"gitlab.com/whom/shs/stdlib"
|
"gitlab.com/whom/shs/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
def_prompt string = "λ "
|
def_prompt string = "λ "
|
||||||
)
|
)
|
||||||
|
|
||||||
func setLogLvl() {
|
func setLogLvl(vars ast.VarTable) {
|
||||||
loglvl := os.Getenv("SH_LOGGING")
|
var loglvl string
|
||||||
|
|
||||||
|
loglvl_t := ast.GetVar("SH_LOGGING", vars)
|
||||||
|
if loglvl_t != nil {
|
||||||
|
loglvl = loglvl_t.Value()
|
||||||
|
}
|
||||||
|
|
||||||
if loglvl != "" {
|
if loglvl != "" {
|
||||||
llvl, err := strconv.ParseInt(loglvl, 10, 8)
|
llvl, err := strconv.ParseInt(loglvl, 10, 8)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -46,23 +51,28 @@ func setLogLvl() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
debug := os.Getenv("SH_DEBUG_MODE")
|
var prompt string
|
||||||
hist := os.Getenv("SH_HIST_FILE")
|
var debug string
|
||||||
prompt := os.Getenv("SHS_SH_PROMPT")
|
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.SyncTablesWithOSEnviron = true
|
||||||
ast.ExecWhenFuncUndef = 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
|
prompt = def_prompt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,7 +89,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
setLogLvl()
|
setLogLvl(vars)
|
||||||
text, err := rl.Readline()
|
text, err := rl.Readline()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
|
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
|
||||||
|
|
|
||||||
66
config/config.go
Normal file
66
config/config.go
Normal file
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue