winegstreamer: Add stubs for stream sink.
[wine.git] / tools / tools.h
blobfa459994eb360fa621fae03bfd85aef6da60fcde
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 #ifndef __WINE_CONFIG_H
25 # error You must include config.h to use this header
26 #endif
28 #include <limits.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <signal.h>
36 #include <fcntl.h>
37 #include <time.h>
38 #include <errno.h>
39 #ifdef HAVE_SYS_SYSCTL_H
40 # include <sys/sysctl.h>
41 #endif
43 #ifdef _WIN32
44 # include <direct.h>
45 # include <io.h>
46 # include <process.h>
47 # define mkdir(path,mode) mkdir(path)
48 # ifndef S_ISREG
49 # define S_ISREG(mod) (((mod) & _S_IFMT) == _S_IFREG)
50 # endif
51 # ifdef _MSC_VER
52 # define popen _popen
53 # define pclose _pclose
54 # define strtoll _strtoi64
55 # define strtoull _strtoui64
56 # define strncasecmp _strnicmp
57 # define strcasecmp _stricmp
58 # endif
59 #else
60 # include <sys/wait.h>
61 # include <unistd.h>
62 # ifndef O_BINARY
63 # define O_BINARY 0
64 # endif
65 # ifndef __int64
66 # if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
67 # define __int64 long
68 # else
69 # define __int64 long long
70 # endif
71 # endif
72 #endif
74 #if !defined(__GNUC__) && !defined(__attribute__)
75 #define __attribute__(x)
76 #endif
78 #ifndef max
79 #define max(a,b) (((a) > (b)) ? (a) : (b))
80 #endif
81 #ifndef min
82 #define min(a,b) (((a) < (b)) ? (a) : (b))
83 #endif
85 #ifndef ARRAY_SIZE
86 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
87 #endif
89 struct target
91 enum { CPU_i386, CPU_x86_64, CPU_ARM, CPU_ARM64 } cpu;
93 enum
95 PLATFORM_UNSPECIFIED,
96 PLATFORM_APPLE,
97 PLATFORM_ANDROID,
98 PLATFORM_LINUX,
99 PLATFORM_FREEBSD,
100 PLATFORM_SOLARIS,
101 PLATFORM_WINDOWS,
102 PLATFORM_MINGW,
103 PLATFORM_CYGWIN
104 } platform;
107 static inline void *xmalloc( size_t size )
109 void *res = malloc( size ? size : 1 );
111 if (res == NULL)
113 fprintf( stderr, "Virtual memory exhausted.\n" );
114 exit(1);
116 return res;
119 static inline void *xrealloc (void *ptr, size_t size)
121 void *res = realloc( ptr, size );
123 if (size && res == NULL)
125 fprintf( stderr, "Virtual memory exhausted.\n" );
126 exit(1);
128 return res;
131 static inline char *xstrdup( const char *str )
133 return strcpy( xmalloc( strlen(str)+1 ), str );
136 static inline int strendswith( const char *str, const char *end )
138 int l = strlen( str );
139 int m = strlen( end );
140 return l >= m && !strcmp( str + l - m, end );
143 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
144 static inline char *strmake( const char* fmt, ... )
146 int n;
147 size_t size = 100;
148 va_list ap;
150 for (;;)
152 char *p = xmalloc( size );
153 va_start( ap, fmt );
154 n = vsnprintf( p, size, fmt, ap );
155 va_end( ap );
156 if (n == -1) size *= 2;
157 else if ((size_t)n >= size) size = n + 1;
158 else return p;
159 free( p );
163 /* string array functions */
165 struct strarray
167 unsigned int count; /* strings in use */
168 unsigned int size; /* total allocated size */
169 const char **str;
172 static const struct strarray empty_strarray;
174 static inline void strarray_add( struct strarray *array, const char *str )
176 if (array->count == array->size)
178 if (array->size) array->size *= 2;
179 else array->size = 16;
180 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
182 array->str[array->count++] = str;
185 static inline void strarray_addall( struct strarray *array, struct strarray added )
187 unsigned int i;
189 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
192 static inline int strarray_exists( const struct strarray *array, const char *str )
194 unsigned int i;
196 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
197 return 0;
200 static inline void strarray_add_uniq( struct strarray *array, const char *str )
202 if (!strarray_exists( array, str )) strarray_add( array, str );
205 static inline void strarray_addall_uniq( struct strarray *array, struct strarray added )
207 unsigned int i;
209 for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
212 static inline struct strarray strarray_fromstring( const char *str, const char *delim )
214 struct strarray array = empty_strarray;
215 char *buf = xstrdup( str );
216 const char *tok;
218 for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
219 strarray_add( &array, xstrdup( tok ));
220 free( buf );
221 return array;
224 static inline struct strarray strarray_frompath( const char *path )
226 if (!path) return empty_strarray;
227 #ifdef _WIN32
228 return strarray_fromstring( path, ";" );
229 #else
230 return strarray_fromstring( path, ":" );
231 #endif
234 static inline char *strarray_tostring( struct strarray array, const char *sep )
236 char *str;
237 unsigned int i, len = 1 + (array.count - 1) * strlen(sep);
239 if (!array.count) return xstrdup("");
240 for (i = 0; i < array.count; i++) len += strlen( array.str[i] );
241 str = xmalloc( len );
242 strcpy( str, array.str[0] );
243 for (i = 1; i < array.count; i++)
245 strcat( str, sep );
246 strcat( str, array.str[i] );
248 return str;
251 static inline void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
253 if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
256 static inline const char *strarray_bsearch( const struct strarray *array, const char *str,
257 int (*func)(const char **, const char **) )
259 char **res = NULL;
261 if (array->count) res = bsearch( &str, array->str, array->count, sizeof(*array->str), (void *)func );
262 return res ? *res : NULL;
265 static inline void strarray_trace( struct strarray args )
267 unsigned int i;
269 for (i = 0; i < args.count; i++)
271 if (strpbrk( args.str[i], " \t\n\r")) printf( "\"%s\"", args.str[i] );
272 else printf( "%s", args.str[i] );
273 putchar( i < args.count - 1 ? ' ' : '\n' );
277 static inline int strarray_spawn( struct strarray args )
279 #ifdef _WIN32
280 strarray_add( &args, NULL );
281 return _spawnvp( _P_WAIT, args.str[0], args.str );
282 #else
283 pid_t pid, wret;
284 int status;
286 if (!(pid = fork()))
288 strarray_add( &args, NULL );
289 execvp( args.str[0], (char **)args.str );
290 _exit(1);
292 if (pid == -1) return -1;
294 while (pid != (wret = waitpid( pid, &status, 0 )))
295 if (wret == -1 && errno != EINTR) break;
297 if (pid == wret && WIFEXITED(status)) return WEXITSTATUS(status);
298 return 255; /* abnormal exit with an abort or an interrupt */
299 #endif
302 static inline char *get_basename( const char *file )
304 const char *ret = strrchr( file, '/' );
305 return xstrdup( ret ? ret + 1 : file );
308 static inline char *get_basename_noext( const char *file )
310 char *ext, *ret = get_basename( file );
311 if ((ext = strrchr( ret, '.' ))) *ext = 0;
312 return ret;
315 static inline char *get_dirname( const char *file )
317 const char *end = strrchr( file, '/' );
318 if (!end) return xstrdup( "." );
319 if (end == file) end++;
320 return strmake( "%.*s", (int)(end - file), file );
323 static inline char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
325 int name_len = strlen( name );
327 if (strendswith( name, old_ext )) name_len -= strlen( old_ext );
328 return strmake( "%.*s%s", name_len, name, new_ext );
331 /* temp files management */
333 extern const char *temp_dir;
334 extern struct strarray temp_files;
336 static inline char *make_temp_dir(void)
338 unsigned int value = time(NULL) + getpid();
339 int count;
340 char *name;
341 const char *tmpdir = NULL;
343 for (count = 0; count < 0x8000; count++)
345 if (tmpdir)
346 name = strmake( "%s/tmp%08x", tmpdir, value );
347 else
348 name = strmake( "tmp%08x", value );
349 if (!mkdir( name, 0700 )) return name;
350 value += 7777;
351 if (errno == EACCES && !tmpdir)
353 if (!(tmpdir = getenv("TMPDIR"))) tmpdir = "/tmp";
355 free( name );
357 fprintf( stderr, "failed to create directory for temp files\n" );
358 exit(1);
361 static inline char *make_temp_file( const char *prefix, const char *suffix )
363 static unsigned int value;
364 int fd, count;
365 char *name;
367 if (!temp_dir) temp_dir = make_temp_dir();
368 if (!suffix) suffix = "";
369 if (!prefix) prefix = "tmp";
370 else prefix = get_basename_noext( prefix );
372 for (count = 0; count < 0x8000; count++)
374 name = strmake( "%s/%s-%08x%s", temp_dir, prefix, value++, suffix );
375 fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0600 );
376 if (fd >= 0)
378 #ifdef HAVE_SIGPROCMASK /* block signals while manipulating the temp files list */
379 sigset_t mask_set, old_set;
381 sigemptyset( &mask_set );
382 sigaddset( &mask_set, SIGHUP );
383 sigaddset( &mask_set, SIGTERM );
384 sigaddset( &mask_set, SIGINT );
385 sigprocmask( SIG_BLOCK, &mask_set, &old_set );
386 strarray_add( &temp_files, name );
387 sigprocmask( SIG_SETMASK, &old_set, NULL );
388 #else
389 strarray_add( &temp_files, name );
390 #endif
391 close( fd );
392 return name;
394 free( name );
396 fprintf( stderr, "failed to create temp file for %s%s in %s\n", prefix, suffix, temp_dir );
397 exit(1);
400 static inline void remove_temp_files(void)
402 unsigned int i;
404 for (i = 0; i < temp_files.count; i++) if (temp_files.str[i]) unlink( temp_files.str[i] );
405 if (temp_dir) rmdir( temp_dir );
409 static inline void init_signals( void (*cleanup)(int) )
411 signal( SIGTERM, cleanup );
412 signal( SIGINT, cleanup );
413 #ifdef SIGHUP
414 signal( SIGHUP, cleanup );
415 #endif
419 static inline void *read_file( const char *name, size_t *size )
421 struct stat st;
422 int res, fd;
423 void *data;
425 if ((fd = open( name, O_RDONLY | O_BINARY )) == -1) return NULL;
426 fstat( fd, &st );
427 data = xmalloc( st.st_size );
428 res = read( fd, data, st.st_size );
429 if (res == -1)
431 free( data );
432 data = NULL;
433 *size = 0;
435 else *size = res;
436 close( fd );
437 return data;
441 static inline struct target get_default_target(void)
443 struct target target;
444 #ifdef __i386__
445 target.cpu = CPU_i386;
446 #elif defined(__x86_64__)
447 target.cpu = CPU_x86_64;
448 #elif defined(__arm__)
449 target.cpu = CPU_ARM;
450 #elif defined(__aarch64__)
451 target.cpu = CPU_ARM64;
452 #else
453 #error Unsupported CPU
454 #endif
456 #ifdef __APPLE__
457 target.platform = PLATFORM_APPLE;
458 #elif defined(__ANDROID__)
459 target.platform = PLATFORM_ANDROID;
460 #elif defined(__linux__)
461 target.platform = PLATFORM_LINUX;
462 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
463 target.platform = PLATFORM_FREEBSD;
464 #elif defined(__sun)
465 target.platform = PLATFORM_SOLARIS;
466 #elif defined(__CYGWIN__)
467 target.platform = PLATFORM_CYGWIN;
468 #elif defined(_WIN32)
469 target.platform = PLATFORM_MINGW;
470 #else
471 target.platform = PLATFORM_UNSPECIFIED;
472 #endif
474 return target;
478 static inline unsigned int get_target_ptr_size( struct target target )
480 static const unsigned int sizes[] =
482 [CPU_i386] = 4,
483 [CPU_x86_64] = 8,
484 [CPU_ARM] = 4,
485 [CPU_ARM64] = 8,
487 return sizes[target.cpu];
491 static inline void set_target_ptr_size( struct target *target, unsigned int size )
493 switch (target->cpu)
495 case CPU_i386:
496 if (size == 8) target->cpu = CPU_x86_64;
497 break;
498 case CPU_x86_64:
499 if (size == 4) target->cpu = CPU_i386;
500 break;
501 case CPU_ARM:
502 if (size == 8) target->cpu = CPU_ARM64;
503 break;
504 case CPU_ARM64:
505 if (size == 4) target->cpu = CPU_ARM;
506 break;
511 static inline int get_cpu_from_name( const char *name )
513 static const struct
515 const char *name;
516 int cpu;
517 } cpu_names[] =
519 { "i386", CPU_i386 },
520 { "i486", CPU_i386 },
521 { "i586", CPU_i386 },
522 { "i686", CPU_i386 },
523 { "i786", CPU_i386 },
524 { "x86_64", CPU_x86_64 },
525 { "amd64", CPU_x86_64 },
526 { "aarch64", CPU_ARM64 },
527 { "arm64", CPU_ARM64 },
528 { "arm", CPU_ARM },
530 unsigned int i;
532 for (i = 0; i < ARRAY_SIZE(cpu_names); i++)
533 if (!strncmp( cpu_names[i].name, name, strlen(cpu_names[i].name) )) return cpu_names[i].cpu;
534 return -1;
538 static inline int get_platform_from_name( const char *name )
540 static const struct
542 const char *name;
543 int platform;
544 } platform_names[] =
546 { "macos", PLATFORM_APPLE },
547 { "darwin", PLATFORM_APPLE },
548 { "android", PLATFORM_ANDROID },
549 { "linux", PLATFORM_LINUX },
550 { "freebsd", PLATFORM_FREEBSD },
551 { "solaris", PLATFORM_SOLARIS },
552 { "mingw32", PLATFORM_MINGW },
553 { "windows-gnu", PLATFORM_MINGW },
554 { "winnt", PLATFORM_MINGW },
555 { "windows", PLATFORM_WINDOWS },
556 { "cygwin", PLATFORM_CYGWIN },
558 unsigned int i;
560 for (i = 0; i < ARRAY_SIZE(platform_names); i++)
561 if (!strncmp( platform_names[i].name, name, strlen(platform_names[i].name) ))
562 return platform_names[i].platform;
563 return -1;
567 static inline const char *get_arch_dir( struct target target )
569 static const char *cpu_names[] =
571 [CPU_i386] = "i386",
572 [CPU_x86_64] = "x86_64",
573 [CPU_ARM] = "arm",
574 [CPU_ARM64] = "aarch64"
577 if (!cpu_names[target.cpu]) return "";
579 switch (target.platform)
581 case PLATFORM_WINDOWS:
582 case PLATFORM_CYGWIN:
583 case PLATFORM_MINGW:
584 return strmake( "/%s-windows", cpu_names[target.cpu] );
585 default:
586 return strmake( "/%s-unix", cpu_names[target.cpu] );
590 static inline int parse_target( const char *name, struct target *target )
592 int res;
593 char *p, *spec = xstrdup( name );
595 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
597 /* get the CPU part */
599 if ((p = strchr( spec, '-' )))
601 *p++ = 0;
602 if ((res = get_cpu_from_name( spec )) == -1)
604 free( spec );
605 return 0;
607 target->cpu = res;
609 else if (!strcmp( spec, "mingw32" ))
611 target->cpu = CPU_i386;
612 p = spec;
614 else
616 free( spec );
617 return 0;
620 /* get the OS part */
622 target->platform = PLATFORM_UNSPECIFIED; /* default value */
623 for (;;)
625 if ((res = get_platform_from_name( p )) != -1)
627 target->platform = res;
628 break;
630 if (!(p = strchr( p, '-' ))) break;
631 p++;
634 free( spec );
635 return 1;
639 static inline struct target init_argv0_target( const char *argv0 )
641 char *name = get_basename( argv0 );
642 struct target target;
644 if (!strchr( name, '-' ) || !parse_target( name, &target ))
645 target = get_default_target();
647 free( name );
648 return target;
652 static inline char *get_argv0_dir( const char *argv0 )
654 #ifndef _WIN32
655 char *dir = NULL;
657 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
658 dir = realpath( "/proc/self/exe", NULL );
659 #elif defined (__FreeBSD__) || defined(__DragonFly__)
660 static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
661 size_t path_size = PATH_MAX;
662 char *path = xmalloc( path_size );
663 if (!sysctl( pathname, ARRAY_SIZE(pathname), path, &path_size, NULL, 0 ))
664 dir = realpath( path, NULL );
665 free( path );
666 #endif
667 if (!dir && !(dir = realpath( argv0, NULL ))) return NULL;
668 return get_dirname( dir );
669 #else
670 return get_dirname( argv0 );
671 #endif
675 /* output buffer management */
677 extern unsigned char *output_buffer;
678 extern size_t output_buffer_pos;
679 extern size_t output_buffer_size;
681 static inline void check_output_buffer_space( size_t size )
683 if (output_buffer_pos + size >= output_buffer_size)
685 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
686 output_buffer = xrealloc( output_buffer, output_buffer_size );
690 static inline void init_output_buffer(void)
692 output_buffer_size = 1024;
693 output_buffer_pos = 0;
694 output_buffer = xmalloc( output_buffer_size );
697 static inline void put_data( const void *data, size_t size )
699 check_output_buffer_space( size );
700 memcpy( output_buffer + output_buffer_pos, data, size );
701 output_buffer_pos += size;
704 static inline void put_byte( unsigned char val )
706 check_output_buffer_space( 1 );
707 output_buffer[output_buffer_pos++] = val;
710 static inline void put_word( unsigned short val )
712 check_output_buffer_space( 2 );
713 output_buffer[output_buffer_pos++] = val;
714 output_buffer[output_buffer_pos++] = val >> 8;
717 static inline void put_dword( unsigned int val )
719 check_output_buffer_space( 4 );
720 output_buffer[output_buffer_pos++] = val;
721 output_buffer[output_buffer_pos++] = val >> 8;
722 output_buffer[output_buffer_pos++] = val >> 16;
723 output_buffer[output_buffer_pos++] = val >> 24;
726 static inline void put_qword( unsigned int val )
728 put_dword( val );
729 put_dword( 0 );
732 static inline void align_output( unsigned int align )
734 size_t size = align - (output_buffer_pos % align);
736 if (size == align) return;
737 check_output_buffer_space( size );
738 memset( output_buffer + output_buffer_pos, 0, size );
739 output_buffer_pos += size;
742 static inline void flush_output_buffer( const char *name )
744 int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
746 if (fd == -1 || write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
748 perror( name );
749 exit(1);
751 close( fd );
752 free( output_buffer );
755 /* command-line option parsing */
756 /* partly based on the Glibc getopt() implementation */
758 struct long_option
760 const char *name;
761 int has_arg;
762 int val;
765 static inline struct strarray parse_options( int argc, char **argv, const char *short_opts,
766 const struct long_option *long_opts, int long_only,
767 void (*callback)( int, char* ) )
769 struct strarray ret = empty_strarray;
770 const char *flag;
771 char *start, *end;
772 int i;
774 #define OPT_ERR(fmt) { callback( '?', strmake( fmt, argv[1] )); continue; }
776 for (i = 1; i < argc; i++)
778 if (argv[i][0] != '-' || !argv[i][1]) /* not an option */
780 strarray_add( &ret, argv[i] );
781 continue;
783 if (!strcmp( argv[i], "--" ))
785 /* add remaining args */
786 while (++i < argc) strarray_add( &ret, argv[i] );
787 break;
789 start = argv[i] + 1 + (argv[i][1] == '-');
791 if (argv[i][1] == '-' || (long_only && (argv[i][2] || !strchr( short_opts, argv[i][1] ))))
793 /* handle long option */
794 const struct long_option *opt, *found = NULL;
795 int count = 0;
797 if (!(end = strchr( start, '=' ))) end = start + strlen(start);
798 for (opt = long_opts; opt && opt->name; opt++)
800 if (strncmp( opt->name, start, end - start )) continue;
801 if (!opt->name[end - start]) /* exact match */
803 found = opt;
804 count = 1;
805 break;
807 if (!found)
809 found = opt;
810 count++;
812 else if (long_only || found->has_arg != opt->has_arg || found->val != opt->val)
814 count++;
818 if (count > 1) OPT_ERR( "option '%s' is ambiguous" );
820 if (found)
822 if (*end)
824 if (!found->has_arg) OPT_ERR( "argument not allowed in '%s'" );
825 end++; /* skip '=' */
827 else if (found->has_arg == 1)
829 if (i == argc - 1) OPT_ERR( "option '%s' requires an argument" );
830 end = argv[++i];
832 else end = NULL;
834 callback( found->val, end );
835 continue;
837 if (argv[i][1] == '-' || !long_only || !strchr( short_opts, argv[i][1] ))
838 OPT_ERR( "unrecognized option '%s'" );
841 /* handle short option */
842 for ( ; *start; start++)
844 if (!(flag = strchr( short_opts, *start ))) OPT_ERR( "invalid option '%s'" );
845 if (flag[1] == ':')
847 end = start + 1;
848 if (!*end) end = NULL;
849 if (flag[2] != ':' && !end)
851 if (i == argc - 1) OPT_ERR( "option '%s' requires an argument" );
852 end = argv[++i];
854 callback( *start, end );
855 break;
857 callback( *start, NULL );
860 return ret;
861 #undef OPT_ERR
864 #endif /* __WINE_TOOLS_H */