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
22 #include "wine/port.h"
32 void *xmalloc (size_t size
)
36 res
= malloc (size
? size
: 1);
39 fprintf (stderr
, "Virtual memory exhausted.\n");
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");
56 char *xstrdup( const char *str
)
58 char *res
= strdup( str
);
61 fprintf (stderr
, "Virtual memory exhausted.\n");
67 char *strupper(char *s
)
70 for (p
= s
; *p
; p
++) *p
= toupper(*p
);
74 int strendswith(const char* str
, const char* end
)
78 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
81 void fatal_error( const char *msg
, ... )
84 va_start( valist
, msg
);
87 fprintf( stderr
, "%s:", input_file_name
);
89 fprintf( stderr
, "%d:", current_line
);
92 else fprintf( stderr
, "winebuild: " );
93 vfprintf( stderr
, msg
, valist
);
98 void fatal_perror( const char *msg
, ... )
101 va_start( valist
, msg
);
104 fprintf( stderr
, "%s:", input_file_name
);
106 fprintf( stderr
, "%d:", current_line
);
107 fputc( ' ', stderr
);
109 vfprintf( stderr
, msg
, valist
);
115 void error( const char *msg
, ... )
118 va_start( valist
, msg
);
121 fprintf( stderr
, "%s:", input_file_name
);
123 fprintf( stderr
, "%d:", current_line
);
124 fputc( ' ', stderr
);
126 vfprintf( stderr
, msg
, valist
);
131 void warning( const char *msg
, ... )
135 if (!display_warnings
) return;
136 va_start( valist
, msg
);
139 fprintf( stderr
, "%s:", input_file_name
);
141 fprintf( stderr
, "%d:", current_line
);
142 fputc( ' ', stderr
);
144 fprintf( stderr
, "warning: " );
145 vfprintf( stderr
, msg
, valist
);
149 /* output a standard header for generated files */
150 void output_standard_file_header( FILE *outfile
)
153 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n",
156 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
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
)
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 /*******************************************************************
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
)
187 FILE *file
= fopen( name
, "r" );
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
;
206 /*******************************************************************
209 * Close the current input file (must have been opened with open_input_file).
211 void close_input_file( FILE *file
)
214 free( input_file_name
);
215 input_file_name
= NULL
;
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;
233 return atoi( end
+ 1 );
237 /*******************************************************************
240 * Create a new dll spec file descriptor
242 DLLSPEC
*alloc_dll_spec(void)
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
;
254 spec
->stack_size
= 0;
256 spec
->nb_entry_points
= 0;
257 spec
->alloc_entry_points
= 0;
259 spec
->nb_resources
= 0;
260 spec
->characteristics
= 0;
262 spec
->subsystem_major
= 4;
263 spec
->subsystem_minor
= 0;
264 spec
->entry_points
= NULL
;
266 spec
->ordinals
= NULL
;
267 spec
->resources
= NULL
;
272 /*******************************************************************
275 * Free dll spec file descriptor
277 void free_dll_spec( DLLSPEC
*spec
)
281 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
283 ORDDEF
*odp
= &spec
->entry_points
[i
];
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
);
294 free( spec
->ordinals
);
295 free( spec
->resources
);
300 /*******************************************************************
303 * Map a string to a valid C identifier.
305 const char *make_c_identifier( const char *str
)
307 static char buffer
[256];
310 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
312 if (isalnum(*str
)) *p
= *str
;
320 /*****************************************************************
321 * Function: get_alignment
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
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
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.
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__)
356 switch(alignBoundary
)
407 fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
412 #elif defined(__i386__) || defined(__sparc__)
414 return alignBoundary
;
417 #error "How does the '.align' assembler directive work on your architecture?"