ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/Frodo4/Src/CmdPipe.cpp
Revision: 1.4
Committed: 2005-06-27T19:55:48Z (19 years, 4 months ago) by cebix
Branch: MAIN
CVS Tags: VERSION_4_2, HEAD
Changes since 1.3: +1 -1 lines
Log Message:
updated copyright dates

File Contents

# Content
1 /*
2 * CmdPipe.cpp
3 *
4 * Frodo (C) 1994-1997,2002-2005 Christian Bauer
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "CmdPipe.h"
22
23
24 extern "C" {
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <sys/types.h>
30 #include <string.h>
31 #include <signal.h>
32
33 #if defined(__alpha__)
34 #include <cma.h>
35 #endif
36
37 #if defined(AIX)
38 #include <sys/select.h>
39 #else
40 #include <unistd.h>
41 #endif
42
43 #if defined(__linux__)
44 #include <sys/time.h>
45 #endif
46
47 #include <time.h>
48 #include <errno.h>
49 }
50
51 static void kaputt(const char * c1, const char * c2) {
52 fprintf(stderr,"error: %s%s\n",c1,c2);
53 exit(20);
54 }
55
56 Pipe::Pipe(void) : fail(true) {
57
58 fds[0] = 0;
59 fds[1] = 1;
60
61 if (-1 == pipe(fds)) {
62 kaputt("Pipe: ","unable to create pipe");
63 return;
64 }
65
66 fail = false;
67 return;
68 }
69
70 Pipe::~Pipe(void) {
71
72 if (! fail) {
73 close(fds[0]);
74 close(fds[1]);
75 }
76 return;
77 }
78
79 unsigned long Pipe::ewrite(const void * buf, unsigned long len) {
80
81 unsigned long wsum = 0;
82 while (len) {
83 long wlen;
84
85 wlen = ::write(fds[1], buf, (long) len);
86 if (wlen <= 0) {
87 kaputt("Pipe::ewrite ","write-error");
88 }
89
90 len -= wlen;
91 buf = (void*) ((char*) buf + wlen);
92 wsum += wlen;
93 }
94 return wsum;
95 }
96
97 unsigned long Pipe::eread(void * buf, unsigned long len) {
98
99 unsigned long rsum = 0;
100 while (len) {
101 long rlen;
102
103 rlen = ::read(fds[0], buf, (long) len);
104
105 if (rlen <= 0) {
106 kaputt("Pipe::eread ","read-error");
107 }
108
109 len -= rlen;
110 buf = (void*) ((char*) buf + rlen);
111 rsum += rlen;
112 }
113 return rsum;
114 }
115
116 int Pipe::probe(void) const {
117
118 fd_set set;
119 FD_ZERO(&set);
120 FD_SET(fds[0], &set);
121
122 struct timeval tv;
123 tv.tv_sec = 0;
124 tv.tv_usec = 0;
125
126 int res;
127 // Use the following commented line for HP-UX < 10.20
128 // res = select(FD_SETSIZE, (int *)&set, (int *)0, (int *)0, &tv);
129 res = select(FD_SETSIZE, &set, (fd_set *)0, (fd_set *)0, &tv);
130
131 if (res > 0) return -1;
132 return 0;
133
134 }
135
136 CmdPipe::CmdPipe(const char * command, const char * arg, int nicediff) : childpid(0), fail(true) {
137
138 if (tocmd.fail || fromcmd.fail) {
139 kaputt("CmdPipe: ","unable to initialize pipes");
140 return;
141 }
142
143 childpid = fork();
144
145 if (childpid == -1) {
146 childpid = 0;
147 kaputt("CmdPipe: ","unable to fork process");
148 return;
149 }
150
151 if (childpid == 0) {
152
153 if (nicediff) {
154 if (-1 == nice(nicediff)) {
155 fprintf(stderr,"CmdPipe: unable to change nice-level (non-fatal)");
156 }
157 }
158
159 dup2(tocmd.get_read_fd(), STDIN_FILENO);
160
161 dup2(fromcmd.get_write_fd(), STDOUT_FILENO);
162 execlp(command, "Frodo_GUI", arg, (char *)0);
163 kaputt("CmdPipe: unable to execute child process ",command);
164 _exit(0); // exit (and do NOT call destructors etc..)
165 }
166
167 fail = false;
168 return;
169 }
170
171 CmdPipe::~CmdPipe(void) {
172
173 if (childpid) {
174 int status;
175 waitpid(childpid, &status, 0);
176
177 if (status != 0) {
178 fprintf(stderr,"~CmdPipe child process returned error\n");
179 }
180 }
181 }