aboutsummaryrefslogtreecommitdiffstats
path: root/LispREPL.m
blob: 90bd08b3794fc4d607544256ab77774b57c9b178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
//  LispREPL.m
//  Moxie
//
//  Created by Brian Cully on Sat Aug 14 2004.
//  Copyright (c) 2004 Brian Cully. All rights reserved.
//

#import "LispREPL.h"
#import "LispREPLController.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

enum repl_lock_condition { NO_DATA, HAS_DATA };

@implementation LispREPL
void
sig_pipe(int signo)
{
	NSLog(@"WARNING: SIGPIPE caught.");
}

void
sig_child(int signo)
{
	int status;
    
	if (wait(&status) == -1) {
		NSLog(@"WARNING: Couldn't clean up child: %s.\n", strerror(errno));
		return;
	}
}

+ (LispREPL *)sharedREPL
{
    static LispREPL *sharedREPL;
    
    if (sharedREPL == nil)
        sharedREPL = [[self alloc] init];
    return sharedREPL;
}

- (int)initLispProcess;
{
    NSString *helperPath;
    NSString *resLocation, *pluginLocation, *frameworkLocation;
    int pin[2], pout[2], res[2];
    pid_t pid;
    struct sigaction sa, ocsa, opsa;

    helperPath = [[NSBundle mainBundle] pathForAuxiliaryExecutable: @"startlisp"];
    if (helperPath == nil) {
        NSLog(@"Couldn't find helper application in resources!");
        return -1;
    }

    resLocation = [[NSBundle mainBundle] resourcePath];
    pluginLocation = [[NSBundle mainBundle] builtInPlugInsPath];
    frameworkLocation = [[NSBundle mainBundle] privateFrameworksPath];
    
    if (pipe(pin) == -1 || pipe(pout) == -1 || pipe(res) == -1) {
        NSLog(@"Couldn't create pipes for REPL!");
        return -1;
    }

    sa.sa_flags = 0;
    sigemptyset(&sa.sa_mask);
    sa.sa_handler = sig_child;
    sigaction(SIGCHLD, &sa, &ocsa);
    sa.sa_handler = sig_pipe;
    sigaction(SIGPIPE, &sa, &opsa);

    setsid();
    pid = fork();
    if (pid == -1) {
        NSLog(@"ERROR: Couldn't fork off lisp process: %s.", strerror(errno));
        return -1;
    } else if (pid == 0) {
        sa.sa_flags = 0;
        sigemptyset(&sa.sa_mask);
        sa.sa_handler = SIG_DFL;
        sigaction(SIGCHLD, &sa, NULL);
        sigaction(SIGPIPE, &sa, NULL);
        
        close(pin[1]); close(pout[0]); close(res[0]);
        if (dup2(pin[0], STDIN_FILENO) == -1 ||
            dup2(pout[1], STDOUT_FILENO) == -1 ||
            dup2(pout[1], STDERR_FILENO) == -1 ||
            dup2(res[1], STDERR_FILENO+1) == -1) {
            NSLog(@"ERROR: Couldn't setup standard I/O pipes: %s.", strerror(errno));
            exit(1);
        }
        execl([helperPath UTF8String], [[helperPath lastPathComponent] UTF8String],
              [resLocation UTF8String], [pluginLocation UTF8String], [frameworkLocation UTF8String], NULL);
        exit(1);
    }
    
    close(pin[0]); close(pout[1]); close(res[1]);
    theStdinWriter = [[NSFileHandle alloc] initWithFileDescriptor: pin[1]
                                                   closeOnDealloc: YES];
    theStdoutReader = [[NSFileHandle alloc] initWithFileDescriptor: pout[0]
                                                    closeOnDealloc: YES];
    theResultReader = [[NSFileHandle alloc] initWithFileDescriptor: res[0]
                                                    closeOnDealloc: YES];
        
    return 0;
}

- (id)init
{
    self = [super init];
    if (self) {
        [self initLispProcess];
        
        // Set up the result reader thread.
        theREPLResults = [[NSMutableArray alloc] init];
        theCommandHandlers = [[NSMutableDictionary alloc] init];
        theDispatcherLock = [[NSConditionLock alloc] initWithCondition: NO_DATA];
        theLispIsLoaded = NO;
        [NSThread detachNewThreadSelector: @selector(readREPLResults:)
                                 toTarget: self
                               withObject: [self resultReader]];
        theDispatcherTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1
                                                              target: self
                                                            selector: @selector(dispatchREPLResults:)
                                                            userInfo: nil
                                                             repeats: YES];
    }
    return self;
}

- (void)dealloc
{
    // Gotta kill the reader thread here.
    [theDispatcherLock release];
    [theDispatcherTimer release];
    
    // Then close file handles.
    [[self stdinWriter] release];
    [[self stdoutReader] release];
    [[self resultReader] release];
    
    // Then release our data.
    [theREPLResults release];
    [theCommandHandlers release];
    [super dealloc];
}

