Skip to content

Commit

Permalink
alias展開済みフラグ等がグローバルで、インタプリタが再入可能になっていない問題を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
hymkor committed Oct 8, 2014
1 parent 00835eb commit a8f87db
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 89 deletions.
37 changes: 14 additions & 23 deletions alias/alias.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package alias

import "bytes"
import "os/exec"
import "regexp"
import "strconv"
import "strings"
import "io"

import "../interpreter"

type Callable interface {
String() string
Call(cmd *exec.Cmd) (interpreter.NextT, error)
Call(cmd *interpreter.Interpreter) (interpreter.NextT, error)
}

type AliasFunc struct {
Expand All @@ -26,9 +24,7 @@ func (this *AliasFunc) String() string {
return this.BaseStr
}

var expanded = false

func (this *AliasFunc) Call(cmd *exec.Cmd) (next interpreter.NextT, err error) {
func (this *AliasFunc) Call(cmd *interpreter.Interpreter) (next interpreter.NextT, err error) {
isReplaced := false
cmdline := paramMatch.ReplaceAllStringFunc(this.BaseStr, func(s string) string {
if s == "$*" {
Expand All @@ -52,14 +48,9 @@ func (this *AliasFunc) Call(cmd *exec.Cmd) (next interpreter.NextT, err error) {
buffer.WriteString(quoteAndJoin(cmd.Args[1:]))
cmdline = buffer.String()
}
stdio := interpreter.Stdio{
Stdin: cmd.Stdin,
Stdout: cmd.Stdout,
Stderr: cmd.Stderr,
}
expanded = true
next, err = interpreter.Interpret(cmdline, &stdio)
expanded = false
it := cmd.Clone()
it.HookCount = cmd.HookCount + 1
next, err = it.Interpret(cmdline)
return
}

Expand All @@ -81,26 +72,26 @@ func quoteAndJoin(list []string) string {

var nextHook interpreter.HookT

func hook(cmd *exec.Cmd, IsBackground bool, closer io.Closer) (interpreter.NextT, error) {
if expanded {
return nextHook(cmd, IsBackground, closer)
func hook(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
if cmd.HookCount > 0 {
return nextHook(cmd)
}
callee, ok := Table[strings.ToLower(cmd.Args[0])]
if !ok {
return nextHook(cmd, IsBackground, closer)
return nextHook(cmd)
}
if IsBackground {
if cmd.IsBackGround {
go func() {
callee.Call(cmd)
if closer != nil {
closer.Close()
if cmd.Closer != nil {
cmd.Closer.Close()
}
}()
return interpreter.CONTINUE, nil
} else {
next, err := callee.Call(cmd)
if next != interpreter.THROUGH && closer != nil {
closer.Close()
if next != interpreter.THROUGH && cmd.Closer != nil {
cmd.Closer.Close()
}
return next, err
}
Expand Down
3 changes: 1 addition & 2 deletions commands/alias.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package commands

import "fmt"
import "os/exec"
import "strings"

import "../alias"
import "../interpreter"

func cmd_alias(cmd *exec.Cmd) (interpreter.NextT, error) {
func cmd_alias(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
if len(cmd.Args) <= 1 {
for key, val := range alias.Table {
fmt.Fprintf(cmd.Stdout, "%s=%s\n", key, val.String())
Expand Down
3 changes: 1 addition & 2 deletions commands/cd.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package commands

import "os"
import "os/exec"

import "../dos"
import "../interpreter"

var prevDir string

func cmd_cd(cmd *exec.Cmd) (interpreter.NextT, error) {
func cmd_cd(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
if len(cmd.Args) >= 2 {
prevDir_, err := os.Getwd()
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions commands/cls.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package commands

import "os/exec"

import "../conio"
import "../interpreter"

func cmd_cls(cmd *exec.Cmd) (interpreter.NextT, error) {
func cmd_cls(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
conio.Cls()
return interpreter.CONTINUE, nil
}
22 changes: 13 additions & 9 deletions commands/commands.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package commands

import "io"
import "os/exec"
import "strings"

import "../history"
import "../interpreter"
import "../dos"

var buildInCmd = map[string]func(cmd *exec.Cmd) (interpreter.NextT, error){
var buildInCmd = map[string]func(cmd *interpreter.Interpreter) (interpreter.NextT, error){
".": cmd_source,
"alias": cmd_alias,
"cd": cmd_cd,
Expand All @@ -21,7 +19,7 @@ var buildInCmd = map[string]func(cmd *exec.Cmd) (interpreter.NextT, error){
"source": cmd_source,
}

func Exec(cmd *exec.Cmd, IsBackground bool, closer io.Closer) (interpreter.NextT, error) {
func Exec(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
name := strings.ToLower(cmd.Args[0])
if len(name) == 2 && strings.HasSuffix(name, ":") {
err := dos.Chdrive(name)
Expand All @@ -41,15 +39,21 @@ func Exec(cmd *exec.Cmd, IsBackground bool, closer io.Closer) (interpreter.NextT
}
}
cmd.Args = newArgs
if IsBackground {
go func(cmd *exec.Cmd, closer io.Closer) {
if cmd.IsBackGround {
go func(cmd *interpreter.Interpreter) {
function(cmd)
closer.Close()
}(cmd, closer)
if cmd.Closer != nil {
cmd.Closer.Close()
cmd.Closer = nil
}
}(cmd)
return interpreter.CONTINUE, nil
} else {
next, err := function(cmd)
closer.Close()
if cmd.Closer != nil {
cmd.Closer.Close()
cmd.Closer = nil
}
return next, err
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions commands/exit.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package commands

import "os/exec"
import "../interpreter"

func cmd_exit(cmd *exec.Cmd) (interpreter.NextT, error) {
func cmd_exit(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
return interpreter.SHUTDOWN, nil
}
4 changes: 1 addition & 3 deletions commands/ls.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package commands

import "os/exec"

import "github.com/shiena/ansicolor"

import "../interpreter"
import "./ls"

func cmd_ls(cmd *exec.Cmd) (interpreter.NextT, error) {
func cmd_ls(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
return interpreter.CONTINUE,
ls.Main(cmd.Args[1:], ansicolor.NewAnsiColorWriter(cmd.Stdout))
}
3 changes: 1 addition & 2 deletions commands/pwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package commands

import "fmt"
import "os"
import "os/exec"
import "../interpreter"

func cmd_pwd(cmd *exec.Cmd) (interpreter.NextT, error) {
func cmd_pwd(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
wd, _ := os.Getwd()
fmt.Fprintln(cmd.Stdout, wd)
return interpreter.CONTINUE, nil
Expand Down
3 changes: 1 addition & 2 deletions commands/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package commands

import "fmt"
import "os"
import "os/exec"
import "strings"

import "../interpreter"

func cmd_set(cmd *exec.Cmd) (interpreter.NextT, error) {
func cmd_set(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
if len(cmd.Args) <= 1 {
for _, val := range os.Environ() {
fmt.Fprintln(cmd.Stdout, val)
Expand Down
2 changes: 1 addition & 1 deletion commands/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "strings"

import "../interpreter"

func cmd_source(cmd *exec.Cmd) (interpreter.NextT, error) {
func cmd_source(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
if len(cmd.Args) < 2 {
return interpreter.CONTINUE, nil
}
Expand Down
3 changes: 1 addition & 2 deletions history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import "bufio"
import "bytes"
import "fmt"
import "os"
import "os/exec"
import "strconv"
import "strings"

Expand Down Expand Up @@ -220,7 +219,7 @@ func insertHisotry(buffer *bytes.Buffer, reader *strings.Reader, history1 string
}
}

func CmdHistory(cmd *exec.Cmd) (interpreter.NextT, error) {
func CmdHistory(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
var num int
if len(cmd.Args) >= 2 {
num64, err := strconv.ParseInt(cmd.Args[1], 0, 32)
Expand Down
73 changes: 42 additions & 31 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@ const (
SHUTDOWN NextT = 2
)

type Stdio struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
type Interpreter struct {
exec.Cmd
HookCount int
IsBackGround bool
Closer io.Closer
}

func New() *Interpreter {
return new(Interpreter)
}

func (this *Interpreter) Clone() *Interpreter {
rv := new(Interpreter)
rv.Stdout = this.Stdout
rv.Stderr = this.Stderr
rv.Stdin = this.Stdin
rv.HookCount = this.HookCount
return this
}

type ArgsHookT func(args []string) []string
Expand All @@ -30,9 +44,9 @@ func SetArgsHook(argsHook_ ArgsHookT) (rv ArgsHookT) {
return
}

type HookT func(*exec.Cmd, bool, io.Closer) (NextT, error)
type HookT func(*Interpreter) (NextT, error)

var hook = func(*exec.Cmd, bool, io.Closer) (NextT, error) {
var hook = func(*Interpreter) (NextT, error) {
return THROUGH, nil
}

Expand All @@ -44,36 +58,27 @@ func SetHook(hook_ HookT) (rv HookT) {
var errorStatusPattern = regexp.MustCompile("^exit status ([0-9]+)")
var ErrorLevel string

func Interpret(text string, stdio *Stdio) (NextT, error) {
statement := Parse(text)
return InterpretStatement(&statement, stdio)
}
func InterpretStatement(statements *[][]StatementT, stdio *Stdio) (NextT, error) {
for _, pipeline := range *statements {
func (this *Interpreter) Interpret(text string) (NextT, error) {
statements := Parse(text)
for _, pipeline := range statements {
var pipeIn *os.File = nil
for _, state := range pipeline {
//fmt.Println(state)
var cmd exec.Cmd
if stdio == nil {
cmd := this.Clone()
if this.Stderr != nil {
cmd.Stdin = this.Stdin
} else {
cmd.Stdin = os.Stdin
}
if this.Stdout != nil {
cmd.Stdout = this.Stdout
} else {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
if this.Stderr != nil {
cmd.Stderr = this.Stderr
} else {
if stdio.Stderr != nil {
cmd.Stdin = stdio.Stdin
} else {
cmd.Stdin = os.Stdin
}
if stdio.Stdout != nil {
cmd.Stdout = stdio.Stdout
} else {
cmd.Stdout = os.Stdout
}
if stdio.Stderr != nil {
cmd.Stderr = stdio.Stderr
} else {
cmd.Stderr = os.Stderr
}
cmd.Stderr = os.Stderr
}
if pipeIn != nil {
cmd.Stdin = pipeIn
Expand Down Expand Up @@ -134,7 +139,9 @@ func InterpretStatement(statements *[][]StatementT, stdio *Stdio) (NextT, error)
state.Argv = argsHook(state.Argv)
}
cmd.Args = state.Argv
whatToDo, err = hook(&cmd, isBackGround, pipeOut)
cmd.IsBackGround = isBackGround
cmd.Closer = pipeOut
whatToDo, err = hook(cmd)
if whatToDo == THROUGH {
cmd.Path, err = exec.LookPath(state.Argv[0])
if err == nil {
Expand Down Expand Up @@ -172,3 +179,7 @@ func InterpretStatement(statements *[][]StatementT, stdio *Stdio) (NextT, error)
}
return CONTINUE, nil
}

func Run(text string) (NextT, error) {
return New().Interpret(text)
}
Loading

0 comments on commit a8f87db

Please sign in to comment.