utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 "wine/port.h"
  23. #include <assert.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include "widl.h"
  30. #include "utils.h"
  31. #include "parser.h"
  32. #define CURRENT_LOCATION { input_name ? input_name : "stdin", line_number, parser_text }
  33. static const int want_near_indication = 0;
  34. static void make_print(char *str)
  35. {
  36. while(*str)
  37. {
  38. if(!isprint(*str))
  39. *str = ' ';
  40. str++;
  41. }
  42. }
  43. static void generic_msg(const loc_info_t *loc_info, const char *s, const char *t, va_list ap)
  44. {
  45. fprintf(stderr, "%s:%d: %s: ", loc_info->input_name, loc_info->line_number, t);
  46. vfprintf(stderr, s, ap);
  47. if (want_near_indication)
  48. {
  49. char *cpy;
  50. if(loc_info->near_text)
  51. {
  52. cpy = xstrdup(loc_info->near_text);
  53. make_print(cpy);
  54. fprintf(stderr, " near '%s'", cpy);
  55. free(cpy);
  56. }
  57. }
  58. }
  59. void error_loc(const char *s, ...)
  60. {
  61. loc_info_t cur_loc = CURRENT_LOCATION;
  62. va_list ap;
  63. va_start(ap, s);
  64. generic_msg(&cur_loc, s, "error", ap);
  65. va_end(ap);
  66. exit(1);
  67. }
  68. /* yyerror: yacc assumes this is not newline terminated. */
  69. void parser_error(const char *s)
  70. {
  71. error_loc("%s\n", s);
  72. }
  73. void error_loc_info(const loc_info_t *loc_info, const char *s, ...)
  74. {
  75. va_list ap;
  76. va_start(ap, s);
  77. generic_msg(loc_info, s, "error", ap);
  78. va_end(ap);
  79. exit(1);
  80. }
  81. int parser_warning(const char *s, ...)
  82. {
  83. loc_info_t cur_loc = CURRENT_LOCATION;
  84. va_list ap;
  85. va_start(ap, s);
  86. generic_msg(&cur_loc, s, "warning", ap);
  87. va_end(ap);
  88. return 0;
  89. }
  90. void error(const char *s, ...)
  91. {
  92. va_list ap;
  93. va_start(ap, s);
  94. fprintf(stderr, "error: ");
  95. vfprintf(stderr, s, ap);
  96. va_end(ap);
  97. exit(2);
  98. }
  99. void warning(const char *s, ...)
  100. {
  101. va_list ap;
  102. va_start(ap, s);
  103. fprintf(stderr, "warning: ");
  104. vfprintf(stderr, s, ap);
  105. va_end(ap);
  106. }
  107. void warning_loc_info(const loc_info_t *loc_info, const char *s, ...)
  108. {
  109. va_list ap;
  110. va_start(ap, s);
  111. generic_msg(loc_info, s, "warning", ap);
  112. va_end(ap);
  113. }
  114. void chat(const char *s, ...)
  115. {
  116. if(debuglevel & DEBUGLEVEL_CHAT)
  117. {
  118. va_list ap;
  119. va_start(ap, s);
  120. fprintf(stderr, "chat: ");
  121. vfprintf(stderr, s, ap);
  122. va_end(ap);
  123. }
  124. }
  125. char *dup_basename(const char *name, const char *ext)
  126. {
  127. int namelen;
  128. int extlen = strlen(ext);
  129. char *base;
  130. char *slash;
  131. if(!name)
  132. name = "widl.tab";
  133. slash = strrchr(name, '/');
  134. if (!slash)
  135. slash = strrchr(name, '\\');
  136. if (slash)
  137. name = slash + 1;
  138. namelen = strlen(name);
  139. /* +6 for later extension (strlen("_r.rgs")) and +1 for '\0' */
  140. base = xmalloc(namelen +6 +1);
  141. strcpy(base, name);
  142. if(!strcasecmp(name + namelen-extlen, ext))
  143. {
  144. base[namelen - extlen] = '\0';
  145. }
  146. return base;
  147. }
  148. size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
  149. {
  150. char *line = *linep;
  151. size_t len = *lenp;
  152. size_t n = 0;
  153. if (!line)
  154. {
  155. len = 64;
  156. line = xmalloc(len);
  157. }
  158. while (fgets(&line[n], len - n, fp))
  159. {
  160. n += strlen(&line[n]);
  161. if (line[n - 1] == '\n')
  162. break;
  163. else if (n == len - 1)
  164. {
  165. len *= 2;
  166. line = xrealloc(line, len);
  167. }
  168. }
  169. *linep = line;
  170. *lenp = len;
  171. return n;
  172. }
  173. void *xmalloc(size_t size)
  174. {
  175. void *res;
  176. assert(size > 0);
  177. res = malloc(size);
  178. if(res == NULL)
  179. {
  180. error("Virtual memory exhausted.\n");
  181. }
  182. memset(res, 0x55, size);
  183. return res;
  184. }
  185. void *xrealloc(void *p, size_t size)
  186. {
  187. void *res;
  188. assert(size > 0);
  189. res = realloc(p, size);
  190. if(res == NULL)
  191. {
  192. error("Virtual memory exhausted.\n");
  193. }
  194. return res;
  195. }
  196. char *strmake( const char* fmt, ... )
  197. {
  198. int n;
  199. size_t size = 100;
  200. va_list ap;
  201. for (;;)
  202. {
  203. char *p = xmalloc( size );
  204. va_start( ap, fmt );
  205. n = vsnprintf( p, size, fmt, ap );
  206. va_end( ap );
  207. if (n == -1) size *= 2;
  208. else if ((size_t)n >= size) size = n + 1;
  209. else return p;
  210. free( p );
  211. }
  212. }
  213. size_t strappend(char **buf, size_t *len, size_t pos, const char* fmt, ...)
  214. {
  215. size_t size;
  216. va_list ap;
  217. char *ptr;
  218. int n;
  219. assert( buf && len );
  220. assert( (*len == 0 && *buf == NULL) || (*len != 0 && *buf != NULL) );
  221. if (*buf)
  222. {
  223. size = *len;
  224. ptr = *buf;
  225. }
  226. else
  227. {
  228. size = 100;
  229. ptr = xmalloc( size );
  230. }
  231. for (;;)
  232. {
  233. va_start( ap, fmt );
  234. n = vsnprintf( ptr + pos, size - pos, fmt, ap );
  235. va_end( ap );
  236. if (n == -1) size *= 2;
  237. else if (pos + (size_t)n >= size) size = pos + n + 1;
  238. else break;
  239. ptr = xrealloc( ptr, size );
  240. }
  241. *len = size;
  242. *buf = ptr;
  243. return n;
  244. }
  245. char *xstrdup(const char *str)
  246. {
  247. char *s;
  248. assert(str != NULL);
  249. s = xmalloc(strlen(str)+1);
  250. return strcpy(s, str);
  251. }
  252. int strendswith(const char* str, const char* end)
  253. {
  254. int l = strlen(str);
  255. int m = strlen(end);
  256. return l >= m && strcmp(str + l - m, end) == 0;
  257. }
  258. /*******************************************************************
  259. * buffer management
  260. *
  261. * Function for writing to a memory buffer.
  262. */
  263. int byte_swapped = 0;
  264. unsigned char *output_buffer;
  265. size_t output_buffer_pos;
  266. size_t output_buffer_size;
  267. static struct resource
  268. {
  269. unsigned char *data;
  270. size_t size;
  271. } resources[16];
  272. static unsigned int nb_resources;
  273. static void check_output_buffer_space( size_t size )
  274. {
  275. if (output_buffer_pos + size >= output_buffer_size)
  276. {
  277. output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
  278. output_buffer = xrealloc( output_buffer, output_buffer_size );
  279. }
  280. }
  281. void init_output_buffer(void)
  282. {
  283. output_buffer_size = 1024;
  284. output_buffer_pos = 0;
  285. output_buffer = xmalloc( output_buffer_size );
  286. }
  287. void flush_output_buffer( const char *name )
  288. {
  289. int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
  290. if (fd == -1) error( "Error creating %s\n", name );
  291. if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
  292. error( "Error writing to %s\n", name );
  293. close( fd );
  294. free( output_buffer );
  295. }
  296. static inline void put_resource_id( const char *str )
  297. {
  298. if (str[0] != '#')
  299. {
  300. while (*str)
  301. {
  302. unsigned char ch = *str++;
  303. put_word( toupper(ch) );
  304. }
  305. put_word( 0 );
  306. }
  307. else
  308. {
  309. put_word( 0xffff );
  310. put_word( atoi( str + 1 ));
  311. }
  312. }
  313. void add_output_to_resources( const char *type, const char *name )
  314. {
  315. size_t data_size = output_buffer_pos;
  316. size_t header_size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
  317. assert( nb_resources < ARRAY_SIZE( resources ));
  318. if (type[0] != '#') header_size += (strlen( type ) + 1) * sizeof(unsigned short);
  319. else header_size += 2 * sizeof(unsigned short);
  320. if (name[0] != '#') header_size += (strlen( name ) + 1) * sizeof(unsigned short);
  321. else header_size += 2 * sizeof(unsigned short);
  322. header_size = (header_size + 3) & ~3;
  323. align_output( 4 );
  324. check_output_buffer_space( header_size );
  325. resources[nb_resources].size = header_size + output_buffer_pos;
  326. memmove( output_buffer + header_size, output_buffer, output_buffer_pos );
  327. output_buffer_pos = 0;
  328. put_dword( data_size ); /* ResSize */
  329. put_dword( header_size ); /* HeaderSize */
  330. put_resource_id( type ); /* ResType */
  331. put_resource_id( name ); /* ResName */
  332. align_output( 4 );
  333. put_dword( 0 ); /* DataVersion */
  334. put_word( 0 ); /* Memory options */
  335. put_word( 0 ); /* Language */
  336. put_dword( 0 ); /* Version */
  337. put_dword( 0 ); /* Characteristics */
  338. resources[nb_resources++].data = output_buffer;
  339. init_output_buffer();
  340. }
  341. void flush_output_resources( const char *name )
  342. {
  343. int fd;
  344. unsigned int i;
  345. /* all output must have been saved with add_output_to_resources() first */
  346. assert( !output_buffer_pos );
  347. put_dword( 0 ); /* ResSize */
  348. put_dword( 32 ); /* HeaderSize */
  349. put_word( 0xffff ); /* ResType */
  350. put_word( 0x0000 );
  351. put_word( 0xffff ); /* ResName */
  352. put_word( 0x0000 );
  353. put_dword( 0 ); /* DataVersion */
  354. put_word( 0 ); /* Memory options */
  355. put_word( 0 ); /* Language */
  356. put_dword( 0 ); /* Version */
  357. put_dword( 0 ); /* Characteristics */
  358. fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
  359. if (fd == -1) error( "Error creating %s\n", name );
  360. if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
  361. error( "Error writing to %s\n", name );
  362. for (i = 0; i < nb_resources; i++)
  363. {
  364. if (write( fd, resources[i].data, resources[i].size ) != resources[i].size)
  365. error( "Error writing to %s\n", name );
  366. free( resources[i].data );
  367. }
  368. close( fd );
  369. nb_resources = 0;
  370. free( output_buffer );
  371. }
  372. void put_data( const void *data, size_t size )
  373. {
  374. check_output_buffer_space( size );
  375. memcpy( output_buffer + output_buffer_pos, data, size );
  376. output_buffer_pos += size;
  377. }
  378. void put_byte( unsigned char val )
  379. {
  380. check_output_buffer_space( 1 );
  381. output_buffer[output_buffer_pos++] = val;
  382. }
  383. void put_word( unsigned short val )
  384. {
  385. if (byte_swapped) val = (val << 8) | (val >> 8);
  386. put_data( &val, sizeof(val) );
  387. }
  388. void put_dword( unsigned int val )
  389. {
  390. if (byte_swapped)
  391. val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
  392. put_data( &val, sizeof(val) );
  393. }
  394. void put_qword( unsigned int val )
  395. {
  396. if (byte_swapped)
  397. {
  398. put_dword( 0 );
  399. put_dword( val );
  400. }
  401. else
  402. {
  403. put_dword( val );
  404. put_dword( 0 );
  405. }
  406. }
  407. /* pointer-sized word */
  408. void put_pword( unsigned int val )
  409. {
  410. if (pointer_size == 8) put_qword( val );
  411. else put_dword( val );
  412. }
  413. void put_str( int indent, const char *format, ... )
  414. {
  415. int n;
  416. va_list args;
  417. check_output_buffer_space( 4 * indent );
  418. memset( output_buffer + output_buffer_pos, ' ', 4 * indent );
  419. output_buffer_pos += 4 * indent;
  420. for (;;)
  421. {
  422. size_t size = output_buffer_size - output_buffer_pos;
  423. va_start( args, format );
  424. n = vsnprintf( (char *)output_buffer + output_buffer_pos, size, format, args );
  425. va_end( args );
  426. if (n == -1) size *= 2;
  427. else if ((size_t)n >= size) size = n + 1;
  428. else
  429. {
  430. output_buffer_pos += n;
  431. return;
  432. }
  433. check_output_buffer_space( size );
  434. }
  435. }
  436. void align_output( unsigned int align )
  437. {
  438. size_t size = align - (output_buffer_pos % align);
  439. if (size == align) return;
  440. check_output_buffer_space( size );
  441. memset( output_buffer + output_buffer_pos, 0, size );
  442. output_buffer_pos += size;
  443. }