2016-07-09 15:04:09 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
2018-03-20 09:53:46 +03:00
|
|
|
#import <Foundation/Foundation.h>
|
2016-10-22 18:25:43 +03:00
|
|
|
|
2017-12-20 23:35:36 +03:00
|
|
|
#import "NvimServer.h"
|
2018-03-20 09:53:46 +03:00
|
|
|
#import "server_ui.h"
|
2016-08-13 00:45:56 +03:00
|
|
|
#import "Logging.h"
|
2016-08-26 16:31:30 +03:00
|
|
|
#import "CocoaCategories.h"
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
|
2018-07-21 12:23:15 +03:00
|
|
|
static CFRunLoopRef _mainRunLoop;
|
|
|
|
|
2017-12-20 23:35:36 +03:00
|
|
|
NvimServer *_neovim_server;
|
2017-01-08 20:45:14 +03:00
|
|
|
|
2017-08-24 01:37:48 +03:00
|
|
|
static void observe_parent_termination() {
|
2017-01-08 20:45:14 +03:00
|
|
|
pid_t parentPID = getppid();
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2017-01-08 20:45:14 +03:00
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
|
|
|
dispatch_source_t source = dispatch_source_create(
|
2017-06-12 01:30:33 +03:00
|
|
|
DISPATCH_SOURCE_TYPE_PROC, (uintptr_t) parentPID, DISPATCH_PROC_EXIT, queue
|
2017-01-08 20:45:14 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (source == NULL) {
|
|
|
|
WLOG("No parent process monitoring...");
|
|
|
|
return;
|
2016-07-10 12:32:54 +03:00
|
|
|
}
|
|
|
|
|
2017-01-08 20:45:14 +03:00
|
|
|
dispatch_source_set_event_handler(source, ^{
|
|
|
|
WLOG("Exiting neovim server due to parent termination.");
|
2017-08-24 01:37:48 +03:00
|
|
|
CFRunLoopStop(_mainRunLoop);
|
2017-01-08 20:45:14 +03:00
|
|
|
dispatch_source_cancel(source);
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch_resume(source);
|
2016-07-10 12:32:54 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
int main(int argc, const char *argv[]) {
|
2017-01-08 20:45:14 +03:00
|
|
|
_mainRunLoop = CFRunLoopGetCurrent();
|
|
|
|
observe_parent_termination();
|
2016-07-10 12:32:54 +03:00
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
@autoreleasepool {
|
|
|
|
NSArray<NSString *> *arguments = [NSProcessInfo processInfo].arguments;
|
2016-07-10 15:43:26 +03:00
|
|
|
NSString *remoteServerName = arguments[1];
|
|
|
|
NSString *localServerName = arguments[2];
|
2017-06-12 01:30:33 +03:00
|
|
|
NSArray<NSString *> *nvimArgs = argc > 3 ? [arguments subarrayWithRange:NSMakeRange(3, (NSUInteger) (argc - 3))]
|
|
|
|
: nil;
|
|
|
|
|
2017-12-20 23:35:36 +03:00
|
|
|
_neovim_server = [[NvimServer alloc] initWithLocalServerName:localServerName
|
2018-05-13 10:52:36 +03:00
|
|
|
remoteServerName:remoteServerName
|
|
|
|
nvimArgs:nvimArgs];
|
2017-06-12 01:30:33 +03:00
|
|
|
DLOG("Started neovim server '%s' with args '%@' and connected it with the remote agent '%s'.",
|
|
|
|
localServerName.cstr, nvimArgs, remoteServerName.cstr);
|
2016-08-26 16:31:30 +03:00
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
[_neovim_server notifyReadiness];
|
|
|
|
}
|
|
|
|
|
2016-07-10 14:51:00 +03:00
|
|
|
CFRunLoopRun();
|
2017-01-08 21:01:34 +03:00
|
|
|
|
2017-12-20 23:35:36 +03:00
|
|
|
DLOG("NvimServer returning.");
|
2016-07-09 15:04:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|