indenting
This commit is contained in:
parent
55a840e2d8
commit
b89f6e55b4
191
os/console.c
191
os/console.c
@ -8,7 +8,7 @@
|
|||||||
* *
|
* *
|
||||||
**************************************************************************
|
**************************************************************************
|
||||||
* *
|
* *
|
||||||
* File: readline.c *
|
* File: readline.c *
|
||||||
* Last rev: 5/2/88 *
|
* Last rev: 5/2/88 *
|
||||||
* mods: *
|
* mods: *
|
||||||
* comments: Input/Output C implemented predicates *
|
* comments: Input/Output C implemented predicates *
|
||||||
@ -19,32 +19,31 @@
|
|||||||
* @file console.c
|
* @file console.c
|
||||||
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
|
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
|
||||||
* @date Wed Jan 20 00:56:23 2016
|
* @date Wed Jan 20 00:56:23 2016
|
||||||
*
|
*
|
||||||
* @brief
|
* @brief
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
* This file includes the interface to the console IO, tty style. Refer also to the readline library.
|
* This file includes the interface to the console IO, tty style. Refer also to
|
||||||
|
the readline library.
|
||||||
* @defgroup console Support for console-based interaction.
|
* @defgroup console Support for console-based interaction.
|
||||||
* @ingroup InputOutput
|
* @ingroup InputOutput
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "sysbits.h"
|
#include "sysbits.h"
|
||||||
|
|
||||||
static Int prompt( USES_REGS1 );
|
static Int prompt(USES_REGS1);
|
||||||
static Int prompt1( USES_REGS1 );
|
static Int prompt1(USES_REGS1);
|
||||||
|
|
||||||
static int ConsoleGetc( int);
|
static int ConsoleGetc(int);
|
||||||
static int ConsolePutc( int, int);
|
static int ConsolePutc(int, int);
|
||||||
|
|
||||||
/* check if we read a newline or an EOF */
|
/* check if we read a newline or an EOF */
|
||||||
int
|
int console_post_process_read_char(int ch, StreamDesc *s) {
|
||||||
console_post_process_read_char(int ch, StreamDesc *s)
|
|
||||||
{
|
|
||||||
/* the character is also going to be output by the console handler */
|
/* the character is also going to be output by the console handler */
|
||||||
console_count_output_char(ch,GLOBAL_Stream+StdErrStream);
|
console_count_output_char(ch, GLOBAL_Stream + StdErrStream);
|
||||||
if (ch == '\n') {
|
if (ch == '\n') {
|
||||||
CACHE_REGS
|
CACHE_REGS
|
||||||
++s->linecount;
|
++s->linecount;
|
||||||
@ -60,77 +59,63 @@ console_post_process_read_char(int ch, StreamDesc *s)
|
|||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool is_same_tty(FILE *f1, FILE *f2) {
|
||||||
bool
|
|
||||||
is_same_tty(FILE *f1, FILE *f2)
|
|
||||||
{
|
|
||||||
#if HAVE_TTYNAME
|
#if HAVE_TTYNAME
|
||||||
return(ttyname(fileno(f1)) == ttyname(fileno(f2)));
|
return ttyname_r(fileno(f1), LOCAL_FileNameBuf, YAP_FILENAME_MAX - 1) ==
|
||||||
#else
|
ttyname_r(fileno(f2), LOCAL_FileNameBuf, YAP_FILENAME_MAX - 1);
|
||||||
|
#endif
|
||||||
// assume a single console, for now
|
// assume a single console, for now
|
||||||
return true;
|
return true;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Int
|
static Int is_same_tty2(USES_REGS1) { /* 'prompt(Atom) */
|
||||||
is_same_tty2 (USES_REGS1)
|
int sni = Yap_CheckStream(ARG1, Input_Stream_f, "put/2");
|
||||||
{ /* 'prompt(Atom) */
|
int sno = Yap_CheckStream(ARG2, Output_Stream_f, "put/2");
|
||||||
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) &&
|
bool out = (GLOBAL_Stream[sni].status & Tty_Stream_f) &&
|
||||||
(GLOBAL_Stream[sno].status & Tty_Stream_f) &&
|
(GLOBAL_Stream[sno].status & Tty_Stream_f) &&
|
||||||
is_same_tty(GLOBAL_Stream[sno].file,GLOBAL_Stream[sni].file);
|
is_same_tty(GLOBAL_Stream[sno].file, GLOBAL_Stream[sni].file);
|
||||||
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
||||||
UNLOCK(GLOBAL_Stream[sni].streamlock);
|
UNLOCK(GLOBAL_Stream[sni].streamlock);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void Yap_ConsoleOps(StreamDesc *s, bool recursive) {
|
||||||
Yap_ConsoleOps( StreamDesc *s, bool recursive )
|
|
||||||
{
|
|
||||||
if (!recursive)
|
if (!recursive)
|
||||||
Yap_DefaultStreamOps( s );
|
Yap_DefaultStreamOps(s);
|
||||||
/* the putc routine only has to check it is putting out a newline */
|
/* the putc routine only has to check it is putting out a newline */
|
||||||
s->stream_putc = ConsolePutc;
|
s->stream_putc = ConsolePutc;
|
||||||
#if USE_READLINE
|
#if USE_READLINE
|
||||||
/* if a tty have a special routine to call readline */
|
/* if a tty have a special routine to call readline */
|
||||||
if (( s->status & Readline_Stream_f) &&
|
if ((s->status & Readline_Stream_f) && trueGlobalPrologFlag(READLINE_FLAG)) {
|
||||||
trueGlobalPrologFlag(READLINE_FLAG) ) {
|
if (Yap_ReadlineOps(s))
|
||||||
if (Yap_ReadlineOps( s ))
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
s->stream_getc = ConsoleGetc;
|
s->stream_getc = ConsoleGetc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */
|
/* static */
|
||||||
static int
|
static int ConsolePutc(int sno, int ch) {
|
||||||
ConsolePutc (int sno, int ch)
|
|
||||||
{
|
|
||||||
StreamDesc *s = &GLOBAL_Stream[sno];
|
StreamDesc *s = &GLOBAL_Stream[sno];
|
||||||
#if MAC || _MSC_VER || defined(__MINGW32__)
|
#if MAC || _MSC_VER || defined(__MINGW32__)
|
||||||
if (ch == 10)
|
if (ch == 10) {
|
||||||
{
|
putc('\n', s->file);
|
||||||
putc ('\n', s->file);
|
LOCAL_newline = true;
|
||||||
LOCAL_newline = true;
|
} else
|
||||||
}
|
|
||||||
else
|
|
||||||
#endif
|
#endif
|
||||||
putc (ch, s->file);
|
putc(ch, s->file);
|
||||||
console_count_output_char(ch,s);
|
console_count_output_char(ch, s);
|
||||||
return ((int) ch);
|
return ((int)ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* send a prompt, and use the system for internal buffering. Speed is
|
/* send a prompt, and use the system for internal buffering. Speed is
|
||||||
not of the essence here !!! */
|
not of the essence here !!! */
|
||||||
static int
|
static int ConsoleGetc(int sno) {
|
||||||
ConsoleGetc(int sno)
|
|
||||||
{
|
|
||||||
CACHE_REGS
|
CACHE_REGS
|
||||||
register StreamDesc *s = &GLOBAL_Stream[sno];
|
register StreamDesc *s = &GLOBAL_Stream[sno];
|
||||||
int ch;
|
int ch;
|
||||||
|
|
||||||
restart:
|
restart:
|
||||||
/* keep the prompt around, just in case, but don't actually
|
/* keep the prompt around, just in case, but don't actually
|
||||||
show it in silent mode */
|
show it in silent mode */
|
||||||
if (LOCAL_newline) {
|
if (LOCAL_newline) {
|
||||||
@ -139,10 +124,10 @@ ConsoleGetc(int sno)
|
|||||||
|
|
||||||
/* use the default routine */
|
/* use the default routine */
|
||||||
while ((ch = *cptr++) != '\0') {
|
while ((ch = *cptr++) != '\0') {
|
||||||
GLOBAL_Stream[StdErrStream].stream_putc(StdErrStream, ch);
|
GLOBAL_Stream[StdErrStream].stream_putc(StdErrStream, ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
strncpy (LOCAL_Prompt, (char *)RepAtom (LOCAL_AtPrompt)->StrOfAE, MAX_PROMPT);
|
strncpy(LOCAL_Prompt, (char *)RepAtom(LOCAL_AtPrompt)->StrOfAE, MAX_PROMPT);
|
||||||
LOCAL_newline = FALSE;
|
LOCAL_newline = FALSE;
|
||||||
}
|
}
|
||||||
#if HAVE_SIGINTERRUPT
|
#if HAVE_SIGINTERRUPT
|
||||||
@ -154,7 +139,7 @@ ConsoleGetc(int sno)
|
|||||||
siginterrupt(SIGINT, FALSE);
|
siginterrupt(SIGINT, FALSE);
|
||||||
#endif
|
#endif
|
||||||
if (LOCAL_PrologMode & InterruptMode) {
|
if (LOCAL_PrologMode & InterruptMode) {
|
||||||
Yap_external_signal( 0, YAP_INT_SIGNAL );
|
Yap_external_signal(0, YAP_INT_SIGNAL);
|
||||||
LOCAL_PrologMode &= ~ConsoleGetcMode;
|
LOCAL_PrologMode &= ~ConsoleGetcMode;
|
||||||
LOCAL_newline = TRUE;
|
LOCAL_newline = TRUE;
|
||||||
if (LOCAL_PrologMode & AbortMode) {
|
if (LOCAL_PrologMode & AbortMode) {
|
||||||
@ -167,7 +152,7 @@ ConsoleGetc(int sno)
|
|||||||
LOCAL_PrologMode &= ~ConsoleGetcMode;
|
LOCAL_PrologMode &= ~ConsoleGetcMode;
|
||||||
}
|
}
|
||||||
if (ch == EOF)
|
if (ch == EOF)
|
||||||
return EOF;
|
return EOF;
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,42 +164,41 @@ Changes YAP input prompt for the .
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static Int
|
static Int prompt1(USES_REGS1) { /* prompt1(Atom) */
|
||||||
prompt1 ( USES_REGS1 )
|
|
||||||
{ /* prompt1(Atom) */
|
|
||||||
Term t = Deref(ARG1);
|
Term t = Deref(ARG1);
|
||||||
Atom a;
|
Atom a;
|
||||||
if (IsVarTerm (t) || !IsAtomTerm (t))
|
if (IsVarTerm(t) || !IsAtomTerm(t))
|
||||||
|
return (FALSE);
|
||||||
|
LOCAL_AtPrompt = a = AtomOfTerm(t);
|
||||||
|
if (strlen((char *)RepAtom(a)->StrOfAE) > MAX_PROMPT) {
|
||||||
|
Yap_Error(SYSTEM_ERROR_INTERNAL, t, "prompt %s is too long",
|
||||||
|
RepAtom(a)->StrOfAE);
|
||||||
return (FALSE);
|
return (FALSE);
|
||||||
LOCAL_AtPrompt = a = AtomOfTerm (t);
|
|
||||||
if (strlen ((char *)RepAtom (a)->StrOfAE) > MAX_PROMPT) {
|
|
||||||
Yap_Error(SYSTEM_ERROR_INTERNAL,t,"prompt %s is too long", RepAtom (a)->StrOfAE);
|
|
||||||
return(FALSE);
|
|
||||||
}
|
}
|
||||||
strncpy(LOCAL_Prompt, (char *)RepAtom (a)->StrOfAE, MAX_PROMPT);
|
strncpy(LOCAL_Prompt, (char *)RepAtom(a)->StrOfAE, MAX_PROMPT);
|
||||||
return (TRUE);
|
return (TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @pred prompt(- _A_,+ _B_)
|
/** @pred prompt(- _A_,+ _B_)
|
||||||
|
|
||||||
Changes YAP input prompt from _A_ to _B_, active on *next* standard input interaction.
|
Changes YAP input prompt from _A_ to _B_, active on *next* standard input
|
||||||
|
interaction.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
static Int
|
static Int prompt(USES_REGS1) { /* prompt(Old,New) */
|
||||||
prompt ( USES_REGS1 )
|
Term t = Deref(ARG2);
|
||||||
{ /* prompt(Old,New) */
|
|
||||||
Term t = Deref (ARG2);
|
|
||||||
Atom a;
|
Atom a;
|
||||||
if (!Yap_unify_constant (ARG1, MkAtomTerm (LOCAL_AtPrompt)))
|
if (!Yap_unify_constant(ARG1, MkAtomTerm(LOCAL_AtPrompt)))
|
||||||
return (FALSE);
|
return (FALSE);
|
||||||
if (IsVarTerm (t) || !IsAtomTerm (t))
|
if (IsVarTerm(t) || !IsAtomTerm(t))
|
||||||
return (FALSE);
|
return (FALSE);
|
||||||
a = AtomOfTerm (t);
|
a = AtomOfTerm(t);
|
||||||
if (strlen(RepAtom (a)->StrOfAE) > MAX_PROMPT) {
|
if (strlen(RepAtom(a)->StrOfAE) > MAX_PROMPT) {
|
||||||
Yap_Error(SYSTEM_ERROR_INTERNAL,t,"prompt %s is too long", RepAtom (a)->StrOfAE);
|
Yap_Error(SYSTEM_ERROR_INTERNAL, t, "prompt %s is too long",
|
||||||
|
RepAtom(a)->StrOfAE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
strncpy(LOCAL_Prompt, (char *)RepAtom (LOCAL_AtPrompt)->StrOfAE, MAX_PROMPT);
|
strncpy(LOCAL_Prompt, (char *)RepAtom(LOCAL_AtPrompt)->StrOfAE, MAX_PROMPT);
|
||||||
LOCAL_AtPrompt = a;
|
LOCAL_AtPrompt = a;
|
||||||
return (TRUE);
|
return (TRUE);
|
||||||
}
|
}
|
||||||
@ -225,44 +209,39 @@ Make sure we have a prompt at this point, even if we have to
|
|||||||
introduce a new line.
|
introduce a new line.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
static Int
|
static Int ensure_prompting(USES_REGS1) { /* prompt(Old,New) */
|
||||||
ensure_prompting ( USES_REGS1 )
|
if (!LOCAL_newline) {
|
||||||
{ /* prompt(Old,New) */
|
|
||||||
if (!LOCAL_newline) {
|
|
||||||
GLOBAL_Stream[2].stream_wputc(2, 10); // hack!
|
GLOBAL_Stream[2].stream_wputc(2, 10); // hack!
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int Yap_GetCharForSIGINT(void) {
|
||||||
Yap_GetCharForSIGINT(void)
|
CACHE_REGS
|
||||||
{
|
|
||||||
CACHE_REGS
|
|
||||||
int ch;
|
int ch;
|
||||||
#if USE_READLINE
|
#if USE_READLINE
|
||||||
if (trueGlobalPrologFlag(READLINE_FLAG) ||
|
if (trueGlobalPrologFlag(READLINE_FLAG) ||
|
||||||
(ch = Yap_ReadlineForSIGINT()) == 0)
|
(ch = Yap_ReadlineForSIGINT()) == 0)
|
||||||
#endif
|
#endif
|
||||||
{ /* ask for a new line */
|
{ /* ask for a new line */
|
||||||
fprintf(stderr, "Action (h for help): ");
|
fprintf(stderr, "Action (h for help): ");
|
||||||
ch = getc(stdin);
|
ch = getc(stdin);
|
||||||
/* first process up to end of line */
|
/* first process up to end of line */
|
||||||
while ((fgetc(stdin)) != '\n');
|
while ((fgetc(stdin)) != '\n')
|
||||||
}
|
;
|
||||||
|
}
|
||||||
LOCAL_newline = TRUE;
|
LOCAL_newline = TRUE;
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Yap_InitConsole(void) {
|
void Yap_InitConsole(void) {
|
||||||
CACHE_REGS
|
CACHE_REGS
|
||||||
LOCAL_newline = true;
|
LOCAL_newline = true;
|
||||||
Yap_InitCPred ("prompt", 1, prompt1, SafePredFlag|SyncPredFlag);
|
Yap_InitCPred("prompt", 1, prompt1, SafePredFlag | SyncPredFlag);
|
||||||
Yap_InitCPred ("prompt1", 1, prompt1, SafePredFlag|SyncPredFlag);
|
Yap_InitCPred("prompt1", 1, prompt1, SafePredFlag | SyncPredFlag);
|
||||||
Yap_InitCPred ("$is_same_tty", 2, is_same_tty2, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
Yap_InitCPred("$is_same_tty", 2, is_same_tty2,
|
||||||
Yap_InitCPred ("prompt", 2, prompt, SafePredFlag|SyncPredFlag);
|
SafePredFlag | SyncPredFlag | HiddenPredFlag);
|
||||||
Yap_InitCPred ("$ensure_prompting", 0, ensure_prompting, SafePredFlag|SyncPredFlag);
|
Yap_InitCPred("prompt", 2, prompt, SafePredFlag | SyncPredFlag);
|
||||||
|
Yap_InitCPred("$ensure_prompting", 0, ensure_prompting,
|
||||||
|
SafePredFlag | SyncPredFlag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user