-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.go
92 lines (81 loc) · 2.1 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
type DebugSet struct {
CFileName string
InputFiles []string
}
func (debugObj *DebugSet) storeInputFiles(kadaiNum string) {
kadaiLevel := strings.TrimPrefix(debugObj.CFileName, fmt.Sprintf("kadai%v", kadaiNum))
kadaiLevel = strings.TrimSuffix(kadaiLevel, ".c")
debugObj.InputFiles, _ = filepath.Glob(fmt.Sprintf("./inputFiles/input%v[0-9].txt", kadaiLevel))
}
func (debugObj *DebugSet) debug() error {
executable := strings.TrimSuffix(debugObj.CFileName, ".c")
// compile
cmdCompile := exec.Command("gcc", "-Wall", "-o", executable, debugObj.CFileName, "-lm")
cmdCompile.Stdout = os.Stdout
cmdCompile.Stderr = os.Stderr
fmt.Printf("compile: gcc -Wall -o %v %v -lm\n", executable, debugObj.CFileName)
err := cmdCompile.Run()
if err != nil {
return err
}
// execute
for _, file := range debugObj.InputFiles {
fp, err := os.Open("./" + file)
if err != nil {
return err
}
defer fp.Close()
cmdExe := exec.Command("./" + executable)
cmdExe.Stdin = fp
cmdExe.Stdout = os.Stdout
cmdExe.Stderr = os.Stderr
fmt.Printf("Output of %v:\n", file)
err = cmdExe.Run()
if err != nil {
return err
}
}
return nil
}
func debug(args []string) error {
kadaiNum := getKadaiNum() // ex. kadai[01]a
var debugObjs []DebugSet
// if no args, debug all c files.
if len(args) == 0 {
cFiles, _ := filepath.Glob(fmt.Sprintf("kadai%v[a-z].c", kadaiNum))
for _, file := range cFiles {
debugObjs = append(debugObjs, DebugSet{CFileName: file})
}
} else {
for _, arg := range args {
cFiles, _ := filepath.Glob(fmt.Sprintf("kadai%v%v.c", kadaiNum, arg))
// if no cFiles, ignore it.
if len(cFiles) == 0 {
fmt.Printf("kadai%v%v.cは存在しません\n", kadaiNum, arg)
continue
}
debugObjs = append(debugObjs, DebugSet{CFileName: cFiles[0]})
}
}
var tmp []DebugSet
for _, debugObj := range debugObjs {
debugObj.storeInputFiles(kadaiNum)
tmp = append(tmp, debugObj)
}
debugObjs = tmp
for _, debugObj := range debugObjs {
err := debugObj.debug()
if err != nil {
return err
}
}
return nil
}