user32: Set IMM active context on focus change.
[wine.git] / tools / tools.h
blobf1240370b46bc0245c4f92b2f8bea85a42cd46b8
1 /*
2 * Helper functions for the Wine tools
4 * Copyright 2021 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_TOOLS_H
22 #define __WINE_TOOLS_H
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <errno.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
37 #ifdef _WIN32
38 # include <direct.h>
39 # include <io.h>
40 # include <process.h>
41 # define mkdir(path,mode) mkdir(path)
42 # ifndef S_ISREG
43 # define S_ISREG(mod) (((mod) & _S_IFMT) == _S_IFREG)
44 # endif
45 # ifdef _MSC_VER
46 # define popen _popen
47 # define pclose _pclose
48 # define strtoll _strtoi64
49 # define strtoull _strtoui64
50 # define strncasecmp _strnicmp
51 # define strcasecmp _stricmp
52 # endif
53 #else
54 # include <sys/wait.h>
55 # include <unistd.h>
56 # ifndef O_BINARY
57 # define O_BINARY 0
58 # endif
59 # ifndef __int64
60 # if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
61 # define __int64 long
62 # else
63 # define __int64 long long
64 # endif
65 # endif
66 #endif
68 #if !defined(__GNUC__) && !defined(__attribute__)
69 #define __attribute__(x)
70 #endif
72 #ifndef max
73 #define max(a,b) (((a) > (b)) ? (a) : (b))
74 #endif
75 #ifndef min
76 #define min(a,b) (((a) < (b)) ? (a) : (b))
77 #endif
79 #ifndef ARRAY_SIZE
80 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
81 #endif
84 static inline void *xmalloc( size_t size )
86 void *res = malloc( size ? size : 1 );
88 if (res == NULL)
90 fprintf( stderr, "Virtual memory exhausted.\n" );
91 exit(1);
93 return res;
96 static inline void *xrealloc (void *ptr, size_t size)
98 void *res = realloc( ptr, size );
100 if (size && res == NULL)
102 fprintf( stderr, "Virtual memory exhausted.\n" );
103 exit(1);
105 return res;
108 static inline char *xstrdup( const char *str )
110 return strcpy( xmalloc( strlen(str)+1 ), str );
113 static inline int strendswith( const char *str, const char *end )
115 int l = strlen( str );
116 int m = strlen( end );
117 return l >= m && !strcmp( str + l - m, end );
120 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
121 static inline char *strmake( const char* fmt, ... )
123 int n;
124 size_t size = 100;
125 va_list ap;
127 for (;;)
129 char *p = xmalloc( size );
130 va_start( ap, fmt );
131 n = vsnprintf( p, size, fmt, ap );
132 va_end( ap );
133 if (n == -1) size *= 2;
134 else if ((size_t)n >= size) size = n + 1;
135 else return p;
136 free( p );
140 /* string array functions */
142 struct strarray
144 unsigned int count; /* strings in use */
145 unsigned int size; /* total allocated size */
146 const char **str;
149 static const struct strarray empty_strarray;
151 static inline void strarray_add( struct strarray *array, const char *str )
153 if (array->count == array->size)
155 if (array->size) array->size *= 2;
156 else array->size = 16;
157 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
159 array->str[array->count++] = str;
162 static inline void strarray_addall( struct strarray *array, struct strarray added )
164 unsigned int i;
166 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
169 static inline int strarray_exists( const struct strarray *array, const char *str )
171 unsigned int i;
173 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
174 return 0;
177 static inline void strarray_add_uniq( struct strarray *array, const char *str )
179 if (!strarray_exists( array, str )) strarray_add( array, str );
182 static inline void strarray_addall_uniq( struct strarray *array, struct strarray added )
184 unsigned int i;
186 for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
189 static inline struct strarray strarray_fromstring( const char *str, const char *delim )
191 struct strarray array = empty_strarray;
192 char *buf = xstrdup( str );
193 const char *tok;
195 for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
196 strarray_add( &array, xstrdup( tok ));
197 free( buf );
198 return array;
201 static inline struct strarray strarray_frompath( const char *path )
203 if (!path) return empty_strarray;
204 #ifdef _WIN32
205 return strarray_fromstring( path, ";" );
206 #else
207 return strarray_fromstring( path, ":" );
208 #endif
211 static inline char *strarray_tostring( struct strarray array, const char *sep )
213 char *str;
214 unsigned int i, len = 1 + (array.count - 1) * strlen(sep);
216 if (!array.count) return xstrdup("");
217 for (i = 0; i < array.count; i++) len += strlen( array.str[i] );
218 str = xmalloc( len );
219 strcpy( str, array.str[0] );
220 for (i = 1; i < array.count; i++)
222 strcat( str, sep );
223 strcat( str, array.str[i] );
225 return str;
228 static inline void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
230 if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
233 static inline const char *strarray_bsearch( const struct strarray *array, const char *str,
234 int (*func)(const char **, const char **) )
236 char **res = NULL;
238 if (array->count) res = bsearch( &str, array->str, array->count, sizeof(*array->str), (void *)func );
239 return res ? *res : NULL;
242 static inline void strarray_trace( struct strarray args )
244 unsigned int i;
246 for (i = 0; i < args.count; i++)
248 if (strpbrk( args.str[i], " \t\n\r")) printf( "\"%s\"", args.str[i] );
249 else printf( "%s", args.str[i] );
250 putchar( i < args.count - 1 ? ' ' : '\n' );
254 static inline int strarray_spawn( struct strarray args )
256 #ifdef _WIN32
257 strarray_add( &args, NULL );
258 return _spawnvp( _P_WAIT, args.str[0], args.str );
259 #else
260 pid_t pid, wret;
261 int status;
263 if (!(pid = fork()))
265 strarray_add( &args, NULL );
266 execvp( args.str[0], (char **)args.str );
267 _exit(1);
269 if (pid == -1) return -1;
271 while (pid != (wret = waitpid( pid, &status, 0 )))
272 if (wret == -1 && errno != EINTR) break;
274 if (pid == wret && WIFEXITED(status)) return WEXITSTATUS(status);
275 return 255; /* abnormal exit with an abort or an interrupt */
276 #endif
279 static inline char *get_basename( const char *file )
281 const char *ret = strrchr( file, '/' );
282 return xstrdup( ret ? ret + 1 : file );
285 static inline char *get_basename_noext( const char *file )
287 char *ext, *ret = get_basename( file );
288 if ((ext = strrchr( ret, '.' ))) *ext = 0;
289 return ret;
292 static inline char *get_dirname( const char *file )
294 const char *end = strrchr( file, '/' );
295 if (!end) return xstrdup( "." );
296 if (end == file) end++;
297 return strmake( "%.*s", (int)(end - file), file );
300 static inline char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
302 int name_len = strlen( name );
304 if (strendswith( name, old_ext )) name_len -= strlen( old_ext );
305 return strmake( "%.*s%s", name_len, name, new_ext );
309 static inline int make_temp_file( const char *prefix, const char *suffix, char **name )
311 static unsigned int value;
312 int fd, count;
313 const char *tmpdir = NULL;
315 if (!prefix) prefix = "tmp";
316 if (!suffix) suffix = "";
317 value += time(NULL) + getpid();
319 for (count = 0; count < 0x8000; count++)
321 if (tmpdir)
322 *name = strmake( "%s/%s-%08x%s", tmpdir, prefix, value, suffix );
323 else
324 *name = strmake( "%s-%08x%s", prefix, value, suffix );
325 fd = open( *name, O_RDWR | O_CREAT | O_EXCL, 0600 );
326 if (fd >= 0) return fd;
327 value += 7777;
328 if (errno == EACCES && !tmpdir && !strchr( prefix, '/' ))
330 if (!(tmpdir = getenv("TMPDIR"))) tmpdir = "/tmp";
332 free( *name );
334 fprintf( stderr, "failed to create temp file for %s%s\n", prefix, suffix );
335 exit(1);
339 /* command-line option parsing */
340 /* partly based on the Glibc getopt() implementation */
342 struct long_option
344 const char *name;
345 int has_arg;
346 int val;
349 static inline struct strarray parse_options( int argc, char **argv, const char *short_opts,
350 const struct long_option *long_opts, int long_only,
351 void (*callback)( int, char* ) )
353 struct strarray ret = empty_strarray;
354 const char *flag;
355 char *start, *end;
356 int i;
358 #define OPT_ERR(fmt) { callback( '?', strmake( fmt, argv[1] )); continue; }
360 for (i = 1; i < argc; i++)
362 if (argv[i][0] != '-' || !argv[i][1]) /* not an option */
364 strarray_add( &ret, argv[i] );
365 continue;
367 if (!strcmp( argv[i], "--" ))
369 /* add remaining args */
370 while (++i < argc) strarray_add( &ret, argv[i] );
371 break;
373 start = argv[i] + 1 + (argv[i][1] == '-');
375 if (argv[i][1] == '-' || (long_only && (argv[i][2] || !strchr( short_opts, argv[i][1] ))))
377 /* handle long option */
378 const struct long_option *opt, *found = NULL;
379 int count = 0;
381 if (!(end = strchr( start, '=' ))) end = start + strlen(start);
382 for (opt = long_opts; opt && opt->name; opt++)
384 if (strncmp( opt->name, start, end - start )) continue;
385 if (!opt->name[end - start]) /* exact match */
387 found = opt;
388 count = 1;
389 break;
391 if (!found)
393 found = opt;
394 count++;
396 else if (long_only || found->has_arg != opt->has_arg || found->val != opt->val)
398 count++;
402 if (count > 1) OPT_ERR( "option '%s' is ambiguous" );
404 if (found)
406 if (*end)
408 if (!found->has_arg) OPT_ERR( "argument not allowed in '%s'" );
409 end++; /* skip '=' */
411 else if (found->has_arg == 1)
413 if (i == argc - 1) OPT_ERR( "option '%s' requires an argument" );
414 end = argv[++i];
416 else end = NULL;
418 callback( found->val, end );
419 continue;
421 if (argv[i][1] == '-' || !long_only || !strchr( short_opts, argv[i][1] ))
422 OPT_ERR( "unrecognized option '%s'" );
425 /* handle short option */
426 for ( ; *start; start++)
428 if (!(flag = strchr( short_opts, *start ))) OPT_ERR( "invalid option '%s'" );
429 if (flag[1] == ':')
431 end = start + 1;
432 if (!*end) end = NULL;
433 if (flag[2] != ':' && !end)
435 if (i == argc - 1) OPT_ERR( "option '%s' requires an argument" );
436 end = argv[++i];
438 callback( *start, end );
439 break;
441 callback( *start, NULL );
444 return ret;
445 #undef OPT_ERR
448 #endif /* __WINE_TOOLS_H */