This commit is contained in:
Vítor Santos Costa 2016-01-31 10:33:32 +00:00
parent 8c873350c9
commit 5073ff9b43
3 changed files with 336 additions and 291 deletions

View File

@ -55,7 +55,7 @@ static int chdir(char *);
void exit(int); void exit(int);
#ifdef __WINDOWS__ #ifdef _WIN32
void void
Yap_WinError(char *yap_error) Yap_WinError(char *yap_error)
{ {
@ -452,7 +452,7 @@ PrologPath(const char *Y, char *X) {
static bool ChDir(const char *path) { static bool ChDir(const char *path) {
bool rc = false; bool rc = false;
char *qpath = Yap_AbsoluteFile(path, NULL, true); const char *qpath = Yap_AbsoluteFile(path, NULL, true);
#ifdef __ANDROID__ #ifdef __ANDROID__
if (GLOBAL_AssetsWD) { if (GLOBAL_AssetsWD) {
@ -487,7 +487,7 @@ static bool ChDir(const char *path) {
#else #else
rc = (chdir(qpath) == 0); rc = (chdir(qpath) == 0);
#endif #endif
free( qpath ); free( (void *)qpath );
return rc; return rc;
} }
#if _WIN32 || defined(__MINGW32__) #if _WIN32 || defined(__MINGW32__)
@ -501,7 +501,8 @@ BaseName(const char *X) {
return qpath; return qpath;
} }
char *
const har *
DirName(const char *X) { DirName(const char *X) {
char dir[YAP_FILENAME_MAX]; char dir[YAP_FILENAME_MAX];
char drive[YAP_FILENAME_MAX]; char drive[YAP_FILENAME_MAX];
@ -520,7 +521,7 @@ DirName(const char *X) {
} }
#endif #endif
static char *myrealpath( const char *path, char *out) static const char *myrealpath( const char *path, char *out)
{ {
#if _WIN32 || defined(__MINGW32__) #if _WIN32 || defined(__MINGW32__)
DWORD retval=0; DWORD retval=0;
@ -539,21 +540,28 @@ static char *myrealpath( const char *path, char *out)
return out; return out;
#elif HAVE_REALPATH #elif HAVE_REALPATH
{ {
char *rc = realpath(path,out); const char *rc;
char *s0; rc = ( const char *)realpath(path,out);
const char *s0, *s;
if (rc == NULL && (errno == ENOENT|| errno == EACCES)) { if (rc == NULL && (errno == ENOENT|| errno == EACCES)) {
char *s = basename((char *)path);
if ( is_directory(rc)) {
s = (const char *)path;
} else {
s = basename((char *)path);
path = dirname((char *)path);
}
s0 = malloc(strlen(s)+1); s0 = malloc(strlen(s)+1);
strcpy(s0, s); strcpy((char *)s0, s);
if ((rc = myrealpath(dirname((char *)path), out))==NULL) { if ((rc = myrealpath(path, out))==NULL) {
Yap_FileError(SYSTEM_ERROR_OPERATING_SYSTEM, TermNil, "could not find file %s: %s", path, strerror(errno)); Yap_FileError(SYSTEM_ERROR_OPERATING_SYSTEM, TermNil, "could not find file %s: %s", path, strerror(errno));
return NULL; return NULL;
} }
if(rc[strlen(rc)-1] != '/' ) if(rc[strlen(rc)-1] != '/' )
strcat(rc, "/"); strcat((char *)rc, "/");
strcat(rc, s0); strcat((char *)rc, s0);
free(s0); free((void *)s0);
} }
return rc; return rc;
} }
@ -596,39 +604,38 @@ PrologExpandVars(const char *spec, char *tmp0, bool ok_to)
return tmp; return tmp;
} }
/** /**
* generate absolute path, if ok first expand SICStus Prolog style * generate absolute path, if ok first expand SICStus Prolog style
* *
* @param spec the file path, including ~ and $ * @param[in] spec the file path, including `~` and `$`.
* @param tmp where to store the file * @param[out] tmp where to store the file.
* @param ok where to process ~and $ * @param[in] ok where to process `~` and `$`.
* *
* @return tmp, or NULL * @return tmp, or NULL
*/ */
char * const char *
Yap_AbsoluteFile(const char *spec, char *tmp, bool ok) Yap_AbsoluteFile(const char *spec, char *tmp, bool ok)
{ {
char *t1 = NULL; char *t1 = NULL;
t1 = PrologExpandVars(spec, t1, ok); t1 = PrologExpandVars(spec, t1, ok);
if (!t1) if (!t1)
return NULL; return NULL;
char *rc = myrealpath(t1, tmp); return myrealpath(t1, tmp);
return rc;
} }
/** /**
* @pred prolog_expanded_file_system_path( +PrologPath, +ExpandVars, -OSPath ) * @pred prolog_expanded_file_system_path( +PrologPath, +ExpandVars, -OSPath )
* *
* Apply basic transformations to paths, and conidtionally apply * Apply basic transformations to paths, and conidtionally apply
* traditional SICStus-style variable expansion. * traditional SICStus-style variable expansion.
* *
* @param PrologPath the source, may be atom or string * @param PrologPath the source, may be atom or string
* @param ExpandVars expand initial occurrence of ~ or $ * @param ExpandVars expand initial occurrence of ~ or $
* @param ExpandVars expand initial occurrence of ~ or $ * @param ExpandVars expand initial occurrence of ~ or $
* @param Prefix add this path before _PrologPath_ * @param Prefix add this path before _PrologPath_
* @param OSPath pathname. * @param OSPath pathname.
* *
* @return * @return
*/ */
static Int static Int
prolog_expanded_file_system_path( USES_REGS1 ) prolog_expanded_file_system_path( USES_REGS1 )
@ -639,38 +646,37 @@ prolog_expanded_file_system_path( USES_REGS1 )
char *o = LOCAL_FileNameBuf; char *o = LOCAL_FileNameBuf;
bool flag; bool flag;
const char *cmd, *p0; const char *cmd, *p0;
if (IsAtomTerm(t1)) { if (IsAtomTerm(t1)) {
cmd = RepAtom(AtomOfTerm(t1))->StrOfAE; cmd = RepAtom(AtomOfTerm(t1))->StrOfAE;
} else if (IsStringTerm(t1)) { } else if (IsStringTerm(t1)) {
cmd = StringOfTerm(t1); cmd = StringOfTerm(t1);
} else { } else {
return FALSE;
}
if (t2 == TermTrue)
flag = true;
else if (t2 == TermFalse)
flag = false;
else
return false; return false;
}
if (t2 == TermTrue) {
flag = true;
} else if (t2 == TermFalse) {
flag = false;
} else {
return false;
}
if (IsAtomTerm(t3)) { if (IsAtomTerm(t3)) {
p0 = RepAtom(AtomOfTerm(t3))->StrOfAE; p0 = RepAtom(AtomOfTerm(t3))->StrOfAE;
} else if (IsStringTerm(t3)) { } else if (IsStringTerm(t3)) {
p0 = StringOfTerm(t3); p0 = StringOfTerm(t3);
} else { } else {
return false;
return FALSE;
} }
const char *out = PrologExpandVars(cmd,o,flag); const char *out = PrologExpandVars(cmd,o,flag);
if (Yap_IsAbsolutePath(out)) { if (Yap_IsAbsolutePath(out)) {
return Yap_unify(MkAtomTerm(Yap_LookupAtom(out)), ARG4); return Yap_unify(MkAtomTerm(Yap_LookupAtom(out)), ARG4);
} else if (p0[0] == '\0') { } else if (p0[0] == '\0') {
char *rc = myrealpath(out, LOCAL_FileNameBuf2 ); const char *rc = myrealpath(out, LOCAL_FileNameBuf2 );
return Yap_unify(MkAtomTerm(Yap_LookupAtom(rc)), ARG4); return Yap_unify(MkAtomTerm(Yap_LookupAtom(rc)), ARG4);
} else { } else {
strncpy( LOCAL_FileNameBuf2, p0, YAP_FILENAME_MAX ); char *pt =stpncpy( LOCAL_FileNameBuf2, p0, YAP_FILENAME_MAX );
char *pt = LOCAL_FileNameBuf2 + strlen( LOCAL_FileNameBuf ); printf("%s\n", LOCAL_FileNameBuf2);
if ( !dir_separator( pt[-1] )) { if ( !dir_separator( pt[-1] )) {
#if ATARI || _MSC_VER || defined(__MINGW32__) #if ATARI || _MSC_VER || defined(__MINGW32__)
pt[0] = '\\'; pt[0] = '\\';
@ -678,16 +684,17 @@ prolog_expanded_file_system_path( USES_REGS1 )
pt[0] = '/'; pt[0] = '/';
#endif #endif
pt++; pt++;
pt[0] = '\n';
} }
out = strncpy( pt, out, YAP_FILENAME_MAX -(pt -LOCAL_FileNameBuf2) ); out = strncpy( pt, out, YAP_FILENAME_MAX -(pt -LOCAL_FileNameBuf2) );
char *rc = myrealpath(out, LOCAL_FileNameBuf ); const char *rc = myrealpath(LOCAL_FileNameBuf2, LOCAL_FileNameBuf );
return Yap_unify(MkAtomTerm(Yap_LookupAtom(rc)), ARG4); return Yap_unify(MkAtomTerm(Yap_LookupAtom(rc)), ARG4);
} }
} }
#define EXPAND_FILENAME_DEFS() \ #define EXPAND_FILENAME_DEFS() \
PAR("parameter_expansion", isatom, EXPAND_FILENAME_PARAMETER_EXPANSION), \ PAR("parameter_expansion", isatom, EXPAND_FILENAME_PARAMETER_EXPANSION), \
PAR("commands", boolean, EXPAND_FILENAME_COMMANDS), \ PAR("commands", booleanFlag, EXPAND_FILENAME_COMMANDS), \
PAR(NULL, ok, EXPAND_FILENAME_END) PAR(NULL, ok, EXPAND_FILENAME_END)
#define PAR(x, y, z) z #define PAR(x, y, z) z
@ -710,20 +717,22 @@ do_expand_file_name(Term t1, Term opts USES_REGS)
{ {
xarg *args; xarg *args;
expand_filename_enum_choices_t i; expand_filename_enum_choices_t i;
bool use_glob = false; bool use_system_expansion = true, glob_vs_wordexp = true;
char **ss = NULL; const char *tmp = NULL;
char *tmp = NULL;
const char *spec;
char *tmpe = NULL; char *tmpe = NULL;
size_t j, pathcount; const char *spec;
int flags = 0;
#if HAVE_WORDEXP
wordexp_t wresult;
#endif
#if HAVE_GLOB #if HAVE_GLOB
glob_t gresult; glob_t gresult;
#endif #endif
#if HAVE_GLOB || HAVE_WORDEXP
char **ss = NULL;
int flags = 0, j;
#endif
#if HAVE_WORDEXP
wordexp_t wresult;
#endif
Term tf;
if (IsVarTerm(t1)) { if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR, t1, NULL); Yap_Error(INSTANTIATION_ERROR, t1, NULL);
return TermNil; return TermNil;
@ -739,160 +748,180 @@ do_expand_file_name(Term t1, Term opts USES_REGS)
if (args == NULL) { if (args == NULL) {
return TermNil; return TermNil;
} }
tmpe = malloc(YAP_FILENAME_MAX+1);
for (i = 0; i < EXPAND_FILENAME_END; i++) { for (i = 0; i < EXPAND_FILENAME_END; i++) {
if (args[i].used) { if (args[i].used) {
Term t = args[i].tvalue; Term t = args[i].tvalue;
switch (i) { switch (i) {
case EXPAND_FILENAME_PARAMETER_EXPANSION: case EXPAND_FILENAME_PARAMETER_EXPANSION:
if (t == TermProlog) { if (t == TermProlog) {
use_glob = true; if (tmpe == NULL) {
tmpe = malloc(YAP_FILENAME_MAX+1); return TermNil;
if (tmpe == NULL) { }
return TermNil; tmpe = expandVars( spec, tmpe, YAP_FILENAME_MAX);
}
tmpe = expandVars( spec, tmpe, YAP_FILENAME_MAX);
#ifdef GLOB_BRACE #ifdef GLOB_BRACE
flags = GLOB_BRACE|GLOB_TILDE; flags = GLOB_BRACE|GLOB_TILDE;
#endif #endif
flags |= GLOB_NOCHECK; //#ifdef GLOB_NOCHECK
spec = tmpe; // flags |= GLOB_NOCHECK;
} else if (t == TermTrue) { //#endif
use_glob = false; spec = tmpe;
} else if (t == TermFalse) { } else if (t == TermTrue) {
use_glob = true; use_system_expansion = true;
} } else if (t == TermFalse) {
break; use_system_expansion = false;
case EXPAND_FILENAME_COMMANDS: }
if (!use_glob) { break;
if (t == TermFalse) { case EXPAND_FILENAME_COMMANDS:
flags = WRDE_NOCMD; if (!use_system_expansion) {
} use_system_expansion = true;
} #ifdef WRDE_NOCMD
case EXPAND_FILENAME_END: if (t == TermFalse) {
break; flags = WRDE_NOCMD;
}
#endif
}
case EXPAND_FILENAME_END:
break;
} }
} }
} }
#if _WIN32 || defined(__MINGW32__) #if _WIN32 || defined(__MINGW32__)
char u[YAP_FILENAME_MAX+1]; {
// first pass, remove Unix style stuff char u[YAP_FILENAME_MAX+1];
if (unix2win(spec, u, YAP_FILENAME_MAX) == NULL) WIN32_FIND_DATA find;
return NULL; HANDLE hFind;
spec = (const char *)u; CELL *dest;
#endif
if (tmp == NULL) { // first pass, remove Unix style stuff
tmp = malloc(YAP_FILENAME_MAX+1); if (unix2win(spec, u, YAP_FILENAME_MAX) == NULL)
if (tmp == NULL) { return TermNil;
spec = (const char *)u;
if (!use_system_expansion) {
return MkPairTerm(MkAtomTerm(Yap_LookupAtom(spec)), TermNil);
}
hFind = FindFirstFile(spec, &find);
if (hFind == INVALID_HANDLE_VALUE)
{
return TermNil; return TermNil;
} }
} else
#if ( __WIN32 || __MINGW32__ )
DWORD retval=0;
// notice that the file does not need to exist
if (ini == NULL) {
ini = malloc(strlen(w)+1);
}
retval = ExpandEnvironmentStrings(pattern,
expanded,
maxlen);
if (retval == 0)
{ {
Yap_WinError("Generating a full path name for a file" ); tf = AbsPair(HR);
return NULL; HR[0] = MkAtomTerm(Yap_LookupAtom(find.cFileName));
} HR[1] = TermNil;
return expanded; dest = HR+1;
HR += 2;
while (FindNextFile(hFind, &find)) {
*dest = AbsPair(HR);
HR[0] = MkAtomTerm(Yap_LookupAtom(find.cFileName));
HR[1] = TermNil;
dest = HR+1;
HR += 2;
}
FindClose(hFind);
}
return tf;
}
#elif HAVE_WORDEXP || HAVE_GLOB #elif HAVE_WORDEXP || HAVE_GLOB
if (!use_system_expansion) {
return MkPairTerm(MkAtomTerm(Yap_LookupAtom(spec)), TermNil);
}
/* Expand the string for the program to run. */ /* Expand the string for the program to run. */
if (use_glob) { size_t pathcount;
if ( glob_vs_wordexp ) {
#if HAVE_GLOB #if HAVE_GLOB
memset( &gresult,0,sizeof(gresult) );
switch (glob (spec, flags, NULL, &gresult)) switch (glob (spec, flags, NULL, &gresult))
{ {
case 0: /* Successful. */ case 0: /* Successful. */
ss = gresult.gl_pathv; ss = gresult.gl_pathv;
pathcount = gresult.gl_pathc; pathcount = gresult.gl_pathc;
if (pathcount) { if (pathcount) {
break; break;
}
case GLOB_NOMATCH:
globfree(&gresult);
{
return TermNil;
}
case GLOB_ABORTED:
PlIOError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "glob aborted: %sn", strerror(errno));
globfree (&gresult);
return TermNil;
case GLOB_NOSPACE:
Yap_Error(RESOURCE_ERROR_HEAP, ARG1, "glob ran out of space: %sn", strerror(errno));
globfree (&gresult);
return TermNil;
/* If the error was WRDE_NOSPACE,
then perhaps part of the result was allocated. */
default: /* Some other error. */
return TermNil;
} }
case GLOB_NOMATCH:
globfree(&gresult);
{
return TermNil;
}
case GLOB_ABORTED:
PlIOError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "glob aborted: %sn", strerror(errno));
globfree (&gresult);
return TermNil;
case GLOB_NOSPACE:
Yap_Error(RESOURCE_ERROR_HEAP, ARG1, "glob ran out of space: %sn", strerror(errno));
globfree (&gresult);
return TermNil;
/* If the error was WRDE_NOSPACE,
then perhaps part of the result was allocated. */
default: /* Some other error. */
return TermNil;
}
#endif #endif
} else { } else {
#if HAVE_WORDEXP #if HAVE_WORDEXP
int rc; int rc;
switch ((rc = wordexp (spec, &wresult, flags))) memset( &wresult,0,sizeof(wresult) );
{ switch ((rc = wordexp (spec, &wresult, flags)))
case 0: /* Successful. */ {
ss = wresult.we_wordv; case 0: /* Successful. */
pathcount = wresult.we_wordc; ss = wresult.we_wordv;
if (pathcount) { pathcount = wresult.we_wordc;
break; if (pathcount) {
} else { break;
Term t; } else {
char *out = LOCAL_FileNameBuf; Term t;
t = MkAtomTerm( Yap_LookupAtom( expandVars(spec, out, YAP_FILENAME_MAX-1) ) ); char *out = LOCAL_FileNameBuf;
wordfree (&wresult); t = MkAtomTerm( Yap_LookupAtom( expandVars(spec, out, YAP_FILENAME_MAX-1) ) );
return MkPairTerm( t, TermNil ); wordfree (&wresult);
return MkPairTerm( t, TermNil );
}
case WRDE_NOSPACE:
/* If the error was WRDE_NOSPACE,
then perhaps part of the result was allocated. */
Yap_Error(RESOURCE_ERROR_HEAP, ARG1, "wordexp ran out of space: %s", strerror(errno));
wordfree (&wresult);
return TermNil;
default: /* Some other error. */
PlIOError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "wordexp failed: %s", strerror(errno));
wordfree (&wresult);
return TermNil;
} }
case WRDE_NOSPACE:
/* If the error was WRDE_NOSPACE,
then perhaps part of the result was allocated. */
Yap_Error(RESOURCE_ERROR_HEAP, ARG1, "wordexp ran out of space: %s", strerror(errno));
wordfree (&wresult);
return TermNil;
default: /* Some other error. */
PlIOError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "wordexp failed: %s", strerror(errno));
wordfree (&wresult);
return TermNil;
}
#endif #endif
} }
Term tf = TermNil; tf = TermNil;
for (j = 0; j < pathcount; j++) { for (j = 0; j < pathcount; j++) {
const char *s = ss[pathcount-(j+1)]; const char *s = ss[pathcount-(j+1)];
#if HAVE_REALPATH #if HAVE_REALPATH
s = myrealpath(s, tmp); tmp = myrealpath(s,(char *) tmp);
#else
tmp = s;
#endif #endif
//if (!exists(s)) //if (!exists(s))
// continue; // continue;
Atom a = Yap_LookupAtom(s); Atom a = Yap_LookupAtom(s);
tf = MkPairTerm(MkAtomTerm( a ),tf); tf = MkPairTerm(MkAtomTerm( a ),tf);
} }
#else
// just use basic
if (expanded == NULL) {
expanded = malloc(strlen(pattern)+1);
}
strcpy(expanded, pattern);
#endif
if (tmp)
free( tmp );
if (tmpe)
free( tmpe );
#if HAVE_GLOB #if HAVE_GLOB
if (use_glob) if (use_system_expansion && glob_vs_wordexp)
globfree( &gresult ); globfree( &gresult );
#endif #endif
#if HAVE_WORDEXP #if HAVE_WORDEXP
if (!use_glob) if (use_system_expansion && !glob_vs_wordexp)
wordfree( &wresult ); wordfree( &wresult );
#endif
if (tmp)
free( (void *)tmp );
if (tmpe)
free( tmpe );
#else
// just use basic
return MkPairTerm(MkAtomTerm(Yap_LookupAtom(spec)), TermNil);
#endif #endif
return tf; return tf;
} }
@ -1291,7 +1320,7 @@ Yap_InitPageSize(void)
return true; return true;
} }
static char * static const char *
expandWithPrefix(const char *source, const char *root, char *result) expandWithPrefix(const char *source, const char *root, char *result)
{ {
char *work; char *work;
@ -1428,7 +1457,7 @@ Yap_InitPageSize(void)
root = expandWithPrefix( root, NULL, save_buffer ); root = expandWithPrefix( root, NULL, save_buffer );
} }
// { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "root= %s %s ", root, source) ; } // { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "root= %s %s ", root, source) ; }
char *work = expandWithPrefix( source, root, result ); const char *work = expandWithPrefix( source, root, (char *)result );
// expand names in case you have // expand names in case you have
// to add a prefix // to add a prefix
@ -1557,34 +1586,31 @@ Yap_InitPageSize(void)
static Int static Int
p_shell ( USES_REGS1 ) p_shell ( USES_REGS1 )
{ /* '$shell'(+SystCommand) */ { /* '$shell'(+SystCommand) */
const char *cmd;
Term t1 = Deref (ARG1);
if (IsAtomTerm(t1))
cmd = RepAtom(AtomOfTerm(t1))->StrOfAE;
else if (IsStringTerm(t1))
cmd = StringOfTerm(t1);
else
return FALSE;
#if _MSC_VER || defined(__MINGW32__) #if _MSC_VER || defined(__MINGW32__)
char *cmd; { int rval = system(cmd);
term_t A1 = Yap_InitSlot(ARG1);
if ( PL_get_chars(A1, &cmd, CVT_ALL|REP_FN|CVT_EXCEPTION) )
{ int rval = System(cmd);
return rval == 0; return rval == 0;
} }
return FALSE; return true;
#else #else
#if HAVE_SYSTEM #if HAVE_SYSTEM
char *shell; char *shell;
register int bourne = FALSE; register int bourne = FALSE;
Term t1 = Deref (ARG1);
const char *cmd;
shell = (char *) getenv ("SHELL"); shell = (char *) getenv ("SHELL");
if (!strcmp (shell, "/bin/sh")) if (!strcmp (shell, "/bin/sh"))
bourne = TRUE; bourne = TRUE;
if (shell == NIL) if (shell == NIL)
bourne = TRUE; bourne = TRUE;
if (IsAtomTerm(t1))
cmd = RepAtom(AtomOfTerm(t1))->StrOfAE;
else if (IsStringTerm(t1))
cmd = StringOfTerm(t1);
else
return FALSE;
/* Yap_CloseStreams(TRUE); */ /* Yap_CloseStreams(TRUE); */
if (bourne) if (bourne)
return system( cmd ) == 0; return system( cmd ) == 0;
@ -1636,10 +1662,26 @@ Yap_InitPageSize(void)
static Int static Int
p_system ( USES_REGS1 ) p_system ( USES_REGS1 )
{ /* '$system'(+SystCommand) */ { /* '$system'(+SystCommand) */
const char *cmd;
Term t1 = Deref (ARG1);
if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR,t1,"argument to system/1 unbound");
return FALSE;
} else if (IsAtomTerm(t1)) {
cmd = RepAtom(AtomOfTerm(t1))->StrOfAE;
} else if (IsStringTerm(t1)) {
cmd = StringOfTerm(t1);
} else {
if (!Yap_GetName (LOCAL_FileNameBuf, YAP_FILENAME_MAX, t1)) {
Yap_Error(TYPE_ERROR_ATOM,t1,"argument to system/1");
return false;
}
cmd = LOCAL_FileNameBuf;
}
/* Yap_CloseStreams(TRUE); */
#if _MSC_VER || defined(__MINGW32__) #if _MSC_VER || defined(__MINGW32__)
char *cmd;
term_t A1 = Yap_InitSlot(ARG1);
if ( PL_get_chars(A1, &cmd, CVT_ALL|REP_FN|CVT_EXCEPTION) )
{ STARTUPINFO si; { STARTUPINFO si;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
@ -1649,7 +1691,7 @@ Yap_InitPageSize(void)
// Start the child process. // Start the child process.
if( !CreateProcess( NULL, // No module name (use command line) if( !CreateProcess( NULL, // No module name (use command line)
cmd, // Command line (LPSTR)cmd, // Command line
NULL, // Process handle not inheritable NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE FALSE, // Set handle inheritance to FALSE
@ -1675,32 +1717,14 @@ Yap_InitPageSize(void)
return FALSE; return FALSE;
#elif HAVE_SYSTEM #elif HAVE_SYSTEM
Term t1 = Deref (ARG1);
const char *s;
if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR,t1,"argument to system/1 unbound");
return FALSE;
} else if (IsAtomTerm(t1)) {
s = RepAtom(AtomOfTerm(t1))->StrOfAE;
} else if (IsStringTerm(t1)) {
s = StringOfTerm(t1);
} else {
if (!Yap_GetName (LOCAL_FileNameBuf, YAP_FILENAME_MAX, t1)) {
Yap_Error(TYPE_ERROR_ATOM,t1,"argument to system/1");
return FALSE;
}
s = LOCAL_FileNameBuf;
}
/* Yap_CloseStreams(TRUE); */
#if _MSC_VER #if _MSC_VER
_flushall(); _flushall();
#endif #endif
if (system (s)) { if (system (cmd)) {
#if HAVE_STRERROR #if HAVE_STRERROR
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM,t1,"%s in system(%s)", strerror(errno), s); Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM,t1,"%s in system(%s)", strerror(errno), cmd);
#else #else
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM,t1,"in system(%s)", s); Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM,t1,"in system(%s)", cmd);
#endif #endif
return FALSE; return FALSE;
} }
@ -1874,38 +1898,6 @@ Yap_InitPageSize(void)
#endif #endif
} }
/* wrapper for alarm system call */
#if _MSC_VER || defined(__MINGW32__)
static DWORD WINAPI
DoTimerThread(LPVOID targ)
{
Int *time = (Int *)targ;
HANDLE htimer;
LARGE_INTEGER liDueTime;
htimer = CreateWaitableTimer(NULL, FALSE, NULL);
liDueTime.QuadPart = -10000000;
liDueTime.QuadPart *= time[0];
/* add time in usecs */
liDueTime.QuadPart -= time[1]*10;
/* Copy the relative time into a LARGE_INTEGER. */
if (SetWaitableTimer(htimer, &liDueTime,0,NULL,NULL,0) == 0) {
return(FALSE);
}
if (WaitForSingleObject(htimer, INFINITE) != WAIT_OBJECT_0)
fprintf(stderr,"WaitForSingleObject failed (%ld)\n", GetLastError());
Yap_signal (YAP_WINTIMER_SIGNAL);
/* now, say what is going on */
Yap_PutValue(AtomAlarm, MkAtomTerm(AtomTrue));
ExitThread(1);
#if _MSC_VER
return(0L);
#endif
}
#endif
static Int static Int
p_host_type( USES_REGS1 ) { p_host_type( USES_REGS1 ) {
Term out = MkAtomTerm(Yap_LookupAtom(HOST_ALIAS)); Term out = MkAtomTerm(Yap_LookupAtom(HOST_ALIAS));
@ -2291,4 +2283,4 @@ Yap_InitPageSize(void)
Yap_InitCPred ("rmdir", 2, p_rmdir, SyncPredFlag); Yap_InitCPred ("rmdir", 2, p_rmdir, SyncPredFlag);
Yap_InitCPred ("make_directory", 1, make_directory, SyncPredFlag); Yap_InitCPred ("make_directory", 1, make_directory, SyncPredFlag);
} }

