4 * Copyright 1998 Bertho A. Stultiens
5 * Copyright 2002 Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
36 #define CURRENT_LOCATION { input_name ? input_name : "stdin", line_number, parser_text }
38 static const int want_near_indication
= 0;
40 static void make_print(char *str
)
50 static void generic_msg(const loc_info_t
*loc_info
, const char *s
, const char *t
, va_list ap
)
52 fprintf(stderr
, "%s:%d: %s: ", loc_info
->input_name
, loc_info
->line_number
, t
);
53 vfprintf(stderr
, s
, ap
);
55 if (want_near_indication
)
58 if(loc_info
->near_text
)
60 cpy
= xstrdup(loc_info
->near_text
);
62 fprintf(stderr
, " near '%s'", cpy
);
69 void error_loc(const char *s
, ...)
71 loc_info_t cur_loc
= CURRENT_LOCATION
;
74 generic_msg(&cur_loc
, s
, "error", ap
);
79 /* yyerror: yacc assumes this is not newline terminated. */
80 void parser_error(const char *s
)
85 void error_loc_info(const loc_info_t
*loc_info
, const char *s
, ...)
89 generic_msg(loc_info
, s
, "error", ap
);
94 int parser_warning(const char *s
, ...)
96 loc_info_t cur_loc
= CURRENT_LOCATION
;
99 generic_msg(&cur_loc
, s
, "warning", ap
);
104 void error(const char *s
, ...)
108 fprintf(stderr
, "error: ");
109 vfprintf(stderr
, s
, ap
);
114 void warning(const char *s
, ...)
118 fprintf(stderr
, "warning: ");
119 vfprintf(stderr
, s
, ap
);
123 void warning_loc_info(const loc_info_t
*loc_info
, const char *s
, ...)
127 generic_msg(loc_info
, s
, "warning", ap
);
131 void chat(const char *s
, ...)
133 if(debuglevel
& DEBUGLEVEL_CHAT
)
137 fprintf(stderr
, "chat: ");
138 vfprintf(stderr
, s
, ap
);
143 char *dup_basename(const char *name
, const char *ext
)
146 int extlen
= strlen(ext
);
153 slash
= strrchr(name
, '/');
155 slash
= strrchr(name
, '\\');
160 namelen
= strlen(name
);
162 /* +4 for later extension and +1 for '\0' */
163 base
= xmalloc(namelen
+4 +1);
165 if(!strcasecmp(name
+ namelen
-extlen
, ext
))
167 base
[namelen
- extlen
] = '\0';
172 size_t widl_getline(char **linep
, size_t *lenp
, FILE *fp
)
184 while (fgets(&line
[n
], len
- n
, fp
))
186 n
+= strlen(&line
[n
]);
187 if (line
[n
- 1] == '\n')
189 else if (n
== len
- 1)
192 line
= xrealloc(line
, len
);
201 void *xmalloc(size_t size
)
209 error("Virtual memory exhausted.\n");
211 memset(res
, 0x55, size
);
216 void *xrealloc(void *p
, size_t size
)
221 res
= realloc(p
, size
);
224 error("Virtual memory exhausted.\n");
229 char *xstrdup(const char *str
)
234 s
= xmalloc(strlen(str
)+1);
235 return strcpy(s
, str
);
238 int strendswith(const char* str
, const char* end
)
242 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
245 /*******************************************************************
248 * Function for writing to a memory buffer.
251 int byte_swapped
= 0;
252 unsigned char *output_buffer
;
253 size_t output_buffer_pos
;
254 size_t output_buffer_size
;
256 static void check_output_buffer_space( size_t size
)
258 if (output_buffer_pos
+ size
>= output_buffer_size
)
260 output_buffer_size
= max( output_buffer_size
* 2, output_buffer_pos
+ size
);
261 output_buffer
= xrealloc( output_buffer
, output_buffer_size
);
265 void init_output_buffer(void)
267 output_buffer_size
= 1024;
268 output_buffer_pos
= 0;
269 output_buffer
= xmalloc( output_buffer_size
);
272 void flush_output_buffer( const char *name
)
274 int fd
= open( name
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666 );
275 if (fd
== -1) error( "Error creating %s\n", name
);
276 if (write( fd
, output_buffer
, output_buffer_pos
) != output_buffer_pos
)
277 error( "Error writing to %s\n", name
);
279 free( output_buffer
);
282 void put_data( const void *data
, size_t size
)
284 check_output_buffer_space( size
);
285 memcpy( output_buffer
+ output_buffer_pos
, data
, size
);
286 output_buffer_pos
+= size
;
289 void put_byte( unsigned char val
)
291 check_output_buffer_space( 1 );
292 output_buffer
[output_buffer_pos
++] = val
;
295 void put_word( unsigned short val
)
297 if (byte_swapped
) val
= (val
<< 8) | (val
>> 8);
298 put_data( &val
, sizeof(val
) );
301 void put_dword( unsigned int val
)
304 val
= ((val
<< 24) | ((val
<< 8) & 0x00ff0000) | ((val
>> 8) & 0x0000ff00) | (val
>> 24));
305 put_data( &val
, sizeof(val
) );
308 void put_qword( unsigned int val
)
322 /* pointer-sized word */
323 void put_pword( unsigned int val
)
325 if (pointer_size
== 8) put_qword( val
);
326 else put_dword( val
);
329 void put_str( int indent
, const char *format
, ... )
334 check_output_buffer_space( 4 * indent
);
335 memset( output_buffer
+ output_buffer_pos
, ' ', 4 * indent
);
336 output_buffer_pos
+= 4 * indent
;
340 size_t size
= output_buffer_size
- output_buffer_pos
;
341 va_start( args
, format
);
342 n
= vsnprintf( (char *)output_buffer
+ output_buffer_pos
, size
, format
, args
);
344 if (n
== -1) size
*= 2;
345 else if ((size_t)n
>= size
) size
= n
+ 1;
348 output_buffer_pos
+= n
;
351 check_output_buffer_space( size
);
355 void align_output( unsigned int align
)
357 size_t size
= align
- (output_buffer_pos
% align
);
359 if (size
== align
) return;
360 check_output_buffer_space( size
);
361 memset( output_buffer
+ output_buffer_pos
, 0, size
);
362 output_buffer_pos
+= size
;