Revert some word-splits to their normal form, e.g. InstallShield.
[wine/multimedia.git] / tools / winebuild / utils.c
blobf87927212be532c7f31ff5223d7c5d383addff5a
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 vfprintf( stderr, msg, valist );
86 va_end( valist );
87 exit(1);
90 void fatal_perror( const char *msg, ... )
92 va_list valist;
93 va_start( valist, msg );
94 if (input_file_name)
96 fprintf( stderr, "%s:", input_file_name );
97 if (current_line)
98 fprintf( stderr, "%d:", current_line );
99 fputc( ' ', stderr );
101 vfprintf( stderr, msg, valist );
102 perror( " " );
103 va_end( valist );
104 exit(1);
107 void error( const char *msg, ... )
109 va_list valist;
110 va_start( valist, msg );
111 if (input_file_name)
113 fprintf( stderr, "%s:", input_file_name );
114 if (current_line)
115 fprintf( stderr, "%d:", current_line );
116 fputc( ' ', stderr );
118 vfprintf( stderr, msg, valist );
119 va_end( valist );
120 nb_errors++;
123 void warning( const char *msg, ... )
125 va_list valist;
127 if (!display_warnings) return;
128 va_start( valist, msg );
129 if (input_file_name)
131 fprintf( stderr, "%s:", input_file_name );
132 if (current_line)
133 fprintf( stderr, "%d:", current_line );
134 fputc( ' ', stderr );
136 fprintf( stderr, "warning: " );
137 vfprintf( stderr, msg, valist );
138 va_end( valist );
141 /* output a standard header for generated files */
142 void output_standard_file_header( FILE *outfile )
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" );
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 )
157 int i;
159 fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
160 constant ? "const " : "", label, len );
161 for (i = 0; i < len; i++)
163 if (!(i & 7)) fprintf( outfile, "\n " );
164 fprintf( outfile, "0x%02x", *data++ );
165 if (i < len - 1) fprintf( outfile, "," );
167 fprintf( outfile, "\n};\n" );
171 /*******************************************************************
172 * open_input_file
174 * Open a file in the given srcdir and set the input_file_name global variable.
176 FILE *open_input_file( const char *srcdir, const char *name )
178 char *fullname;
179 FILE *file;
181 if (srcdir)
183 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
184 strcpy( fullname, srcdir );
185 strcat( fullname, "/" );
186 strcat( fullname, name );
188 else fullname = xstrdup( name );
190 if (!(file = fopen( fullname, "r" ))) fatal_error( "Cannot open file '%s'\n", fullname );
191 input_file_name = fullname;
192 current_line = 1;
193 return file;
197 /*******************************************************************
198 * close_input_file
200 * Close the current input file (must have been opened with open_input_file).
202 void close_input_file( FILE *file )
204 fclose( file );
205 free( input_file_name );
206 input_file_name = NULL;
207 current_line = 0;
211 /*******************************************************************
212 * make_c_identifier
214 * Map a string to a valid C identifier.
216 const char *make_c_identifier( const char *str )
218 static char buffer[256];
219 char *p;
221 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
223 if (isalnum(*str)) *p = *str;
224 else *p = '_';
226 *p = 0;
227 return buffer;
231 /*****************************************************************
232 * Function: get_alignment
234 * Description:
235 * According to the info page for gas, the .align directive behaves
236 * differently on different systems. On some architectures, the
237 * argument of a .align directive is the number of bytes to pad to, so
238 * to align on an 8-byte boundary you'd say
239 * .align 8
240 * On other systems, the argument is "the number of low-order zero bits
241 * that the location counter must have after advancement." So to
242 * align on an 8-byte boundary you'd say
243 * .align 3
245 * The reason gas is written this way is that it's trying to mimick
246 * native assemblers for the various architectures it runs on. gas
247 * provides other directives that work consistantly across
248 * architectures, but of course we want to work on all arches with or
249 * without gas. Hence this function.
252 * Parameters:
253 * alignBoundary -- the number of bytes to align to.
254 * If we're on an architecture where
255 * the assembler requires a 'number
256 * of low-order zero bits' as a
257 * .align argument, then this number
258 * must be a power of 2.
261 int get_alignment(int alignBoundary)
263 #ifdef __powerpc__
265 int n = 0;
267 switch(alignBoundary)
269 case 2:
270 n = 1;
271 break;
272 case 4:
273 n = 2;
274 break;
275 case 8:
276 n = 3;
277 break;
278 case 16:
279 n = 4;
280 break;
281 case 32:
282 n = 5;
283 break;
284 case 64:
285 n = 6;
286 break;
287 case 128:
288 n = 7;
289 break;
290 case 256:
291 n = 8;
292 break;
293 case 512:
294 n = 9;
295 break;
296 case 1024:
297 n = 10;
298 break;
299 case 2048:
300 n = 11;
301 break;
302 case 4096:
303 n = 12;
304 break;
305 case 8192:
306 n = 13;
307 break;
308 case 16384:
309 n = 14;
310 break;
311 case 32768:
312 n = 15;
313 break;
314 case 65536:
315 n = 16;
316 break;
317 default:
318 fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
319 alignBoundary);
321 return n;
323 #elif defined(__i386__) || defined(__sparc__)
325 return alignBoundary;
327 #else
328 #error "How does the '.align' assembler directive work on your architecture?"
329 #endif