-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// NSTask.h | ||
// Santander | ||
// | ||
// Created by Serena on 06/09/2022 | ||
// | ||
|
||
|
||
#ifndef NSTask_h | ||
#define NSTask_h | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface NSTask : NSObject | ||
|
||
@property (copy) NSArray <NSString *> *arguments; | ||
@property (copy) NSURL *currentDirectoryURL; | ||
@property (copy) NSDictionary *environment; | ||
@property (copy) NSURL *executableURL; | ||
@property (readonly) int processIdentifier; | ||
@property NSInteger qualityOfService; | ||
@property (getter=isRunning, readonly) BOOL running; | ||
@property (retain) NSPipe *standardError; | ||
@property (retain) id standardInput; | ||
@property (retain) NSPipe *standardOutput; | ||
@property(copy) void (^terminationHandler)(NSTask *); | ||
@property (readonly) NSInteger terminationReason; | ||
@property (readonly) int terminationStatus; | ||
|
||
|
||
+(id)allocWithZone:(struct _NSZone *)arg0 ; | ||
+(id)currentTaskDictionary; | ||
+(id)launchedTaskWithDictionary:(id)arg0 ; | ||
-(BOOL)isSpawnedProcessDisclaimed; | ||
-(BOOL)resume; | ||
-(BOOL)suspend; | ||
-(NSInteger)suspendCount; | ||
-(BOOL)launchAndReturnError:(out NSError * _Nullable *)error; | ||
-(id)currentDirectoryPath; | ||
-(id)init; | ||
-(id)launchPath; | ||
-(void)waitUntilExit; | ||
-(void)interrupt; | ||
-(void)launch; | ||
-(void)setCurrentDirectoryPath:(id)arg0 ; | ||
-(void)setLaunchPath:(id)arg0 ; | ||
-(void)setSpawnedProcessDisclaimed:(BOOL)arg0 ; | ||
-(void)terminate; | ||
|
||
@end | ||
NS_ASSUME_NONNULL_END | ||
|
||
#endif /* NSTask_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module NSTaskiOS { | ||
header "NSTask.h" | ||
} |
133 changes: 133 additions & 0 deletions
133
Santander/UI/Editors/BinaryExecutionViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// | ||
// BinaryExecutionViewController.swift | ||
// Santander | ||
// | ||
// Created by Serena on 06/09/2022 | ||
// | ||
|
||
|
||
import UIKit | ||
import NSTaskiOS | ||
|
||
class BinaryExecutionViewController: UIViewController { | ||
let executableURL: URL | ||
var task: NSTask = NSTask() | ||
var textView: UITextView! | ||
|
||
init(executableURL: URL) { | ||
self.executableURL = executableURL | ||
|
||
super.init(nibName: nil, bundle: nil) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
view.backgroundColor = .secondarySystemBackground | ||
|
||
title = "Execution" | ||
let doneAction = UIAction { | ||
if self.task.isRunning { | ||
self.task.interrupt() | ||
} | ||
self.dismiss(animated: true) | ||
} | ||
|
||
navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: doneAction) | ||
configureNavigationBarToNormal() | ||
|
||
let executableTextField = UITextField() | ||
|
||
let action = UIAction { | ||
if self.task.isRunning { | ||
self.task.interrupt() | ||
} | ||
self.spawnExecutable(pathAndArgs: executableTextField.text!) | ||
} | ||
|
||
executableTextField.returnKeyType = .go | ||
executableTextField.addAction(action, for: .primaryActionTriggered) | ||
executableTextField.text = executableURL.path | ||
executableTextField.font = UIFont(name: "Menlo", size: UIFont.systemFontSize) | ||
executableTextField.translatesAutoresizingMaskIntoConstraints = false | ||
executableTextField.backgroundColor = .systemBackground | ||
executableTextField.inputAccessoryView = makeKeyboardToolbar(forTextField: executableTextField) | ||
view.addSubview(executableTextField) | ||
|
||
let guide = view.safeAreaLayoutGuide | ||
NSLayoutConstraint.activate([ | ||
executableTextField.trailingAnchor.constraint(equalTo: guide.trailingAnchor), | ||
executableTextField.leadingAnchor.constraint(equalTo: guide.leadingAnchor), | ||
executableTextField.topAnchor.constraint(equalTo: guide.topAnchor), | ||
executableTextField.heightAnchor.constraint(equalToConstant: 50), | ||
]) | ||
|
||
self.textView = UITextView() | ||
textView.text = "" | ||
textView.font = .systemFont(ofSize: 20) | ||
textView.isEditable = false | ||
textView.translatesAutoresizingMaskIntoConstraints = false | ||
textView.backgroundColor = view.backgroundColor | ||
view.addSubview(textView) | ||
|
||
NSLayoutConstraint.activate([ | ||
textView.leadingAnchor.constraint(equalTo: guide.leadingAnchor), | ||
textView.trailingAnchor.constraint(equalTo: guide.trailingAnchor), | ||
textView.topAnchor.constraint(equalTo: executableTextField.bottomAnchor), | ||
textView.heightAnchor.constraint(equalTo: view.heightAnchor) | ||
]) | ||
} | ||
|
||
func makeKeyboardToolbar(forTextField textField: UITextField) -> UIToolbar { | ||
let toolbar = UIToolbar() | ||
|
||
let dismissKeyboardAction = UIAction { | ||
textField.resignFirstResponder() | ||
} | ||
|
||
let dismissButton = UIBarButtonItem(title: "Dismiss", primaryAction: dismissKeyboardAction) | ||
toolbar.setItems([.flexibleSpace(), dismissButton], animated: true) | ||
toolbar.sizeToFit() | ||
return toolbar | ||
} | ||
|
||
func spawnExecutable(pathAndArgs: String) { | ||
var components = pathAndArgs.components(separatedBy: " ") | ||
guard let executable = components.first, !executable.isEmpty else { | ||
self.errorAlert("Enter a valid executable and arguments.", title: "Input is empty") | ||
return | ||
} | ||
|
||
// make it just args | ||
components.removeFirst() | ||
self.task = NSTask() | ||
task.executableURL = URL(fileURLWithPath: executable) | ||
task.arguments = components | ||
|
||
let pipe = Pipe() | ||
pipe.fileHandleForReading.readabilityHandler = { outPipe in | ||
guard let output = String(data: outPipe.availableData, encoding: .utf8), | ||
!output.isEmpty else { | ||
return | ||
} | ||
|
||
DispatchQueue.main.async { | ||
self.textView.text.append(output) | ||
} | ||
} | ||
|
||
task.standardError = pipe | ||
task.standardOutput = pipe | ||
do { | ||
textView.text = "" | ||
try task.launchAndReturnError() | ||
task.waitUntilExit() | ||
} catch { | ||
self.errorAlert(error, title: "Unable to launch process") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters