MCPcopy
hub / github.com/lmorg/murex / compile

Method compile

lang/preview.go:92–149  ·  view source on GitHub ↗
(tree *[]functions.FunctionT, procs *[]Process)

Source from the content-addressed store, hash-verified

90}
91
92func (pc *previewCacheT) compile(tree *[]functions.FunctionT, procs *[]Process) error {
93 pc.mutex.Lock()
94 defer pc.mutex.Unlock()
95
96 if pc.err != nil {
97 return pc.err
98 }
99
100 s := make([]string, len(*tree))
101 for i := range s {
102 s[i] = string((*tree)[i].Raw)
103 }
104
105 offset, err := pc.compare(s)
106 if err != nil {
107 pc.err = err
108 return err
109 }
110
111 if len(pc.raw) > 0 {
112
113 safe := parser.GetSafeCmds()
114 for i := offset + 1; i < len(*tree)-1; i++ {
115 cmd := string((*tree)[i].CommandName())
116 check:
117 switch {
118 case cmd == ExpressionFunctionName:
119 continue
120 case cmd == "exec" && !strings.HasPrefix(s[i], cmd):
121 if len((*tree)[i].Parameters) > 0 {
122 cmd = string((*tree)[i].Parameters[0])
123 goto check
124 }
125 case !strings.HasPrefix(s[i], cmd):
126 return fmt.Errorf("a command executable has changed name: %s", errPressF9)
127 case !lists.Match(safe, cmd):
128 return fmt.Errorf("a command line change has been made prior to potentially unsafe commands: %s", errPressF9)
129 }
130 }
131
132 }
133
134 pc.grow(s)
135
136 for i := 0; i < len(*procs)-1; i++ {
137 (*procs)[i].cache = new(cacheT)
138 (*procs)[i].cache.b = &pc.cache[i]
139 (*procs)[i].cache.dt = &pc.dt[i]
140 (*procs)[i].Stdout, (*procs)[i].cache.tee.stdout = streams.NewTee((*procs)[i].Stdout)
141 (*procs)[i].Stderr, (*procs)[i].cache.tee.stderr = streams.NewTee((*procs)[i].Stderr)
142
143 if i <= offset {
144 (*procs)[i].cache.use = true
145 }
146 }
147
148 return nil
149}

Callers 1

ExecuteMethod · 0.80

Calls 8

compareMethod · 0.95
growMethod · 0.95
GetSafeCmdsFunction · 0.92
MatchFunction · 0.92
NewTeeFunction · 0.92
CommandNameMethod · 0.80
LockMethod · 0.45
UnlockMethod · 0.45

Tested by

no test coverage detected