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
23 /******************************************************************************
24 ******************************************************************************
27 ** RCS: $XConsortium: util.c /main/3 1996/10/30 11:16:26 drk $
29 ** Description: Utility code for the dtpdmd
31 ** (c) Copyright 1995, 1996, Hewlett-Packard Company, all rights reserved.
33 ******************************************************************************
34 *****************************************************************************/
42 jmp_buf xio_quickie_jmp_buf;
44 /******************************************************************************
46 * Misc utility routines.
49 int xio_quickie_handler( Display *dpy )
51 longjmp( xio_quickie_jmp_buf, 1 );
54 /***************************************
56 * str_dup using Xmalloc
58 char *xpstrdup(char * str)
63 len = strlen(str) + 1;
64 newstr = (char *) Xmalloc( len );
65 memcpy( newstr, str, len );
69 /***************************************
71 * Multi-byte capable version of strspn(s1, s2).
72 * Returns the span of characters in s1 contained in s2.
73 * Only s1 can be multibyte.
81 wchar_t s1char, s2char;
89 /* A Null string has no spans */
96 /* Extract the next character from s1; may be multibyte */
97 if ((s1len = mbtowc(&s1char, s1, MB_CUR_MAX)) < 0)
102 * Compare this character against all the chars in s2. Keep
103 * working through s1, until a character is found in s1 which
104 * is not contained in s2.
110 /* Extract the next character from s2; cannot be multibyte */
113 /* If a match is found, keep processing s1 */
114 if (s1char == s2char)
123 * If we made it here because all of s2 was searched, and a match
124 * was not found against s1, then we are done.
132 return(strspn(s1, s2));
136 /***************************************
138 * Multi-byte capable version of strcspn(s1, s2).
139 * Returns the span of characters in s1 not contained in s2.
140 * Only s1 can be multibyte.
148 wchar_t s1char, s2char;
155 /* An empty string has no spans */
162 /* Extract the next character from s1; may be multibyte */
163 if ((s1len = mbtowc(&s1char, s1, MB_CUR_MAX)) < 0)
168 * Compare this character against all the chars in s2. Keep
169 * working through s1, until a character is found in s1 which
170 * is contained in s2.
175 /* Extract the next character from s2; cannot be multibyte */
178 /* If a match occurs, then we are done */
179 if (s1char == s2char)
184 * If we've made it here, then we searched all of s2, and none of
185 * its components matched s1; continue with the next character
193 return(strcspn(s1, s2));
197 /***************************************
199 * Multi-byte capable version of strtok(s1, s2).
200 * Returns a pointer to the span of characters in s1 terminated by
201 * one of the characters in s2. Only s1 can be multibyte.
209 static char * ptr = NULL;
216 * If this is the first call, save the string pointer, and bypass
217 * any leading separators.
220 ptr = s1 + xpstrspn(s1, s2);
222 /* A Null string pointer has no tokens */
226 /* Find out where the first terminator is */
227 if ((len = xpstrcspn(ptr, s2)) <= 0)
233 /* Keep track of where the token started */
236 /* Null out the terminator; we need to know how many bytes are
237 * occupied by the terminator, so that we can skip over it to
238 * the next character.
240 offset = mblen(ptr + len, MB_CUR_MAX);
242 ptr += (len + offset);
245 * In preparation for the next pass, skip any other occurrences of
246 * the terminator characters which were joined with the terminator
247 * we first encountered.
249 len = xpstrspn(ptr, s2);
254 return(strtok(s1, s2));
258 void xp_add_argv( char ***argv, char *str )
263 for ( i = 0; (*argv)[i]; i++ );
264 *argv = (char **) Xrealloc( (char *) *argv, sizeof(char *) * (i + 2) );
268 *argv = (char **) Xmalloc( sizeof(char *) * 2 );
272 (*argv)[i+1] = (char *) NULL;