Loading...
Searching...
No Matches
pipeLink.cc File Reference
`#include "kernel/mod2.h"`
`#include "reporter/si_signals.h"`
`#include "tok.h"`
`#include "ipid.h"`
`#include "subexpr.h"`
`#include "links/silink.h"`
`#include "lists.h"`
`#include "pipeLink.h"`
`#include "Singular/feOpt.h"`
`#include <errno.h>`
`#include <sys/types.h>`

Go to the source code of this file.

## Data Structures

struct | pipeInfo

## Functions

static BOOLEAN | pipeOpen (si_link l, short, leftv)
static BOOLEAN | pipeClose (si_link l)
static BOOLEAN | pipeKill (si_link l)
static leftv | pipeRead1 (si_link l)
static BOOLEAN | pipeWrite (si_link l, leftv data)
static const char * | slStatusPipe (si_link l, const char *request)
si_link_extension | slInitPipeExtension (si_link_extension s)

## Variables

EXTERN_VAR BOOLEAN | FE_OPT_NO_SHELL_FLAG
EXTERN_VAR si_link | pipeLastLink

## Data Structure Documentation

## pipeInfo

struct pipeInfo

Definition at line 26 of file pipeLink.cc.

Data Fields
FILE * | f_read |
FILE * | f_write |
int | fd_read |
int | fd_write |
char | level |
pid_t | pid |

## Function Documentation

## pipeClose()

BOOLEAN pipeClose | ( | si_link | l | ) |

static

Definition at line 95 of file pipeLink.cc.

{
pipeInfo *d = (pipeInfo *)l->data;
if (d!=NULL)
{
BOOLEAN unidirectional=TRUE;
if ( (d->f_read!=NULL) && (d->f_write!=NULL))
unidirectional=FALSE;
if (d->f_read!=NULL)
{
fclose(d->f_read);
d->f_read=NULL;
SI_LINK_SET_CLOSE_P(l);
SI_LINK_SET_R_OPEN_P(l);
}
if (unidirectional && (d->f_write!=NULL))
{
fclose(d->f_write);
d->f_write=NULL;
SI_LINK_SET_CLOSE_P(l);
}
if (unidirectional && (d->pid!=0))
{ kill(d->pid,15); kill(d->pid,9); }
}
else SI_LINK_SET_CLOSE_P(l);
return FALSE;
}
BOOLEAN
int BOOLEAN
Definition auxiliary.h:88
TRUE
#define TRUE
Definition auxiliary.h:101
FALSE
#define FALSE
Definition auxiliary.h:97
l
int l
Definition cfEzgcd.cc:100
NULL
#define NULL
Definition omList.c:12
pipeInfo
Definition pipeLink.cc:27
SI_LINK_SET_R_OPEN_P
#define SI_LINK_SET_R_OPEN_P(l)
Definition silink.h:79
SI_LINK_SET_CLOSE_P
#define SI_LINK_SET_CLOSE_P(l)
Definition silink.h:76

## pipeKill()

BOOLEAN pipeKill | ( | si_link | l | ) |

static

Definition at line 125 of file pipeLink.cc.

{
if(SI_LINK_OPEN_P(l)) pipeClose(l);
pipeInfo *d = (pipeInfo *)l->data;
if (d!=NULL)
{
omFreeSize((ADDRESS)d,(sizeof *d));
}
l->data=NULL;
return FALSE;
}
ADDRESS
void * ADDRESS
Definition auxiliary.h:120
omFreeSize
#define omFreeSize(addr, size)
Definition omAllocDecl.h:260
pipeClose
static BOOLEAN pipeClose(si_link l)
Definition pipeLink.cc:95
SI_LINK_OPEN_P
#define SI_LINK_OPEN_P(l)
Definition silink.h:71

## pipeOpen()

BOOLEAN pipeOpen | ( | si_link | l,
|  | short | ,
|  | leftv | )

static

Definition at line 36 of file pipeLink.cc.

