stdlib Call, fg, bg, $, and cd functions. wow
This commit is contained in:
parent
94c9b2beba
commit
af184d9176
6 changed files with 272 additions and 33 deletions
171
stdlib/call.go
Normal file
171
stdlib/call.go
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
/* 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 stdlib
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"os/exec"
|
||||
"git.callpipe.com/aidan/shs/ast"
|
||||
"git.callpipe.com/aidan/shs/log"
|
||||
)
|
||||
|
||||
var bgProcs = make([]*exec.Cmd, 0)
|
||||
|
||||
func call(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
path, err := exec.LookPath(in.Inner.(string))
|
||||
if err != nil {
|
||||
log.Log(log.ERR, "Couldnt exec " + in.Inner.(string) + ", file not found", "call")
|
||||
return nil
|
||||
}
|
||||
|
||||
args := []string{}
|
||||
for i := in.Next; i != nil; i = i.Next {
|
||||
if i.Tag == ast.LIST {
|
||||
log.Log(log.ERR, "Couldnt exec " + path + ", element in arguments is a list", "call")
|
||||
return nil
|
||||
}
|
||||
|
||||
args = append(args, i.Inner.(string))
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
if len(args) > 0 {
|
||||
cmd = exec.Command(path, args...)
|
||||
} else {
|
||||
cmd = exec.Command(path)
|
||||
}
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdin = os.Stdin
|
||||
|
||||
exit := 0
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
if exitError, ok := err.(*exec.ExitError); ok {
|
||||
exit = exitError.ExitCode()
|
||||
} else {
|
||||
log.Log(log.ERR, "Execution step returned unparsable error: " + err.Error(), "call")
|
||||
}
|
||||
}
|
||||
|
||||
return &ast.Token{Tag: ast.NUMBER, Inner: fmt.Sprintf("%d", exit)}
|
||||
}
|
||||
|
||||
func bgcall(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
path, err := exec.LookPath(in.Inner.(string))
|
||||
if err != nil {
|
||||
log.Log(log.ERR, "Couldnt exec " + in.Inner.(string) + ", file not found", "call")
|
||||
return nil
|
||||
}
|
||||
|
||||
args := []string{}
|
||||
for i := in.Next; i != nil; i = i.Next {
|
||||
if i.Tag == ast.LIST {
|
||||
log.Log(log.ERR, "Couldnt exec " + path + ", element in arguments is a list", "call")
|
||||
return nil
|
||||
}
|
||||
|
||||
args = append(args, i.Inner.(string))
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
if len(args) > 0 {
|
||||
cmd = exec.Command(path, args...)
|
||||
} else {
|
||||
cmd = exec.Command(path)
|
||||
}
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
bgProcs = append(bgProcs, cmd)
|
||||
cmd.Start()
|
||||
return nil
|
||||
}
|
||||
|
||||
func fg(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
if len(bgProcs) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
cmd := bgProcs[0]
|
||||
bgProcs = bgProcs[1:]
|
||||
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stdin = os.Stdin
|
||||
exit := 0
|
||||
err := cmd.Wait()
|
||||
if err != nil {
|
||||
if exitError, ok := err.(*exec.ExitError); ok {
|
||||
exit = exitError.ExitCode()
|
||||
} else {
|
||||
log.Log(log.ERR, "Execution step returned unparsable error: " + err.Error(), "call")
|
||||
}
|
||||
}
|
||||
|
||||
return &ast.Token{Tag: ast.NUMBER, Inner: fmt.Sprintf("%d", exit)}
|
||||
}
|
||||
|
||||
func read_cmd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var out bytes.Buffer
|
||||
path, err := exec.LookPath(in.Inner.(string))
|
||||
if err != nil {
|
||||
log.Log(log.ERR, "Couldnt exec " + in.Inner.(string) + ", file not found", "call")
|
||||
return nil
|
||||
}
|
||||
|
||||
args := []string{}
|
||||
for i := in.Next; i != nil; i = i.Next {
|
||||
if i.Tag == ast.LIST {
|
||||
log.Log(log.ERR, "Couldnt exec " + path + ", element in arguments is a list", "call")
|
||||
return nil
|
||||
}
|
||||
|
||||
args = append(args, i.Inner.(string))
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
if len(args) > 0 {
|
||||
cmd = exec.Command(path, args...)
|
||||
} else {
|
||||
cmd = exec.Command(path)
|
||||
}
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdin = os.Stdin
|
||||
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
log.Log(log.ERR, err.Error(), "$")
|
||||
}
|
||||
|
||||
return &ast.Token{Tag: ast.STRING, Inner: out.String()}
|
||||
|
||||
}
|
||||
37
stdlib/filesys.go
Normal file
37
stdlib/filesys.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/* 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 stdlib
|
||||
|
||||
import (
|
||||
"os"
|
||||
"git.callpipe.com/aidan/shs/ast"
|
||||
"git.callpipe.com/aidan/shs/log"
|
||||
)
|
||||
|
||||
func cd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
if in.Tag == ast.LIST {
|
||||
log.Log(log.ERR, "Couldnt change dir to a list", "cd")
|
||||
return nil
|
||||
}
|
||||
|
||||
err := os.Chdir(in.Inner.(string))
|
||||
if err != nil {
|
||||
log.Log(log.ERR, err.Error(), "cd")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -54,14 +54,16 @@ func l_append(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Tok
|
|||
iter := &i
|
||||
if *iter == nil {
|
||||
src.Inner = input.Next
|
||||
return src
|
||||
}
|
||||
src.Next = nil
|
||||
|
||||
for (*iter).Next != nil {
|
||||
iter = &((*iter).Next)
|
||||
} else {
|
||||
for (*iter).Next != nil {
|
||||
iter = &((*iter).Next)
|
||||
}
|
||||
|
||||
(*iter).Next = input.Next
|
||||
input.Next = nil
|
||||
}
|
||||
(*iter).Next = input.Next
|
||||
input.Next = nil
|
||||
|
||||
return src
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,12 +59,54 @@ func GenFuncTable() ast.FuncTable {
|
|||
Args: -1,
|
||||
},
|
||||
|
||||
"div": &ast.Function{
|
||||
"/": &ast.Function{
|
||||
Function: div,
|
||||
Name: "div",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"l": &ast.Function{
|
||||
Function: call,
|
||||
Name: "call",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"bg": &ast.Function{
|
||||
Function: bgcall,
|
||||
Name: "background call",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"fg": &ast.Function{
|
||||
Function: fg,
|
||||
Name: "foreground",
|
||||
TimesCalled: 0,
|
||||
Args: 0,
|
||||
},
|
||||
|
||||
"cd": &ast.Function{
|
||||
Function: cd,
|
||||
Name: "changedir",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"$": &ast.Function{
|
||||
Function: read_cmd,
|
||||
Name: "read cmd",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"concat": &ast.Function{
|
||||
Function: concat,
|
||||
Name:"concatenate",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
}
|
||||
|
||||
return stdlib
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue