2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
24 * File: spc-util.c $XConsortium: spc-util.c /main/5 1996/06/21 17:33:16 ageorge $
27 * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
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. *
35 #include <bms/sbport.h> /* NOTE: sbport.h must be the first include. */
39 #include <bms/MemoryMgr.h>
40 #include "DtSvcLock.h"
42 /* The application's SPC activation list */
43 SPC_Channel_Ptr spc_activation_list = NULL;
45 /* Allocate us up a wire */
47 /*----------------------------------------------------------------------+*/
48 Wire *get_new_wire(void)
49 /*----------------------------------------------------------------------+*/
53 tmp_wire=(Wire *)XeMalloc(sizeof(Wire));
54 memset(tmp_wire, 0, sizeof(Wire));
56 tmp_wire->master_name=(XeString)XeMalloc(PTY_NAMLEN);
57 memset(tmp_wire->master_name, 0, PTY_NAMLEN);
58 tmp_wire->slave_name =(XeString)XeMalloc(PTY_NAMLEN);
59 memset(tmp_wire->slave_name, 0, PTY_NAMLEN);
60 tmp_wire->fd[0] = tmp_wire->fd[1] = (-1);
61 tmp_wire->read_toolkit_id = (-1);
62 tmp_wire->except_toolkit_id = (-1);
66 /*----------------------------------------------------------------------+*/
67 void free_wire(Wire *wire)
68 /*----------------------------------------------------------------------+*/
70 free(wire->master_name);
71 free(wire->slave_name);
76 * Channel object access
79 /*----------------------------------------------------------------------+*/
80 SPC_Channel_Ptr SPC_Find_PID(int pid)
81 /*----------------------------------------------------------------------+*/
83 /* Attempt to return a channel which currently handles process number PID */
87 for (spc = spc_activation_list; spc != NULL; spc = spc->next) {
88 if (spc->pid == pid) break;
90 _DtSvcProcessUnlock();
91 return spc; /* NULL when not found */
98 /*----------------------------------------------------------------------+*/
101 /*----------------------------------------------------------------------+*/
103 /* Close a file descriptor which was referenced through an SPC structure */
111 /*----------------------------------------------------------------------+*/
113 spc_dup2(int from, int to)
114 /*----------------------------------------------------------------------+*/
118 /* Dup file descriptors. If a null descriptor, then use /dev/null */
119 static int devnull = 0;
127 devnull = open("/dev/null", 0);
128 /* Use /dev/null when no source file descriptor */
130 _DtSvcProcessUnlock();
133 /* Now do the dup2 */
134 retval=dup2(from, to);
138 /*----------------------------------------------------------------------+*/
140 SPC_fd_to_connector(SPC_Channel_Ptr channel,
142 /*----------------------------------------------------------------------+*/
144 if(Stdout(channel) == fd)
146 if(Stdin(channel) == fd)
148 if(Stderr(channel) == fd)
153 XeString *Alloc_Argv(int n)
155 /* Allocate an array to hold argv list */
158 av = (XeString *)XeMalloc((n + 1) * sizeof(XeString));
160 /* Zero the space so we don't have to worry about trailing NULL */
161 memset((XeString) av, 0, (n + 1) * sizeof(XeString));
168 *** Toolkit integration stuff, without direct calls to the toolkit.
172 int break_on_termination=FALSE;
174 /*----------------------------------------------------------------------+*/
175 void SPC_Conditional_Packet_Handler(void * UNUSED_PARM(client_data),
177 SPCInputId * UNUSED_PARM(id))
178 /*----------------------------------------------------------------------+*/
181 SPC_Channel_Ptr channel;
183 channel = XeSPCHandleTerminator(*source);
185 /* Okay, blast out of our wait */
187 if( (channel==SPC_ERROR || !IS_ACTIVE(channel)) &&
188 break_on_termination)
190 _DtSvcProcessUnlock();
194 /*----------------------------------------------------------------------+*/
195 int sprintf_len (XeString s, XeString format, ...)
196 /*----------------------------------------------------------------------+*/
200 va_start(ap, format);
201 if (s) *s = XeChar_NULL;
202 vsprintf(s, format, ap);
208 *** _path_search is a generalized function used for parsing the PATH
209 *** environment variable wrt a passed filename. It will run down the
210 *** passed path variable looking for ':'. When it finds one (or hits
211 *** the end of the string), it will concatenate the substring with
212 *** filename, passing the result to the passed path_search_predicate.
213 *** If this predicate returns true, the function will return with a
214 *** true value. If all elements of the path are processed with no
215 *** true result from the predicate, the function will return false.
217 *** A few special cases:
218 *** - If the filename begins with '/' (an absolute path), the
219 *** funciton will return the value of the predicate on the
220 *** filename (e.g. PATH processing will not be done).
221 *** - a NULL value of any path component will be interpreted as the
222 *** current directory ('.').
223 *** - a NULL value for the path parameter means use the PATH
224 *** environment variable.
225 *** - a NULL value for filename will return FALSE
231 *** MAXPATHLEN is defined in sbstdinc.h
235 /*----------------------------------------------------------------------+*/
236 Boolean _path_search (XeString path, XeString filename, path_search_predicate p)
237 /*----------------------------------------------------------------------+*/
240 XeString path_rest, next_colon=NULL;
241 XeChar buffer[MAXPATHLEN+1];
242 XeChar path_component[MAXPATHLEN+1];
243 int path_component_len;
245 int component_separator=(int)':'; /* this is here because strchr takes an in */
251 return((*p)(filename, NULL, filename));
253 filename_len = strlen(filename);
257 for( (path_rest=path ,
258 next_colon=strchr(path_rest, component_separator));
259 path_rest && *path_rest;
260 next_colon=strchr(path_rest, component_separator)) {
263 ** Copy path component into buffer
266 if(next_colon) { /* found colon */
267 path_component_len = next_colon-path_rest;
268 strncpy(buffer, path_rest, path_component_len);
269 buffer[path_component_len]='\0';
270 path_rest=next_colon+1;
272 /* We've seen a ':' at the end of the string. Make path_rest be "."
275 } else { /* no colon */
276 path_component_len = strlen(path_rest);
277 strcpy(buffer, path_rest);
282 ** if path component is NULL, use default ('.');
291 ** Concatenate filename
294 if((path_component_len + filename_len + 1) < MAXPATHLEN) {
296 strcat(path_component, buffer);
298 strcat(buffer, filename);
301 ** Check this file. If the predicate returns true, we return true.
304 if((*p)(buffer, path_component, filename))
310 ** We've checked all components. Return False.