Data Structures | Functions | Variables
pipeLink.cc File Reference
#include <kernel/mod2.h>
#include <omalloc/omalloc.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 <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/socket.h>

Go to the source code of this file.

Data Structures

struct  pipeInfo
 

Functions

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

Variables

si_link pipeLastLink
 

Data Structure Documentation

◆ pipeInfo

struct pipeInfo

Definition at line 31 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()

LINKAGE BOOLEAN pipeClose ( si_link  l)

Definition at line 87 of file pipeLink.cc.

88 {
89  pipeInfo *d = (pipeInfo *)l->data;
90  if (d!=NULL)
91  {
92  if (d->f_read!=NULL) fclose(d->f_read);
93  if (d->f_write!=NULL) fclose(d->f_write);
94  if (d->pid!=0) { kill(d->pid,15); kill(d->pid,9); }
95  }
97  return FALSE;
98 }
if(0 > strat->sl)
Definition: myNF.cc:73
#define FALSE
Definition: auxiliary.h:94
#define NULL
Definition: omList.c:10
int l
Definition: cfEzgcd.cc:94

◆ pipeKill()

LINKAGE BOOLEAN pipeKill ( si_link  l)

Definition at line 101 of file pipeLink.cc.

102 {
103  if(SI_LINK_OPEN_P(l)) pipeClose(l);
104  pipeInfo *d = (pipeInfo *)l->data;
105  if (d!=NULL)
106  {
107  omFreeSize((ADDRESS)d,(sizeof *d));
108  }
109  l->data=NULL;
110  return FALSE;
111 }
if(0 > strat->sl)
Definition: myNF.cc:73
#define FALSE
Definition: auxiliary.h:94
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
void * ADDRESS
Definition: auxiliary.h:115
#define NULL
Definition: omList.c:10
int l
Definition: cfEzgcd.cc:94

◆ pipeOpen()

BOOLEAN pipeOpen ( si_link  l,
short  flag,
leftv   
)

Definition at line 41 of file pipeLink.cc.

42 {
43  pipeInfo *d=(pipeInfo*)omAlloc0(sizeof(pipeInfo));
44  if (flag & SI_LINK_OPEN)
45  {
47  }
48  int pc[2];
49  int cp[2];
50  pipe(pc);
51  pipe(cp);
52  pid_t pid=fork();
53  if (pid==0) /*child*/
54  {
55  /* close unnecessary pipe descriptors for a clean environment */
56  si_close(pc[1]); si_close(cp[0]);
57  /* dup pipe read/write to stdin/stdout */
58  si_dup2( pc[0], STDIN_FILENO );
59  si_dup2( cp[1], STDOUT_FILENO );
60  int r=system(l->name);
61  si_close(pc[0]);
62  si_close(cp[1]);
63  exit(r);
64  /* never reached*/
65  }
66  else if (pid>0)
67  {
68  d->pid=pid;
69  si_close(pc[0]); si_close(cp[1]);
70  d->f_read=fdopen(cp[0],"r");
71  d->fd_read=cp[0];
72  d->f_write=fdopen(pc[1],"w");
73  d->fd_write=pc[1];
75  }
76  else
77  {
78  Werror("fork failed (%d)",errno);
79  omFreeSize(d,sizeof(*d));
80  return TRUE;
81  }
82  l->data=d;
83  return FALSE;
84 }
#define STDOUT_FILENO
Definition: feread.cc:45
#define FALSE
Definition: auxiliary.h:94
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define STDIN_FILENO
Definition: fereadl.c:54
#define TRUE
Definition: auxiliary.h:98
const ring r
Definition: syzextra.cc:208
void system(sys)
void Werror(const char *fmt,...)
Definition: reporter.cc:189
#define omAlloc0(size)
Definition: omAllocDecl.h:211
int l
Definition: cfEzgcd.cc:94

◆ pipeRead1()

LINKAGE leftv pipeRead1 ( si_link  l)

Definition at line 114 of file pipeLink.cc.

