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 librararies 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, NULL, sizeof(Wire));
56 tmp_wire->master_name=(XeString)XeMalloc(PTY_NAMLEN);
57 memset(tmp_wire->master_name, NULL, PTY_NAMLEN);
58 tmp_wire->slave_name =(XeString)XeMalloc(PTY_NAMLEN);
59 memset(tmp_wire->slave_name, NULL, 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 /*----------------------------------------------------------------------+*/
100 /*----------------------------------------------------------------------+*/
102 /* Close a file descriptor which was referenced through an SPC structure */
110 /*----------------------------------------------------------------------+*/
111 spc_dup2(int from, int to)
112 /*----------------------------------------------------------------------+*/
116 /* Dup file descriptors. If a null descriptor, then use /dev/null */
117 static int devnull = NULL;
125 devnull = open("/dev/null", 0);
126 /* Use /dev/null when no source file descriptor */
128 _DtSvcProcessUnlock();
131 /* Now do the dup2 */
132 retval=dup2(from, to);
136 /*----------------------------------------------------------------------+*/
137 SPC_fd_to_connector(SPC_Channel_Ptr channel,
139 /*----------------------------------------------------------------------+*/
141 if(Stdout(channel) == fd)
143 if(Stdin(channel) == fd)
145 if(Stderr(channel) == fd)
150 XeString *Alloc_Argv(int n)
152 /* Allocate an array to hold argv list */
155 av = (XeString *)XeMalloc((n + 1) * sizeof(XeString));
157 /* Zero the space so we don't have to worry about trailing NULL */
158 memset((XeString) av, NULL, (n + 1) * sizeof(XeString));
165 *** Toolkit integration stuff, without direct calls to the toolkit.
169 int break_on_termination=FALSE;
171 /*----------------------------------------------------------------------+*/
172 void SPC_Conditional_Packet_Handler(void * UNUSED_PARM(client_data),
174 SPCInputId * UNUSED_PARM(id))
175 /*----------------------------------------------------------------------+*/
178 SPC_Channel_Ptr channel;
180 channel = XeSPCHandleTerminator(*source);
182 /* Okay, blast out of our wait */
184 if( (channel==SPC_ERROR || !IS_ACTIVE(channel)) &&
185 break_on_termination)
187 _DtSvcProcessUnlock();
191 /*----------------------------------------------------------------------+*/
192 int sprintf_len (XeString s, XeString format, ...)
193 /*----------------------------------------------------------------------+*/
197 va_start(ap, format);
198 if (s) *s = XeChar_NULL;
199 vsprintf(s, format, ap);
205 *** _path_search is a generalized function used for parsing the PATH
206 *** environment variable wrt a passed filename. It will run down the
207 *** passed path variable looking for ':'. When it finds one (or hits
208 *** the end of the string), it will concatenate the substring with
209 *** filename, passing the result to the passed path_search_predicate.
210 *** If this predicate returns true, the function will return with a
211 *** true value. If all elements of the path are processed with no
212 *** true result from the predicate, the function will return false.
214 *** A few special cases:
215 *** - If the filename begins with '/' (an absolute path), the
216 *** funciton will return the value of the predicate on the
217 *** filename (e.g. PATH processing will not be done).
218 *** - a NULL value of any path component will be interpreted as the
219 *** current directory ('.').
220 *** - a NULL value for the path parameter means use the PATH
221 *** environment variable.
222 *** - a NULL value for filename will return FALSE
228 *** MAXPATHLEN is defined in sbstdinc.h
232 /*----------------------------------------------------------------------+*/
233 Boolean _path_search (XeString path, XeString filename, path_search_predicate p)
234 /*----------------------------------------------------------------------+*/
237 XeString path_rest, next_colon=NULL;
238 XeChar buffer[MAXPATHLEN+1];
239 XeChar path_component[MAXPATHLEN+1];
240 int path_component_len;
242 int component_seperator=(int)':'; /* this is here because strchr takes an in */
248 return((*p)(filename, NULL, filename));
250 filename_len = strlen(filename);
254 for( (path_rest=path ,
255 next_colon=strchr(path_rest, component_seperator));
256 path_rest && *path_rest;
257 next_colon=strchr(path_rest, component_seperator)) {
260 ** Copy path component into buffer
263 if(next_colon) { /* found colon */
264 path_component_len = next_colon-path_rest;
265 strncpy(buffer, path_rest, path_component_len);
266 buffer[path_component_len]=NULL;
267 path_rest=next_colon+1;
269 /* We've seen a ':' at the end of the string. Make path_rest be "."
272 } else { /* no colon */
273 path_component_len = strlen(path_rest);
274 strcpy(buffer, path_rest);
279 ** if path component is NULL, use default ('.');
288 ** Concatenate filename
291 if((path_component_len + filename_len + 1) < MAXPATHLEN) {
293 strcat(path_component, buffer);
295 strcat(buffer, filename);
298 ** Check this file. If the predicate returns true, we return true.
301 if((*p)(buffer, path_component, filename))
307 ** We've checked all components. Return False.