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: Utility.c $XConsortium: Utility.c /main/5 1996/06/21 17:20:20 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. *
36 #include <sys/types.h>
53 #include <sys/param.h> /* MAXPATHLEN, MAXHOSTNAMELEN */
55 #include <X11/Intrinsic.h>
56 #include <X11/StringDefs.h>
58 #include <Dt/Connect.h>
59 #include <Dt/FileUtil.h>
60 #include <Dt/DtNlUtils.h>
61 #include <Dt/Utility.h>
62 #include <Dt/UtilityP.h>
63 #include "DtSvcLock.h"
71 /******** Static Function Declarations ********/
73 static char * RemapSpecialDisplayName(
76 /******** End Static Function Declarations ********/
81 * Function Name: _DtVectorizeInPlace
85 * A "string vector" is an array of pointers to strings. The
86 * end of the array is marked with a null pointer. This function
87 * takes a long string and creates a string vector from it by
88 * allocating the array of pointers, breaking the string up into
89 * sub-strings (based on a specified separator character), and
90 * making the array elements point to the sub-strings;
92 * NOTE that it does this "in place". If you don't want the original
93 * string disturbed, be sure to make a copy of it before calling
96 * The memory for the array of pointers is owned by the caller.
97 * It can be safely freed (though this doesn't free the sub-strings).
101 * svector = _DtVectorizeInPlace (string, separator);
103 * char **svector; The string vector that was created. The
104 * memory is owned by the caller.
105 * char *string; A pointer to the string to be vectorized.
106 * THIS STRING WILL BE ALTERED.
107 * char separtor; The separator character which marks the
108 * ends of the sub-strings.
117 /* LOCAL VARIABLES */
119 char **vector, **next_string, *nextc;
124 /* Count the elements in the string and allocate an appropriate size
125 vector. There is one more element than separator characters. */
128 while ((nextc = DtStrchr(nextc, separator)))
134 vector = (char **) XtMalloc ((Cardinal)(sizeof(char *) * (num_pieces + 1)));
136 /* Set the first element of the vector to point to the start of
139 next_string = vector + 1;
142 /* Parse out each component, terminating it with a NULL */
143 while ((nextc = DtStrchr(nextc, separator)))
147 *next_string = nextc;
151 /* The last pointer in the vector must be set to NULL. */
158 * Function Name: _DtFreeStringVector
162 * A "string vector" is an array of pointers to strings.
166 * _DtFreeStringVector (stringv);
168 * char **stringv; The string vector which is freed.
170 * NOTE: this function assumes that "stringv" was created by
171 * "_DtVectorizeInPlace".
185 XtFree ((char *)stringv[0]);
187 XtFree ((char *)stringv);
193 * Functions for mapping a display pointer into a display name. It
194 * special cases 'local:' and 'unix:', and maps these into the real
199 RemapSpecialDisplayName(
202 static char * localHost = NULL;
206 if (localHost == NULL)
208 localHost = XtMalloc((Cardinal)30);
209 DtGetShortHostname(localHost, 30);
211 _DtSvcProcessUnlock();
213 name = XtMalloc((Cardinal)(strlen(localHost) + strlen(dispInfo) + 1));
214 (void)strcpy(name, localHost);
215 (void)strcat(name, dispInfo);
220 * Move here from DragUtil.c because the function is useful and DragUtil.c
227 char * name = DisplayString(display);
229 if (strncmp("unix:", name, 5) == 0)
230 return(RemapSpecialDisplayName(name+4));
231 else if (strncmp("local:", name, 6) == 0)
232 return(RemapSpecialDisplayName(name+5));
233 else if (strncmp(":", name, 1) == 0)
234 return(RemapSpecialDisplayName(name));
236 return(XtNewString(name));