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