CanvasOs.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * system includes
  44. */
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <sys/stat.h>
  51. /*
  52. * Canvas Engine includes
  53. */
  54. #include "CanvasP.h"
  55. /*
  56. * private includes
  57. */
  58. #include "bufioI.h"
  59. #include "CanvasOsI.h"
  60. #include "FormatUtilI.h"
  61. #ifdef NLS16
  62. #endif
  63. /******** Private Function Declarations ********/
  64. /******** End Private Function Declarations ********/
  65. /******************************************************************************
  66. *
  67. * Private defines.
  68. *
  69. *****************************************************************************/
  70. /******************************************************************************
  71. *
  72. * Private macros.
  73. *
  74. *****************************************************************************/
  75. /******************************************************************************
  76. *
  77. * Private data.
  78. *
  79. *****************************************************************************/
  80. /******************************************************************************
  81. *
  82. * Private Functions
  83. *
  84. *****************************************************************************/
  85. /******************************************************************************
  86. *
  87. * Semi Public Functions
  88. *
  89. *****************************************************************************/
  90. /*********************************************************************
  91. * Function: _DtCvRunInterp
  92. *
  93. * _DtCvRunInterp calls a script and maybe gets data.
  94. *
  95. *********************************************************************/
  96. int
  97. _DtCvRunInterp(
  98. int (*filter_exec)(),
  99. _DtCvPointer client_data,
  100. char *interp,
  101. char *data,
  102. char **ret_data)
  103. {
  104. int result;
  105. int myFd;
  106. FILE *myFile;
  107. int size;
  108. int writeBufSize = 0;
  109. char *writeBuf = NULL;
  110. char *ptr;
  111. char *fileName;
  112. char *newData;
  113. char readBuf[BUFSIZ];
  114. BufFilePtr myBufFile;
  115. /*
  116. * ask for permission to run the interperator command.
  117. */
  118. newData = data;
  119. if (filter_exec != NULL && (*filter_exec)(client_data,data,&newData) != 0)
  120. return -1;
  121. /*
  122. * open a temporary file to write the data to.
  123. */
  124. fileName = tempnam(NULL, NULL);
  125. if (fileName == NULL)
  126. {
  127. if (newData != data)
  128. free(newData);
  129. return -1;
  130. }
  131. /*
  132. * write the data to file.
  133. */
  134. result = -1;
  135. #if defined(__linux__)
  136. myFd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
  137. #else
  138. myFd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC);
  139. #endif
  140. if (myFd != -1)
  141. {
  142. /*
  143. * write the data to the file
  144. */
  145. result = write(myFd, newData, strlen(newData));
  146. if (result != -1)
  147. {
  148. /*
  149. * change the access permissions so the interpreter can read
  150. * the file.
  151. */
  152. result = fchmod(myFd, S_IRUSR | S_IRGRP | S_IROTH);
  153. }
  154. /*
  155. * close the file.
  156. */
  157. close(myFd);
  158. }
  159. if (newData != data)
  160. free(newData);
  161. if (result == -1)
  162. {
  163. unlink(fileName);
  164. free(fileName);
  165. return -1;
  166. }
  167. /*
  168. * create the system command string with its parameters
  169. */
  170. ptr = (char *) malloc(sizeof(interp) + strlen(fileName) + 1);
  171. if (!ptr)
  172. {
  173. unlink(fileName);
  174. free(fileName);
  175. return -1;
  176. }
  177. strcpy (ptr, interp);
  178. strcat (ptr, " ");
  179. strcat (ptr, fileName);
  180. myFile = popen(ptr, "r");
  181. /*
  182. * free the command
  183. */
  184. free (ptr);
  185. /*
  186. * check for problems
  187. */
  188. if (!myFile) /* couldn't create execString process */
  189. {
  190. unlink(fileName);
  191. free(fileName);
  192. return -1;
  193. }
  194. /*
  195. * create a file handler for the pipe.
  196. */
  197. myBufFile = _DtHelpCeCreatePipeBufFile(myFile);
  198. if (myBufFile == NULL)
  199. {
  200. (void) pclose(myFile); /* don't check for error, it was popen'd */
  201. unlink(fileName);
  202. free(fileName);
  203. return -1;
  204. }
  205. /*
  206. * read the file the pipe writes to until the pipe finishes.
  207. * Create a string from the results
  208. */
  209. do {
  210. readBuf[0] = '\0';
  211. ptr = readBuf;
  212. result = _DtHelpCeGetNxtBuf(myBufFile, readBuf, &ptr, BUFSIZ);
  213. if (result > 0)
  214. {
  215. size = strlen(readBuf);
  216. if (writeBuf == NULL)
  217. writeBuf = (char *) malloc (size + 1);
  218. else
  219. writeBuf = (char *) realloc (writeBuf, writeBufSize + size + 1);
  220. if (writeBuf != NULL)
  221. {
  222. writeBuf[writeBufSize] = '\0';
  223. strcat(writeBuf, readBuf);
  224. writeBufSize += size;
  225. }
  226. else
  227. result = -1;
  228. }
  229. } while (result != -1 && !feof(FileStream(myBufFile)));
  230. /*
  231. * close the pipe
  232. */
  233. _DtHelpCeBufFileClose (myBufFile, True);
  234. if (result == -1)
  235. {
  236. if (writeBuf != NULL)
  237. free(writeBuf);
  238. writeBuf = NULL;
  239. }
  240. else
  241. result = 0;
  242. /*
  243. * unlink the temporary file and free the memory.
  244. */
  245. unlink(fileName);
  246. free(fileName);
  247. /*
  248. * return the data
  249. */
  250. *ret_data = writeBuf;
  251. return result;
  252. } /* End _DtCvRunInterp */