diff --git a/Examples/Shell/AppKit.txt b/Examples/Shell/AppKit.txt new file mode 100644 index 0000000..7d834ee --- /dev/null +++ b/Examples/Shell/AppKit.txt @@ -0,0 +1,40 @@ +Simple AppKit Examples +---------------------- +To be able to try following examples you have to load the AppKit module: + + > Environment loadModule: 'AppKit' + + +How to get a filename using the Open panel + + > NSOpenPanel openPanel runModal ; filename + +Applications and files +---------------------- + +> Workspace := NSWorkspace sharedWorkspace + +How to launch an application + + > Workspace launchApplication:'application name' + +How to open a file + + > Workspace openFile:'file name' + +How to open a file with specified application + + > Workspace openFile:'file name' withApplication:'application name' + +How to open a file using the open panel + + > Workspace openFile:(NSOpenPanel openPanel runModal ; filename) + +Text +---- + +How to display an RTF file + + > text := NSArrtibutedString alloc + > text initWithPath: file documentAttributes:nil + > Transcript show: (text string) diff --git a/Examples/Shell/GNUmakefile b/Examples/Shell/GNUmakefile index 855278b..46b6183 100644 --- a/Examples/Shell/GNUmakefile +++ b/Examples/Shell/GNUmakefile @@ -26,7 +26,7 @@ include $(GNUSTEP_MAKEFILES)/common.make TOOL_NAME = stshell -stshell_OBJC_FILES = STShell.m stshell.m +stshell_OBJC_FILES = STShell.m STShell+output.m stshell.m ADDITIONAL_TOOL_LIBS += -lStepTalk -lreadline diff --git a/Examples/Shell/STShell+output.m b/Examples/Shell/STShell+output.m new file mode 100644 index 0000000..fdbff3d --- /dev/null +++ b/Examples/Shell/STShell+output.m @@ -0,0 +1,160 @@ +/** + STShell+output + + Copyright (c) 2002 Free Software Foundation + + Written by: Stefan Urbanek + Date: 2002 Jun 7 + + This file is part of the StepTalk project. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02111, USA. + + */ + +#import "STShell.h" + +#import + +#import +#import +#import +#import +#import +#import +#import +#import + +#include + +@implementation STShell(STShellOutput) +- show:(id)anObject +{ + printf("%s", [[anObject description] cString]); + + return self; +} +- showLine:(id)anObject +{ + [self show:anObject]; + putchar('\n'); + + return nil; +} + +- showResult:(id)obj +{ + int objIndex = [objectStack count] - 1; + int i; + const char *className = [NSStringFromClass([obj class]) cString]; + + if(obj) + { + if([obj isKindOfClass:[NSArray class]]) + { + printf("(%i) %s\n", objIndex, className); + + for(i = 0;i<[obj count]; i++) + { + printf("%i %s\n", i, + [self displayCStringForObject:[obj objectAtIndex:i]]); + } + + } + else if([obj isKindOfClass:[NSSet class]]) + { + printf("(%i) %s\n", objIndex, className); + + obj = [[obj allObjects] sortedArrayUsingSelector:@selector(compare:)]; + for(i = 0;i<[obj count]; i++) + { + printf("%s\n", i, + [self displayCStringForObject:[obj objectAtIndex:i]]); + } + + } + else if([obj isKindOfClass:[NSDictionary class]]) + { + NSString *key; + NSArray *keys; + + printf("(%i) %s\n", objIndex, className); + + keys = [[obj allKeys] sortedArrayUsingSelector:@selector(compare:)]; + + for(i = 0;i<[keys count]; i++) + { + key = [keys objectAtIndex:i]; + printf("%s : %s\n", + [self displayCStringForObject:key], + [self displayCStringForObject:[obj objectForKey:key]]); + } + } + else + { + printf("(%i) %s\n", objIndex, [self displayCStringForObject:obj]); + } + } + + return self; +} +- (char *)displayCStringForObject:(id)object +{ + NSString *str = [object description]; + + if( [str length] > 60 ) + { + str = [str substringToIndex:60]; + str = [str stringByAppendingString:@"..."]; + } + + return [str cString]; +} + +- showException:(NSException *)exception +{ + printf("Error (%s): %s\n", + [[exception name] cString], + [[exception reason] cString]); + + + return self; +} +- (id)listObjects +{ + int i; + id object; + NSString *str; + + printf("Objects\n"); + for(i = 0; i < [objectStack count]; i++) + { + object = [objectStack objectAtIndex:i]; + + str = [object description]; + + if( [str length] > 60 ) + { + str = [str substringToIndex:60]; + str = [str stringByAppendingString:@"..."]; + } + + printf("%4i: '%s' (%s)\n", i, + [str cString], + [[[object class] description] cString]); + } + return nil; +} +@end diff --git a/Examples/Shell/STShell.m b/Examples/Shell/STShell.m index efc9dce..30ecd36 100644 --- a/Examples/Shell/STShell.m +++ b/Examples/Shell/STShell.m @@ -165,7 +165,10 @@ int complete_handler(void) if(result) { - [objectStack addObject:result]; + if(result != objectStack) + { + [objectStack addObject:result]; + } [self showResult:result]; } else @@ -324,65 +327,6 @@ int complete_handler(void) return 0; } -- show:(id)anObject -{ - printf("%s", [[anObject description] cString]); - - return self; -} -- showLine:(id)anObject -{ - [self show:anObject]; - putchar('\n'); - - return nil; -} -- showResult:(id)anObject -{ - if(anObject) - { - printf("%i: ", [objectStack count] - 1); - [self show:anObject]; - putchar('\n'); - } - - return self; -} -- showException:(NSException *)exception -{ - printf("Error (%s): %s\n", - [[exception name] cString], - [[exception reason] cString]); - - - return self; -} -- (id)listObjects -{ - int i; - id object; - NSString *str; - - printf("Objects\n"); - for(i = 0; i < [objectStack count]; i++) - { - object = [objectStack objectAtIndex:i]; - - str = [object description]; - - if( [str length] > 40 ) - { - str = [str substringToIndex:40]; - str = [str stringByAppendingString:@"..."]; - } - - printf("%4i: '%s' (%s)\n", i, - [str cString], - [[[object class] description] cString]); - } - return nil; -} - - (void)exit { /* FIXME: this is not nice */ diff --git a/Examples/Shell/Unix.txt b/Examples/Shell/Unix.txt new file mode 100644 index 0000000..b29a975 --- /dev/null +++ b/Examples/Shell/Unix.txt @@ -0,0 +1,194 @@ +Unix shell equivalents +---------------------- + +StepTalk is not meant to be used for tasks that can be done using ordinary Unix +shells. But this does not mean, that it cannot be used for such tasks. In this +file you may find list of unix commands and tasks and their Smalltalk +equivalents. + +Contents: + + File Manipulation + Output + Paths and filenames + Network + Math + Date and Time + +File manipulation +----------------- + +> fm := NSFileManager defaultManager + +ls + + > (fm directoryContentsAtPath: '.') sortedArrayUsingSelector:#compare: + +pwd + + > fm currentDirectoryPath + +cd path + + > fm changeCurrentDirectoryPath: 'path' + +ln -s path other + + > fm createSymbolicLinkAtPath:'path' pathContent:'other' + +cp src dest + + > fm copyPath:'src' toPath:'dest' handler: nil + +cp file_list dest + + > file_list do: [ :file | fm copyPath:file toPath:'dest' handler: nil ] + +mv - as cp, movePath:toPath:handler: +ln - as cp, linkPath:toPath:handler: +rm - removeFileAtPath:handler: + +mkdir dir + + > fm createDirectoryAtPath:'dir' attributes:nil + +df path + + > fm fileSystemAttributesAtPath:'path' + +Output +------ + +echo 'string' + + > Transcript show:'string' + +cat file + + > Transcript show: (NSString stringWithContentsOfFile:'file') + +"write a string to a file" + + > ('string' writeToFile:'file' atomically:YES) + +"create a string from a fle" + + > str := NSString stringWithContentsOfFile:'file' + + +Paths and filenames +-------------------------------- +For more information, refer to the NSString documentation. +NSString methods for path manipulation: + + - fileSystemRepresentation + - isAbsolutePath + + - pathComponents + - lastPathComponent + - pathExtension + + - stringByAbbreviatingWithTildeInPath + - stringByAppendingPathComponent: + - stringByAppendingPathExtension: + - stringByDeletingLastPathComponent + - stringByDeletingPathExtension + - stringByExpandingTildeInPath + - stringByResolvingSymlinksInPath + - stringByStandardizingPath + - stringsByAppendingPaths: + +Examples: + + > path := '/usr/GNUstep/System/Applications/Ink.app' + > path pathComponents + (?) GSArray + 0 / + 1 usr + 2 GNUstep + 3 System + 4 Applications + 5 Ink.app + + > path lastPathComponent + (?) Ink.app + + > path pathExtension + (?) app + +In Smalltalk there is a symbolic selector '/' for NSString that is equivalent to +the 'stringByAppendingPathComponent:'. + + > path := 'somePath' + > filename := 'someFilename' + > fullPath := path / filename + +Network +------- + +localhost + + > NSHost currentHost name + +nslookup host_name + + > (NSHost hostWithName:'host_name') addresses + +nslookup host_address + + > (NSHost hostWithAddress:'host_address') names + +"download a file from the web" + + > data := NSData dataWithContentsOfURL:'url' + > data writeToFile:'file' atomically:YES + +"read a file from the web" + + > string := NSString stringWithContentsOfURL:'url' + + +Math +---- +Just like in: + > 1 + 1 +or in: + > a := b * c + + +Date and Time +------------- + +date + > NSDate date +...or... +date + > NSCalendarDate date + + +For more information read the NSDate and NSCalendarDate documentation + +NSCalendarDate methods: + +Creating a date + + + calendarDate + + dateWithString:calendarFormat: + + dateWithString:calendarFormat:locale: + + dateWithYear:month:day:hour:minute:second:timeZone: + +Retrieving date elements + + - dayOfCommonEra + - dayOfMonth + - dayOfWeek + - dayOfYear + - hourOfDay + - minuteOfHour + - monthOfYear + - secondOfMinute + - yearOfCommonEra + +Adjusting a date + + - dateByAddingYears:months:days:hours:minutes:seconds: diff --git a/Examples/Shell/stshell.m b/Examples/Shell/stshell.m index fcb721f..a8eb260 100644 --- a/Examples/Shell/stshell.m +++ b/Examples/Shell/stshell.m @@ -134,6 +134,8 @@ STEnvironment *env; STShell *shell; + [self parseArguments]; + if(!envName || [envName isEqualToString:@""]) { env = [STEnvironment defaultScriptingEnvironment]; @@ -145,6 +147,9 @@ [env loadModule:@"Foundation"]; [env addObjectFinderWithName:@"DistributedFinder"]; + + /* FIXME: make this an option */ + [env setFullScriptingEnabled:YES]; shell = [STShell sharedShell]; [shell setEnvironment:env];