static Int is_same_tty2(USES_REGS1) { /* 'prompt(Atom) */ int sni = Yap_CheckStream(ARG1, Input_Stream_f, "put/2"); int sno = Yap_CheckStream(ARG2, Output_Stream_f, "put/2"); bool out = (GLOBAL_Stream[sni].status & Tty_Stream_f) && (GLOBAL_Stream[sno].status & Tty_Stream_f) && is_same_tty(GLOBAL_Stream[sno].file, GLOBAL_Stream[sni].file); UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sni].streamlock); return out; }
static Int p_stream_to_terms(void) { int sno = Yap_CheckStream (ARG1, Input_Stream_f, "read_line_to_codes/2"); Term t = Deref(ARG3), tpos = TermNil; if (sno < 0) return FALSE; while (!(Stream[sno].status & Eof_Stream_f)) { /* skip errors */ TokEntry *tokstart = Yap_tokptr = Yap_toktide = Yap_tokenizer(sno, &tpos); if (!Yap_ErrorMessage) { Term th = Yap_Parse(); if (H >= ASP-1024) { UNLOCK(Stream[sno].streamlock); Yap_Error(OUT_OF_STACK_ERROR, ARG1, "read_stream_to_terms/3"); return FALSE; } if (!th || Yap_ErrorMessage) break; if (th == MkAtomTerm (AtomEof)) { UNLOCK(Stream[sno].streamlock); Yap_clean_tokenizer(tokstart, Yap_VarTable, Yap_AnonVarTable); return Yap_unify(t,ARG2); } else { t = MkPairTerm(th,t); } } Yap_clean_tokenizer(tokstart, Yap_VarTable, Yap_AnonVarTable); } UNLOCK(Stream[sno].streamlock); return Yap_unify(t,ARG2); }
static Int p_check_if_stream(USES_REGS1) { /* '$check_stream'(Stream) */ int sno = Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f | Socket_Stream_f, "check_stream/1"); if (sno != -1) UNLOCK(GLOBAL_Stream[sno].streamlock); return sno != -1; }
static Int clear_input( USES_REGS1 ) { int sno = Yap_CheckStream(ARG1, Input_Stream_f | Socket_Stream_f, "clear_input/1"); if (sno != -1) UNLOCK(GLOBAL_Stream[sno].streamlock); return clearInput(sno); }
static Int p_check_stream(USES_REGS1) { /* '$check_stream'(Stream,Mode) */ Term mode = Deref(ARG2); int sno = Yap_CheckStream( ARG1, AtomOfTerm(mode) == AtomRead ? Input_Stream_f : Output_Stream_f, "check_stream/2"); if (sno != -1) UNLOCK(GLOBAL_Stream[sno].streamlock); return sno != -1; }
FILE *Yap_GetOutputStream(Term t, const char *msg) { int sno = Yap_CheckStream(t, Output_Stream_f, msg); FILE *rc; if (!(GLOBAL_Stream[sno].status & (Tty_Stream_f | Socket_Stream_f))) rc = GLOBAL_Stream[sno].file; else rc = NULL; UNLOCK(GLOBAL_Stream[sno].streamlock); return rc; }
static Int lines_in_file(USES_REGS1) { Int sno = Yap_CheckStream(ARG1, (Input_Stream_f), "lines_in_file/2"); if (sno < 0) return false; FILE *f = GLOBAL_Stream[sno].file; size_t count = 0; int ch; #if __ANDROID__ #define getw getc #endif if (!f) return false; while ((ch = getw(f)) >= 0) { if (ch == '\n') { count++; } } return Yap_unify(ARG2, MkIntegerTerm(count)); }
static Int peek_mem_write_stream ( USES_REGS1 ) { /* '$peek_mem_write_stream'(+GLOBAL_Stream,?S0,?S) */ Int sno = Yap_CheckStream (ARG1, (Output_Stream_f | InMemory_Stream_f), "close/2"); Int i; Term tf = ARG2; CELL *HI; const char *ptr; if (sno < 0) return (FALSE); restart: HI = HR; #if MAY_WRITE if (fflush(GLOBAL_Stream[sno].file) == 0) { ptr = GLOBAL_Stream[sno].nbuf; i = GLOBAL_Stream[sno].nsize; } #else ptr = GLOBAL_Stream[sno].u.mem_string.buf; i = GLOBAL_Stream[sno].u.mem_string.pos; #endif while (i > 0) { --i; tf = MkPairTerm(MkIntTerm(ptr[i]),tf); if (HR + 1024 >= ASP) { UNLOCK(GLOBAL_Stream[sno].streamlock); HR = HI; if (!Yap_gcl((ASP-HI)*sizeof(CELL), 3, ENV, Yap_gcP()) ) { UNLOCK(GLOBAL_Stream[sno].streamlock); Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); return(FALSE); } i = GLOBAL_Stream[sno].u.mem_string.pos; tf = ARG2; LOCK(GLOBAL_Stream[sno].streamlock); goto restart; } } UNLOCK(GLOBAL_Stream[sno].streamlock); return (Yap_unify(ARG3,tf)); }
static Int p_stream_to_codes(void) { int sno = Yap_CheckStream (ARG1, Input_Stream_f, "read_line_to_codes/2"); CELL *HBASE = H; CELL *h0 = &ARG4; if (sno < 0) return FALSE; while (!(Stream[sno].status & Eof_Stream_f)) { /* skip errors */ Int ch = Stream[sno].stream_getc(sno); Term t; if (ch == EOFCHAR) break; t = MkIntegerTerm(ch); h0[0] = AbsPair(H); *H = t; H+=2; h0 = H-1; if (H >= ASP-1024) { RESET_VARIABLE(h0); ARG4 = AbsPair(HBASE); ARG5 = (CELL)h0; if (!Yap_gcl((ASP-HBASE)*sizeof(CELL), 5, ENV, gc_P(P,CP))) { Yap_Error(OUT_OF_STACK_ERROR, ARG1, "read_stream_to_codes/3"); return FALSE; } /* build a legal term again */ h0 = (CELL *)ARG5; HBASE = RepPair(ARG4); } } UNLOCK(Stream[sno].streamlock); if (H == HBASE) return Yap_unify(ARG2,ARG3); RESET_VARIABLE(H-1); Yap_unify(H[-1],ARG3); return Yap_unify(AbsPair(HBASE),ARG2); }
static Int read_stream_to_codes(USES_REGS1) { int sno = Yap_CheckStream(ARG1, Input_Stream_f, "reaMkAtomTerm (AtomEofd_line_to_codes/2"); CELL *HBASE = HR; CELL *h0 = &ARG4; if (sno < 0) return FALSE; while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) { /* skip errors */ Int ch = GLOBAL_Stream[sno].stream_getc(sno); Term t; if (ch == EOFCHAR) break; t = MkIntegerTerm(ch); h0[0] = AbsPair(HR); *HR = t; HR += 2; h0 = HR - 1; yhandle_t news, news1, st = Yap_StartSlots(); if (HR >= ASP - 1024) { RESET_VARIABLE(h0); news = Yap_InitSlot(AbsPair(HBASE)); news1 = Yap_InitSlot((CELL)(h0)); if (!Yap_gcl((ASP - HBASE) * sizeof(CELL), 3, ENV, Yap_gcP())) { Yap_Error(RESOURCE_ERROR_STACK, ARG1, "read_stream_to_codes/3"); return false; } /* build a legal term again */ h0 = (CELL *)(Yap_GetFromSlot(news1)); HBASE = RepPair(Yap_GetFromSlot(news)); } Yap_CloseSlots(st); } UNLOCK(GLOBAL_Stream[sno].streamlock); if (HR == HBASE) return Yap_unify(ARG2, ARG3); RESET_VARIABLE(HR - 1); Yap_unify(HR[-1], ARG3); return Yap_unify(AbsPair(HBASE), ARG2); }
static Int file_size(USES_REGS1) { int rc; Int sno = Yap_CheckStream( ARG1, (Input_Stream_f | Output_Stream_f | Socket_Stream_f), "file_size/2"); if (sno < 0) return (FALSE); VFS_t *vfs; char *s = RepAtom(GLOBAL_Stream[sno].name)->StrOfAE; if (!s) return false; if ((vfs = vfs_owner(s))) { vfs_stat st; vfs->stat(vfs, s, &st); UNLOCK(GLOBAL_Stream[sno].streamlock); return Yap_unify_constant(ARG2, MkIntegerTerm(st.st_size)); } if (GLOBAL_Stream[sno].status & Seekable_Stream_f && !(GLOBAL_Stream[sno].status & (InMemory_Stream_f | Socket_Stream_f | Pipe_Stream_f))) { // there struct stat file_stat; if ((rc = fstat(fileno(GLOBAL_Stream[sno].file), &file_stat)) < 0) { UNLOCK(GLOBAL_Stream[sno].streamlock); if (rc == ENOENT) PlIOError(EXISTENCE_ERROR_SOURCE_SINK, ARG1, "%s in file_size", strerror(errno)); else PlIOError(PERMISSION_ERROR_INPUT_STREAM, ARG1, "%s in file_size", strerror(errno)); return false; } // and back again UNLOCK(GLOBAL_Stream[sno].streamlock); return Yap_unify_constant(ARG2, MkIntegerTerm(file_stat.st_size)); } UNLOCK(GLOBAL_Stream[sno].streamlock); return false; }
static Int read_stream_to_terms(USES_REGS1) { int sno = Yap_CheckStream(ARG1, Input_Stream_f, "read_line_to_codes/2"); Term t, hd; yhandle_t tails, news; if (sno < 0) return FALSE; t = AbsPair(HR); RESET_VARIABLE(HR); Yap_InitSlot((CELL)(HR)); tails = Yap_InitSlot((CELL)(HR)); news = Yap_InitSlot((CELL)(HR)); HR++; while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) { RESET_VARIABLE(HR); RESET_VARIABLE(HR + 1); hd = (CELL)HR; Yap_PutInSlot(news, (CELL)(HR + 1)); HR += 2; while ((hd = Yap_read_term(sno, TermNil, 2)) == 0L) ; // just ignore failure CELL *pt = VarOfTerm(Yap_GetFromSlot(tails)); if (Deref(hd) == TermEOfCode) { *pt = Deref(ARG3); break; } else { CELL *newpt = (CELL *)Yap_GetFromSlot(news); *pt = AbsPair(newpt - 1); Yap_PutInSlot(tails, (CELL)newpt); } } UNLOCK(GLOBAL_Stream[sno].streamlock); return Yap_unify(t, ARG2); }
static Int rl_to_codes(Term TEnd, int do_as_binary, int arity) { int sno = Yap_CheckStream (ARG1, Input_Stream_f, "read_line_to_codes/2"); Int status; UInt max_inp, buf_sz, sz; char *buf; int binary_stream; if (sno < 0) return FALSE; status = Stream[sno].status; binary_stream = Stream[sno].status & Binary_Stream_f; if (status & Eof_Stream_f) { UNLOCK(Stream[sno].streamlock); return Yap_unify_constant(ARG2, MkAtomTerm (AtomEof)); } max_inp = (ASP-H)/2-1024; buf = (char *)TR; buf_sz = (char *)Yap_TrailTop-buf; while (TRUE) { if ( buf_sz > max_inp ) { buf_sz = max_inp; } if (do_as_binary && !binary_stream) Stream[sno].status |= Binary_Stream_f; sz = Stream[sno].stream_gets(sno, buf_sz, buf); if (do_as_binary && !binary_stream) Stream[sno].status &= ~Binary_Stream_f; if (sz == -1 || sz == 0) { if (Stream[sno].status & Eof_Stream_f) { UNLOCK(Stream[sno].streamlock); return Yap_unify_constant(ARG2, MkAtomTerm (AtomEof)); } UNLOCK(Stream[sno].streamlock); return FALSE; } if (Stream[sno].status & Eof_Stream_f || buf[sz-1] == 10) { /* we're done */ Term end; if (!(do_as_binary || Stream[sno].status & Eof_Stream_f)) { UNLOCK(Stream[sno].streamlock); /* handle CR before NL */ if (sz-2 >= 0 && buf[sz-2] == 13) buf[sz-2] = '\0'; else buf[sz-1] = '\0'; } else { UNLOCK(Stream[sno].streamlock); } if (arity == 2) end = TermNil; else end = Deref(XREGS[arity]); return Yap_unify(ARG2, Yap_StringToDiffList((char *)TR, end)) ; } buf += (buf_sz-1); max_inp -= (buf_sz-1); if (max_inp <= 0) { UNLOCK(Stream[sno].streamlock); Yap_Error(OUT_OF_STACK_ERROR, ARG1, "read_line_to_codes/%d", arity); return FALSE; } } }
static Int rl_to_codes(Term TEnd, int do_as_binary, int arity USES_REGS) { int sno = Yap_CheckStream(ARG1, Input_Stream_f, "read_line_to_codes/2"); StreamDesc *st = GLOBAL_Stream + sno; Int status; UInt max_inp, buf_sz, sz; unsigned char *buf; bool binary_stream; int ch; if (sno < 0) return false; status = GLOBAL_Stream[sno].status; binary_stream = GLOBAL_Stream[sno].status & Binary_Stream_f; if (status & Eof_Stream_f) { UNLOCK(GLOBAL_Stream[sno].streamlock); return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof)); } max_inp = (ASP - HR) / 2 - 1024; buf = (unsigned char *)TR; buf_sz = (unsigned char *)LOCAL_TrailTop - buf; while (true) { if (buf_sz > max_inp) { buf_sz = max_inp; } if (do_as_binary && !binary_stream) { GLOBAL_Stream[sno].status |= Binary_Stream_f; } if (st->status & Binary_Stream_f) { char *b = (char *)TR; sz = fread(b, 1, buf_sz, GLOBAL_Stream[sno].file); } else { unsigned char *pt = buf; do { ch = st->stream_wgetc_for_read(sno); if (ch < 127) { *pt++ = ch; if (ch < 0) { ch = '\n'; pt[-1] = '\n'; } } else { pt += get_utf8(pt, 4, &ch); if (pt + 4 == buf + buf_sz) break; } } while (ch != '\n'); sz = pt - buf; } if (do_as_binary && !binary_stream) GLOBAL_Stream[sno].status &= ~Binary_Stream_f; if (sz == -1 || sz == 0) { if (GLOBAL_Stream[sno].status & Eof_Stream_f) { UNLOCK(GLOBAL_Stream[sno].streamlock); return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof)); } UNLOCK(GLOBAL_Stream[sno].streamlock); } if (GLOBAL_Stream[sno].status & Eof_Stream_f || buf[sz - 1] == 10) { /* we're done */ Term end; if (!(do_as_binary || GLOBAL_Stream[sno].status & Eof_Stream_f)) { UNLOCK(GLOBAL_Stream[sno].streamlock); /* handle CR before NL */ if ((Int)sz - 2 >= 0 && buf[sz - 2] == 13) buf[sz - 2] = '\0'; else buf[sz - 1] = '\0'; } else { UNLOCK(GLOBAL_Stream[sno].streamlock); } if (arity == 2) end = TermNil; else end = Deref(XREGS[arity]); return Yap_unify( ARG2, Yap_UTF8ToDiffListOfCodes((const char *)TR, end PASS_REGS)); } buf += (buf_sz - 1); max_inp -= (buf_sz - 1); if (max_inp <= 0) { UNLOCK(GLOBAL_Stream[sno].streamlock); Yap_Error(RESOURCE_ERROR_STACK, ARG1, "read_line_to_codes/%d", arity); return FALSE; } } }
static Int read_line_to_string(USES_REGS1) { int sno = Yap_CheckStream(ARG1, Input_Stream_f, "read_line_to_codes/2"); Int status; UInt max_inp, buf_sz; unsigned char *buf; size_t sz; StreamDesc *st = GLOBAL_Stream + sno; int ch; if (sno < 0) return false; status = GLOBAL_Stream[sno].status; if (status & Eof_Stream_f) { UNLOCK(GLOBAL_Stream[sno].streamlock); return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof)); } max_inp = (ASP - HR) / 2 - 1024; buf = (unsigned char *)TR; buf_sz = (unsigned char *)LOCAL_TrailTop - buf; if (buf_sz > max_inp) { buf_sz = max_inp; } if (st->status & Binary_Stream_f) { char *b = (char *)TR; sz = fread(b, 1, buf_sz, GLOBAL_Stream[sno].file); } else { unsigned char *pt = buf; do { ch = st->stream_wgetc_for_read(sno); if (ch < 127) { *pt++ = ch; if (ch < 0) { ch = '\n'; pt[-1] = '\n'; } } else { pt += get_utf8(pt, 4, &ch); if (pt + 4 == buf + buf_sz) break; } } while (ch != '\n'); sz = pt - buf; } if (sz == -1 || sz == 0) { if (GLOBAL_Stream[sno].status & Eof_Stream_f) { UNLOCK(GLOBAL_Stream[sno].streamlock); return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof)); } UNLOCK(GLOBAL_Stream[sno].streamlock); return false; } if (GLOBAL_Stream[sno].status & Eof_Stream_f || buf[sz - 1] == 10) { /* we're done */ if (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) { UNLOCK(GLOBAL_Stream[sno].streamlock); /* handle CR before NL */ if ((Int)sz - 2 >= 0 && buf[sz - 2] == 13) buf[sz - 2] = '\0'; else { buf[sz - 1] = '\0'; } } else { UNLOCK(GLOBAL_Stream[sno].streamlock); } } if (GLOBAL_Stream[sno].encoding == ENC_ISO_UTF8) { return Yap_unify(ARG2, Yap_UTF8ToString((const char *)TR PASS_REGS)); } else if (GLOBAL_Stream[sno].encoding == ENC_WCHAR) { return Yap_unify(ARG2, Yap_WCharsToString((const wchar_t *)TR PASS_REGS)); } else { return Yap_unify( ARG2, Yap_CharsToString((const char *)TR, ENC_ISO_LATIN1 PASS_REGS)); } buf += (buf_sz - 1); max_inp -= (buf_sz - 1); if (max_inp <= 0) { UNLOCK(GLOBAL_Stream[sno].streamlock); Yap_Error(RESOURCE_ERROR_STACK, ARG1, NULL); return FALSE; } }