Introduce really basic scripting of actions. This is primarily to get
[wine/gsoc_dplay.git] / tools / winebuild / utils.c
blobc66c8dc3a5f4f10b677cabc5b9c97331c80752f9
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 (size && 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 int strendswith(const char* str, const char* end)
76 int l = strlen(str);
77 int m = strlen(end);
78 return l >= m && strcmp(str + l - m, end) == 0;
81 void fatal_error( const char *msg, ... )
83 va_list valist;
84 va_start( valist, msg );
85 if (input_file_name)
87 fprintf( stderr, "%s:", input_file_name );
88 if (current_line)
89 fprintf( stderr, "%d:", current_line );
90 fputc( ' ', stderr );
92 else fprintf( stderr, "winebuild: " );
93 vfprintf( stderr, msg, valist );
94 va_end( valist );
95 exit(1);
98 void fatal_perror( const char *msg, ... )
100 va_list valist;
101 va_start( valist, msg );
102 if (input_file_name)
104 fprintf( stderr, "%s:", input_file_name );
105 if (current_line)
106 fprintf( stderr, "%d:", current_line );
107 fputc( ' ', stderr );
109 vfprintf( stderr, msg, valist );
110 perror( " " );
111 va_end( valist );
112 exit(1);
115 void error( const char *msg, ... )
117 va_list valist;
118 va_start( valist, msg );
119 if (input_file_name)
121 fprintf( stderr, "%s:", input_file_name );
122 if (current_line)
123 fprintf( stderr, "%d:", current_line );
124 fputc( ' ', stderr );
126 vfprintf( stderr, msg, valist );
127 va_end( valist );
128 nb_errors++;
131 void warning( const char *msg, ... )
133 va_list valist;
135 if (!display_warnings) return;
136 va_start( valist, msg );
137 if (input_file_name)
139 fprintf( stderr, "%s:", input_file_name );
140 if (current_line)
141 fprintf( stderr, "%d:", current_line );
142 fputc( ' ', stderr );
144 fprintf( stderr, "warning: " );
145 vfprintf( stderr, msg, valist );
146 va_end( valist );
149 /* output a standard header for generated files */
150 void output_standard_file_header( FILE *outfile )
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" );
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 )
165 int i;
167 fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
168 constant ? "const " : "", label, len );
169 for (i = 0; i < len; i++)
171 if (!(i & 7)) fprintf( outfile, "\n " );
172 fprintf( outfile, "0x%02x", *data++ );
173 if (i < len - 1) fprintf( outfile, "," );
175 fprintf( outfile, "\n};\n" );
179 /*******************************************************************
180 * open_input_file
182 * Open a file in the given srcdir and set the input_file_name global variable.
184 FILE *open_input_file( const char *srcdir, const char *name )
186 char *fullname;
187 FILE *file = fopen( name, "r" );
189 if (!file && srcdir)
191 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
192 strcpy( fullname, srcdir );
193 strcat( fullname, "/" );
194 strcat( fullname, name );
195 file = fopen( fullname, "r" );
197 else fullname = xstrdup( name );
199 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
200 input_file_name = fullname;
201 current_line = 1;
202 return file;
206 /*******************************************************************
207 * close_input_file
209 * Close the current input file (must have been opened with open_input_file).
211 void close_input_file( FILE *file )
213 fclose( file );
214 free( input_file_name );
215 input_file_name = NULL;
216 current_line = 0;
220 /*******************************************************************
221 * remove_stdcall_decoration
223 * Remove a possible @xx suffix from a function name.
224 * Return the numerical value of the suffix, or -1 if none.
226 int remove_stdcall_decoration( char *name )
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 );
237 /*******************************************************************
238 * alloc_dll_spec
240 * Create a new dll spec file descriptor
242 DLLSPEC *alloc_dll_spec(void)
244 DLLSPEC *spec;
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;
272 /*******************************************************************
273 * free_dll_spec
275 * Free dll spec file descriptor
277 void free_dll_spec( DLLSPEC *spec )
279 int i;
281 for (i = 0; i < spec->nb_entry_points; i++)
283 ORDDEF *odp = &spec->entry_points[i];
284 free( odp->name );
285 free( odp->export_name );
286 free( odp->link_name );
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 );
300 /*******************************************************************
301 * make_c_identifier
303 * Map a string to a valid C identifier.
305 const char *make_c_identifier( const char *str )
307 static char buffer[256];
308 char *p;
310 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
312 if (isalnum(*str)) *p = *str;
313 else *p = '_';
315 *p = 0;
316 return buffer;
320 /*****************************************************************
321 * Function: get_alignment
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
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.
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.
350 int get_alignment(int alignBoundary)
352 #if defined(__powerpc__) || defined(__ALPHA__)
354 int n = 0;
356 switch(alignBoundary)
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);
410 return n;
412 #elif defined(__i386__) || defined(__sparc__)
414 return alignBoundary;
416 #else
417 #error "How does the '.align' assembler directive work on your architecture?"
418 #endif