Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/tools.h
4386 views
1
/*
2
* Helper functions for the Wine tools
3
*
4
* Copyright 2021 Alexandre Julliard
5
*
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.
10
*
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.
15
*
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
19
*/
20
21
#ifndef __WINE_TOOLS_H
22
#define __WINE_TOOLS_H
23
24
#ifndef __WINE_CONFIG_H
25
# error You must include config.h to use this header
26
#endif
27
28
#include <limits.h>
29
#include <stdarg.h>
30
#include <stdbool.h>
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include <sys/types.h>
35
#include <sys/stat.h>
36
#include <signal.h>
37
#include <fcntl.h>
38
#include <time.h>
39
#include <errno.h>
40
#ifdef HAVE_SYS_SYSCTL_H
41
# include <sys/sysctl.h>
42
#endif
43
#ifdef __APPLE__
44
# include <mach-o/dyld.h>
45
#endif
46
47
#ifdef _WIN32
48
# include <direct.h>
49
# include <io.h>
50
# include <process.h>
51
# define mkdir(path,mode) mkdir(path)
52
# ifndef S_ISREG
53
# define S_ISREG(mod) (((mod) & _S_IFMT) == _S_IFREG)
54
# endif
55
# ifdef _MSC_VER
56
# define popen _popen
57
# define pclose _pclose
58
# define strtoll _strtoi64
59
# define strtoull _strtoui64
60
# define strncasecmp _strnicmp
61
# define strcasecmp _stricmp
62
# endif
63
# include <windef.h>
64
# include <winbase.h>
65
#else
66
extern char **environ;
67
# include <spawn.h>
68
# include <sys/wait.h>
69
# include <unistd.h>
70
# ifndef O_BINARY
71
# define O_BINARY 0
72
# endif
73
# ifndef __int64
74
# if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
75
# define __int64 long
76
# else
77
# define __int64 long long
78
# endif
79
# endif
80
#endif
81
82
#if !defined(__GNUC__) && !defined(__attribute__)
83
#define __attribute__(x)
84
#endif
85
86
#ifndef max
87
#define max(a,b) (((a) > (b)) ? (a) : (b))
88
#endif
89
#ifndef min
90
#define min(a,b) (((a) < (b)) ? (a) : (b))
91
#endif
92
93
#ifndef ARRAY_SIZE
94
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
95
#endif
96
97
struct target
98
{
99
enum { CPU_i386, CPU_x86_64, CPU_ARM, CPU_ARM64, CPU_ARM64EC } cpu;
100
101
enum
102
{
103
PLATFORM_UNSPECIFIED,
104
PLATFORM_APPLE,
105
PLATFORM_ANDROID,
106
PLATFORM_LINUX,
107
PLATFORM_FREEBSD,
108
PLATFORM_SOLARIS,
109
PLATFORM_WINDOWS,
110
PLATFORM_MINGW,
111
PLATFORM_CYGWIN
112
} platform;
113
};
114
115
static inline void *xmalloc( size_t size )
116
{
117
void *res = malloc( size ? size : 1 );
118
119
if (res == NULL)
120
{
121
fprintf( stderr, "Virtual memory exhausted.\n" );
122
exit(1);
123
}
124
return res;
125
}
126
127
static inline void *xrealloc (void *ptr, size_t size)
128
{
129
void *res = realloc( ptr, size );
130
131
if (size && res == NULL)
132
{
133
fprintf( stderr, "Virtual memory exhausted.\n" );
134
exit(1);
135
}
136
return res;
137
}
138
139
static inline char *xstrdup( const char *str )
140
{
141
return strcpy( xmalloc( strlen(str)+1 ), str );
142
}
143
144
static inline bool strendswith( const char *str, const char *end )
145
{
146
int l = strlen( str );
147
int m = strlen( end );
148
return l >= m && !strcmp( str + l - m, end );
149
}
150
151
static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
152
static inline char *strmake( const char* fmt, ... )
153
{
154
int n;
155
size_t size = 100;
156
va_list ap;
157
158
for (;;)
159
{
160
char *p = xmalloc( size );
161
va_start( ap, fmt );
162
n = vsnprintf( p, size, fmt, ap );
163
va_end( ap );
164
if (n == -1) size *= 2;
165
else if ((size_t)n >= size) size = n + 1;
166
else return p;
167
free( p );
168
}
169
}
170
171
/* string array functions */
172
173
struct strarray
174
{
175
unsigned int count; /* strings in use */
176
unsigned int size; /* total allocated size */
177
const char **str;
178
};
179
180
static const struct strarray empty_strarray;
181
182
static inline void strarray_add( struct strarray *array, const char *str )
183
{
184
if (array->count == array->size)
185
{
186
if (array->size) array->size *= 2;
187
else array->size = 16;
188
array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
189
}
190
array->str[array->count++] = str;
191
}
192
193
static inline void strarray_addall( struct strarray *array, struct strarray added )
194
{
195
unsigned int i;
196
197
for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
198
}
199
200
static inline bool strarray_exists( struct strarray array, const char *str )
201
{
202
unsigned int i;
203
204
for (i = 0; i < array.count; i++) if (!strcmp( array.str[i], str )) return true;
205
return false;
206
}
207
208
static inline void strarray_add_uniq( struct strarray *array, const char *str )
209
{
210
if (!strarray_exists( *array, str )) strarray_add( array, str );
211
}
212
213
static inline void strarray_addall_uniq( struct strarray *array, struct strarray added )
214
{
215
unsigned int i;
216
217
for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
218
}
219
220
static inline struct strarray strarray_fromstring( const char *str, const char *delim )
221
{
222
struct strarray array = empty_strarray;
223
char *buf = xstrdup( str );
224
const char *tok;
225
226
for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
227
strarray_add( &array, xstrdup( tok ));
228
free( buf );
229
return array;
230
}
231
232
static inline struct strarray strarray_frompath( const char *path )
233
{
234
if (!path) return empty_strarray;
235
#ifdef _WIN32
236
return strarray_fromstring( path, ";" );
237
#else
238
return strarray_fromstring( path, ":" );
239
#endif
240
}
241
242
static inline char *strarray_tostring( struct strarray array, const char *sep )
243
{
244
char *str;
245
unsigned int i, len = 1 + (array.count - 1) * strlen(sep);
246
247
if (!array.count) return xstrdup("");
248
for (i = 0; i < array.count; i++) len += strlen( array.str[i] );
249
str = xmalloc( len );
250
strcpy( str, array.str[0] );
251
for (i = 1; i < array.count; i++)
252
{
253
strcat( str, sep );
254
strcat( str, array.str[i] );
255
}
256
return str;
257
}
258
259
static inline void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
260
{
261
if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
262
}
263
264
static inline const char *strarray_bsearch( struct strarray array, const char *str,
265
int (*func)(const char **, const char **) )
266
{
267
char **res = NULL;
268
269
if (array.count) res = bsearch( &str, array.str, array.count, sizeof(*array.str), (void *)func );
270
return res ? *res : NULL;
271
}
272
273
static inline void strarray_trace( struct strarray args )
274
{
275
unsigned int i;
276
277
for (i = 0; i < args.count; i++)
278
{
279
if (strpbrk( args.str[i], " \t\n\r")) printf( "\"%s\"", args.str[i] );
280
else printf( "%s", args.str[i] );
281
if (i < args.count - 1) putchar( ' ' );
282
}
283
putchar( '\n' );
284
}
285
286
static inline int strarray_spawn( struct strarray args )
287
{
288
#ifdef _WIN32
289
strarray_add( &args, NULL );
290
return _spawnvp( _P_WAIT, args.str[0], args.str );
291
#else
292
pid_t pid, wret;
293
int status;
294
295
strarray_add( &args, NULL );
296
if (posix_spawnp( &pid, args.str[0], NULL, NULL, (char **)args.str, environ ))
297
return -1;
298
299
while (pid != (wret = waitpid( pid, &status, 0 )))
300
if (wret == -1 && errno != EINTR) break;
301
302
if (pid == wret && WIFEXITED(status)) return WEXITSTATUS(status);
303
return 255; /* abnormal exit with an abort or an interrupt */
304
#endif
305
}
306
307
static inline char *get_basename( const char *file )
308
{
309
const char *ret = strrchr( file, '/' );
310
return xstrdup( ret ? ret + 1 : file );
311
}
312
313
static inline char *get_basename_noext( const char *file )
314
{
315
char *ext, *ret = get_basename( file );
316
if ((ext = strrchr( ret, '.' ))) *ext = 0;
317
return ret;
318
}
319
320
static inline char *get_dirname( const char *file )
321
{
322
const char *end = strrchr( file, '/' );
323
if (!end) return xstrdup( "." );
324
if (end == file) end++;
325
return strmake( "%.*s", (int)(end - file), file );
326
}
327
328
static inline char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
329
{
330
int name_len = strlen( name );
331
332
if (strendswith( name, old_ext )) name_len -= strlen( old_ext );
333
return strmake( "%.*s%s", name_len, name, new_ext );
334
}
335
336
/* build a path with the relative dir from 'from' to 'dest' appended to base */
337
static inline char *build_relative_path( const char *base, const char *from, const char *dest )
338
{
339
const char *start;
340
char *ret;
341
unsigned int dotdots = 0;
342
343
for (;;)
344
{
345
while (*from == '/') from++;
346
while (*dest == '/') dest++;
347
start = dest; /* save start of next path element */
348
if (!*from) break;
349
350
while (*from && *from != '/' && *from == *dest) { from++; dest++; }
351
if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
352
353
do /* count remaining elements in 'from' */
354
{
355
dotdots++;
356
while (*from && *from != '/') from++;
357
while (*from == '/') from++;
358
}
359
while (*from);
360
break;
361
}
362
363
ret = xmalloc( strlen(base) + 3 * dotdots + strlen(start) + 2 );
364
strcpy( ret, base );
365
while (dotdots--) strcat( ret, "/.." );
366
367
if (!start[0]) return ret;
368
strcat( ret, "/" );
369
strcat( ret, start );
370
return ret;
371
}
372
373
/* temp files management */
374
375
extern const char *temp_dir;
376
extern struct strarray temp_files;
377
378
static inline char *make_temp_dir(void)
379
{
380
unsigned int value = time(NULL) + getpid();
381
int count;
382
char *name;
383
const char *tmpdir = NULL;
384
385
for (count = 0; count < 0x8000; count++)
386
{
387
if (tmpdir)
388
name = strmake( "%s/tmp%08x", tmpdir, value );
389
else
390
name = strmake( "tmp%08x", value );
391
if (!mkdir( name, 0700 )) return name;
392
value += 7777;
393
if (errno == EACCES && !tmpdir)
394
{
395
if (!(tmpdir = getenv("TMPDIR"))) tmpdir = "/tmp";
396
}
397
free( name );
398
}
399
fprintf( stderr, "failed to create directory for temp files\n" );
400
exit(1);
401
}
402
403
static inline char *make_temp_file( const char *prefix, const char *suffix )
404
{
405
static unsigned int value;
406
int fd, count;
407
char *name;
408
409
if (!temp_dir) temp_dir = make_temp_dir();
410
if (!suffix) suffix = "";
411
if (!prefix) prefix = "tmp";
412
else prefix = get_basename_noext( prefix );
413
414
for (count = 0; count < 0x8000; count++)
415
{
416
name = strmake( "%s/%s-%08x%s", temp_dir, prefix, value++, suffix );
417
fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0600 );
418
if (fd >= 0)
419
{
420
#ifdef HAVE_SIGPROCMASK /* block signals while manipulating the temp files list */
421
sigset_t mask_set, old_set;
422
423
sigemptyset( &mask_set );
424
sigaddset( &mask_set, SIGHUP );
425
sigaddset( &mask_set, SIGTERM );
426
sigaddset( &mask_set, SIGINT );
427
sigprocmask( SIG_BLOCK, &mask_set, &old_set );
428
strarray_add( &temp_files, name );
429
sigprocmask( SIG_SETMASK, &old_set, NULL );
430
#else
431
strarray_add( &temp_files, name );
432
#endif
433
close( fd );
434
return name;
435
}
436
free( name );
437
}
438
fprintf( stderr, "failed to create temp file for %s%s in %s\n", prefix, suffix, temp_dir );
439
exit(1);
440
}
441
442
static inline void remove_temp_files(void)
443
{
444
unsigned int i;
445
446
for (i = 0; i < temp_files.count; i++) if (temp_files.str[i]) unlink( temp_files.str[i] );
447
if (temp_dir) rmdir( temp_dir );
448
}
449
450
451
static inline void init_signals( void (*cleanup)(int) )
452
{
453
signal( SIGTERM, cleanup );
454
signal( SIGINT, cleanup );
455
#ifdef SIGHUP
456
signal( SIGHUP, cleanup );
457
#endif
458
}
459
460
461
static inline void *read_file( const char *name, size_t *size )
462
{
463
struct stat st;
464
int res, fd;
465
void *data;
466
467
if ((fd = open( name, O_RDONLY | O_BINARY )) == -1) return NULL;
468
fstat( fd, &st );
469
data = xmalloc( st.st_size );
470
res = read( fd, data, st.st_size );
471
if (res == -1)
472
{
473
free( data );
474
data = NULL;
475
*size = 0;
476
}
477
else *size = res;
478
close( fd );
479
return data;
480
}
481
482
483
static inline struct target get_default_target(void)
484
{
485
struct target target;
486
#ifdef __i386__
487
target.cpu = CPU_i386;
488
#elif defined(__x86_64__)
489
target.cpu = CPU_x86_64;
490
#elif defined(__arm__)
491
target.cpu = CPU_ARM;
492
#elif defined(__aarch64__)
493
target.cpu = CPU_ARM64;
494
#else
495
#error Unsupported CPU
496
#endif
497
498
#ifdef __APPLE__
499
target.platform = PLATFORM_APPLE;
500
#elif defined(__ANDROID__)
501
target.platform = PLATFORM_ANDROID;
502
#elif defined(__linux__)
503
target.platform = PLATFORM_LINUX;
504
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
505
target.platform = PLATFORM_FREEBSD;
506
#elif defined(__sun)
507
target.platform = PLATFORM_SOLARIS;
508
#elif defined(__CYGWIN__)
509
target.platform = PLATFORM_CYGWIN;
510
#elif defined(_WIN32)
511
target.platform = PLATFORM_MINGW;
512
#else
513
target.platform = PLATFORM_UNSPECIFIED;
514
#endif
515
516
return target;
517
}
518
519
520
static inline unsigned int get_target_ptr_size( struct target target )
521
{
522
static const unsigned int sizes[] =
523
{
524
[CPU_i386] = 4,
525
[CPU_x86_64] = 8,
526
[CPU_ARM] = 4,
527
[CPU_ARM64] = 8,
528
[CPU_ARM64EC] = 8,
529
};
530
return sizes[target.cpu];
531
}
532
533
534
static inline void set_target_ptr_size( struct target *target, unsigned int size )
535
{
536
switch (target->cpu)
537
{
538
case CPU_i386:
539
if (size == 8) target->cpu = CPU_x86_64;
540
break;
541
case CPU_x86_64:
542
if (size == 4) target->cpu = CPU_i386;
543
break;
544
case CPU_ARM:
545
if (size == 8) target->cpu = CPU_ARM64;
546
break;
547
case CPU_ARM64:
548
case CPU_ARM64EC:
549
if (size == 4) target->cpu = CPU_ARM;
550
break;
551
}
552
}
553
554
555
static inline bool is_pe_target( struct target target )
556
{
557
return (target.platform == PLATFORM_WINDOWS ||
558
target.platform == PLATFORM_MINGW ||
559
target.platform == PLATFORM_CYGWIN);
560
}
561
562
563
static inline int get_cpu_from_name( const char *name )
564
{
565
static const struct
566
{
567
const char *name;
568
int cpu;
569
} cpu_names[] =
570
{
571
{ "i386", CPU_i386 },
572
{ "i486", CPU_i386 },
573
{ "i586", CPU_i386 },
574
{ "i686", CPU_i386 },
575
{ "i786", CPU_i386 },
576
{ "x86_64", CPU_x86_64 },
577
{ "amd64", CPU_x86_64 },
578
{ "aarch64", CPU_ARM64 },
579
{ "arm64ec", CPU_ARM64EC },
580
{ "arm64", CPU_ARM64 },
581
{ "arm", CPU_ARM },
582
};
583
unsigned int i;
584
585
for (i = 0; i < ARRAY_SIZE(cpu_names); i++)
586
if (!strncmp( cpu_names[i].name, name, strlen(cpu_names[i].name) )) return cpu_names[i].cpu;
587
return -1;
588
}
589
590
591
static inline int get_platform_from_name( const char *name )
592
{
593
static const struct
594
{
595
const char *name;
596
int platform;
597
} platform_names[] =
598
{
599
{ "macos", PLATFORM_APPLE },
600
{ "darwin", PLATFORM_APPLE },
601
{ "android", PLATFORM_ANDROID },
602
{ "linux", PLATFORM_LINUX },
603
{ "freebsd", PLATFORM_FREEBSD },
604
{ "solaris", PLATFORM_SOLARIS },
605
{ "mingw32", PLATFORM_MINGW },
606
{ "windows-gnu", PLATFORM_MINGW },
607
{ "winnt", PLATFORM_MINGW },
608
{ "windows", PLATFORM_WINDOWS },
609
{ "cygwin", PLATFORM_CYGWIN },
610
};
611
unsigned int i;
612
613
for (i = 0; i < ARRAY_SIZE(platform_names); i++)
614
if (!strncmp( platform_names[i].name, name, strlen(platform_names[i].name) ))
615
return platform_names[i].platform;
616
return -1;
617
};
618
619
620
static inline const char *get_arch_dir( struct target target )
621
{
622
static const char *cpu_names[] =
623
{
624
[CPU_i386] = "i386",
625
[CPU_x86_64] = "x86_64",
626
[CPU_ARM] = "arm",
627
[CPU_ARM64] = "aarch64",
628
[CPU_ARM64EC] = "aarch64",
629
};
630
631
if (!cpu_names[target.cpu]) return "";
632
return strmake( "/%s-%s", cpu_names[target.cpu], is_pe_target( target ) ? "windows" : "unix" );
633
}
634
635
static inline bool parse_target( const char *name, struct target *target )
636
{
637
int res;
638
char *p, *spec = xstrdup( name );
639
640
/* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
641
642
/* get the CPU part */
643
644
if ((p = strchr( spec, '-' )))
645
{
646
*p++ = 0;
647
if ((res = get_cpu_from_name( spec )) == -1)
648
{
649
free( spec );
650
return false;
651
}
652
target->cpu = res;
653
}
654
else if (!strcmp( spec, "mingw32" ))
655
{
656
target->cpu = CPU_i386;
657
p = spec;
658
}
659
else
660
{
661
free( spec );
662
return false;
663
}
664
665
/* get the OS part */
666
667
target->platform = PLATFORM_UNSPECIFIED; /* default value */
668
for (;;)
669
{
670
if ((res = get_platform_from_name( p )) != -1)
671
{
672
target->platform = res;
673
break;
674
}
675
if (!(p = strchr( p, '-' ))) break;
676
p++;
677
}
678
679
free( spec );
680
return true;
681
}
682
683
684
static inline struct target init_argv0_target( const char *argv0 )
685
{
686
char *name = get_basename( argv0 );
687
struct target target;
688
689
if (!strchr( name, '-' ) || !parse_target( name, &target ))
690
target = get_default_target();
691
692
free( name );
693
return target;
694
}
695
696
697
static inline char *get_bindir( const char *argv0 )
698
{
699
#ifndef _WIN32
700
char *dir = NULL;
701
702
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) \
703
|| defined(__CYGWIN__) || defined(__MSYS__)
704
dir = realpath( "/proc/self/exe", NULL );
705
#elif defined (__FreeBSD__) || defined(__DragonFly__)
706
static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
707
size_t path_size = PATH_MAX;
708
char *path = xmalloc( path_size );
709
if (!sysctl( pathname, ARRAY_SIZE(pathname), path, &path_size, NULL, 0 ))
710
dir = realpath( path, NULL );
711
free( path );
712
#elif defined(__APPLE__)
713
uint32_t path_size = PATH_MAX;
714
char *path = xmalloc( path_size );
715
if (!_NSGetExecutablePath( path, &path_size ))
716
dir = realpath( path, NULL );
717
free( path );
718
#endif
719
if (!dir && !(dir = realpath( argv0, NULL ))) return NULL;
720
return get_dirname( dir );
721
#else
722
char path[MAX_PATH], *p;
723
GetModuleFileNameA( NULL, path, ARRAYSIZE(path) );
724
for (p = path; *p; p++) if (*p == '\\') *p = '/';
725
return get_dirname( path );
726
#endif
727
}
728
729
#ifdef LIBDIR
730
static inline const char *get_libdir( const char *bindir )
731
{
732
#ifdef BINDIR
733
if (bindir) return build_relative_path( bindir, BINDIR, LIBDIR );
734
#endif
735
return LIBDIR;
736
}
737
#endif
738
739
#ifdef DATADIR
740
static inline const char *get_datadir( const char *bindir )
741
{
742
#ifdef BINDIR
743
if (bindir) return build_relative_path( bindir, BINDIR, DATADIR );
744
#endif
745
return DATADIR;
746
}
747
#endif
748
749
#ifdef INCLUDEDIR
750
static inline const char *get_includedir( const char *bindir )
751
{
752
#ifdef BINDIR
753
if (bindir) return build_relative_path( bindir, BINDIR, INCLUDEDIR );
754
#endif
755
return INCLUDEDIR;
756
}
757
#endif
758
759
static inline const char *get_nlsdir( const char *bindir, const char *srcdir )
760
{
761
if (bindir && strendswith( bindir, srcdir )) return strmake( "%s/../../nls", bindir );
762
#ifdef DATADIR
763
else
764
{
765
const char *datadir = get_datadir( bindir );
766
if (datadir) return strmake( "%s/wine/nls", datadir );
767
}
768
#endif
769
return NULL;
770
}
771
772
773
/* output buffer management */
774
775
extern unsigned char *output_buffer;
776
extern size_t output_buffer_pos;
777
extern size_t output_buffer_size;
778
779
static inline void check_output_buffer_space( size_t size )
780
{
781
if (output_buffer_pos + size >= output_buffer_size)
782
{
783
output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
784
output_buffer = xrealloc( output_buffer, output_buffer_size );
785
}
786
}
787
788
static inline void init_output_buffer(void)
789
{
790
output_buffer_size = 1024;
791
output_buffer_pos = 0;
792
output_buffer = xmalloc( output_buffer_size );
793
}
794
795
static inline void put_data( const void *data, size_t size )
796
{
797
check_output_buffer_space( size );
798
memcpy( output_buffer + output_buffer_pos, data, size );
799
output_buffer_pos += size;
800
}
801
802
static inline void put_byte( unsigned char val )
803
{
804
check_output_buffer_space( 1 );
805
output_buffer[output_buffer_pos++] = val;
806
}
807
808
static inline void put_word( unsigned short val )
809
{
810
check_output_buffer_space( 2 );
811
output_buffer[output_buffer_pos++] = val;
812
output_buffer[output_buffer_pos++] = val >> 8;
813
}
814
815
static inline void put_dword( unsigned int val )
816
{
817
check_output_buffer_space( 4 );
818
output_buffer[output_buffer_pos++] = val;
819
output_buffer[output_buffer_pos++] = val >> 8;
820
output_buffer[output_buffer_pos++] = val >> 16;
821
output_buffer[output_buffer_pos++] = val >> 24;
822
}
823
824
static inline void put_qword( unsigned int val )
825
{
826
put_dword( val );
827
put_dword( 0 );
828
}
829
830
static inline void align_output( unsigned int align )
831
{
832
size_t size = align - (output_buffer_pos % align);
833
834
if (size == align) return;
835
check_output_buffer_space( size );
836
memset( output_buffer + output_buffer_pos, 0, size );
837
output_buffer_pos += size;
838
}
839
840
static inline void flush_output_buffer( const char *name )
841
{
842
int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
843
844
if (fd == -1 || write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
845
{
846
perror( name );
847
exit(1);
848
}
849
close( fd );
850
free( output_buffer );
851
}
852
853
/* command-line option parsing */
854
/* partly based on the Glibc getopt() implementation */
855
856
struct long_option
857
{
858
const char *name;
859
bool has_arg;
860
int val;
861
};
862
863
static inline struct strarray parse_options( int argc, char **argv, const char *short_opts,
864
const struct long_option *long_opts, int long_only,
865
void (*callback)( int, char* ) )
866
{
867
struct strarray ret = empty_strarray;
868
const char *flag;
869
char *start, *end;
870
int i;
871
872
#define OPT_ERR(fmt) { callback( '?', strmake( fmt, argv[i] )); continue; }
873
874
for (i = 1; i < argc; i++)
875
{
876
if (argv[i][0] != '-' || !argv[i][1]) /* not an option */
877
{
878
strarray_add( &ret, argv[i] );
879
continue;
880
}
881
if (!strcmp( argv[i], "--" ))
882
{
883
/* add remaining args */
884
while (++i < argc) strarray_add( &ret, argv[i] );
885
break;
886
}
887
start = argv[i] + 1 + (argv[i][1] == '-');
888
889
if (argv[i][1] == '-' || (long_only && (argv[i][2] || !strchr( short_opts, argv[i][1] ))))
890
{
891
/* handle long option */
892
const struct long_option *opt, *found = NULL;
893
int count = 0;
894
895
if (!(end = strchr( start, '=' ))) end = start + strlen(start);
896
for (opt = long_opts; opt && opt->name; opt++)
897
{
898
if (strncmp( opt->name, start, end - start )) continue;
899
if (!opt->name[end - start]) /* exact match */
900
{
901
found = opt;
902
count = 1;
903
break;
904
}
905
if (!found)
906
{
907
found = opt;
908
count++;
909
}
910
else if (long_only || found->has_arg != opt->has_arg || found->val != opt->val)
911
{
912
count++;
913
}
914
}
915
916
if (count > 1) OPT_ERR( "option '%s' is ambiguous" );
917
918
if (found)
919
{
920
if (*end)
921
{
922
if (!found->has_arg) OPT_ERR( "argument not allowed in '%s'" );
923
end++; /* skip '=' */
924
}
925
else if (found->has_arg == 1)
926
{
927
if (i == argc - 1) OPT_ERR( "option '%s' requires an argument" );
928
end = argv[++i];
929
}
930
else end = NULL;
931
932
callback( found->val, end );
933
continue;
934
}
935
if (argv[i][1] == '-' || !long_only || !strchr( short_opts, argv[i][1] ))
936
OPT_ERR( "unrecognized option '%s'" );
937
}
938
939
/* handle short option */
940
for ( ; *start; start++)
941
{
942
if (!(flag = strchr( short_opts, *start ))) OPT_ERR( "invalid option '%s'" );
943
if (flag[1] == ':')
944
{
945
end = start + 1;
946
if (!*end) end = NULL;
947
if (flag[2] != ':' && !end)
948
{
949
if (i == argc - 1) OPT_ERR( "option '%s' requires an argument" );
950
end = argv[++i];
951
}
952
callback( *start, end );
953
break;
954
}
955
callback( *start, NULL );
956
}
957
}
958
return ret;
959
#undef OPT_ERR
960
}
961
962
#endif /* __WINE_TOOLS_H */
963
964