mirror of
https://github.com/gnustep/libs-steptalk.git
synced 2025-02-21 02:31:01 +00:00
Shell split
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/steptalk/trunk@13795 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
60f32db4b2
commit
600de3ee1e
6 changed files with 404 additions and 61 deletions
40
Examples/Shell/AppKit.txt
Normal file
40
Examples/Shell/AppKit.txt
Normal file
|
@ -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)
|
|
@ -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
|
||||
|
||||
|
|
160
Examples/Shell/STShell+output.m
Normal file
160
Examples/Shell/STShell+output.m
Normal file
|
@ -0,0 +1,160 @@
|
|||
/**
|
||||
STShell+output
|
||||
|
||||
Copyright (c) 2002 Free Software Foundation
|
||||
|
||||
Written by: Stefan Urbanek <urbanek@host.sk>
|
||||
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 <StepTalk/StepTalk.h>
|
||||
|
||||
#import <Foundation/NSArray.h>
|
||||
#import <Foundation/NSBundle.h>
|
||||
#import <Foundation/NSDebug.h>
|
||||
#import <Foundation/NSNotification.h>
|
||||
#import <Foundation/NSException.h>
|
||||
#import <Foundation/NSSet.h>
|
||||
#import <Foundation/NSString.h>
|
||||
#import <Foundation/NSValue.h>
|
||||
|
||||
#include <readline/readline.h>
|
||||
|
||||
@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
|
|
@ -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 */
|
||||
|
|
194
Examples/Shell/Unix.txt
Normal file
194
Examples/Shell/Unix.txt
Normal file
|
@ -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:
|
|
@ -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];
|
||||
|
|
Loading…
Reference in a new issue