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