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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/**
STExecutor.m
Common class for stalk and stexec tools
Copyright (c) 2002 Free Software Foundation
Written by: Stefan Urbanek <urbanek@host.sk>
Date: 2001
This file is part of the StepTalk project.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#import "STExecutor.h"
#import <StepTalk/StepTalk.h>
#import <StepTalk/STFileScript.h>
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSException.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSString.h>
NSString *STExecutorException = @"STExecutorException";
const char *STExecutorCommonOptions =
" -help print this message\n"
" -list-all-objects list all available objects\n"
" -list-classes list available classes\n"
" -list-objects list named instances\n\n"
" -language lang force use of language lang\n"
" -list-languages list available languages\n"
" -continue do not stop when one of scripts failed to execute\n\n";
//" -set obj=value define named object 'obj' with string value 'value'\n"
@implementation STExecutor
- (void)createEnvironment
{
[self subclassResponsibility:_cmd];
}
- (void) dealloc
{
RELEASE(conversation);
[super dealloc];
}
- (void)executeScripts
{
#ifndef DEBUG
NSString *logFmt = @"'%@': execution failed, reason: %@";
#endif
NSMutableArray *scriptArgs;
NSString *scriptFileName;
NSString *arg;
scriptFileName = [self nextArgument];
if(!scriptFileName)
{
[self executeScriptFromStandardInputArguments:nil];
return;
}
scriptArgs = [NSMutableArray array];
do
{
[scriptArgs removeAllObjects];
arg = [self nextArgument];
while(![arg isEqualToString:@","] && arg != nil)
{
[scriptArgs addObject:arg];
arg = [self nextArgument];
}
#ifndef DEBUG
NS_DURING
#endif
if([scriptFileName isEqualToString:@"-"])
{
[self executeScriptFromStandardInputArguments:scriptArgs];
}
else
{
[self executeScript:scriptFileName withArguments:scriptArgs];
}
#ifndef DEBUG
NS_HANDLER
if(contFlag)
{
NSLog(logFmt,scriptFileName,[localException reason]);
}
else
{
[localException raise];
}
NS_ENDHANDLER
#endif
} while( (scriptFileName = [self nextArgument]) );
}
- (void)executeScriptFromStandardInputArguments:(NSArray *)args
{
NSFileHandle *handle;
STContext *env;
NSString *source;
NSData *data;
handle = [NSFileHandle fileHandleWithStandardInput];
data = [handle readDataToEndOfFile];
source = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(langName)
{
[conversation setLanguage:langName];
}
else
{
STLanguageManager *langManager = [STLanguageManager defaultManager];
[conversation setLanguage:[langManager defaultLanguage]];
}
if(conversation)
{
NSDebugLog(@"Executing script from stdin");
if([args count] > 0)
{
if([conversation isKindOfClass:[STRemoteConversation class]])
{
[NSException raise:@"STExecutorException"
format:@"Passing arguments to distant environment"
@" is not implemented."];
}
else
{
env = [conversation context];
[env setObject:args forName:@"Args"];
}
}
[conversation interpretScript:source];
}
RELEASE(source);
}
- (void)executeScript:(NSString *)file withArguments:(NSArray *)args;
{
NSFileManager *manager = [NSFileManager defaultManager];
STEnvironment *env;
NSString *useLanguage;
NSString *source;
/* Get proper language name */
if( [manager fileExistsAtPath:file isDirectory:NULL] )
{
source = [NSString stringWithContentsOfFile:file];
if(langName)
{
NSDebugLog(@"Using language %@", langName);
useLanguage = langName;
}
else
{
STLanguageManager *langManager = [STLanguageManager defaultManager];
NSDebugLog(@"Using language for file extension %@",
[file pathExtension]);
useLanguage = [langManager languageForFileType:[file pathExtension]];
}
}
else
{
STScriptsManager *sm;
STFileScript *script;
/* Try to find it in standard script locations */
sm = [[STScriptsManager alloc] initWithDomainName:@"Shell"];
AUTORELEASE(sm);
script = [sm scriptWithName:file];
source = [script source];
if(!source)
{
[NSException raise:STExecutorException
format:@"Could not find script '%@'", file];
return;
}
else
{
useLanguage = [script language];
}
}
[conversation setLanguage:useLanguage];
if(conversation)
{
NSDebugLog(@"Executing script '%@'",file);
if([args count] > 0)
{
if([conversation isKindOfClass:[STRemoteConversation class]])
{
[NSException raise:@"STExecutorException"
format:@"Passing arguments to distant environment"
@" is not implemented."];
}
else
{
/* FIXME: do not cast */
env = (STEnvironment *)[conversation context];
[env setObject:args forName:@"Args"];
}
}
[conversation interpretScript:source];
}
else
{
[NSException raise:STExecutorException
format:@"Unable to create a StepTalk conversation."];
}
}
- (void)listLanguages
{
NSArray *languages;
NSEnumerator *enumerator;
NSString *name;
languages = [[STLanguageManager defaultManager] availableLanguages];
enumerator = [languages objectEnumerator];
while( (name = [enumerator nextObject]) )
{
printf("%s\n", [name cString]);
}
}
- (NSString *)nextArgument
{
if(currentArg < [arguments count])
{
return [arguments objectAtIndex:currentArg++];
}
return nil;
}
- (void)reuseArgument
{
currentArg--;
}
- (void)listObjects
{
NSEnumerator *enumerator;
NSDictionary *dict;
NSString *name;
NSArray *objects;
dict = [[conversation context] objectDictionary];
objects = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
enumerator = [objects objectEnumerator];
if(listObjects == STListAll)
{
while( (name = [enumerator nextObject]) )
{
printf("%s\n", [name cString]);
}
}
else if (listObjects == STListClasses)
{
while( (name = [enumerator nextObject]) )
{
if([[dict objectForKey:name] isClass])
{
printf("%s\n", [name cString]);
}
}
}
else /* (listObjects == STListInstances) */
{
while( (name = [enumerator nextObject]) )
{
if(! [[dict objectForKey:name] isClass])
{
printf("%s\n", [name cString]);
}
}
}
}
- (int)parseOptions
{
BOOL isOption = NO;
NSString *arg;
listObjects = STListNone;
while( (arg = [self nextArgument]) )
{
isOption = NO;
if( [arg isEqualToString:@"-"] )
{
isOption = NO;
}
else if( [arg hasPrefix:@"--"] )
{
arg = [arg substringFromIndex:2];
isOption = YES;
}
else if( [arg hasPrefix:@"-"] )
{
arg = [arg substringFromIndex:1];
isOption = YES;
}
if ([@"help" hasPrefix:arg])
{
[self printHelp];
return 1;
}
else if ([@"list-languages" hasPrefix:arg])
{
[self listLanguages];
return 1;
}
/*
else if ([@"list-engines" hasPrefix:arg])
{
[self listEngines];
return 1;
}
*/
else if ([@"list-objects" hasPrefix:arg])
{
listObjects = STListObjects;
}
else if ([@"list-classes" hasPrefix:arg])
{
listObjects = STListClasses;
}
else if ([@"list-all-objects" hasPrefix:arg])
{
listObjects = STListAll;
}
else if ([@"continue" hasPrefix:arg])
{
contFlag = YES;
}
else if ([@"language" hasPrefix:arg])
{
RELEASE(langName);
langName = [self nextArgument];
if(!langName)
{
[NSException raise:STExecutorException
format:@"Language name expected"];
}
}
else
{
if(!isOption)
{
break;
}
else
{
if( [self processOption:arg] )
{
return 1;
}
}
}
}
if(arg)
{
[self reuseArgument];
}
return 0;
}
- (void)beforeExecuting
{
}
- (void)afterExecuting
{
}
- (void)runWithArguments:(NSArray *)args
{
arguments = RETAIN(args);
currentArg = 1;
if([self parseOptions])
{
return;
}
[self beforeExecuting];
[self createConversation];
[self executeScripts];
[self afterExecuting];
if(listObjects != STListNone)
{
[self listObjects];
}
}
- (void)printHelp
{
[self subclassResponsibility:_cmd];
}
- (int)processOption:(NSString *)option
{
[self subclassResponsibility:_cmd];
return 0;
}
- (void)createConversation
{
[self subclassResponsibility:_cmd];
}
@end
|