Skip to content

Commit

Permalink
Adding "source" command
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Holzschuch committed Jan 19, 2022
1 parent 1d96973 commit f60baf6
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 7 deletions.
10 changes: 10 additions & 0 deletions ios_system.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
224AAC5C21CB931300F8C22F /* reallocarray.c in Sources */ = {isa = PBXBuildFile; fileRef = 224AAC5B21CB931300F8C22F /* reallocarray.c */; };
224AAC5E21CB935100F8C22F /* utf8.c in Sources */ = {isa = PBXBuildFile; fileRef = 224AAC5D21CB935100F8C22F /* utf8.c */; };
224AAC6021CB93AE00F8C22F /* readpass.c in Sources */ = {isa = PBXBuildFile; fileRef = 224AAC5F21CB93AE00F8C22F /* readpass.c */; settings = {COMPILER_FLAGS = "-x objective-c"; }; };
224B48F4279491FE00D310A8 /* source.swift in Sources */ = {isa = PBXBuildFile; fileRef = 224B48F3279491FE00D310A8 /* source.swift */; };
225782911FDB4D390050F312 /* curl_config.h in Headers */ = {isa = PBXBuildFile; fileRef = 2257811A1FDB4D380050F312 /* curl_config.h */; };
225E1B6F2067F39F005AC151 /* tar.h in Headers */ = {isa = PBXBuildFile; fileRef = 225E1B6D2067F39F005AC151 /* tar.h */; settings = {ATTRIBUTES = (Public, ); }; };
225F06102016751900466685 /* getopt_long.c in Sources */ = {isa = PBXBuildFile; fileRef = 225F060F2016751800466685 /* getopt_long.c */; };
Expand Down Expand Up @@ -626,6 +627,7 @@
224AAC5B21CB931300F8C22F /* reallocarray.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = reallocarray.c; path = "ssh_keygen/openbsd-compat/reallocarray.c"; sourceTree = "<group>"; };
224AAC5D21CB935100F8C22F /* utf8.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = utf8.c; path = ssh_keygen/utf8.c; sourceTree = "<group>"; };
224AAC5F21CB93AE00F8C22F /* readpass.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = readpass.c; path = ssh_keygen/readpass.c; sourceTree = "<group>"; };
224B48F3279491FE00D310A8 /* source.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = source.swift; sourceTree = "<group>"; };
2253BA1D201942B10019CB39 /* libresolv.9.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libresolv.9.tbd; path = usr/lib/libresolv.9.tbd; sourceTree = SDKROOT; };
22577D451FDB47A90050F312 /* err.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = err.c; sourceTree = "<group>"; };
22577D461FDB47A90050F312 /* lafe_err.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lafe_err.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1941,6 +1943,7 @@
22F0803620973712003C3BF0 /* sleep.c */,
221CD98D21E8B03D00F3B47C /* open.m */,
22484B022434C5BB00D6BDDA /* jsc.swift */,
224B48F3279491FE00D310A8 /* source.swift */,
222CC4D9218CBEED00D3A11C /* xarg */,
22C5057C209875E200FDDFA9 /* find */,
225F060920163BFC00466685 /* tee */,
Expand Down Expand Up @@ -2622,6 +2625,7 @@
};
22F6A10B2068390800E618F9 = {
CreatedOnToolsVersion = 9.3;
LastSwiftMigration = 1320;
ProvisioningStyle = Automatic;
};
22F6A127206839EF00E618F9 = {
Expand Down Expand Up @@ -2935,6 +2939,7 @@
22C5058C2098ADD800FDDFA9 /* main.c in Sources */,
22F6A1202068395B00E618F9 /* proc_compare.c in Sources */,
22F6A11B2068394E00E618F9 /* printenv.c in Sources */,
224B48F4279491FE00D310A8 /* source.swift in Sources */,
22F6A1152068393E00E618F9 /* echo.c in Sources */,
22F6A11C2068395200E618F9 /* pwd.c in Sources */,
22C505862098ADD800FDDFA9 /* find.c in Sources */,
Expand Down Expand Up @@ -3647,6 +3652,8 @@
22F6A1122068390800E618F9 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUILD_LIBRARY_FOR_DISTRIBUTION = YES;
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
Expand All @@ -3670,13 +3677,16 @@
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
STRIP_SWIFT_SYMBOLS = NO;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
};
name = Debug;
};
22F6A1132068390800E618F9 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUILD_LIBRARY_FOR_DISTRIBUTION = YES;
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>awk.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
<integer>0</integer>
</dict>
<key>curl_ios.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -17,17 +17,17 @@
<key>files.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>6</integer>
<integer>4</integer>
</dict>
<key>ios_system.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>2</integer>
</dict>
<key>shell.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>3</integer>
</dict>
<key>ssh_cmd.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -37,12 +37,12 @@
<key>tar.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>4</integer>
<integer>1</integer>
</dict>
<key>text.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>6</integer>
</dict>
</dict>
</dict>
Expand Down
3 changes: 3 additions & 0 deletions ios_system/ios_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ extern int ios_activePager(void);
extern void ios_setContext(const void *context);
extern void* ios_getContext(void);
extern void ios_setDirectoryURL(NSURL* workingDirectoryURL);
extern void newPreviousDirectory(void);
extern void makeGlobal(void);
extern void makeLocal(void);
extern void replaceCommand(NSString* commandName, NSString* functionName, bool allOccurences);
extern NSError* addCommandList(NSString* fileLocation);
extern int numPythonInterpreters;
Expand Down
13 changes: 12 additions & 1 deletion libc_replacement.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ int ios_putw(int w, FILE *stream) {
static pthread_t thread_ids[IOS_MAX_THREADS];
static int numVariablesSet[IOS_MAX_THREADS];
static char** environment[IOS_MAX_THREADS];
static char** copyEnvironment[IOS_MAX_THREADS];
static char previousDirectory[IOS_MAX_THREADS][MAXPATHLEN];
static int previousPid[IOS_MAX_THREADS];

Expand All @@ -138,12 +139,22 @@ pthread_mutex_t pid_mtx = PTHREAD_MUTEX_INITIALIZER;
_Atomic(int) cleanup_counter = 0;
static pid_t last_allocated_pid = 0;


void makeGlobal(void) {
copyEnvironment[current_pid] = environment[current_pid];
environment[current_pid] = NULL;
}
void makeLocal(void) {
environment[current_pid] = copyEnvironment[current_pid];
copyEnvironment[current_pid] = NULL;
}

inline pthread_t ios_getThreadId(pid_t pid) {
// return ios_getLastThreadId(); // previous behaviour
return thread_ids[pid];
}

void newPreviousDirectory() {
void newPreviousDirectory(void) {
// Called when a command calls "cd". Actually changes the directory for that command.
getwd(previousDirectory[current_pid]);
}
Expand Down
83 changes: 83 additions & 0 deletions shell_cmds_ios/source.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// source.swift
// shell
//
// Created by Nicolas Holzschuch on 16/01/2022.
// Copyright © 2022 Nicolas Holzschuch. All rights reserved.
//

import Foundation
import ios_system


func convertCArguments(argc: Int32, argv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?) -> [String]? {

var args = [String]()

for i in 0..<Int(argc) {

guard let argC = argv?[i] else {
return nil
}

let arg = String(cString: argC)

args.append(arg)

}
return args
}

// TODO: create commands makeGlobal, makeLocal in libc_replacement

// Small replacement for the "source" shell command.
// Everything that affects the current shell should affect the current shell (for now: setenv and cd).
// Files with "if" / "while" commands are going to fail.
@_cdecl("source")
public func source(argc: Int32, argv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?) -> Int32 {
let shortUsage = "usage: source file [file2] [file3]\n"
guard let args = convertCArguments(argc: argc, argv: argv) else {
fputs(shortUsage, thread_stdout)
return 0
}
if args.count == 1 {
fputs(shortUsage, thread_stdout)
return 0
}
// To make environment global, set environment[current_pid] to null, restore afterwards
makeGlobal() // setenv commands go global
for i in 1..<args.count {
let fileName = URL(fileURLWithPath: args[i])
if (FileManager().fileExists(atPath: fileName.path)) {
do {
let contentOfFile = try String(contentsOf: fileName, encoding: String.Encoding.utf8)
let commands = contentOfFile.split(separator: "\n")
for command in commands {
let trimmedCommand = command.trimmingCharacters(in: .whitespacesAndNewlines)
if (trimmedCommand.count == 0) { continue } // skip white lines
if (trimmedCommand.hasPrefix("#")) { continue } // skip comments
// reset the LC_CTYPE (some commands (luatex) can change it):
setenv("LC_CTYPE", "UTF-8", 1);
setlocale(LC_CTYPE, "UTF-8");
// Todo: make cd globzl: ???
let pid = ios_fork()
_ = ios_system(trimmedCommand)
fflush(thread_stdout)
ios_waitpid(pid)
ios_releaseThreadId(pid)
}
}
catch {
fputs("source: error loading \(fileName).", thread_stderr)
}
} else {
fputs("source: file \(fileName) not found.", thread_stderr)
}
}
makeLocal() // back to local
newPreviousDirectory() // make directory changes permanent
return 0
}

// Now check that it *does* change the environment when run. Should not. Might have to be edited.
// HOW? detect "setenv" and "cd"?

0 comments on commit f60baf6

Please sign in to comment.