move executable extensions code out to NSTask+GNUstepBase.m to avoid duplication

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@37590 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2014-01-13 12:08:04 +00:00
parent e92bfba565
commit f8006295b0
5 changed files with 93 additions and 93 deletions

View file

@ -36,6 +36,18 @@ extern "C" {
#if OS_API_VERSION(GS_API_NONE,GS_API_LATEST)
@interface NSTask (GNUstepBase)
/** Returns the set of extensions known to indicate an executable file
* type on systems which require that (currently mswindows).
*/
+ (NSSet*) executableExtensions;
/** Checks the specified file to see if it is executable or if by
* appending one of the +executableExtensions it can be made executable.
* The return value is the actual executable path or nil if the file
* cannot be executed.
*/
+ (NSString*) executablePath: (NSString*)aFile;
/** Returns the launch path for a tool given the name of a tool.<br />
* Locates the tool by looking in the standard directories and,
* if not found there, looking in the PATH set in the environment.<br />

View file

@ -26,34 +26,83 @@
#import "Foundation/NSFileManager.h"
#import "Foundation/NSPathUtilities.h"
#import "Foundation/NSProcessInfo.h"
#import "Foundation/NSSet.h"
#import "GNUstepBase/NSTask+GNUstepBase.h"
@implementation NSTask (GNUstepBase)
+ (NSSet*) executableExtensions
{
static NSSet *executable = nil;
#if defined(__MINGW__)
if (nil == executable)
{
NSMutableSet *m;
NSEnumerator *e;
NSString *s;
/* Get PATHEXT environment variable and split apart on ';'
*/
e = [[[[[NSProcessInfo processInfo] environment]
objectForKey: @"PATHEXT"]
componentsSeparatedByString: @";"] objectEnumerator];
m = [NSMutableSet set];
while (nil != (s = [e nextObject]))
{
/* We don't have a '.' in a file extension, but the
* environment variable probably does ... fix it.
*/
s = [s stringByTrimmingSpaces];
if ([s hasPrefix: @"."])
{
s = [s substringFromIndex: 1];
}
if ([s length] > 0)
{
[m addObject: s];
}
}
/* Make sure we at least have the EXE extension.
*/
[m addObject: @"EXE"];
ASSIGNCOPY(executable, m);
}
#endif
return executable;
}
static NSString*
executablePath(NSFileManager *mgr, NSString *path)
{
#if defined(__MINGW__)
NSString *tmp;
NSString *tmp = [path pathExtension];
if ([mgr isExecutableFileAtPath: path])
if ([tmp length] == 0)
{
return path;
NSEnumerator *e = [[NSTask executableExtensions] objectEnumerator];
NSString *ext = @"EXE";
/* Try 'EXE' first, but otherwise iterate through all available
* extensions to find an executable path.
*/
do
{
tmp = [path stringByAppendingPathExtension: ext];
if ([mgr isExecutableFileAtPath: path])
{
return path;
}
}
while (nil != (ext = [e nextObject]));
}
tmp = [path stringByAppendingPathExtension: @"exe"];
if ([mgr isExecutableFileAtPath: tmp])
else
{
return tmp;
}
tmp = [path stringByAppendingPathExtension: @"com"];
if ([mgr isExecutableFileAtPath: tmp])
{
return tmp;
}
tmp = [path stringByAppendingPathExtension: @"cmd"];
if ([mgr isExecutableFileAtPath: tmp])
{
return tmp;
if ([mgr isExecutableFileAtPath: path])
{
return path;
}
}
#else
if ([mgr isExecutableFileAtPath: path])
@ -64,6 +113,11 @@ executablePath(NSFileManager *mgr, NSString *path)
return nil;
}
+ (NSString*) executablePath: (NSString*)aPath
{
return executablePath([NSFileManager defaultManager], aPath);
}
+ (NSString*) launchPathForTool: (NSString*)name
{
NSEnumerator *enumerator;

View file

@ -52,6 +52,7 @@
#import "Foundation/NSURL.h"
#import "Foundation/NSValue.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#import "GNUstepBase/NSTask+GNUstepBase.h"
#import "GSPrivate.h"
@ -318,34 +319,7 @@ AbsolutePathOfExecutable(NSString *path, BOOL atLaunch)
if (nil == executable)
{
NSMutableSet *m;
/* Get PATHEXT environment variable and split apart on ';'
*/
e = [[[[[NSProcessInfo processInfo] environment]
objectForKey: @"PATHEXT"]
componentsSeparatedByString: @";"] objectEnumerator];
m = [NSMutableSet set];
while (nil != (s = [e nextObject]))
{
/* We don't have a '.' in a file extension, but the
* environment variable probably does ... fix it.
*/
s = [s stringByTrimmingSpaces];
if ([s hasPrefix: @"."])
{
s = [s substringFromIndex: 1];
}
if ([s length] > 0)
{
[m addObject: s];
}
}
/* Make sure we at least have the EXE extension.
*/
[m addObject: @"EXE"];
ASSIGNCOPY(executable, m);
executable = [[NSTask executableExtensions] copy];
}
e = [executable objectEnumerator];

View file

@ -62,6 +62,7 @@
#import "Foundation/NSValue.h"
#import "GSPrivate.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#import "GNUstepBase/NSTask+GNUstepBase.h"
#include <stdio.h>
@ -1670,36 +1671,7 @@ static NSStringEncoding defaultEncoding;
if (nil == executable)
{
NSMutableSet *m;
NSEnumerator *e;
NSString *s;
/* Get PATHEXT environment variable and split apart on ';'
*/
e = [[[[[NSProcessInfo processInfo] environment]
objectForKey: @"PATHEXT"]
componentsSeparatedByString: @";"] objectEnumerator];
m = [NSMutableSet set];
while (nil != (s = [e nextObject]))
{
/* We don't have a '.' in a file extension, but the
* environment variable probably does ... fix it.
*/
s = [s stringByTrimmingSpaces];
if ([s hasPrefix: @"."])
{
s = [s substringFromIndex: 1];
}
if ([s length] > 0)
{
[m addObject: s];
}
}
/* Make sure we at least have the EXE extension.
*/
[m addObject: @"EXE"];
ASSIGNCOPY(executable, m);
executable = [[NSTask executableExtensions] copy];
}
if (nil != [executable member: ext])
{

View file

@ -45,6 +45,7 @@
#import "Foundation/NSTimer.h"
#import "Foundation/NSLock.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#import "GNUstepBase/NSTask+GNUstepBase.h"
#import "GSPrivate.h"
#include <sys/types.h>
@ -771,17 +772,6 @@ pty_slave(const char* name)
arch_path = [arch_path stringByAppendingPathComponent: os];
full_path = [arch_path stringByAppendingPathComponent: libs];
#ifdef __MINGW__
/* As a convenience on windows, if the program was supplied without
* an extension (which means it can't be executable) try using the
* most common extension.
*/
if ([[prog pathExtension] length] == 0)
{
prog = [prog stringByAppendingPathExtension: @"exe"];
}
#endif
lpath = [full_path stringByAppendingPathComponent: prog];
if ([mgr isExecutableFileAtPath: lpath] == NO)
{
@ -812,8 +802,12 @@ pty_slave(const char* name)
}
if (lpath != nil)
{
/*
* Make sure we have a standardised absolute path to pass to execve()
/* Fix up path by adding any extension required on systems like
* mswindows which don't work by file permission.
*/
lpath = [NSTask executablePath: lpath];
/* Make sure we have a standardised absolute path to pass to execve()
*/
if ([lpath isAbsolutePath] == NO)
{
@ -823,12 +817,6 @@ pty_slave(const char* name)
}
lpath = [lpath stringByStandardizingPath];
}
#ifdef __MINGW__
/** We need this to be native windows format, and some of the standardisation
* above may have left unix style separators in the string.
*/
lpath = [lpath stringByReplacingString: @"/" withString: @"\\"];
#endif
return lpath;
}