|
| 1 | +// |
| 2 | +// SystemOSLibrary.swift |
| 3 | +// LispKit |
| 4 | +// |
| 5 | +// Created by Matthias Zenger on 07/06/2020. |
| 6 | +// Copyright © 2020 ObjectHub. All rights reserved. |
| 7 | +// |
| 8 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +// you may not use this file except in compliance with the License. |
| 10 | +// You may obtain a copy of the License at |
| 11 | +// |
| 12 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +// |
| 14 | +// Unless required by applicable law or agreed to in writing, software |
| 15 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +// See the License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | +// |
| 20 | + |
| 21 | +import Foundation |
| 22 | + |
| 23 | +/// |
| 24 | +/// System OS library |
| 25 | +/// |
| 26 | +public final class SystemOSLibrary: NativeLibrary { |
| 27 | + |
| 28 | + /// Imported native library |
| 29 | + private unowned var systemLibrary: SystemLibrary! |
| 30 | + private unowned var portLibrary: PortLibrary! |
| 31 | + |
| 32 | + /// Initialize port library, in particular its parameter objects. |
| 33 | + public required init(in context: Context) throws { |
| 34 | + try super.init(in: context) |
| 35 | + } |
| 36 | + |
| 37 | + /// Name of the library. |
| 38 | + public override class var name: [String] { |
| 39 | + return ["lispkit", "system", "os"] |
| 40 | + } |
| 41 | + |
| 42 | + /// Dependencies of the library. |
| 43 | + public override func dependencies() { |
| 44 | + self.`import`(from: ["lispkit", "system"], "current-directory") |
| 45 | + self.`import`(from: ["lispkit", "port"], "current-output-port") |
| 46 | + } |
| 47 | + |
| 48 | + /// Declarations of the library. |
| 49 | + public override func declarations() { |
| 50 | + self.define(Procedure("system-call", self.systemCall)) |
| 51 | + } |
| 52 | + |
| 53 | + public override func initializations() { |
| 54 | + self.systemLibrary = self.nativeLibrary(SystemLibrary.self) |
| 55 | + self.portLibrary = self.nativeLibrary(PortLibrary.self) |
| 56 | + } |
| 57 | + |
| 58 | + private func systemCall(expr: Expr, |
| 59 | + arguments: Expr, |
| 60 | + args: Arguments) throws -> Expr { |
| 61 | + guard let (e, op, ipt) = args.optional(.false, |
| 62 | + .port(portLibrary.outputPort ?? self.context.outputPort), |
| 63 | + .false) else { |
| 64 | + throw RuntimeError.argumentCount(of: "system-call", |
| 65 | + min: 2, |
| 66 | + max: 5, |
| 67 | + args: .pair(expr, .pair(arguments, .makeList(args)))) |
| 68 | + } |
| 69 | + return try self.systemCall(binary: expr, |
| 70 | + arguments: arguments, |
| 71 | + environment: e, |
| 72 | + outport: op, |
| 73 | + input: ipt) |
| 74 | + } |
| 75 | + |
| 76 | + private func systemCall(binary expr: Expr, |
| 77 | + arguments: Expr, |
| 78 | + environment e: Expr, |
| 79 | + outport op: Expr, |
| 80 | + input ipt: Expr) throws -> Expr { |
| 81 | + let proc = Process() |
| 82 | + proc.currentDirectoryURL = URL(fileURLWithPath: self.systemLibrary.currentDirectoryPath, |
| 83 | + isDirectory: true) |
| 84 | + proc.executableURL = |
| 85 | + URL(fileURLWithPath: try expr.asPath(), |
| 86 | + relativeTo: URL(fileURLWithPath: self.systemLibrary.currentDirectoryPath, |
| 87 | + isDirectory: true)) |
| 88 | + var args: [String] = [] |
| 89 | + var lst = arguments |
| 90 | + while case .pair(let car, let cdr) = lst { |
| 91 | + args.append(car.unescapedDescription) |
| 92 | + lst = cdr |
| 93 | + } |
| 94 | + proc.arguments = args |
| 95 | + if e.isTrue { |
| 96 | + var env: [String : String] = [:] |
| 97 | + lst = e |
| 98 | + while case .pair(let car, let cdr) = lst { |
| 99 | + guard case .pair(let name, let value) = car else { |
| 100 | + throw RuntimeError.type(car, expected: [.pairType]) |
| 101 | + } |
| 102 | + env[try name.asString()] = value.unescapedDescription |
| 103 | + lst = cdr |
| 104 | + } |
| 105 | + proc.environment = env |
| 106 | + } |
| 107 | + let condition = NSCondition() |
| 108 | + proc.terminationHandler = { _ in |
| 109 | + condition.lock() |
| 110 | + condition.signal() |
| 111 | + condition.unlock() |
| 112 | + } |
| 113 | + let outputPipe = Pipe() |
| 114 | + let textOutput = op.isTrue ? try portLibrary.textOutputFrom(op) : nil |
| 115 | + if op.isTrue { |
| 116 | + outputPipe.fileHandleForReading.readabilityHandler = { handle in |
| 117 | + condition.lock() |
| 118 | + let data = handle.availableData |
| 119 | + if data.count > 0 { |
| 120 | + _ = textOutput?.writeString(String(data: data, encoding: .utf8) ?? "") |
| 121 | + } |
| 122 | + condition.unlock() |
| 123 | + } |
| 124 | + proc.standardOutput = outputPipe |
| 125 | + proc.standardError = outputPipe |
| 126 | + } else { |
| 127 | + proc.standardOutput = nil |
| 128 | + proc.standardError = nil |
| 129 | + } |
| 130 | + let inputPipe = Pipe() |
| 131 | + if ipt.isTrue { |
| 132 | + if let data = try ipt.asString().data(using: .utf8) { |
| 133 | + inputPipe.fileHandleForWriting.write(data) |
| 134 | + } |
| 135 | + } |
| 136 | + inputPipe.fileHandleForWriting.closeFile() |
| 137 | + proc.standardInput = inputPipe |
| 138 | + condition.lock() |
| 139 | + defer { |
| 140 | + if proc.isRunning { |
| 141 | + proc.terminate() |
| 142 | + } |
| 143 | + outputPipe.fileHandleForReading.readabilityHandler = nil |
| 144 | + inputPipe.fileHandleForWriting.writeabilityHandler = nil |
| 145 | + condition.unlock() |
| 146 | + } |
| 147 | + try proc.run() |
| 148 | + while proc.isRunning && !self.context.machine.isAbortionRequested() { |
| 149 | + condition.wait(until: Date(timeInterval: 0.7, since: Date())) |
| 150 | + } |
| 151 | + inputPipe.fileHandleForWriting.writeabilityHandler = nil |
| 152 | + outputPipe.fileHandleForReading.readabilityHandler = nil |
| 153 | + if op.isTrue { |
| 154 | + let restdata = outputPipe.fileHandleForReading.availableData |
| 155 | + if restdata.count > 0 { |
| 156 | + _ = textOutput?.writeString(String(data: restdata, encoding: .utf8) ?? "") |
| 157 | + } |
| 158 | + } |
| 159 | + return .fixnum(Int64(proc.isRunning ? -1 : proc.terminationStatus)) |
| 160 | + } |
| 161 | +} |
0 commit comments