2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: iopreds.c *
|
|
|
|
* Last rev: 5/2/88 *
|
|
|
|
* mods: *
|
|
|
|
* comments: Input/Output C implemented predicates *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file includes the definition of a miscellania of standard predicates
|
|
|
|
* for yap refering to: Files and Streams, Simple Input/Output,
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "yapio.h"
|
2010-05-28 15:29:20 +01:00
|
|
|
#include "eval.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <stdlib.h>
|
2001-04-27 17:02:43 +01:00
|
|
|
#if HAVE_STDARG_H
|
|
|
|
#include <stdarg.h>
|
|
|
|
#endif
|
2007-12-29 12:26:41 +00:00
|
|
|
#if HAVE_CTYPE_H
|
|
|
|
#include <ctype.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_WCTYPE_H
|
|
|
|
#include <wctype.h>
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.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
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
2001-06-22 18:53:36 +01:00
|
|
|
#if HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2001-05-28 20:54:53 +01:00
|
|
|
#if HAVE_FCNTL_H
|
|
|
|
/* for O_BINARY and O_TEXT in WIN32 */
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2008-06-05 17:24:08 +01:00
|
|
|
#ifdef _WIN32
|
2001-05-28 20:54:53 +01:00
|
|
|
#if HAVE_IO_H
|
|
|
|
/* Windows */
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2008-06-05 17:24:08 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#if !HAVE_STRNCAT
|
|
|
|
#define strncat(X,Y,Z) strcat(X,Y)
|
|
|
|
#endif
|
|
|
|
#if !HAVE_STRNCPY
|
|
|
|
#define strncpy(X,Y,Z) strcpy(X,Y)
|
|
|
|
#endif
|
2002-08-28 15:02:35 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2010-07-20 11:28:45 +01:00
|
|
|
#include <windows.h>
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifndef S_ISDIR
|
|
|
|
#define S_ISDIR(x) (((x)&_S_IFDIR)==_S_IFDIR)
|
|
|
|
#endif
|
|
|
|
#endif
|
2002-06-01 02:46:06 +01:00
|
|
|
#include "iopreds.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static Int p_set_read_error_handler( USES_REGS1 );
|
|
|
|
static Int p_get_read_error_handler( USES_REGS1 );
|
|
|
|
static Int p_read( USES_REGS1 );
|
|
|
|
static Int p_startline( USES_REGS1 );
|
|
|
|
static Int p_change_type_of_char( USES_REGS1 );
|
|
|
|
static Int p_type_of_char( USES_REGS1 );
|
2006-11-27 17:42:03 +00:00
|
|
|
|
2011-02-15 14:43:28 +00:00
|
|
|
extern Atom Yap_FileName(IOSTREAM *s);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-02-15 14:43:28 +00:00
|
|
|
static Term
|
|
|
|
StreamName(IOSTREAM *s)
|
2006-08-02 19:18:31 +01:00
|
|
|
{
|
2011-02-15 14:43:28 +00:00
|
|
|
return MkAtomTerm(Yap_FileName(s));
|
2006-08-02 19:18:31 +01:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-05-28 17:26:00 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitStdStreams (void)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitPlIO (void)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
* Used by the prompts to check if they are after a newline, and then a
|
2001-08-08 22:17:27 +01:00
|
|
|
* prompt should be output, or if we are in the middle of a line.
|
2001-04-09 20:54:03 +01:00
|
|
|
*/
|
2002-11-11 17:38:10 +00:00
|
|
|
static int newline = TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
static int eolflg = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char my_line[200] = {0};
|
|
|
|
static char *lp = my_line;
|
|
|
|
|
|
|
|
static YP_File curfile;
|
|
|
|
|
|
|
|
#ifdef MACC
|
|
|
|
|
|
|
|
static void
|
|
|
|
InTTYLine(char *line)
|
|
|
|
{
|
|
|
|
char *p = line;
|
|
|
|
char ch;
|
|
|
|
while ((ch = InKey()) != '\n' && ch != '\r')
|
|
|
|
if (ch == 8) {
|
|
|
|
if (line < p)
|
|
|
|
BackupTTY(*--p);
|
|
|
|
} else
|
|
|
|
TTYChar(*p++ = ch);
|
|
|
|
TTYChar('\n');
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_DebugSetIFile(char *fname)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
if (curfile)
|
|
|
|
YP_fclose(curfile);
|
|
|
|
curfile = YP_fopen(fname, "r");
|
|
|
|
if (curfile == NULL) {
|
|
|
|
curfile = stdin;
|
2004-06-23 18:24:20 +01:00
|
|
|
fprintf(stderr,"%% YAP Warning: can not open %s for input\n", fname);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_DebugEndline()
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
*lp = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_DebugGetc()
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
if (eolflg) {
|
|
|
|
if (curfile != NULL) {
|
|
|
|
if (YP_fgets(my_line, 200, curfile) == 0)
|
|
|
|
curfile = NULL;
|
|
|
|
}
|
|
|
|
if (curfile == NULL)
|
2010-01-14 15:43:18 +00:00
|
|
|
if (YP_fgets(my_line, 200, stdin) == NULL) {
|
|
|
|
return EOF;
|
|
|
|
}
|
2002-11-11 17:38:10 +00:00
|
|
|
eolflg = 0;
|
|
|
|
lp = my_line;
|
|
|
|
}
|
|
|
|
if ((ch = *lp++) == 0)
|
|
|
|
ch = '\n', eolflg = 1;
|
2011-05-25 16:40:36 +01:00
|
|
|
if (GLOBAL_Option['l' - 96])
|
|
|
|
putc(ch, GLOBAL_logfile);
|
2002-11-11 17:38:10 +00:00
|
|
|
return (ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-01-28 14:26:37 +00:00
|
|
|
Yap_DebugPutc(int sno, wchar_t ch)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2011-05-25 16:40:36 +01:00
|
|
|
if (GLOBAL_Option['l' - 96])
|
|
|
|
(void) putc(ch, GLOBAL_logfile);
|
|
|
|
return (putc(ch, GLOBAL_stderr));
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
2005-12-05 17:16:12 +00:00
|
|
|
|
2008-02-01 22:40:05 +00:00
|
|
|
void
|
|
|
|
Yap_DebugPlWrite(Term t)
|
|
|
|
{
|
2012-02-14 07:46:37 +00:00
|
|
|
Yap_plwrite(t, NULL, 15, 0, 1200);
|
2008-02-01 22:40:05 +00:00
|
|
|
}
|
|
|
|
|
2005-12-05 17:16:12 +00:00
|
|
|
void
|
|
|
|
Yap_DebugErrorPutc(int c)
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2011-05-04 10:11:41 +01:00
|
|
|
Yap_DebugPutc (LOCAL_c_error_stream, c);
|
2005-12-05 17:16:12 +00:00
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
#endif
|
|
|
|
|
2010-06-23 11:46:16 +01:00
|
|
|
|
2010-07-21 10:58:24 +01:00
|
|
|
|
2001-05-21 21:00:05 +01:00
|
|
|
|
2002-11-12 10:29:31 +00:00
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_GetCharForSIGINT(void)
|
2002-11-12 10:29:31 +00:00
|
|
|
{
|
2007-01-28 14:26:37 +00:00
|
|
|
int ch;
|
2002-11-12 10:29:31 +00:00
|
|
|
/* ask for a new line */
|
2013-01-19 09:59:31 +00:00
|
|
|
Sfprintf(Serror, "\nAction (h for help): ");
|
|
|
|
ch = Sgetchar();
|
2002-11-12 10:29:31 +00:00
|
|
|
/* first process up to end of line */
|
2013-01-19 09:59:31 +00:00
|
|
|
while ((Sfgetc(Sinput)) != '\n');
|
2002-11-12 10:29:31 +00:00
|
|
|
newline = TRUE;
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
2011-02-12 14:14:12 +00:00
|
|
|
typedef struct stream_ref
|
|
|
|
{ struct io_stream *read;
|
|
|
|
struct io_stream *write;
|
|
|
|
} stream_ref;
|
|
|
|
|
2005-09-08 23:06:45 +01:00
|
|
|
#ifdef BEAM
|
|
|
|
int beam_write (void)
|
|
|
|
{
|
|
|
|
Yap_StartSlots();
|
2012-02-14 07:46:37 +00:00
|
|
|
Yap_plwrite (ARG1, NULL, 0, 0, 1200);
|
2010-06-01 00:40:58 +01:00
|
|
|
Yap_CloseSlots();
|
2005-09-08 23:06:45 +01:00
|
|
|
if (EX != 0L) {
|
2010-07-27 23:21:15 +01:00
|
|
|
Term ball = Yap_PopTermFromDB(EX);
|
2005-09-08 23:06:45 +01:00
|
|
|
EX = 0L;
|
|
|
|
Yap_JumpToEnv(ball);
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-04-16 20:48:45 +01:00
|
|
|
static void
|
|
|
|
clean_vars(VarEntry *p)
|
|
|
|
{
|
|
|
|
if (p == NULL) return;
|
|
|
|
p->VarAdr = TermNil;
|
|
|
|
clean_vars(p->VarLeft);
|
|
|
|
clean_vars(p->VarRight);
|
|
|
|
}
|
|
|
|
|
2002-04-11 16:31:58 +01:00
|
|
|
static Term
|
2011-02-15 07:39:27 +00:00
|
|
|
syntax_error (TokEntry * tokptr, IOSTREAM *st, Term *outp)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
Term info;
|
2002-04-11 16:31:58 +01:00
|
|
|
int count = 0, out = 0;
|
|
|
|
Int start, err = 0, end;
|
2008-11-26 09:56:18 +00:00
|
|
|
Term tf[7];
|
2002-04-11 16:31:58 +01:00
|
|
|
Term *error = tf+3;
|
2002-04-19 15:42:58 +01:00
|
|
|
CELL *Hi = H;
|
2002-04-11 16:31:58 +01:00
|
|
|
|
2010-03-02 22:21:48 +00:00
|
|
|
/* make sure to globalise variable */
|
2002-04-11 16:31:58 +01:00
|
|
|
start = tokptr->TokPos;
|
2011-05-23 16:19:47 +01:00
|
|
|
clean_vars(LOCAL_VarTable);
|
|
|
|
clean_vars(LOCAL_AnonVarTable);
|
2002-04-11 16:31:58 +01:00
|
|
|
while (1) {
|
|
|
|
Term ts[2];
|
2002-04-19 15:42:58 +01:00
|
|
|
if (H > ASP-1024) {
|
|
|
|
tf[3] = TermNil;
|
|
|
|
err = 0;
|
|
|
|
end = 0;
|
2004-09-10 21:18:01 +01:00
|
|
|
/* for some reason moving this earlier confuses gcc on solaris */
|
|
|
|
H = Hi;
|
2002-04-19 15:42:58 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if (tokptr == LOCAL_toktide) {
|
2002-04-11 16:31:58 +01:00
|
|
|
err = tokptr->TokPos;
|
|
|
|
out = count;
|
|
|
|
}
|
|
|
|
info = tokptr->TokInfo;
|
|
|
|
switch (tokptr->Tok) {
|
|
|
|
case Name_tok:
|
|
|
|
{
|
2004-09-10 21:18:01 +01:00
|
|
|
Term t0[1];
|
|
|
|
t0[0] = MkAtomTerm((Atom)info);
|
2008-12-23 01:53:52 +00:00
|
|
|
ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomAtom,1),1,t0);
|
2002-04-11 16:31:58 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Number_tok:
|
2008-12-23 01:53:52 +00:00
|
|
|
ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomNumber,1),1,&(tokptr->TokInfo));
|
2002-04-11 16:31:58 +01:00
|
|
|
break;
|
|
|
|
case Var_tok:
|
|
|
|
{
|
|
|
|
Term t[3];
|
|
|
|
VarEntry *varinfo = (VarEntry *)info;
|
|
|
|
|
|
|
|
t[0] = MkIntTerm(0);
|
2002-11-18 18:18:05 +00:00
|
|
|
t[1] = Yap_StringToList(varinfo->VarRep);
|
2002-04-11 16:31:58 +01:00
|
|
|
if (varinfo->VarAdr == TermNil) {
|
2002-04-16 20:48:45 +01:00
|
|
|
t[2] = varinfo->VarAdr = MkVarTerm();
|
|
|
|
} else {
|
|
|
|
t[2] = varinfo->VarAdr;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-23 01:53:52 +00:00
|
|
|
ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomGVar,3),3,t);
|
2002-04-11 16:31:58 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case String_tok:
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
Term t0 = Yap_StringToList((char *)info);
|
2008-12-23 01:53:52 +00:00
|
|
|
ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomString,1),1,&t0);
|
2002-04-11 16:31:58 +01:00
|
|
|
}
|
|
|
|
break;
|
2006-11-27 17:42:03 +00:00
|
|
|
case WString_tok:
|
|
|
|
{
|
2008-07-24 17:02:04 +01:00
|
|
|
Term t0 = Yap_WideStringToList((wchar_t *)info);
|
2008-12-23 01:53:52 +00:00
|
|
|
ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomString,1),1,&t0);
|
2006-11-27 17:42:03 +00:00
|
|
|
}
|
|
|
|
break;
|
2002-11-19 17:10:45 +00:00
|
|
|
case Error_tok:
|
2005-12-17 03:25:39 +00:00
|
|
|
case eot_tok:
|
2002-11-19 17:10:45 +00:00
|
|
|
break;
|
2002-04-11 16:31:58 +01:00
|
|
|
case Ponctuation_tok:
|
|
|
|
{
|
|
|
|
char s[2];
|
|
|
|
s[1] = '\0';
|
2013-11-04 01:14:48 +00:00
|
|
|
s[0] = (char)info;
|
2002-11-18 18:18:05 +00:00
|
|
|
ts[0] = MkAtomTerm(Yap_LookupAtom(s));
|
2002-04-11 16:31:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tokptr->Tok == Ord (eot_tok)) {
|
|
|
|
*error = TermNil;
|
|
|
|
end = tokptr->TokPos;
|
|
|
|
break;
|
2002-11-19 17:10:45 +00:00
|
|
|
} else if (tokptr->Tok != Ord (Error_tok)) {
|
|
|
|
ts[1] = MkIntegerTerm(tokptr->TokPos);
|
|
|
|
*error =
|
2008-12-24 09:04:44 +00:00
|
|
|
MkPairTerm(Yap_MkApplTerm(FunctorMinus,2,ts),TermNil);
|
2002-11-19 17:10:45 +00:00
|
|
|
error = RepPair(*error)+1;
|
|
|
|
count++;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-04-11 16:31:58 +01:00
|
|
|
tokptr = tokptr->TokNext;
|
|
|
|
}
|
2012-10-16 17:24:39 +01:00
|
|
|
/* now we can throw away tokens, so we can unify and possibly overwrite TR */
|
|
|
|
Yap_unify(*outp, MkVarTerm());
|
2010-03-02 23:18:04 +00:00
|
|
|
if (IsVarTerm(*outp) && (VarOfTerm(*outp) > H || VarOfTerm(*outp) < H0)) {
|
|
|
|
tf[0] = Yap_MkNewApplTerm(Yap_MkFunctor(AtomRead,1),1);
|
|
|
|
} else {
|
|
|
|
tf[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomRead,1),1,outp);
|
|
|
|
}
|
2002-04-11 16:31:58 +01:00
|
|
|
{
|
|
|
|
Term t[3];
|
2008-06-12 11:55:52 +01:00
|
|
|
|
2002-04-11 16:31:58 +01:00
|
|
|
t[0] = MkIntegerTerm(start);
|
|
|
|
t[1] = MkIntegerTerm(err);
|
|
|
|
t[2] = MkIntegerTerm(end);
|
2008-12-23 01:53:52 +00:00
|
|
|
tf[1] = Yap_MkApplTerm(Yap_MkFunctor(AtomBetween,3),3,t);
|
2002-04-11 16:31:58 +01:00
|
|
|
}
|
2008-12-23 01:53:52 +00:00
|
|
|
tf[2] = MkAtomTerm(AtomHERE);
|
2002-04-11 16:31:58 +01:00
|
|
|
tf[4] = MkIntegerTerm(out);
|
2002-06-17 16:28:01 +01:00
|
|
|
tf[5] = MkIntegerTerm(err);
|
2011-02-15 07:39:27 +00:00
|
|
|
tf[6] = StreamName(st);
|
2008-12-24 09:04:44 +00:00
|
|
|
return(Yap_MkApplTerm(FunctorSyntaxError,7,tf));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2011-02-27 02:34:44 +00:00
|
|
|
static void
|
2011-03-07 16:02:55 +00:00
|
|
|
GenerateSyntaxError(Term *tp, TokEntry *tokstart, IOSTREAM *sno USES_REGS)
|
2011-02-27 02:34:44 +00:00
|
|
|
{
|
|
|
|
if (tp) {
|
|
|
|
Term et[2];
|
|
|
|
Term t = MkVarTerm();
|
2012-05-03 00:16:11 +01:00
|
|
|
et[1] = MkPairTerm(syntax_error(tokstart, sno, &t), TermNil);
|
|
|
|
t = MkAtomTerm(AtomSyntaxError);
|
|
|
|
et[0] = Yap_MkApplTerm(FunctorShortSyntaxError,1,&t);
|
2011-02-27 02:34:44 +00:00
|
|
|
*tp = Yap_MkApplTerm(FunctorError, 2, et);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-15 13:54:19 +00:00
|
|
|
Term
|
|
|
|
Yap_StringToTerm(char *s,Term *tp)
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2011-02-15 13:54:19 +00:00
|
|
|
IOSTREAM *sno = Sopenmem(&s, NULL, "r");
|
|
|
|
Term t;
|
|
|
|
TokEntry *tokstart;
|
|
|
|
tr_fr_ptr TR_before_parse;
|
|
|
|
Term tpos = TermNil;
|
|
|
|
|
|
|
|
if (sno == NULL)
|
|
|
|
return FALSE;
|
|
|
|
TR_before_parse = TR;
|
2011-06-12 17:23:10 +01:00
|
|
|
tokstart = LOCAL_tokptr = LOCAL_toktide = Yap_tokenizer(sno, FALSE, &tpos);
|
2011-02-15 13:54:19 +00:00
|
|
|
if (tokstart == NIL || tokstart->Tok == Ord (eot_tok)) {
|
|
|
|
if (tp) {
|
|
|
|
*tp = MkAtomTerm(AtomEOFBeforeEOT);
|
|
|
|
}
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-02-15 13:54:19 +00:00
|
|
|
Sclose(sno);
|
|
|
|
return FALSE;
|
2011-05-23 16:19:47 +01:00
|
|
|
} else if (LOCAL_ErrorMessage) {
|
2011-02-15 13:54:19 +00:00
|
|
|
if (tp) {
|
2011-05-23 16:19:47 +01:00
|
|
|
*tp = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
|
2011-02-15 13:54:19 +00:00
|
|
|
}
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-02-15 13:54:19 +00:00
|
|
|
Sclose(sno);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
t = Yap_Parse();
|
|
|
|
TR = TR_before_parse;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (!t || LOCAL_ErrorMessage) {
|
2011-03-07 16:02:55 +00:00
|
|
|
GenerateSyntaxError(tp, tokstart, sno PASS_REGS);
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-02-15 13:54:19 +00:00
|
|
|
Sclose(sno);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-02-15 13:54:19 +00:00
|
|
|
Sclose(sno);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-17 16:28:01 +01:00
|
|
|
Int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FirstLineInParse (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2011-05-04 10:11:41 +01:00
|
|
|
return LOCAL_StartLine;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_startline ( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-05-04 10:11:41 +01:00
|
|
|
return (Yap_unify_constant (ARG1, MkIntegerTerm (LOCAL_StartLine)));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* control the parser error handler */
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_set_read_error_handler( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
char *s;
|
|
|
|
if (IsVarTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,t,"set_read_error_handler");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_ATOM,t,"bad syntax_error handler");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
s = RepAtom(AtomOfTerm(t))->StrOfAE;
|
|
|
|
if (!strcmp(s, "fail")) {
|
2002-06-05 02:34:06 +01:00
|
|
|
ParserErrorStyle = FAIL_ON_PARSER_ERROR;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (!strcmp(s, "error")) {
|
2002-06-05 02:34:06 +01:00
|
|
|
ParserErrorStyle = EXCEPTION_ON_PARSER_ERROR;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (!strcmp(s, "quiet")) {
|
2002-06-05 02:34:06 +01:00
|
|
|
ParserErrorStyle = QUIET_ON_PARSER_ERROR;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (!strcmp(s, "dec10")) {
|
2002-06-05 02:34:06 +01:00
|
|
|
ParserErrorStyle = CONTINUE_ON_PARSER_ERROR;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_SYNTAX_ERROR_HANDLER,t,"bad syntax_error handler");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the status for the parser error handler */
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_get_read_error_handler( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t;
|
|
|
|
|
2002-06-05 02:34:06 +01:00
|
|
|
switch (ParserErrorStyle) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case FAIL_ON_PARSER_ERROR:
|
2008-12-23 01:53:52 +00:00
|
|
|
t = MkAtomTerm(AtomFail);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case EXCEPTION_ON_PARSER_ERROR:
|
2008-12-23 01:53:52 +00:00
|
|
|
t = MkAtomTerm(AtomError);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case QUIET_ON_PARSER_ERROR:
|
2008-12-23 01:53:52 +00:00
|
|
|
t = MkAtomTerm(AtomQuiet);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case CONTINUE_ON_PARSER_ERROR:
|
2008-12-23 01:53:52 +00:00
|
|
|
t = MkAtomTerm(AtomDec10);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,"corrupted syntax_error handler");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
return (Yap_unify_constant (ARG1, t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2011-02-12 18:42:44 +00:00
|
|
|
int
|
2011-02-15 07:39:27 +00:00
|
|
|
Yap_readTerm(void *st0, Term *tp, Term *varnames, Term *terror, Term *tpos)
|
2011-02-12 18:42:44 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2011-02-12 18:42:44 +00:00
|
|
|
TokEntry *tokstart;
|
|
|
|
Term pt;
|
2011-02-15 07:39:27 +00:00
|
|
|
IOSTREAM *st = (IOSTREAM *)st0;
|
2011-02-12 18:42:44 +00:00
|
|
|
|
2011-02-15 07:39:27 +00:00
|
|
|
if (st == NULL) {
|
2011-02-12 18:42:44 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-06-12 17:23:10 +01:00
|
|
|
tokstart = LOCAL_tokptr = LOCAL_toktide = Yap_tokenizer(st, FALSE, tpos);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2011-02-12 18:42:44 +00:00
|
|
|
{
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-02-12 18:42:44 +00:00
|
|
|
if (terror)
|
2011-05-23 16:19:47 +01:00
|
|
|
*terror = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-02-12 18:42:44 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
pt = Yap_Parse();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage || pt == (CELL)0) {
|
2011-03-07 16:02:55 +00:00
|
|
|
GenerateSyntaxError(terror, tokstart, st PASS_REGS);
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-02-27 02:34:44 +00:00
|
|
|
return FALSE;
|
2011-02-12 18:42:44 +00:00
|
|
|
}
|
|
|
|
if (varnames) {
|
2011-05-23 16:19:47 +01:00
|
|
|
*varnames = Yap_VarNames(LOCAL_VarTable, TermNil);
|
2011-02-12 18:42:44 +00:00
|
|
|
if (!*varnames) {
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-02-12 18:42:44 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*tp = pt;
|
2011-02-27 02:34:44 +00:00
|
|
|
if (!pt)
|
|
|
|
return FALSE;
|
2011-02-12 18:42:44 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-10-21 17:09:03 +01:00
|
|
|
/*
|
|
|
|
Assumes
|
|
|
|
Flag: ARG1
|
|
|
|
Term: ARG2
|
|
|
|
Module: ARG3
|
|
|
|
Vars: ARG4
|
|
|
|
Pos: ARG5
|
|
|
|
Err: ARG6
|
2011-06-12 17:23:10 +01:00
|
|
|
Comments: ARG7
|
2005-10-21 17:09:03 +01:00
|
|
|
*/
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
do_read(IOSTREAM *inp_stream, int nargs USES_REGS)
|
2003-01-08 16:45:35 +00:00
|
|
|
{
|
2001-04-09 20:54:03 +01:00
|
|
|
Term t, v;
|
2003-01-08 16:45:35 +00:00
|
|
|
TokEntry *tokstart;
|
2008-02-12 17:03:59 +00:00
|
|
|
Term tmod = Deref(ARG3), OCurrentModule = CurrentModule, tpos;
|
2011-04-22 15:29:41 +01:00
|
|
|
extern void Yap_setCurrentSourceLocation(IOSTREAM **s);
|
2011-06-12 17:23:10 +01:00
|
|
|
Term tcomms = Deref(ARG7);
|
|
|
|
int store_comments = IsVarTerm(tcomms);
|
2005-10-21 17:09:03 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(tmod)) {
|
|
|
|
tmod = CurrentModule;
|
|
|
|
} else if (!IsAtomTerm(tmod)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM, tmod, "read_term/2");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2001-04-09 20:54:03 +01:00
|
|
|
while (TRUE) {
|
2002-10-28 20:00:59 +00:00
|
|
|
CELL *old_H;
|
2011-02-15 07:39:27 +00:00
|
|
|
int64_t cpos = 0;
|
|
|
|
int seekable = inp_stream->functions->seek != NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-12-17 03:25:39 +00:00
|
|
|
/* two cases where we can seek: memory and console */
|
|
|
|
if (seekable) {
|
2011-02-15 07:39:27 +00:00
|
|
|
cpos = inp_stream->posbuf.byteno;
|
2005-12-17 03:25:39 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Scans the term using stack space */
|
2005-12-17 03:25:39 +00:00
|
|
|
while (TRUE) {
|
|
|
|
old_H = H;
|
2011-04-22 15:29:41 +01:00
|
|
|
tpos = Yap_StreamPosition(inp_stream);
|
2011-06-12 17:23:10 +01:00
|
|
|
LOCAL_Comments = TermNil;
|
|
|
|
LOCAL_CommentsNextChar = LOCAL_CommentsTail = NULL;
|
|
|
|
tokstart = LOCAL_tokptr = LOCAL_toktide = Yap_tokenizer(inp_stream, store_comments, &tpos);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_Error_TYPE != YAP_NO_ERROR && seekable) {
|
2005-12-17 03:25:39 +00:00
|
|
|
H = old_H;
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2010-04-14 10:57:00 +01:00
|
|
|
if (seekable) {
|
2011-02-15 07:39:27 +00:00
|
|
|
Sseek64(inp_stream, cpos, SIO_SEEK_SET);
|
2005-12-17 03:25:39 +00:00
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_Error_TYPE == OUT_OF_TRAIL_ERROR) {
|
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2010-05-11 12:25:49 +01:00
|
|
|
if (!Yap_growtrail (sizeof(CELL) * K16, FALSE)) {
|
2005-12-17 03:25:39 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
} else if (LOCAL_Error_TYPE == OUT_OF_AUXSPACE_ERROR) {
|
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2009-05-23 00:35:24 +01:00
|
|
|
if (!Yap_ExpandPreAllocCodeSpace(0, NULL, TRUE)) {
|
2005-12-17 03:25:39 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
} else if (LOCAL_Error_TYPE == OUT_OF_HEAP_ERROR) {
|
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2006-02-01 13:58:30 +00:00
|
|
|
if (!Yap_growheap(FALSE, 0, NULL)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
} else if (LOCAL_Error_TYPE == OUT_OF_STACK_ERROR) {
|
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
|
|
|
if (!Yap_gcl(LOCAL_Error_Size, nargs, ENV, CP)) {
|
2005-12-17 03:25:39 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* done with this */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-06-14 08:57:23 +01:00
|
|
|
if (!Yap_unify(tpos,ARG5)) {
|
|
|
|
/* do this early so that we do not have to protect it in case of stack expansion */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2002-10-28 20:00:59 +00:00
|
|
|
/* preserve value of H after scanning: otherwise we may lose strings
|
|
|
|
and floats */
|
|
|
|
old_H = H;
|
2011-02-15 07:39:27 +00:00
|
|
|
if (tokstart != NULL && tokstart->Tok == Ord (eot_tok)) {
|
|
|
|
/* did we get the end of file from an abort? */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage &&
|
|
|
|
!strcmp(LOCAL_ErrorMessage,"Abort")) {
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-02-15 07:39:27 +00:00
|
|
|
return FALSE;
|
2003-10-06 15:16:23 +01:00
|
|
|
} else {
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2002-06-17 16:28:01 +01:00
|
|
|
|
2011-06-21 15:09:30 +01:00
|
|
|
if (store_comments && !Yap_unify(LOCAL_Comments, ARG7))
|
|
|
|
return FALSE;
|
2011-02-15 07:39:27 +00:00
|
|
|
return Yap_unify_constant(ARG2, MkAtomTerm (AtomEof))
|
|
|
|
&& Yap_unify_constant(ARG4, TermNil);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
repeat_cycle:
|
2005-10-21 17:09:03 +01:00
|
|
|
CurrentModule = tmod;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage || (t = Yap_Parse()) == 0) {
|
2005-10-21 17:09:03 +01:00
|
|
|
CurrentModule = OCurrentModule;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage) {
|
2005-01-03 17:06:05 +00:00
|
|
|
int res;
|
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
if (!strcmp(LOCAL_ErrorMessage,"Stack Overflow") ||
|
|
|
|
!strcmp(LOCAL_ErrorMessage,"Trail Overflow") ||
|
|
|
|
!strcmp(LOCAL_ErrorMessage,"Heap Overflow")) {
|
2005-01-03 17:06:05 +00:00
|
|
|
/* ignore term we just built */
|
|
|
|
tr_fr_ptr old_TR = TR;
|
|
|
|
|
2007-04-18 07:30:41 +01:00
|
|
|
|
2005-01-03 17:06:05 +00:00
|
|
|
H = old_H;
|
2011-05-04 10:11:41 +01:00
|
|
|
TR = (tr_fr_ptr)LOCAL_ScannerStack;
|
2005-01-03 17:06:05 +00:00
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
if (!strcmp(LOCAL_ErrorMessage,"Stack Overflow"))
|
|
|
|
res = Yap_growstack_in_parser(&old_TR, &tokstart, &LOCAL_VarTable);
|
|
|
|
else if (!strcmp(LOCAL_ErrorMessage,"Heap Overflow"))
|
|
|
|
res = Yap_growheap_in_parser(&old_TR, &tokstart, &LOCAL_VarTable);
|
2005-01-03 17:06:05 +00:00
|
|
|
else
|
2011-05-23 16:19:47 +01:00
|
|
|
res = Yap_growtrail_in_parser(&old_TR, &tokstart, &LOCAL_VarTable);
|
2005-01-03 17:06:05 +00:00
|
|
|
if (res) {
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ScannerStack = (char *)TR;
|
2005-01-03 17:06:05 +00:00
|
|
|
TR = old_TR;
|
|
|
|
old_H = H;
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_tokptr = LOCAL_toktide = tokstart;
|
|
|
|
LOCAL_ErrorMessage = NULL;
|
2005-01-03 17:06:05 +00:00
|
|
|
goto repeat_cycle;
|
|
|
|
}
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ScannerStack = (char *)TR;
|
2004-10-28 21:12:23 +01:00
|
|
|
TR = old_TR;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2002-06-05 02:34:06 +01:00
|
|
|
if (ParserErrorStyle == QUIET_ON_PARSER_ERROR) {
|
2002-06-05 02:22:57 +01:00
|
|
|
/* just fail */
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2004-02-05 16:57:02 +00:00
|
|
|
return FALSE;
|
2002-06-05 02:34:06 +01:00
|
|
|
} else if (ParserErrorStyle == CONTINUE_ON_PARSER_ERROR) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = NULL;
|
2002-06-05 02:22:57 +01:00
|
|
|
/* try again */
|
|
|
|
goto repeat_cycle;
|
2002-04-19 15:42:58 +01:00
|
|
|
} else {
|
2009-10-26 22:56:18 +00:00
|
|
|
Term terr = syntax_error(tokstart, inp_stream, &ARG2);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage == NULL)
|
|
|
|
LOCAL_ErrorMessage = "SYNTAX ERROR";
|
2002-06-05 02:22:57 +01:00
|
|
|
|
2002-06-05 02:34:06 +01:00
|
|
|
if (ParserErrorStyle == EXCEPTION_ON_PARSER_ERROR) {
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(SYNTAX_ERROR,terr,LOCAL_ErrorMessage);
|
2004-02-05 16:57:02 +00:00
|
|
|
return FALSE;
|
2002-06-05 02:22:57 +01:00
|
|
|
} else /* FAIL ON PARSER ERROR */ {
|
2008-02-12 22:53:02 +00:00
|
|
|
Term t[2];
|
2002-06-05 02:22:57 +01:00
|
|
|
t[0] = terr;
|
2011-05-23 16:19:47 +01:00
|
|
|
t[1] = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2010-08-02 13:03:48 +01:00
|
|
|
return Yap_unify(ARG6,Yap_MkApplTerm(Yap_MkFunctor(AtomError,2),2,t));
|
2002-06-05 02:22:57 +01:00
|
|
|
}
|
2002-04-11 16:31:58 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2005-10-21 17:09:03 +01:00
|
|
|
CurrentModule = OCurrentModule;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* parsing succeeded */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 13:03:48 +01:00
|
|
|
if (!Yap_unify(t, ARG2))
|
2010-08-02 11:56:14 +01:00
|
|
|
return FALSE;
|
2011-06-12 17:23:10 +01:00
|
|
|
if (store_comments && !Yap_unify(LOCAL_Comments, ARG7))
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (AtomOfTerm (Deref (ARG1)) == AtomTrue) {
|
|
|
|
while (TRUE) {
|
|
|
|
CELL *old_H = H;
|
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
if (setjmp(LOCAL_IOBotch) == 0) {
|
|
|
|
v = Yap_VarNames(LOCAL_VarTable, TermNil);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
tr_fr_ptr old_TR;
|
|
|
|
restore_machine_regs();
|
|
|
|
|
|
|
|
old_TR = TR;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* restart global */
|
|
|
|
H = old_H;
|
2011-05-04 10:11:41 +01:00
|
|
|
TR = (tr_fr_ptr)LOCAL_ScannerStack;
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_growstack_in_parser(&old_TR, &tokstart, &LOCAL_VarTable);
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ScannerStack = (char *)TR;
|
2004-10-28 21:12:23 +01:00
|
|
|
TR = old_TR;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2010-08-02 11:56:14 +01:00
|
|
|
return Yap_unify (v, ARG4);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2011-06-12 17:23:10 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable, LOCAL_Comments);
|
2010-08-02 11:56:14 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_read ( USES_REGS1 )
|
2005-10-21 17:09:03 +01:00
|
|
|
{ /* '$read'(+Flag,?Term,?Module,?Vars,-Pos,-Err) */
|
2011-07-10 12:50:55 +01:00
|
|
|
IOSTREAM *Yap_Scurin(void);
|
|
|
|
|
|
|
|
return do_read(Yap_Scurin(), 7 PASS_REGS);
|
2003-01-08 16:45:35 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_read2 ( USES_REGS1 )
|
2005-10-21 17:09:03 +01:00
|
|
|
{ /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */
|
2011-02-15 07:39:27 +00:00
|
|
|
IOSTREAM *inp_stream;
|
2006-04-28 16:48:33 +01:00
|
|
|
Int out;
|
2012-02-18 11:01:06 +00:00
|
|
|
Term t8 = Deref(ARG8);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2012-02-18 11:01:06 +00:00
|
|
|
if (IsVarTerm(t8)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR,t8,"read_term/3");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t8)) {
|
|
|
|
Yap_Error(TYPE_ERROR_LIST,t8,"read_term/3");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!(inp_stream = Yap_GetInputStream(AtomOfTerm(t8))) ) {
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2011-06-12 17:23:10 +01:00
|
|
|
out = do_read(inp_stream, 8 PASS_REGS);
|
2006-04-28 16:48:33 +01:00
|
|
|
return out;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2011-02-14 20:14:31 +00:00
|
|
|
|
2011-02-15 14:43:28 +00:00
|
|
|
#if HAVE_SELECT && FALSE
|
2001-04-09 20:54:03 +01:00
|
|
|
/* stream_select(+Streams,+TimeOut,-Result) */
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_stream_select( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t1 = Deref(ARG1), t2;
|
|
|
|
fd_set readfds, writefds, exceptfds;
|
|
|
|
struct timeval timeout, *ptime;
|
|
|
|
|
|
|
|
#if _MSC_VER
|
|
|
|
u_int fdmax=0;
|
|
|
|
#else
|
|
|
|
int fdmax=0;
|
|
|
|
#endif
|
|
|
|
Term tout = TermNil, ti, Head;
|
|
|
|
|
|
|
|
if (IsVarTerm(t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,t1,"stream_select/3");
|
2008-07-23 00:34:50 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (!IsPairTerm(t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_LIST,t1,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_ZERO(&exceptfds);
|
|
|
|
ti = t1;
|
|
|
|
while (ti != TermNil) {
|
|
|
|
#if _MSC_VER
|
|
|
|
u_int fd;
|
|
|
|
#else
|
|
|
|
int fd;
|
|
|
|
#endif
|
|
|
|
int sno;
|
|
|
|
|
|
|
|
Head = HeadOfTerm(ti);
|
|
|
|
sno = CheckStream(Head, Input_Stream_f, "stream_select/3");
|
|
|
|
if (sno < 0)
|
|
|
|
return(FALSE);
|
|
|
|
fd = GetStreamFd(sno);
|
|
|
|
FD_SET(fd, &readfds);
|
2006-04-28 16:48:33 +01:00
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (fd > fdmax)
|
|
|
|
fdmax = fd;
|
|
|
|
ti = TailOfTerm(ti);
|
|
|
|
}
|
|
|
|
t2 = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t2)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,t2,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (IsAtomTerm(t2)) {
|
2008-12-23 01:53:52 +00:00
|
|
|
if (t2 == MkAtomTerm(AtomOff)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* wait indefinitely */
|
|
|
|
ptime = NULL;
|
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC,t1,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Term t21, t22;
|
|
|
|
|
|
|
|
if (!IsApplTerm(t2) || FunctorOfTerm(t2) != FunctorModule) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC,t2,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
t21 = ArgOfTerm(1, t2);
|
|
|
|
if (IsVarTerm(t21)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,t2,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsIntegerTerm(t21)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC,t2,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
timeout.tv_sec = IntegerOfTerm(t21);
|
|
|
|
if (timeout.tv_sec < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC,t2,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
t22 = ArgOfTerm(2, t2);
|
|
|
|
if (IsVarTerm(t22)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,t2,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsIntegerTerm(t22)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC,t2,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
timeout.tv_usec = IntegerOfTerm(t22);
|
|
|
|
if (timeout.tv_usec < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC,t2,"stream_select/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
ptime = &timeout;
|
|
|
|
}
|
|
|
|
/* do the real work */
|
|
|
|
if (select(fdmax+1, &readfds, &writefds, &exceptfds, ptime) < 0) {
|
|
|
|
#if HAVE_STRERROR
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil,
|
2001-04-09 20:54:03 +01:00
|
|
|
"stream_select/3 (select: %s)", strerror(errno));
|
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil,
|
2001-04-09 20:54:03 +01:00
|
|
|
"stream_select/3 (select)");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
while (t1 != TermNil) {
|
|
|
|
int fd;
|
|
|
|
int sno;
|
|
|
|
|
|
|
|
Head = HeadOfTerm(t1);
|
|
|
|
sno = CheckStream(Head, Input_Stream_f, "stream_select/3");
|
|
|
|
fd = GetStreamFd(sno);
|
|
|
|
if (FD_ISSET(fd, &readfds))
|
|
|
|
tout = MkPairTerm(Head,tout);
|
|
|
|
else
|
|
|
|
tout = MkPairTerm(TermNil,tout);
|
2006-04-28 16:48:33 +01:00
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2001-04-09 20:54:03 +01:00
|
|
|
t1 = TailOfTerm(t1);
|
|
|
|
}
|
|
|
|
/* we're done, just pass the info back */
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(ARG3,tout));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_change_type_of_char ( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* change_type_of_char(+char,+type) */
|
|
|
|
Term t1 = Deref (ARG1);
|
|
|
|
Term t2 = Deref (ARG2);
|
2007-12-29 12:26:41 +00:00
|
|
|
if (!IsVarTerm (t1) && !IsIntegerTerm (t1))
|
|
|
|
return FALSE;
|
|
|
|
if (!IsVarTerm(t2) && !IsIntegerTerm(t2))
|
|
|
|
return FALSE;
|
|
|
|
Yap_chtype[IntegerOfTerm(t1)] = IntegerOfTerm(t2);
|
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_type_of_char ( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* type_of_char(+char,-type) */
|
|
|
|
Term t;
|
|
|
|
|
|
|
|
Term t1 = Deref (ARG1);
|
2007-12-29 12:26:41 +00:00
|
|
|
if (!IsVarTerm (t1) && !IsIntegerTerm (t1))
|
|
|
|
return FALSE;
|
|
|
|
t = MkIntTerm(Yap_chtype[IntegerOfTerm (t1)]);
|
|
|
|
return Yap_unify(t,ARG2);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_force_char_conversion( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
/* don't actually enable it until someone tries to add a conversion */
|
|
|
|
if (CharConversionTable2 == NULL)
|
|
|
|
return(TRUE);
|
|
|
|
CharConversionTable = CharConversionTable2;
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_disable_char_conversion( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MaxStreams; i++) {
|
|
|
|
if (!(Stream[i].status & Free_Stream_f))
|
2006-11-27 17:42:03 +00:00
|
|
|
Stream[i].stream_wgetc_for_read = Stream[i].stream_wgetc;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
CharConversionTable = NULL;
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_char_conversion( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t0 = Deref(ARG1), t1 = Deref(ARG2);
|
|
|
|
char *s0, *s1;
|
|
|
|
|
|
|
|
if (IsVarTerm(t0)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t0, "char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t0)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(REPRESENTATION_ERROR_CHARACTER, t0, "char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
s0 = RepAtom(AtomOfTerm(t0))->StrOfAE;
|
|
|
|
if (s0[1] != '\0') {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(REPRESENTATION_ERROR_CHARACTER, t0, "char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (IsVarTerm(t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t1, "char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(REPRESENTATION_ERROR_CHARACTER, t1, "char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
s1 = RepAtom(AtomOfTerm(t1))->StrOfAE;
|
|
|
|
if (s1[1] != '\0') {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(REPRESENTATION_ERROR_CHARACTER, t1, "char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
/* check if we do have a table for converting characters */
|
|
|
|
if (CharConversionTable2 == NULL) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* don't create a table if we don't need to */
|
|
|
|
if (s0[0] == s1[0])
|
|
|
|
return(TRUE);
|
2002-11-18 18:18:05 +00:00
|
|
|
CharConversionTable2 = Yap_AllocCodeSpace(NUMBER_OF_CHARS*sizeof(char));
|
2001-04-09 20:54:03 +01:00
|
|
|
while (CharConversionTable2 == NULL) {
|
2004-01-23 02:23:51 +00:00
|
|
|
if (!Yap_growheap(FALSE, NUMBER_OF_CHARS*sizeof(char), NULL)) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(OUT_OF_HEAP_ERROR, TermNil, LOCAL_ErrorMessage);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (yap_flags[CHAR_CONVERSION_FLAG] != 0) {
|
2011-03-07 16:02:55 +00:00
|
|
|
if (p_force_char_conversion( PASS_REGS1 ) == FALSE)
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
for (i = 0; i < NUMBER_OF_CHARS; i++)
|
2010-02-26 09:12:06 +00:00
|
|
|
CharConversionTable2[i] = i;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* just add the new entry */
|
2010-02-26 09:12:06 +00:00
|
|
|
CharConversionTable2[(int)s0[0]] = s1[0];
|
2001-04-09 20:54:03 +01:00
|
|
|
/* done */
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_current_char_conversion( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t0, t1;
|
|
|
|
char *s0, *s1;
|
|
|
|
|
|
|
|
if (CharConversionTable == NULL) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
t0 = Deref(ARG1);
|
|
|
|
if (IsVarTerm(t0)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t0, "current_char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t0)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(REPRESENTATION_ERROR_CHARACTER, t0, "current_char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
s0 = RepAtom(AtomOfTerm(t0))->StrOfAE;
|
|
|
|
if (s0[1] != '\0') {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(REPRESENTATION_ERROR_CHARACTER, t0, "current_char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
t1 = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t1)) {
|
|
|
|
char out[2];
|
|
|
|
if (CharConversionTable[(int)s0[0]] == '\0') return(FALSE);
|
|
|
|
out[0] = CharConversionTable[(int)s0[0]];
|
|
|
|
out[1] = '\0';
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(ARG2,MkAtomTerm(Yap_LookupAtom(out))));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(REPRESENTATION_ERROR_CHARACTER, t1, "current_char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
s1 = RepAtom(AtomOfTerm(t1))->StrOfAE;
|
|
|
|
if (s1[1] != '\0') {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(REPRESENTATION_ERROR_CHARACTER, t1, "current_char_conversion/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
} else {
|
|
|
|
return (CharConversionTable[(int)s0[0]] == '\0' &&
|
|
|
|
CharConversionTable[(int)s0[0]] == s1[0] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_all_char_conversions( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term out = TermNil;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (CharConversionTable == NULL) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
for (i = NUMBER_OF_CHARS; i > 0; ) {
|
|
|
|
i--;
|
|
|
|
if (CharConversionTable[i] != '\0') {
|
|
|
|
Term t1, t2;
|
|
|
|
char s[2];
|
|
|
|
s[1] = '\0';
|
|
|
|
s[0] = CharConversionTable[i];
|
2002-11-18 18:18:05 +00:00
|
|
|
t1 = MkAtomTerm(Yap_LookupAtom(s));
|
2001-04-09 20:54:03 +01:00
|
|
|
out = MkPairTerm(t1,out);
|
|
|
|
s[0] = i;
|
2002-11-18 18:18:05 +00:00
|
|
|
t2 = MkAtomTerm(Yap_LookupAtom(s));
|
2001-04-09 20:54:03 +01:00
|
|
|
out = MkPairTerm(t2,out);
|
|
|
|
}
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(ARG1,out));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_float_format( USES_REGS1 )
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
|
|
|
Term in = Deref(ARG1);
|
|
|
|
if (IsVarTerm(in))
|
2008-12-23 01:53:52 +00:00
|
|
|
return Yap_unify(ARG1, MkAtomTerm(AtomFloatFormat));
|
|
|
|
AtomFloatFormat = AtomOfTerm(in);
|
2006-01-02 02:16:19 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitBackIO (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:08:01 +01:00
|
|
|
#if DEBUG
|
2012-09-18 23:06:26 +01:00
|
|
|
static Int
|
|
|
|
p_write_string( USES_REGS1 )
|
|
|
|
{
|
|
|
|
Term in = Deref(ARG1);
|
|
|
|
char *s;
|
|
|
|
size_t length;
|
|
|
|
int encoding;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
if ((s = Yap_TermToString( in, NULL, 0, &length, &encoding, 0)))
|
|
|
|
fprintf(stderr,"%ld %s\n",length, s);
|
|
|
|
if ((s = Yap_TermToString( in, buf, 256, &length, &encoding, 0)))
|
|
|
|
fprintf(stderr,"%ld %s\n",length, s);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-26 23:08:01 +01:00
|
|
|
#endif
|
2002-12-06 20:03:26 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitIOPreds(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2004-09-27 21:45:04 +01:00
|
|
|
if (!Stream)
|
|
|
|
Stream = (StreamDesc *)Yap_AllocCodeSpace(sizeof(StreamDesc)*MaxStreams);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* here the Input/Output predicates */
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred ("$set_read_error_handler", 1, p_set_read_error_handler, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$get_read_error_handler", 1, p_get_read_error_handler, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$read", 7, p_read, SyncPredFlag|UserCPredFlag);
|
|
|
|
Yap_InitCPred ("$read", 8, p_read2, SyncPredFlag|UserCPredFlag);
|
2012-09-26 23:08:01 +01:00
|
|
|
#if DEBUG
|
|
|
|
Yap_InitCPred ("write_string", 2, p_write_string, SyncPredFlag|UserCPredFlag);
|
|
|
|
#endif
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred ("$start_line", 1, p_startline, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$change_type_of_char", 2, p_change_type_of_char, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$type_of_char", 2, p_type_of_char, SafePredFlag|SyncPredFlag);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred ("char_conversion", 2, p_char_conversion, SyncPredFlag);
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred ("$current_char_conversion", 2, p_current_char_conversion, SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$all_char_conversions", 1, p_all_char_conversions, SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$force_char_conversion", 0, p_force_char_conversion, SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$disable_char_conversion", 0, p_disable_char_conversion, SyncPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_SELECT
|
2011-02-15 14:43:28 +00:00
|
|
|
// Yap_InitCPred ("stream_select", 3, p_stream_select, SafePredFlag|SyncPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred ("$float_format", 1, p_float_format, SafePredFlag|SyncPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
}
|