From 25666f1659cf5ff7347fb7d5689d004a22f26405 Mon Sep 17 00:00:00 2001 From: hwachakarter Date: Wed, 22 Apr 2026 09:19:53 +0900 Subject: [PATCH] fix: fixed sonya not working properly with stdin #1 shell now calls standard system from stdlib.h from c works on macOS, needed to test on linux regex strings in VarRegex were refactored into RegexBuilder all tests passed. --- Sources/sonya/sonya.swift | 8 ++++++++ Sources/sonyalib/Package.swift | 13 +++++++------ .../sonyalib/Sources/shellHelper/shell_helper.c | 13 +++++++++++++ .../sonyalib/Sources/shellHelper/shell_helper.h | 8 ++++++++ Sources/sonyalib/Sources/sonyalib/Shell.swift | 17 ++--------------- .../sonyalib/Sources/sonyalib/VarRegex.swift | 13 +++++++++++-- 6 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 Sources/sonyalib/Sources/shellHelper/shell_helper.c create mode 100644 Sources/sonyalib/Sources/shellHelper/shell_helper.h diff --git a/Sources/sonya/sonya.swift b/Sources/sonya/sonya.swift index f205ef5..1876b54 100644 --- a/Sources/sonya/sonya.swift +++ b/Sources/sonya/sonya.swift @@ -1,3 +1,4 @@ +import Foundation import sonyalib @main @@ -12,18 +13,25 @@ struct sonya { break case .noSonyafile: print("sonya: no sonyafile found in current directory") + exit(1) case .isEmpty: print("sonya: sonyafile is empty") + exit(2) case .syntaxError: print("sonya: syntax error in sonyafile") + exit(3) case .noSuchRecipe: print("sonya: no recipe named '\(recipe)'") + exit(4) case .noSuchDependency: print("sonya: unknown dependency") + exit(5) case .shellReturnedError: print("sonya: command failed") + exit(6) case .noSuchVariable: print("sonya: undefined variable referenced in recipe '\(recipe)'") + exit(7) } } } diff --git a/Sources/sonyalib/Package.swift b/Sources/sonyalib/Package.swift index 7562575..77af91d 100644 --- a/Sources/sonyalib/Package.swift +++ b/Sources/sonyalib/Package.swift @@ -1,6 +1,4 @@ // swift-tools-version: 6.2 -// The swift-tools-version declares the minimum version of Swift required to build this package. - import PackageDescription let package = Package( @@ -9,17 +7,20 @@ let package = Package( .macOS(.v13) ], products: [ - // Products define the executables and libraries a package produces, making them visible to other packages. .library( name: "sonyalib", targets: ["sonyalib"] ), ], targets: [ - // Targets are the basic building blocks of a package, defining a module or a test suite. - // Targets can depend on other targets in this package and products from dependencies. .target( - name: "sonyalib" + name: "ShellHelper", + path: "Sources/shellHelper", + publicHeadersPath: "." + ), + .target( + name: "sonyalib", + dependencies: ["ShellHelper"] ), .testTarget( name: "sonyalibTests", diff --git a/Sources/sonyalib/Sources/shellHelper/shell_helper.c b/Sources/sonyalib/Sources/shellHelper/shell_helper.c new file mode 100644 index 0000000..65a3ca3 --- /dev/null +++ b/Sources/sonyalib/Sources/shellHelper/shell_helper.c @@ -0,0 +1,13 @@ +// +// shell_helper.c +// sonyalib +// +// Created by hwacha on 4/22/26. +// + +#include +#include "shell_helper.h" + +int run_system(const char *cmd) { + return system(cmd); +} diff --git a/Sources/sonyalib/Sources/shellHelper/shell_helper.h b/Sources/sonyalib/Sources/shellHelper/shell_helper.h new file mode 100644 index 0000000..11ed7e6 --- /dev/null +++ b/Sources/sonyalib/Sources/shellHelper/shell_helper.h @@ -0,0 +1,8 @@ +// +// shell_helper.h +// sonyalib +// +// Created by hwacha on 4/22/26. +// + +int run_system(const char *cmd); diff --git a/Sources/sonyalib/Sources/sonyalib/Shell.swift b/Sources/sonyalib/Sources/sonyalib/Shell.swift index 18ed201..0d972a5 100644 --- a/Sources/sonyalib/Sources/sonyalib/Shell.swift +++ b/Sources/sonyalib/Sources/sonyalib/Shell.swift @@ -6,6 +6,7 @@ // import Foundation +import ShellHelper func openSonyafile() -> String? { let fileManager = FileManager.default @@ -21,19 +22,5 @@ func openSonyafile() -> String? { @discardableResult func shell(_ command: String) -> Int32 { - let process = Process() - let pipe = Pipe() - - process.standardOutput = pipe - process.standardError = pipe - process.executableURL = URL(fileURLWithPath: "/bin/bash") - process.arguments = ["-c", command] - - try? process.run() - process.waitUntilExit() - - let data = pipe.fileHandleForReading.readDataToEndOfFile() - FileHandle.standardOutput.write(data) - - return process.terminationStatus + return run_system(command) } diff --git a/Sources/sonyalib/Sources/sonyalib/VarRegex.swift b/Sources/sonyalib/Sources/sonyalib/VarRegex.swift index 284665c..e91c866 100644 --- a/Sources/sonyalib/Sources/sonyalib/VarRegex.swift +++ b/Sources/sonyalib/Sources/sonyalib/VarRegex.swift @@ -7,9 +7,18 @@ // TODO: args -nonisolated(unsafe) let varRegex = #/\$\(([^\s\n]*)\)/# +import RegexBuilder -nonisolated(unsafe) let argRegex = #/\$(\d+)/# +nonisolated(unsafe) let varRegex = Regex { + "$(" + Capture(ZeroOrMore(CharacterClass(.whitespace, .newlineSequence).inverted)) + ")" +} + +nonisolated(unsafe) let argRegex = Regex { + "$" + OneOrMore(.digit) +} func resolve(_ command: String, vars: [String: String]) -> String? { return resolveVars(command, vars: vars)