utils.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Utility routines
  3. *
  4. * Copyright 1998 Bertho A. Stultiens
  5. * Copyright 2002 Ove Kaaven
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #include "config.h"
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include "widl.h"
  29. #include "utils.h"
  30. #include "parser.h"
  31. #define CURRENT_LOCATION { input_name ? input_name : "stdin", line_number, parser_text }
  32. static const int want_near_indication = 0;
  33. static void make_print(char *str)
  34. {
  35. while(*str)
  36. {
  37. if(!isprint(*str))
  38. *str = ' ';
  39. str++;
  40. }
  41. }
  42. static void generic_msg(const loc_info_t *loc_info, const char *s, const char *t, va_list ap)
  43. {
  44. fprintf(stderr, "%s:%d: %s: ", loc_info->input_name, loc_info->line_number, t);
  45. vfprintf(stderr, s, ap);
  46. if (want_near_indication)
  47. {
  48. char *cpy;
  49. if(loc_info->near_text)
  50. {
  51. cpy = xstrdup(loc_info->near_text);
  52. make_print(cpy);
  53. fprintf(stderr, " near '%s'", cpy);
  54. free(cpy);
  55. }
  56. }
  57. }
  58. void error_loc(const char *s, ...)
  59. {
  60. loc_info_t cur_loc = CURRENT_LOCATION;
  61. va_list ap;
  62. va_start(ap, s);
  63. generic_msg(&cur_loc, s, "error", ap);
  64. va_end(ap);
  65. exit(1);
  66. }
  67. /* yyerror: yacc assumes this is not newline terminated. */
  68. void parser_error(const char *s)
  69. {
  70. error_loc("%s\n", s);
  71. }
  72. void error_loc_info(const loc_info_t *loc_info, const char *s, ...)
  73. {
  74. va_list ap;
  75. va_start(ap, s);
  76. generic_msg(loc_info, s, "error", ap);
  77. va_end(ap);
  78. exit(1);
  79. }
  80. int parser_warning(const char *s, ...)
  81. {
  82. loc_info_t cur_loc = CURRENT_LOCATION;
  83. va_list ap;
  84. va_start(ap, s);
  85. generic_msg(&cur_loc, s, "warning", ap);
  86. va_end(ap);
  87. return 0;
  88. }
  89. void error(const char *s, ...)
  90. {
  91. va_list ap;
  92. va_start(ap, s);
  93. fprintf(stderr, "error: ");
  94. vfprintf(stderr, s, ap);
  95. va_end(ap);
  96. exit(2);
  97. }
  98. void warning(const char *s, ...)
  99. {
  100. va_list ap;
  101. va_start(ap, s);
  102. fprintf(stderr, "warning: ");
  103. vfprintf(stderr, s, ap);
  104. va_end(ap);
  105. }
  106. void warning_loc_info(const loc_info_t *loc_info, const char *s, ...)
  107. {
  108. va_list ap;
  109. va_start(ap, s);
  110. generic_msg(loc_info, s, "warning", ap);
  111. va_end(ap);
  112. }
  113. void chat(const char *s, ...)
  114. {
  115. if(debuglevel & DEBUGLEVEL_CHAT)
  116. {
  117. va_list ap;
  118. va_start(ap, s);
  119. fprintf(stderr, "chat: ");
  120. vfprintf(stderr, s, ap);
  121. va_end(ap);
  122. }
  123. }
  124. size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
  125. {
  126. char *line = *linep;
  127. size_t len = *lenp;
  128. size_t n = 0;
  129. if (!line)
  130. {
  131. len = 64;
  132. line = xmalloc(len);
  133. }
  134. while (fgets(&line[n], len - n, fp))
  135. {
  136. n += strlen(&line[n]);
  137. if (line[n - 1] == '\n')
  138. break;
  139. else if (n == len - 1)
  140. {
  141. len *= 2;
  142. line = xrealloc(line, len);
  143. }
  144. }
  145. *linep = line;
  146. *lenp = len;
  147. return n;
  148. }
  149. size_t strappend(char **buf, size_t *len, size_t pos, const char* fmt, ...)
  150. {
  151. size_t size;
  152. va_list ap;
  153. char *ptr;
  154. int n;
  155. assert( buf && len );
  156. assert( (*len == 0 && *buf == NULL) || (*len != 0 && *buf != NULL) );
  157. if (*buf)
  158. {
  159. size = *len;
  160. ptr = *buf;
  161. }
  162. else
  163. {
  164. size = 100;
  165. ptr = xmalloc( size );
  166. }
  167. for (;;)
  168. {
  169. va_start( ap, fmt );
  170. n = vsnprintf( ptr + pos, size - pos, fmt, ap );
  171. va_end( ap );
  172. if (n == -1) size *= 2;
  173. else if (pos + (size_t)n >= size) size = pos + n + 1;
  174. else break;
  175. ptr = xrealloc( ptr, size );
  176. }
  177. *len = size;
  178. *buf = ptr;
  179. return n;
  180. }
  181. /*******************************************************************
  182. * buffer management
  183. *
  184. * Function for writing to a memory buffer.
  185. */
  186. unsigned char *output_buffer;
  187. size_t output_buffer_pos;
  188. size_t output_buffer_size;
  189. static struct resource
  190. {
  191. unsigned char *data;
  192. size_t size;
  193. } resources[16];
  194. static unsigned int nb_resources;
  195. static inline void put_resource_id( const char *str )
  196. {
  197. if (str[0] != '#')
  198. {
  199. while (*str)
  200. {
  201. unsigned char ch = *str++;
  202. put_word( toupper(ch) );
  203. }
  204. put_word( 0 );
  205. }
  206. else
  207. {
  208. put_word( 0xffff );
  209. put_word( atoi( str + 1 ));
  210. }
  211. }
  212. void add_output_to_resources( const char *type, const char *name )
  213. {
  214. size_t data_size = output_buffer_pos;
  215. size_t header_size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
  216. assert( nb_resources < ARRAY_SIZE( resources ));
  217. if (type[0] != '#') header_size += (strlen( type ) + 1) * sizeof(unsigned short);
  218. else header_size += 2 * sizeof(unsigned short);
  219. if (name[0] != '#') header_size += (strlen( name ) + 1) * sizeof(unsigned short);
  220. else header_size += 2 * sizeof(unsigned short);
  221. header_size = (header_size + 3) & ~3;
  222. align_output( 4 );
  223. check_output_buffer_space( header_size );
  224. resources[nb_resources].size = header_size + output_buffer_pos;
  225. memmove( output_buffer + header_size, output_buffer, output_buffer_pos );
  226. output_buffer_pos = 0;
  227. put_dword( data_size ); /* ResSize */
  228. put_dword( header_size ); /* HeaderSize */
  229. put_resource_id( type ); /* ResType */
  230. put_resource_id( name ); /* ResName */
  231. align_output( 4 );
  232. put_dword( 0 ); /* DataVersion */
  233. put_word( 0 ); /* Memory options */
  234. put_word( 0 ); /* Language */
  235. put_dword( 0 ); /* Version */
  236. put_dword( 0 ); /* Characteristics */
  237. resources[nb_resources++].data = output_buffer;
  238. init_output_buffer();
  239. }
  240. void flush_output_resources( const char *name )
  241. {
  242. int fd;
  243. unsigned int i;
  244. /* all output must have been saved with add_output_to_resources() first */
  245. assert( !output_buffer_pos );
  246. put_dword( 0 ); /* ResSize */
  247. put_dword( 32 ); /* HeaderSize */
  248. put_word( 0xffff ); /* ResType */
  249. put_word( 0x0000 );
  250. put_word( 0xffff ); /* ResName */
  251. put_word( 0x0000 );
  252. put_dword( 0 ); /* DataVersion */
  253. put_word( 0 ); /* Memory options */
  254. put_word( 0 ); /* Language */
  255. put_dword( 0 ); /* Version */
  256. put_dword( 0 ); /* Characteristics */
  257. fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
  258. if (fd == -1) error( "Error creating %s\n", name );
  259. if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
  260. error( "Error writing to %s\n", name );
  261. for (i = 0; i < nb_resources; i++)
  262. {
  263. if (write( fd, resources[i].data, resources[i].size ) != resources[i].size)
  264. error( "Error writing to %s\n", name );
  265. free( resources[i].data );
  266. }
  267. close( fd );
  268. nb_resources = 0;
  269. free( output_buffer );
  270. }
  271. /* pointer-sized word */
  272. void put_pword( unsigned int val )
  273. {
  274. if (pointer_size == 8) put_qword( val );
  275. else put_dword( val );
  276. }
  277. void put_str( int indent, const char *format, ... )
  278. {
  279. int n;
  280. va_list args;
  281. check_output_buffer_space( 4 * indent );
  282. memset( output_buffer + output_buffer_pos, ' ', 4 * indent );
  283. output_buffer_pos += 4 * indent;
  284. for (;;)
  285. {
  286. size_t size = output_buffer_size - output_buffer_pos;
  287. va_start( args, format );
  288. n = vsnprintf( (char *)output_buffer + output_buffer_pos, size, format, args );
  289. va_end( args );
  290. if (n == -1) size *= 2;
  291. else if ((size_t)n >= size) size = n + 1;
  292. else
  293. {
  294. output_buffer_pos += n;
  295. return;
  296. }
  297. check_output_buffer_space( size );
  298. }
  299. }