2016-07-09 15:04:09 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
2016-10-22 18:25:43 +03:00
|
|
|
@import Foundation;
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
#import "NeoVimServer.h"
|
|
|
|
#import "server_globals.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
|
|
|
|
|
|
|
|
|
|
|
NeoVimServer *_neovim_server;
|
2017-01-08 20:45:14 +03:00
|
|
|
CFRunLoopRef _mainRunLoop;
|
|
|
|
|
|
|
|
void observe_parent_termination() {
|
|
|
|
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(
|
|
|
|
DISPATCH_SOURCE_TYPE_PROC, (uintptr_t) parentPID, DISPATCH_PROC_EXIT, queue
|
|
|
|
);
|
|
|
|
|
|
|
|
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-01-08 21:01:34 +03:00
|
|
|
quit_neovim();
|
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];
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-10 15:43:26 +03:00
|
|
|
_neovim_server = [[NeoVimServer alloc] initWithLocalServerName:localServerName remoteServerName:remoteServerName];
|
2016-08-26 16:31:30 +03:00
|
|
|
DLOG("Started neovim server '%s' and connected it with the remote agent '%s'.",
|
|
|
|
localServerName.cstr, remoteServerName.cstr);
|
|
|
|
|
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-01-08 23:35:54 +03:00
|
|
|
DLOG("NeoVimServer returning.");
|
2016-07-09 15:04:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|