ReactOS 0.4.15-dev-7968-g24a56f8
utils.c File Reference
#include "config.h"
#include "wine/port.h"
#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"
Include dependency graph for utils.c:

Go to the source code of this file.

Classes

struct  resource
 

Macros

#define CURRENT_LOCATION   { input_name ? input_name : "stdin", line_number, parser_text }
 

Functions

static void make_print (char *str)
 
static void generic_msg (const loc_info_t *loc_info, const char *s, const char *t, va_list ap)
 
void error_loc (const char *s,...)
 
void parser_error (const char *s)
 
void error_loc_info (const loc_info_t *loc_info, const char *s,...)
 
int parser_warning (const char *s,...)
 
void error (const char *s,...)
 
void warning (const char *s,...)
 
void warning_loc_info (const loc_info_t *loc_info, const char *s,...)
 
void chat (const char *s,...)
 
chardup_basename (const char *name, const char *ext)
 
size_t widl_getline (char **linep, size_t *lenp, FILE *fp)
 
voidxmalloc (size_t size)
 
voidxrealloc (void *p, size_t size)
 
charxstrdup (const char *str)
 
int strendswith (const char *str, const char *end)
 
static void check_output_buffer_space (size_t size)
 
void init_output_buffer (void)
 
void flush_output_buffer (const char *name)
 
static void put_resource_id (const char *str)
 
void add_output_to_resources (const char *type, const char *name)
 
void flush_output_resources (const char *name)
 
void put_data (const void *data, size_t size)
 
void put_byte (unsigned char val)
 
void put_word (unsigned short val)
 
void put_dword (unsigned int val)
 
void put_qword (unsigned int val)
 
void put_pword (unsigned int val)
 
void put_str (int indent, const char *format,...)
 
void align_output (unsigned int align)
 

Variables

static const int want_near_indication = 0
 
int byte_swapped = 0
 
unsigned charoutput_buffer
 
size_t output_buffer_pos
 
size_t output_buffer_size
 
static struct resource resources [16]
 
static unsigned int nb_resources
 

Macro Definition Documentation

◆ CURRENT_LOCATION

#define CURRENT_LOCATION   { input_name ? input_name : "stdin", line_number, parser_text }

Definition at line 36 of file utils.c.

Function Documentation

◆ add_output_to_resources()

void add_output_to_resources ( const char type,
const char name 
)

Definition at line 307 of file utils.c.

308{
309 size_t data_size = output_buffer_pos;
310 size_t header_size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
311
312 assert( nb_resources < sizeof(resources)/sizeof(resources[0]) );
313
314 if (type[0] != '#') header_size += (strlen( type ) + 1) * sizeof(unsigned short);
315 else header_size += 2 * sizeof(unsigned short);
316 if (name[0] != '#') header_size += (strlen( name ) + 1) * sizeof(unsigned short);
317 else header_size += 2 * sizeof(unsigned short);
318
319 header_size = (header_size + 3) & ~3;
320 align_output( 4 );
321 check_output_buffer_space( header_size );
322 resources[nb_resources].size = header_size + output_buffer_pos;
324
326 put_dword( data_size ); /* ResSize */
327 put_dword( header_size ); /* HeaderSize */
328 put_resource_id( type ); /* ResType */
329 put_resource_id( name ); /* ResName */
330 align_output( 4 );
331 put_dword( 0 ); /* DataVersion */
332 put_word( 0 ); /* Memory options */
333 put_word( 0 ); /* Language */
334 put_dword( 0 ); /* Version */
335 put_dword( 0 ); /* Characteristics */
336
339}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
unsigned short(__cdecl typeof(TIFFCurrentDirectory))(struct tiff *)
Definition: typeof.h:94
#define assert(x)
Definition: debug.h:53
FxCmResList * resources
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
void align_output(unsigned int align)
Definition: utils.c:449
size_t output_buffer_pos
Definition: utils.c:253
void put_word(unsigned short val)
Definition: utils.c:389
void put_dword(unsigned int val)
Definition: utils.c:395
void init_output_buffer(void)
Definition: utils.c:272
static void put_resource_id(const char *str)
Definition: utils.c:289
static unsigned int nb_resources
Definition: utils.c:261
static void check_output_buffer_space(size_t size)
Definition: utils.c:263
unsigned char * output_buffer
Definition: utils.c:252
Definition: name.c:39

Referenced by output_typelib_regscript(), save_all_changes(), and write_regscript().

◆ align_output()

void align_output ( unsigned int  align)

Definition at line 449 of file utils.c.

450{
451 size_t size = align - (output_buffer_pos % align);
452
453 if (size == align) return;
457}
int align(int length, int align)
Definition: dsound8.c:36
GLsizeiptr size
Definition: glext.h:5919
#define memset(x, y, z)
Definition: compat.h:39

