Merge branch 'master' into cde-next
[oweals/cde.git] / cde / lib / DtHelp / CanvasOs.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: CanvasOs.c /main/5 1996/05/09 03:40:38 drk $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  **
27  **   File:        UtilSDL.c
28  **
29  **   Project:     Cde Help System
30  **
31  **   Description: Utility functions for parsing an SDL volume.
32  **
33  **  (c) Copyright 1987, 1988, 1989, 1990, 1991, 1992 Hewlett-Packard Company
34  **
35  **  (c) Copyright 1993, 1994 Hewlett-Packard Company
36  **  (c) Copyright 1993, 1994 International Business Machines Corp.
37  **  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
38  **  (c) Copyright 1993, 1994 Novell, Inc.
39  **
40  ****************************************************************************
41  ************************************<+>*************************************/
42
43 /*
44  * system includes
45  */
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <sys/stat.h>
52
53 /*
54  * Canvas Engine includes
55  */
56 #include "CanvasP.h"
57
58 /*
59  * private includes
60  */
61 #include "bufioI.h"
62 #include "CanvasOsI.h"
63 #include "FormatUtilI.h"
64
65 #ifdef NLS16
66 #endif
67
68 /********    Private Function Declarations    ********/
69 /********    End Private Function Declarations    ********/
70
71 /******************************************************************************
72  *
73  * Private defines.
74  *
75  *****************************************************************************/
76 /******************************************************************************
77  *
78  * Private macros.
79  *
80  *****************************************************************************/
81 /******************************************************************************
82  *
83  * Private data.
84  *
85  *****************************************************************************/
86 /******************************************************************************
87  *
88  * Private Functions
89  *
90  *****************************************************************************/
91 /******************************************************************************
92  *
93  * Semi Public Functions
94  *
95  *****************************************************************************/
96 /*********************************************************************
97  * Function: _DtCvRunInterp
98  *
99  *    _DtCvRunInterp calls a script and maybe gets data.
100  *
101  *********************************************************************/
102 int
103 _DtCvRunInterp(
104     int                 (*filter_exec)(),
105     _DtCvPointer          client_data,
106     char                 *interp,
107     char                 *data,
108     char                **ret_data)
109 {
110     int          result;
111     int          myFd;
112     FILE        *myFile;
113     int          size;
114     int          writeBufSize = 0;
115     char        *writeBuf     = NULL;
116     char        *ptr;
117     char        *fileName;
118     char        *newData;
119     char         readBuf[BUFSIZ];
120     BufFilePtr   myBufFile;
121
122     /*
123      * ask for permission to run the interperator command.
124      */
125     newData = data;
126     if (filter_exec != NULL && (*filter_exec)(client_data,data,&newData) != 0)
127         return -1;
128
129     /*
130      * open a temporary file to write the data to.
131      */
132     fileName = tempnam(NULL, NULL);
133     if (fileName == NULL)
134       {
135         if (newData != data)
136             free(newData);
137         return -1;
138       }
139
140     /*
141      * write the data to file.
142      */
143     result = -1;
144 #if defined(__linux__)
145     myFd   = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
146 #else
147     myFd   = open(fileName, O_WRONLY | O_CREAT | O_TRUNC);
148 #endif
149     if (myFd != -1)
150       {
151         /*
152          * write the data to the file
153          */
154         result = write(myFd, newData, strlen(newData));
155
156         if (result != -1)
157           {
158             /*
159              * change the access permissions so the interpreter can read
160              * the file.
161              */
162             result = fchmod(myFd, S_IRUSR | S_IRGRP | S_IROTH);
163           }
164
165         /*
166          * close the file.
167          */
168         close(myFd);
169       }
170
171     if (newData != data)
172         free(newData);
173
174     if (result == -1)
175       {
176         unlink(fileName);
177         free(fileName);
178         return -1;
179       }
180
181     /*
182      * create the system command string with its parameters
183      */
184     ptr = (char *) malloc(sizeof(interp) + strlen(fileName) + 1);
185     if (!ptr)
186       {
187         unlink(fileName);
188         free(fileName);
189         return -1;
190       }
191
192     strcpy (ptr, interp);
193     strcat (ptr, " ");
194     strcat (ptr, fileName);
195
196     myFile = popen(ptr, "r");
197
198     /*
199      * free the command
200      */
201     free (ptr);
202
203     /*
204      * check for problems
205      */
206     if (!myFile) /* couldn't create execString process */
207       {
208         unlink(fileName);
209         free(fileName);
210         return -1;
211       }
212
213     /*
214      * create a file handler for the pipe.
215      */
216     myBufFile = _DtHelpCeCreatePipeBufFile(myFile);
217     if (myBufFile == NULL)
218       {
219         (void) pclose(myFile); /* don't check for error, it was popen'd */
220         unlink(fileName);
221         free(fileName);
222         return -1;
223       }
224
225     /*
226      * read the file the pipe writes to until the pipe finishes.
227      * Create a string from the results
228      */
229     do {
230         readBuf[0] = '\0';
231         ptr        = readBuf;
232
233         result = _DtHelpCeGetNxtBuf(myBufFile, readBuf, &ptr, BUFSIZ);
234
235         if (result > 0)
236           {
237             size = strlen(readBuf);
238             if (writeBuf == NULL)
239                 writeBuf = (char *) malloc (size + 1);
240             else
241                 writeBuf = (char *) realloc (writeBuf, writeBufSize + size + 1);
242
243             if (writeBuf != NULL)
244               {
245                 writeBuf[writeBufSize] = '\0';
246                 strcat(writeBuf, readBuf);
247                 writeBufSize += size;
248               }
249             else
250                 result = -1;
251           }
252     } while (result != -1 && !feof(FileStream(myBufFile)));
253
254     /*
255      * close the pipe
256      */
257     _DtHelpCeBufFileClose (myBufFile, True);
258
259     if (result == -1)
260       {
261         if (writeBuf != NULL)
262             free(writeBuf);
263         
264         writeBuf = NULL;
265       }
266     else
267         result = 0;
268
269     /*
270      * unlink the temporary file and free the memory.
271      */
272     unlink(fileName);
273     free(fileName);
274
275     /*
276      * return the data
277      */
278     *ret_data = writeBuf;
279
280     return result;
281
282 }  /* End _DtCvRunInterp */