QU_initio() user-redefinable #include <quintus/quintus.h>
int QU_initio(user_input, user_output, user_error, act_astty, error_num)
QP_stream **user_input;
QP_stream **user_output;
QP_stream **user_error;
int act_astty;
int *error_num;
Initializes Prolog input/output system. Returns QP_SUCCESS upon
success and QP_ERROR upon failure.
The three Prolog initial stream are created in QU_initio().
The Prolog standard input stream is returned through user_input,
the standard output stream is returned through user_output, and
the standard error stream is returned through user_error.
The created streams are accessed in the Prolog system as
user_input (QP_stdin), user_output (QP_stdout),
and user_error (QP_stderr).
If act_astty is non-zero, the Prolog system requests QU_initio()
to initialize the three initial streams as tty streams even if
they are not really connected to a tty. One example of such a request
is that Prolog is running under remote shell.
The parameter error_num stores the error code if QU_initio()
returns QP_ERROR. The error code can be any of the host operating
system error numbers, QP error numbers or a user-defined error number.
The process required to create these three initial
streams is similar to that of implementing a customized Prolog stream.
(see fli-ios-cps). However, these three initial
streams should not be registered. Calling QP_register_stream() to
register any of the three streams created by QU_initio() may cause an
error when Prolog starts up.
The following is the source code for an
implementation of QU_initio() function in C language.
#include <sys/types.h>
#include <sys/stat.h>
#include <quintus/quintus.h>
#define TTY_BUFSIZ 128
#define MAX_FIFO_BUFSIZ 4096
extern QP_stream *QU_fdopen();
/*
* This I/O initialization function only handles three possible
* types of file, a tty file , a pipe and an ordinary file
*/
int QU_initio(user_input, user_output, user_error, act_astty,
error_num)
QP_stream **user_input, **user_output, **user_error;
int act_astty, *error_num;
{
int fd, is_tty;
struct stat statbuf;
QP_stream option, *streams[3], *prompt_stream;
extern char *ttyname();
for (fd=2; fd >= 0 ; --fd) {
is_tty = isatty(fd);
QU_stream_param((is_tty) ? "/dev/tty" : "",
(fd) ? QP_WRITE : QP_READ, &option);
if (is_tty || act_astty) {
/* make sure other parameters are right */
option.format = QP_DELIM_TTY;
option.max_reclen = TTY_BUFSIZ;
option.seek_type = QP_SEEK_ERROR;
if (fd == 0)
option.peof_act = QP_PASTEOF_RESET;
} else {
if (fstat(fd, &statbuf) < 0)
return QP_ERROR;
if ((statbuf.st_mode & S_IFIFO) == S_IFIFO) {
option.max_reclen = MAX_FIFO_BUFSIZ;
option.seek_type = QP_SEEK_ERROR;
} else
option.max_reclen = statbuf.st_blksize;
}
option.mode = (fd) ? QP_WRITE : QP_READ;
if ((streams[fd]=QU_fdopen(&option,"",error_num,fd))
==QP_NULL_STREAM)
return QP_ERROR;
if (is_tty) {
char *tty_id;
if (! (tty_id = ttyname(fd)) )
tty_id = "/PROLOG DEFAULT TTYS";
(void) QP_add_tty(streams[fd], tty_id);
} else if (act_astty)
(void) QP_add_tty(streams[fd],
"/PROLOG INITAIL STREAMS");
}
(streams[0])->filename="USER$INPUT";
*user_input = streams[0];
(streams[1])->filename="USER$OUTPUT";
*user_output = streams[1];
(streams[2])->filename="USER$ERROR";
*user_error = streams[2];
if ((streams[0])->format == QP_DELIM_TTY
&& (streams[1])->format != QP_DELIM_TTY
&& (streams[2])->format != QP_DELIM_TTY) {
char *tty_id;
/* create an output stream for prompt */
QU_stream_param(isatty(0) ? "/dev/tty" : "", QP_WRITE,
&option);
option.format = QP_DELIM_TTY;
option.max_reclen = TTY_BUFSIZ;
option.seek_type = QP_SEEK_ERROR;
if ((prompt_stream = QU_fdopen(&option, "", error_num,
0)) == QP_NULL_STREAM)
return QP_ERROR;
(void) QP_register_stream(prompt_stream);
if (! (tty_id = ttyname(0)) )
tty_id = "/PROLOG DEFAULT TTYS";
(void) QP_add_tty(prompt_stream, tty_id);
}
return QP_SUCCESS;
}