Automatic date update in version.in
[binutils-gdb.git] / binutils / windres.c
blobf0f6433160c36a32cf3568d0c5f05d28f53a3f9f
1 /* windres.c -- a program to manipulate Windows resources
2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
4 Rewritten by Kai Tietz, Onevision.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This program can read and write Windows resources in various
24 formats. In particular, it can act like the rc resource compiler
25 program, and it can act like the cvtres res to COFF conversion
26 program.
28 It is based on information taken from the following sources:
30 * Microsoft documentation.
32 * The rcl program, written by Gunther Ebert
33 <gunther.ebert@ixos-leipzig.de>.
35 * The res2coff program, written by Pedro A. Aranda <paag@tid.es>. */
37 #include "sysdep.h"
38 #include <assert.h>
39 #include "bfd.h"
40 #include "getopt.h"
41 #include "bucomm.h"
42 #include "libiberty.h"
43 #include "safe-ctype.h"
44 #include "obstack.h"
45 #include "windres.h"
47 /* Used by resrc.c at least. */
49 int verbose = 0;
51 bool target_is_bigendian = 0;
52 const char *def_target_arch;
54 static void set_endianness (bfd *, const char *);
56 /* An enumeration of format types. */
58 enum res_format
60 /* Unknown format. */
61 RES_FORMAT_UNKNOWN,
62 /* Textual RC file. */
63 RES_FORMAT_RC,
64 /* Binary RES file. */
65 RES_FORMAT_RES,
66 /* COFF file. */
67 RES_FORMAT_COFF
70 /* A structure used to map between format types and strings. */
72 struct format_map
74 const char *name;
75 enum res_format format;
78 /* A mapping between names and format types. */
80 static const struct format_map format_names[] =
82 { "rc", RES_FORMAT_RC },
83 { "res", RES_FORMAT_RES },
84 { "coff", RES_FORMAT_COFF },
85 { NULL, RES_FORMAT_UNKNOWN }
88 /* A mapping from file extensions to format types. */
90 static const struct format_map format_fileexts[] =
92 { "rc", RES_FORMAT_RC },
93 { "res", RES_FORMAT_RES },
94 { "exe", RES_FORMAT_COFF },
95 { "obj", RES_FORMAT_COFF },
96 { "o", RES_FORMAT_COFF },
97 { NULL, RES_FORMAT_UNKNOWN }
100 /* A list of include directories. */
102 struct include_dir
104 struct include_dir *next;
105 char *dir;
108 static struct include_dir *include_dirs;
110 /* Static functions. */
112 static void res_init (void);
113 static int extended_menuitems (const rc_menuitem *);
114 static enum res_format format_from_name (const char *, int);
115 static enum res_format format_from_filename (const char *, int);
116 static void usage (FILE *, int);
117 static int cmp_res_entry (const void *, const void *);
118 static rc_res_directory *sort_resources (rc_res_directory *);
119 static void reswr_init (void);
120 static const char * quot (const char *);
122 static rc_uint_type target_get_8 (const void *, rc_uint_type);
123 static void target_put_8 (void *, rc_uint_type);
124 static rc_uint_type target_get_16 (const void *, rc_uint_type);
125 static void target_put_16 (void *, rc_uint_type);
126 static rc_uint_type target_get_32 (const void *, rc_uint_type);
127 static void target_put_32 (void *, rc_uint_type);
130 /* When we are building a resource tree, we allocate everything onto
131 an obstack, so that we can free it all at once if we want. */
133 #define obstack_chunk_alloc xmalloc
134 #define obstack_chunk_free free
136 /* The resource building obstack. */
138 static struct obstack res_obstack;
140 /* Initialize the resource building obstack. */
142 static void
143 res_init (void)
145 obstack_init (&res_obstack);
148 /* Allocate space on the resource building obstack. */
150 void *
151 res_alloc (rc_uint_type bytes)
153 return obstack_alloc (&res_obstack, (size_t) bytes);
156 /* We also use an obstack to save memory used while writing out a set
157 of resources. */
159 static struct obstack reswr_obstack;
161 /* Initialize the resource writing obstack. */
163 static void
164 reswr_init (void)
166 obstack_init (&reswr_obstack);
169 /* Allocate space on the resource writing obstack. */
171 void *
172 reswr_alloc (rc_uint_type bytes)
174 return obstack_alloc (&reswr_obstack, (size_t) bytes);
177 /* Open a file using the include directory search list. */
179 FILE *
180 open_file_search (const char *filename, const char *mode, const char *errmsg,
181 char **real_filename)
183 FILE *e;
184 struct include_dir *d;
186 e = fopen (filename, mode);
187 if (e != NULL)
189 *real_filename = xstrdup (filename);
190 return e;
193 if (errno == ENOENT)
195 for (d = include_dirs; d != NULL; d = d->next)
197 char *n;
199 n = (char *) xmalloc (strlen (d->dir) + strlen (filename) + 2);
200 sprintf (n, "%s/%s", d->dir, filename);
201 e = fopen (n, mode);
202 if (e != NULL)
204 *real_filename = n;
205 return e;
207 free (n);
209 if (errno != ENOENT)
210 break;
214 fatal (_("can't open %s `%s': %s"), errmsg, filename, strerror (errno));
216 /* Return a value to avoid a compiler warning. */
217 return NULL;
220 /* Compare two resource ID's. We consider name entries to come before
221 numeric entries, because that is how they appear in the COFF .rsrc
222 section. */
225 res_id_cmp (rc_res_id a, rc_res_id b)
227 if (! a.named)
229 if (b.named)
230 return 1;
231 if (a.u.id > b.u.id)
232 return 1;
233 else if (a.u.id < b.u.id)
234 return -1;
235 else
236 return 0;
238 else
240 unichar *as, *ase, *bs, *bse;
242 if (! b.named)
243 return -1;
245 as = a.u.n.name;
246 ase = as + a.u.n.length;
247 bs = b.u.n.name;
248 bse = bs + b.u.n.length;
250 while (as < ase)
252 int i;
254 if (bs >= bse)
255 return 1;
256 i = (int) *as - (int) *bs;
257 if (i != 0)
258 return i;
259 ++as;
260 ++bs;
263 if (bs < bse)
264 return -1;
266 return 0;
270 /* Print a resource ID. */
272 void
273 res_id_print (FILE *stream, rc_res_id id, int quote)
275 if (! id.named)
276 fprintf (stream, "%u", (int) id.u.id);
277 else
279 if (quote)
280 unicode_print_quoted (stream, id.u.n.name, id.u.n.length);
281 else
282 unicode_print (stream, id.u.n.name, id.u.n.length);
286 /* Print a list of resource ID's. */
288 void
289 res_ids_print (FILE *stream, int cids, const rc_res_id *ids)
291 int i;
293 for (i = 0; i < cids; i++)
295 res_id_print (stream, ids[i], 1);
296 if (i + 1 < cids)
297 fprintf (stream, ": ");
301 /* Convert an ASCII string to a resource ID. */
303 void
304 res_string_to_id (rc_res_id *res_id, const char *string)
306 res_id->named = 1;
307 unicode_from_ascii (&res_id->u.n.length, &res_id->u.n.name, string);
310 /* Convert an unicode string to a resource ID. */
311 void
312 res_unistring_to_id (rc_res_id *res_id, const unichar *u)
314 res_id->named = 1;
315 res_id->u.n.length = unichar_len (u);
316 res_id->u.n.name = unichar_dup_uppercase (u);
319 /* Define a resource. The arguments are the resource tree, RESOURCES,
320 and the location at which to put it in the tree, CIDS and IDS.
321 This returns a newly allocated rc_res_resource structure, which the
322 caller is expected to initialize. If DUPOK is non-zero, then if a
323 resource with this ID exists, it is returned. Otherwise, a warning
324 is issued, and a new resource is created replacing the existing
325 one. */
327 rc_res_resource *
328 define_resource (rc_res_directory **resources, int cids,
329 const rc_res_id *ids, int dupok)
331 rc_res_entry *re = NULL;
332 int i;
334 assert (cids > 0);
335 for (i = 0; i < cids; i++)
337 rc_res_entry **pp;
339 if (*resources == NULL)
341 *resources = ((rc_res_directory *)
342 res_alloc (sizeof (rc_res_directory)));
343 (*resources)->characteristics = 0;
344 /* Using a real timestamp only serves to create non-deterministic
345 results. Use zero instead. */
346 (*resources)->time = 0;
347 (*resources)->major = 0;
348 (*resources)->minor = 0;
349 (*resources)->entries = NULL;
352 for (pp = &(*resources)->entries; *pp != NULL; pp = &(*pp)->next)
353 if (res_id_cmp ((*pp)->id, ids[i]) == 0)
354 break;
356 if (*pp != NULL)
357 re = *pp;
358 else
360 re = (rc_res_entry *) res_alloc (sizeof (rc_res_entry));
361 re->next = NULL;
362 re->id = ids[i];
363 if ((i + 1) < cids)
365 re->subdir = 1;
366 re->u.dir = NULL;
368 else
370 re->subdir = 0;
371 re->u.res = NULL;
374 *pp = re;
377 if ((i + 1) < cids)
379 if (! re->subdir)
381 fprintf (stderr, "%s: ", program_name);
382 res_ids_print (stderr, i, ids);
383 fprintf (stderr, _(": expected to be a directory\n"));
384 xexit (1);
387 resources = &re->u.dir;
391 if (re->subdir)
393 fprintf (stderr, "%s: ", program_name);
394 res_ids_print (stderr, cids, ids);
395 fprintf (stderr, _(": expected to be a leaf\n"));
396 xexit (1);
399 if (re->u.res != NULL)
401 if (dupok)
402 return re->u.res;
404 fprintf (stderr, _("%s: warning: "), program_name);
405 res_ids_print (stderr, cids, ids);
406 fprintf (stderr, _(": duplicate value\n"));
409 re->u.res = ((rc_res_resource *)
410 res_alloc (sizeof (rc_res_resource)));
411 memset (re->u.res, 0, sizeof (rc_res_resource));
413 re->u.res->type = RES_TYPE_UNINITIALIZED;
414 return re->u.res;
417 /* Define a standard resource. This is a version of define_resource
418 that just takes type, name, and language arguments. */
420 rc_res_resource *
421 define_standard_resource (rc_res_directory **resources, int type,
422 rc_res_id name, rc_uint_type language, int dupok)
424 rc_res_id a[3];
426 a[0].named = 0;
427 a[0].u.id = type;
428 a[1] = name;
429 a[2].named = 0;
430 a[2].u.id = language;
431 return define_resource (resources, 3, a, dupok);
434 /* Comparison routine for resource sorting. */
436 static int
437 cmp_res_entry (const void *p1, const void *p2)
439 const rc_res_entry **re1, **re2;
441 re1 = (const rc_res_entry **) p1;
442 re2 = (const rc_res_entry **) p2;
443 return res_id_cmp ((*re1)->id, (*re2)->id);
446 /* Sort the resources. */
448 static rc_res_directory *
449 sort_resources (rc_res_directory *resdir)
451 int c, i;
452 rc_res_entry *re;
453 rc_res_entry **a;
455 if (resdir->entries == NULL)
456 return resdir;
458 c = 0;
459 for (re = resdir->entries; re != NULL; re = re->next)
460 ++c;
462 /* This is a recursive routine, so using xmalloc is probably better
463 than alloca. */
464 a = (rc_res_entry **) xmalloc (c * sizeof (rc_res_entry *));
466 for (i = 0, re = resdir->entries; re != NULL; re = re->next, i++)
467 a[i] = re;
469 qsort (a, c, sizeof (rc_res_entry *), cmp_res_entry);
471 resdir->entries = a[0];
472 for (i = 0; i < c - 1; i++)
473 a[i]->next = a[i + 1];
474 a[i]->next = NULL;
476 free (a);
478 /* Now sort the subdirectories. */
480 for (re = resdir->entries; re != NULL; re = re->next)
481 if (re->subdir)
482 re->u.dir = sort_resources (re->u.dir);
484 return resdir;
487 /* Return whether the dialog resource DIALOG is a DIALOG or a
488 DIALOGEX. */
491 extended_dialog (const rc_dialog *dialog)
493 const rc_dialog_control *c;
495 if (dialog->ex != NULL)
496 return 1;
498 for (c = dialog->controls; c != NULL; c = c->next)
499 if (c->data != NULL || c->help != 0)
500 return 1;
502 return 0;
505 /* Return whether MENUITEMS are a MENU or a MENUEX. */
508 extended_menu (const rc_menu *menu)
510 return extended_menuitems (menu->items);
513 static int
514 extended_menuitems (const rc_menuitem *menuitems)
516 const rc_menuitem *mi;
518 for (mi = menuitems; mi != NULL; mi = mi->next)
520 if (mi->help != 0 || mi->state != 0)
521 return 1;
522 if (mi->popup != NULL && mi->id != 0)
523 return 1;
524 if ((mi->type
525 & ~ (MENUITEM_CHECKED
526 | MENUITEM_GRAYED
527 | MENUITEM_HELP
528 | MENUITEM_INACTIVE
529 | MENUITEM_MENUBARBREAK
530 | MENUITEM_BITMAP
531 | MENUITEM_OWNERDRAW
532 | MENUITEM_MENUBREAK))
533 != 0)
534 return 1;
535 if (mi->popup != NULL)
537 if (extended_menuitems (mi->popup))
538 return 1;
542 return 0;
545 /* Convert a string to a format type, or exit if it can't be done. */
547 static enum res_format
548 format_from_name (const char *name, int exit_on_error)
550 const struct format_map *m;
552 for (m = format_names; m->name != NULL; m++)
553 if (strcasecmp (m->name, name) == 0)
554 break;
556 if (m->name == NULL && exit_on_error)
558 non_fatal (_("unknown format type `%s'"), name);
559 fprintf (stderr, _("%s: supported formats:"), program_name);
560 for (m = format_names; m->name != NULL; m++)
561 fprintf (stderr, " %s", m->name);
562 fprintf (stderr, "\n");
563 xexit (1);
566 return m->format;
569 /* Work out a format type given a file name. If INPUT is non-zero,
570 it's OK to look at the file itself. */
572 static enum res_format
573 format_from_filename (const char *filename, int input)
575 const char *ext;
576 FILE *e;
577 bfd_byte b1, b2, b3, b4, b5;
578 int magic;
580 /* If we have an extension, see if we recognize it as implying a
581 particular format. */
582 ext = strrchr (filename, '.');
583 if (ext != NULL)
585 const struct format_map *m;
587 ++ext;
588 for (m = format_fileexts; m->name != NULL; m++)
589 if (strcasecmp (m->name, ext) == 0)
590 return m->format;
593 /* If we don't recognize the name of an output file, assume it's a
594 COFF file. */
595 if (! input)
596 return RES_FORMAT_COFF;
598 /* Read the first few bytes of the file to see if we can guess what
599 it is. */
600 e = fopen (filename, FOPEN_RB);
601 if (e == NULL)
602 fatal ("%s: %s", filename, strerror (errno));
604 b1 = getc (e);
605 b2 = getc (e);
606 b3 = getc (e);
607 b4 = getc (e);
608 b5 = getc (e);
610 fclose (e);
612 /* A PE executable starts with 0x4d 0x5a. */
613 if (b1 == 0x4d && b2 == 0x5a)
614 return RES_FORMAT_COFF;
616 /* A COFF .o file starts with a COFF magic number. */
617 magic = (b2 << 8) | b1;
618 switch (magic)
620 case 0x14c: /* i386 */
621 case 0x166: /* MIPS */
622 case 0x184: /* Alpha */
623 case 0x268: /* 68k */
624 case 0x1f0: /* PowerPC */
625 case 0x290: /* PA */
626 return RES_FORMAT_COFF;
629 /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
630 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0 && b5 == 0x20)
631 return RES_FORMAT_RES;
633 /* If every character is printable or space, assume it's an RC file. */
634 if ((ISPRINT (b1) || ISSPACE (b1))
635 && (ISPRINT (b2) || ISSPACE (b2))
636 && (ISPRINT (b3) || ISSPACE (b3))
637 && (ISPRINT (b4) || ISSPACE (b4))
638 && (ISPRINT (b5) || ISSPACE (b5)))
639 return RES_FORMAT_RC;
641 /* Otherwise, we give up. */
642 fatal (_("can not determine type of file `%s'; use the -J option"),
643 filename);
645 /* Return something to silence the compiler warning. */
646 return RES_FORMAT_UNKNOWN;
649 /* Print a usage message and exit. */
651 static void
652 usage (FILE *stream, int status)
654 fprintf (stream, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
655 program_name);
656 fprintf (stream, _(" The options are:\n\
657 -i --input=<file> Name input file\n\
658 -o --output=<file> Name output file\n\
659 -J --input-format=<format> Specify input format\n\
660 -O --output-format=<format> Specify output format\n\
661 -F --target=<target> Specify COFF target\n\
662 --preprocessor=<program> Program to use to preprocess rc file\n\
663 --preprocessor-arg=<arg> Additional preprocessor argument\n\
664 -I --include-dir=<dir> Include directory when preprocessing rc file\n\
665 -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\
666 -U --undefine <sym> Undefine SYM when preprocessing rc file\n\
667 -v --verbose Verbose - tells you what it's doing\n\
668 -c --codepage=<codepage> Specify default codepage\n\
669 -l --language=<val> Set language when reading rc file\n\
670 --use-temp-file Use a temporary file instead of popen to read\n\
671 the preprocessor output\n\
672 --no-use-temp-file Use popen (default)\n"));
673 #ifdef YYDEBUG
674 fprintf (stream, _("\
675 --yydebug Turn on parser debugging\n"));
676 #endif
677 fprintf (stream, _("\
678 -r Ignored for compatibility with rc\n\
679 @<file> Read options from <file>\n\
680 -h --help Print this help message\n\
681 -V --version Print version information\n"));
682 fprintf (stream, _("\
683 FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
684 extension if not specified. A single file name is an input file.\n\
685 No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
687 list_supported_targets (program_name, stream);
689 if (REPORT_BUGS_TO[0] && status == 0)
690 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
692 exit (status);
695 /* Quote characters that will confuse the shell when we run the preprocessor. */
697 static const char *
698 quot (const char *string)
700 static char *buf = 0;
701 static int buflen = 0;
702 int slen = strlen (string);
703 const char *src;
704 char *dest;
706 if ((buflen < slen * 2 + 3) || ! buf)
708 buflen = slen * 2 + 3;
709 free (buf);
710 buf = (char *) xmalloc (buflen);
713 for (src = string, dest = buf; *src; src++, dest++)
715 if (*src == '(' || *src == ')' || *src == ' ')
716 *dest++ = '\\';
717 *dest = *src;
720 *dest = 0;
721 return buf;
724 /* Long options. */
726 enum option_values
728 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
729 OPTION_PREPROCESSOR = 150,
730 OPTION_USE_TEMP_FILE,
731 OPTION_NO_USE_TEMP_FILE,
732 OPTION_YYDEBUG,
733 OPTION_INCLUDE_DIR,
734 OPTION_PREPROCESSOR_ARG
737 static const struct option long_options[] =
739 {"input", required_argument, 0, 'i'},
740 {"output", required_argument, 0, 'o'},
741 {"input-format", required_argument, 0, 'J'},
742 {"output-format", required_argument, 0, 'O'},
743 {"target", required_argument, 0, 'F'},
744 {"preprocessor", required_argument, 0, OPTION_PREPROCESSOR},
745 {"preprocessor-arg", required_argument, 0, OPTION_PREPROCESSOR_ARG},
746 {"include-dir", required_argument, 0, OPTION_INCLUDE_DIR},
747 {"define", required_argument, 0, 'D'},
748 {"undefine", required_argument, 0, 'U'},
749 {"verbose", no_argument, 0, 'v'},
750 {"codepage", required_argument, 0, 'c'},
751 {"language", required_argument, 0, 'l'},
752 {"use-temp-file", no_argument, 0, OPTION_USE_TEMP_FILE},
753 {"no-use-temp-file", no_argument, 0, OPTION_NO_USE_TEMP_FILE},
754 {"yydebug", no_argument, 0, OPTION_YYDEBUG},
755 {"version", no_argument, 0, 'V'},
756 {"help", no_argument, 0, 'h'},
757 {0, no_argument, 0, 0}
760 void
761 windres_add_include_dir (const char *p)
763 struct include_dir *n, **pp;
765 /* Computing paths is often complicated and error prone.
766 The easiest way to check for mistakes is at the time
767 we add them to include_dirs. */
768 assert (p != NULL);
769 assert (*p != '\0');
771 n = xmalloc (sizeof *n);
772 n->next = NULL;
773 n->dir = (char * ) p;
775 for (pp = &include_dirs; *pp != NULL; pp = &(*pp)->next)
777 *pp = n;
780 /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */
781 int main (int, char **);
783 /* The main function. */
786 main (int argc, char **argv)
788 int c;
789 char *input_filename;
790 char *output_filename;
791 enum res_format input_format;
792 enum res_format input_format_tmp;
793 enum res_format output_format;
794 char *target;
795 char *preprocessor;
796 char *preprocargs;
797 const char *quotedarg;
798 int language;
799 rc_res_directory *resources;
800 int use_temp_file;
802 #ifdef HAVE_LC_MESSAGES
803 setlocale (LC_MESSAGES, "");
804 #endif
805 setlocale (LC_CTYPE, "");
806 bindtextdomain (PACKAGE, LOCALEDIR);
807 textdomain (PACKAGE);
809 program_name = argv[0];
810 xmalloc_set_program_name (program_name);
811 bfd_set_error_program_name (program_name);
813 expandargv (&argc, &argv);
815 if (bfd_init () != BFD_INIT_MAGIC)
816 fatal (_("fatal error: libbfd ABI mismatch"));
817 set_default_bfd_target ();
819 res_init ();
821 input_filename = NULL;
822 output_filename = NULL;
823 input_format = RES_FORMAT_UNKNOWN;
824 output_format = RES_FORMAT_UNKNOWN;
825 target = NULL;
826 preprocessor = NULL;
827 preprocargs = NULL;
828 language = 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
829 use_temp_file = 0;
831 while ((c = getopt_long (argc, argv, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options,
832 (int *) 0)) != EOF)
834 switch (c)
836 case 'c':
838 rc_uint_type ncp;
840 if (optarg[0] == '0' && (optarg[1] == 'x' || optarg[1] == 'X'))
841 ncp = (rc_uint_type) strtol (optarg + 2, NULL, 16);
842 else
843 ncp = (rc_uint_type) strtol (optarg, NULL, 10);
844 if (ncp == CP_UTF16 || ! unicode_is_valid_codepage (ncp))
845 fatal (_("invalid codepage specified.\n"));
846 wind_default_codepage = wind_current_codepage = ncp;
848 break;
850 case 'i':
851 input_filename = optarg;
852 break;
854 case 'f':
855 /* For compatibility with rc we accept "-fo <name>" as being the
856 equivalent of "-o <name>". We do not advertise this fact
857 though, as we do not want users to use non-GNU like command
858 line switches. */
859 if (*optarg != 'o')
860 fatal (_("invalid option -f\n"));
861 optarg++;
862 if (* optarg == 0)
864 if (optind == argc)
865 fatal (_("No filename following the -fo option.\n"));
866 optarg = argv [optind++];
868 /* Fall through. */
870 case 'o':
871 output_filename = optarg;
872 break;
874 case 'J':
875 input_format = format_from_name (optarg, 1);
876 break;
878 case 'O':
879 output_format = format_from_name (optarg, 1);
880 break;
882 case 'F':
883 target = optarg;
884 break;
886 case OPTION_PREPROCESSOR:
887 if (strchr (optarg, ' '))
888 preprocessor = xasprintf ("\"%s\"", optarg);
889 else
890 preprocessor = optarg;
891 break;
893 case OPTION_PREPROCESSOR_ARG:
894 if (preprocargs == NULL)
896 quotedarg = quot (optarg);
897 preprocargs = xstrdup (quotedarg);
899 else
901 char *n;
903 quotedarg = quot (optarg);
904 n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 2);
905 sprintf (n, "%s %s", preprocargs, quotedarg);
906 free (preprocargs);
907 preprocargs = n;
909 break;
911 case 'D':
912 case 'U':
913 if (preprocargs == NULL)
915 quotedarg = quot (optarg);
916 preprocargs = xmalloc (strlen (quotedarg) + 3);
917 sprintf (preprocargs, "-%c%s", c, quotedarg);
919 else
921 char *n;
923 quotedarg = quot (optarg);
924 n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
925 sprintf (n, "%s -%c%s", preprocargs, c, quotedarg);
926 free (preprocargs);
927 preprocargs = n;
929 break;
931 case 'r':
932 /* Ignored for compatibility with rc. */
933 break;
935 case 'v':
936 verbose ++;
937 break;
939 case 'I':
940 /* For backward compatibility, should be removed in the future. */
941 input_format_tmp = format_from_name (optarg, 0);
942 if (input_format_tmp != RES_FORMAT_UNKNOWN)
944 struct stat statbuf;
945 char modebuf[11];
947 if (stat (optarg, & statbuf) == 0
948 /* Coded this way to avoid importing knowledge of S_ISDIR into this file. */
949 && (mode_string (statbuf.st_mode, modebuf), modebuf[0] == 'd'))
950 /* We have a -I option with a directory name that just happens
951 to match a format name as well. eg: -I res Assume that the
952 user knows what they are doing and do not complain. */
954 else
956 fprintf (stderr,
957 _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
958 input_format = input_format_tmp;
959 break;
962 /* Fall through. */
964 case OPTION_INCLUDE_DIR:
965 if (preprocargs == NULL)
967 quotedarg = quot (optarg);
968 preprocargs = xmalloc (strlen (quotedarg) + 3);
969 sprintf (preprocargs, "-I%s", quotedarg);
971 else
973 char *n;
975 quotedarg = quot (optarg);
976 n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
977 sprintf (n, "%s -I%s", preprocargs, quotedarg);
978 free (preprocargs);
979 preprocargs = n;
982 windres_add_include_dir (optarg);
984 break;
986 case 'l':
987 language = strtol (optarg, (char **) NULL, 16);
988 break;
990 case OPTION_USE_TEMP_FILE:
991 use_temp_file = 1;
992 break;
994 case OPTION_NO_USE_TEMP_FILE:
995 use_temp_file = 0;
996 break;
998 #ifdef YYDEBUG
999 case OPTION_YYDEBUG:
1000 yydebug = 1;
1001 break;
1002 #endif
1004 case 'h':
1005 case 'H':
1006 usage (stdout, 0);
1007 break;
1009 case 'V':
1010 print_version ("windres");
1011 break;
1013 default:
1014 usage (stderr, 1);
1015 break;
1019 if (input_filename == NULL && optind < argc)
1021 input_filename = argv[optind];
1022 ++optind;
1025 if (output_filename == NULL && optind < argc)
1027 output_filename = argv[optind];
1028 ++optind;
1031 if (argc != optind)
1032 usage (stderr, 1);
1034 if (input_format == RES_FORMAT_UNKNOWN)
1036 if (input_filename == NULL)
1037 input_format = RES_FORMAT_RC;
1038 else
1039 input_format = format_from_filename (input_filename, 1);
1042 if (output_format == RES_FORMAT_UNKNOWN)
1044 if (output_filename == NULL)
1045 output_format = RES_FORMAT_RC;
1046 else
1047 output_format = format_from_filename (output_filename, 0);
1050 set_endianness (NULL, target);
1052 /* Read the input file. */
1053 switch (input_format)
1055 default:
1056 abort ();
1057 case RES_FORMAT_RC:
1058 resources = read_rc_file (input_filename, preprocessor, preprocargs,
1059 language, use_temp_file);
1060 break;
1061 case RES_FORMAT_RES:
1062 resources = read_res_file (input_filename);
1063 break;
1064 case RES_FORMAT_COFF:
1065 resources = read_coff_rsrc (input_filename, target);
1066 break;
1069 if (resources == NULL)
1070 fatal (_("no resources"));
1072 /* Sort the resources. This is required for COFF, convenient for
1073 rc, and unimportant for res. */
1074 resources = sort_resources (resources);
1076 /* Write the output file. */
1077 reswr_init ();
1079 switch (output_format)
1081 default:
1082 abort ();
1083 case RES_FORMAT_RC:
1084 write_rc_file (output_filename, resources);
1085 break;
1086 case RES_FORMAT_RES:
1087 write_res_file (output_filename, resources);
1088 break;
1089 case RES_FORMAT_COFF:
1090 write_coff_file (output_filename, target, resources);
1091 break;
1094 xexit (0);
1095 return 0;
1098 static void
1099 set_endianness (bfd *abfd, const char *target)
1101 const bfd_target *target_vec;
1103 def_target_arch = NULL;
1104 target_vec = bfd_get_target_info (target, abfd, &target_is_bigendian, NULL,
1105 &def_target_arch);
1106 if (! target_vec)
1107 fatal ("Can't detect target endianness and architecture.");
1108 if (! def_target_arch)
1109 fatal ("Can't detect architecture.");
1112 bfd *
1113 windres_open_as_binary (const char *filename, int rdmode)
1115 bfd *abfd;
1117 abfd = (rdmode ? bfd_openr (filename, "binary") : bfd_openw (filename, "binary"));
1118 if (! abfd)
1119 fatal ("can't open `%s' for %s", filename, (rdmode ? "input" : "output"));
1121 if (rdmode && ! bfd_check_format (abfd, bfd_object))
1122 fatal ("can't open `%s' for input.", filename);
1124 return abfd;
1127 void
1128 set_windres_bfd_endianness (windres_bfd *wrbfd, int is_bigendian)
1130 assert (!! wrbfd);
1131 switch (WR_KIND(wrbfd))
1133 case WR_KIND_BFD_BIN_L:
1134 if (is_bigendian)
1135 WR_KIND(wrbfd) = WR_KIND_BFD_BIN_B;
1136 break;
1137 case WR_KIND_BFD_BIN_B:
1138 if (! is_bigendian)
1139 WR_KIND(wrbfd) = WR_KIND_BFD_BIN_L;
1140 break;
1141 default:
1142 /* only binary bfd can be overriden. */
1143 abort ();
1147 void
1148 set_windres_bfd (windres_bfd *wrbfd, bfd *abfd, asection *sec, rc_uint_type kind)
1150 assert (!! wrbfd);
1151 switch (kind)
1153 case WR_KIND_TARGET:
1154 abfd = NULL;
1155 sec = NULL;
1156 break;
1157 case WR_KIND_BFD:
1158 case WR_KIND_BFD_BIN_L:
1159 case WR_KIND_BFD_BIN_B:
1160 assert (!! abfd);
1161 assert (!!sec);
1162 break;
1163 default:
1164 abort ();
1166 WR_KIND(wrbfd) = kind;
1167 WR_BFD(wrbfd) = abfd;
1168 WR_SECTION(wrbfd) = sec;
1171 void
1172 set_windres_bfd_content (windres_bfd *wrbfd, const void *data, rc_uint_type off,
1173 rc_uint_type length)
1175 if (WR_KIND(wrbfd) != WR_KIND_TARGET)
1177 if (! bfd_set_section_contents (WR_BFD(wrbfd), WR_SECTION(wrbfd), data, off, length))
1178 bfd_fatal ("bfd_set_section_contents");
1180 else
1181 abort ();
1184 void
1185 get_windres_bfd_content (windres_bfd *wrbfd, void *data, rc_uint_type off,
1186 rc_uint_type length)
1188 if (WR_KIND(wrbfd) != WR_KIND_TARGET)
1190 if (! bfd_get_section_contents (WR_BFD(wrbfd), WR_SECTION(wrbfd), data, off, length))
1191 bfd_fatal ("bfd_get_section_contents");
1193 else
1194 abort ();
1197 void
1198 windres_put_8 (windres_bfd *wrbfd, void *p, rc_uint_type value)
1200 switch (WR_KIND(wrbfd))
1202 case WR_KIND_TARGET:
1203 target_put_8 (p, value);
1204 break;
1205 case WR_KIND_BFD:
1206 case WR_KIND_BFD_BIN_L:
1207 case WR_KIND_BFD_BIN_B:
1208 bfd_put_8 (WR_BFD(wrbfd), value, p);
1209 break;
1210 default:
1211 abort ();
1215 void
1216 windres_put_16 (windres_bfd *wrbfd, void *data, rc_uint_type value)
1218 switch (WR_KIND(wrbfd))
1220 case WR_KIND_TARGET:
1221 target_put_16 (data, value);
1222 break;
1223 case WR_KIND_BFD:
1224 case WR_KIND_BFD_BIN_B:
1225 bfd_put_16 (WR_BFD(wrbfd), value, data);
1226 break;
1227 case WR_KIND_BFD_BIN_L:
1228 bfd_putl16 (value, data);
1229 break;
1230 default:
1231 abort ();
1235 void
1236 windres_put_32 (windres_bfd *wrbfd, void *data, rc_uint_type value)
1238 switch (WR_KIND(wrbfd))
1240 case WR_KIND_TARGET:
1241 target_put_32 (data, value);
1242 break;
1243 case WR_KIND_BFD:
1244 case WR_KIND_BFD_BIN_B:
1245 bfd_put_32 (WR_BFD(wrbfd), value, data);
1246 break;
1247 case WR_KIND_BFD_BIN_L:
1248 bfd_putl32 (value, data);
1249 break;
1250 default:
1251 abort ();
1255 rc_uint_type
1256 windres_get_8 (windres_bfd *wrbfd, const void *data, rc_uint_type length)
1258 if (length < 1)
1259 fatal ("windres_get_8: unexpected eob.");
1260 switch (WR_KIND(wrbfd))
1262 case WR_KIND_TARGET:
1263 return target_get_8 (data, length);
1264 case WR_KIND_BFD:
1265 case WR_KIND_BFD_BIN_B:
1266 case WR_KIND_BFD_BIN_L:
1267 return bfd_get_8 (WR_BFD(wrbfd), data);
1268 default:
1269 abort ();
1271 return 0;
1274 rc_uint_type
1275 windres_get_16 (windres_bfd *wrbfd, const void *data, rc_uint_type length)
1277 if (length < 2)
1278 fatal ("windres_get_16: unexpected eob.");
1279 switch (WR_KIND(wrbfd))
1281 case WR_KIND_TARGET:
1282 return target_get_16 (data, length);
1283 case WR_KIND_BFD:
1284 case WR_KIND_BFD_BIN_B:
1285 return bfd_get_16 (WR_BFD(wrbfd), data);
1286 case WR_KIND_BFD_BIN_L:
1287 return bfd_getl16 (data);
1288 default:
1289 abort ();
1291 return 0;
1294 rc_uint_type
1295 windres_get_32 (windres_bfd *wrbfd, const void *data, rc_uint_type length)
1297 if (length < 4)
1298 fatal ("windres_get_32: unexpected eob.");
1299 switch (WR_KIND(wrbfd))
1301 case WR_KIND_TARGET:
1302 return target_get_32 (data, length);
1303 case WR_KIND_BFD:
1304 case WR_KIND_BFD_BIN_B:
1305 return bfd_get_32 (WR_BFD(wrbfd), data);
1306 case WR_KIND_BFD_BIN_L:
1307 return bfd_getl32 (data);
1308 default:
1309 abort ();
1311 return 0;
1314 static rc_uint_type
1315 target_get_8 (const void *p, rc_uint_type length)
1317 rc_uint_type ret;
1319 if (length < 1)
1320 fatal ("Resource too small for getting 8-bit value.");
1322 ret = (rc_uint_type) *((const bfd_byte *) p);
1323 return ret & 0xff;
1326 static rc_uint_type
1327 target_get_16 (const void *p, rc_uint_type length)
1329 if (length < 2)
1330 fatal ("Resource too small for getting 16-bit value.");
1332 if (target_is_bigendian)
1333 return bfd_getb16 (p);
1334 else
1335 return bfd_getl16 (p);
1338 static rc_uint_type
1339 target_get_32 (const void *p, rc_uint_type length)
1341 if (length < 4)
1342 fatal ("Resource too small for getting 32-bit value.");
1344 if (target_is_bigendian)
1345 return bfd_getb32 (p);
1346 else
1347 return bfd_getl32 (p);
1350 static void
1351 target_put_8 (void *p, rc_uint_type value)
1353 assert (!! p);
1354 *((bfd_byte *) p)=(bfd_byte) value;
1357 static void
1358 target_put_16 (void *p, rc_uint_type value)
1360 assert (!! p);
1362 if (target_is_bigendian)
1363 bfd_putb16 (value, p);
1364 else
1365 bfd_putl16 (value, p);
1368 static void
1369 target_put_32 (void *p, rc_uint_type value)
1371 assert (!! p);
1373 if (target_is_bigendian)
1374 bfd_putb32 (value, p);
1375 else
1376 bfd_putl32 (value, p);
1379 static int isInComment = 0;
1381 int wr_printcomment (FILE *e, const char *fmt, ...)
1383 va_list arg;
1384 int r = 0;
1386 if (isInComment)
1387 r += fprintf (e, "\n ");
1388 else
1389 fprintf (e, "/* ");
1390 isInComment = 1;
1391 if (fmt == NULL)
1392 return r;
1393 va_start (arg, fmt);
1394 r += vfprintf (e, fmt, arg);
1395 va_end (arg);
1396 return r;
1399 int wr_print (FILE *e, const char *fmt, ...)
1401 va_list arg;
1402 int r = 0;
1403 if (isInComment)
1404 r += fprintf (e, ". */\n");
1405 isInComment = 0;
1406 if (! fmt)
1407 return r;
1408 va_start (arg, fmt);
1409 r += vfprintf (e, fmt, arg);
1410 va_end (arg);
1411 return r;