59
59
char localMiniRoot[MAXPATHLEN];
60
60
pthread_t current_command_root_thread; // thread ID of first command
61
61
pthread_t lastThreadId; // thread ID of last command
62
+ pthread_t mainThreadId; // thread ID of parent command, if any (e.g. vim, which starts "sh -c cd dir && flake8 file")
62
63
FILE* stdin;
63
64
FILE* stdout;
64
65
FILE* stderr;
@@ -76,6 +77,7 @@ static void initSessionParameters(sessionParameters* sp) {
76
77
sp->isMainThread = TRUE ;
77
78
sp->current_command_root_thread = 0 ;
78
79
sp->lastThreadId = 0 ;
80
+ sp->mainThreadId = 0 ;
79
81
NSString * currentDirectory = [fileManager currentDirectoryPath ];
80
82
strcpy (sp->currentDir , [currentDirectory UTF8String ]);
81
83
strcpy (sp->previousDirectory , [currentDirectory UTF8String ]);
@@ -96,6 +98,46 @@ void ios_setBookmarkDictionaryName(NSString* name) {
96
98
ios_bookmarkDictionaryName = name;
97
99
}
98
100
101
+ void ios_printBookmarkedVersion (char * p) {
102
+ // p is a directory. See if there is a bookmark that can make it shorter:
103
+ NSString * pathString = [NSString stringWithUTF8String: p];
104
+ if ([pathString hasPrefix: @" /private" ]) {
105
+ pathString = [pathString stringByReplacingOccurrencesOfString: @" /private" withString: @" " ];
106
+ }
107
+ NSString *homePath;
108
+ homePath = [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) lastObject ] stringByDeletingLastPathComponent ];
109
+ if ([homePath hasPrefix: @" /private" ]) {
110
+ homePath = [homePath stringByReplacingOccurrencesOfString: @" /private" withString: @" " ];
111
+ }
112
+ NSLog (@" ios_printBookmarkedVersion: %s %s " , homePath.UTF8String , pathString.UTF8String );
113
+ if ([pathString hasPrefix: homePath]) {
114
+ pathString = [pathString stringByReplacingOccurrencesOfString: homePath withString: @" " ];
115
+ fprintf (thread_stdout, " ~%s \n " , pathString.UTF8String );
116
+ return ;
117
+ }
118
+ if (ios_bookmarkDictionaryName == nil ) {
119
+ fprintf (thread_stdout, " %s \n " , p);
120
+ return ;
121
+ }
122
+ NSDictionary *tildeExpansionDictionary = [[NSUserDefaults standardUserDefaults ] dictionaryForKey: ios_bookmarkDictionaryName];
123
+ if (tildeExpansionDictionary == nil ) {
124
+ fprintf (thread_stdout, " %s \n " , p);
125
+ return ;
126
+ }
127
+ for (NSString * bookmark in tildeExpansionDictionary) {
128
+ NSString * bookmarkPath = tildeExpansionDictionary[bookmark];
129
+ if ([bookmarkPath hasPrefix: @" /private" ]) {
130
+ bookmarkPath = [bookmarkPath stringByReplacingOccurrencesOfString: @" /private" withString: @" " ];
131
+ }
132
+ if ([pathString hasPrefix: bookmarkPath]) {
133
+ pathString = [pathString stringByReplacingOccurrencesOfString: bookmarkPath withString: bookmark];
134
+ fprintf (thread_stdout, " ~%s \n " , pathString.UTF8String );
135
+ return ;
136
+ }
137
+ }
138
+ fprintf (thread_stdout, " %s \n " , p);
139
+ }
140
+
99
141
static NSMutableDictionary * sessionList;
100
142
static NSMutableDictionary * aliasDictionary;
101
143
@@ -145,6 +187,8 @@ void _exit(int n) {
145
187
//
146
188
147
189
void ios_signal (int signal) {
190
+ // This function is probably obsolete now. If we keep using it, remember that currentSession is not necessarily the currentSession
191
+ // (if currentSession started sh_session, then we might be sending the signal to the wrong session).
148
192
// Signals the threads of the current session:
149
193
if (currentSession != NULL ) {
150
194
if (currentSession->current_command_root_thread != NULL ) {
@@ -153,6 +197,9 @@ void ios_signal(int signal) {
153
197
if (currentSession->lastThreadId != NULL ) {
154
198
pthread_kill (currentSession->lastThreadId , signal );
155
199
}
200
+ if (currentSession->mainThreadId != NULL ) {
201
+ pthread_kill (currentSession->mainThreadId , signal );
202
+ }
156
203
}
157
204
}
158
205
@@ -173,6 +220,16 @@ void ios_setWindowSize(int width, int height, const void* sessionId) {
173
220
174
221
sprintf (resizedSession->columns , " %d " , width);
175
222
sprintf (resizedSession->lines , " %d " ,height);
223
+ // Also send SIGWINCH to the main thread of resizedSession:
224
+ if (resizedSession->current_command_root_thread != NULL ) {
225
+ pthread_kill (resizedSession->current_command_root_thread , SIGWINCH);
226
+ }
227
+ if (resizedSession->lastThreadId != NULL ) {
228
+ pthread_kill (resizedSession->lastThreadId , SIGWINCH);
229
+ }
230
+ if (resizedSession->mainThreadId != NULL ) {
231
+ pthread_kill (resizedSession->mainThreadId , SIGWINCH);
232
+ }
176
233
}
177
234
178
235
extern char * libc_getenv (const char * variableName);
@@ -325,6 +382,9 @@ static void cleanup_function(void* parameters) {
325
382
if (currentSession->current_command_root_thread == pthread_self ()) {
326
383
currentSession->current_command_root_thread = 0 ;
327
384
}
385
+ if (currentSession->mainThreadId == pthread_self ()) {
386
+ currentSession->mainThreadId = 0 ;
387
+ }
328
388
}
329
389
330
390
// Avoir calling crash_handler several times:
@@ -1380,15 +1440,14 @@ int sh_main(int argc, char** argv) {
1380
1440
// Only one command left
1381
1441
argv[0 ][0 ] = ' h' ; // prevent termination?
1382
1442
pid_t pid = ios_fork ();
1383
- NSLog (@" Starting single command in sh -c, stored last_thread= %x pid: %d , command= %s " , currentSession->lastThreadId , pid, newCommand);
1384
1443
int returnValue = ios_system (newCommand);
1385
1444
ios_waitpid (pid);
1386
1445
free (newCommand);
1387
1446
return returnValue;
1388
1447
}
1389
1448
// If we reach this point, we have multiple commands to execute.
1390
1449
// Store current sesssion, create a new session specific for this, execute commands
1391
- id sessionKey = @((NSUInteger )& sh_session);
1450
+ id sessionKey = @((NSUInteger )sh_session);
1392
1451
if (sessionList != nil ) {
1393
1452
sessionParameters* runningShellSession = (sessionParameters*)[[sessionList objectForKey: sessionKey] pointerValue ];
1394
1453
if (runningShellSession != NULL ) {
@@ -1411,7 +1470,7 @@ int sh_main(int argc, char** argv) {
1411
1470
parentSession = currentSession;
1412
1471
parentDir = [fileManager currentDirectoryPath ];
1413
1472
}
1414
- ios_switchSession (& sh_session); // create a new session
1473
+ ios_switchSession (sh_session); // create a new session
1415
1474
// NSLog(@"after switchSession, currentDir = %s\n", [fileManager currentDirectoryPath].UTF8String);
1416
1475
currentSession->isMainThread = false ;
1417
1476
currentSession->context = sh_session;
@@ -1420,6 +1479,7 @@ int sh_main(int argc, char** argv) {
1420
1479
currentSession->stderr = thread_stderr;
1421
1480
currentSession->current_command_root_thread = NULL ;
1422
1481
currentSession->lastThreadId = NULL ;
1482
+ currentSession->mainThreadId = parentSession->mainThreadId ;
1423
1483
// Need to loop twice: over each argument, and inside each argument.
1424
1484
// &&: keep computing until one command is in error
1425
1485
// ||: keep computing until one command is not in error
@@ -1453,7 +1513,7 @@ int sh_main(int argc, char** argv) {
1453
1513
// NSLog(@"Reset current Dir to= %s instead of %s", parentDir.UTF8String, [fileManager currentDirectoryPath].UTF8String);
1454
1514
[fileManager changeCurrentDirectoryPath: parentDir];
1455
1515
}
1456
- ios_closeSession (& sh_session);
1516
+ ios_closeSession (sh_session);
1457
1517
currentSession = parentSession;
1458
1518
parentSession = NULL ;
1459
1519
return returnValue;
@@ -2696,6 +2756,7 @@ int ios_system(const char* inputCmd) {
2696
2756
while (_tid == NULL ) { }
2697
2757
// ios_storeThreadId(_tid);
2698
2758
currentSession->current_command_root_thread = _tid;
2759
+ if (currentSession->mainThreadId == NULL ) currentSession->mainThreadId = _tid;
2699
2760
// Wait for this process to finish:
2700
2761
if (joinMainThread) {
2701
2762
pthread_join (_tid, NULL );
@@ -2715,6 +2776,7 @@ int ios_system(const char* inputCmd) {
2715
2776
while (_tid == NULL ) { }
2716
2777
// ios_storeThreadId(_tid);
2717
2778
currentSession->current_command_root_thread = _tid;
2779
+ if (currentSession->mainThreadId == NULL ) currentSession->mainThreadId = _tid;
2718
2780
// Wait for this process to finish:
2719
2781
if (joinMainThread) {
2720
2782
pthread_join (_tid, NULL );
0 commit comments