fix argv[0] discovery.

This commit is contained in:
Vítor Santos Costa 2013-01-13 17:55:13 +00:00
parent ec37045ca3
commit 798afc81c4
16 changed files with 89 additions and 30 deletions

View File

@ -333,6 +333,7 @@
#include "Yap.h"
#include "clause.h"
#include "yapio.h"
#include "Foreign.h"
#include "attvar.h"
#include "SWI-Stream.h"
#if HAVE_STDARG_H
@ -3261,8 +3262,14 @@ YAP_FastInit(char saved_state[])
init_args.DelayedReleaseLoad = 3;
init_args.PrologShouldHandleInterrupts = FALSE;
init_args.ExecutionMode = INTERPRETED;
init_args.Argc = 0;
init_args.Argv = NULL;
init_args.Argc = 1;
{
size_t l1 = 2*sizeof(char *);
if (!(init_args.Argv = (char **)malloc(l1)))
return YAP_BOOT_ERROR;
init_args.Argv[0] = Yap_FindExecutable ();
init_args.Argv[1] = NULL;
}
init_args.ErrorNo = 0;
init_args.ErrorCause = NULL;
init_args.QuietMode = FALSE;

View File

@ -26,9 +26,10 @@
* FindExecutable(argv[0]) should be called on yap initialization to
* locate the executable of Yap
*/
void
Yap_FindExecutable(char *name)
char *
Yap_FindExecutable(void)
{
return NULL;
}

View File

@ -54,8 +54,8 @@ this code is no being maintained anymore
* YAP_FindExecutable(argv[0]) should be called on yap initialization to
* locate the executable of Yap
*/
void
Yap_FindExecutable(char *name)
char *
Yap_FindExecutable(void)
{
register char *cp, *cp2;
struct stat stbuf;
@ -94,7 +94,7 @@ Yap_FindExecutable(char *name)
strcpy(LOCAL_FileNameBuf, GLOBAL_argv[0]);
Yap_TrueFileName(LOCAL_FileNameBuf, GLOBAL_Executable, TRUE);
if (oktox(GLOBAL_Executable))
return;
return GLOBAL_Executable;
else
Yap_Error(SYSTEM_ERROR,MkAtomTerm(Yap_LookupAtom(GLOBAL_Executable)),
"cannot find file being executed");

View File

@ -50,8 +50,8 @@ this code is no being maintained anymore
* YAP_FindExecutable(argv[0]) should be called on yap initialization to
* locate the executable of Yap
*/
void
Yap_FindExecutable(char *name)
char *
Yap_FindExecutable(void)
{
register char *cp, *cp2;
struct stat stbuf;
@ -64,7 +64,7 @@ Yap_FindExecutable(char *name)
if (oktox(GLOBAL_argv[0])) {
strcpy(LOCAL_FileNameBuf, GLOBAL_argv[0]);
Yap_TrueFileName(LOCAL_FileNameBuf, GLOBAL_Executable, TRUE);
return;
return NULL;
}
}
if (*cp == ':')
@ -84,16 +84,17 @@ Yap_FindExecutable(char *name)
if (!oktox(LOCAL_FileNameBuf))
continue;
Yap_TrueFileName(LOCAL_FileNameBuf, GLOBAL_Executable, TRUE);
return;
return GLOBAL_Executable;
}
/* one last try for dual systems */
strcpy(LOCAL_FileNameBuf, GLOBAL_argv[0]);
Yap_TrueFileName(LOCAL_FileNameBuf, GLOBAL_Executable, TRUE);
if (oktox(GLOBAL_Executable))
return;
return GLOBAL_Executable;
else
Yap_Error(SYSTEM_ERROR,MkAtomTerm(Yap_LookupAtom(GLOBAL_Executable)),
"cannot find file being executed");
return NULL;
}

View File

@ -24,6 +24,10 @@
#include <dlfcn.h>
#include <string.h>
#include <stdio.h>
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
typedef void (*prismf)(void);
@ -57,9 +61,29 @@ Yap_CallFunctionByName(const char *thing_string)
* YAP_FindExecutable(argv[0]) should be called on yap initialization to
* locate the executable of Yap
*/
void
Yap_FindExecutable(char *name)
char *
Yap_FindExecutable(void)
{
if (GLOBAL_argv && GLOBAL_argv[0])
return GLOBAL_argv[0];
#if HAVE_GETEXECNAME
return getxecname();
#elif __APPLE__
char path[1024];
uint32_t size = sizeof(path);
if (!_NSGetExecutablePath(path, &size)) {
size_t sz = strlen(path);
char *rc = malloc(sz+1);
strncpy(rc, path, sz);
return rc;
} else {
char *rc = malloc(size+1);
if (_NSGetExecutablePath(rc, &size) == 0)
return "yap";
return rc;
}
#endif
return "yap";
}
void *
@ -200,7 +224,7 @@ Yap_ShutdownLoadForeign(void)
if (dlclose(objs->handle) != 0)
return; /* ERROR */
objs = objs->next;
Yap_FreeCodeSpace(old);
Yap_FreeCodeSpace((ADDR)old);
}
libs = f_code->libs;
while (libs != NULL) {
@ -208,7 +232,7 @@ Yap_ShutdownLoadForeign(void)
if (dlclose(libs->handle) != 0)
return; /* ERROR */
libs = libs->next;
Yap_FreeCodeSpace(old);
Yap_FreeCodeSpace((ADDR)old);
}
f_code = f_code->next;
Yap_FreeCodeSpace((ADDR)of_code);

