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 charslStatusPipe (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 86 of file pipeLink.cc.

87{
88 pipeInfo *d = (pipeInfo *)l->data;
89 if (d!=NULL)
90 {
92 if ( (d->f_read!=NULL) && (d->f_write!=NULL))
94
95 if (d->f_read!=NULL)
96 {
97 fclose(d->f_read);
98 d->f_read=NULL;
101 }
102 if (unidirectional && (d->f_write!=NULL))
103 {
104 fclose(d->f_write);
105 d->f_write=NULL;
107 }
108 if (unidirectional && (d->pid!=0))
109 { kill(d->pid,15); kill(d->pid,9); }
110 }
112 return FALSE;
113}
int BOOLEAN
Definition auxiliary.h:87
#define TRUE
Definition auxiliary.h:100
#define FALSE
Definition auxiliary.h:96
int l
Definition cfEzgcd.cc:100
#define NULL
Definition omList.c:12

◆ pipeKill()

static BOOLEAN pipeKill ( si_link  l)
static

Definition at line 116 of file pipeLink.cc.

117{
119 pipeInfo *d = (pipeInfo *)l->data;
120 if (d!=NULL)
121 {
122 omFreeSize((ADDRESS)d,(sizeof *d));
123 }
124 l->data=NULL;
125 return FALSE;
126}
#define omFreeSize(addr, size)

◆ 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 int err1=pipe(pc);
43 int err2=pipe(cp);
44 if (err1 || err2)
45 {
46 Werror("pipe failed with %d\n",errno);
47 omFreeSize(d,sizeof(*d));
48 return TRUE;
49 }
50 /* else */
51 pid_t pid=fork();
52 if (pid==0) /*child*/
53 {
54 /* close unnecessary pipe descriptors for a clean environment */
55 si_close(pc[1]); si_close(cp[0]);
56 /* dup pipe read/write to stdin/stdout */
57 si_dup2( pc[0], STDIN_FILENO );
58 si_dup2( cp[1], STDOUT_FILENO );
59 int r=system(l->name);
60 si_close(pc[0]);
61 si_close(cp[1]);
62 exit(r);
63 /* never reached*/
64 }
65 else if (pid>0)
66 {
67 d->pid=pid;
68 si_close(pc[0]); si_close(cp[1]);
69 d->f_read=fdopen(cp[0],"r");
70 d->fd_read=cp[0];
71 d->f_write=fdopen(pc[1],"w");
72 d->fd_write=pc[1];
74 }
75 else
76 {
77 Werror("fork failed (%d)",errno);
78 omFreeSize(d,sizeof(*d));
79 return TRUE;
80 }
81 l->data=d;
82 return FALSE;
83}
#define STDOUT_FILENO
Definition feread.cc:43
#define STDIN_FILENO
Definition fereadl.c:52
#define omAlloc0(size)
void Werror(const char *fmt,...)
Definition reporter.cc:189

◆ pipeRead1()

static leftv pipeRead1 ( si_link  l)
static

Definition at line 129 of file pipeLink.cc.

130{
131 pipeInfo *d = (pipeInfo *)l->data;
132 leftv res=(leftv)omAlloc0(sizeof(sleftv));
133 char *s=(char *)omAlloc0(1024);
134 char *ss=fgets(s,1024,d->f_read);
135 if (ss==NULL) { omFreeSize(s,1024); pipeClose(l);return NULL; }
136 int i=strlen(s)-1;
137 if ((i>=0) && (s[i]=='\n')) s[i]='\0';
138 res->rtyp=STRING_CMD;
139 res->data=s;
140 return res;
141}
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
sleftv * leftv
Definition structs.h:57
@ STRING_CMD
Definition tok.h:187

◆ pipeWrite()

static BOOLEAN pipeWrite ( si_link  l,
leftv  data 
)
static

Definition at line 144 of file pipeLink.cc.

145{
147 pipeInfo *d = (pipeInfo *)l->data;
148 FILE *outfile=d->f_write;;
149 BOOLEAN err=FALSE;
150 char *s;
152 while (data!=NULL)
153 {
154 s = data->String();
155 // free data ??
156 if (s!=NULL)
157 {
158 fprintf(outfile,"%s\n",s);
159 omFree((ADDRESS)s);
160 }
161 else
162 {
163 WerrorS("cannot convert to string");
164 err=TRUE;
165 }
166 if (pipeLastLink==NULL) return TRUE;
167 data = data->next;
168 }
171 return err;
172}
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:766
void WerrorS(const char *s)
Definition feFopen.cc:24
#define omFree(addr)

◆ slInitPipeExtension()

si_link_extension slInitPipeExtension ( si_link_extension  s)

Definition at line 216 of file pipeLink.cc.

217{
218 s->Open=pipeOpen;
219 s->Close=pipeClose;
220 s->Kill=pipeKill;
221 s->Read=pipeRead1;
222 s->Read2=(slRead2Proc)NULL;
223 s->Write=pipeWrite;
224
225 s->Status=slStatusPipe;
226 s->type="pipe";
227 return s;
228}

◆ slStatusPipe()

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

Definition at line 174 of file pipeLink.cc.

175{
176 pipeInfo *d=(pipeInfo*)l->data;
177 if (d==NULL) return "not open";
178 if(strcmp(request, "read") == 0)
179 {
180 int s;
181 if ((!SI_LINK_R_OPEN_P(l)) || (feof(d->f_read))) s=0;
182 else if (FD_SETSIZE<=d->fd_read)
183 {
184 Werror("file descriptor number too high (%d)",d->fd_read);
185 s=-1;
186 }
187 else
188 {
189 fd_set mask/*, fdmask*/;
190 struct timeval wt;
191 /* Don't block. Return socket status immediately. */
192 wt.tv_sec = 0;
193 wt.tv_usec = 0;
194
195 FD_ZERO(&mask);
196 FD_SET(d->fd_read, &mask);
197 //Print("test fd %d\n",d->fd_read);
198 /* check with select: chars waiting: no -> not ready */
199 s=si_select(d->fd_read+1, &mask, NULL, NULL, &wt);
200 }
201 switch (s)
202 {
203 case 0: /* not ready */ return "not ready";
204 case -1: /*error*/ return "error";
205 default: /*1: ready ? */return "ready";
206 }
207 }
208 else if (strcmp(request, "write") == 0)
209 {
210 if (SI_LINK_W_OPEN_P(l)) return "ready";
211 return "not ready";
212 }
213 return "unknown status request";
214}

Variable Documentation

◆ pipeLastLink

EXTERN_VAR si_link pipeLastLink

Definition at line 143 of file pipeLink.cc.