Fix encoding

This commit is contained in:
Vítor Santos Costa 2016-02-18 12:10:58 +00:00
parent 05a978ce82
commit 484168b1ce
18 changed files with 1557 additions and 1522 deletions

View File

@ -921,7 +921,7 @@ cont_string_code3( USES_REGS1 )
s0 = UStringOfTerm( t2 );
i = IntOfTerm(EXTRA_CBACK_ARG(3,1)); // offset in coded string, increases by 1..6
j = IntOfTerm(EXTRA_CBACK_ARG(3,2)); // offset in UNICODE string, always increases by 1
s = (s0+i) + get_utf8( (unsigned char *)s0+i, &chr );
s = (s0+i) + get_utf8( (unsigned char *)s0+i, -1, &chr );
if (s[0]) {
EXTRA_CBACK_ARG(3,1) = MkIntTerm(s-s0);
EXTRA_CBACK_ARG(3,2) = MkIntTerm(j+1);
@ -983,7 +983,7 @@ string_code3( USES_REGS1 )
if (ns == NULL) {
cut_fail(); // silently fail?
}
get_utf8( (unsigned char *)ns, &chr);
get_utf8( (unsigned char *)ns, -1, &chr);
if ( chr == '\0') cut_fail();
if (Yap_unify(ARG3, MkIntegerTerm(chr))) cut_succeed();
cut_fail();
@ -1042,7 +1042,7 @@ get_string_code3( USES_REGS1 )
if (ns == NULL) {
return FALSE;
} else {
get_utf8( ns, &chr);
get_utf8( ns, -1, &chr);
if ( chr != '\0') return Yap_unify(ARG3, MkIntegerTerm(chr));
}
}
@ -1611,7 +1611,7 @@ build_new_atomic(int mask, wchar_t *wp, const unsigned char *p, size_t min, size
buf = buf_from_tstring(HR);
while (len) {
utf8proc_int32_t chr;
cp += get_utf8((unsigned char *)cp, &chr);
cp += get_utf8((unsigned char *)cp, -1, &chr);
buf += put_utf8((unsigned char *)buf, chr);
len--;
}
@ -1834,7 +1834,7 @@ cont_sub_atomic( USES_REGS1 )
/* found one, check if there is any left */
while (min <= sz-len) {
int chr;
p += get_utf8((unsigned char *)p, &chr);
p += get_utf8((unsigned char *)p, -1, &chr);
after--;
min++;
if (cmpn_utf8(p, UStringOfTerm(nat), len) == 0)

View File

@ -365,7 +365,7 @@ Yap_OpaqueTermToString(Term t, char *str, size_t max)
str_index += sprintf(& str[str_index], "\"");
do {
utf8proc_int32_t chr;
ptr += get_utf8(ptr, &chr);
ptr += get_utf8(ptr, -1, &chr);
if (chr == '\0') break;
str_index += sprintf(str+str_index, "%C", chr);
} while (TRUE);

View File

@ -195,15 +195,11 @@ static bool mkprompt(Term inp) {
static bool getenc(Term inp) {
CACHE_REGS
if (IsVarTerm(inp)) {
return Yap_unify(inp, MkAtomTerm(Yap_LookupAtom(enc_name(LOCAL_encoding))));
}
if (!IsAtomTerm(inp)) {
Yap_Error(TYPE_ERROR_ATOM, inp, "set_prolog_flag");
if (!IsVarTerm(inp) && !IsAtomTerm(inp)) {
Yap_Error(TYPE_ERROR_ATOM, inp, "get_encoding");
return false;
}
enc_id((char *)RepAtom(AtomOfTerm(inp))->StrOfAE);
return true;
return Yap_unify(inp, MkAtomTerm(Yap_LookupAtom(enc_name(LOCAL_encoding))));
}
/*
@ -217,7 +213,7 @@ if (!IsAtomTerm(inp) ) {
Yap_Error(TYPE_ERROR_ATOM, inp, "set_prolog_flag");
return false;
}
enc_id( RepAtom( AtomOfTerm( inp ) )->StrOfAE );
enc_id( RepAtom( AtomOfTerm( inp ) )->StrOfAE, ENC_OCTET );
return true;
}
*/

View File

@ -1232,7 +1232,12 @@ const char *Yap_tokRep(TokEntry *tokptr) {
switch (tokptr->Tok) {
case Name_tok:
return (char *)RepAtom((Atom)info)->StrOfAE;
if (IsWideAtom((Atom)info)) {
wchar_t *wc = RepAtom((Atom)info)->WStrOfAE;
Term s = Yap_WCharsToString(wc PASS_REGS);
return StringOfTerm(s);
}
return RepAtom((Atom)info)->StrOfAE;
case Number_tok:
if ((b = Yap_TermToString(info, buf, sze, &length, &LOCAL_encoding,
flags)) != buf) {
@ -2038,9 +2043,6 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
char err[1024];
snprintf(err, 1023, "\n++++ token: unrecognised char %c (%d), type %c\n",
ch, ch, chtype(ch));
#if DEBUG
fprintf(stderr, "%s", err);
#endif
}
t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermEof;

View File

@ -408,7 +408,7 @@ write_strings( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng
buf = buf_from_tstring(HR);
while (*cp && cp < lim) {
utf8proc_int32_t chr;
cp += get_utf8(cp, &chr);
cp += get_utf8(cp, -1, &chr);
buf += put_utf8(buf, chr);
}
if (max >= min) *buf++ = '\0';
@ -496,7 +496,7 @@ write_atoms( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng U
while (cp < lim && *cp) {
utf8proc_int32_t chr;
CELL *cl;
cp += get_utf8(cp, &chr);
cp += get_utf8(cp, -1, &chr);
if (chr == '\0') break;
w[0] = chr;
cl = HR;
@ -581,7 +581,7 @@ write_codes( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng U
LOCAL_TERM_ERROR( t, 2*(lim-s) );
while (*cp && cp < lim) {
utf8proc_int32_t chr;
cp += get_utf8(cp, &chr);
cp += get_utf8(cp, -1, &chr);
HR[0] = MkIntTerm(chr);
HR[1] = AbsPair(HR+2);
HR += 2;
@ -659,7 +659,7 @@ write_atom( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng US
while (*s && s < lim) {
utf8proc_int32_t chr;
s += get_utf8(s, &chr);
s += get_utf8(s,-1, &chr);
*ptr++ = chr;
}
*ptr++ = '\0';
@ -743,14 +743,14 @@ write_wbuffer( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng
return -1;
while (*cp && cp < lim) {
utf8proc_int32_t chr;
cp += get_utf8(cp, &chr);
cp += get_utf8(cp, -1, &chr);
*buf++ = chr;
}
if (max >= min) *buf++ = '\0';
else while (max < min) {
utf8proc_int32_t chr;
max++;
cp += get_utf8(cp, &chr);
cp += get_utf8(cp, -1, &chr);
*buf++ = chr;
}
*buf = '\0';
@ -890,14 +890,14 @@ write_buffer( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng
return -1;
while (*cp && cp < lim) {
utf8proc_int32_t chr;
cp += get_utf8(cp, &chr);
cp += get_utf8(cp, -1, &chr);
*buf++ = chr;
}
if (max >= min) *buf++ = '\0';
else while (max < min) {
utf8proc_int32_t chr;
max++;
cp += get_utf8(cp, &chr);
cp += get_utf8(cp, -1, &chr);
*buf++ = chr;
}
sz_end = buf-out->val.uc;
@ -1104,7 +1104,7 @@ cmp_Text( void *s1, void *s2, int l, encoding_t enc1, encoding_t enc2 )
{
utf8proc_int32_t chr1, chr2;
unsigned char *w2 = s2;
for (i = 0; i < l; i++) { chr1 = *w1++; w2 += get_utf8(w2, &chr2); if (chr1-chr2) return chr1-chr2; }
for (i = 0; i < l; i++) { chr1 = *w1++; w2 += get_utf8(w2, -1, &chr2); if (chr1-chr2) return chr1-chr2; }
}
return 0;
case ENC_WCHAR:
@ -1126,21 +1126,21 @@ cmp_Text( void *s1, void *s2, int l, encoding_t enc1, encoding_t enc2 )
{
utf8proc_int32_t chr1, chr2;
unsigned char *w2 = s2;
for (i = 0; i < l; i++) { chr2 = *w2++; w1 += get_utf8(w1, &chr1); if (chr1-chr2) return chr1-chr2; }
for (i = 0; i < l; i++) { chr2 = *w2++; w1 += get_utf8(w1, -1, &chr1); if (chr1-chr2) return chr1-chr2; }
}
return 0;
case ENC_ISO_UTF8:
{
utf8proc_int32_t chr1, chr2;
unsigned char *w2 = s2;
for (i = 0; i < l; i++) { w2 += get_utf8(w2, &chr2); w1 += get_utf8(w1, &chr1); if (chr1-chr2) return chr1-chr2; }
for (i = 0; i < l; i++) { w2 += get_utf8(w2, -1, &chr2); w1 += get_utf8(w1,-1, &chr1); if (chr1-chr2) return chr1-chr2; }
}
return 0;
case ENC_WCHAR:
{
utf8proc_int32_t chr1, chr2;
wchar_t *w2 = s2;
for (i = 0; i < l; i++) { chr2 = *w2++; w1 += get_utf8(w1, &chr1); if (chr1-chr2) return chr1-chr2; }
for (i = 0; i < l; i++) { chr2 = *w2++; w1 += get_utf8(w1, -1, &chr1); if (chr1-chr2) return chr1-chr2; }
}
return 0;
default:
@ -1162,7 +1162,7 @@ cmp_Text( void *s1, void *s2, int l, encoding_t enc1, encoding_t enc2 )
{
utf8proc_int32_t chr1, chr2;
unsigned char *w2 = s2;
for (i = 0; i < l; i++) { chr1 = *w1++; w2 += get_utf8(w2, &chr2); if (chr1-chr2) return chr1-chr2; }
for (i = 0; i < l; i++) { chr1 = *w1++; w2 += get_utf8(w2, -1, &chr2); if (chr1-chr2) return chr1-chr2; }
}
return 0;
case ENC_WCHAR:
@ -1232,7 +1232,7 @@ concat( int n, seq_tv_t *out, void *sv[], encoding_t encv[], size_t lengv[] USES
} else {
unsigned char *ptr = sv[i];
utf8proc_int32_t chr;
while ( (ptr += get_utf8( ptr, &chr )) != NULL ) { if (chr == '\0') break; else *buf++ = chr; }
while ( (ptr += get_utf8( ptr, -1, &chr )) != NULL ) { if (chr == '\0') break; else *buf++ = chr; }
}
}
*buf++ = '\0';
@ -1276,7 +1276,7 @@ slice( size_t min, size_t max, void *buf, seq_tv_t *out, encoding_t enc USES_REG
unsigned char *ptr = skip_utf8 (buf, min );
utf8proc_int32_t chr;
if (!ptr) return NULL;
while ( min++ < max ) { ptr += get_utf8(ptr, & chr); nbuf += put_utf8(nbuf, chr); }
while ( min++ < max ) { ptr += get_utf8(ptr, -1, & chr); nbuf += put_utf8(nbuf, chr); }
}
*nbuf ++ = '\0';
close_tstring( nbuf PASS_REGS );
@ -1313,7 +1313,7 @@ slice( size_t min, size_t max, void *buf, seq_tv_t *out, encoding_t enc USES_REG
utf8proc_int32_t chr;
LOCAL_ERROR( MkAtomTerm(Yap_LookupAtom(buf)), max-min );
while ( min++ < max ) { ptr += get_utf8(ptr, & chr); *nbuf++ = chr; }
while ( min++ < max ) { ptr += get_utf8(ptr, -1, & chr); *nbuf++ = chr; }
nbuf[0] = '\0';
at = Yap_LookupMaybeWideAtom( (wchar_t*)HR );
}

View File

@ -572,7 +572,7 @@ static void write_string(const unsigned char *s,
qt = '"';
wrputc(qt, stream);
do {
ptr += get_utf8(ptr, &chr);
ptr += get_utf8(ptr, -1, &chr);
if (chr == '\0')
break;
write_quoted(chr, qt, stream);

View File

@ -141,9 +141,9 @@ INLINE_ONLY EXTERN inline char_kind_t chtype(Int ch) {
#define __android_log_print(...)
#endif
inline static utf8proc_ssize_t get_utf8(utf8proc_uint8_t *ptr,
inline static utf8proc_ssize_t get_utf8(utf8proc_uint8_t *ptr, size_t n,
utf8proc_int32_t *valp) {
return utf8proc_iterate(ptr, -1, valp);
return utf8proc_iterate(ptr, n, valp);
}
inline static utf8proc_ssize_t put_utf8(utf8proc_uint8_t *ptr,
@ -164,6 +164,11 @@ inline static utf8proc_uint8_t *skip_utf8(utf8proc_uint8_t *pt,
return pt;
}
inline static utf8proc_ssize_t utf8_nof( utf8proc_int32_t val) {
return utf8proc_charwidth(val);
}
inline static utf8proc_ssize_t strlen_utf8(const utf8proc_uint8_t *pt) {
utf8proc_ssize_t rc = 0;
utf8proc_int32_t b;
@ -259,6 +264,12 @@ inline static int cmpn_utf8(const utf8proc_uint8_t *pt1,
return 0;
}
// UTF16
#define LEAD_OFFSET (0xD800 - (0x10000 >> 10))
#define SURROGATE_OFFSET ( 0x10000 - (0xD800 << 10) - 0xDC00 )
const char *Yap_tokRep(TokEntry *tokptr);
// standard strings

View File

@ -94,10 +94,6 @@ INLINE_ONLY inline EXTERN Int CharOfAtom(Atom at) {
}
}
static int plUnGetc(int sno, int ch) {
return ungetc(ch, GLOBAL_Stream[sno].file);
}
Int Yap_peek(int sno) {
CACHE_REGS
Int ocharcount, olinecount, olinepos;
@ -144,33 +140,41 @@ Int Yap_peek(int sno) {
}
} else if (s->encoding == ENC_UTF16_BE) {
/* do the ungetc as if a write .. */
unsigned long int c = ch;
if (c > ((1 << 16) - 1)) {
ungetc(c / 1 << 16, s->file);
c %= 1 << 16;
// computations
int lead = LEAD_OFFSET + (ch >> 10);
int trail = 0xDC00 + (ch & 0x3FF);
if (lead) {
ungetc(lead / 256, s->file);
ungetc(lead % 256, s->file);
}
ungetc(c, s->file);
} else if (s->encoding == ENC_UTF16_BE) {
/* do the ungetc as if a write .. */
unsigned long int c = ch;
if (c > ((1 << 16) - 1)) {
ungetc(c / 1 << 16, s->file);
c %= 1 << 16;
}
return c;
ungetc(trail / 256, s->file);
ungetc(trail % 256, s->file);
} else if (s->encoding == ENC_UTF16_LE) {
/* do the ungetc as if a write .. */
unsigned long int c = ch;
if (c > ((1 << 16) - 1)) {
ungetc(c % 1 << 16, s->file);
c /= 1 << 16;
// computations
uint16_t lead = LEAD_OFFSET + (ch >> 10);
uint16_t trail = 0xDC00 + (ch & 0x3FF);
lead = 0;
trail = ch;
if (lead) {
ungetc(lead / 256, s->file);
ungetc(lead % 256, s->file);
}
ungetc(c, s->file);
} else {
int (*f)(int, int) = s->stream_putc;
s->stream_putc = plUnGetc;
put_wchar(sno, ch);
s->stream_putc = f;
if (trail) {
ungetc(trail / 256, s->file);
ungetc(trail % 256, s->file);
}
} else if (s->encoding == ENC_ISO_UTF32_LE) {
ungetc( (ch >> 24) & 0xff, s->file);
ungetc( (ch >> 16) & 0xff, s->file);
ungetc( (ch >> 8) & 0xff, s->file);
return ungetc( ch & 0xff, s->file);
} else if (s->encoding == ENC_ISO_UTF32_BE) {
ungetc( ch & 0xff, s->file);
ungetc( (ch >> 8) & 0xff, s->file);
ungetc( (ch >> 16) & 0xff, s->file);
return ungetc( (ch >> 24) & 0xff, s->file);
}
return ch;
}
@ -421,7 +425,7 @@ static Int get_byte(USES_REGS1) { /* '$get_byte'(Stream,-N) */
//&& strictISOFlag()
) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_TEXT_STREAM, ARG1, "get_byte/2");
Yap_Error(PERMISSION_ERROR_INPUT_STREAM, ARG1, "get_byte/2");
return (FALSE);
}
out = MkIntTerm(GLOBAL_Stream[sno].stream_getc(sno));
@ -481,7 +485,7 @@ static Int put_code_1(USES_REGS1) { /* '$put'(,N) */
return FALSE;
}
LOCK(GLOBAL_Stream[sno].streamlock);
GLOBAL_Stream[sno].stream_wputc(sno, (int)IntegerOfTerm(Deref(ARG2)));
GLOBAL_Stream[sno].stream_wputc(sno, ch);
/*
* if (!(GLOBAL_Stream[sno].status & Null_Stream_f))
* yap_fflush(GLOBAL_Stream[sno].file);
@ -925,7 +929,6 @@ static Int flush_all_streams(USES_REGS1) { /* $flush_all_streams */
/** @pred peek_code(+ _S_, - _C_) is iso
If _C_ is unbound, or is the code for a character, and
the stream _S_ is a text stream, read the next character from the
current stream and unify its code with _C_, while

View File

@ -31,7 +31,7 @@ typedef enum {
ENC_ISO_UTF32_LE = 128, /// yes, nobody
} encoding_t;
#if defined(__BIG_ENDIAN__)
#if WORDS_BIGENDIAN
#define ENC_WCHAR ENC_ISO_UTF32_BE
#else
#define ENC_WCHAR ENC_ISO_UTF32_LE
@ -114,14 +114,27 @@ static inline const char *enc_name(encoding_t enc) {
}
}
static inline encoding_t enc_id(char *s) {
static inline encoding_t enc_id(const char *s, encoding_t enc_bom) {
{
if (!strcmp(s, "iso_utf8"))
return ENC_ISO_UTF8;
if (!strcmp(s, "utf8"))
return ENC_ISO_UTF8;
if (!strcmp(s, "UTF-8"))
return ENC_ISO_UTF8;
if (!strcmp(s, "utf16_le"))
return ENC_UTF16_LE;
if (!strcmp(s, "utf16_be"))
return ENC_UTF16_BE;
if (!strcmp(s, "UTF-16")) {
if (enc_bom == ENC_UTF16_LE)
return ENC_UTF16_LE;
return ENC_UTF16_BE;
}
if (!strcmp(s, "UTF-16LE"))
return ENC_UTF16_LE;
if (!strcmp(s, "UTF16-BE"))
return ENC_UTF16_BE;
if (!strcmp(s, "octet"))
return ENC_OCTET;
if (!strcmp(s, "iso_latin_1"))
@ -134,11 +147,31 @@ static inline encoding_t enc_id(char *s) {
return ENC_ISO_UTF32_BE;
if (!strcmp(s, "utf32_le"))
return ENC_ISO_UTF32_LE;
if (!strcmp(s, "default"))
if (!strcmp(s, "UTF-32")) {
if (enc_bom == ENC_ISO_UTF32_LE)
return ENC_ISO_UTF32_LE;
return ENC_ISO_UTF32_BE;
}
if (!strcmp(s, "UTF-32BE"))
return ENC_ISO_UTF32_BE;
if (!strcmp(s, "UTF-32LE"))
return ENC_ISO_UTF32_LE;
if (!strcmp(s, "ISO-8859-1"))
return ENC_ISO_LATIN1;
// just for SWI compat, this actually refers to
// UCS-2
if (!strcmp(s, "unicode_be"))
return ENC_UTF16_BE;
if (!strcmp(s, "unicode_le"))
return ENC_UTF16_LE;
if (!strcmp(s, "default")) {
if (enc_bom != ENC_OCTET)
return enc_bom;
return Yap_DefaultEncoding();
}
else {
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, 0, "bad encoding %s", s);
return ENC_OCTET;
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, MkAtomTerm(Yap_LookupAtom(s)), "bad encoding %s", s);
return Yap_DefaultEncoding();
}
}
}

View File

@ -328,7 +328,7 @@ static int format_print_str(Int sno, Int size, Int has_size, Term args,
const unsigned char *pt = UStringOfTerm(args);
while (*pt && (!has_size || size > 0)) {
utf8proc_int32_t ch;
pt += get_utf8((unsigned char *)pt, &ch);
pt += get_utf8((unsigned char *)pt, -1, &ch);
f_putc(sno, ch);
}
} else {

File diff suppressed because it is too large Load Diff

View File

@ -37,10 +37,14 @@ extern size_t Yap_page_size;
#include <wchar.h>
#define Yap_CheckStream( arg, kind, msg) Yap_CheckStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
extern int Yap_CheckStream__(const char *, const char *, int , Term, int, const char *);
#define Yap_CheckTextStream( arg, kind, msg) Yap_CheckTextStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
extern int Yap_CheckTextStream__(const char *, const char *, int , Term, int, const char *);
#define Yap_CheckStream(arg, kind, msg) \
Yap_CheckStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
extern int Yap_CheckStream__(const char *, const char *, int, Term, int,
const char *);
#define Yap_CheckTextStream(arg, kind, msg) \
Yap_CheckTextStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
extern int Yap_CheckTextStream__(const char *, const char *, int, Term, int,
const char *);
extern bool Yap_initStream(int sno, FILE *fd, const char *name, Term file_name,
encoding_t encoding, stream_flags_t flags,
@ -51,7 +55,7 @@ extern int Yap_sockets_io;
/****************** defines for sockets *********************************/
typedef enum{ /* in YAP, sockets may be in one of 4 possible status */
typedef enum { /* in YAP, sockets may be in one of 4 possible status */
new_socket,
server_socket,
client_socket,
@ -59,7 +63,7 @@ typedef enum{ /* in YAP, sockets may be in one of 4 possible status */
closed_socket
} socket_info;
typedef enum{ /* we accept two domains for the moment, IPV6 may follow */
typedef enum { /* we accept two domains for the moment, IPV6 may follow */
af_inet, /* IPV4 */
af_unix /* or AF_FILE */
} socket_domain;
@ -76,8 +80,8 @@ Int Yap_CloseSocket(int, socket_info, socket_domain);
#endif /* USE_SOCKET */
/************ SWI compatible support for unicode representations ************/
typedef struct yap_io_position
{ int64_t byteno; /* byte-position in file */
typedef struct yap_io_position {
int64_t byteno; /* byte-position in file */
int64_t charno; /* character position in file */
long int lineno; /* lineno in file */
long int linepos; /* position in line */
@ -85,15 +89,14 @@ typedef struct yap_io_position
} yapIOPOS;
#ifndef _PL_STREAM_H
typedef struct
{ Atom file; /* current source file */
typedef struct {
Atom file; /* current source file */
yapIOPOS position; /* Line, line pos, char and byte */
} yapSourceLocation;
#endif
#define RD_MAGIC 0xefebe128
typedef struct vlist_struct_t {
struct VARSTRUCT *ve;
struct vlist_struct_t *next;
@ -106,9 +109,8 @@ typedef struct qq_struct_t {
struct qq_struct_t *next;
} qq_t;
typedef struct read_data_t
{ unsigned char *here; /* current character */
typedef struct read_data_t {
unsigned char *here; /* current character */
unsigned char *base; /* base of clause */
unsigned char *end; /* end of the clause */
unsigned char *token_start; /* start of most recent read token */
@ -123,7 +125,7 @@ typedef struct read_data_t
Term subtpos; /* Report Subterm positions */
bool cycles; /* Re-establish cycles */
yapSourceLocation start_of_term; /* Position of start of term */
struct mod_entry* module; /* Current source module */
struct mod_entry *module; /* Current source module */
unsigned int flags; /* Module syntax flags */
int styleCheck; /* style-checking mask */
bool backquoted_string; /* Read `hello` as string */
@ -150,7 +152,7 @@ typedef struct read_data_t
} read_data, *ReadData;
Term Yap_read_term(int inp_stream, Term opts, int nargs);
Term Yap_Parse( UInt prio );
Term Yap_Parse(UInt prio);
void init_read_data(ReadData _PL_rd, struct stream_desc *s);
@ -163,8 +165,7 @@ typedef int (*GetsFunc)(int, UInt, char *);
#include <sys/socket.h>
#endif
typedef
struct mem_desc {
typedef struct mem_desc {
char *buf; /* where the file is being read from/written to */
int src; /* where the space comes from, 0 code space, 1 malloc */
Int max_size; /* maximum buffer size (may be changed dynamically) */
@ -172,18 +173,17 @@ struct mem_desc {
volatile void *error_handler;
} memHandle;
typedef struct stream_desc
{
typedef struct stream_desc {
Atom name;
Term user_name;
FILE* file;
FILE *file;
// useful in memory streams
char *nbuf;
size_t nsize;
union {
struct {
#define PLGETC_BUF_SIZE 4096
char *buf, *ptr;
unsigned char *buf, *ptr;
int left;
} file;
memHandle mem_string;
@ -198,7 +198,7 @@ typedef struct stream_desc
} socket;
#endif
struct {
const char *buf, *ptr;
const unsigned char *buf, *ptr;
} irl;
} u;
Int charcount, linecount, linepos;
@ -207,39 +207,31 @@ typedef struct stream_desc
#if defined(YAPOR) || defined(THREADS)
lockvar streamlock; /* protect stream access */
#endif
int (* stream_putc)(int, int); /* function the stream uses for writing */
int (* stream_getc)(int); /* function the stream uses for reading */
GetsFunc stream_gets; /* function the stream uses for reading a sequence of characters */
int (*stream_putc)(int, int); /* function the stream uses for writing */
int (*stream_getc)(int); /* function the stream uses for reading */
GetsFunc stream_gets; /* function the stream uses for reading a sequence of
characters */
/* function the stream uses for parser. It may be different if the ISO
character conversion is on */
int (* stream_wgetc_for_read)(int);
int (* stream_wgetc)(int);
int (* stream_getc_for_utf8)(int);
int (* stream_wputc)(int,wchar_t);
uint64_t utf8_buf; /* used to translate to utf-8 */
int (*stream_wgetc_for_read)(int);
int (*stream_wgetc)(int);
int (*stream_wputc)(int, wchar_t);
encoding_t encoding;
mbstate_t mbstate;
}
StreamDesc;
} StreamDesc;
static inline bool
IsStreamTerm(Term t)
{
static inline bool IsStreamTerm(Term t) {
return !IsVarTerm(t) &&
(IsAtomTerm(t) || (IsApplTerm(t) && (FunctorOfTerm(t) == FunctorStream)));
(IsAtomTerm(t) ||
(IsApplTerm(t) && (FunctorOfTerm(t) == FunctorStream)));
}
static inline StreamDesc *
Yap_GetStreamHandle(Term t)
{
int sno = Yap_CheckStream( t , 0, "stream search");
static inline StreamDesc *Yap_GetStreamHandle(Term t) {
int sno = Yap_CheckStream(t, 0, "stream search");
if (sno < 0)
return NULL;
return GLOBAL_Stream+sno;
return GLOBAL_Stream + sno;
}
#define YAP_ERROR NIL
#define MaxStreams 64
@ -252,54 +244,52 @@ Yap_GetStreamHandle(Term t)
#define ALIASES_BLOCK_SIZE 8
void Yap_InitStdStreams (void);
Term Yap_StreamPosition (int);
void Yap_InitStdStreams(void);
Term Yap_StreamPosition(int);
static inline int
GetCurInpPos (StreamDesc * inp_stream)
{
static inline int GetCurInpPos(StreamDesc *inp_stream) {
return (inp_stream->linecount);
}
#define PlIOError(type, culprit,...) PlIOError__(__FILE__, __FUNCTION__, __LINE__, type, culprit, __VA_ARGS__)
#define PlIOError(type, culprit, ...) \
PlIOError__(__FILE__, __FUNCTION__, __LINE__, type, culprit, __VA_ARGS__)
Int PlIOError__( const char *, const char *, int, yap_error_number, Term, ...);
Int PlIOError__(const char *, const char *, int, yap_error_number, Term, ...);
int GetFreeStreamD(void);
Term Yap_MkStream (int n);
bool Yap_PrintWarning( Term twarning );
Term Yap_MkStream(int n);
bool Yap_PrintWarning(Term twarning);
void Yap_plwrite(Term, struct stream_desc *, int, int, int);
void Yap_WriteAtom(struct stream_desc *s, Atom atom);
Term Yap_scan_num(struct stream_desc *);
void Yap_DefaultStreamOps( StreamDesc *st );
void Yap_PipeOps( StreamDesc *st );
void Yap_MemOps( StreamDesc *st );
bool Yap_CloseMemoryStream( int sno );
void Yap_ConsolePipeOps( StreamDesc *st );
void Yap_SocketOps( StreamDesc *st );
void Yap_ConsoleSocketOps( StreamDesc *st );
bool Yap_ReadlineOps( StreamDesc *st );
int Yap_OpenBufWriteStream( USES_REGS1);
void Yap_ConsoleOps( StreamDesc *s );
void Yap_DefaultStreamOps(StreamDesc *st);
void Yap_PipeOps(StreamDesc *st);
void Yap_MemOps(StreamDesc *st);
bool Yap_CloseMemoryStream(int sno);
void Yap_ConsolePipeOps(StreamDesc *st);
void Yap_SocketOps(StreamDesc *st);
void Yap_ConsoleSocketOps(StreamDesc *st);
bool Yap_ReadlineOps(StreamDesc *st);
int Yap_OpenBufWriteStream(USES_REGS1);
void Yap_ConsoleOps(StreamDesc *s);
void Yap_InitRandomPreds (void);
void Yap_InitSignalPreds (void);
void Yap_InitTimePreds (void);
void Yap_InitRandomPreds(void);
void Yap_InitSignalPreds(void);
void Yap_InitTimePreds(void);
void Yap_init_socks(char *host, long interface_port);
void Yap_InitPipes( void );
void Yap_InitMem( void );
void Yap_InitSockets( void );
void Yap_InitPipes(void);
void Yap_InitMem(void);
void Yap_InitSockets(void);
void Yap_InitSocketLayer(void);
void Yap_InitMems( void );
void Yap_InitConsole( void );
void Yap_InitReadlinePreds( void );
bool Yap_InitReadline( Term );
void Yap_InitMems(void);
void Yap_InitConsole(void);
void Yap_InitReadlinePreds(void);
bool Yap_InitReadline(Term);
void Yap_InitChtypes(void);
void Yap_InitCharsio(void);
void Yap_InitFormat(void);
@ -307,75 +297,69 @@ void Yap_InitFiles(void);
void Yap_InitIOStreams(void);
void Yap_InitWriteTPreds(void);
void Yap_InitReadTPreds(void);
void Yap_socketStream( StreamDesc *s );
void Yap_ReadlineFlush( int sno );
Int Yap_ReadlinePeekChar( int sno );
void Yap_socketStream(StreamDesc *s);
void Yap_ReadlineFlush(int sno);
Int Yap_ReadlinePeekChar(int sno);
int Yap_ReadlineForSIGINT(void);
bool Yap_ReadlinePrompt( StreamDesc * s );
bool Yap_ReadlinePrompt(StreamDesc *s);
Int Yap_peek( int sno );
Int Yap_peek(int sno);
Term Yap_syntax_error (TokEntry * tokptr, int sno);
Term Yap_syntax_error(TokEntry *tokptr, int sno);
int console_post_process_read_char( int, StreamDesc *);
int console_post_process_eof( StreamDesc *);
int post_process_read_char( int, StreamDesc *);
int post_process_eof( StreamDesc *);
int post_process_weof( StreamDesc *);
int console_post_process_read_char(int, StreamDesc *);
int console_post_process_eof(StreamDesc *);
int post_process_read_char(int, StreamDesc *);
int post_process_eof(StreamDesc *);
int post_process_weof(StreamDesc *);
bool is_same_tty(FILE *f1, FILE *f2);
int ISOWGetc (int sno);
int GetUTF8 (int sno);
int ISOWGetc(int sno);
int GetUTF8(int sno);
Term read_line(int sno);
int PlGets (int sno, UInt size, char *buf);
int PlGets(int sno, UInt size, char *buf);
GetsFunc PlGetsFunc(void);
int PlGetc (int sno);
int FilePutc (int sno, int c);
int DefaultGets( int,UInt,char*);
int PlGetc(int sno);
int FilePutc(int sno, int c);
int DefaultGets(int, UInt, char *);
int put_wchar(int sno, wchar_t ch);
Int GetStreamFd(int sno);
int ResetEOF(StreamDesc *s);
int EOFPeek(int sno);
int EOFWPeek(int sno);
void Yap_SetAlias (Atom arg, int sno);
bool Yap_AddAlias (Atom arg, int sno);
int Yap_CheckAlias (Atom arg);
int Yap_RemoveAlias (Atom arg, int snoinline);
void Yap_SetAlias (Atom arg, int sno);
void Yap_SetAlias(Atom arg, int sno);
bool Yap_AddAlias(Atom arg, int sno);
int Yap_CheckAlias(Atom arg);
int Yap_RemoveAlias(Atom arg, int snoinline);
void Yap_SetAlias(Atom arg, int sno);
void Yap_InitAliases(void);
void Yap_DeleteAliases (int sno);
bool Yap_FindStreamForAlias (Atom al);
bool Yap_FetchStreamAlias (int sno, Term t2 USES_REGS);
void Yap_DeleteAliases(int sno);
bool Yap_FindStreamForAlias(Atom al);
bool Yap_FetchStreamAlias(int sno, Term t2 USES_REGS);
INLINE_ONLY inline EXTERN void count_output_char(int ch, StreamDesc *s);
Term Yap_StreamUserName(int sno);
INLINE_ONLY inline EXTERN void
count_output_char(int ch, StreamDesc *s)
{
if (ch == '\n')
{
INLINE_ONLY inline EXTERN void count_output_char(int ch, StreamDesc *s) {
if (ch == '\n') {
#if MPWSHELL
if (mpwshell && (sno == StdOutStream || sno ==
StdErrStream) &&
!(s->status & Null_Stream_f))
{
putc (MPWSEP, s->file);
if (mpwshell && (sno == StdOutStream || sno == StdErrStream) &&
!(s->status & Null_Stream_f)) {
putc(MPWSEP, s->file);
if (!(GLOBAL_Stream[LOCAL_output_stream].status & Null_Stream_f))
fflush (stdout);
fflush(stdout);
}
#endif
/* Inform that we have written a newline */
++s->charcount;
++s->linecount;
s->linepos = 0; }
else {
s->linepos = 0;
} else {
#if MAC
if ((sno == StdOutStream || sno == StdErrStream)
&& s->linepos > 200)
if ((sno == StdOutStream || sno == StdErrStream) && s->linepos > 200)
sno->stream_putc(sno, '\n');
#endif
++s->charcount;
@ -383,32 +367,19 @@ count_output_char(int ch, StreamDesc *s)
}
}
inline static Term
StreamName(int i)
{
return(GLOBAL_Stream[i].user_name);
}
inline static Term StreamName(int i) { return (GLOBAL_Stream[i].user_name); }
inline static Atom
StreamFullName(int i)
{
return(GLOBAL_Stream[i].name);
}
inline static Atom StreamFullName(int i) { return (GLOBAL_Stream[i].name); }
inline static void
console_count_output_char(int ch, StreamDesc *s)
{
inline static void console_count_output_char(int ch, StreamDesc *s) {
CACHE_REGS
if (ch == '\n')
{
if (ch == '\n') {
#if MPWSHELL
if (mpwshell && (sno == StdOutStream || sno ==
StdErrStream) &&
!(s->status & Null_Stream_f))
{
putc (MPWSEP, s->file);
if (mpwshell && (sno == StdOutStream || sno == StdErrStream) &&
!(s->status & Null_Stream_f)) {
putc(MPWSEP, s->file);
if (!(GLOBAL_Stream[LOCAL_output_stream].status & Null_Stream_f))
fflush (stdout);
fflush(stdout);
}
#endif
++s->charcount;
@ -416,12 +387,10 @@ console_count_output_char(int ch, StreamDesc *s)
s->linepos = 0;
LOCAL_newline = TRUE;
/* Inform we are not at the start of a newline */
}
else {
} else {
LOCAL_newline = FALSE;
#if MAC
if ((sno == StdOutStream || sno == StdErrStream)
&& s->linepos > 200)
if ((sno == StdOutStream || sno == StdErrStream) && s->linepos > 200)
sno->stream_putc(sno, '\n');
#endif
++s->charcount;
@ -429,40 +398,31 @@ console_count_output_char(int ch, StreamDesc *s)
}
}
inline static Term
StreamPosition(int sno)
{
inline static Term StreamPosition(int sno) {
CACHE_REGS
Term sargs[5];
Int cpos;
cpos = GLOBAL_Stream[sno].charcount;
sargs[0] = MkIntegerTerm (LOCAL_StartCharCount = cpos);
sargs[1] = MkIntegerTerm (LOCAL_StartLineCount = GLOBAL_Stream[sno].linecount);
sargs[2] = MkIntegerTerm (LOCAL_StartLinePos = GLOBAL_Stream[sno].linepos);
sargs[3] = sargs[4] = MkIntTerm (0);
return Yap_MkApplTerm (FunctorStreamPos, 5, sargs);
sargs[0] = MkIntegerTerm(LOCAL_StartCharCount = cpos);
sargs[1] = MkIntegerTerm(LOCAL_StartLineCount = GLOBAL_Stream[sno].linecount);
sargs[2] = MkIntegerTerm(LOCAL_StartLinePos = GLOBAL_Stream[sno].linepos);
sargs[3] = sargs[4] = MkIntTerm(0);
return Yap_MkApplTerm(FunctorStreamPos, 5, sargs);
}
inline static Term
CurrentPositionToTerm(void)
{
inline static Term CurrentPositionToTerm(void) {
CACHE_REGS
Term sargs[5];
sargs[0] = MkIntegerTerm (LOCAL_StartCharCount);
sargs[1] = MkIntegerTerm (LOCAL_StartLineCount );
sargs[2] = MkIntegerTerm (LOCAL_StartLinePos );
sargs[3] = sargs[4] = MkIntTerm (0);
return Yap_MkApplTerm (FunctorStreamPos, 5, sargs);
sargs[0] = MkIntegerTerm(LOCAL_StartCharCount);
sargs[1] = MkIntegerTerm(LOCAL_StartLineCount);
sargs[2] = MkIntegerTerm(LOCAL_StartLinePos);
sargs[3] = sargs[4] = MkIntTerm(0);
return Yap_MkApplTerm(FunctorStreamPos, 5, sargs);
}
extern FILE *Yap_stdin;
extern FILE *Yap_stdout;
extern FILE *Yap_stderr;
char *Yap_MemExportStreamPtr( int sno );
char *Yap_MemExportStreamPtr(int sno);
#endif

View File

@ -227,21 +227,19 @@ static bool getLine(int inp, int out) {
CACHE_REGS
rl_instream = GLOBAL_Stream[inp].file;
rl_outstream = GLOBAL_Stream[out].file;
const char *myrl_line;
const unsigned char *myrl_line;
StreamDesc *s = GLOBAL_Stream + inp;
if (!(s->status & Tty_Stream_f))
return false;
/* window of vulnerability opened */
LOCAL_PrologMode |= ConsoleGetcMode;
fflush(NULL);
LOCAL_PrologMode |= ConsoleGetcMode;
if (LOCAL_newline) { // no output so far
myrl_line = readline(LOCAL_Prompt);
myrl_line = (unsigned char *)readline(LOCAL_Prompt);
} else {
myrl_line = readline(NULL);
myrl_line = (unsigned char *)readline(NULL);
}
/* Do it the gnu way */
if (LOCAL_PrologMode & InterruptMode) {
@ -261,7 +259,7 @@ static bool getLine(int inp, int out) {
if (myrl_line == NULL)
return false;
if (myrl_line[0] != '\0' && myrl_line[1] != '\0') {
add_history(myrl_line);
add_history((char *)myrl_line);
append_history(1, history_file);
}
s->u.irl.ptr = s->u.irl.buf = myrl_line;
@ -296,7 +294,7 @@ static int ReadlineGetc(int sno) {
bool fetch = (s->u.irl.buf == NULL);
if (!fetch || getLine(sno, StdErrStream)) {
const char *ttyptr = s->u.irl.ptr++, *myrl_line = s->u.irl.buf;
const unsigned char *ttyptr = s->u.irl.ptr++, *myrl_line = s->u.irl.buf;
ch = *ttyptr;
if (ch == '\0') {
ch = '\n';
@ -322,7 +320,7 @@ Int Yap_ReadlinePeekChar(int sno) {
int ch;
if (s->u.irl.buf) {
const char *ttyptr = s->u.irl.ptr;
const unsigned char *ttyptr = s->u.irl.ptr;
ch = *ttyptr;
if (ch == '\0') {
ch = '\n';
@ -350,15 +348,15 @@ int Yap_ReadlineForSIGINT(void) {
CACHE_REGS
int ch;
StreamDesc *s = &GLOBAL_Stream[StdInStream];
const char *myrl_line = s->u.irl.buf;
const unsigned char *myrl_line = s->u.irl.buf;
if ((LOCAL_PrologMode & ConsoleGetcMode) && myrl_line != (char *)NULL) {
if ((LOCAL_PrologMode & ConsoleGetcMode) && myrl_line != NULL) {
ch = myrl_line[0];
free((void *)myrl_line);
myrl_line = NULL;
return ch;
} else {
myrl_line = readline("Action (h for help): ");
myrl_line = (const unsigned char *)readline("Action (h for help): ");
if (!myrl_line) {
ch = EOF;
return ch;

View File

@ -976,6 +976,7 @@ static Int read_term(
Int out;
/* needs to change LOCAL_output_stream for write */
yhandle_t h = Yap_PushHandle(ARG2);
inp_stream = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3");
if (inp_stream == -1) {

View File

@ -295,19 +295,11 @@ static Int has_close_on_abort(
static bool
has_encoding(int sno,
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
if (0 && IsAtomTerm(t2)) {
encoding_t e = enc_id(RepAtom(AtomOfTerm(t2))->StrOfAE);
GLOBAL_Stream[sno].encoding = e;
return true;
} else {
const char *s = enc_name(LOCAL_encoding);
const char *s = enc_name(GLOBAL_Stream[sno].encoding);
return Yap_unify(t2, MkAtomTerm(Yap_LookupAtom(s)));
}
}
static bool
found_eof(int sno,
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
@ -719,9 +711,12 @@ static bool do_set_stream(int sno,
sno, (args[SET_STREAM_CLOSE_ON_ABORT].tvalue == TermTrue));
break;
case SET_STREAM_ENCODING:
{
Term t2 = args[SET_STREAM_ENCODING].tvalue;
Atom atEnc = AtomOfTerm(t2);
GLOBAL_Stream[sno].encoding =
enc_id(AtomOfTerm(args[SET_STREAM_ENCODING].tvalue)->StrOfAE);
has_encoding(sno, args[SET_STREAM_ENCODING].tvalue PASS_REGS);
enc_id(atEnc->StrOfAE, (GLOBAL_Stream[sno].status & HAS_BOM_f ? GLOBAL_Stream[sno].encoding :ENC_OCTET ) );
}
break;
case SET_STREAM_EOF_ACTION: {
Term t2 = args[SET_STREAM_EOF_ACTION].tvalue;

View File

@ -423,7 +423,7 @@ stream_position_data(Prop, Term, Value) :-
'$stream_position_field'(byte_count, 4).
'$set_encoding'(Enc) :-
stream_property(loop_stream, Enc).
set_stream(loop_stream, encoding(Enc)).
%! @}