Skip to content

Commit 218dbeb

Browse files
committed
Moved "cd" to ios_system (code simplification)
1 parent 4640d61 commit 218dbeb

File tree

1 file changed

+12
-66
lines changed

1 file changed

+12
-66
lines changed

Sessions/MCPSession.m

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
#import "SSHCopyIDSession.h"
4444
#import "SSHSession.h"
4545

46+
// from ios_system:
4647
extern int ios_system(char* cmd);
4748
extern int ios_executable(char* inputCmd);
49+
extern void initializeEnvironment();
4850
extern int curl_static_main(int argc, char** argv);
4951

5052
#define MCP_MAX_LINE 4096
@@ -55,7 +57,7 @@ @implementation MCPSession {
5557

5658
static NSString *docsPath;
5759
static NSString *filePath;
58-
static NSString* previousDirectory;
60+
// for file completion
5961
// do recompute directoriesInPath only if $PATH has changed
6062
static NSString* fullCommandPath = @"";
6163
static NSArray *directoriesInPath;
@@ -178,7 +180,7 @@ - (char **) makeargs:(NSMutableArray*) listArgv argc:(int*) argc
178180
NSString *prefix = [cmd stringByAppendingString:[NSString stringWithCString:"://" encoding:NSASCIIStringEncoding]];
179181
argument = [[prefix stringByAppendingString:userAndHost] stringByAppendingString:fileLocation];
180182
// avoid ~ conversion:
181-
argv[i] = [argument UTF8String];
183+
argv[i] = strdup([argument UTF8String]);
182184
continue;
183185
}
184186
if (![argument hasPrefix:@"-"]) {
@@ -231,18 +233,18 @@ - (char **) makeargs:(NSMutableArray*) listArgv argc:(int*) argc
231233
}
232234
}
233235
if (([cmd isEqualToString:@"scp"] || [cmd isEqualToString:@"sftp"]) && (i == 0))
234-
argv[i] = [@"curl" UTF8String];
236+
argv[i] = strdup([@"curl" UTF8String]);
235237
else
236-
argv[i] = [argument UTF8String];
238+
argv[i] = strdup([argument UTF8String]);
237239
}
238240
if (mustAddMinusTPosition > 0) {
239241
// For scp uploads
240242
// Need to add parameter "-T" before parameter number i.
241243
*argc += 1;
242244
argv = (char **)realloc(argv, (*argc + 1) * sizeof(char*));
243245
for (int i = *argc; i > mustAddMinusTPosition; i--)
244-
argv[i - 1] = strdup(argv[i - 2]);
245-
argv[mustAddMinusTPosition] = [@"-T" UTF8String];
246+
argv[i - 1] = argv[i - 2];
247+
argv[mustAddMinusTPosition] = strdup([@"-T" UTF8String]);
246248
}
247249

