spc-util.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. /*
  24. * File: spc-util.c $XConsortium: spc-util.c /main/5 1996/06/21 17:33:16 ageorge $
  25. * Language: C
  26. *
  27. * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
  28. *
  29. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  30. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  31. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  32. * (c) Copyright 1993, 1994 Novell, Inc. *
  33. */
  34. #include <bms/sbport.h> /* NOTE: sbport.h must be the first include. */
  35. #include <stdarg.h>
  36. #include <SPC/spcP.h>
  37. #include <bms/MemoryMgr.h>
  38. #include "DtSvcLock.h"
  39. /* The application's SPC activation list */
  40. SPC_Channel_Ptr spc_activation_list = NULL;
  41. /* Allocate us up a wire */
  42. /*----------------------------------------------------------------------+*/
  43. Wire *get_new_wire(void)
  44. /*----------------------------------------------------------------------+*/
  45. {
  46. Wire *tmp_wire;
  47. tmp_wire=(Wire *)XeMalloc(sizeof(Wire));
  48. memset(tmp_wire, 0, sizeof(Wire));
  49. tmp_wire->master_name=(XeString)XeMalloc(PTY_NAMLEN);
  50. memset(tmp_wire->master_name, 0, PTY_NAMLEN);
  51. tmp_wire->slave_name =(XeString)XeMalloc(PTY_NAMLEN);
  52. memset(tmp_wire->slave_name, 0, PTY_NAMLEN);
  53. tmp_wire->fd[0] = tmp_wire->fd[1] = (-1);
  54. tmp_wire->read_toolkit_id = (-1);
  55. tmp_wire->except_toolkit_id = (-1);
  56. return(tmp_wire);
  57. }
  58. /*----------------------------------------------------------------------+*/
  59. void free_wire(Wire *wire)
  60. /*----------------------------------------------------------------------+*/
  61. {
  62. free(wire->master_name);
  63. free(wire->slave_name);
  64. free((char *)wire);
  65. }
  66. /*
  67. * Channel object access
  68. */
  69. /*----------------------------------------------------------------------+*/
  70. SPC_Channel_Ptr SPC_Find_PID(int pid)
  71. /*----------------------------------------------------------------------+*/
  72. {
  73. /* Attempt to return a channel which currently handles process number PID */
  74. SPC_Channel_Ptr spc;
  75. _DtSvcProcessLock();
  76. for (spc = spc_activation_list; spc != NULL; spc = spc->next) {
  77. if (spc->pid == pid) break;
  78. }
  79. _DtSvcProcessUnlock();
  80. return spc; /* NULL when not found */
  81. }
  82. /*
  83. * Miscellaneous
  84. */
  85. /*----------------------------------------------------------------------+*/
  86. int
  87. spc_close(int fd)
  88. /*----------------------------------------------------------------------+*/
  89. {
  90. /* Close a file descriptor which was referenced through an SPC structure */
  91. if(fd != -1)
  92. close(fd);
  93. return(fd);
  94. }
  95. /*----------------------------------------------------------------------+*/
  96. int
  97. spc_dup2(int from, int to)
  98. /*----------------------------------------------------------------------+*/
  99. {
  100. int retval;
  101. /* Dup file descriptors. If a null descriptor, then use /dev/null */
  102. static int devnull = 0;
  103. if (from == to)
  104. return(TRUE);
  105. if (from == -1) {
  106. _DtSvcProcessLock();
  107. if (!devnull)
  108. devnull = open("/dev/null", 0);
  109. /* Use /dev/null when no source file descriptor */
  110. from = devnull;
  111. _DtSvcProcessUnlock();
  112. }
  113. /* Now do the dup2 */
  114. retval=dup2(from, to);
  115. return(retval);
  116. }
  117. /*----------------------------------------------------------------------+*/
  118. int
  119. SPC_fd_to_connector(SPC_Channel_Ptr channel,
  120. int fd)
  121. /*----------------------------------------------------------------------+*/
  122. {
  123. if(Stdout(channel) == fd)
  124. return(STDOUT);
  125. if(Stdin(channel) == fd)
  126. return(STDIN);
  127. if(Stderr(channel) == fd)
  128. return(STDERR);
  129. return(ERROR);
  130. }
  131. XeString *Alloc_Argv(int n)
  132. {
  133. /* Allocate an array to hold argv list */
  134. XeString *av;
  135. av = (XeString *)XeMalloc((n + 1) * sizeof(XeString));
  136. /* Zero the space so we don't have to worry about trailing NULL */
  137. memset((XeString) av, 0, (n + 1) * sizeof(XeString));
  138. return(av);
  139. }
  140. /*
  141. ***
  142. *** Toolkit integration stuff, without direct calls to the toolkit.
  143. ***
  144. */
  145. int break_on_termination=FALSE;
  146. /*----------------------------------------------------------------------+*/
  147. void SPC_Conditional_Packet_Handler(void * UNUSED_PARM(client_data),
  148. int * source,
  149. SPCInputId * UNUSED_PARM(id))
  150. /*----------------------------------------------------------------------+*/
  151. {
  152. SPC_Channel_Ptr channel;
  153. channel = XeSPCHandleTerminator(*source);
  154. /* Okay, blast out of our wait */
  155. _DtSvcProcessLock();
  156. if( (channel==SPC_ERROR || !IS_ACTIVE(channel)) &&
  157. break_on_termination)
  158. SPC_XtBreak();
  159. _DtSvcProcessUnlock();
  160. /* return(TRUE); */
  161. }
  162. /*----------------------------------------------------------------------+*/
  163. int sprintf_len (XeString s, XeString format, ...)
  164. /*----------------------------------------------------------------------+*/
  165. {
  166. va_list ap;
  167. va_start(ap, format);
  168. if (s) *s = XeChar_NULL;
  169. vsprintf(s, format, ap);
  170. return(strlen(s));
  171. }
  172. /*
  173. ***
  174. *** _path_search is a generalized function used for parsing the PATH
  175. *** environment variable wrt a passed filename. It will run down the
  176. *** passed path variable looking for ':'. When it finds one (or hits
  177. *** the end of the string), it will concatenate the substring with
  178. *** filename, passing the result to the passed path_search_predicate.
  179. *** If this predicate returns true, the function will return with a
  180. *** true value. If all elements of the path are processed with no
  181. *** true result from the predicate, the function will return false.
  182. ***
  183. *** A few special cases:
  184. *** - If the filename begins with '/' (an absolute path), the
  185. *** funciton will return the value of the predicate on the
  186. *** filename (e.g. PATH processing will not be done).
  187. *** - a NULL value of any path component will be interpreted as the
  188. *** current directory ('.').
  189. *** - a NULL value for the path parameter means use the PATH
  190. *** environment variable.
  191. *** - a NULL value for filename will return FALSE
  192. ***
  193. */
  194. /*
  195. ***
  196. *** MAXPATHLEN is defined in sbstdinc.h
  197. ***
  198. */
  199. /*----------------------------------------------------------------------+*/
  200. Boolean _path_search (XeString path, XeString filename, path_search_predicate p)
  201. /*----------------------------------------------------------------------+*/
  202. {
  203. XeString path_rest, next_colon=NULL;
  204. XeChar buffer[MAXPATHLEN+1];
  205. XeChar path_component[MAXPATHLEN+1];
  206. int path_component_len;
  207. int filename_len;
  208. int component_separator=(int)':'; /* this is here because strchr takes an in */
  209. if(!filename)
  210. return(FALSE);
  211. if(*filename == '/')
  212. return((*p)(filename, NULL, filename));
  213. filename_len = strlen(filename);
  214. if(!path)
  215. path=getenv("PATH");
  216. for( (path_rest=path ,
  217. next_colon=strchr(path_rest, component_separator));
  218. path_rest && *path_rest;
  219. next_colon=strchr(path_rest, component_separator)) {
  220. /*
  221. ** Copy path component into buffer
  222. */
  223. if(next_colon) { /* found colon */
  224. path_component_len = next_colon-path_rest;
  225. strncpy(buffer, path_rest, path_component_len);
  226. buffer[path_component_len]='\0';
  227. path_rest=next_colon+1;
  228. if(!*path_rest)
  229. /* We've seen a ':' at the end of the string. Make path_rest be "."
  230. next go-round */
  231. path_rest=".";
  232. } else { /* no colon */
  233. path_component_len = strlen(path_rest);
  234. strcpy(buffer, path_rest);
  235. path_rest=NULL;
  236. }
  237. /*
  238. ** if path component is NULL, use default ('.');
  239. */
  240. if(!buffer[0]) {
  241. buffer[0] = '.';
  242. buffer[1] = '\0';
  243. }
  244. /*
  245. ** Concatenate filename
  246. */
  247. if((path_component_len + filename_len + 1) < MAXPATHLEN) {
  248. path_component[0]=0;
  249. strcat(path_component, buffer);
  250. strcat(buffer, "/");
  251. strcat(buffer, filename);
  252. /*
  253. ** Check this file. If the predicate returns true, we return true.
  254. */
  255. if((*p)(buffer, path_component, filename))
  256. return(TRUE);
  257. }
  258. }
  259. /*
  260. ** We've checked all components. Return False.
  261. */
  262. return(FALSE);
  263. }