Added version info to 16-bit shell.dll.
[wine/multimedia.git] / tools / winebuild / utils.c
blob68b6e3127242464ce4133fa6dd09bb44c42b68ec
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 warning( const char *msg, ... )
109 va_list valist;
111 if (!display_warnings) return;
112 va_start( valist, msg );
113 if (input_file_name)
115 fprintf( stderr, "%s:", input_file_name );
116 if (current_line)
117 fprintf( stderr, "%d:", current_line );
118 fputc( ' ', stderr );
120 fprintf( stderr, "warning: " );
121 vfprintf( stderr, msg, valist );
122 va_end( valist );
125 /* output a standard header for generated files */
126 void output_standard_file_header( FILE *outfile )
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" );
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 )
141 int i;
143 fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
144 constant ? "const " : "", label, len );
145 for (i = 0; i < len; i++)
147 if (!(i & 7)) fprintf( outfile, "\n " );
148 fprintf( outfile, "0x%02x", *data++ );
149 if (i < len - 1) fprintf( outfile, "," );
151 fprintf( outfile, "\n};\n" );
155 /*******************************************************************
156 * make_c_identifier
158 * Map a string to a valid C identifier.
160 const char *make_c_identifier( const char *str )
162 static char buffer[256];
163 char *p;
165 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
167 if (isalnum(*str)) *p = *str;
168 else *p = '_';
170 *p = 0;
171 return buffer;
175 /*****************************************************************
176 * Function: get_alignment
178 * Description:
179 * According to the info page for gas, the .align directive behaves
180 * differently on different systems. On some architectures, the
181 * argument of a .align directive is the number of bytes to pad to, so
182 * to align on an 8-byte boundary you'd say
183 * .align 8
184 * On other systems, the argument is "the number of low-order zero bits
185 * that the location counter must have after advancement." So to
186 * align on an 8-byte boundary you'd say
187 * .align 3
189 * The reason gas is written this way is that it's trying to mimick
190 * native assemblers for the various architectures it runs on. gas
191 * provides other directives that work consistantly across
192 * architectures, but of course we want to work on all arches with or
193 * without gas. Hence this function.
196 * Parameters:
197 * alignBoundary -- the number of bytes to align to.
198 * If we're on an architecture where
199 * the assembler requires a 'number
200 * of low-order zero bits' as a
201 * .align argument, then this number
202 * must be a power of 2.
205 int get_alignment(int alignBoundary)
207 #ifdef __PPC__
209 int n = 0;
211 switch(alignBoundary)
213 case 2:
214 n = 1;
215 break;
216 case 4:
217 n = 2;
218 break;
219 case 8:
220 n = 3;
221 break;
222 case 16:
223 n = 4;
224 break;
225 case 32:
226 n = 5;
227 break;
228 case 64:
229 n = 6;
230 break;
231 case 128:
232 n = 7;
233 break;
234 case 256:
235 n = 8;
236 break;
237 case 512:
238 n = 9;
239 break;
240 case 1024:
241 n = 10;
242 break;
243 case 2048:
244 n = 11;
245 break;
246 case 4096:
247 n = 12;
248 break;
249 case 8192:
250 n = 13;
251 break;
252 case 16384:
253 n = 14;
254 break;
255 case 32768:
256 n = 15;
257 break;
258 case 65536:
259 n = 16;
260 break;
261 default:
262 fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
263 alignBoundary);
265 return n;
267 #elif defined(__i386__) || defined(__sparc__)
269 return alignBoundary;
271 #else
272 #error "How does the '.align' assembler directive work on your architecture?"
273 #endif