Fixed some issues found by winapi_check.
[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 warning( const char *msg, ... )
108 {
109     va_list valist;
110
111     if (!display_warnings) return;
112     va_start( valist, msg );
113     if (input_file_name)
114     {
115         fprintf( stderr, "%s:", input_file_name );
116         if (current_line)
117             fprintf( stderr, "%d:", current_line );
118         fputc( ' ', stderr );
119     }
120     fprintf( stderr, "warning: " );
121     vfprintf( stderr, msg, valist );
122     va_end( valist );
123 }
124
125 /* output a standard header for generated files */
126 void output_standard_file_header( FILE *outfile )
127 {
128     if (input_file_name)
129         fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
130                  input_file_name );
131     else
132         fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
133     fprintf( outfile,
134              "/* This file can be copied, modified and distributed without restriction. */\n\n" );
135 }
136
137 /* dump a byte stream into the assembly code */
138 void dump_bytes( FILE *outfile, const unsigned char *data, int len,
139                  const char *label, int constant )
140 {
141     int i;
142
143     fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
144              constant ? "const " : "", label, len );
145     for (i = 0; i < len; i++)
146     {
147         if (!(i & 7)) fprintf( outfile, "\n  " );
148         fprintf( outfile, "0x%02x", *data++ );
149         if (i < len - 1) fprintf( outfile, "," );
150     }
151     fprintf( outfile, "\n};\n" );
152 }
153
154
155 /*******************************************************************
156  *         open_input_file
157  *
158  * Open a file in the given srcdir and set the input_file_name global variable.
159  */
160 FILE *open_input_file( const char *srcdir, const char *name )
161 {
162     char *fullname;
163     FILE *file;
164
165     if (srcdir)
166     {
167         fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
168         strcpy( fullname, srcdir );
169         strcat( fullname, "/" );
170         strcat( fullname, name );
171     }
172     else fullname = xstrdup( name );
173
174     if (!(file = fopen( fullname, "r" ))) fatal_error( "Cannot open file '%s'\n", fullname );
175     input_file_name = fullname;
176     current_line = 1;
177     return file;
178 }
179
180
181 /*******************************************************************
182  *         close_input_file
183  *
184  * Close the current input file (must have been opened with open_input_file).
185  */
186 void close_input_file( FILE *file )
187 {
188     fclose( file );
189     free( input_file_name );
190     input_file_name = NULL;
191     current_line = 0;
192 }
193
194
195 /*******************************************************************
196  *         make_c_identifier
197  *
198  * Map a string to a valid C identifier.
199  */
200 const char *make_c_identifier( const char *str )
201 {
202     static char buffer[256];
203     char *p;
204
205     for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
206     {
207         if (isalnum(*str)) *p = *str;
208         else *p = '_';
209     }
210     *p = 0;
211     return buffer;
212 }
213
214
215 /*****************************************************************
216  *  Function:    get_alignment
217  *
218  *  Description:
219  *    According to the info page for gas, the .align directive behaves
220  * differently on different systems.  On some architectures, the
221  * argument of a .align directive is the number of bytes to pad to, so
222  * to align on an 8-byte boundary you'd say
223  *     .align 8
224  * On other systems, the argument is "the number of low-order zero bits
225  * that the location counter must have after advancement."  So to
226  * align on an 8-byte boundary you'd say
227  *     .align 3
228  *
229  * The reason gas is written this way is that it's trying to mimick
230  * native assemblers for the various architectures it runs on.  gas
231  * provides other directives that work consistantly across
232  * architectures, but of course we want to work on all arches with or
233  * without gas.  Hence this function.
234  *
235  *
236  *  Parameters:
237  *                alignBoundary  --  the number of bytes to align to.
238  *                                   If we're on an architecture where
239  *                                   the assembler requires a 'number
240  *                                   of low-order zero bits' as a
241  *                                   .align argument, then this number
242  *                                   must be a power of 2.
243  *
244  */
245 int get_alignment(int alignBoundary)
246 {
247 #ifdef __PPC__
248
249     int n = 0;
250
251     switch(alignBoundary)
252     {
253     case 2:
254         n = 1;
255         break;
256     case 4:
257         n = 2;
258         break;
259     case 8:
260         n = 3;
261         break;
262     case 16:
263         n = 4;
264         break;
265     case 32:
266         n = 5;
267         break;
268     case 64:
269         n = 6;
270         break;
271     case 128:
272         n = 7;
273         break;
274     case 256:
275         n = 8;
276         break;
277     case 512:
278         n = 9;
279         break;
280     case 1024:
281         n = 10;
282         break;
283     case 2048:
284         n = 11;
285         break;
286     case 4096:
287         n = 12;
288         break;
289     case 8192:
290         n = 13;
291         break;
292     case 16384:
293         n = 14;
294         break;
295     case 32768:
296         n = 15;
297         break;
298     case 65536:
299         n = 16;
300         break;
301     default:
302         fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
303                     alignBoundary);
304     }
305     return n;
306
307 #elif defined(__i386__) || defined(__sparc__)
308
309     return alignBoundary;
310
311 #else
312 #error "How does the '.align' assembler directive work on your architecture?"
313 #endif
314 }