- (BOOL)isLoaded
{
    return theLispIsLoaded;
}

- (NSArray *)handlerInfoForCommand: (id)command
{
    NSArray *handlerInfo;

    handlerInfo = [[self commandHandlers] objectForKey: [command isKindOfClass: [NSString class]] ? command : [command stringValue]];
    if (handlerInfo && [handlerInfo count] == 2)
        return handlerInfo;
    return nil;
}

- (void)setHandlerInfo: (NSArray *)handlerInfo forCommand: (NSString *)command
{
    if ([handlerInfo count] == 2)
        [[self commandHandlers] setObject: handlerInfo forKey: command];
    else if (handlerInfo == nil)
        [[self commandHandlers] removeObjectForKey: command];
}

- (void)addCommand: (NSString *)command
           handler: (id)object
          selector: (SEL)handler
{
    [self setHandlerInfo: [NSArray arrayWithObjects: object, NSStringFromSelector(handler), nil]
              forCommand: command];
}

- (void)removeCommand: (NSString *)command
{
    [self setHandlerInfo: nil forCommand: command];
}

- (void)runCommand: (NSString *)command withObjects: (id)objects
{
    id handler;
    SEL selector;

    handler = [self handlerForCommand: command];
    if (handler) {
        selector = [self selectorForCommand: command];

        NS_DURING
            [handler performSelector: selector withObject: objects];
        NS_HANDLER
            NSLog(@"WARNING: Got exception: %@ (%@) while evaluating %@:%@",
                  [localException name], [localException reason], handler, NSStringFromSelector(selector));
        NS_ENDHANDLER
    }
}

- (BOOL)commandHasHandler: (NSString *)command
{
    if ([self handlerInfoForCommand: command])
        return YES;
    return NO;
}

- (id)handlerForCommand: (NSString *)command
{
    return [[self handlerInfoForCommand: command] objectAtIndex: 0];
}

- (SEL)selectorForCommand: (NSString *)command
{
    return NSSelectorFromString([[self handlerInfoForCommand: command] objectAtIndex: 1]);
}

- (void)eval: (id)aForm
{
    [[self stdinWriter] writeData: [[NSString stringWithFormat: @"%@\n", aForm]
        dataUsingEncoding: NSUTF8StringEncoding]];
}

/*
 * Read and parse result forms from the REPL. Once parsed into arrays, dispatch them via
 * the main thread by adding them to a worker queue.
 */
- (void)readREPLResults: (NSFileHandle *)resultsHandle
{
    NS_DURING
        while (1) {
            NSAutoreleasePool *rp;
            id results;
            
            rp = [[NSAutoreleasePool alloc] init];
            results = [resultsHandle readLispForm];
            [theDispatcherLock lock];
            [theREPLResults addObject: results];
            [theDispatcherLock unlock];
            [rp release];
        }
    NS_HANDLER
        [self performSelectorOnMainThread: @selector(replDiedSelector:)
                               withObject: self
                            waitUntilDone: NO];
    NS_ENDHANDLER
}

- (void)replDiedSelector: (id)sender
{
    [[LispREPLController sharedController] showWindow: self];
    NSRunCriticalAlertPanel(@"REPL died", @"The REPL sub-process has died, and Moxie cannot continue.",
                            @"Quit", nil, nil);
    [NSApp stop: self]; 
}

/*
 * Wait for updates on the queue and dispatch results to the appropriate callback
 * method on the values, if they're there, and call regular callbacks on the rest of
 * the elements.
 */
- (void)dispatchREPLResults: (NSNotification *)resultsNotification
{
    [theDispatcherLock lock];
    if (theLispIsLoaded == NO) {
        NSNotificationCenter *defaultCenter;
        theLispIsLoaded = YES;
        
        defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter postNotificationName: LispFinishedLoadingNotification
                                     object: self];
    }

    while ([theREPLResults count] > 0) {
        id result;
        
        result = [theREPLResults objectAtIndex: 0];
        if ([result isKindOfClass: [NSArray class]]) {
            NSString *command;
            NSArray *argArray;
            
            command = [result objectAtIndex: 0];
            argArray = [result subarrayWithRange: NSMakeRange(1, [result count] - 1)];
            [self runCommand: command withObjects: argArray];
        } else {
            NSLog(@"ERROR: Couldn't dispatch results: %@", result);
        }
        [theREPLResults removeObjectAtIndex: 0];
    }
    [theDispatcherLock unlock];
}
@end

@implementation LispREPL (Accessors)
- (NSMutableDictionary *)commandHandlers
{
    return theCommandHandlers;
}

- (NSFileHandle *)stdinWriter
{
    return theStdinWriter;
}

- (NSFileHandle *)stdoutReader
{
    return theStdoutReader;
}

- (NSFileHandle *)resultReader
{
    return theResultReader;
}
@end