View File

@ -13,22 +13,98 @@
* *
*/ */
#if _WIN32 || defined(__MINGW32__)
#if !defined(MINGW_HAS_SECURE_API)
#define MINGW_HAS_SECURE_API 1
#endif
//#undef _POSIX_
#endif
#include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h"
#include "yapio.h"
#include "eval.h"
#if _WIN32 || defined(__MINGW32__)
#include <winsock2.h>
/* Windows */
#include <io.h>
#include <windows.h>
#include <direct.h>
#include "Shlwapi.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STDARG_H
#include <stdarg.h>
#endif
#include <stdlib.h>
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef FENV_H
#include <fenv.h>
#endif
#define S_ISDIR(x) (((x)&_S_IFDIR)==_S_IFDIR)
#if HAVE_STDARG_H
#include <stdarg.h>
#endif
#if HAVE_CTYPE_H
#include <ctype.h>
#endif
#if HAVE_SYS_PARAMS_H
#include <sys/params.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_SYS_SELECT_H && !_MSC_VER && !defined(__MINGW32__)
#include <sys/select.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#if HAVE_LIBGEN_H
#include <libgen.h>
#endif
#if HAVE_WCTYPE_H
#include <wctype.h>
#endif
#if HAVE_LIMITS_H
#include <limits.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if !HAVE_STRNCAT
#define strncat(X,Y,Z) strcat(X,Y)
#endif
#if !HAVE_STRNCPY
#define strncpy(X,Y,Z) strcpy(X,Y)
#endif
#include "iopreds.h"
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef MPW
#define signal sigset
#endif
/* windows.h does not like absmi.h, this /* windows.h does not like absmi.h, this
should fix it for now */ should fix it for now */
#if _WIN32 || __MINGW32__
#include <winsock2.h>
#endif
#include "absmi.h"
#include "yapio.h"
#include "iopreds.h"
#include "alloc.h"
#include <math.h> #include <math.h>
#if STDC_HEADERS
#include <stdlib.h>
#endif
#if HAVE_WINDOWS_H
#include <windows.h>
#endif
#if HAVE_SYS_TIME_H && !_MSC_VER #if HAVE_SYS_TIME_H && !_MSC_VER
#include <sys/time.h> #include <sys/time.h>
#endif #endif
@ -41,12 +117,6 @@
#if HAVE_STRING_H #if HAVE_STRING_H
#include <string.h> #include <string.h>
#endif #endif
#if !HAVE_STRNCAT
#define strncat(X,Y,Z) strcat(X,Y)
#endif
#if !HAVE_STRNCPY
#define strncpy(X,Y,Z) strcpy(X,Y)
#endif
#if HAVE_GETPWNAM #if HAVE_GETPWNAM
#include <pwd.h> #include <pwd.h>
#endif #endif
@ -61,17 +131,6 @@
#include <fcntl.h> #include <fcntl.h>
#endif #endif
#if _MSC_VER || defined(__MINGW32__) #if _MSC_VER || defined(__MINGW32__)
#include <windows.h>
/* required for DLL compatibility */
#if HAVE_DIRECT_H
#include <direct.h>
#endif
#include <io.h>
#include <shlwapi.h>
#else
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#endif #endif
/* CYGWIN seems to include this automatically */ /* CYGWIN seems to include this automatically */
#if HAVE_FENV_H && !defined(__CYGWIN__) #if HAVE_FENV_H && !defined(__CYGWIN__)
@ -86,13 +145,7 @@
#if HAVE_LIBGEN_H #if HAVE_LIBGEN_H
#include <libgen.h> #include <libgen.h>
#endif #endif
#if HAVE_STDARG_H #if defined(HAVE_READLINE_READLINE_H)
#include <stdarg.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_READLINE_READLINE_H
#include <readline/readline.h> #include <readline/readline.h>
#endif #endif

View File

@ -114,7 +114,7 @@ int Yap_growtrail_in_parser(tr_fr_ptr *, TokEntry **, VarEntry **);
bool Yap_IsAbsolutePath(const char *p); bool Yap_IsAbsolutePath(const char *p);
Atom Yap_TemporaryFile(const char *prefix, int *fd); Atom Yap_TemporaryFile(const char *prefix, int *fd);
char *Yap_AbsoluteFile(const char *spec, char *tmp, bool expand); const char *Yap_AbsoluteFile(const char *spec, char *tmp, bool expand);
typedef enum mem_buf_source { typedef enum mem_buf_source {
MEM_BUF_CODE = 1, MEM_BUF_CODE = 1,