More correctly establish cases where the OSS input buffer should not
[wine/wine64.git] / tools / winebuild / utils.c
blob0d21028858a387b9c6eb64c39ef9ec96cda03551
1 /*
2 * Small utility functions for winebuild
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "build.h"
32 void *xmalloc (size_t size)
34 void *res;
36 res = malloc (size ? size : 1);
37 if (res == NULL)
39 fprintf (stderr, "Virtual memory exhausted.\n");
40 exit (1);
42 return res;
45 void *xrealloc (void *ptr, size_t size)
47 void *res = realloc (ptr, size);
48 if (res == NULL)
50 fprintf (stderr, "Virtual memory exhausted.\n");
51 exit (1);
53 return res;
56 char *xstrdup( const char *str )
58 char *res = strdup( str );
59 if (!res)
61 fprintf (stderr, "Virtual memory exhausted.\n");
62 exit (1);
64 return res;
67 char *strupper(char *s)
69 char *p;
70 for (p = s; *p; p++) *p = toupper(*p);
71 return s;
74 void fatal_error( const char *msg, ... )
76 va_list valist;
77 va_start( valist, msg );
78 if (input_file_name)
80 fprintf( stderr, "%s:", input_file_name );
81 if (current_line)
82 fprintf( stderr, "%d:", current_line );
83 fputc( ' ', stderr );
85 else fprintf( stderr, "winebuild: " );
86 vfprintf( stderr, msg, valist );
87 va_end( valist );
88 exit(1);
91 void fatal_perror( const char *msg, ... )
93 va_list valist;
94 va_start( valist, msg );
95 if (input_file_name)
97 fprintf( stderr, "%s:", input_file_name );
98 if (current_line)
99 fprintf( stderr, "%d:", current_line );
100 fputc( ' ', stderr );
102 vfprintf( stderr, msg, valist );
103 perror( " " );
104 va_end( valist );
105 exit(1);
108 void error( const char *msg, ... )
110 va_list valist;
111 va_start( valist, msg );
112 if (input_file_name)
114 fprintf( stderr, "%s:", input_file_name );
115 if (current_line)
116 fprintf( stderr, "%d:", current_line );
117 fputc( ' ', stderr );
119 vfprintf( stderr, msg, valist );
120 va_end( valist );
121 nb_errors++;
124 void warning( const char *msg, ... )
126 va_list valist;
128 if (!display_warnings) return;
129 va_start( valist, msg );
130 if (input_file_name)
132 fprintf( stderr, "%s:", input_file_name );
133 if (current_line)
134 fprintf( stderr, "%d:", current_line );
135 fputc( ' ', stderr );
137 fprintf( stderr, "warning: " );
138 vfprintf( stderr, msg, valist );
139 va_end( valist );
142 /* output a standard header for generated files */
143 void output_standard_file_header( FILE *outfile )
145 if (input_file_name)
146 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
147 input_file_name );
148 else
149 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
150 fprintf( outfile,
151 "/* This file can be copied, modified and distributed without restriction. */\n\n" );
154 /* dump a byte stream into the assembly code */
155 void dump_bytes( FILE *outfile, const unsigned char *data, int len,
156 const char *label, int constant )
158 int i;
160 fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
161 constant ? "const " : "", label, len );
162 for (i = 0; i < len; i++)
164 if (!(i & 7)) fprintf( outfile, "\n " );
165 fprintf( outfile, "0x%02x", *data++ );
166 if (i < len - 1) fprintf( outfile, "," );
168 fprintf( outfile, "\n};\n" );
172 /*******************************************************************
173 * open_input_file
175 * Open a file in the given srcdir and set the input_file_name global variable.
177 FILE *open_input_file( const char *srcdir, const char *name )
179 char *fullname;
180 FILE *file = fopen( name, "r" );
182 if (!file && srcdir)
184 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
185 strcpy( fullname, srcdir );
186 strcat( fullname, "/" );
187 strcat( fullname, name );
188 file = fopen( fullname, "r" );
190 else fullname = xstrdup( name );
192 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
193 input_file_name = fullname;
194 current_line = 1;
195 return file;
199 /*******************************************************************
200 * close_input_file
202 * Close the current input file (must have been opened with open_input_file).
204 void close_input_file( FILE *file )
206 fclose( file );
207 free( input_file_name );
208 input_file_name = NULL;
209 current_line = 0;
213 /*******************************************************************
214 * remove_stdcall_decoration
216 * Remove a possible @xx suffix from a function name.
217 * Return the numerical value of the suffix, or -1 if none.
219 int remove_stdcall_decoration( char *name )
221 char *p, *end = strrchr( name, '@' );
222 if (!end || !end[1] || end == name) return -1;
223 /* make sure all the rest is digits */
224 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
225 *end = 0;
226 return atoi( end + 1 );
230 /*******************************************************************
231 * alloc_dll_spec
233 * Create a new dll spec file descriptor
235 DLLSPEC *alloc_dll_spec(void)
237 DLLSPEC *spec;
239 spec = xmalloc( sizeof(*spec) );
240 spec->file_name = NULL;
241 spec->dll_name = NULL;
242 spec->owner_name = NULL;
243 spec->init_func = NULL;
244 spec->type = SPEC_WIN32;
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->characteristics = 0;
254 spec->subsystem = 0;
255 spec->subsystem_major = 4;
256 spec->subsystem_minor = 0;
257 spec->entry_points = NULL;
258 spec->names = NULL;
259 spec->ordinals = NULL;
260 spec->resources = NULL;
261 return spec;
265 /*******************************************************************
266 * free_dll_spec
268 * Free dll spec file descriptor
270 void free_dll_spec( DLLSPEC *spec )
272 int i;
274 for (i = 0; i < spec->nb_entry_points; i++)
276 ORDDEF *odp = &spec->entry_points[i];
277 free( odp->name );
278 free( odp->export_name );
279 free( odp->link_name );
281 free( spec->file_name );
282 free( spec->dll_name );
283 free( spec->owner_name );
284 free( spec->init_func );
285 free( spec->entry_points );
286 free( spec->names );
287 free( spec->ordinals );
288 free( spec->resources );
289 free( spec );
293 /*******************************************************************
294 * make_c_identifier
296 * Map a string to a valid C identifier.
298 const char *make_c_identifier( const char *str )
300 static char buffer[256];
301 char *p;
303 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
305 if (isalnum(*str)) *p = *str;
306 else *p = '_';
308 *p = 0;
309 return buffer;
313 /*****************************************************************
314 * Function: get_alignment
316 * Description:
317 * According to the info page for gas, the .align directive behaves
318 * differently on different systems. On some architectures, the
319 * argument of a .align directive is the number of bytes to pad to, so
320 * to align on an 8-byte boundary you'd say
321 * .align 8
322 * On other systems, the argument is "the number of low-order zero bits
323 * that the location counter must have after advancement." So to
324 * align on an 8-byte boundary you'd say
325 * .align 3
327 * The reason gas is written this way is that it's trying to mimick
328 * native assemblers for the various architectures it runs on. gas
329 * provides other directives that work consistantly across
330 * architectures, but of course we want to work on all arches with or
331 * without gas. Hence this function.
334 * Parameters:
335 * alignBoundary -- the number of bytes to align to.
336 * If we're on an architecture where
337 * the assembler requires a 'number
338 * of low-order zero bits' as a
339 * .align argument, then this number
340 * must be a power of 2.
343 int get_alignment(int alignBoundary)
345 #if defined(__powerpc__) || defined(__ALPHA__)
347 int n = 0;
349 switch(alignBoundary)
351 case 2:
352 n = 1;
353 break;
354 case 4:
355 n = 2;
356 break;
357 case 8:
358 n = 3;
359 break;
360 case 16:
361 n = 4;
362 break;
363 case 32:
364 n = 5;
365 break;
366 case 64:
367 n = 6;
368 break;
369 case 128:
370 n = 7;
371 break;
372 case 256:
373 n = 8;
374 break;
375 case 512:
376 n = 9;
377 break;
378 case 1024:
379 n = 10;
380 break;
381 case 2048:
382 n = 11;
383 break;
384 case 4096:
385 n = 12;
386 break;
387 case 8192:
388 n = 13;
389 break;
390 case 16384:
391 n = 14;
392 break;
393 case 32768:
394 n = 15;
395 break;
396 case 65536:
397 n = 16;
398 break;
399 default:
400 fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
401 alignBoundary);
403 return n;
405 #elif defined(__i386__) || defined(__sparc__)
407 return alignBoundary;
409 #else
410 #error "How does the '.align' assembler directive work on your architecture?"
411 #endif