1 /* windres.c -- a program to manipulate Windows resources
2 Copyright 1997, 1998, 1999 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program 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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 /* This program can read and write Windows resources in various
23 formats. In particular, it can act like the rc resource compiler
24 program, and it can act like the cvtres res to COFF conversion
27 It is based on information taken from the following sources:
29 * Microsoft documentation.
31 * The rcl program, written by Gunther Ebert
32 <gunther.ebert@ixos-leipzig.de>.
34 * The res2coff program, written by Pedro A. Aranda <paag@tid.es>.
41 #include "libiberty.h"
49 /* An enumeration of format types. */
55 /* Textual RC file. */
57 /* Binary RES file. */
63 /* A structure used to map between format types and strings. */
68 enum res_format format;
71 /* A mapping between names and format types. */
73 static const struct format_map format_names[] =
75 { "rc", RES_FORMAT_RC },
76 { "res", RES_FORMAT_RES },
77 { "coff", RES_FORMAT_COFF },
78 { NULL, RES_FORMAT_UNKNOWN }
81 /* A mapping from file extensions to format types. */
83 static const struct format_map format_fileexts[] =
85 { "rc", RES_FORMAT_RC },
86 { "res", RES_FORMAT_RES },
87 { "exe", RES_FORMAT_COFF },
88 { "obj", RES_FORMAT_COFF },
89 { "o", RES_FORMAT_COFF },
90 { NULL, RES_FORMAT_UNKNOWN }
93 /* A list of include directories. */
97 struct include_dir *next;
101 static struct include_dir *include_dirs;
105 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
107 #define OPTION_DEFINE 150
108 #define OPTION_HELP (OPTION_DEFINE + 1)
109 #define OPTION_INCLUDE_DIR (OPTION_HELP + 1)
110 #define OPTION_LANGUAGE (OPTION_INCLUDE_DIR + 1)
111 #define OPTION_PREPROCESSOR (OPTION_LANGUAGE + 1)
112 #define OPTION_VERSION (OPTION_PREPROCESSOR + 1)
113 #define OPTION_YYDEBUG (OPTION_VERSION + 1)
115 static const struct option long_options[] =
117 {"define", required_argument, 0, OPTION_DEFINE},
118 {"help", no_argument, 0, OPTION_HELP},
119 {"include-dir", required_argument, 0, OPTION_INCLUDE_DIR},
120 {"input-format", required_argument, 0, 'I'},
121 {"language", required_argument, 0, OPTION_LANGUAGE},
122 {"output-format", required_argument, 0, 'O'},
123 {"preprocessor", required_argument, 0, OPTION_PREPROCESSOR},
124 {"target", required_argument, 0, 'F'},
125 {"version", no_argument, 0, OPTION_VERSION},
126 {"yydebug", no_argument, 0, OPTION_YYDEBUG},
127 {0, no_argument, 0, 0}
130 /* Static functions. */
132 static void res_init PARAMS ((void));
133 static int extended_menuitems PARAMS ((const struct menuitem *));
134 static enum res_format format_from_name PARAMS ((const char *));
135 static enum res_format format_from_filename PARAMS ((const char *, int));
136 static void usage PARAMS ((FILE *, int));
137 static int cmp_res_entry PARAMS ((const PTR, const PTR));
138 static struct res_directory *sort_resources PARAMS ((struct res_directory *));
140 /* When we are building a resource tree, we allocate everything onto
141 an obstack, so that we can free it all at once if we want. */
143 #define obstack_chunk_alloc xmalloc
144 #define obstack_chunk_free free
146 /* The resource building obstack. */
148 static struct obstack res_obstack;
150 /* Initialize the resource building obstack. */
155 obstack_init (&res_obstack);
158 /* Allocate space on the resource building obstack. */
164 return (PTR) obstack_alloc (&res_obstack, bytes);
167 /* We also use an obstack to save memory used while writing out a set
170 static struct obstack reswr_obstack;
172 /* Initialize the resource writing obstack. */
177 obstack_init (&reswr_obstack);
180 /* Allocate space on the resource writing obstack. */
186 return (PTR) obstack_alloc (&reswr_obstack, bytes);
189 /* Open a file using the include directory search list. */
192 open_file_search (filename, mode, errmsg, real_filename)
193 const char *filename;
196 char **real_filename;
199 struct include_dir *d;
201 e = fopen (filename, mode);
204 *real_filename = xstrdup (filename);
210 for (d = include_dirs; d != NULL; d = d->next)
214 n = (char *) xmalloc (strlen (d->dir) + strlen (filename) + 2);
215 sprintf (n, "%s/%s", d->dir, filename);
228 fatal (_("can't open %s `%s': %s"), errmsg, filename, strerror (errno));
230 /* Return a value to avoid a compiler warning. */
234 /* Compare two resource ID's. We consider name entries to come before
235 numeric entries, because that is how they appear in the COFF .rsrc
249 else if (a.u.id < b.u.id)
256 unichar *as, *ase, *bs, *bse;
262 ase = as + a.u.n.length;
264 bse = bs + b.u.n.length;
272 i = (int) *as - (int) *bs;
286 /* Print a resource ID. */
289 res_id_print (stream, id, quote)
295 fprintf (stream, "%lu", id.u.id);
300 unicode_print (stream, id.u.n.name, id.u.n.length);
306 /* Print a list of resource ID's. */
309 res_ids_print (stream, cids, ids)
312 const struct res_id *ids;
316 for (i = 0; i < cids; i++)
318 res_id_print (stream, ids[i], 1);
320 fprintf (stream, ": ");
324 /* Convert an ASCII string to a resource ID. */
327 res_string_to_id (res_id, string)
328 struct res_id *res_id;
332 unicode_from_ascii (&res_id->u.n.length, &res_id->u.n.name, string);
335 /* Define a resource. The arguments are the resource tree, RESOURCES,
336 and the location at which to put it in the tree, CIDS and IDS.
337 This returns a newly allocated res_resource structure, which the
338 caller is expected to initialize. If DUPOK is non-zero, then if a
339 resource with this ID exists, it is returned. Otherwise, a warning
340 is issued, and a new resource is created replacing the existing
343 struct res_resource *
344 define_resource (resources, cids, ids, dupok)
345 struct res_directory **resources;
347 const struct res_id *ids;
350 struct res_entry *re = NULL;
354 for (i = 0; i < cids; i++)
356 struct res_entry **pp;
358 if (*resources == NULL)
360 static unsigned long timeval;
362 /* Use the same timestamp for every resource created in a
365 timeval = time (NULL);
367 *resources = ((struct res_directory *)
368 res_alloc (sizeof **resources));
369 (*resources)->characteristics = 0;
370 (*resources)->time = timeval;
371 (*resources)->major = 0;
372 (*resources)->minor = 0;
373 (*resources)->entries = NULL;
376 for (pp = &(*resources)->entries; *pp != NULL; pp = &(*pp)->next)
377 if (res_id_cmp ((*pp)->id, ids[i]) == 0)
384 re = (struct res_entry *) res_alloc (sizeof *re);
405 fprintf (stderr, "%s: ", program_name);
406 res_ids_print (stderr, i, ids);
407 fprintf (stderr, _(": expected to be a directory\n"));
411 resources = &re->u.dir;
417 fprintf (stderr, "%s: ", program_name);
418 res_ids_print (stderr, cids, ids);
419 fprintf (stderr, _(": expected to be a leaf\n"));
423 if (re->u.res != NULL)
428 fprintf (stderr, _("%s: warning: "), program_name);
429 res_ids_print (stderr, cids, ids);
430 fprintf (stderr, _(": duplicate value\n"));
433 re->u.res = ((struct res_resource *)
434 res_alloc (sizeof (struct res_resource)));
436 re->u.res->type = RES_TYPE_UNINITIALIZED;
437 memset (&re->u.res->res_info, 0, sizeof (struct res_res_info));
438 memset (&re->u.res->coff_info, 0, sizeof (struct res_coff_info));
443 /* Define a standard resource. This is a version of define_resource
444 that just takes type, name, and language arguments. */
446 struct res_resource *
447 define_standard_resource (resources, type, name, language, dupok)
448 struct res_directory **resources;
460 a[2].u.id = language;
461 return define_resource (resources, 3, a, dupok);
464 /* Comparison routine for resource sorting. */
467 cmp_res_entry (p1, p2)
471 const struct res_entry **re1, **re2;
473 re1 = (const struct res_entry **) p1;
474 re2 = (const struct res_entry **) p2;
475 return res_id_cmp ((*re1)->id, (*re2)->id);
478 /* Sort the resources. */
480 static struct res_directory *
481 sort_resources (resdir)
482 struct res_directory *resdir;
485 struct res_entry *re;
486 struct res_entry **a;
488 if (resdir->entries == NULL)
492 for (re = resdir->entries; re != NULL; re = re->next)
495 /* This is a recursive routine, so using xmalloc is probably better
497 a = (struct res_entry **) xmalloc (c * sizeof (struct res_entry *));
499 for (i = 0, re = resdir->entries; re != NULL; re = re->next, i++)
502 qsort (a, c, sizeof (struct res_entry *), cmp_res_entry);
504 resdir->entries = a[0];
505 for (i = 0; i < c - 1; i++)
506 a[i]->next = a[i + 1];
511 /* Now sort the subdirectories. */
513 for (re = resdir->entries; re != NULL; re = re->next)
515 re->u.dir = sort_resources (re->u.dir);
520 /* Return whether the dialog resource DIALOG is a DIALOG or a
524 extended_dialog (dialog)
525 const struct dialog *dialog;
527 const struct dialog_control *c;
529 if (dialog->ex != NULL)
532 for (c = dialog->controls; c != NULL; c = c->next)
533 if (c->data != NULL || c->help != 0)
539 /* Return whether MENUITEMS are a MENU or a MENUEX. */
543 const struct menu *menu;
545 return extended_menuitems (menu->items);
549 extended_menuitems (menuitems)
550 const struct menuitem *menuitems;
552 const struct menuitem *mi;
554 for (mi = menuitems; mi != NULL; mi = mi->next)
556 if (mi->help != 0 || mi->state != 0)
558 if (mi->popup != NULL && mi->id != 0)
561 & ~ (MENUITEM_CHECKED
565 | MENUITEM_MENUBARBREAK
566 | MENUITEM_MENUBREAK))
569 if (mi->popup != NULL)
571 if (extended_menuitems (mi->popup))
579 /* Convert a string to a format type, or exit if it can't be done. */
581 static enum res_format
582 format_from_name (name)
585 const struct format_map *m;
587 for (m = format_names; m->name != NULL; m++)
588 if (strcasecmp (m->name, name) == 0)
593 fprintf (stderr, _("%s: unknown format type `%s'\n"), program_name, name);
594 fprintf (stderr, _("%s: supported formats:"), program_name);
595 for (m = format_names; m->name != NULL; m++)
596 fprintf (stderr, " %s", m->name);
597 fprintf (stderr, "\n");
604 /* Work out a format type given a file name. If INPUT is non-zero,
605 it's OK to look at the file itself. */
607 static enum res_format
608 format_from_filename (filename, input)
609 const char *filename;
614 unsigned char b1, b2, b3, b4, b5;
617 /* If we have an extension, see if we recognize it as implying a
618 particular format. */
619 ext = strrchr (filename, '.');
622 const struct format_map *m;
625 for (m = format_fileexts; m->name != NULL; m++)
626 if (strcasecmp (m->name, ext) == 0)
630 /* If we don't recognize the name of an output file, assume it's a
634 return RES_FORMAT_COFF;
636 /* Read the first few bytes of the file to see if we can guess what
639 e = fopen (filename, FOPEN_RB);
641 fatal ("%s: %s", filename, strerror (errno));
651 /* A PE executable starts with 0x4d 0x5a. */
652 if (b1 == 0x4d && b2 == 0x5a)
653 return RES_FORMAT_COFF;
655 /* A COFF .o file starts with a COFF magic number. */
656 magic = (b2 << 8) | b1;
659 case 0x14c: /* i386 */
660 case 0x166: /* MIPS */
661 case 0x184: /* Alpha */
662 case 0x268: /* 68k */
663 case 0x1f0: /* PowerPC */
665 return RES_FORMAT_COFF;
668 /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
669 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0 && b5 == 0x20)
670 return RES_FORMAT_RES;
672 /* If every character is printable or space, assume it's an RC file. */
673 if ((isprint (b1) || isspace (b1))
674 && (isprint (b2) || isspace (b2))
675 && (isprint (b3) || isspace (b3))
676 && (isprint (b4) || isspace (b4))
677 && (isprint (b5) || isspace (b5)))
678 return RES_FORMAT_RC;
680 /* Otherwise, we give up. */
681 fatal (_("can not determine type of file `%s'; use the -I option"),
684 /* Return something to silence the compiler warning. */
685 return RES_FORMAT_UNKNOWN;
688 /* Print a usage message and exit. */
691 usage (stream, status)
695 fprintf (stream, _("Usage: %s [options] [input-file] [output-file]\n"),
697 fprintf (stream, _("\
699 -i FILE, --input FILE Name input file\n\
700 -o FILE, --output FILE Name output file\n\
701 -I FORMAT, --input-format FORMAT\n\
702 Specify input format\n\
703 -O FORMAT, --output-format FORMAT\n\
704 Specify output format\n\
705 -F TARGET, --target TARGET Specify COFF target\n\
706 --preprocessor PROGRAM Program to use to preprocess rc file\n\
707 --include-dir DIR Include directory when preprocessing rc file\n\
708 --define SYM[=VAL] Define SYM when preprocessing rc file\n\
709 --language VAL Set language when reading rc file\n"));
711 fprintf (stream, _("\
712 --yydebug Turn on parser debugging\n"));
714 fprintf (stream, _("\
715 --help Print this help message\n\
716 --version Print version information\n"));
717 fprintf (stream, _("\
718 FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
719 extension if not specified. A single file name is an input file.\n\
720 No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
721 list_supported_targets (program_name, stream);
723 fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n"));
727 /* Quote characters that will confuse the shell when we run the preprocessor */
728 static const char *quot (string)
731 static char *buf = 0;
732 static int buflen = 0;
733 int slen = strlen (string);
737 if ((buflen < slen * 2 + 2) || !buf)
739 buflen = slen * 2 + 2;
742 buf = (char *) xmalloc (buflen);
745 for (src=string, dest=buf; *src; src++, dest++)
747 if (*src == '(' || *src == ')' || *src == ' ')
755 /* The main function. */
763 char *input_filename;
764 char *output_filename;
765 enum res_format input_format;
766 enum res_format output_format;
770 const char *quotedarg;
772 struct res_directory *resources;
774 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
775 setlocale (LC_MESSAGES, "");
777 bindtextdomain (PACKAGE, LOCALEDIR);
778 textdomain (PACKAGE);
780 program_name = argv[0];
781 xmalloc_set_program_name (program_name);
784 set_default_bfd_target ();
788 input_filename = NULL;
789 output_filename = NULL;
790 input_format = RES_FORMAT_UNKNOWN;
791 output_format = RES_FORMAT_UNKNOWN;
797 while ((c = getopt_long (argc, argv, "i:o:I:O:F:D:", long_options,
803 input_filename = optarg;
807 output_filename = optarg;
811 input_format = format_from_name (optarg);
815 output_format = format_from_name (optarg);
822 case OPTION_PREPROCESSOR:
823 preprocessor = optarg;
828 if (preprocargs == NULL)
830 quotedarg = quot (optarg);
831 preprocargs = xmalloc (strlen (quotedarg) + 3);
832 sprintf (preprocargs, "-D%s", quotedarg);
838 quotedarg = quot (optarg);
839 n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
840 sprintf (n, "%s -D%s", preprocargs, quotedarg);
846 case OPTION_INCLUDE_DIR:
847 if (preprocargs == NULL)
849 quotedarg = quot (optarg);
850 preprocargs = xmalloc (strlen (quotedarg) + 3);
851 sprintf (preprocargs, "-I%s", quotedarg);
857 quotedarg = quot (optarg);
858 n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
859 sprintf (n, "%s -I%s", preprocargs, quotedarg);
865 struct include_dir *n, **pp;
867 n = (struct include_dir *) xmalloc (sizeof *n);
871 for (pp = &include_dirs; *pp != NULL; pp = &(*pp)->next)
878 case OPTION_LANGUAGE:
879 language = strtol (optarg, (char **) NULL, 16);
893 print_version ("windres");
902 if (input_filename == NULL && optind < argc)
904 input_filename = argv[optind];
908 if (output_filename == NULL && optind < argc)
910 output_filename = argv[optind];
917 if (input_format == RES_FORMAT_UNKNOWN)
919 if (input_filename == NULL)
920 input_format = RES_FORMAT_RC;
922 input_format = format_from_filename (input_filename, 1);
925 if (output_format == RES_FORMAT_UNKNOWN)
927 if (output_filename == NULL)
928 output_format = RES_FORMAT_RC;
930 output_format = format_from_filename (output_filename, 0);
933 /* Read the input file. */
935 switch (input_format)
940 resources = read_rc_file (input_filename, preprocessor, preprocargs,
944 resources = read_res_file (input_filename);
946 case RES_FORMAT_COFF:
947 resources = read_coff_rsrc (input_filename, target);
951 if (resources == NULL)
952 fatal (_("no resources"));
954 /* Sort the resources. This is required for COFF, convenient for
955 rc, and unimportant for res. */
957 resources = sort_resources (resources);
959 /* Write the output file. */
963 switch (output_format)
968 write_rc_file (output_filename, resources);
971 write_res_file (output_filename, resources);
973 case RES_FORMAT_COFF:
974 write_coff_file (output_filename, target, resources);