248250
argv[*argc] = NULL;
@@ -277,29 +279,6 @@ - (bool)executeCommand:(int)argc argv:(char **)argv {
277279
[self ssh_save_id:argc argv:argv];
278280
} else if ([cmd isEqualToString:@"config"]) {
279281
[self showConfig];
280-
} else if ([cmd isEqualToString:@"cd"]) {
281-
NSString* currentDir = [[NSFileManager defaultManager] currentDirectoryPath];
282-
if (argc > 1) {
283-
NSString* newDir = @(argv[1]);
284-
if (strcmp(argv[1], "-") == 0) {
285-
// "cd -" option to pop back to previous directory
286-
newDir = previousDirectory;
287-
}
288-
BOOL isDir;
289-
if ([[NSFileManager defaultManager] fileExistsAtPath:newDir isDirectory:&isDir]) {
290-
if (isDir) {
291-
[[NSFileManager defaultManager] changeCurrentDirectoryPath:newDir];
292-
previousDirectory = currentDir;
293-
}
294-
else fprintf(_stream.out, "cd: %s: not a directory\n", [newDir UTF8String]);
295-
} else {
296-
fprintf(_stream.out, "cd: %s: no such file or directory\n", [newDir UTF8String]);
297-
}
298-
} else { // [cd] Help, I'm lost, bring me back home
299-
previousDirectory = [[NSFileManager defaultManager] currentDirectoryPath];
300-
[[NSFileManager defaultManager] changeCurrentDirectoryPath:docsPath];
301-
}
302-
// Higher level commands, not from system: curl, tar, scp, sftp
303282
} else if ([cmd isEqualToString:@"preview"]) {
304283
// Opening in helper apps (PDFViewer, in this example)
305284
NSString* fileLocation = @(argv[1]);
@@ -362,14 +341,14 @@ - (BOOL)executeCommand:(NSMutableArray*) listArgv {
362341
}
363342

364343
// This is a superset of all commands available. We check at runtime whether they are actually available (using ios_executable)
365-
char* commandList[] = {"ls", "touch", "rm", "cp", "ln", "link", "mv", "mkdir", "chown", "chgrp", "chflags", "chmod", "du", "df", "chksum", "sum", "stat", "readlink", "compress", "uncompress", "gzip", "gunzip", "tar", "printenv", "pwd", "uname", "date", "env", "id", "groups", "whoami", "uptime", "w", "cat", "wc", "grep", "egrep", "fgrep", "curl", "python", "lua", "luac", "amstex", "cslatex", "csplain", "eplain", "etex", "jadetex", "latex", "mex", "mllatex", "mltex", "pdflatex", "pdftex", "pdfcslatex", "pdfcstex", "pdfcsplain", "pdfetex", "pdfjadetex", "pdfmex", "pdfxmltex", "texsis", "utf8mex", "xmltex", "lualatex", "luatex", "texlua", "texluac", "dviluatex", "dvilualatex", "bibtex",
344+
char* commandList[] = {"ls", "touch", "rm", "cp", "ln", "link", "mv", "mkdir", "chown", "chgrp", "chflags", "chmod", "du", "df", "chksum", "sum", "stat", "readlink", "compress", "uncompress", "gzip", "gunzip", "tar", "printenv", "pwd", "uname", "date", "env", "id", "groups", "whoami", "uptime", "w", "cat", "wc", "grep", "egrep", "fgrep", "curl", "python", "lua", "luac", "amstex", "cslatex", "csplain", "eplain", "etex", "jadetex", "latex", "mex", "mllatex", "mltex", "pdflatex", "pdftex", "pdfcslatex", "pdfcstex", "pdfcsplain", "pdfetex", "pdfjadetex", "pdfmex", "pdfxmltex", "texsis", "utf8mex", "xmltex", "lualatex", "luatex", "texlua", "texluac", "dviluatex", "dvilualatex", "bibtex", "setenv", "unsetenv", "cd",
366345
NULL}; // must end with NULL pointer
367346

368347
// Commands defined outside of ios_executable:
369-
char* localCommandList[] = {"help", "mosh", "ssh", "exit", "ssh-copy-id", "ssh-save-id", "config", "setenv", "cd", "scp", "sftp", NULL}; // must end with NULL pointer
348+
char* localCommandList[] = {"help", "mosh", "ssh", "exit", "ssh-copy-id", "ssh-save-id", "config", "scp", "sftp", NULL}; // must end with NULL pointer
370349

371350
// Commands that don't take a file as argument:
372-
char* commandsNoFileList[] = {"help", "mosh", "ssh", "exit", "ssh-copy-id", "ssh-save-id", "config", "setenv", "printenv", "pwd", "uname", "date", "env", "id", "groups", "whoami", "uptime", "w", NULL};
351+
char* commandsNoFileList[] = {"help", "mosh", "ssh", "exit", "ssh-copy-id", "ssh-save-id", "config", "setenv", "unsetenv", "printenv", "pwd", "uname", "date", "env", "id", "groups", "whoami", "uptime", "w", NULL};
373352
// must end with NULL pointer
374353

375354
void completion(const char *command, linenoiseCompletions *lc) {
@@ -471,42 +450,9 @@ - (int)main:(int)argc argv:(char **)argv
471450

472451
// Initialize paths for application files, including history.txt and keys
473452
docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
474-
NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
475453
filePath = [docsPath stringByAppendingPathComponent:@"history.txt"];
476-
477-
// Where the executables are stored: $PATH + ~/Library/bin + ~/Documents/bin
478-
// Add content of old PATH to this. PATH *is* defined in iOS, surprising as it may be.
479-
// I'm not going to erase it, so we just add ourselves.
480-
// We go through main several times, so make sure we only append to PATH once
481-
NSString* checkingPath = [NSString stringWithCString:getenv("PATH") encoding:NSASCIIStringEncoding];
482-
if (! [fullCommandPath isEqualToString:checkingPath]) {
483-
fullCommandPath = checkingPath;
484-
}
485-
if (![fullCommandPath containsString:@"Library/bin"]) {
486-
NSString *binPath = [libPath stringByAppendingPathComponent:@"bin"];
487-
fullCommandPath = [[binPath stringByAppendingString:@":"] stringByAppendingString:fullCommandPath];
488-
}
489-
if (![fullCommandPath containsString:@"Documents/bin"]) {
490-
NSString *binPath = [docsPath stringByAppendingPathComponent:@"bin"];
491-
fullCommandPath = [[binPath stringByAppendingString:@":"] stringByAppendingString:fullCommandPath];
492-
}
493-
setenv("BLINK", [[NSBundle mainBundle] resourcePath].UTF8String, 1);
494-
setenv("PATH", fullCommandPath.UTF8String, 1); // 1 = override existing value
495-
directoriesInPath = [fullCommandPath componentsSeparatedByString:@":"];
496-
497-
// We can't write in $HOME so we need to set the position of config files:
498-
setenv("SSH_HOME", docsPath.UTF8String, 0); // SSH keys in ~/Documents/.ssh/
499-
setenv("CURL_HOME", docsPath.UTF8String, 0); // CURL config in ~/Documents/
500-
setenv("PYTHONHOME", libPath.UTF8String, 0); // Python scripts in ~/Library/lib/python3.6/
501-
setenv("PYZMQ_BACKEND", "cffi", 0);
502-
setenv("JUPYTER_CONFIG_DIR", [docsPath stringByAppendingPathComponent:@".jupyter"].UTF8String, 0);
503-
setenv("TMPDIR", NSTemporaryDirectory().UTF8String, 0); // tmp directory
504-
setenv("SSL_CERT_FILE", [docsPath stringByAppendingPathComponent:@"cacert.pem"].UTF8String, 0); // SLL cacert.pem in ~/Documents/cacert.pem
505-
// hg config file in ~/Documents/.hgrc
506-
setenv("HGRCPATH", [docsPath stringByAppendingPathComponent:@".hgrc"].UTF8String, 0);
507-
// iOS already defines "HOME" as the home dir of the application
454+
initializeEnvironment();
508455
[[NSFileManager defaultManager] changeCurrentDirectoryPath:docsPath];
509-
previousDirectory = docsPath;
510456

511457
const char *history = [filePath UTF8String];
512458

0 commit comments

Comments
 (0)