avoid disaster regarding setting of env variables from number tokens
This commit is contained in:
parent
7c04e3de95
commit
94c9b2beba
2 changed files with 23 additions and 3 deletions
|
|
@ -19,15 +19,25 @@ package ast
|
|||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"git.callpipe.com/aidan/shs/log"
|
||||
)
|
||||
|
||||
// Trigger this if you are using this for a shell
|
||||
var SyncTablesWithOSEnviron = false
|
||||
|
||||
type VarTable *map[string]*Token
|
||||
|
||||
func GetVar(arg string, vt VarTable) *Token {
|
||||
val, ok := (*vt)[arg]
|
||||
if !ok {
|
||||
if !SyncTablesWithOSEnviron {
|
||||
return nil
|
||||
}
|
||||
|
||||
e := os.Getenv(arg)
|
||||
if e != "" {
|
||||
t := &Token{Inner: e}
|
||||
|
|
@ -45,11 +55,20 @@ func GetVar(arg string, vt VarTable) *Token {
|
|||
return val
|
||||
}
|
||||
|
||||
// TODO: Document me :)
|
||||
// TODO: this could be much more optimal
|
||||
func SetVar(variable string, value *Token, vt VarTable) {
|
||||
(*vt)[variable] = value
|
||||
if value.Tag == NUMBER || value.Tag == STRING {
|
||||
os.Setenv(variable, value.Inner.(string))
|
||||
if SyncTablesWithOSEnviron &&
|
||||
(value.Tag == NUMBER || value.Tag == STRING) {
|
||||
token := value.Inner.(string)
|
||||
if value.Tag == NUMBER {
|
||||
// make sure its an int
|
||||
a, err := strconv.ParseFloat(token, 64)
|
||||
if err == nil && math.Mod(a, 1.0) == 0 {
|
||||
token = fmt.Sprintf("%d", int(a))
|
||||
}
|
||||
}
|
||||
os.Setenv(variable, token)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ func main() {
|
|||
funcs = stdlib.GenFuncTable()
|
||||
vars = &map[string]*ast.Token{}
|
||||
ast.InitVarTable(vars)
|
||||
ast.SyncTablesWithOSEnviron = true
|
||||
|
||||
useHist := false
|
||||
var histFile *os.File
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue