Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 170 lines (144 sloc) 3.573 kb
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
//
// CLIProxy.mm
// Dialog2
//
// Created by Ciaran Walsh on 16/02/2008.
//

#import "CLIProxy.h"
#import "TMDCommand.h"

@interface CLIProxy (Private)
- (NSArray*)arguments;
@end

@implementation CLIProxy
+ (id)proxyWithOptions:(NSDictionary*)options;
{
return [[(CLIProxy*)[[self class] alloc] initWithOptions:options] autorelease];
}

- (id)initWithOptions:(NSDictionary*)options
{
if(self = [super init])
{
inputHandle = [[NSFileHandle fileHandleForReadingAtPath:[options objectForKey:@"stdin"]] retain];
outputHandle = [[NSFileHandle fileHandleForWritingAtPath:[options objectForKey:@"stdout"]] retain];
errorHandle = [[NSFileHandle fileHandleForWritingAtPath:[options objectForKey:@"stderr"]] retain];
arguments = [[options objectForKey:@"arguments"] retain];
environment = [[options objectForKey:@"environment"] retain];
workingDirectory = [[options objectForKey:@"cwd"] retain];
}
return self;
}

- (void)dealloc
{
[inputHandle release];
[outputHandle release];
[errorHandle release];
[arguments release];
[environment release];
[workingDirectory release];
[parameters release];
[super dealloc];
}

- (NSDictionary*)parameters
{
if(!parameters)
{
NSMutableDictionary* res = [NSMutableDictionary dictionary];
if(id plist = [TMDCommand readPropertyList:[self inputHandle] error:NULL])
{
if([plist isKindOfClass:[NSDictionary class]])
res = plist;
}

NSString* lastKey = nil;
for(size_t i = 2; i < [arguments count]; ++i)
{
NSString* arg = [arguments objectAtIndex:i];
BOOL isOption = [arg hasPrefix:@"--"];
if(lastKey)
[res setObject:(isOption ? [NSNull null] : arg) forKey:lastKey];
lastKey = isOption ? [arg substringFromIndex:2] : nil;
}

if(lastKey)
[res setObject:[NSNull null] forKey:lastKey];

parameters = [res retain];
}
return parameters;
}

- (NSString*)workingDirectory
{
return workingDirectory;
}

- (NSDictionary*)environment
{
return environment;
}

- (NSArray*)arguments
{
if(!parsedOptions)
return arguments;
return [parsedOptions objectForKey:@"literals"];
}

- (int)numberOfArguments;
{
return [[self arguments] count];
}

- (NSString*)argumentAtIndex:(int)index;
{
id argument = nil;
if([[self arguments] count] > index)
argument = [[self arguments] objectAtIndex:index];
return argument;
}

- (id)valueForOption:(NSString*)option;
{
if(!parsedOptions)
{
NSLog(@"Error: -valueForOption: called without first setting an option template");
return nil;
}
return [[parsedOptions objectForKey:@"options"] objectForKey:option];
}

- (void)parseOptions
{
parsedOptions = ParseOptions([self arguments], optionTemplate, optionCount);
}

- (void)setOptionTemplate:(option_t const*)options count:(size_t)count;
{
optionTemplate = options;
optionCount = count;
[self parseOptions];
}

// ===================
// = Reading/Writing =
// ===================
- (void)writeStringToOutput:(NSString*)text;
{
[[self outputHandle] writeString:text];
}

- (void)writeStringToError:(NSString*)text;
{
[[self errorHandle] writeString:text];
}

- (id)readPropertyListFromInput;
{
NSString* error = nil;
id plist = [TMDCommand readPropertyList:[self inputHandle] error:&error];

if(error || !plist)
[self writeStringToError:error ?: @"unknown error parsing property list\n"];

return plist;
}

// ================
// = File handles =
// ================
- (NSFileHandle*)inputHandle;
{
return inputHandle;
}

- (NSFileHandle*)outputHandle;
{
return outputHandle;
}

- (NSFileHandle*)errorHandle;
{
return errorHandle;
}
@end
Something went wrong with that request. Please try again.