-
Notifications
You must be signed in to change notification settings - Fork 249
Add scripting, using Cub #75
Changes from 4 commits
b71ca7e
3e5c199
d9469d8
3efef61
ae0c08a
52c1bde
2fffdc4
31af3af
d9018d8
58251e3
0497a67
0eeeea1
4d81534
350cb3f
a1e9f43
6113745
4ab52c6
334b81c
6de7154
e213773
9c431ee
2657938
00cabba
843ecc0
74f409d
23b4c0d
be6138c
25baa7d
7397a45
a30cfac
e8595a3
8f43801
de10325
f472b27
fbdd9e5
bf37aef
fa639a2
1bc31b8
f0d8d88
1ed6881
47dc405
af286a4
ffd43f9
07c6f58
826de69
714f6dc
3803d0f
a1afac1
8221a86
4eed964
4c46f61
4d84b8a
6380233
d3ce647
046a8a0
adee51d
dcfbe1d
38e956c
84bba0c
dfbd0b1
69c233b
44e6d8a
73cf0b0
7bd0313
8c51310
fde9985
d10471b
920a3e8
d001003
681b33b
2d9db7e
7e8c10c
bb2d54c
333086c
09e7d55
7e8139d
8865972
329d780
38ae772
f3623ee
6be15fa
4bbc75c
a42f4db
a8f514c
a37c6a7
fcdf334
546d9d1
c1da715
49dcd77
e8bbd39
c54edd6
7677a52
30cf8bb
4c535cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// | ||
// Cub.swift | ||
// OpenTerm | ||
// | ||
// Created by Louis D'hauwe on 03/02/2018. | ||
// Copyright © 2018 Silver Fox. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Cub | ||
import ios_system | ||
|
||
public func cub(argc: Int32, argv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?) -> Int32 { | ||
|
||
guard argc == 2 else { | ||
fputs("Usage: cub script.cub\n", thread_stderr) | ||
return 1 | ||
} | ||
|
||
guard let fileName = argv?[1] else { | ||
fputs("Usage: cub script.cub\n", thread_stderr) | ||
return 1 | ||
} | ||
|
||
let path = String(cString: fileName) | ||
|
||
guard FileManager.default.fileExists(atPath: path) else { | ||
fputs("Missing file \(path)\n", thread_stderr) | ||
return 1 | ||
} | ||
|
||
let url = URL(fileURLWithPath: path) | ||
|
||
guard let data = FileManager.default.contents(atPath: url.path) else { | ||
fputs("Missing file \(path)\n", thread_stderr) | ||
return 1 | ||
} | ||
|
||
guard let source = String.init(data: data, encoding: .utf8) else { | ||
fputs("Missing file \(path)\n", thread_stderr) | ||
return 1 | ||
} | ||
|
||
let runner = Runner(logDebug: true, logTime: false) | ||
|
||
runner.registerExternalFunction(name: "print", argumentNames: ["input"], returns: false) { (arguments, callback) in | ||
|
||
for (name, arg) in arguments { | ||
fputs("\(arg)\n", thread_stdout) | ||
} | ||
|
||
callback(nil) | ||
return | ||
} | ||
|
||
runner.registerExternalFunction(name: "exec", argumentNames: ["command"], returns: true) { (arguments, callback) in | ||
|
||
var arguments = arguments | ||
|
||
guard let command = arguments.removeValue(forKey: "command") else { | ||
callback(.number(1)) | ||
return | ||
} | ||
|
||
guard case let .string(commandStr) = command else { | ||
callback(.number(1)) | ||
return | ||
} | ||
|
||
print("run command: \(commandStr)") | ||
|
||
executor.dispatch(commandStr, callback: { (code) in | ||
|
||
print("did run command: \(commandStr) -> \(code)") | ||
|
||
DispatchQueue.main.async { | ||
|
||
callback(.number(Double(code))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we not care about the output of the command, just the return code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be better to create a class conforming to |
||
} | ||
|
||
}) | ||
|
||
} | ||
|
||
runner.registerExternalFunction(name: "format", argumentNames: ["input", "arg"], returns: true) { (arguments, callback) in | ||
|
||
var arguments = arguments | ||
|
||
guard let input = arguments.removeValue(forKey: "input") else { | ||
callback(.string("")) | ||
return | ||
} | ||
|
||
guard case let .string(inputStr) = input else { | ||
callback(.string("")) | ||
return | ||
} | ||
|
||
var otherValues = arguments.values | ||
|
||
var varArgs = [CVarArg]() | ||
|
||
for value in otherValues { | ||
|
||
switch value { | ||
case .bool(let b): | ||
break | ||
case .number(let n): | ||
varArgs.append(n) | ||
case .string(let str): | ||
varArgs.append(str) | ||
case .struct: | ||
break | ||
} | ||
|
||
} | ||
|
||
let output = String(format: inputStr, arguments: varArgs) | ||
|
||
callback(.string(output)) | ||
return | ||
} | ||
|
||
print("run") | ||
|
||
do { | ||
|
||
activeVC.terminalView.isExecutingScript = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Looks like they are used only to determine whether or not to write the prompt, so some refactoring would probably be able to remove the need for these anyways. |
||
|
||
runner.executionFinishedCallback = { | ||
|
||
print("executionFinishedCallback") | ||
|
||
DispatchQueue.main.async { | ||
|
||
activeVC.terminalView.isExecutingScript = false | ||
// activeVC.terminalView.isWaitingForCommand = false | ||
|
||
activeVC.commandExecutor(executor, didFinishDispatchWithExitCode: 0) | ||
|
||
} | ||
|
||
} | ||
|
||
try runner.run(source) | ||
|
||
} catch { | ||
return 1 | ||
} | ||
|
||
|
||
return 0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both of the above lines conflict with the Tabs change, which will introduce multiple
ViewController
instances, and movesexecutor
to theTerminalView
.To get the visible view controller, there is an API on the
TerminalTabViewController
calledvisibleViewController