1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* popen() and pclose() calls for systems without pipe facilities |
6 |
*/ |
7 |
|
8 |
/* ==================================================================== |
9 |
* The Radiance Software License, Version 1.0 |
10 |
* |
11 |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
12 |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
13 |
* |
14 |
* Redistribution and use in source and binary forms, with or without |
15 |
* modification, are permitted provided that the following conditions |
16 |
* are met: |
17 |
* |
18 |
* 1. Redistributions of source code must retain the above copyright |
19 |
* notice, this list of conditions and the following disclaimer. |
20 |
* |
21 |
* 2. Redistributions in binary form must reproduce the above copyright |
22 |
* notice, this list of conditions and the following disclaimer in |
23 |
* the documentation and/or other materials provided with the |
24 |
* distribution. |
25 |
* |
26 |
* 3. The end-user documentation included with the redistribution, |
27 |
* if any, must include the following acknowledgment: |
28 |
* "This product includes Radiance software |
29 |
* (http://radsite.lbl.gov/) |
30 |
* developed by the Lawrence Berkeley National Laboratory |
31 |
* (http://www.lbl.gov/)." |
32 |
* Alternately, this acknowledgment may appear in the software itself, |
33 |
* if and wherever such third-party acknowledgments normally appear. |
34 |
* |
35 |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
36 |
* and "The Regents of the University of California" must |
37 |
* not be used to endorse or promote products derived from this |
38 |
* software without prior written permission. For written |
39 |
* permission, please contact [email protected]. |
40 |
* |
41 |
* 5. Products derived from this software may not be called "Radiance", |
42 |
* nor may "Radiance" appear in their name, without prior written |
43 |
* permission of Lawrence Berkeley National Laboratory. |
44 |
* |
45 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
46 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
47 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
48 |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
49 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
50 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
51 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
52 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
53 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
54 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
55 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
56 |
* SUCH DAMAGE. |
57 |
* ==================================================================== |
58 |
* |
59 |
* This software consists of voluntary contributions made by many |
60 |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
61 |
* information on Lawrence Berkeley National Laboratory, please see |
62 |
* <http://www.lbl.gov/>. |
63 |
*/ |
64 |
|
65 |
#include <stdio.h> |
66 |
#include <stdlib.h> |
67 |
#include <string.h> |
68 |
#include <ctype.h> |
69 |
#include "paths.h" |
70 |
|
71 |
#ifndef NFILE |
72 |
#define NFILE 32 |
73 |
#endif |
74 |
|
75 |
static struct { |
76 |
char *f; /* file name, NULL if inactive */ |
77 |
char *c; /* command if writing */ |
78 |
} pips[NFILE]; |
79 |
|
80 |
|
81 |
FILE * |
82 |
popen(cmd, mode) /* open command for reading or writing */ |
83 |
register char *cmd; |
84 |
char *mode; |
85 |
{ |
86 |
FILE *fp; |
87 |
char *newcmd, *fname; |
88 |
register char *cp, *cp2 = NULL; |
89 |
int quote = '\0', paren = 0; |
90 |
|
91 |
while (isspace(*cmd)) |
92 |
cmd++; |
93 |
if (!*cmd) |
94 |
return(NULL); |
95 |
fname = (char *)malloc(TEMPLEN+1); |
96 |
newcmd = (char *)malloc(TEMPLEN+6+strlen(cmd)); |
97 |
if (fname == NULL | newcmd == NULL) |
98 |
return(NULL); |
99 |
mktemp(strcpy(fname,TEMPLATE)); |
100 |
/* build our command */ |
101 |
for (cp = newcmd; ; cmd++) { |
102 |
switch (*cmd) { |
103 |
case '\'': |
104 |
case '"': |
105 |
if (!quote) |
106 |
quote = *cmd; |
107 |
else if (quote == *cmd) |
108 |
quote = '\0'; |
109 |
#ifdef MSDOS |
110 |
else |
111 |
break; |
112 |
*cp++ = '"'; /* double quotes only */ |
113 |
continue; |
114 |
case '\\': |
115 |
if (!quote && cmd[1] == '\n') { |
116 |
*cp++ = ' '; |
117 |
cmd++; |
118 |
continue; |
119 |
} |
120 |
cmd++; |
121 |
break; |
122 |
#else |
123 |
break; |
124 |
case '\\': |
125 |
*cp++ = *cmd++; |
126 |
break; |
127 |
case '(': |
128 |
if (!quote) |
129 |
paren++; |
130 |
break; |
131 |
case ')': |
132 |
if (!quote && paren) |
133 |
paren--; |
134 |
break; |
135 |
case ';': |
136 |
#endif |
137 |
case '|': |
138 |
if (!quote && !paren && cp2 == NULL) |
139 |
cp2 = fname; |
140 |
break; |
141 |
case ' ': |
142 |
case '\t': |
143 |
if (!quote) |
144 |
while (isspace(cmd[1])) |
145 |
cmd++; |
146 |
break; |
147 |
case '\n': |
148 |
case '\0': |
149 |
if (cp2 == NULL) |
150 |
cp2 = fname; |
151 |
break; |
152 |
} |
153 |
if (cp2 == fname && *mode == 'w') { /* add input file */ |
154 |
*cp++ = ' '; *cp++ = '<'; |
155 |
while (*cp2) |
156 |
*cp++ = *cp2++; |
157 |
*cp++ = ' '; |
158 |
} |
159 |
if (!(*cp = *cmd)) |
160 |
break; |
161 |
cp++; |
162 |
} |
163 |
if (*mode == 'r') { /* add output file */ |
164 |
while (isspace(cp[-1]) || cp[-1] == ';') |
165 |
cp--; |
166 |
*cp++ = ' '; *cp++ = '>'; |
167 |
while (*cp2) |
168 |
*cp++ = *cp2++; |
169 |
*cp = '\0'; |
170 |
system(newcmd); /* execute command first */ |
171 |
} |
172 |
if ((fp = fopen(fname, mode)) == NULL) { /* open file */ |
173 |
free(fname); |
174 |
free(newcmd); |
175 |
return(NULL); |
176 |
} |
177 |
if (fileno(fp) >= NFILE) { |
178 |
eputs("popen: too many open files\n"); |
179 |
quit(1); |
180 |
} |
181 |
pips[fileno(fp)].f = fname; |
182 |
if (*mode == 'r') { |
183 |
free(newcmd); |
184 |
pips[fileno(fp)].c = NULL; |
185 |
} else |
186 |
pips[fileno(fp)].c = newcmd; |
187 |
return(fp); |
188 |
} |
189 |
|
190 |
|
191 |
pclose(fp) /* close pipe and cleanup */ |
192 |
FILE *fp; |
193 |
{ |
194 |
register int pn = fileno(fp); |
195 |
|
196 |
if (pn >= NFILE || pips[pn].f == NULL || fclose(fp) == -1) |
197 |
return(-1); |
198 |
if (pips[pn].c != NULL) { |
199 |
system(pips[pn].c); /* execute command last */ |
200 |
free(pips[pn].c); |
201 |
} |
202 |
unlink(pips[pn].f); /* remove temp file */ |
203 |
free(pips[pn].f); |
204 |
pips[pn].f = NULL; |
205 |
return(0); |
206 |
} |