2002-07-16 03:20:45 +00:00
|
|
|
/*
|
|
|
|
* Utility routines
|
|
|
|
*
|
|
|
|
* Copyright 1998 Bertho A. Stultiens
|
|
|
|
* Copyright 2002 Ove Kaaven
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-07-16 03:20:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2003-08-18 20:00:44 +00:00
|
|
|
#include "wine/port.h"
|
2002-07-16 03:20:45 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "widl.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "parser.h"
|
|
|
|
|
2008-04-14 10:00:20 +00:00
|
|
|
#define CURRENT_LOCATION { input_name ? input_name : "stdin", line_number, parser_text }
|
|
|
|
|
2006-09-13 23:31:47 +00:00
|
|
|
static const int want_near_indication = 0;
|
2002-07-16 03:20:45 +00:00
|
|
|
|
2006-09-13 23:31:47 +00:00
|
|
|
static void make_print(char *str)
|
2002-07-16 03:20:45 +00:00
|
|
|
{
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
if(!isprint(*str))
|
|
|
|
*str = ' ';
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-14 10:00:20 +00:00
|
|
|
static void generic_msg(const loc_info_t *loc_info, const char *s, const char *t, va_list ap)
|
2002-07-16 03:20:45 +00:00
|
|
|
{
|
2008-04-14 10:00:20 +00:00
|
|
|
fprintf(stderr, "%s:%d: %s: ", loc_info->input_name, loc_info->line_number, t);
|
2002-07-16 03:20:45 +00:00
|
|
|
vfprintf(stderr, s, ap);
|
2006-09-13 23:31:47 +00:00
|
|
|
|
|
|
|
if (want_near_indication)
|
2002-07-16 03:20:45 +00:00
|
|
|
{
|
|
|
|
char *cpy;
|
2008-04-14 10:00:20 +00:00
|
|
|
if(loc_info->near_text)
|
2002-07-16 03:20:45 +00:00
|
|
|
{
|
2008-04-14 10:00:20 +00:00
|
|
|
cpy = xstrdup(loc_info->near_text);
|
2002-07-16 03:20:45 +00:00
|
|
|
make_print(cpy);
|
|
|
|
fprintf(stderr, " near '%s'", cpy);
|
|
|
|
free(cpy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-24 03:18:18 +00:00
|
|
|
void error_loc(const char *s, ...)
|
2008-04-14 10:00:20 +00:00
|
|
|
{
|
|
|
|
loc_info_t cur_loc = CURRENT_LOCATION;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
2009-01-07 11:43:47 +00:00
|
|
|
generic_msg(&cur_loc, s, "error", ap);
|
2008-04-14 10:00:20 +00:00
|
|
|
va_end(ap);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-12-05 05:25:56 +00:00
|
|
|
/* yyerror: yacc assumes this is not newline terminated. */
|
|
|
|
void parser_error(const char *s)
|
|
|
|
{
|
|
|
|
error_loc("%s\n", s);
|
|
|
|
}
|
|
|
|
|
2008-04-14 10:00:20 +00:00
|
|
|
void error_loc_info(const loc_info_t *loc_info, const char *s, ...)
|
2007-10-24 03:18:18 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
2009-01-07 11:43:47 +00:00
|
|
|
generic_msg(loc_info, s, "error", ap);
|
2007-10-24 03:18:18 +00:00
|
|
|
va_end(ap);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2006-09-12 07:05:07 +00:00
|
|
|
int parser_warning(const char *s, ...)
|
2002-07-16 03:20:45 +00:00
|
|
|
{
|
2008-04-14 10:00:20 +00:00
|
|
|
loc_info_t cur_loc = CURRENT_LOCATION;
|
2002-07-16 03:20:45 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
2009-01-07 11:43:47 +00:00
|
|
|
generic_msg(&cur_loc, s, "warning", ap);
|
2002-07-16 03:20:45 +00:00
|
|
|
va_end(ap);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void error(const char *s, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
2005-01-05 17:30:37 +00:00
|
|
|
fprintf(stderr, "error: ");
|
2002-07-16 03:20:45 +00:00
|
|
|
vfprintf(stderr, s, ap);
|
|
|
|
va_end(ap);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void warning(const char *s, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
2005-01-05 17:30:37 +00:00
|
|
|
fprintf(stderr, "warning: ");
|
2002-07-16 03:20:45 +00:00
|
|
|
vfprintf(stderr, s, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2008-04-14 10:00:20 +00:00
|
|
|
void warning_loc_info(const loc_info_t *loc_info, const char *s, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
2009-01-07 11:43:47 +00:00
|
|
|
generic_msg(loc_info, s, "warning", ap);
|
2008-04-14 10:00:20 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2002-07-16 03:20:45 +00:00
|
|
|
void chat(const char *s, ...)
|
|
|
|
{
|
|
|
|
if(debuglevel & DEBUGLEVEL_CHAT)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
2005-01-05 17:30:37 +00:00
|
|
|
fprintf(stderr, "chat: ");
|
2002-07-16 03:20:45 +00:00
|
|
|
vfprintf(stderr, s, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *dup_basename(const char *name, const char *ext)
|
|
|
|
{
|
|
|
|
int namelen;
|
|
|
|
int extlen = strlen(ext);
|
|
|
|
char *base;
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
if(!name)
|
|
|
|
name = "widl.tab";
|
|
|
|
|
|
|
|
slash = strrchr(name, '/');
|
2008-02-07 16:41:00 +00:00
|
|
|
if (!slash)
|
|
|
|
slash = strrchr(name, '\\');
|
|
|
|
|
2002-07-16 03:20:45 +00:00
|
|
|
if (slash)
|
|
|
|
name = slash + 1;
|
|
|
|
|
|
|
|
namelen = strlen(name);
|
|
|
|
|
|
|
|
/* +4 for later extension and +1 for '\0' */
|
2006-08-26 19:39:44 +00:00
|
|
|
base = xmalloc(namelen +4 +1);
|
2002-07-16 03:20:45 +00:00
|
|
|
strcpy(base, name);
|
|
|
|
if(!strcasecmp(name + namelen-extlen, ext))
|
|
|
|
{
|
|
|
|
base[namelen - extlen] = '\0';
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2007-10-18 01:03:57 +00:00
|
|
|
size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
|
|
|
|
{
|
|
|
|
char *line = *linep;
|
|
|
|
size_t len = *lenp;
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
if (!line)
|
|
|
|
{
|
|
|
|
len = 64;
|
|
|
|
line = xmalloc(len);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(&line[n], len - n, fp))
|
|
|
|
{
|
|
|
|
n += strlen(&line[n]);
|
|
|
|
if (line[n - 1] == '\n')
|
|
|
|
break;
|
|
|
|
else if (n == len - 1)
|
|
|
|
{
|
|
|
|
len *= 2;
|
|
|
|
line = xrealloc(line, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*linep = line;
|
|
|
|
*lenp = len;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2002-07-16 03:20:45 +00:00
|
|
|
void *xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void *res;
|
|
|
|
|
|
|
|
assert(size > 0);
|
|
|
|
res = malloc(size);
|
|
|
|
if(res == NULL)
|
|
|
|
{
|
|
|
|
error("Virtual memory exhausted.\n");
|
|
|
|
}
|
2006-08-26 19:39:44 +00:00
|
|
|
memset(res, 0x55, size);
|
2002-07-16 03:20:45 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *xrealloc(void *p, size_t size)
|
|
|
|
{
|
|
|
|
void *res;
|
|
|
|
|
|
|
|
assert(size > 0);
|
|
|
|
res = realloc(p, size);
|
|
|
|
if(res == NULL)
|
|
|
|
{
|
|
|
|
error("Virtual memory exhausted.\n");
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *xstrdup(const char *str)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
assert(str != NULL);
|
2006-08-26 19:39:44 +00:00
|
|
|
s = xmalloc(strlen(str)+1);
|
2002-07-16 03:20:45 +00:00
|
|
|
return strcpy(s, str);
|
|
|
|
}
|
2010-11-27 10:42:09 +00:00
|
|
|
|
|
|
|
int strendswith(const char* str, const char* end)
|
|
|
|
{
|
|
|
|
int l = strlen(str);
|
|
|
|
int m = strlen(end);
|
|
|
|
return l >= m && strcmp(str + l - m, end) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* buffer management
|
|
|
|
*
|
|
|
|
* Function for writing to a memory buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int byte_swapped = 0;
|
|
|
|
unsigned char *output_buffer;
|
|
|
|
size_t output_buffer_pos;
|
|
|
|
size_t output_buffer_size;
|
|
|
|
|
|
|
|
static void check_output_buffer_space( size_t size )
|
|
|
|
{
|
|
|
|
if (output_buffer_pos + size >= output_buffer_size)
|
|
|
|
{
|
|
|
|
output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
|
|
|
|
output_buffer = xrealloc( output_buffer, output_buffer_size );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_output_buffer(void)
|
|
|
|
{
|
|
|
|
output_buffer_size = 1024;
|
|
|
|
output_buffer_pos = 0;
|
|
|
|
output_buffer = xmalloc( output_buffer_size );
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush_output_buffer( const char *name )
|
|
|
|
{
|
|
|
|
int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
|
|
|
|
if (fd == -1) error( "Error creating %s\n", name );
|
|
|
|
if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
|
|
|
|
error( "Error writing to %s\n", name );
|
|
|
|
close( fd );
|
|
|
|
free( output_buffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_data( const void *data, size_t size )
|
|
|
|
{
|
|
|
|
check_output_buffer_space( size );
|
|
|
|
memcpy( output_buffer + output_buffer_pos, data, size );
|
|
|
|
output_buffer_pos += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_byte( unsigned char val )
|
|
|
|
{
|
|
|
|
check_output_buffer_space( 1 );
|
|
|
|
output_buffer[output_buffer_pos++] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_word( unsigned short val )
|
|
|
|
{
|
|
|
|
if (byte_swapped) val = (val << 8) | (val >> 8);
|
|
|
|
put_data( &val, sizeof(val) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_dword( unsigned int val )
|
|
|
|
{
|
|
|
|
if (byte_swapped)
|
|
|
|
val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
|
|
|
|
put_data( &val, sizeof(val) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_qword( unsigned int val )
|
|
|
|
{
|
|
|
|
if (byte_swapped)
|
|
|
|
{
|
|
|
|
put_dword( 0 );
|
|
|
|
put_dword( val );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
put_dword( val );
|
|
|
|
put_dword( 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pointer-sized word */
|
|
|
|
void put_pword( unsigned int val )
|
|
|
|
{
|
|
|
|
if (pointer_size == 8) put_qword( val );
|
|
|
|
else put_dword( val );
|
|
|
|
}
|
|
|
|
|
|
|
|
void align_output( unsigned int align )
|
|
|
|
{
|
|
|
|
size_t size = align - (output_buffer_pos % align);
|
|
|
|
|
|
|
|
if (size == align) return;
|
|
|
|
check_output_buffer_space( size );
|
|
|
|
memset( output_buffer + output_buffer_pos, 0, size );
|
|
|
|
output_buffer_pos += size;
|
|
|
|
}
|