Referenced by add_output_to_resources().

◆ chat()

void chat ( const char s,
  ... 
)

Definition at line 131 of file utils.c.

132{
134 {
135 va_list ap;
136 va_start(ap, s);
137 fprintf(stderr, "chat: ");
138 vfprintf(stderr, s, ap);
139 va_end(ap);
140 }
141}
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
GLdouble s
Definition: gl.h:2039
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl vfprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
int debuglevel
Definition: widl.c:110
#define DEBUGLEVEL_CHAT
Definition: widl.h:30
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36

Referenced by add_block(), add_func_desc(), add_importlib(), add_index(), add_interface_typeinfo(), add_name(), add_structure_typeinfo(), add_type_typeinfo(), add_var_desc(), alloc_importinfo(), create_msft_typeinfo(), ctl2_alloc_guid(), dump_type(), dump_var_desc(), encode_type(), encode_var(), find_importinfo(), get_param_flags(), get_type_vt(), local_href(), main(), save_all_changes(), sltg_add_typeinfo(), sltg_write_header(), sltg_write_helpstrings(), sltg_write_nametable(), sltg_write_typeinfo(), write_default_value(), and write_var_desc().

◆ check_output_buffer_space()

static void check_output_buffer_space ( size_t  size)
static

Definition at line 263 of file utils.c.

264{
266 {
269 }
270}
size_t output_buffer_size
Definition: utils.c:254
void * xrealloc(void *p, size_t size)
Definition: utils.c:216
#define max(a, b)
Definition: svc.c:63

Referenced by add_output_to_resources(), align_output(), put_byte(), put_data(), and put_str().

◆ dup_basename()

char * dup_basename ( const char name,
const char ext 
)

Definition at line 143 of file utils.c.

144{
145 int namelen;
146 int extlen = strlen(ext);
147 char *base;
148 char *slash;
149
150 if(!name)
151 name = "widl.tab";
152
153 slash = strrchr(name, '/');
154 if (!slash)
155 slash = strrchr(name, '\\');
156
157 if (slash)
158 name = slash + 1;
159
161
162 /* +6 for later extension (strlen("_r.rgs")) and +1 for '\0' */
163 base = xmalloc(namelen +6 +1);
164 strcpy(base, name);
165 if(!strcasecmp(name + namelen-extlen, ext))
166 {
167 base[namelen - extlen] = '\0';
168 }
169 return base;
170}
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
static const WCHAR *const ext[]
Definition: module.c:53
#define strcasecmp
Definition: fake.h:9
GLint namelen
Definition: glext.h:7232
_Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
void * xmalloc(size_t size)
Definition: utils.c:201

Referenced by add_filename_node(), dup_basename_token(), main(), and write_import().

◆ error()

void error ( const char s,
  ... 
)

Definition at line 104 of file utils.c.

105{
106 va_list ap;
107 va_start(ap, s);
108 fprintf(stderr, "error: ");
109 vfprintf(stderr, s, ap);
110 va_end(ap);
111 exit(2);
112}
#define exit(n)
Definition: config.h:202

◆ error_loc()

void error_loc ( const char s,
  ... 
)

Definition at line 69 of file utils.c.

70{
72 va_list ap;
73 va_start(ap, s);
74 generic_msg(&cur_loc, s, "error", ap);
75 va_end(ap);
76 exit(1);
77}
#define CURRENT_LOCATION
Definition: utils.c:36
static void generic_msg(const loc_info_t *loc_info, const char *s, const char *t, va_list ap)
Definition: utils.c:50

Referenced by get_struct_fc(), make_expr2(), make_exprs(), make_exprt(), parser_error(), type_dispinterface_define(), type_dispinterface_define_from_iface(), type_module_define(), type_new_bitfield(), type_new_coclass(), type_new_function(), type_new_module(), and write_locals().

◆ error_loc_info()

void error_loc_info ( const loc_info_t loc_info,
const char s,
  ... 
)

Definition at line 85 of file utils.c.

86{
87 va_list ap;
88 va_start(ap, s);
89 generic_msg(loc_info, s, "error", ap);
90 va_end(ap);
91 exit(1);
92}

Referenced by check_arithmetic_type(), check_integer_type(), check_scalar_type(), and resolve_expression().

◆ flush_output_buffer()

void flush_output_buffer ( const char name)

Definition at line 279 of file utils.c.

280{
281 int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
282 if (fd == -1) error( "Error creating %s\n", name );
284 error( "Error writing to %s\n", name );
285 close( fd );
287}
#define O_WRONLY
Definition: acwin.h:111
#define O_CREAT
Definition: acwin.h:110
#define O_BINARY
Definition: acwin.h:109
#define open
Definition: acwin.h:95
#define close
Definition: acwin.h:98
#define write
Definition: acwin.h:97
#define O_TRUNC
Definition: acwin.h:112
#define free
Definition: debug_ros.c:5
#define error(str)
Definition: mkdosfs.c:1605
static int fd
Definition: io.c:51

