dinput: Mark effect parameters as modified when duration is set.
[wine.git] / tools / tools.h
blob751580d68cba38099ee26dda717ab7fedb2522cb
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
83 struct target
85 enum { CPU_i386, CPU_x86_64, CPU_ARM, CPU_ARM64 } cpu;
87 enum
89 PLATFORM_UNSPECIFIED,
90 PLATFORM_APPLE,
91 PLATFORM_ANDROID,
92 PLATFORM_LINUX,
93 PLATFORM_FREEBSD,
94 PLATFORM_SOLARIS,
95 PLATFORM_WINDOWS,
96 PLATFORM_MINGW,
97 PLATFORM_CYGWIN
98 } platform;
101 static inline void *xmalloc( size_t size )
103 void *res = malloc( size ? size : 1 );
105 if (res == NULL)
107 fprintf( stderr, "Virtual memory exhausted.\n" );
108 exit(1);
110 return res;
113 static inline void *xrealloc (void *ptr, size_t size)
115 void *res = realloc( ptr, size );
117 if (size && res == NULL)
119 fprintf( stderr, "Virtual memory exhausted.\n" );
120 exit(1);
122 return res;
125 static inline char *xstrdup( const char *str )
127 return strcpy( xmalloc( strlen(str)+1 ), str );
130 static inline int strendswith( const char *str, const char *end )
132 int l = strlen( str );
133 int m = strlen( end );
134 return l >= m && !strcmp( str + l - m, end );
137 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
138 static inline char *strmake( const char* fmt, ... )
140 int n;
141 size_t size = 100;
142 va_list ap;
144 for (;;)
146 char *p = xmalloc( size );
147 va_start( ap, fmt );
148 n = vsnprintf( p, size, fmt, ap );
149 va_end( ap );
150 if (n == -1) size *= 2;
151 else if ((size_t)n >= size) size = n + 1;
152 else return p;
153 free( p );
157 /* string array functions */
159 struct strarray
161 unsigned int count; /* strings in use */
162 unsigned int size; /* total allocated size */
163 const char **str;
166 static const struct strarray empty_strarray;
168 static inline void strarray_add( struct strarray *array, const char *str )
170 if (array->count == array->size)
172 if (array->size) array->size *= 2;
173 else array->size = 16;
174 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
176 array->str[array->count++] = str;
179 static inline void strarray_addall( struct strarray *array, struct strarray added )
181 unsigned int i;
183 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
186 static inline int strarray_exists( const struct strarray *array, const char *str )
188 unsigned int i;
190 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
191 return 0;
194 static inline void strarray_add_uniq( struct strarray *array, const char *str )
196 if (!strarray_exists( array, str )) strarray_add( array, str );
199 static inline void strarray_addall_uniq( struct strarray *array, struct strarray added )
201 unsigned int i;
203 for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
206 static inline struct strarray strarray_fromstring( const char *str, const char *delim )
208 struct strarray array = empty_strarray;
209 char *buf = xstrdup( str );
210 const char *tok;
212 for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
213 strarray_add( &array, xstrdup( tok ));
214 free( buf );
215 return array;
218 static inline struct strarray strarray_frompath( const char *path )
220 if (!path) return empty_strarray;
221 #ifdef _WIN32
222 return strarray_fromstring( path, ";" );
223 #else
224 return strarray_fromstring( path, ":" );
225 #endif
228 static inline char *strarray_tostring( struct strarray array, const char *sep )
230 char *str;
231 unsigned int i, len = 1 + (array.count - 1) * strlen(sep);
233 if (!array.count) return xstrdup("");
234 for (i = 0; i < array.count; i++) len += strlen( array.str[i] );
235 str = xmalloc( len );
236 strcpy( str, array.str[0] );
237 for (i = 1; i < array.count; i++)
239 strcat( str, sep );
240 strcat( str, array.str[i] );
242 return str;
245 static inline void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
247 if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
250 static inline const char *strarray_bsearch( const struct strarray *array, const char *str,
251 int (*func)(const char **, const char **) )
253 char **res = NULL;
255 if (array->count) res = bsearch( &str, array->str, array->count, sizeof(*array->str), (void *)func );
256 return res ? *res : NULL;
259 static inline void strarray_trace( struct strarray args )
261 unsigned int i;
263 for (i = 0; i < args.count; i++)
265 if (strpbrk( args.str[i], " \t\n\r")) printf( "\"%s\"", args.str[i] );
266 else printf( "%s", args.str[i] );
267 putchar( i < args.count - 1 ? ' ' : '\n' );
271 static inline int strarray_spawn( struct strarray args )
273 #ifdef _WIN32
274 strarray_add( &args, NULL );
275 return _spawnvp( _P_WAIT, args.str[0], args.str );
276 #else
277 pid_t pid, wret;
278 int status;
280 if (!(pid = fork()))
282 strarray_add( &args, NULL );
283 execvp( args.str[0], (char **)args.str );
284 _exit(1);
286 if (pid == -1) return -1;
288 while (pid != (wret = waitpid( pid, &status, 0 )))
289 if (wret == -1 && errno != EINTR) break;
291 if (pid == wret && WIFEXITED(status)) return WEXITSTATUS(status);
292 return 255; /* abnormal exit with an abort or an interrupt */
293 #endif
296 static inline char *get_basename( const char *file )
298 const char *ret = strrchr( file, '/' );
299 return xstrdup( ret ? ret + 1 : file );
302 static inline char *get_basename_noext( const char *file )
304 char *ext, *ret = get_basename( file );
305 if ((ext = strrchr( ret, '.' ))) *ext = 0;
306 return ret;
309 static inline char *get_dirname( const char *file )
311 const char *end = strrchr( file, '/' );
312 if (!end) return xstrdup( "." );
313 if (end == file) end++;
314 return strmake( "%.*s", (int)(end - file), file );
317 static inline char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
319 int name_len = strlen( name );
321 if (strendswith( name, old_ext )) name_len -= strlen( old_ext );
322 return strmake( "%.*s%s", name_len, name, new_ext );
326 static inline int make_temp_file( const char *prefix, const char *suffix, char **name )
328 static unsigned int value;
329 int fd, count;
330 const char *tmpdir = NULL;
332 if (!prefix) prefix = "tmp";
333 if (!suffix) suffix = "";
334 value += time(NULL) + getpid();
336 for (count = 0; count < 0x8000; count++)
338 if (tmpdir)
339 *name = strmake( "%s/%s-%08x%s", tmpdir, prefix, value, suffix );
340 else
341 *name = strmake( "%s-%08x%s", prefix, value, suffix );
342 fd = open( *name, O_RDWR | O_CREAT | O_EXCL, 0600 );
343 if (fd >= 0) return fd;
344 value += 7777;
345 if (errno == EACCES && !tmpdir && !strchr( prefix, '/' ))
347 if (!(tmpdir = getenv("TMPDIR"))) tmpdir = "/tmp";
349 free( *name );
351 fprintf( stderr, "failed to create temp file for %s%s\n", prefix, suffix );
352 exit(1);
356 static inline struct target get_default_target(void)
358 struct target target;
359 #ifdef __i386__
360 target.cpu = CPU_i386;
361 #elif defined(__x86_64__)
362 target.cpu = CPU_x86_64;
363 #elif defined(__arm__)
364 target.cpu = CPU_ARM;
365 #elif defined(__aarch64__)
366 target.cpu = CPU_ARM64;
367 #else
368 #error Unsupported CPU
369 #endif
371 #ifdef __APPLE__
372 target.platform = PLATFORM_APPLE;
373 #elif defined(__ANDROID__)
374 target.platform = PLATFORM_ANDROID;
375 #elif defined(__linux__)
376 target.platform = PLATFORM_LINUX;
377 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
378 target.platform = PLATFORM_FREEBSD;
379 #elif defined(__sun)
380 target.platform = PLATFORM_SOLARIS;
381 #elif defined(__CYGWIN__)
382 target.platform = PLATFORM_CYGWIN;
383 #elif defined(_WIN32)
384 target.platform = PLATFORM_MINGW;
385 #else
386 target.platform = PLATFORM_UNSPECIFIED;
387 #endif
389 return target;
393 static inline unsigned int get_target_ptr_size( struct target target )
395 static const unsigned int sizes[] =
397 [CPU_i386] = 4,
398 [CPU_x86_64] = 8,
399 [CPU_ARM] = 4,
400 [CPU_ARM64] = 8,
402 return sizes[target.cpu];
406 static inline void set_target_ptr_size( struct target *target, unsigned int size )
408 switch (target->cpu)
410 case CPU_i386:
411 if (size == 8) target->cpu = CPU_x86_64;
412 break;
413 case CPU_x86_64:
414 if (size == 4) target->cpu = CPU_i386;
415 break;
416 case CPU_ARM:
417 if (size == 8) target->cpu = CPU_ARM64;
418 break;
419 case CPU_ARM64:
420 if (size == 4) target->cpu = CPU_ARM;
421 break;
426 static inline int get_cpu_from_name( const char *name )
428 static const struct
430 const char *name;
431 int cpu;
432 } cpu_names[] =
434 { "i386", CPU_i386 },
435 { "i486", CPU_i386 },
436 { "i586", CPU_i386 },
437 { "i686", CPU_i386 },
438 { "i786", CPU_i386 },
439 { "x86_64", CPU_x86_64 },
440 { "amd64", CPU_x86_64 },
441 { "aarch64", CPU_ARM64 },
442 { "arm64", CPU_ARM64 },
443 { "arm", CPU_ARM },
445 unsigned int i;
447 for (i = 0; i < ARRAY_SIZE(cpu_names); i++)
448 if (!strncmp( cpu_names[i].name, name, strlen(cpu_names[i].name) )) return cpu_names[i].cpu;
449 return -1;
453 static inline int get_platform_from_name( const char *name )
455 static const struct
457 const char *name;
458 int platform;
459 } platform_names[] =
461 { "macos", PLATFORM_APPLE },
462 { "darwin", PLATFORM_APPLE },
463 { "android", PLATFORM_ANDROID },
464 { "linux", PLATFORM_LINUX },
465 { "freebsd", PLATFORM_FREEBSD },
466 { "solaris", PLATFORM_SOLARIS },
467 { "mingw32", PLATFORM_MINGW },
468 { "windows-gnu", PLATFORM_MINGW },
469 { "winnt", PLATFORM_MINGW },
470 { "windows", PLATFORM_WINDOWS },
471 { "cygwin", PLATFORM_CYGWIN },
473 unsigned int i;
475 for (i = 0; i < ARRAY_SIZE(platform_names); i++)
476 if (!strncmp( platform_names[i].name, name, strlen(platform_names[i].name) ))
477 return platform_names[i].platform;
478 return -1;
482 static inline const char *get_arch_dir( struct target target )
484 static const char *cpu_names[] =
486 [CPU_i386] = "i386",
487 [CPU_x86_64] = "x86_64",
488 [CPU_ARM] = "arm",
489 [CPU_ARM64] = "aarch64"
492 if (!cpu_names[target.cpu]) return "";
494 switch (target.platform)
496 case PLATFORM_WINDOWS:
497 case PLATFORM_CYGWIN:
498 case PLATFORM_MINGW:
499 return strmake( "/%s-windows", cpu_names[target.cpu] );
500 default:
501 return strmake( "/%s-unix", cpu_names[target.cpu] );
505 static inline int parse_target( const char *name, struct target *target )
507 int res;
508 char *p, *spec = xstrdup( name );
510 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
512 /* get the CPU part */
514 if ((p = strchr( spec, '-' )))
516 *p++ = 0;
517 if ((res = get_cpu_from_name( spec )) == -1)
519 free( spec );
520 return 0;
522 target->cpu = res;
524 else if (!strcmp( spec, "mingw32" ))
526 target->cpu = CPU_i386;
527 p = spec;
529 else
531 free( spec );
532 return 0;
535 /* get the OS part */
537 target->platform = PLATFORM_UNSPECIFIED; /* default value */
538 for (;;)
540 if ((res = get_platform_from_name( p )) != -1)
542 target->platform = res;
543 break;
545 if (!(p = strchr( p, '-' ))) break;
546 p++;
549 free( spec );
550 return 1;
554 static inline struct target init_argv0_target( const char *argv0 )
556 char *name = get_basename( argv0 );
557 struct target target;
559 if (!strchr( name, '-' ) || !parse_target( name, &target ))
560 target = get_default_target();
562 free( name );
563 return target;
567 /* command-line option parsing */
568 /* partly based on the Glibc getopt() implementation */
570 struct long_option
572 const char *name;
573 int has_arg;
574 int val;
577 static inline struct strarray parse_options( int argc, char **argv, const char *short_opts,
578 const struct long_option *long_opts, int long_only,
579 void (*callback)( int, char* ) )
581 struct strarray ret = empty_strarray;
582 const char *flag;
583 char *start, *end;
584 int i;
586 #define OPT_ERR(fmt) { callback( '?', strmake( fmt, argv[1] )); continue; }
588 for (i = 1; i < argc; i++)
590 if (argv[i][0] != '-' || !argv[i][1]) /* not an option */
592 strarray_add( &ret, argv[i] );
593 continue;
595 if (!strcmp( argv[i], "--" ))
597 /* add remaining args */
598 while (++i < argc) strarray_add( &ret, argv[i] );
599 break;
601 start = argv[i] + 1 + (argv[i][1] == '-');
603 if (argv[i][1] == '-' || (long_only && (argv[i][2] || !strchr( short_opts, argv[i][1] ))))
605 /* handle long option */
606 const struct long_option *opt, *found = NULL;
607 int count = 0;
609 if (!(end = strchr( start, '=' ))) end = start + strlen(start);
610 for (opt = long_opts; opt && opt->name; opt++)
612 if (strncmp( opt->name, start, end - start )) continue;
613 if (!opt->name[end - start]) /* exact match */
615 found = opt;
616 count = 1;
617 break;
619 if (!found)
621 found = opt;
622 count++;
624 else if (long_only || found->has_arg != opt->has_arg || found->val != opt->val)
626 count++;
630 if (count > 1) OPT_ERR( "option '%s' is ambiguous" );
632 if (found)
634 if (*end)
636 if (!found->has_arg) OPT_ERR( "argument not allowed in '%s'" );
637 end++; /* skip '=' */
639 else if (found->has_arg == 1)
641 if (i == argc - 1) OPT_ERR( "option '%s' requires an argument" );
642 end = argv[++i];
644 else end = NULL;
646 callback( found->val, end );
647 continue;
649 if (argv[i][1] == '-' || !long_only || !strchr( short_opts, argv[i][1] ))
650 OPT_ERR( "unrecognized option '%s'" );
653 /* handle short option */
654 for ( ; *start; start++)
656 if (!(flag = strchr( short_opts, *start ))) OPT_ERR( "invalid option '%s'" );
657 if (flag[1] == ':')
659 end = start + 1;
660 if (!*end) end = NULL;
661 if (flag[2] != ':' && !end)
663 if (i == argc - 1) OPT_ERR( "option '%s' requires an argument" );
664 end = argv[++i];
666 callback( *start, end );
667 break;
669 callback( *start, NULL );
672 return ret;
673 #undef OPT_ERR
676 #endif /* __WINE_TOOLS_H */