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
35 void error_at( const struct location
*where
, const char *s
, ... )
41 vsnprintf( buffer
, sizeof(buffer
), s
, ap
);
44 parser_error( where
, buffer
);
48 void error(const char *s
, ...)
52 fprintf(stderr
, "error: ");
53 vfprintf(stderr
, s
, ap
);
58 void warning(const char *s
, ...)
62 fprintf(stderr
, "warning: ");
63 vfprintf(stderr
, s
, ap
);
67 void warning_at( const struct location
*where
, const char *s
, ... )
73 vsnprintf( buffer
, sizeof(buffer
), s
, ap
);
76 parser_warning( where
, buffer
);
79 void chat(const char *s
, ...)
81 if(debuglevel
& DEBUGLEVEL_CHAT
)
85 fprintf(stderr
, "chat: ");
86 vfprintf(stderr
, s
, ap
);
91 size_t widl_getline(char **linep
, size_t *lenp
, FILE *fp
)
103 while (fgets(&line
[n
], len
- n
, fp
))
105 n
+= strlen(&line
[n
]);
106 if (line
[n
- 1] == '\n')
108 else if (n
== len
- 1)
111 line
= xrealloc(line
, len
);
120 size_t strappend(char **buf
, size_t *len
, size_t pos
, const char* fmt
, ...)
127 assert( buf
&& len
);
128 assert( (*len
== 0 && *buf
== NULL
) || (*len
!= 0 && *buf
!= NULL
) );
138 ptr
= xmalloc( size
);
144 n
= vsnprintf( ptr
+ pos
, size
- pos
, fmt
, ap
);
146 if (n
== -1) size
*= 2;
147 else if (pos
+ (size_t)n
>= size
) size
= pos
+ n
+ 1;
149 ptr
= xrealloc( ptr
, size
);
157 /*******************************************************************
160 * Function for writing to a memory buffer.
163 unsigned char *output_buffer
;
164 size_t output_buffer_pos
;
165 size_t output_buffer_size
;
167 static struct resource
172 static unsigned int nb_resources
;
174 static inline void put_resource_id( const char *str
)
180 unsigned char ch
= *str
++;
181 put_word( toupper(ch
) );
188 put_word( atoi( str
+ 1 ));
192 void add_output_to_resources( const char *type
, const char *name
)
194 size_t data_size
= output_buffer_pos
;
195 size_t header_size
= 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
197 assert( nb_resources
< ARRAY_SIZE( resources
));
199 if (type
[0] != '#') header_size
+= (strlen( type
) + 1) * sizeof(unsigned short);
200 else header_size
+= 2 * sizeof(unsigned short);
201 if (name
[0] != '#') header_size
+= (strlen( name
) + 1) * sizeof(unsigned short);
202 else header_size
+= 2 * sizeof(unsigned short);
204 header_size
= (header_size
+ 3) & ~3;
206 check_output_buffer_space( header_size
);
207 resources
[nb_resources
].size
= header_size
+ output_buffer_pos
;
208 memmove( output_buffer
+ header_size
, output_buffer
, output_buffer_pos
);
210 output_buffer_pos
= 0;
211 put_dword( data_size
); /* ResSize */
212 put_dword( header_size
); /* HeaderSize */
213 put_resource_id( type
); /* ResType */
214 put_resource_id( name
); /* ResName */
216 put_dword( 0 ); /* DataVersion */
217 put_word( 0 ); /* Memory options */
218 put_word( 0 ); /* Language */
219 put_dword( 0 ); /* Version */
220 put_dword( 0 ); /* Characteristics */
222 resources
[nb_resources
++].data
= output_buffer
;
223 init_output_buffer();
226 void flush_output_resources( const char *name
)
230 /* all output must have been saved with add_output_to_resources() first */
231 assert( !output_buffer_pos
);
233 put_dword( 0 ); /* ResSize */
234 put_dword( 32 ); /* HeaderSize */
235 put_word( 0xffff ); /* ResType */
237 put_word( 0xffff ); /* ResName */
239 put_dword( 0 ); /* DataVersion */
240 put_word( 0 ); /* Memory options */
241 put_word( 0 ); /* Language */
242 put_dword( 0 ); /* Version */
243 put_dword( 0 ); /* Characteristics */
245 for (i
= 0; i
< nb_resources
; i
++)
247 put_data( resources
[i
].data
, resources
[i
].size
);
248 free( resources
[i
].data
);
250 flush_output_buffer( name
);
254 /* pointer-sized word */
255 void put_pword( unsigned int val
)
257 if (pointer_size
== 8) put_qword( val
);
258 else put_dword( val
);
261 void put_str( int indent
, const char *format
, ... )
266 check_output_buffer_space( 4 * indent
);
267 memset( output_buffer
+ output_buffer_pos
, ' ', 4 * indent
);
268 output_buffer_pos
+= 4 * indent
;
272 size_t size
= output_buffer_size
- output_buffer_pos
;
273 va_start( args
, format
);
274 n
= vsnprintf( (char *)output_buffer
+ output_buffer_pos
, size
, format
, args
);
276 if (n
== -1) size
*= 2;
277 else if ((size_t)n
>= size
) size
= n
+ 1;
280 output_buffer_pos
+= n
;
283 check_output_buffer_space( size
);