◆ flush_output_resources()

void flush_output_resources ( const char name)

Definition at line 341 of file utils.c.

342{
343 int fd;
344 unsigned int i;
345
346 /* all output must have been saved with add_output_to_resources() first */
348
349 put_dword( 0 ); /* ResSize */
350 put_dword( 32 ); /* HeaderSize */
351 put_word( 0xffff ); /* ResType */
352 put_word( 0x0000 );
353 put_word( 0xffff ); /* ResName */
354 put_word( 0x0000 );
355 put_dword( 0 ); /* DataVersion */
356 put_word( 0 ); /* Memory options */
357 put_word( 0 ); /* Language */
358 put_dword( 0 ); /* Version */
359 put_dword( 0 ); /* Characteristics */
360
361 fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
362 if (fd == -1) error( "Error creating %s\n", name );
364 error( "Error writing to %s\n", name );
365 for (i = 0; i < nb_resources; i++)
366 {
368 error( "Error writing to %s\n", name );
369 free( resources[i].data );
370 }
371 close( fd );
372 nb_resources = 0;
374}
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248

Referenced by save_all_changes(), write_regscript(), and write_typelib_regscript().

◆ generic_msg()

static void generic_msg ( const loc_info_t loc_info,
const char s,
const char t,
va_list  ap 
)
static

Definition at line 50 of file utils.c.

51{
52 fprintf(stderr, "%s:%d: %s: ", loc_info->input_name, loc_info->line_number, t);
54
56 {
57 char *cpy;
58 if(loc_info->near_text)
59 {
60 cpy = xstrdup(loc_info->near_text);
61 make_print(cpy);
62 fprintf(stderr, " near '%s'", cpy);
63 free(cpy);
64 }
65 }
66}
GLdouble GLdouble t
Definition: gl.h:2047
char * xstrdup(const char *str)
Definition: utils.c:229
static void make_print(char *str)
Definition: utils.c:40
static const int want_near_indication
Definition: utils.c:38
int line_number
Definition: widltypes.h:287
const char * near_text
Definition: widltypes.h:288
const char * input_name
Definition: widltypes.h:286

Referenced by error_loc(), error_loc_info(), parser_warning(), and warning_loc_info().

◆ init_output_buffer()

void init_output_buffer ( void  )

◆ make_print()

static void make_print ( char str)
static

Definition at line 40 of file utils.c.

41{
42 while(*str)
43 {
44 if(!isprint(*str))
45 *str = ' ';
46 str++;
47 }
48}
#define isprint(c)
Definition: acclib.h:73
const WCHAR * str

Referenced by generic_msg().

◆ parser_error()

void parser_error ( const char s)

Definition at line 80 of file utils.c.

81{
82 error_loc("%s\n", s);
83}
void error_loc(const char *s,...)
Definition: utils.c:69

◆ parser_warning()

int parser_warning ( const char s,
  ... 
)

Definition at line 94 of file utils.c.

95{
97 va_list ap;
98 va_start(ap, s);
99 generic_msg(&cur_loc, s, "warning", ap);
100 va_end(ap);
101 return 0;
102}

◆ put_byte()

void put_byte ( unsigned char  val)

Definition at line 383 of file utils.c.

384{
387}
GLuint GLfloat * val
Definition: glext.h:7180

◆ put_data()

◆ put_dword()

void put_dword ( unsigned int  val)

Definition at line 395 of file utils.c.

396{
397 if (byte_swapped)
398 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
399 put_data( &val, sizeof(val) );
400}
void put_data(const void *data, size_t size)
Definition: utils.c:376
int byte_swapped
Definition: utils.c:251

Referenced by add_output_to_resources(), flush_output_resources(), put_pword(), and put_qword().

◆ put_pword()

void put_pword ( unsigned int  val)

Definition at line 417 of file utils.c.

418{
419 if (pointer_size == 8) put_qword( val );
420 else put_dword( val );
421}
void put_qword(unsigned int val)
Definition: utils.c:402
unsigned int pointer_size
Definition: widl.c:158

◆ put_qword()

void put_qword ( unsigned int  val)

Definition at line 402 of file utils.c.

403{
404 if (byte_swapped)
405 {
406 put_dword( 0 );
407 put_dword( val );
408 }
409 else
410 {
411 put_dword( val );
412 put_dword( 0 );
413 }
414}

Referenced by put_pword().

◆ put_resource_id()

static void put_resource_id ( const char str)
inlinestatic

Definition at line 289 of file utils.c.

