My Project
Loading...
Searching...
No Matches
Data Structures | Functions | Variables
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 <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 flag, 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 si_link pipeLastLink
 

Data Structure Documentation

◆ pipeInfo

struct pipeInfo

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

static BOOLEAN pipeClose ( si_link  l)
static

Definition at line 79 of file pipeLink.cc.

80{
81 pipeInfo *d = (pipeInfo *)l->data;
82 if (d!=NULL)
83 {
84 BOOLEAN unidirectional=TRUE;
85 if ( (d->f_read!=NULL) && (d->f_write!=NULL))
86 unidirectional=FALSE;
87
88 if (d->f_read!=NULL)
89 {
90 fclose(d->f_read);
91 d->f_read=NULL;
94 }
95 if (unidirectional && (d->f_write!=NULL))
96 {
97 fclose(d->f_write);
98 d->f_write=NULL;
100 }
101 if (unidirectional && (d->pid!=0))
102 { kill(d->pid,15); kill(d->pid,9); }
103 }
105 return FALSE;
106}
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
int l
Definition: cfEzgcd.cc:100
if(!FE_OPT_NO_SHELL_FLAG)(void) system(sys)
#define NULL
Definition: omList.c:12

◆ pipeKill()

static BOOLEAN pipeKill ( si_link  l)
static

Definition at line 109 of file pipeLink.cc.

110{
112 pipeInfo *d = (pipeInfo *)l->data;
113 if (d!=NULL)
114 {
115 omFreeSize((ADDRESS)d,(sizeof *d));
116 }
117 l->data=NULL;
118 return FALSE;
119}
void * ADDRESS
Definition: auxiliary.h:119
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260

◆ pipeOpen()

static BOOLEAN pipeOpen ( si_link  l,
short  flag,
leftv   
)
static

Definition at line 33 of file pipeLink.cc.

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

◆ pipeRead1()

static leftv pipeRead1 ( si_link  l)
static

Definition at line 122 of file pipeLink.cc.

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

◆ pipeWrite()

static BOOLEAN pipeWrite ( si_link  l,
leftv  data 
)
static

Definition at line 137 of file pipeLink.cc.

138{
140 pipeInfo *d = (pipeInfo *)l->data;
141 FILE *outfile=d->f_write;;
142 BOOLEAN err=FALSE;
143 char *s;
145 while (data!=NULL)
146 {
147 s = data->String();
148 // free data ??
149 if (s!=NULL)
150 {
151 fprintf(outfile,"%s\n",s);
152 omFree((ADDRESS)s);
153 }
154 else
155 {
156 WerrorS("cannot convert to string");
157 err=TRUE;
158 }
159 if (pipeLastLink==NULL) return TRUE;
160 data = data->next;
161 }
162 fflush(outfile);
164 return err;
165}
leftv next
Definition: subexpr.h:86
char * String(void *d=NULL, BOOLEAN typed=FALSE, int dim=1)
Called for conversion to string (used by string(..), write(..),..)
Definition: subexpr.cc:761
while(B< b)
Definition: facBivar.cc:54
void WerrorS(const char *s)
Definition: feFopen.cc:24
#define omFree(addr)
Definition: omAllocDecl.h:261

◆ slInitPipeExtension()

si_link_extension slInitPipeExtension ( si_link_extension  s)

Definition at line 204 of file pipeLink.cc.

205{
206 s->Open=pipeOpen;
207 s->Close=pipeClose;
208 s->Kill=pipeKill;
209 s->Read=pipeRead1;
210 s->Read2=(slRead2Proc)NULL;
211 s->Write=pipeWrite;
212
213 s->Status=slStatusPipe;
214 s->type="pipe";
215 return s;
216}

◆ slStatusPipe()

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

Definition at line 167 of file pipeLink.cc.

168{
169 pipeInfo *d=(pipeInfo*)l->data;
170 if (d==NULL) return "not open";
171 if(strcmp(request, "read") == 0)
172 {
173 int s;
174 if ((!SI_LINK_R_OPEN_P(l)) || (feof(d->f_read))) s=0;
175 else
176 {
177 fd_set mask/*, fdmask*/;
178 struct timeval wt;
179 /* Don't block. Return socket status immediately. */
180 wt.tv_sec = 0;
181 wt.tv_usec = 0;
182
183 FD_ZERO(&mask);
184 FD_SET(d->fd_read, &mask);
185 //Print("test fd %d\n",d->fd_read);
186 /* check with select: chars waiting: no -> not ready */
187 s=si_select(d->fd_read+1, &mask, NULL, NULL, &wt);
188 }
189 switch (s)
190 {
191 case 0: /* not ready */ return "not ready";
192 case -1: /*error*/ return "error";
193 default: /*1: ready ? */return "ready";
194 }
195 }
196 else if (strcmp(request, "write") == 0)
197 {
198 if (SI_LINK_W_OPEN_P(l)) return "ready";
199 return "not ready";
200 }
201 return "unknown status request";
202}

Variable Documentation

◆ pipeLastLink

EXTERN_VAR si_link pipeLastLink

Definition at line 136 of file pipeLink.cc.