115 {
116  pipeInfo *d = (pipeInfo *)l->data;
117  leftv res=(leftv)omAlloc0(sizeof(sleftv));
118  char *s=(char *)omAlloc0(1024);
119  char *ss=fgets(s,1024,d->f_read);
120  if (ss==NULL) { omFreeSize(s,1024); pipeClose(l);return NULL; }
121  int i=strlen(s)-1;
122  if ((i>=0) && (s[i]=='\n')) s[i]='\0';
123  res->rtyp=STRING_CMD;
124  res->data=s;
125  return res;
126 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
poly res
Definition: myNF.cc:322
int i
Definition: cfEzgcd.cc:123
#define NULL
Definition: omList.c:10
#define omAlloc0(size)
Definition: omAllocDecl.h:211
int l
Definition: cfEzgcd.cc:94

◆ pipeWrite()

LINKAGE BOOLEAN pipeWrite ( si_link  l,
leftv  data 
)

Definition at line 129 of file pipeLink.cc.

130 {
132  pipeInfo *d = (pipeInfo *)l->data;
133  FILE *outfile=d->f_write;;
134  BOOLEAN err=FALSE;
135  char *s;
136  pipeLastLink=l;
137  while (data!=NULL)
138  {
139  s = data->String();
140  // free data ??
141  if (s!=NULL)
142  {
143  fprintf(outfile,"%s\n",s);
144  omFree((ADDRESS)s);
145  }
146  else
147  {
148  WerrorS("cannot convert to string");
149  err=TRUE;
150  }
151  if (pipeLastLink==NULL) return TRUE;
152  data = data->next;
153  }
154  fflush(outfile);
156  return err;
157 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
#define FALSE
Definition: auxiliary.h:94
#define TRUE
Definition: auxiliary.h:98
void * ADDRESS
Definition: auxiliary.h:115
void WerrorS(const char *s)
Definition: feFopen.cc:24
char * String(void *d=NULL, BOOLEAN typed=FALSE, int dim=1)
Called for conversion to string (used by string(..), write(..),..)
Definition: subexpr.cc:751
#define omFree(addr)
Definition: omAllocDecl.h:261
while(1)
Definition: libparse.cc:1442
leftv next
Definition: subexpr.h:87
#define NULL
Definition: omList.c:10
int BOOLEAN
Definition: auxiliary.h:85
int l
Definition: cfEzgcd.cc:94

◆ slInitPipeExtension()

si_link_extension slInitPipeExtension ( si_link_extension  s)

Definition at line 196 of file pipeLink.cc.

197 {
198  s->Open=pipeOpen;
199  s->Close=pipeClose;
200  s->Kill=pipeKill;
201  s->Read=pipeRead1;
202  s->Read2=(slRead2Proc)NULL;
203  s->Write=pipeWrite;
204 
205  s->Status=slStatusPipe;
206  s->type="pipe";
207  return s;
208 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
#define NULL
Definition: omList.c:10

◆ slStatusPipe()

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

Definition at line 159 of file pipeLink.cc.

160 {
161  pipeInfo *d=(pipeInfo*)l->data;
162  if (d==NULL) return "not open";
163  if(strcmp(request, "read") == 0)
164  {
165  int s;
166  if ((!SI_LINK_R_OPEN_P(l)) || (feof(d->f_read))) s=0;
167  else
168  {
169  fd_set mask/*, fdmask*/;
170  struct timeval wt;
171  /* Don't block. Return socket status immediately. */
172  wt.tv_sec = 0;
173  wt.tv_usec = 0;
174 
175  FD_ZERO(&mask);
176  FD_SET(d->fd_read, &mask);
177  //Print("test fd %d\n",d->fd_read);
178  /* check with select: chars waiting: no -> not ready */
179  s=si_select(d->fd_read+1, &mask, NULL, NULL, &wt);
180  }
181  switch (s)
182  {
183  case 0: /* not ready */ return "not ready";
184  case -1: /*error*/ return "error";
185  default: /*1: ready ? */return "ready";
186  }
187  }
188  else if (strcmp(request, "write") == 0)
189  {
190  if (SI_LINK_W_OPEN_P(l)) return "ready";
191  return "not ready";
192  }
193  return "unknown status request";
194 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
if(0 > strat->sl)
Definition: myNF.cc:73
#define NULL
Definition: omList.c:10
int l
Definition: cfEzgcd.cc:94

Variable Documentation

◆ pipeLastLink

si_link pipeLastLink

Definition at line 70 of file cntrlc.cc.