This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/os/alias.c

452 lines
12 KiB
C
Raw Permalink Normal View History

2015-06-18 00:58:51 +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 *
* *
**************************************************************************
* *
2015-06-19 01:30:13 +01:00
* File: alias.c *
2015-06-18 00:58:51 +01:00
* Last rev: 5/2/88 *
* mods: *
* comments: Input/Output C implemented predicates *
* *
*************************************************************************/
#ifdef SCCS
static char SccsId[] = "%W% %G%";
#endif
2015-12-15 09:07:36 +00:00
/**
* @file alias.c
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
* @date Thu Nov 19 10:53:20 2015
2016-03-29 01:56:00 +01:00
*
2015-12-15 09:07:36 +00:00
* @brief File Aliases
2016-03-29 01:56:00 +01:00
*
*
2015-12-15 09:07:36 +00:00
*/
2016-03-29 01:56:00 +01:00
/**
2017-04-07 23:10:59 +01:00
* @defgroup Aliases Aliases to Stream Names
2016-01-20 22:18:17 +00:00
* @ingroup InputOutput
2015-11-11 08:45:03 +00:00
*
* Aliases:
2015-06-18 00:58:51 +01:00
* This file defines the main operations on aliases, a second name for a file. Aliases are always
2016-03-29 01:56:00 +01:00
* textual constants (atoms).
2015-06-18 00:58:51 +01:00
*
* Their first advantage is that they allow cleaning up code, by separating name from operation, eg
2016-03-29 01:56:00 +01:00
* YAP has a loop stream used to run the main top-level, which can be std0 originally but then
2015-06-18 00:58:51 +01:00
* changed to a pipe, a file, or a memory region. Other important streams are the user streams. Finally,
* the debugger uses debugger input and output.
*
* Predefined stream aliases are:
* + user: special alias, initially refers to all the three standard streams.
* + `user_input: initially refers to the stdandard input stream;
* + `user_output: initially refers to the stdandard output stream;
* + `user_error: initially refers to the stdandard error stream. Often this is the same device
* as `stderr`, just accessed in different ways.
* + loop_stream: refers to the stream for the file or object being current consulted
* + debugger_input: refers to the stream used to send debugger commands, by default `user_input`.
2018-11-23 00:01:55 +00:00
* + debugger_output: refers to the stream used to output debugging, by default `user_error`.
2015-06-18 00:58:51 +01:00
* It must always be interactive.
*/
2016-06-03 17:05:18 +01:00
#include "sysbits.h"
2015-06-18 00:58:51 +01:00
#if HAVE_FCNTL_H
/* for O_BINARY and O_TEXT in WIN32 */
#include <fcntl.h>
#endif
#include "Yatom.h"
#include "YapHeap.h"
#include "yapio.h"
2017-02-20 14:21:46 +00:00
#include "YapEval.h"
2015-06-18 00:58:51 +01:00
#include "YapText.h"
#include <stdlib.h>
#if HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_CTYPE_H
#include <ctype.h>
#endif
#if HAVE_WCTYPE_H
#include <wctype.h>
#endif
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#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
2016-03-29 01:56:00 +01:00
#if HAVE_SYS_SELECT_H && !_MSC_VER && !defined(__MINGW32__)
2015-06-18 00:58:51 +01:00
#include <sys/select.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef _WIN32
2016-06-03 17:05:18 +01:00
// WIN32 API support
2015-06-18 00:58:51 +01:00
#if HAVE_IO_H
/* Windows */
#include <io.h>
#endif
#endif
2016-06-03 17:05:18 +01:00
2016-03-29 01:56:00 +01:00
#if _MSC_VER || defined(__MINGW32__)
2015-06-18 00:58:51 +01:00
#if HAVE_SOCKET
#include <winsock2.h>
#endif
#include <windows.h>
#endif
#include "iopreds.h"
2016-03-29 01:56:00 +01:00
#if _MSC_VER || defined(__MINGW32__)
2015-06-18 00:58:51 +01:00
#define SYSTEM_STAT _stat
#else
#define SYSTEM_STAT stat
#endif
static Atom FetchAlias (int sno);
static bool ExistsAliasForStream (int sno, Atom al);
2016-03-29 01:56:00 +01:00
/**
2015-11-11 08:45:03 +00:00
* Specify an alias to the stream. The alias <tt>Name</tt> must be an atom. The
* alias can be used instead of the stream descriptor for every operation
* concerning the stream.
2016-03-29 01:56:00 +01:00
*
* @param + _tname_ Name of Alias
2015-11-11 08:45:03 +00:00
* @param + _tstream_ stream identifier
2016-03-29 01:56:00 +01:00
*
* @return
2015-11-11 08:45:03 +00:00
*/
2015-06-18 00:58:51 +01:00
static Int add_alias_to_stream (USES_REGS1)
{
Term tname = Deref(ARG1);
Term tstream = Deref(ARG2);
Atom at;
Int sno;
if (IsVarTerm(tname)) {
Yap_Error(INSTANTIATION_ERROR, tname, "$add_alias_to_stream");
return (FALSE);
} else if (!IsAtomTerm (tname)) {
Yap_Error(TYPE_ERROR_ATOM, tname, "$add_alias_to_stream");
return (FALSE);
}
if (IsVarTerm(tstream)) {
Yap_Error(INSTANTIATION_ERROR, tstream, "$add_alias_to_stream");
return (FALSE);
} else if (!IsApplTerm (tstream) || FunctorOfTerm (tstream) != FunctorStream ||
!IsIntTerm(ArgOfTerm(1,tstream))) {
Yap_Error(DOMAIN_ERROR_STREAM_OR_ALIAS, tstream, "$add_alias_to_stream");
return (FALSE);
}
at = AtomOfTerm(tname);
sno = (int)IntOfTerm(ArgOfTerm(1,tstream));
if (Yap_AddAlias(at, sno))
return(TRUE);
/* we could not create the alias, time to close the stream */
Yap_CloseStream(sno);
Yap_Error(PERMISSION_ERROR_NEW_ALIAS_FOR_STREAM, tname, "open/3");
return (FALSE);
}
static Int check_if_valid_new_alias (USES_REGS1)
{
Term tname = Deref(ARG1);
Atom at;
if (IsVarTerm(tname)) {
Yap_Error(INSTANTIATION_ERROR, tname, "$add_alias_to_stream");
return (FALSE);
} else if (!IsAtomTerm (tname)) {
Yap_Error(TYPE_ERROR_ATOM, tname, "$add_alias_to_stream");
return (FALSE);
}
at = AtomOfTerm(tname);
return(Yap_CheckAlias(at) == -1);
}
bool
Yap_FetchStreamAlias (int sno, Term t2 USES_REGS)
2016-03-29 01:56:00 +01:00
{
2015-06-18 00:58:51 +01:00
if (IsVarTerm(t2)) {
Atom at = FetchAlias(sno);
if (at == NULL)
return false;
else {
return Yap_unify_constant(t2, MkAtomTerm(at));
}
} else if (IsAtomTerm(t2)) {
Atom at = AtomOfTerm(t2);
return ExistsAliasForStream(sno,at);
} else {
Yap_Error(TYPE_ERROR_ATOM, t2, "stream_property(_,alias( ))");
return false;
}
}
static void
ExtendAliasArray(void)
{
CACHE_REGS
AliasDesc new;
2016-03-29 01:56:00 +01:00
UInt new_size = GLOBAL_SzOfFileAliases+ALIASES_BLOCK_SIZE;
2015-06-18 00:58:51 +01:00
new = (AliasDesc)Yap_AllocCodeSpace(sizeof(AliasDesc *)*new_size);
2018-06-30 14:33:32 +01:00
memmove((void *)new, (void *)GLOBAL_FileAliases, sizeof(AliasDesc *)*GLOBAL_SzOfFileAliases);
2016-03-29 01:56:00 +01:00
Yap_FreeCodeSpace((ADDR) GLOBAL_FileAliases);
GLOBAL_FileAliases = new;
GLOBAL_SzOfFileAliases = new_size;
2015-06-18 00:58:51 +01:00
}
void
Yap_SetAlias (Atom arg, int sno)
{
CACHE_REGS
2016-03-29 01:56:00 +01:00
AliasDesc aliasp = GLOBAL_FileAliases, aliasp_max = GLOBAL_FileAliases+GLOBAL_NOfFileAliases;
2015-06-18 00:58:51 +01:00
2018-03-02 21:18:24 +00:00
if (arg == AtomUserIn)
LOCAL_c_input_stream = sno;
if (arg == AtomUserOut)
LOCAL_c_output_stream = sno;
if (arg == AtomUserErr)
LOCAL_c_error_stream = sno;
2018-07-06 16:51:19 +01:00
while (aliasp < aliasp_max) {
// replace alias
if (aliasp->name == arg) {
aliasp->alias_stream = sno;
2015-06-18 00:58:51 +01:00
return;
}
aliasp++;
}
// set new alias
/* we have not found an alias, create one */
2016-03-29 01:56:00 +01:00
if (aliasp == GLOBAL_FileAliases+ GLOBAL_SzOfFileAliases)
2015-06-18 00:58:51 +01:00
ExtendAliasArray();
2016-03-29 01:56:00 +01:00
GLOBAL_NOfFileAliases++;
2015-06-18 00:58:51 +01:00
aliasp->name = arg;
aliasp->alias_stream = sno;
}
/* purge all aliases for stream sno */
void
Yap_DeleteAliases (int sno)
{
CACHE_REGS
2016-03-29 01:56:00 +01:00
AliasDesc aliasp = GLOBAL_FileAliases, aliasp_max = GLOBAL_FileAliases+ GLOBAL_NOfFileAliases, new_aliasp = aliasp;
2015-06-18 00:58:51 +01:00
while (aliasp < aliasp_max) {
if (aliasp->alias_stream == sno) {
2016-03-29 01:56:00 +01:00
if (aliasp - GLOBAL_FileAliases < 3) {
2015-06-18 00:58:51 +01:00
/* get back to std streams, but keep alias around */
2016-03-29 01:56:00 +01:00
Int alno = aliasp-GLOBAL_FileAliases;
2015-06-18 00:58:51 +01:00
new_aliasp->alias_stream = alno;
new_aliasp++;
2015-07-23 01:33:30 +01:00
} else {
2016-03-29 01:56:00 +01:00
GLOBAL_NOfFileAliases--;
// printf("RM %p at %d/%d %d\n", new_aliasp->name, new_aliasp-GLOBAL_FileAliases, new_aliasp->alias_stream, sno);
2015-06-18 00:58:51 +01:00
}
} else {
/* avoid holes in alias array */
if (new_aliasp != aliasp) {
new_aliasp->alias_stream = aliasp->alias_stream;
new_aliasp->name = aliasp->name;
}
new_aliasp++;
}
aliasp++;
2015-07-23 01:33:30 +01:00
}/////
2015-06-18 00:58:51 +01:00
}
/* check if name is an alias */
int
Yap_CheckAlias (Atom arg)
{
CACHE_REGS
2016-03-29 01:56:00 +01:00
AliasDesc aliasp = GLOBAL_FileAliases, aliasp_max = GLOBAL_FileAliases+GLOBAL_NOfFileAliases;
2015-06-18 00:58:51 +01:00
while (aliasp < aliasp_max) {
if (aliasp->name == arg) {
return(aliasp->alias_stream);
}
aliasp++;
}
return(-1);
}
/* check if stream has an alias */
static Atom
FetchAlias (int sno)
{
CACHE_REGS
2016-03-29 01:56:00 +01:00
AliasDesc aliasp = GLOBAL_FileAliases, aliasp_max = GLOBAL_FileAliases+GLOBAL_NOfFileAliases;
2015-06-18 00:58:51 +01:00
while (aliasp < aliasp_max) {
if (aliasp->alias_stream == sno) {
return(aliasp->name);
}
aliasp++;
}
2016-03-05 00:08:04 +00:00
return NULL;
2015-06-18 00:58:51 +01:00
}
/* check if arg is an alias */
static bool
ExistsAliasForStream (int sno, Atom al)
{
CACHE_REGS
2016-03-29 01:56:00 +01:00
AliasDesc aliasp = GLOBAL_FileAliases, aliasp_max = GLOBAL_FileAliases+GLOBAL_NOfFileAliases;
2015-06-18 00:58:51 +01:00
while (aliasp < aliasp_max) {
if (aliasp->alias_stream == sno && aliasp->name == al) {
2018-03-02 21:18:24 +00:00
if (al == AtomUserIn) {
2018-07-06 16:51:19 +01:00
LOCAL_c_input_stream = sno;
aliasp->alias_stream = sno;
} else
2018-03-02 21:18:24 +00:00
if (al == AtomUserOut) {
2018-07-06 16:51:19 +01:00
LOCAL_c_output_stream = sno;
aliasp->alias_stream = sno;
2018-03-02 21:18:24 +00:00
}
if (al == AtomUserErr) {
2018-07-06 16:51:19 +01:00
LOCAL_c_error_stream = sno;
aliasp->alias_stream = sno;
2018-03-02 21:18:24 +00:00
}
2015-06-18 00:58:51 +01:00
return true;
}
aliasp++;
}
return false;
}
/* check if arg is an alias */
bool
Yap_FindStreamForAlias (Atom al)
{
CACHE_REGS
2016-03-29 01:56:00 +01:00
AliasDesc aliasp = GLOBAL_FileAliases,
aliasp_max = GLOBAL_FileAliases+GLOBAL_NOfFileAliases;
2015-06-18 00:58:51 +01:00
while (aliasp < aliasp_max) {
if (aliasp->name == al) {
2019-02-27 11:04:32 +00:00
return aliasp->alias_stream > 0;
2015-06-18 00:58:51 +01:00
}
aliasp++;
}
2019-02-27 11:04:32 +00:00
LOCAL_Error_TYPE = DOMAIN_ERROR_STREAM;
return false;
2015-06-18 00:58:51 +01:00
}
/* create a new alias arg for stream sno */
int
Yap_RemoveAlias (Atom arg, int sno)
{
CACHE_REGS
2016-03-29 01:56:00 +01:00
AliasDesc aliasp = GLOBAL_FileAliases, aliasp_max = GLOBAL_FileAliases+GLOBAL_NOfFileAliases;
2015-06-18 00:58:51 +01:00
while (aliasp < aliasp_max) {
if (aliasp->name == arg) {
if (aliasp->alias_stream != sno) {
return(FALSE);
}
return(TRUE);
}
aliasp++;
}
2016-03-29 01:56:00 +01:00
//printf("RM %p at %d\n", arg, aliasp-GLOBAL_FileAliases);
2015-06-18 00:58:51 +01:00
/* we have not found an alias neither a hole */
2016-03-29 01:56:00 +01:00
if (aliasp == GLOBAL_FileAliases+GLOBAL_SzOfFileAliases)
2015-06-18 00:58:51 +01:00
ExtendAliasArray();
2016-03-29 01:56:00 +01:00
GLOBAL_NOfFileAliases--;
2015-06-18 00:58:51 +01:00
aliasp->name = arg;
aliasp->alias_stream = sno;
return(TRUE);
}
/* create a new alias arg for stream sno */
bool
Yap_AddAlias (Atom arg, int sno)
{
CACHE_REGS
2016-03-29 01:56:00 +01:00
AliasDesc aliasp = GLOBAL_FileAliases, aliasp_max = GLOBAL_FileAliases+GLOBAL_NOfFileAliases;
2015-06-18 00:58:51 +01:00
2018-07-06 16:51:19 +01:00
if (arg == AtomUserIn)
LOCAL_c_input_stream = sno;
else if (arg == AtomUserOut)
LOCAL_c_output_stream = sno;
else if (arg == AtomUserErr)
LOCAL_c_error_stream = sno;
2015-06-18 00:58:51 +01:00
while (aliasp < aliasp_max) {
if (aliasp->name == arg) {
aliasp->alias_stream = sno;
return true;
}
aliasp++;
}
2018-07-06 16:51:19 +01:00
2015-06-18 00:58:51 +01:00
/* we have not found an alias neither a hole */
2016-03-29 01:56:00 +01:00
if (aliasp == GLOBAL_FileAliases+GLOBAL_SzOfFileAliases)
2015-06-18 00:58:51 +01:00
ExtendAliasArray();
2016-03-29 01:56:00 +01:00
GLOBAL_NOfFileAliases++;
// printf("ADD %p at %d\n", arg, aliasp-GLOBAL_FileAliases);
2015-06-18 00:58:51 +01:00
aliasp->name = arg;
aliasp->alias_stream = sno;
return true;
}
/* create a new alias arg for stream sno */
struct AliasDescS *
Yap_InitStandardAliases(void)
{
2015-06-19 01:30:13 +01:00
CACHE_REGS
2015-06-18 00:58:51 +01:00
/* init standard aliases */
/* alloca alias array */
2016-03-29 01:56:00 +01:00
GLOBAL_FileAliases = (AliasDesc)Yap_AllocCodeSpace(sizeof(struct AliasDescS)*ALIASES_BLOCK_SIZE);
2015-06-18 00:58:51 +01:00
2016-03-29 01:56:00 +01:00
if (GLOBAL_FileAliases == NULL)
2015-06-18 00:58:51 +01:00
return NULL;
2016-03-29 01:56:00 +01:00
GLOBAL_FileAliases[0].name = AtomUserIn;
GLOBAL_FileAliases[0].alias_stream = 0;
GLOBAL_FileAliases[1].name = AtomUserOut;
GLOBAL_FileAliases[1].alias_stream = 1;
GLOBAL_FileAliases[2].name = AtomUserErr;
GLOBAL_FileAliases[2].alias_stream = 2;
GLOBAL_FileAliases[3].name = AtomLoopStream;
GLOBAL_FileAliases[3].alias_stream = 0;
GLOBAL_FileAliases[4].name = AtomDebuggerInput;
GLOBAL_FileAliases[4].alias_stream = 0;
GLOBAL_NOfFileAliases = 5;
GLOBAL_SzOfFileAliases = ALIASES_BLOCK_SIZE;
return GLOBAL_FileAliases;
2015-06-18 00:58:51 +01:00
}
/* create a new alias arg for stream sno */
void
Yap_InitAliases(void)
{
Yap_InitCPred ("$check_if_valid_new_alias", 1, check_if_valid_new_alias, TestPredFlag|SafePredFlag|SyncPredFlag|HiddenPredFlag);
Yap_InitCPred ("$add_alias_to_stream", 2, add_alias_to_stream, SafePredFlag|SyncPredFlag|HiddenPredFlag);
}