2 // RApplicationDelegate.m
5 // Created by Byron Ellis on 6/26/07.
6 // Copyright 2007 __MyCompanyName__. All rights reserved.
9 #import "RApplicationDelegate.h"
10 #import "RInterpreter.h"
12 #include <sys/types.h>
14 #include <Security/Authorization.h>
15 #include <Security/AuthorizationTags.h>
17 @implementation RApplicationDelegate
19 - (BOOL)applicationShouldOpenUntitledFile:(NSApplication*)sender { return NO; }
20 - (void)applicationWillFinishLaunching:(NSNotification*)aNotification {
21 RInterpreter *ri = [RInterpreter sharedInterpreter];
23 if(!(NULL != getenv("RNOTERM") && strncmp(getenv("RNOTERM"),"t",1)==0) &&
24 ((NULL != getenv("EMACS") && strncmp(getenv("EMACS"),"t",1)==0) || (NULL != getenv("TERM"))))
25 [ri setAllowTerminal:YES];
26 if(NULL != getenv("RNOVEND"))
31 if(-1==lstat("/usr/bin/R-serve",&sb)) {
34 NSAlert *alert = [NSAlert alertWithMessageText:@"RExecServer can create a symbolic link named R-server in /usr/bin. Would you like to do this now?"
37 otherButton:nil informativeTextWithFormat:@""];
38 if(NSAlertDefaultReturn == [alert runModal]) {
40 AuthorizationRef auth;
41 if(errAuthorizationSuccess == AuthorizationCreate(NULL,kAuthorizationEmptyEnvironment,kAuthorizationFlagDefaults,&auth)) {
42 AuthorizationItem item = {kAuthorizationRightExecute,0,NULL,0};
43 AuthorizationRights rights = {1,&item};
44 if(errAuthorizationSuccess == AuthorizationCopyRights(auth,&rights,NULL,
45 kAuthorizationFlagDefaults|kAuthorizationFlagInteractionAllowed|kAuthorizationFlagPreAuthorize|kAuthorizationFlagExtendRights
47 char *toolpath = "/bin/ln";
50 toolargs[1] = (char*)[[[NSBundle mainBundle] pathForResource:@"RExecServer" ofType:@"sh"] UTF8String];
51 toolargs[2] = "/usr/bin/R-serve";
53 if(errAuthorizationSuccess != (status = AuthorizationExecuteWithPrivileges(auth,toolpath,kAuthorizationFlagDefaults,toolargs,NULL)))
54 NSLog(@"Unable to execute tool [%d]: %s %s %s %s",status, toolpath,toolargs[0],toolargs[1],toolargs[2]);
56 AuthorizationFree(auth,kAuthorizationFlagDefaults);
61 symlink([[NSBundle mainBundle] executablePath],"/usr/bin/R-serve");
70 - (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
71 //Process the remaining events and then bail out to initialize R.
72 [NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
73 location:NSMakePoint(0,0)