daily update
[deliverable/binutils-gdb.git] / binutils / windres.c
CommitLineData
252b5132 1/* windres.c -- a program to manipulate Windows resources
92f01d61 2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
29b058f1 3 Free Software Foundation, Inc.
252b5132 4 Written by Ian Lance Taylor, Cygnus Support.
4a594fce 5 Rewritten by Kai Tietz, Onevision.
252b5132
RH
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
b43b5d5f
NC
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
252b5132
RH
23
24/* This program can read and write Windows resources in various
25 formats. In particular, it can act like the rc resource compiler
26 program, and it can act like the cvtres res to COFF conversion
27 program.
28
29 It is based on information taken from the following sources:
30
31 * Microsoft documentation.
32
33 * The rcl program, written by Gunther Ebert
34 <gunther.ebert@ixos-leipzig.de>.
35
29b058f1 36 * The res2coff program, written by Pedro A. Aranda <paag@tid.es>. */
252b5132 37
3db64b00 38#include "sysdep.h"
0af6db78
AM
39#include <assert.h>
40#include <time.h>
252b5132
RH
41#include "bfd.h"
42#include "getopt.h"
4a594fce 43#include "bucomm.h"
252b5132 44#include "libiberty.h"
3882b010 45#include "safe-ctype.h"
252b5132
RH
46#include "obstack.h"
47#include "windres.h"
252b5132 48
4a594fce
NC
49/* Defined in bfd/binary.c. Used to set architecture and machine of input
50 binary files. */
51extern enum bfd_architecture bfd_external_binary_architecture;
52extern unsigned long bfd_external_machine;
53
29b058f1 54/* Used by resrc.c at least. */
751d21b5
DD
55
56int verbose = 0;
57
4a594fce
NC
58int target_is_bigendian = 0;
59const char *def_target_arch;
60
61static void set_endianess (bfd *, const char *);
62
252b5132
RH
63/* An enumeration of format types. */
64
65enum res_format
66{
67 /* Unknown format. */
68 RES_FORMAT_UNKNOWN,
69 /* Textual RC file. */
70 RES_FORMAT_RC,
71 /* Binary RES file. */
72 RES_FORMAT_RES,
73 /* COFF file. */
74 RES_FORMAT_COFF
75};
76
77/* A structure used to map between format types and strings. */
78
79struct format_map
80{
81 const char *name;
82 enum res_format format;
83};
84
85/* A mapping between names and format types. */
86
87static const struct format_map format_names[] =
88{
89 { "rc", RES_FORMAT_RC },
90 { "res", RES_FORMAT_RES },
91 { "coff", RES_FORMAT_COFF },
92 { NULL, RES_FORMAT_UNKNOWN }
93};
94
95/* A mapping from file extensions to format types. */
96
97static const struct format_map format_fileexts[] =
98{
99 { "rc", RES_FORMAT_RC },
100 { "res", RES_FORMAT_RES },
101 { "exe", RES_FORMAT_COFF },
102 { "obj", RES_FORMAT_COFF },
103 { "o", RES_FORMAT_COFF },
104 { NULL, RES_FORMAT_UNKNOWN }
105};
106
107/* A list of include directories. */
108
109struct include_dir
110{
111 struct include_dir *next;
112 char *dir;
113};
114
115static struct include_dir *include_dirs;
116
252b5132
RH
117/* Static functions. */
118
2da42df6 119static void res_init (void);
4a594fce 120static int extended_menuitems (const rc_menuitem *);
2da42df6
AJ
121static enum res_format format_from_name (const char *, int);
122static enum res_format format_from_filename (const char *, int);
123static void usage (FILE *, int);
124static int cmp_res_entry (const void *, const void *);
4a594fce 125static rc_res_directory *sort_resources (rc_res_directory *);
2da42df6
AJ
126static void reswr_init (void);
127static const char * quot (const char *);
4a594fce
NC
128\f
129static rc_uint_type target_get_8 (const void *, rc_uint_type);
130static void target_put_8 (void *, rc_uint_type);
131static rc_uint_type target_get_16 (const void *, rc_uint_type);
132static void target_put_16 (void *, rc_uint_type);
133static rc_uint_type target_get_32 (const void *, rc_uint_type);
134static void target_put_32 (void *, rc_uint_type);
135
252b5132
RH
136\f
137/* When we are building a resource tree, we allocate everything onto
138 an obstack, so that we can free it all at once if we want. */
139
140#define obstack_chunk_alloc xmalloc
141#define obstack_chunk_free free
142
143/* The resource building obstack. */
144
145static struct obstack res_obstack;
146
147/* Initialize the resource building obstack. */
148
149static void
2da42df6 150res_init (void)
252b5132
RH
151{
152 obstack_init (&res_obstack);
153}
154
155/* Allocate space on the resource building obstack. */
156
2da42df6 157void *
4a594fce 158res_alloc (rc_uint_type bytes)
252b5132 159{
4a594fce 160 return (void *) obstack_alloc (&res_obstack, (size_t) bytes);
252b5132
RH
161}
162
163/* We also use an obstack to save memory used while writing out a set
164 of resources. */
165
166static struct obstack reswr_obstack;
167
168/* Initialize the resource writing obstack. */
169
170static void
2da42df6 171reswr_init (void)
252b5132
RH
172{
173 obstack_init (&reswr_obstack);
174}
175
176/* Allocate space on the resource writing obstack. */
177
2da42df6 178void *
4a594fce 179reswr_alloc (rc_uint_type bytes)
252b5132 180{
4a594fce 181 return (void *) obstack_alloc (&reswr_obstack, (size_t) bytes);
252b5132
RH
182}
183\f
184/* Open a file using the include directory search list. */
185
186FILE *
2da42df6
AJ
187open_file_search (const char *filename, const char *mode, const char *errmsg,
188 char **real_filename)
252b5132
RH
189{
190 FILE *e;
191 struct include_dir *d;
192
193 e = fopen (filename, mode);
194 if (e != NULL)
195 {
196 *real_filename = xstrdup (filename);
197 return e;
198 }
199
200 if (errno == ENOENT)
201 {
202 for (d = include_dirs; d != NULL; d = d->next)
203 {
204 char *n;
205
206 n = (char *) xmalloc (strlen (d->dir) + strlen (filename) + 2);
207 sprintf (n, "%s/%s", d->dir, filename);
208 e = fopen (n, mode);
209 if (e != NULL)
210 {
211 *real_filename = n;
212 return e;
213 }
214
215 if (errno != ENOENT)
216 break;
217 }
218 }
219
220 fatal (_("can't open %s `%s': %s"), errmsg, filename, strerror (errno));
221
222 /* Return a value to avoid a compiler warning. */
223 return NULL;
224}
225\f
226/* Compare two resource ID's. We consider name entries to come before
227 numeric entries, because that is how they appear in the COFF .rsrc
228 section. */
229
230int
4a594fce 231res_id_cmp (rc_res_id a, rc_res_id b)
252b5132
RH
232{
233 if (! a.named)
234 {
235 if (b.named)
236 return 1;
237 if (a.u.id > b.u.id)
238 return 1;
239 else if (a.u.id < b.u.id)
240 return -1;
241 else
242 return 0;
243 }
244 else
245 {
246 unichar *as, *ase, *bs, *bse;
247
248 if (! b.named)
249 return -1;
250
251 as = a.u.n.name;
252 ase = as + a.u.n.length;
253 bs = b.u.n.name;
254 bse = bs + b.u.n.length;
255
256 while (as < ase)
257 {
258 int i;
259
260 if (bs >= bse)
261 return 1;
262 i = (int) *as - (int) *bs;
263 if (i != 0)
264 return i;
265 ++as;
266 ++bs;
267 }
268
269 if (bs < bse)
270 return -1;
271
272 return 0;
273 }
274}
275
276/* Print a resource ID. */
277
278void
4a594fce 279res_id_print (FILE *stream, rc_res_id id, int quote)
252b5132
RH
280{
281 if (! id.named)
4a594fce 282 fprintf (stream, "%u", (int) id.u.id);
252b5132
RH
283 else
284 {
285 if (quote)
4a594fce
NC
286 unicode_print_quoted (stream, id.u.n.name, id.u.n.length);
287 else
252b5132 288 unicode_print (stream, id.u.n.name, id.u.n.length);
252b5132
RH
289 }
290}
291
292/* Print a list of resource ID's. */
293
294void
4a594fce 295res_ids_print (FILE *stream, int cids, const rc_res_id *ids)
252b5132
RH
296{
297 int i;
298
299 for (i = 0; i < cids; i++)
300 {
301 res_id_print (stream, ids[i], 1);
302 if (i + 1 < cids)
303 fprintf (stream, ": ");
304 }
305}
306
307/* Convert an ASCII string to a resource ID. */
308
309void
4a594fce 310res_string_to_id (rc_res_id *res_id, const char *string)
252b5132
RH
311{
312 res_id->named = 1;
313 unicode_from_ascii (&res_id->u.n.length, &res_id->u.n.name, string);
314}
315
4a594fce
NC
316/* Convert an unicode string to a resource ID. */
317void
318res_unistring_to_id (rc_res_id *res_id, const unichar *u)
319{
320 res_id->named = 1;
321 res_id->u.n.length = unichar_len (u);
322 res_id->u.n.name = unichar_dup_uppercase (u);
323}
324
252b5132
RH
325/* Define a resource. The arguments are the resource tree, RESOURCES,
326 and the location at which to put it in the tree, CIDS and IDS.
4a594fce 327 This returns a newly allocated rc_res_resource structure, which the
252b5132
RH
328 caller is expected to initialize. If DUPOK is non-zero, then if a
329 resource with this ID exists, it is returned. Otherwise, a warning
330 is issued, and a new resource is created replacing the existing
331 one. */
332
4a594fce
NC
333rc_res_resource *
334define_resource (rc_res_directory **resources, int cids,
335 const rc_res_id *ids, int dupok)
252b5132 336{
4a594fce 337 rc_res_entry *re = NULL;
252b5132
RH
338 int i;
339
340 assert (cids > 0);
341 for (i = 0; i < cids; i++)
342 {
4a594fce 343 rc_res_entry **pp;
252b5132
RH
344
345 if (*resources == NULL)
346 {
4a594fce 347 static unsigned int timeval;
252b5132
RH
348
349 /* Use the same timestamp for every resource created in a
350 single run. */
351 if (timeval == 0)
352 timeval = time (NULL);
353
4a594fce
NC
354 *resources = ((rc_res_directory *)
355 res_alloc (sizeof (rc_res_directory)));
252b5132
RH
356 (*resources)->characteristics = 0;
357 (*resources)->time = timeval;
358 (*resources)->major = 0;
359 (*resources)->minor = 0;
360 (*resources)->entries = NULL;
361 }
362
363 for (pp = &(*resources)->entries; *pp != NULL; pp = &(*pp)->next)
364 if (res_id_cmp ((*pp)->id, ids[i]) == 0)
365 break;
366
367 if (*pp != NULL)
368 re = *pp;
369 else
370 {
4a594fce 371 re = (rc_res_entry *) res_alloc (sizeof (rc_res_entry));
252b5132
RH
372 re->next = NULL;
373 re->id = ids[i];
374 if ((i + 1) < cids)
375 {
376 re->subdir = 1;
377 re->u.dir = NULL;
378 }
379 else
380 {
381 re->subdir = 0;
382 re->u.res = NULL;
383 }
384
385 *pp = re;
386 }
387
388 if ((i + 1) < cids)
389 {
390 if (! re->subdir)
391 {
392 fprintf (stderr, "%s: ", program_name);
393 res_ids_print (stderr, i, ids);
394 fprintf (stderr, _(": expected to be a directory\n"));
395 xexit (1);
396 }
397
398 resources = &re->u.dir;
399 }
400 }
401
402 if (re->subdir)
403 {
404 fprintf (stderr, "%s: ", program_name);
405 res_ids_print (stderr, cids, ids);
406 fprintf (stderr, _(": expected to be a leaf\n"));
407 xexit (1);
408 }
409
410 if (re->u.res != NULL)
411 {
412 if (dupok)
413 return re->u.res;
414
415 fprintf (stderr, _("%s: warning: "), program_name);
416 res_ids_print (stderr, cids, ids);
417 fprintf (stderr, _(": duplicate value\n"));
418 }
419
4a594fce
NC
420 re->u.res = ((rc_res_resource *)
421 res_alloc (sizeof (rc_res_resource)));
422 memset (re->u.res, 0, sizeof (rc_res_resource));
252b5132
RH
423
424 re->u.res->type = RES_TYPE_UNINITIALIZED;
252b5132
RH
425 return re->u.res;
426}
427
428/* Define a standard resource. This is a version of define_resource
429 that just takes type, name, and language arguments. */
430
4a594fce
NC
431rc_res_resource *
432define_standard_resource (rc_res_directory **resources, int type,
433 rc_res_id name, rc_uint_type language, int dupok)
252b5132 434{
4a594fce 435 rc_res_id a[3];
252b5132
RH
436
437 a[0].named = 0;
438 a[0].u.id = type;
439 a[1] = name;
440 a[2].named = 0;
441 a[2].u.id = language;
442 return define_resource (resources, 3, a, dupok);
443}
444
445/* Comparison routine for resource sorting. */
446
447static int
2da42df6 448cmp_res_entry (const void *p1, const void *p2)
252b5132 449{
4a594fce 450 const rc_res_entry **re1, **re2;
252b5132 451
4a594fce
NC
452 re1 = (const rc_res_entry **) p1;
453 re2 = (const rc_res_entry **) p2;
252b5132
RH
454 return res_id_cmp ((*re1)->id, (*re2)->id);
455}
456
457/* Sort the resources. */
458
4a594fce
NC
459static rc_res_directory *
460sort_resources (rc_res_directory *resdir)
252b5132
RH
461{
462 int c, i;
4a594fce
NC
463 rc_res_entry *re;
464 rc_res_entry **a;
252b5132
RH
465
466 if (resdir->entries == NULL)
467 return resdir;
468
469 c = 0;
470 for (re = resdir->entries; re != NULL; re = re->next)
471 ++c;
472
473 /* This is a recursive routine, so using xmalloc is probably better
474 than alloca. */
4a594fce 475 a = (rc_res_entry **) xmalloc (c * sizeof (rc_res_entry *));
252b5132
RH
476
477 for (i = 0, re = resdir->entries; re != NULL; re = re->next, i++)
478 a[i] = re;
479
4a594fce 480 qsort (a, c, sizeof (rc_res_entry *), cmp_res_entry);
252b5132
RH
481
482 resdir->entries = a[0];
483 for (i = 0; i < c - 1; i++)
484 a[i]->next = a[i + 1];
485 a[i]->next = NULL;
486
487 free (a);
488
489 /* Now sort the subdirectories. */
490
491 for (re = resdir->entries; re != NULL; re = re->next)
492 if (re->subdir)
493 re->u.dir = sort_resources (re->u.dir);
494
495 return resdir;
496}
497\f
498/* Return whether the dialog resource DIALOG is a DIALOG or a
499 DIALOGEX. */
500
501int
4a594fce 502extended_dialog (const rc_dialog *dialog)
252b5132 503{
4a594fce 504 const rc_dialog_control *c;
252b5132
RH
505
506 if (dialog->ex != NULL)
507 return 1;
508
509 for (c = dialog->controls; c != NULL; c = c->next)
510 if (c->data != NULL || c->help != 0)
511 return 1;
512
513 return 0;
514}
515
516/* Return whether MENUITEMS are a MENU or a MENUEX. */
517
518int
4a594fce 519extended_menu (const rc_menu *menu)
252b5132
RH
520{
521 return extended_menuitems (menu->items);
522}
523
524static int
4a594fce 525extended_menuitems (const rc_menuitem *menuitems)
252b5132 526{
4a594fce 527 const rc_menuitem *mi;
252b5132
RH
528
529 for (mi = menuitems; mi != NULL; mi = mi->next)
530 {
531 if (mi->help != 0 || mi->state != 0)
532 return 1;
533 if (mi->popup != NULL && mi->id != 0)
534 return 1;
535 if ((mi->type
536 & ~ (MENUITEM_CHECKED
537 | MENUITEM_GRAYED
538 | MENUITEM_HELP
539 | MENUITEM_INACTIVE
540 | MENUITEM_MENUBARBREAK
541 | MENUITEM_MENUBREAK))
542 != 0)
543 return 1;
544 if (mi->popup != NULL)
545 {
546 if (extended_menuitems (mi->popup))
547 return 1;
548 }
549 }
550
551 return 0;
552}
553\f
554/* Convert a string to a format type, or exit if it can't be done. */
555
556static enum res_format
2da42df6 557format_from_name (const char *name, int exit_on_error)
252b5132
RH
558{
559 const struct format_map *m;
560
561 for (m = format_names; m->name != NULL; m++)
562 if (strcasecmp (m->name, name) == 0)
563 break;
564
85eb5110 565 if (m->name == NULL && exit_on_error)
252b5132 566 {
37cc8ec1 567 non_fatal (_("unknown format type `%s'"), name);
252b5132
RH
568 fprintf (stderr, _("%s: supported formats:"), program_name);
569 for (m = format_names; m->name != NULL; m++)
570 fprintf (stderr, " %s", m->name);
571 fprintf (stderr, "\n");
572 xexit (1);
573 }
574
575 return m->format;
576}
577
578/* Work out a format type given a file name. If INPUT is non-zero,
579 it's OK to look at the file itself. */
580
581static enum res_format
2da42df6 582format_from_filename (const char *filename, int input)
252b5132
RH
583{
584 const char *ext;
585 FILE *e;
4a594fce 586 bfd_byte b1, b2, b3, b4, b5;
252b5132
RH
587 int magic;
588
589 /* If we have an extension, see if we recognize it as implying a
590 particular format. */
591 ext = strrchr (filename, '.');
592 if (ext != NULL)
593 {
594 const struct format_map *m;
595
596 ++ext;
597 for (m = format_fileexts; m->name != NULL; m++)
598 if (strcasecmp (m->name, ext) == 0)
599 return m->format;
600 }
601
602 /* If we don't recognize the name of an output file, assume it's a
603 COFF file. */
252b5132
RH
604 if (! input)
605 return RES_FORMAT_COFF;
606
607 /* Read the first few bytes of the file to see if we can guess what
608 it is. */
252b5132
RH
609 e = fopen (filename, FOPEN_RB);
610 if (e == NULL)
611 fatal ("%s: %s", filename, strerror (errno));
612
613 b1 = getc (e);
614 b2 = getc (e);
615 b3 = getc (e);
616 b4 = getc (e);
617 b5 = getc (e);
618
619 fclose (e);
620
621 /* A PE executable starts with 0x4d 0x5a. */
622 if (b1 == 0x4d && b2 == 0x5a)
623 return RES_FORMAT_COFF;
624
625 /* A COFF .o file starts with a COFF magic number. */
626 magic = (b2 << 8) | b1;
627 switch (magic)
628 {
629 case 0x14c: /* i386 */
630 case 0x166: /* MIPS */
631 case 0x184: /* Alpha */
632 case 0x268: /* 68k */
633 case 0x1f0: /* PowerPC */
634 case 0x290: /* PA */
635 return RES_FORMAT_COFF;
636 }
637
638 /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
639 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0 && b5 == 0x20)
640 return RES_FORMAT_RES;
641
642 /* If every character is printable or space, assume it's an RC file. */
3882b010
L
643 if ((ISPRINT (b1) || ISSPACE (b1))
644 && (ISPRINT (b2) || ISSPACE (b2))
645 && (ISPRINT (b3) || ISSPACE (b3))
646 && (ISPRINT (b4) || ISSPACE (b4))
647 && (ISPRINT (b5) || ISSPACE (b5)))
252b5132
RH
648 return RES_FORMAT_RC;
649
650 /* Otherwise, we give up. */
d412a550 651 fatal (_("can not determine type of file `%s'; use the -J option"),
252b5132
RH
652 filename);
653
654 /* Return something to silence the compiler warning. */
655 return RES_FORMAT_UNKNOWN;
656}
657
658/* Print a usage message and exit. */
659
660static void
2da42df6 661usage (FILE *stream, int status)
252b5132 662{
8b53311e 663 fprintf (stream, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
252b5132 664 program_name);
8b53311e
NC
665 fprintf (stream, _(" The options are:\n\
666 -i --input=<file> Name input file\n\
667 -o --output=<file> Name output file\n\
85eb5110 668 -J --input-format=<format> Specify input format\n\
8b53311e
NC
669 -O --output-format=<format> Specify output format\n\
670 -F --target=<target> Specify COFF target\n\
671 --preprocessor=<program> Program to use to preprocess rc file\n\
85eb5110 672 -I --include-dir=<dir> Include directory when preprocessing rc file\n\
8b53311e 673 -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\
29b058f1 674 -U --undefine <sym> Undefine SYM when preprocessing rc file\n\
8b53311e 675 -v --verbose Verbose - tells you what it's doing\n\
d856f2dd 676 -c --codepage=<codepage> Specify default codepage\n\
85eb5110 677 -l --language=<val> Set language when reading rc file\n\
8b53311e
NC
678 --use-temp-file Use a temporary file instead of popen to read\n\
679 the preprocessor output\n\
680 --no-use-temp-file Use popen (default)\n"));
252b5132
RH
681#ifdef YYDEBUG
682 fprintf (stream, _("\
8b53311e 683 --yydebug Turn on parser debugging\n"));
252b5132
RH
684#endif
685 fprintf (stream, _("\
3126d709 686 -r Ignored for compatibility with rc\n\
07012eee 687 @<file> Read options from <file>\n\
8b53311e
NC
688 -h --help Print this help message\n\
689 -V --version Print version information\n"));
252b5132
RH
690 fprintf (stream, _("\
691FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
692extension if not specified. A single file name is an input file.\n\
693No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
8b53311e 694
252b5132 695 list_supported_targets (program_name, stream);
8b53311e 696
92f01d61 697 if (REPORT_BUGS_TO[0] && status == 0)
8ad3436c 698 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
8b53311e 699
252b5132
RH
700 exit (status);
701}
702
8b53311e
NC
703/* Quote characters that will confuse the shell when we run the preprocessor. */
704
705static const char *
2da42df6 706quot (const char *string)
09cda596
DD
707{
708 static char *buf = 0;
709 static int buflen = 0;
710 int slen = strlen (string);
711 const char *src;
712 char *dest;
713
4a594fce 714 if ((buflen < slen * 2 + 2) || ! buf)
09cda596
DD
715 {
716 buflen = slen * 2 + 2;
717 if (buf)
718 free (buf);
719 buf = (char *) xmalloc (buflen);
720 }
721
722 for (src=string, dest=buf; *src; src++, dest++)
723 {
724 if (*src == '(' || *src == ')' || *src == ' ')
725 *dest++ = '\\';
726 *dest = *src;
727 }
728 *dest = 0;
729 return buf;
730}
731
32df8966
NC
732/* Long options. */
733
734/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
735
736#define OPTION_PREPROCESSOR 150
737#define OPTION_USE_TEMP_FILE (OPTION_PREPROCESSOR + 1)
738#define OPTION_NO_USE_TEMP_FILE (OPTION_USE_TEMP_FILE + 1)
739#define OPTION_YYDEBUG (OPTION_NO_USE_TEMP_FILE + 1)
740
741static const struct option long_options[] =
742{
743 {"input", required_argument, 0, 'i'},
744 {"output", required_argument, 0, 'o'},
745 {"input-format", required_argument, 0, 'J'},
746 {"output-format", required_argument, 0, 'O'},
747 {"target", required_argument, 0, 'F'},
748 {"preprocessor", required_argument, 0, OPTION_PREPROCESSOR},
749 {"include-dir", required_argument, 0, 'I'},
750 {"define", required_argument, 0, 'D'},
751 {"undefine", required_argument, 0, 'U'},
752 {"verbose", no_argument, 0, 'v'},
d856f2dd 753 {"codepage", required_argument, 0, 'c'},
32df8966
NC
754 {"language", required_argument, 0, 'l'},
755 {"use-temp-file", no_argument, 0, OPTION_USE_TEMP_FILE},
756 {"no-use-temp-file", no_argument, 0, OPTION_NO_USE_TEMP_FILE},
757 {"yydebug", no_argument, 0, OPTION_YYDEBUG},
758 {"version", no_argument, 0, 'V'},
759 {"help", no_argument, 0, 'h'},
760 {0, no_argument, 0, 0}
761};
762
f7d63484 763/* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */
2da42df6 764int main (int, char **);
f7d63484 765
252b5132
RH
766/* The main function. */
767
768int
2da42df6 769main (int argc, char **argv)
252b5132
RH
770{
771 int c;
772 char *input_filename;
773 char *output_filename;
774 enum res_format input_format;
85eb5110 775 enum res_format input_format_tmp;
252b5132
RH
776 enum res_format output_format;
777 char *target;
778 char *preprocessor;
779 char *preprocargs;
09cda596 780 const char *quotedarg;
252b5132 781 int language;
4a594fce 782 rc_res_directory *resources;
5a298d2d 783 int use_temp_file;
252b5132
RH
784
785#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
786 setlocale (LC_MESSAGES, "");
3882b010
L
787#endif
788#if defined (HAVE_SETLOCALE)
789 setlocale (LC_CTYPE, "");
252b5132
RH
790#endif
791 bindtextdomain (PACKAGE, LOCALEDIR);
792 textdomain (PACKAGE);
793
794 program_name = argv[0];
795 xmalloc_set_program_name (program_name);
796
c843b1bb 797 expandargv (&argc, &argv);
869b9d07 798
252b5132
RH
799 bfd_init ();
800 set_default_bfd_target ();
801
802 res_init ();
803
804 input_filename = NULL;
805 output_filename = NULL;
806 input_format = RES_FORMAT_UNKNOWN;
807 output_format = RES_FORMAT_UNKNOWN;
808 target = NULL;
809 preprocessor = NULL;
810 preprocargs = NULL;
f7d63484 811 language = 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
5a298d2d 812 use_temp_file = 0;
252b5132 813
d856f2dd 814 while ((c = getopt_long (argc, argv, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options,
252b5132
RH
815 (int *) 0)) != EOF)
816 {
817 switch (c)
818 {
d856f2dd
NC
819 case 'c':
820 {
821 rc_uint_type ncp;
822
823 if (optarg[0] == '0' && (optarg[1] == 'x' || optarg[1] == 'X'))
824 ncp = (rc_uint_type) strtol (optarg + 2, NULL, 16);
825 else
826 ncp = (rc_uint_type) strtol (optarg, NULL, 10);
827 if (ncp == CP_UTF16 || ! unicode_is_valid_codepage (ncp))
828 fatal (_("invalid codepage specified.\n"));
829 wind_default_codepage = wind_current_codepage = ncp;
830 }
831 break;
832
252b5132
RH
833 case 'i':
834 input_filename = optarg;
835 break;
836
32df8966 837 case 'f':
50c2245b 838 /* For compatibility with rc we accept "-fo <name>" as being the
32df8966
NC
839 equivalent of "-o <name>". We do not advertise this fact
840 though, as we do not want users to use non-GNU like command
841 line switches. */
842 if (*optarg != 'o')
843 fatal (_("invalid option -f\n"));
844 optarg++;
845 if (* optarg == 0)
846 {
847 if (optind == argc)
2da42df6 848 fatal (_("No filename following the -fo option.\n"));
32df8966
NC
849 optarg = argv [optind++];
850 }
851 /* Fall through. */
852
252b5132
RH
853 case 'o':
854 output_filename = optarg;
855 break;
856
85eb5110
NC
857 case 'J':
858 input_format = format_from_name (optarg, 1);
252b5132
RH
859 break;
860
861 case 'O':
85eb5110 862 output_format = format_from_name (optarg, 1);
252b5132
RH
863 break;
864
865 case 'F':
866 target = optarg;
867 break;
868
869 case OPTION_PREPROCESSOR:
870 preprocessor = optarg;
871 break;
872
09cda596 873 case 'D':
29b058f1 874 case 'U':
252b5132
RH
875 if (preprocargs == NULL)
876 {
09cda596
DD
877 quotedarg = quot (optarg);
878 preprocargs = xmalloc (strlen (quotedarg) + 3);
29b058f1 879 sprintf (preprocargs, "-%c%s", c, quotedarg);
252b5132
RH
880 }
881 else
882 {
883 char *n;
884
09cda596
DD
885 quotedarg = quot (optarg);
886 n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
29b058f1 887 sprintf (n, "%s -%c%s", preprocargs, c, quotedarg);
252b5132
RH
888 free (preprocargs);
889 preprocargs = n;
890 }
891 break;
892
3126d709 893 case 'r':
29b058f1 894 /* Ignored for compatibility with rc. */
3126d709
CF
895 break;
896
751d21b5
DD
897 case 'v':
898 verbose ++;
899 break;
900
85eb5110
NC
901 case 'I':
902 /* For backward compatibility, should be removed in the future. */
903 input_format_tmp = format_from_name (optarg, 0);
904 if (input_format_tmp != RES_FORMAT_UNKNOWN)
905 {
4a594fce
NC
906 fprintf (stderr,
907 _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
85eb5110
NC
908 input_format = input_format_tmp;
909 break;
910 }
2da42df6 911
252b5132
RH
912 if (preprocargs == NULL)
913 {
09cda596
DD
914 quotedarg = quot (optarg);
915 preprocargs = xmalloc (strlen (quotedarg) + 3);
916 sprintf (preprocargs, "-I%s", quotedarg);
252b5132
RH
917 }
918 else
919 {
920 char *n;
921
09cda596
DD
922 quotedarg = quot (optarg);
923 n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
924 sprintf (n, "%s -I%s", preprocargs, quotedarg);
252b5132
RH
925 free (preprocargs);
926 preprocargs = n;
927 }
928
929 {
930 struct include_dir *n, **pp;
931
932 n = (struct include_dir *) xmalloc (sizeof *n);
933 n->next = NULL;
934 n->dir = optarg;
935
936 for (pp = &include_dirs; *pp != NULL; pp = &(*pp)->next)
937 ;
938 *pp = n;
939 }
940
941 break;
942
3077f5d8 943 case 'l':
252b5132
RH
944 language = strtol (optarg, (char **) NULL, 16);
945 break;
946
5a298d2d
NC
947 case OPTION_USE_TEMP_FILE:
948 use_temp_file = 1;
949 break;
950
951 case OPTION_NO_USE_TEMP_FILE:
952 use_temp_file = 0;
953 break;
954
252b5132
RH
955#ifdef YYDEBUG
956 case OPTION_YYDEBUG:
957 yydebug = 1;
958 break;
959#endif
960
8b53311e
NC
961 case 'h':
962 case 'H':
252b5132
RH
963 usage (stdout, 0);
964 break;
965
8b53311e 966 case 'V':
252b5132
RH
967 print_version ("windres");
968 break;
969
970 default:
971 usage (stderr, 1);
972 break;
973 }
974 }
975
976 if (input_filename == NULL && optind < argc)
977 {
978 input_filename = argv[optind];
979 ++optind;
980 }
981
982 if (output_filename == NULL && optind < argc)
983 {
984 output_filename = argv[optind];
985 ++optind;
986 }
987
988 if (argc != optind)
989 usage (stderr, 1);
990
991 if (input_format == RES_FORMAT_UNKNOWN)
992 {
993 if (input_filename == NULL)
994 input_format = RES_FORMAT_RC;
995 else
996 input_format = format_from_filename (input_filename, 1);
997 }
998
999 if (output_format == RES_FORMAT_UNKNOWN)
1000 {
1001 if (output_filename == NULL)
1002 output_format = RES_FORMAT_RC;
1003 else
1004 output_format = format_from_filename (output_filename, 0);
1005 }
1006
4a594fce
NC
1007 set_endianess (NULL, target);
1008
252b5132 1009 /* Read the input file. */
252b5132
RH
1010 switch (input_format)
1011 {
1012 default:
1013 abort ();
1014 case RES_FORMAT_RC:
1015 resources = read_rc_file (input_filename, preprocessor, preprocargs,
5a298d2d 1016 language, use_temp_file);
252b5132
RH
1017 break;
1018 case RES_FORMAT_RES:
1019 resources = read_res_file (input_filename);
1020 break;
1021 case RES_FORMAT_COFF:
1022 resources = read_coff_rsrc (input_filename, target);
1023 break;
1024 }
1025
1026 if (resources == NULL)
1027 fatal (_("no resources"));
1028
1029 /* Sort the resources. This is required for COFF, convenient for
1030 rc, and unimportant for res. */
252b5132
RH
1031 resources = sort_resources (resources);
1032
1033 /* Write the output file. */
252b5132
RH
1034 reswr_init ();
1035
1036 switch (output_format)
1037 {
1038 default:
1039 abort ();
1040 case RES_FORMAT_RC:
1041 write_rc_file (output_filename, resources);
1042 break;
1043 case RES_FORMAT_RES:
1044 write_res_file (output_filename, resources);
1045 break;
1046 case RES_FORMAT_COFF:
1047 write_coff_file (output_filename, target, resources);
1048 break;
1049 }
1050
1051 xexit (0);
1052 return 0;
1053}
4a594fce
NC
1054
1055static void set_endianess (bfd *abfd, const char *target)
1056{
1057 const bfd_target *target_vec;
1058
1059 def_target_arch = NULL;
1060 target_vec = bfd_find_target (target, abfd);
1061 if (! target_vec)
1062 fatal ("Can't detect target endianess and architecture.");
1063 target_is_bigendian = ((target_vec->byteorder == BFD_ENDIAN_BIG) ? 1 : 0);
1064 {
1065 const char *tname = target_vec->name;
1066 const char **arch = bfd_arch_list();
1067 if (arch && tname)
1068 {
1069 if (strchr (tname, '-') != NULL)
1070 tname = strchr (tname, '-') + 1;
1071 while (*arch != NULL)
1072 {
1073 const char *in_a = strstr (*arch, tname);
1074 char end_ch = (in_a ? in_a[strlen(tname)] : 0);
1075 if (in_a && (in_a == *arch || in_a[-1] == ':')
1076 && end_ch == 0)
1077 {
1078 def_target_arch = *arch;
1079 break;
1080 }
1081 arch++;
1082 }
1083 }
1084 if (! def_target_arch)
1085 fatal ("Can't detect architecture.");
1086 }
1087}
1088
1089bfd *
1090windres_open_as_binary (const char *filename, int rdmode)
1091{
1092 bfd *abfd;
1093
1094 abfd = (rdmode ? bfd_openr (filename, "binary") : bfd_openw (filename, "binary"));
1095 if (! abfd)
1096 fatal ("can't open `%s' for %s", filename, (rdmode ? "input" : "output"));
1097
1098 if (rdmode && ! bfd_check_format (abfd, bfd_object))
1099 fatal ("can't open `%s' for input.", filename);
1100
1101 return abfd;
1102}
1103
1104void
1105set_windres_bfd_endianess (windres_bfd *wrbfd, int is_bigendian)
1106{
1107 assert (!! wrbfd);
1108 switch (WR_KIND(wrbfd))
1109 {
1110 case WR_KIND_BFD_BIN_L:
1111 if (is_bigendian)
1112 WR_KIND(wrbfd) = WR_KIND_BFD_BIN_B;
1113 break;
1114 case WR_KIND_BFD_BIN_B:
1115 if (! is_bigendian)
1116 WR_KIND(wrbfd) = WR_KIND_BFD_BIN_L;
1117 break;
1118 default:
1119 /* only binary bfd can be overriden. */
1120 abort ();
1121 }
1122}
1123
1124void
1125set_windres_bfd (windres_bfd *wrbfd, bfd *abfd, asection *sec, rc_uint_type kind)
1126{
1127 assert (!! wrbfd);
1128 switch (kind)
1129 {
1130 case WR_KIND_TARGET:
1131 abfd = NULL;
1132 sec = NULL;
1133 break;
1134 case WR_KIND_BFD:
1135 case WR_KIND_BFD_BIN_L:
1136 case WR_KIND_BFD_BIN_B:
1137 assert (!! abfd);
1138 assert (!!sec);
1139 break;
1140 default:
1141 abort ();
1142 }
1143 WR_KIND(wrbfd) = kind;
1144 WR_BFD(wrbfd) = abfd;
1145 WR_SECTION(wrbfd) = sec;
1146}
1147
1148void
1149set_windres_bfd_content(windres_bfd *wrbfd, const void *data, rc_uint_type off,
1150 rc_uint_type length)
1151{
1152 if (WR_KIND(wrbfd) != WR_KIND_TARGET)
1153 {
1154 if (! bfd_set_section_contents (WR_BFD(wrbfd), WR_SECTION(wrbfd), data, off, length))
1155 bfd_fatal ("bfd_set_section_contents");
1156 }
1157 else
1158 abort ();
1159}
1160
1161void
1162get_windres_bfd_content(windres_bfd *wrbfd, void *data, rc_uint_type off,
1163 rc_uint_type length)
1164{
1165 if (WR_KIND(wrbfd) != WR_KIND_TARGET)
1166 {
1167 if (! bfd_get_section_contents (WR_BFD(wrbfd), WR_SECTION(wrbfd), data, off, length))
1168 bfd_fatal ("bfd_get_section_contents");
1169 }
1170 else
1171 abort ();
1172}
1173
1174void
1175windres_put_8 (windres_bfd *wrbfd, void *p, rc_uint_type value)
1176{
1177 switch (WR_KIND(wrbfd))
1178 {
1179 case WR_KIND_TARGET:
1180 target_put_8 (p, value);
1181 break;
1182 case WR_KIND_BFD:
1183 case WR_KIND_BFD_BIN_L:
1184 case WR_KIND_BFD_BIN_B:
1185 bfd_put_8 (WR_BFD(wrbfd), value, p);
1186 break;
1187 default:
1188 abort ();
1189 }
1190}
1191
1192void
1193windres_put_16 (windres_bfd *wrbfd, void *data, rc_uint_type value)
1194{
1195 switch (WR_KIND(wrbfd))
1196 {
1197 case WR_KIND_TARGET:
1198 target_put_16 (data, value);
1199 break;
1200 case WR_KIND_BFD:
1201 case WR_KIND_BFD_BIN_B:
1202 bfd_put_16 (WR_BFD(wrbfd), value, data);
1203 break;
1204 case WR_KIND_BFD_BIN_L:
1205 bfd_putl16 (value, data);
1206 break;
1207 default:
1208 abort ();
1209 }
1210}
1211
1212void
1213windres_put_32 (windres_bfd *wrbfd, void *data, rc_uint_type value)
1214{
1215 switch (WR_KIND(wrbfd))
1216 {
1217 case WR_KIND_TARGET:
1218 target_put_32 (data, value);
1219 break;
1220 case WR_KIND_BFD:
1221 case WR_KIND_BFD_BIN_B:
1222 bfd_put_32 (WR_BFD(wrbfd), value, data);
1223 break;
1224 case WR_KIND_BFD_BIN_L:
1225 bfd_putl32 (value, data);
1226 break;
1227 default:
1228 abort ();
1229 }
1230}
1231
1232rc_uint_type
1233windres_get_8 (windres_bfd *wrbfd, const void *data, rc_uint_type length)
1234{
1235 if (length < 1)
1236 fatal ("windres_get_8: unexpected eob.");
1237 switch (WR_KIND(wrbfd))
1238 {
1239 case WR_KIND_TARGET:
1240 return target_get_8 (data, length);
1241 case WR_KIND_BFD:
1242 case WR_KIND_BFD_BIN_B:
1243 case WR_KIND_BFD_BIN_L:
1244 return bfd_get_8 (WR_BFD(wrbfd), data);
1245 default:
1246 abort ();
1247 }
1248 return 0;
1249}
1250
1251rc_uint_type
1252windres_get_16 (windres_bfd *wrbfd, const void *data, rc_uint_type length)
1253{
1254 if (length < 2)
1255 fatal ("windres_get_16: unexpected eob.");
1256 switch (WR_KIND(wrbfd))
1257 {
1258 case WR_KIND_TARGET:
1259 return target_get_16 (data, length);
1260 case WR_KIND_BFD:
1261 case WR_KIND_BFD_BIN_B:
1262 return bfd_get_16 (WR_BFD(wrbfd), data);
1263 case WR_KIND_BFD_BIN_L:
1264 return bfd_getl16 (data);
1265 default:
1266 abort ();
1267 }
1268 return 0;
1269}
1270
1271rc_uint_type
1272windres_get_32 (windres_bfd *wrbfd, const void *data, rc_uint_type length)
1273{
1274 if (length < 4)
1275 fatal ("windres_get_32: unexpected eob.");
1276 switch (WR_KIND(wrbfd))
1277 {
1278 case WR_KIND_TARGET:
1279 return target_get_32 (data, length);
1280 case WR_KIND_BFD:
1281 case WR_KIND_BFD_BIN_B:
1282 return bfd_get_32 (WR_BFD(wrbfd), data);
1283 case WR_KIND_BFD_BIN_L:
1284 return bfd_getl32 (data);
1285 default:
1286 abort ();
1287 }
1288 return 0;
1289}
1290
1291static rc_uint_type
1292target_get_8 (const void *p, rc_uint_type length)
1293{
1294 rc_uint_type ret;
1295
1296 if (length < 1)
1297 fatal ("Resource too small for getting 8-bit value.");
1298
1299 ret = (rc_uint_type) *((const bfd_byte *) p);
1300 return ret & 0xff;
1301}
1302
1303static rc_uint_type
1304target_get_16 (const void *p, rc_uint_type length)
1305{
1306 if (length < 2)
1307 fatal ("Resource too small for getting 16-bit value.");
1308
1309 if (target_is_bigendian)
1310 return bfd_getb16 (p);
1311 else
1312 return bfd_getl16 (p);
1313}
1314
1315static rc_uint_type
1316target_get_32 (const void *p, rc_uint_type length)
1317{
1318 if (length < 4)
1319 fatal ("Resource too small for getting 32-bit value.");
1320
1321 if (target_is_bigendian)
1322 return bfd_getb32 (p);
1323 else
1324 return bfd_getl32 (p);
1325}
1326
1327static void
1328target_put_8 (void *p, rc_uint_type value)
1329{
1330 assert (!! p);
1331 *((bfd_byte *) p)=(bfd_byte) value;
1332}
1333
1334static void
1335target_put_16 (void *p, rc_uint_type value)
1336{
1337 assert (!! p);
1338
1339 if (target_is_bigendian)
1340 bfd_putb16 (value, p);
1341 else
1342 bfd_putl16 (value, p);
1343}
1344
1345static void
1346target_put_32 (void *p, rc_uint_type value)
1347{
1348 assert (!! p);
1349
1350 if (target_is_bigendian)
1351 bfd_putb32 (value, p);
1352 else
1353 bfd_putl32 (value, p);
1354}
1355
1356static int isInComment = 0;
1357
1358int wr_printcomment (FILE *e, const char *fmt, ...)
1359{
1360 va_list arg;
1361 int r = 0;
1362
1363 if (isInComment)
1364 r += fprintf (e, "\n ");
1365 else
1366 fprintf (e, "/* ");
1367 isInComment = 1;
1368 if (fmt == NULL)
1369 return r;
1370 va_start (arg, fmt);
1371 r += vfprintf (e, fmt, arg);
1372 va_end (arg);
1373 return r;
1374}
1375
1376int wr_print (FILE *e, const char *fmt, ...)
1377{
1378 va_list arg;
1379 int r = 0;
1380 if (isInComment)
1381 r += fprintf (e, ". */\n");
1382 isInComment = 0;
1383 if (! fmt)
1384 return r;
1385 va_start (arg, fmt);
1386 r += vfprintf (e, fmt, arg);
1387 va_end (arg);
1388 return r;
1389}
This page took 0.379517 seconds and 4 git commands to generate.