11 static const unsigned int StringSize = 4096;
15 static void my_close(int fd);
16 static void my_dup2(int old_fd, int new_fd) ;
22 void pipex_open(pipex_t *pipex, const char *name, const char *command){
23 char string[StringSize];
27 int from_child[2], to_child[2];
33 pipex->io->in_fd = STDIN_FILENO;
34 pipex->io->out_fd = STDOUT_FILENO;
36 // attach standard error to standard output
38 my_dup2(STDOUT_FILENO,STDERR_FILENO);
42 // parse the command line and create the argument list
44 if (strlen(command) >= StringSize) my_fatal("pipex_open(): buffer overflow\n");
45 strcpy(string,command);
48 for (ptr = strtok(string," "); ptr != NULL; ptr = strtok(NULL," ")) {
56 if (pipe(from_child) == -1) {
57 my_fatal("pipex_open(): pipe(): %s\n",strerror(errno));
60 if (pipe(to_child) == -1) {
61 my_fatal("pipex_open(): pipe(): %s\n",strerror(errno));
64 // create the child process
68 if (pipex->pid == -1) {
70 my_fatal("pipex_open(): fork(): %s\n",strerror(errno));
72 } else if (pipex->pid == 0) {
76 // close unused pipe descriptors to avoid deadlocks
78 my_close(from_child[0]);
79 my_close(to_child[1]);
81 // attach the pipe to standard input
83 my_dup2(to_child[0],STDIN_FILENO);
84 my_close(to_child[0]);
86 // attach the pipe to standard output
88 my_dup2(from_child[1],STDOUT_FILENO);
89 my_close(from_child[1]);
91 // attach standard error to standard output
92 // commenting this out gives error messages on the console
94 /* my_dup2(STDOUT_FILENO,STDERR_FILENO); */
96 // launch the new executable file
98 execvp(argv[0],&argv[0]);
100 // execvp() only returns when an error has occured
102 my_fatal("engine_open(): execvp(): %s\n",strerror(errno));
106 ASSERT(pipex->pid>0);
110 // close unused pipe descriptors to avoid deadlocks
112 my_close(from_child[1]);
113 my_close(to_child[0]);
115 // fill in the pipex struct
117 pipex->io->in_fd = from_child[0];
118 pipex->io->out_fd = to_child[1];
119 pipex->state|=PIPEX_ACTIVE; // can we test if this really TRUE?
126 void pipex_wait_event(pipex_t *pipex[]){
140 while((p=*(q++))!=NULL){
141 ASSERT(p->io->in_fd>=0);
142 FD_SET(p->io->in_fd,set);
143 if (p->io->in_fd > fd_max){
144 fd_max = p->io->in_fd;
148 // wait for something to read (no timeout)
151 val = select(fd_max+1,set,NULL,NULL,NULL);
152 if (val == -1 && errno != EINTR) my_fatal("pipex_wait_event(): select(): %s\n",strerror(errno));
156 while((p=*(q++))!=NULL){
157 if (FD_ISSET(p->io->in_fd,set)) io_get_update(p->io);
164 bool pipex_active(pipex_t *pipex){
165 return (pipex->state&PIPEX_ACTIVE)!=0;
170 bool pipex_eof(pipex_t *pipex){
171 return (pipex->state&PIPEX_EOF)!=0;
175 // pipex_set_priority()
177 void pipex_set_priority(pipex_t *pipex, int value){
179 setpriority(PRIO_PROCESS,pipex->pid,value);
183 // pipex_set_affinity()
185 void pipex_set_affinity(pipex_t *pipex, int value){
186 my_log("POLYGLOT Setting affinity is not yet implemented on posix\n");
191 void pipex_send_eof(pipex_t *pipex){
197 void pipex_exit(pipex_t *pipex){
204 bool pipex_readln(pipex_t *pipex, char *string){
205 while (!io_line_ready(pipex->io)) {
206 io_get_update(pipex->io);
208 if (!io_get_line(pipex->io,string,StringSize)) { // EOF
210 pipex->state|=PIPEX_EOF;
220 bool pipex_readln_nb(pipex_t *pipex, char *string){
221 if(io_line_ready(pipex->io)){
222 return pipex_readln(pipex,string);
231 void pipex_writeln(pipex_t *pipex, const char *string){
232 io_send(pipex->io,"%s",string);
237 static void my_close(int fd) {
241 if (close(fd) == -1) my_fatal("my_close(): close(): %s\n",strerror(errno));
246 static void my_dup2(int old_fd, int new_fd) {
251 if (dup2(old_fd,new_fd) == -1) my_fatal("my_dup2(): dup2(): %s\n",strerror(errno));