290{
291 if (str[0] != '#')
292 {
293 while (*str)
294 {
295 unsigned char ch = *str++;
296 put_word( toupper(ch) );
297 }
298 put_word( 0 );
299 }
300 else
301 {
302 put_word( 0xffff );
303 put_word( atoi( str + 1 ));
304 }
305}
int toupper(int c)
Definition: utclib.c:881
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)

Referenced by add_output_to_resources().

◆ put_str()

void put_str ( int  indent,
const char format,
  ... 
)

Definition at line 423 of file utils.c.

424{
425 int n;
427
431
432 for (;;)
433 {
435 va_start( args, format );
437 va_end( args );
438 if (n == -1) size *= 2;
439 else if ((size_t)n >= size) size = n + 1;
440 else
441 {
443 return;
444 }
446 }
447}
static const WCHAR indent[]
Definition: object.c:1156
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
GLdouble n
Definition: glext.h:7729
#define args
Definition: format.c:66
Definition: match.c:390
#define vsnprintf
Definition: tif_win32.c:406

Referenced by output_typelib_regscript(), write_coclass(), write_interface(), write_progid(), write_regscript(), and write_typelib_interface().

◆ put_word()

void put_word ( unsigned short  val)

Definition at line 389 of file utils.c.

390{
391 if (byte_swapped) val = (val << 8) | (val >> 8);
392 put_data( &val, sizeof(val) );
393}

Referenced by add_output_to_resources(), flush_output_resources(), and put_resource_id().

◆ strendswith()

int strendswith ( const char str,
const char end 
)

Definition at line 238 of file utils.c.

239{
240 int l = strlen(str);
241 int m = strlen(end);
242 return l >= m && strcmp(str + l - m, end) == 0;
243}
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
r l[0]
Definition: byte_order.h:168
GLuint GLuint end
Definition: gl.h:1545
const GLfloat * m
Definition: glext.h:10848

Referenced by main(), save_all_changes(), write_regscript(), and write_typelib_regscript().

◆ warning()

void warning ( const char s,
  ... 
)

Definition at line 114 of file utils.c.

115{
116 va_list ap;
117 va_start(ap, s);
118 fprintf(stderr, "warning: ");
119 vfprintf(stderr, s, ap);
120 va_end(ap);
121}

◆ warning_loc_info()

void warning_loc_info ( const loc_info_t loc_info,
const char s,
  ... 
)

Definition at line 123 of file utils.c.

124{
125 va_list ap;
126 va_start(ap, s);
127 generic_msg(loc_info, s, "warning", ap);
128 va_end(ap);
129}

◆ widl_getline()

size_t widl_getline ( char **  linep,
size_t lenp,
FILE fp 
)

Definition at line 172 of file utils.c.

173{
174 char *line = *linep;
175 size_t len = *lenp;
176 size_t n = 0;
177
178 if (!line)
179 {
180 len = 64;
181 line = xmalloc(len);
182 }
183
184 while (fgets(&line[n], len - n, fp))
185 {
186 n += strlen(&line[n]);
187 if (line[n - 1] == '\n')
188 break;
189 else if (n == len - 1)
190 {
191 len *= 2;
192 line = xrealloc(line, len);
193 }
194 }
195
196 *linep = line;
197 *lenp = len;
198 return n;
199}
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
Definition: parser.c:49

Referenced by write_dlldata().

◆ xmalloc()

void * xmalloc ( size_t  size)

Definition at line 201 of file utils.c.

202{
203 void *res;
204
205 assert(size > 0);
206 res = malloc(size);
207 if(res == NULL)
208 {
209 error("Virtual memory exhausted.\n");
210 }
211 memset(res, 0x55, size);
212 return res;
213}
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
GLuint res
Definition: glext.h:9613

Referenced by dup_basename(), init_output_buffer(), widl_getline(), and xstrdup().

◆ xrealloc()

void * xrealloc ( void p,
size_t  size 
)

◆ xstrdup()

Variable Documentation

◆ byte_swapped

int byte_swapped = 0

Definition at line 251 of file utils.c.

Referenced by put_dword(), put_qword(), put_word(), and save_all_changes().

◆ nb_resources

unsigned int nb_resources
static

Definition at line 261 of file utils.c.

Referenced by add_output_to_resources(), and flush_output_resources().

◆ output_buffer

Definition at line 252 of file utils.c.

Referenced by add_output_to_resources().

◆ output_buffer_pos

◆ output_buffer_size

size_t output_buffer_size

Definition at line 254 of file utils.c.

Referenced by check_output_buffer_space(), init_output_buffer(), and put_str().

◆ resources

struct resource resources[16]
static

◆ want_near_indication

const int want_near_indication = 0
static

Definition at line 38 of file utils.c.

Referenced by generic_msg().