{
if (FE_OPT_NO_SHELL_FLAG) {WerrorS("no links allowed");return TRUE;}
int cpus = (long) feOptValue(FE_OPT_CPUS);
if (cpus<1)
{
WerrorS("no sub-processes allowed");
l->flags=0;
return TRUE;
}
pipeInfo *d=(pipeInfo*)omAlloc0(sizeof(pipeInfo));
int pc[2];
int cp[2];
int err1=pipe(pc);
int err2=pipe(cp);
if (err1 || err2)
{
Werror("pipe failed with %d\n",errno);
omFreeSize(d,sizeof(*d));
l->flags=0;
return TRUE;
}
/* else */
pid_t pid=fork();
if (pid==0) /*child*/
{
/* close unnecessary pipe descriptors for a clean environment */
si_close(pc[1]); si_close(cp[0]);
/* dup pipe read/write to stdin/stdout */
si_dup2( pc[0], STDIN_FILENO );
si_dup2( cp[1], STDOUT_FILENO );
int r=system(l->name);
si_close(pc[0]);
si_close(cp[1]);
_exit(r);
/* never reached*/
}
else if (pid>0)
{
d->pid=pid;
si_close(pc[0]); si_close(cp[1]);
d->f_read=fdopen(cp[0],"r");
d->fd_read=cp[0];
d->f_write=fdopen(pc[1],"w");
d->fd_write=pc[1];
SI_LINK_SET_RW_OPEN_P(l);
}
else
{
Werror("fork failed (%d)",errno);
omFreeSize(d,sizeof(*d));
l->flags=0;
return TRUE;
}
l->data=d;
return FALSE;
}
FE_OPT_NO_SHELL_FLAG
EXTERN_VAR BOOLEAN FE_OPT_NO_SHELL_FLAG
Definition extra.cc:166
WerrorS
void WerrorS(const char *s)
Definition feFopen.cc:24
feOptValue
static void * feOptValue(feOptIndex opt)
Definition feOpt.h:40
STDOUT_FILENO
#define STDOUT_FILENO
Definition feread.cc:43
STDIN_FILENO
#define STDIN_FILENO
Definition fereadl.c:52
omAlloc0
#define omAlloc0(size)
Definition omAllocDecl.h:211
pipeInfo::f_read
FILE * f_read
Definition pipeLink.cc:28
pipeInfo::f_write
FILE * f_write
Definition pipeLink.cc:29
pipeInfo::fd_write
int fd_write
Definition pipeLink.cc:31
pipeInfo::pid
pid_t pid
Definition pipeLink.cc:30
pipeInfo::fd_read
int fd_read
Definition pipeLink.cc:31
Werror
void Werror(const char *fmt,...)
Definition reporter.cc:189
SI_LINK_SET_RW_OPEN_P
#define SI_LINK_SET_RW_OPEN_P(l)
Definition silink.h:80

## pipeRead1()

leftv pipeRead1 | ( | si_link | l | ) |

static

Definition at line 138 of file pipeLink.cc.

{
pipeInfo *d = (pipeInfo *)l->data;
leftv res=(leftv)omAlloc0(sizeof(sleftv));
char *s=(char *)omAlloc0(1024);
char *ss=fgets(s,1024,d->f_read);
if (ss==NULL) { omFreeSize(s,1024); pipeClose(l);return NULL; }
int i=strlen(s)-1;
if ((i>=0) && (s[i]=='\n')) s[i]='\0';
res->rtyp=STRING_CMD;
res->data=s;
return res;
}
i
int i
Definition cfEzgcd.cc:132
sleftv
Class used for (list of) interpreter objects.
Definition subexpr.h:83
s
const CanonicalForm int s
Definition facAbsFact.cc:51
res
CanonicalForm res
Definition facAbsFact.cc:60
leftv
sleftv * leftv
Definition structs.h:53
STRING_CMD
@ STRING_CMD
Definition tok.h:187

## pipeWrite()

BOOLEAN pipeWrite | ( | si_link | l,
|  | leftv | data )

static

Definition at line 153 of file pipeLink.cc.

