update PLStream to more recent version of SWI.

This commit is contained in:
Vitor Santos Costa
2010-02-22 09:35:47 +00:00
parent 4fda6b7488
commit 4a53759fc1
12 changed files with 545 additions and 292 deletions

View File

@@ -119,7 +119,8 @@ have to be dropped. See the header of pl-incl.h for details.
bool
initOs(void)
{ DEBUG(1, Sdprintf("OS:initExpand() ...\n"));
{ GET_LD
DEBUG(1, Sdprintf("OS:initExpand() ...\n"));
initExpand();
DEBUG(1, Sdprintf("OS:initEnviron() ...\n"));
initEnviron();
@@ -409,10 +410,10 @@ setOSPrologFlags(void)
* MEMORY *
*******************************/
#if __SWI_PROLOG__
uintptr_t
UsedMemory(void)
{
{ GET_LD
#if defined(HAVE_GETRUSAGE) && defined(HAVE_RU_IDRSS)
struct rusage usage;
@@ -427,23 +428,15 @@ UsedMemory(void)
usedStack(local) +
usedStack(trail));
}
#else
uintptr_t
UsedMemory(void)
{
return 0;
}
#endif
uintptr_t
FreeMemory(void)
{
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_DATA)
uintptr_t used = UsedMemory();
struct rlimit limit;
struct rlimit limit;
if ( getrlimit(RLIMIT_DATA, &limit) == 0 )
return limit.rlim_cur - used;
@@ -511,7 +504,9 @@ setRandom(unsigned int *seedp)
uint64_t
_PL_Random(void)
{ if ( !LD->os.rand_initialised )
{ GET_LD
if ( !LD->os.rand_initialised )
{ setRandom(NULL);
LD->os.rand_initialised = TRUE;
}
@@ -530,9 +525,9 @@ _PL_Random(void)
#else
{ uint64_t l = rand(); /* 0<n<2^15-1 */
l ^= rand()<<15;
l ^= rand()<<30;
l ^= rand()<<45;
l ^= (uint64_t)rand()<<15;
l ^= (uint64_t)rand()<<30;
l ^= (uint64_t)rand()<<45;
return l;
}
@@ -552,7 +547,7 @@ available to the Prolog user based on these functions. These functions
are in this module as non-UNIX OS probably don't have getpid() or put
temporaries on /tmp.
atom_t TemporaryFile(const char *id)
atom_t TemporaryFile(const char *id, int *fdp)
The return value of this call is an atom, whose string represents
the path name of a unique file that can be used as temporary file.
@@ -566,14 +561,6 @@ temporaries on /tmp.
not be created at all, or might already have been deleted.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
struct tempfile
{ atom_t name;
TempFile next;
}; /* chain of temporary files */
#define tmpfile_head (GD->os._tmpfile_head)
#define tmpfile_tail (GD->os._tmpfile_tail)
#ifndef DEFTMPDIR
#ifdef __WINDOWS__
#define DEFTMPDIR "c:/tmp"
@@ -582,22 +569,64 @@ struct tempfile
#endif
#endif
static int
free_tmp_symbol(Symbol s)
{ int rc;
atom_t tname = (atom_t)s->name;
PL_chars_t txt;
get_atom_text(tname, &txt);
PL_mb_text(&txt, REP_FN);
rc = RemoveFile(txt.text.t);
PL_free_text(&txt);
PL_unregister_atom(tname);
return rc;
}
static void
void_free_tmp_symbol(Symbol s)
{ (void)free_tmp_symbol(s);
}
#ifndef O_EXCL
#define O_EXCL 0
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
atom_t
TemporaryFile(const char *id)
TemporaryFile(const char *id, int *fdp)
{ char temp[MAXPATHLEN];
TempFile tf = allocHeap(sizeof(struct tempfile));
char envbuf[MAXPATHLEN];
char *tmpdir;
static char *tmpdir = NULL;
atom_t tname;
int retries = 0;
if ( !((tmpdir = Getenv("TEMP", envbuf, sizeof(envbuf))) ||
(tmpdir = Getenv("TMP", envbuf, sizeof(envbuf)))) )
tmpdir = DEFTMPDIR;
if ( !tmpdir )
{ LOCK();
if ( !tmpdir )
{ char envbuf[MAXPATHLEN];
char *td;
if ( (td = Getenv("TEMP", envbuf, sizeof(envbuf))) ||
(td = Getenv("TMP", envbuf, sizeof(envbuf))) )
tmpdir = strdup(td);
else
tmpdir = DEFTMPDIR;
}
UNLOCK();
}
retry:
#ifdef __unix__
{ static int MTOK_temp_counter = 0;
const char *sep = id[0] ? "_" : "";
Ssprintf(temp, "%s/pl_%s_%d_%d",
tmpdir, id, (int) getpid(), MTOK_temp_counter++);
Ssprintf(temp, "%s/pl_%s%s%d_%d",
tmpdir, id, sep, (int) getpid(), MTOK_temp_counter++);
}
#endif
@@ -612,49 +641,74 @@ TemporaryFile(const char *id)
#endif
{ PrologPath(tmp, temp, sizeof(temp));
} else
Ssprintf(temp, "%s/pl_%s_%d", tmpdir, id, temp_counter++);
}
#endif
{ const char *sep = id[0] ? "_" : "";
#if EMX
static int temp_counter = 0;
char *foo;
if ( (foo = tempnam(".", (const char *)id)) )
{ strcpy(temp, foo);
free(foo);
} else
Ssprintf(temp, "pl_%s_%d_%d", id, getpid(), temp_counter++);
#endif
tf->name = PL_new_atom(temp); /* locked: ok! */
tf->next = NULL;
startCritical;
if ( !tmpfile_tail )
{ tmpfile_head = tmpfile_tail = tf;
} else
{ tmpfile_tail->next = tf;
tmpfile_tail = tf;
Ssprintf(temp, "%s/pl_%s%s%d", tmpdir, id, sep, temp_counter++);
}
endCritical;
return tf->name;
}
#endif
if ( fdp )
{ int fd;
if ( (fd=open(temp, O_CREAT|O_EXCL|O_WRONLY|O_BINARY, 0600)) < 0 )
{ if ( ++retries < 10000 )
goto retry;
else
return NULL_ATOM;
}
*fdp = fd;
}
tname = PL_new_atom(temp); /* locked: ok! */
LOCK();
if ( !GD->os.tmp_files )
{ GD->os.tmp_files = newHTable(4);
GD->os.tmp_files->free_symbol = void_free_tmp_symbol;
}
UNLOCK();
addHTable(GD->os.tmp_files, (void*)tname, (void*)TRUE);
return tname;
}
int
DeleteTemporaryFile(atom_t name)
{ int rc = FALSE;
if ( GD->os.tmp_files )
{ LOCK();
if ( GD->os.tmp_files && GD->os.tmp_files->size > 0 )
{ Symbol s = lookupHTable(GD->os.tmp_files, (void*)name);
if ( s )
{ rc = free_tmp_symbol(s);
deleteSymbolHTable(GD->os.tmp_files, s);
}
}
UNLOCK();
}
return rc;
}
void
RemoveTemporaryFiles(void)
{ TempFile tf, tf2;
{ LOCK();
if ( GD->os.tmp_files )
{ Table t = GD->os.tmp_files;
startCritical;
for(tf = tmpfile_head; tf; tf = tf2)
{ RemoveFile(stringAtom(tf->name));
tf2 = tf->next;
freeHeap(tf, sizeof(struct tempfile));
GD->os.tmp_files = NULL;
UNLOCK();
destroyHTable(t);
} else
{ UNLOCK();
}
tmpfile_head = tmpfile_tail = NULL;
endCritical;
}
@@ -756,7 +810,8 @@ OsPath(const char *p, char *buf)
#if O_XOS
char *
PrologPath(const char *p, char *buf, size_t len)
{ int flags = (truePrologFlag(PLFLAG_FILE_CASE) ? 0 : XOS_DOWNCASE);
{ GET_LD
int flags = (truePrologFlag(PLFLAG_FILE_CASE) ? 0 : XOS_DOWNCASE);
return _xos_canonical_filename(p, buf, len, flags);
}
@@ -813,7 +868,7 @@ forwards char *canoniseDir(char *);
static void
initExpand(void)
{
{ GET_LD
#ifdef O_CANONISE_DIRS
char *dir;
char *cpaths;
@@ -923,6 +978,7 @@ verify_entry(CanonicalDir d)
d->inode = buf.st_ino;
d->device = buf.st_dev;
return TRUE;
} else
{ DEBUG(1, Sdprintf("%s: no longer exists\n", d->canonical));
@@ -939,6 +995,9 @@ verify_entry(CanonicalDir d)
}
}
remove_string(d->name);
if ( d->canonical != d->name )
remove_string(d->canonical);
free(d);
}
@@ -1139,7 +1198,9 @@ utf8_strlwr(char *s)
char *
canonisePath(char *path)
{ if ( !truePrologFlag(PLFLAG_FILE_CASE) )
{ GET_LD
if ( !truePrologFlag(PLFLAG_FILE_CASE) )
utf8_strlwr(path);
canoniseFileName(path);
@@ -1186,7 +1247,8 @@ takeWord(const char **string, char *wrd, int maxlen)
bool
expandVars(const char *pattern, char *expanded, int maxlen)
{ int size = 0;
{ GET_LD
int size = 0;
char wordbuf[MAXPATHLEN];
if ( *pattern == '~' )
@@ -1338,7 +1400,8 @@ ExpandFile(const char *pattern, char **vector)
char *
ExpandOneFile(const char *spec, char *file)
{ char *vector[256];
{ GET_LD
char *vector[256];
int size;
switch( (size=ExpandFile(spec, vector)) )
@@ -1437,10 +1500,13 @@ IsAbsolutePath(const char *p)
char *
AbsoluteFile(const char *spec, char *path)
{ char tmp[MAXPATHLEN];
{ GET_LD
char tmp[MAXPATHLEN];
char buf[MAXPATHLEN];
char *file = PrologPath(spec, buf, sizeof(buf));
if ( !file )
return (char *) NULL;
if ( truePrologFlag(PLFLAG_FILEVARS) )
{ if ( !(file = ExpandOneFile(buf, tmp)) )
return (char *) NULL;
@@ -1485,7 +1551,9 @@ AbsoluteFile(const char *spec, char *path)
void
PL_changed_cwd(void)
{ if ( CWDdir )
{ GET_LD
if ( CWDdir )
remove_string(CWDdir);
CWDdir = NULL;
CWDlen = 0;
@@ -1494,7 +1562,9 @@ PL_changed_cwd(void)
const char *
PL_cwd(void)
{ if ( CWDlen == 0 )
{ GET_LD
if ( CWDlen == 0 )
{ char buf[MAXPATHLEN];
char *rval;
@@ -1583,7 +1653,8 @@ DirName(const char *f, char *dir)
bool
ChDir(const char *path)
{ char ospath[MAXPATHLEN];
{ GET_LD
char ospath[MAXPATHLEN];
char tmp[MAXPATHLEN];
OsPath(path, ospath);
@@ -1681,7 +1752,8 @@ ResetStdin(void)
static ssize_t
Sread_terminal(void *handle, char *buf, size_t size)
{ intptr_t h = (intptr_t)handle;
{ GET_LD
intptr_t h = (intptr_t)handle;
int fd = (int)h;
source_location oldsrc = LD->read_source;
@@ -1708,7 +1780,8 @@ Sread_terminal(void *handle, char *buf, size_t size)
void
ResetTty()
{ startCritical;
{ GET_LD
startCritical;
ResetStdin();
if ( !GD->os.iofunctions.read )
@@ -1736,7 +1809,8 @@ ResetTty()
bool
PushTty(IOSTREAM *s, ttybuf *buf, int mode)
{ struct termios tio;
{ GET_LD
struct termios tio;
int fd;
buf->mode = ttymode;
@@ -1803,7 +1877,8 @@ PushTty(IOSTREAM *s, ttybuf *buf, int mode)
bool
PopTty(IOSTREAM *s, ttybuf *buf)
{ int fd;
{ GET_LD
int fd;
ttymode = buf->mode;
if ( (fd = Sfileno(s)) < 0 || !isatty(fd) )
@@ -1898,7 +1973,8 @@ PushTty(IOSTREAM *s, ttybuf *buf, int mode)
bool
PopTty(IOSTREAM *s, ttybuf *buf)
{ ttymode = buf->mode;
{ GET_LD
ttymode = buf->mode;
if ( ttymode != TTY_RAW )
LD->prompt.next = TRUE;
@@ -2204,7 +2280,8 @@ argument to wait()
int
System(char *cmd)
{ int pid;
{ GET_LD
int pid;
char *shell = "/bin/sh";
int rval;
void (*old_int)();