Fixed a few prototypes in the USER driver.
[wine] / tools / winebuild / utils.c
1 /*
2  * Small utility functions for winebuild
3  *
4  * Copyright 2000 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "build.h"
32
33 void *xmalloc (size_t size)
34 {
35     void *res;
36
37     res = malloc (size ? size : 1);
38     if (res == NULL)
39     {
40         fprintf (stderr, "Virtual memory exhausted.\n");
41         exit (1);
42     }
43     return res;
44 }
45
46 void *xrealloc (void *ptr, size_t size)
47 {
48     void *res = realloc (ptr, size);
49     if (size && res == NULL)
50     {
51         fprintf (stderr, "Virtual memory exhausted.\n");
52         exit (1);
53     }
54     return res;
55 }
56
57 char *xstrdup( const char *str )
58 {
59     char *res = strdup( str );
60     if (!res)
61     {
62         fprintf (stderr, "Virtual memory exhausted.\n");
63         exit (1);
64     }
65     return res;
66 }
67
68 char *strupper(char *s)
69 {
70     char *p;
71     for (p = s; *p; p++) *p = toupper(*p);
72     return s;
73 }
74
75 int strendswith(const char* str, const char* end)
76 {
77     int l = strlen(str);
78     int m = strlen(end);
79     return l >= m && strcmp(str + l - m, end) == 0;
80 }
81
82 void fatal_error( const char *msg, ... )
83 {
84     va_list valist;
85     va_start( valist, msg );
86     if (input_file_name)
87     {
88         fprintf( stderr, "%s:", input_file_name );
89         if (current_line)
90             fprintf( stderr, "%d:", current_line );
91         fputc( ' ', stderr );
92     }
93     else fprintf( stderr, "winebuild: " );
94     vfprintf( stderr, msg, valist );
95     va_end( valist );
96     exit(1);
97 }
98
99 void fatal_perror( const char *msg, ... )
100 {
101     va_list valist;
102     va_start( valist, msg );
103     if (input_file_name)
104     {
105         fprintf( stderr, "%s:", input_file_name );
106         if (current_line)
107             fprintf( stderr, "%d:", current_line );
108         fputc( ' ', stderr );
109     }
110     vfprintf( stderr, msg, valist );
111     perror( " " );
112     va_end( valist );
113     exit(1);
114 }
115
116 void error( const char *msg, ... )
117 {
118     va_list valist;
119     va_start( valist, msg );
120     if (input_file_name)
121     {
122         fprintf( stderr, "%s:", input_file_name );
123         if (current_line)
124             fprintf( stderr, "%d:", current_line );
125         fputc( ' ', stderr );
126     }
127     vfprintf( stderr, msg, valist );
128     va_end( valist );
129     nb_errors++;
130 }
131
132 void warning( const char *msg, ... )
133 {
134     va_list valist;
135
136     if (!display_warnings) return;
137     va_start( valist, msg );
138     if (input_file_name)
139     {
140         fprintf( stderr, "%s:", input_file_name );
141         if (current_line)
142             fprintf( stderr, "%d:", current_line );
143         fputc( ' ', stderr );
144     }
145     fprintf( stderr, "warning: " );
146     vfprintf( stderr, msg, valist );
147     va_end( valist );
148 }
149
150 /* output a standard header for generated files */
151 void output_standard_file_header( FILE *outfile )
152 {
153     if (spec_file_name)
154         fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
155                  spec_file_name );
156     else
157         fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
158     fprintf( outfile,
159              "/* This file can be copied, modified and distributed without restriction. */\n\n" );
160 }
161
162 /* dump a byte stream into the assembly code */
163 void dump_bytes( FILE *outfile, const unsigned char *data, int len,
164                  const char *label, int constant )
165 {
166     int i;
167
168     fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
169              constant ? "const " : "", label, len );
170     for (i = 0; i < len; i++)
171     {
172         if (!(i & 7)) fprintf( outfile, "\n  " );
173         fprintf( outfile, "0x%02x", *data++ );
174         if (i < len - 1) fprintf( outfile, "," );
175     }
176     fprintf( outfile, "\n};\n" );
177 }
178
179
180 /*******************************************************************
181  *         open_input_file
182  *
183  * Open a file in the given srcdir and set the input_file_name global variable.
184  */
185 FILE *open_input_file( const char *srcdir, const char *name )
186 {
187     char *fullname;
188     FILE *file = fopen( name, "r" );
189
190     if (!file && srcdir)
191     {
192         fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
193         strcpy( fullname, srcdir );
194         strcat( fullname, "/" );
195         strcat( fullname, name );
196         file = fopen( fullname, "r" );
197     }
198     else fullname = xstrdup( name );
199
200     if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
201     input_file_name = fullname;
202     current_line = 1;
203     return file;
204 }
205
206
207 /*******************************************************************
208  *         close_input_file
209  *
210  * Close the current input file (must have been opened with open_input_file).
211  */
212 void close_input_file( FILE *file )
213 {
214     fclose( file );
215     free( input_file_name );
216     input_file_name = NULL;
217     current_line = 0;
218 }
219
220
221 /*******************************************************************
222  *         remove_stdcall_decoration
223  *
224  * Remove a possible @xx suffix from a function name.
225  * Return the numerical value of the suffix, or -1 if none.
226  */
227 int remove_stdcall_decoration( char *name )
228 {
229     char *p, *end = strrchr( name, '@' );
230     if (!end || !end[1] || end == name) return -1;
231     /* make sure all the rest is digits */
232     for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
233     *end = 0;
234     return atoi( end + 1 );
235 }
236
237
238 /*******************************************************************
239  *         alloc_dll_spec
240  *
241  * Create a new dll spec file descriptor
242  */
243 DLLSPEC *alloc_dll_spec(void)
244 {
245     DLLSPEC *spec;
246
247     spec = xmalloc( sizeof(*spec) );
248     spec->file_name          = NULL;
249     spec->dll_name           = NULL;
250     spec->owner_name         = NULL;
251     spec->init_func          = NULL;
252     spec->type               = SPEC_WIN32;
253     spec->base               = MAX_ORDINALS;
254     spec->limit              = 0;
255     spec->stack_size         = 0;
256     spec->heap_size          = 0;
257     spec->nb_entry_points    = 0;
258     spec->alloc_entry_points = 0;
259     spec->nb_names           = 0;
260     spec->nb_resources       = 0;
261     spec->characteristics    = 0;
262     spec->subsystem          = 0;
263     spec->subsystem_major    = 4;
264     spec->subsystem_minor    = 0;
265     spec->entry_points       = NULL;
266     spec->names              = NULL;
267     spec->ordinals           = NULL;
268     spec->resources          = NULL;
269     return spec;
270 }
271
272
273 /*******************************************************************
274  *         free_dll_spec
275  *
276  * Free dll spec file descriptor
277  */
278 void free_dll_spec( DLLSPEC *spec )
279 {
280     int i;
281
282     for (i = 0; i < spec->nb_entry_points; i++)
283     {
284         ORDDEF *odp = &spec->entry_points[i];
285         free( odp->name );
286         free( odp->export_name );
287         free( odp->link_name );
288     }
289     free( spec->file_name );
290     free( spec->dll_name );
291     free( spec->owner_name );
292     free( spec->init_func );
293     free( spec->entry_points );
294     free( spec->names );
295     free( spec->ordinals );
296     free( spec->resources );
297     free( spec );
298 }
299
300
301 /*******************************************************************
302  *         make_c_identifier
303  *
304  * Map a string to a valid C identifier.
305  */
306 const char *make_c_identifier( const char *str )
307 {
308     static char buffer[256];
309     char *p;
310
311     for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
312     {
313         if (isalnum(*str)) *p = *str;
314         else *p = '_';
315     }
316     *p = 0;
317     return buffer;
318 }
319
320
321 /*****************************************************************
322  *  Function:    get_alignment
323  *
324  *  Description:
325  *    According to the info page for gas, the .align directive behaves
326  * differently on different systems.  On some architectures, the
327  * argument of a .align directive is the number of bytes to pad to, so
328  * to align on an 8-byte boundary you'd say
329  *     .align 8
330  * On other systems, the argument is "the number of low-order zero bits
331  * that the location counter must have after advancement."  So to
332  * align on an 8-byte boundary you'd say
333  *     .align 3
334  *
335  * The reason gas is written this way is that it's trying to mimick
336  * native assemblers for the various architectures it runs on.  gas
337  * provides other directives that work consistantly across
338  * architectures, but of course we want to work on all arches with or
339  * without gas.  Hence this function.
340  *
341  *
342  *  Parameters:
343  *    align  --  the number of bytes to align to. Must be a power of 2.
344  */
345 unsigned int get_alignment(unsigned int align)
346 {
347     unsigned int n;
348
349     assert( !(align & (align - 1)) );
350
351     switch(target_cpu)
352     {
353     case CPU_x86:
354     case CPU_SPARC:
355         if (target_platform != PLATFORM_APPLE) return align;
356         /* fall through */
357     case CPU_POWERPC:
358     case CPU_ALPHA:
359         n = 0;
360         while ((1 << n) != align) n++;
361         return n;
362     }
363     /* unreached */
364     assert(0);
365     return 0;
366 }
367
368 /* return the page size for the target CPU */
369 unsigned int get_page_size(void)
370 {
371     switch(target_cpu)
372     {
373     case CPU_x86:     return 4096;
374     case CPU_POWERPC: return 4096;
375     case CPU_SPARC:   return 8192;
376     case CPU_ALPHA:   return 8192;
377     }
378     /* unreached */
379     assert(0);
380     return 0;
381 }
382
383 /* return the assembly name for a C symbol */
384 const char *asm_name( const char *sym )
385 {
386     static char buffer[256];
387
388     switch (target_platform)
389     {
390     case PLATFORM_APPLE:
391     case PLATFORM_WINDOWS:
392         buffer[0] = '_';
393         strcpy( buffer + 1, sym );
394         return buffer;
395     default:
396         return sym;
397     }
398 }
399
400 /* return an assembly function declaration for a C function name */
401 const char *func_declaration( const char *func )
402 {
403     static char buffer[256];
404
405     switch (target_platform)
406     {
407     case PLATFORM_APPLE:
408         return "";
409     case PLATFORM_WINDOWS:
410         sprintf( buffer, ".def _%s; .scl 2; .type 32; .endef", func );
411         break;
412     case PLATFORM_SVR4:
413         sprintf( buffer, ".type %s,2", func );
414         break;
415     default:
416         sprintf( buffer, ".type %s,@function", func );
417         break;
418     }
419     return buffer;
420 }
421
422 /* return a size declaration for an assembly function */
423 const char *func_size( const char *func )
424 {
425     static char buffer[256];
426
427     switch (target_platform)
428     {
429     case PLATFORM_APPLE:
430     case PLATFORM_WINDOWS:
431         return "";
432     default:
433         sprintf( buffer, ".size %s, .-%s", func, func );
434         return buffer;
435     }
436 }
437
438 const char *get_asm_string_keyword(void)
439 {
440     switch (target_platform)
441     {
442     case PLATFORM_APPLE:
443     case PLATFORM_SVR4:
444         return ".asciz";
445     default:
446         return ".string";
447     }
448 }
449
450 const char *get_asm_short_keyword(void)
451 {
452     switch (target_platform)
453     {
454     case PLATFORM_SVR4: return ".half";
455     default:            return ".short";
456     }
457 }