{
if(!SI_LINK_W_OPEN_P(l)) slOpen(l,SI_LINK_OPEN|SI_LINK_WRITE,NULL);
pipeInfo *d = (pipeInfo *)l->data;
FILE *outfile=d->f_write;;
BOOLEAN err=FALSE;
char *s;
pipeLastLink=l;
while (data!=NULL)
{
s = data->String();
// free data ??
if (s!=NULL)
{
fprintf(outfile,"%s\n",s);
omFree((ADDRESS)s);
}
else
{
WerrorS("cannot convert to string");
err=TRUE;
}
if (pipeLastLink==NULL) return TRUE;
data = data->next;
}
fflush(outfile);
pipeLastLink=NULL;
return err;
}
sleftv::next
leftv next
Definition subexpr.h:86
sleftv::String
char * String(void *d=NULL, BOOLEAN typed=FALSE, int dim=1)
Called for conversion to string (used by string(..), write(..),..).
Definition subexpr.cc:765
pipeLastLink
VAR si_link pipeLastLink
Definition cntrlc.cc:61
omFree
#define omFree(addr)
Definition omAllocDecl.h:261
slOpen
BOOLEAN slOpen(si_link l, short flag, leftv h)
Definition silink.cc:194
SI_LINK_WRITE
#define SI_LINK_WRITE
Definition silink.h:67
SI_LINK_W_OPEN_P
#define SI_LINK_W_OPEN_P(l)
Definition silink.h:72
SI_LINK_OPEN
#define SI_LINK_OPEN
Definition silink.h:65

## slInitPipeExtension()

si_link_extension slInitPipeExtension | ( | si_link_extension | s | ) |

Definition at line 225 of file pipeLink.cc.

{
s->Open=pipeOpen;
s->Close=pipeClose;
s->Kill=pipeKill;
s->Read=pipeRead1;
s->Read2=(slRead2Proc)NULL;
s->Write=pipeWrite;
s->Status=slStatusPipe;
s->type="pipe";
return s;
}
slStatusPipe
static const char * slStatusPipe(si_link l, const char *request)
Definition pipeLink.cc:183
pipeOpen
static BOOLEAN pipeOpen(si_link l, short, leftv)
Definition pipeLink.cc:36
pipeKill
static BOOLEAN pipeKill(si_link l)
Definition pipeLink.cc:125
pipeRead1
static leftv pipeRead1(si_link l)
Definition pipeLink.cc:138
pipeWrite
static BOOLEAN pipeWrite(si_link l, leftv data)
Definition pipeLink.cc:153
slRead2Proc
leftv(* slRead2Proc)(si_link l, leftv a)
Definition silink.h:30

## slStatusPipe()

const char * slStatusPipe | ( | si_link | l,
|  | const char * | request )

static

Definition at line 183 of file pipeLink.cc.

{
pipeInfo *d=(pipeInfo*)l->data;
if (d==NULL) return "not open";
if(strcmp(request, "read") == 0)
{
int s;
if ((!SI_LINK_R_OPEN_P(l)) || (feof(d->f_read))) s=0;
else if (FD_SETSIZE<=d->fd_read)
{
Werror("file descriptor number too high (%d)",d->fd_read);
s=-1;
}
else
{
fd_set mask/*, fdmask*/;
struct timeval wt;
/* Don't block. Return socket status immediately. */
wt.tv_sec = 0;
wt.tv_usec = 0;
FD_ZERO(&mask);
FD_SET(d->fd_read, &mask);
//Print("test fd %d\n",d->fd_read);
/* check with select: chars waiting: no -> not ready */
s=si_select(d->fd_read+1, &mask, NULL, NULL, &wt);
}
switch (s)
{
case 0: /* not ready */ return "not ready";
case -1: /*error*/ return "error";
default: /*1: ready ? */return "ready";
}
}
else if (strcmp(request, "write") == 0)
{
if (SI_LINK_W_OPEN_P(l)) return "ready";
return "not ready";
}
return "unknown status request";
}
SI_LINK_R_OPEN_P
#define SI_LINK_R_OPEN_P(l)
Definition silink.h:73

## Variable Documentation

## FE_OPT_NO_SHELL_FLAG

EXTERN_VAR BOOLEAN FE_OPT_NO_SHELL_FLAG

Definition at line 24 of file pipeLink.cc.

## pipeLastLink

EXTERN_VAR si_link pipeLastLink

Definition at line 152 of file pipeLink.cc.