View File

@ -26,15 +26,15 @@ this code is no being maintained anymore
* YAP_FindExecutable(argv[0]) should be called on yap initialization to
* locate the executable of Yap
*/
void
Yap_FindExecutable(char *name)
char *
Yap_FindExecutable(void)
{
/* use dld_find_executable */
char *res;
if(name != NULL && (res=dld_find_executable(name))) {
strcpy(GLOBAL_Executable,res);
return GLOBAL_Executable;
} else {
strcpy(GLOBAL_Executable,"./yap");
return "yap";
}
}

View File

@ -30,6 +30,7 @@
void
Yap_FindExecutable(char *name)
{
return NULL;
}
void *

View File

@ -65,6 +65,18 @@ mydlerror(void)
void
Yap_FindExecutable(char *name)
{
char path[1024];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0) {
char *rc = malloc(size+1);
strncpy(rc, path, size);
return rc;
} else {
char *rc = malloc(size+1);
if (_NSGetExecutablePath(rc, &size) == 0)
return "yap";
return rc;
}
}

View File

@ -225,10 +225,6 @@ p_open_shared_objects( USES_REGS1 ) {
void
Yap_InitLoadForeign( void )
{
if (GLOBAL_argv == NULL)
Yap_FindExecutable("yap");
else
Yap_FindExecutable(GLOBAL_argv[0]);
Yap_InitCPred("$load_foreign_files", 3, p_load_foreign, SafePredFlag|SyncPredFlag);
Yap_InitCPred("$open_shared_objects", 0, p_open_shared_objects, SafePredFlag);
Yap_InitCPred("$open_shared_object", 3, p_open_shared_object, SyncPredFlag);

View File

@ -26,9 +26,10 @@
* YAP_FindExecutable(argv[0]) should be called on yap initialization to
* locate the executable of Yap
*/
void
Yap_FindExecutable(char *name)
char *
Yap_FindExecutable(void)
{
return NULL;
}

View File

@ -16,7 +16,7 @@
* locate the executable of Yap
*/
void Yap_FindExecutable(char *name)
char * Yap_FindExecutable(void)
{
}

View File

@ -4007,8 +4007,11 @@ p_argv( USES_REGS1 )
static Int
p_executable( USES_REGS1 )
{
Yap_TrueFileName (GLOBAL_argv[0], LOCAL_FileNameBuf, FALSE);
if (GLOBAL_argv && GLOBAL_argv[0])
Yap_TrueFileName (GLOBAL_argv[0], LOCAL_FileNameBuf, FALSE);
else
strncpy(LOCAL_FileNameBuf,Yap_FindExecutable (), YAP_FILENAME_MAX) ;
return Yap_unify(MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf)),ARG1);
}

View File

@ -105,7 +105,7 @@ typedef void (*YapInitProc)(void);
#define STD_PROTO(F,A) F A
#endif
void STD_PROTO(Yap_FindExecutable,(char *));
char *STD_PROTO(Yap_FindExecutable,(void));
void *STD_PROTO(Yap_LoadForeignFile,(char *, int));
int STD_PROTO(Yap_CallForeignFile,(void *, char *));
int STD_PROTO(Yap_CloseForeignFile,(void *));

View File

@ -186,6 +186,7 @@
#undef HAVE_FTIME
#undef HAVE_GETCWD
#undef HAVE_GETENV
#undef HAVE_GETEXECNAME
#undef HAVE_GETHOSTBYNAME
#undef HAVE_GETHOSTENT
#undef HAVE_GETHOSTID

11
configure vendored
View File

@ -10250,6 +10250,17 @@ _ACEOF
fi
done
for ac_func in getexecname
do :
ac_fn_c_check_func "$LINENO" "getexecname" "ac_cv_func_getexecname"
if test "x$ac_cv_func_getexecname" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_GETEXECNAME 1
_ACEOF
fi
done
for ac_func in gethostbyname gethostent gethostid gethostname
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`

View File

@ -2069,6 +2069,7 @@ AC_CHECK_FUNCS(alloca asinh atanh chdir clock clock_gettime)
AC_CHECK_FUNCS(ctime dlopen dup2)
AC_CHECK_FUNCS(erf feclearexcept)
AC_CHECK_FUNCS(fesettrapenable fgetpos finite fpclass ftime getcwd getenv)
AC_CHECK_FUNCS(getexecname)
AC_CHECK_FUNCS(gethostbyname gethostent gethostid gethostname)
AC_CHECK_FUNCS(gethrtime getpagesize)
AC_CHECK_FUNCS(getpwnam getrlimit getrusage gettimeofday getwd)