SHS/config/config.go
2020-07-03 02:02:12 -07:00

66 lines
1.8 KiB
Go

/* 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
}