Fix --add-gnu-debuglink
[binutils.git] / binutils / windres.c
blobd874fc0873f5beca241a116f11260ffe535030ad
1 /* windres.c -- a program to manipulate Windows resources
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
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 2 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., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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 "bfd.h"
38 #include "getopt.h"
39 #include "bucomm.h"
40 #include "libiberty.h"
41 #include "safe-ctype.h"
42 #include "obstack.h"
43 #include "windres.h"
44 #include <assert.h>
45 #include <time.h>
47 /* Used by resrc.c at least. */
49 int verbose = 0;
51 /* An enumeration of format types. */
53 enum res_format
55 /* Unknown format. */
56 RES_FORMAT_UNKNOWN,
57 /* Textual RC file. */
58 RES_FORMAT_RC,
59 /* Binary RES file. */
60 RES_FORMAT_RES,
61 /* COFF file. */
62 RES_FORMAT_COFF
65 /* A structure used to map between format types and strings. */
67 struct format_map
69 const char *name;
70 enum res_format format;
73 /* A mapping between names and format types. */
75 static const struct format_map format_names[] =
77 { "rc", RES_FORMAT_RC },
78 { "res", RES_FORMAT_RES },
79 { "coff", RES_FORMAT_COFF },
80 { NULL, RES_FORMAT_UNKNOWN }
83 /* A mapping from file extensions to format types. */
85 static const struct format_map format_fileexts[] =
87 { "rc", RES_FORMAT_RC },
88 { "res", RES_FORMAT_RES },
89 { "exe", RES_FORMAT_COFF },
90 { "obj", RES_FORMAT_COFF },
91 { "o", RES_FORMAT_COFF },
92 { NULL, RES_FORMAT_UNKNOWN }
95 /* A list of include directories. */
97 struct include_dir
99 struct include_dir *next;
100 char *dir;
103 static struct include_dir *include_dirs;
105 /* Static functions. */
107 static void res_init PARAMS ((void));
108 static int extended_menuitems PARAMS ((const struct menuitem *));
109 static enum res_format format_from_name PARAMS ((const char *, int));
110 static enum res_format format_from_filename PARAMS ((const char *, int));
111 static void usage PARAMS ((FILE *, int));
112 static int cmp_res_entry PARAMS ((const PTR, const PTR));
113 static struct res_directory *sort_resources PARAMS ((struct res_directory *));
114 static void reswr_init PARAMS ((void));
115 static const char * quot PARAMS ((const char *));
117 /* When we are building a resource tree, we allocate everything onto
118 an obstack, so that we can free it all at once if we want. */
120 #define obstack_chunk_alloc xmalloc
121 #define obstack_chunk_free free
123 /* The resource building obstack. */
125 static struct obstack res_obstack;
127 /* Initialize the resource building obstack. */
129 static void
130 res_init ()
132 obstack_init (&res_obstack);
135 /* Allocate space on the resource building obstack. */
138 res_alloc (bytes)
139 size_t bytes;
141 return (PTR) obstack_alloc (&res_obstack, bytes);
144 /* We also use an obstack to save memory used while writing out a set
145 of resources. */
147 static struct obstack reswr_obstack;
149 /* Initialize the resource writing obstack. */
151 static void
152 reswr_init ()
154 obstack_init (&reswr_obstack);
157 /* Allocate space on the resource writing obstack. */
160 reswr_alloc (bytes)
161 size_t bytes;
163 return (PTR) obstack_alloc (&reswr_obstack, bytes);
166 /* Open a file using the include directory search list. */
168 FILE *
169 open_file_search (filename, mode, errmsg, real_filename)
170 const char *filename;
171 const char *mode;
172 const char *errmsg;
173 char **real_filename;
175 FILE *e;
176 struct include_dir *d;
178 e = fopen (filename, mode);
179 if (e != NULL)
181 *real_filename = xstrdup (filename);
182 return e;
185 if (errno == ENOENT)
187 for (d = include_dirs; d != NULL; d = d->next)
189 char *n;
191 n = (char *) xmalloc (strlen (d->dir) + strlen (filename) + 2);
192 sprintf (n, "%s/%s", d->dir, filename);
193 e = fopen (n, mode);
194 if (e != NULL)
196 *real_filename = n;
197 return e;
200 if (errno != ENOENT)
201 break;
205 fatal (_("can't open %s `%s': %s"), errmsg, filename, strerror (errno));
207 /* Return a value to avoid a compiler warning. */
208 return NULL;
211 /* Compare two resource ID's. We consider name entries to come before
212 numeric entries, because that is how they appear in the COFF .rsrc
213 section. */
216 res_id_cmp (a, b)
217 struct res_id a;
218 struct res_id b;
220 if (! a.named)
222 if (b.named)
223 return 1;
224 if (a.u.id > b.u.id)
225 return 1;
226 else if (a.u.id < b.u.id)
227 return -1;
228 else
229 return 0;
231 else
233 unichar *as, *ase, *bs, *bse;
235 if (! b.named)
236 return -1;
238 as = a.u.n.name;
239 ase = as + a.u.n.length;
240 bs = b.u.n.name;
241 bse = bs + b.u.n.length;
243 while (as < ase)
245 int i;
247 if (bs >= bse)
248 return 1;
249 i = (int) *as - (int) *bs;
250 if (i != 0)
251 return i;
252 ++as;
253 ++bs;
256 if (bs < bse)
257 return -1;
259 return 0;
263 /* Print a resource ID. */
265 void
266 res_id_print (stream, id, quote)
267 FILE *stream;
268 struct res_id id;
269 int quote;
271 if (! id.named)
272 fprintf (stream, "%lu", id.u.id);
273 else
275 if (quote)
276 putc ('"', stream);
277 unicode_print (stream, id.u.n.name, id.u.n.length);
278 if (quote)
279 putc ('"', stream);
283 /* Print a list of resource ID's. */
285 void
286 res_ids_print (stream, cids, ids)
287 FILE *stream;
288 int cids;
289 const struct 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 (res_id, string)
305 struct res_id *res_id;
306 const char *string;
308 res_id->named = 1;
309 unicode_from_ascii (&res_id->u.n.length, &res_id->u.n.name, string);
312 /* Define a resource. The arguments are the resource tree, RESOURCES,
313 and the location at which to put it in the tree, CIDS and IDS.
314 This returns a newly allocated res_resource structure, which the
315 caller is expected to initialize. If DUPOK is non-zero, then if a
316 resource with this ID exists, it is returned. Otherwise, a warning
317 is issued, and a new resource is created replacing the existing
318 one. */
320 struct res_resource *
321 define_resource (resources, cids, ids, dupok)
322 struct res_directory **resources;
323 int cids;
324 const struct res_id *ids;
325 int dupok;
327 struct res_entry *re = NULL;
328 int i;
330 assert (cids > 0);
331 for (i = 0; i < cids; i++)
333 struct res_entry **pp;
335 if (*resources == NULL)
337 static unsigned long timeval;
339 /* Use the same timestamp for every resource created in a
340 single run. */
341 if (timeval == 0)
342 timeval = time (NULL);
344 *resources = ((struct res_directory *)
345 res_alloc (sizeof **resources));
346 (*resources)->characteristics = 0;
347 (*resources)->time = timeval;
348 (*resources)->major = 0;
349 (*resources)->minor = 0;
350 (*resources)->entries = NULL;
353 for (pp = &(*resources)->entries; *pp != NULL; pp = &(*pp)->next)
354 if (res_id_cmp ((*pp)->id, ids[i]) == 0)
355 break;
357 if (*pp != NULL)
358 re = *pp;
359 else
361 re = (struct res_entry *) res_alloc (sizeof *re);
362 re->next = NULL;
363 re->id = ids[i];
364 if ((i + 1) < cids)
366 re->subdir = 1;
367 re->u.dir = NULL;
369 else
371 re->subdir = 0;
372 re->u.res = NULL;
375 *pp = re;
378 if ((i + 1) < cids)
380 if (! re->subdir)
382 fprintf (stderr, "%s: ", program_name);
383 res_ids_print (stderr, i, ids);
384 fprintf (stderr, _(": expected to be a directory\n"));
385 xexit (1);
388 resources = &re->u.dir;
392 if (re->subdir)
394 fprintf (stderr, "%s: ", program_name);
395 res_ids_print (stderr, cids, ids);
396 fprintf (stderr, _(": expected to be a leaf\n"));
397 xexit (1);
400 if (re->u.res != NULL)
402 if (dupok)
403 return re->u.res;
405 fprintf (stderr, _("%s: warning: "), program_name);
406 res_ids_print (stderr, cids, ids);
407 fprintf (stderr, _(": duplicate value\n"));
410 re->u.res = ((struct res_resource *)
411 res_alloc (sizeof (struct res_resource)));
412 memset (re->u.res, 0, sizeof (struct res_resource));
414 re->u.res->type = RES_TYPE_UNINITIALIZED;
415 return re->u.res;
418 /* Define a standard resource. This is a version of define_resource
419 that just takes type, name, and language arguments. */
421 struct res_resource *
422 define_standard_resource (resources, type, name, language, dupok)
423 struct res_directory **resources;
424 int type;
425 struct res_id name;
426 int language;
427 int dupok;
429 struct res_id a[3];
431 a[0].named = 0;
432 a[0].u.id = type;
433 a[1] = name;
434 a[2].named = 0;
435 a[2].u.id = language;
436 return define_resource (resources, 3, a, dupok);
439 /* Comparison routine for resource sorting. */
441 static int
442 cmp_res_entry (p1, p2)
443 const PTR p1;
444 const PTR p2;
446 const struct res_entry **re1, **re2;
448 re1 = (const struct res_entry **) p1;
449 re2 = (const struct res_entry **) p2;
450 return res_id_cmp ((*re1)->id, (*re2)->id);
453 /* Sort the resources. */
455 static struct res_directory *
456 sort_resources (resdir)
457 struct res_directory *resdir;
459 int c, i;
460 struct res_entry *re;
461 struct res_entry **a;
463 if (resdir->entries == NULL)
464 return resdir;
466 c = 0;
467 for (re = resdir->entries; re != NULL; re = re->next)
468 ++c;
470 /* This is a recursive routine, so using xmalloc is probably better
471 than alloca. */
472 a = (struct res_entry **) xmalloc (c * sizeof (struct res_entry *));
474 for (i = 0, re = resdir->entries; re != NULL; re = re->next, i++)
475 a[i] = re;
477 qsort (a, c, sizeof (struct res_entry *), cmp_res_entry);
479 resdir->entries = a[0];
480 for (i = 0; i < c - 1; i++)
481 a[i]->next = a[i + 1];
482 a[i]->next = NULL;
484 free (a);
486 /* Now sort the subdirectories. */
488 for (re = resdir->entries; re != NULL; re = re->next)
489 if (re->subdir)
490 re->u.dir = sort_resources (re->u.dir);
492 return resdir;
495 /* Return whether the dialog resource DIALOG is a DIALOG or a
496 DIALOGEX. */
499 extended_dialog (dialog)
500 const struct dialog *dialog;
502 const struct dialog_control *c;
504 if (dialog->ex != NULL)
505 return 1;
507 for (c = dialog->controls; c != NULL; c = c->next)
508 if (c->data != NULL || c->help != 0)
509 return 1;
511 return 0;
514 /* Return whether MENUITEMS are a MENU or a MENUEX. */
517 extended_menu (menu)
518 const struct menu *menu;
520 return extended_menuitems (menu->items);
523 static int
524 extended_menuitems (menuitems)
525 const struct menuitem *menuitems;
527 const struct menuitem *mi;
529 for (mi = menuitems; mi != NULL; mi = mi->next)
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)
546 if (extended_menuitems (mi->popup))
547 return 1;
551 return 0;
554 /* Convert a string to a format type, or exit if it can't be done. */
556 static enum res_format
557 format_from_name (name, exit_on_error)
558 const char *name;
559 int exit_on_error;
561 const struct format_map *m;
563 for (m = format_names; m->name != NULL; m++)
564 if (strcasecmp (m->name, name) == 0)
565 break;
567 if (m->name == NULL && exit_on_error)
569 non_fatal (_("unknown format type `%s'"), name);
570 fprintf (stderr, _("%s: supported formats:"), program_name);
571 for (m = format_names; m->name != NULL; m++)
572 fprintf (stderr, " %s", m->name);
573 fprintf (stderr, "\n");
574 xexit (1);
577 return m->format;
580 /* Work out a format type given a file name. If INPUT is non-zero,
581 it's OK to look at the file itself. */
583 static enum res_format
584 format_from_filename (filename, input)
585 const char *filename;
586 int input;
588 const char *ext;
589 FILE *e;
590 unsigned char b1, b2, b3, b4, b5;
591 int magic;
593 /* If we have an extension, see if we recognize it as implying a
594 particular format. */
595 ext = strrchr (filename, '.');
596 if (ext != NULL)
598 const struct format_map *m;
600 ++ext;
601 for (m = format_fileexts; m->name != NULL; m++)
602 if (strcasecmp (m->name, ext) == 0)
603 return m->format;
606 /* If we don't recognize the name of an output file, assume it's a
607 COFF file. */
608 if (! input)
609 return RES_FORMAT_COFF;
611 /* Read the first few bytes of the file to see if we can guess what
612 it is. */
613 e = fopen (filename, FOPEN_RB);
614 if (e == NULL)
615 fatal ("%s: %s", filename, strerror (errno));
617 b1 = getc (e);
618 b2 = getc (e);
619 b3 = getc (e);
620 b4 = getc (e);
621 b5 = getc (e);
623 fclose (e);
625 /* A PE executable starts with 0x4d 0x5a. */
626 if (b1 == 0x4d && b2 == 0x5a)
627 return RES_FORMAT_COFF;
629 /* A COFF .o file starts with a COFF magic number. */
630 magic = (b2 << 8) | b1;
631 switch (magic)
633 case 0x14c: /* i386 */
634 case 0x166: /* MIPS */
635 case 0x184: /* Alpha */
636 case 0x268: /* 68k */
637 case 0x1f0: /* PowerPC */
638 case 0x290: /* PA */
639 return RES_FORMAT_COFF;
642 /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
643 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0 && b5 == 0x20)
644 return RES_FORMAT_RES;
646 /* If every character is printable or space, assume it's an RC file. */
647 if ((ISPRINT (b1) || ISSPACE (b1))
648 && (ISPRINT (b2) || ISSPACE (b2))
649 && (ISPRINT (b3) || ISSPACE (b3))
650 && (ISPRINT (b4) || ISSPACE (b4))
651 && (ISPRINT (b5) || ISSPACE (b5)))
652 return RES_FORMAT_RC;
654 /* Otherwise, we give up. */
655 fatal (_("can not determine type of file `%s'; use the -I option"),
656 filename);
658 /* Return something to silence the compiler warning. */
659 return RES_FORMAT_UNKNOWN;
662 /* Print a usage message and exit. */
664 static void
665 usage (stream, status)
666 FILE *stream;
667 int status;
669 fprintf (stream, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
670 program_name);
671 fprintf (stream, _(" The options are:\n\
672 -i --input=<file> Name input file\n\
673 -o --output=<file> Name output file\n\
674 -J --input-format=<format> Specify input format\n\
675 -O --output-format=<format> Specify output format\n\
676 -F --target=<target> Specify COFF target\n\
677 --preprocessor=<program> Program to use to preprocess rc file\n\
678 -I --include-dir=<dir> Include directory when preprocessing rc file\n\
679 -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\
680 -U --undefine <sym> Undefine SYM when preprocessing rc file\n\
681 -v --verbose Verbose - tells you what it's doing\n\
682 -l --language=<val> Set language when reading rc file\n\
683 --use-temp-file Use a temporary file instead of popen to read\n\
684 the preprocessor output\n\
685 --no-use-temp-file Use popen (default)\n"));
686 #ifdef YYDEBUG
687 fprintf (stream, _("\
688 --yydebug Turn on parser debugging\n"));
689 #endif
690 fprintf (stream, _("\
691 -r Ignored for compatibility with rc\n\
692 -h --help Print this help message\n\
693 -V --version Print version information\n"));
694 fprintf (stream, _("\
695 FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
696 extension if not specified. A single file name is an input file.\n\
697 No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
699 list_supported_targets (program_name, stream);
701 if (status == 0)
702 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
704 exit (status);
707 /* Quote characters that will confuse the shell when we run the preprocessor. */
709 static const char *
710 quot (string)
711 const char *string;
713 static char *buf = 0;
714 static int buflen = 0;
715 int slen = strlen (string);
716 const char *src;
717 char *dest;
719 if ((buflen < slen * 2 + 2) || !buf)
721 buflen = slen * 2 + 2;
722 if (buf)
723 free (buf);
724 buf = (char *) xmalloc (buflen);
727 for (src=string, dest=buf; *src; src++, dest++)
729 if (*src == '(' || *src == ')' || *src == ' ')
730 *dest++ = '\\';
731 *dest = *src;
733 *dest = 0;
734 return buf;
737 /* Long options. */
739 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
741 #define OPTION_PREPROCESSOR 150
742 #define OPTION_USE_TEMP_FILE (OPTION_PREPROCESSOR + 1)
743 #define OPTION_NO_USE_TEMP_FILE (OPTION_USE_TEMP_FILE + 1)
744 #define OPTION_YYDEBUG (OPTION_NO_USE_TEMP_FILE + 1)
746 static const struct option long_options[] =
748 {"input", required_argument, 0, 'i'},
749 {"output", required_argument, 0, 'o'},
750 {"input-format", required_argument, 0, 'J'},
751 {"output-format", required_argument, 0, 'O'},
752 {"target", required_argument, 0, 'F'},
753 {"preprocessor", required_argument, 0, OPTION_PREPROCESSOR},
754 {"include-dir", required_argument, 0, 'I'},
755 {"define", required_argument, 0, 'D'},
756 {"undefine", required_argument, 0, 'U'},
757 {"verbose", no_argument, 0, 'v'},
758 {"language", required_argument, 0, 'l'},
759 {"use-temp-file", no_argument, 0, OPTION_USE_TEMP_FILE},
760 {"no-use-temp-file", no_argument, 0, OPTION_NO_USE_TEMP_FILE},
761 {"yydebug", no_argument, 0, OPTION_YYDEBUG},
762 {"version", no_argument, 0, 'V'},
763 {"help", no_argument, 0, 'h'},
764 {0, no_argument, 0, 0}
767 /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */
768 int main PARAMS ((int, char **));
770 /* The main function. */
773 main (argc, argv)
774 int argc;
775 char **argv;
777 int c;
778 char *input_filename;
779 char *output_filename;
780 enum res_format input_format;
781 enum res_format input_format_tmp;
782 enum res_format output_format;
783 char *target;
784 char *preprocessor;
785 char *preprocargs;
786 const char *quotedarg;
787 int language;
788 struct res_directory *resources;
789 int use_temp_file;
791 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
792 setlocale (LC_MESSAGES, "");
793 #endif
794 #if defined (HAVE_SETLOCALE)
795 setlocale (LC_CTYPE, "");
796 #endif
797 bindtextdomain (PACKAGE, LOCALEDIR);
798 textdomain (PACKAGE);
800 program_name = argv[0];
801 xmalloc_set_program_name (program_name);
803 bfd_init ();
804 set_default_bfd_target ();
806 res_init ();
808 input_filename = NULL;
809 output_filename = NULL;
810 input_format = RES_FORMAT_UNKNOWN;
811 output_format = RES_FORMAT_UNKNOWN;
812 target = NULL;
813 preprocessor = NULL;
814 preprocargs = NULL;
815 language = 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
816 use_temp_file = 0;
818 while ((c = getopt_long (argc, argv, "f:i:l:o:I:J:O:F:D:U:rhHvV", long_options,
819 (int *) 0)) != EOF)
821 switch (c)
823 case 'i':
824 input_filename = optarg;
825 break;
827 case 'f':
828 /* For compatability with rc we accept "-fo <name>" as being the
829 equivalent of "-o <name>". We do not advertise this fact
830 though, as we do not want users to use non-GNU like command
831 line switches. */
832 if (*optarg != 'o')
833 fatal (_("invalid option -f\n"));
834 optarg++;
835 if (* optarg == 0)
837 if (optind == argc)
838 fatal (_("No filename following the -fo option.\n"));
839 optarg = argv [optind++];
841 /* Fall through. */
843 case 'o':
844 output_filename = optarg;
845 break;
847 case 'J':
848 input_format = format_from_name (optarg, 1);
849 break;
851 case 'O':
852 output_format = format_from_name (optarg, 1);
853 break;
855 case 'F':
856 target = optarg;
857 break;
859 case OPTION_PREPROCESSOR:
860 preprocessor = optarg;
861 break;
863 case 'D':
864 case 'U':
865 if (preprocargs == NULL)
867 quotedarg = quot (optarg);
868 preprocargs = xmalloc (strlen (quotedarg) + 3);
869 sprintf (preprocargs, "-%c%s", c, quotedarg);
871 else
873 char *n;
875 quotedarg = quot (optarg);
876 n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
877 sprintf (n, "%s -%c%s", preprocargs, c, quotedarg);
878 free (preprocargs);
879 preprocargs = n;
881 break;
883 case 'r':
884 /* Ignored for compatibility with rc. */
885 break;
887 case 'v':
888 verbose ++;
889 break;
891 case 'I':
892 /* For backward compatibility, should be removed in the future. */
893 input_format_tmp = format_from_name (optarg, 0);
894 if (input_format_tmp != RES_FORMAT_UNKNOWN)
896 fprintf (stderr, _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
897 input_format = input_format_tmp;
898 break;
901 if (preprocargs == NULL)
903 quotedarg = quot (optarg);
904 preprocargs = xmalloc (strlen (quotedarg) + 3);
905 sprintf (preprocargs, "-I%s", quotedarg);
907 else
909 char *n;
911 quotedarg = quot (optarg);
912 n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
913 sprintf (n, "%s -I%s", preprocargs, quotedarg);
914 free (preprocargs);
915 preprocargs = n;
919 struct include_dir *n, **pp;
921 n = (struct include_dir *) xmalloc (sizeof *n);
922 n->next = NULL;
923 n->dir = optarg;
925 for (pp = &include_dirs; *pp != NULL; pp = &(*pp)->next)
927 *pp = n;
930 break;
932 case 'l':
933 language = strtol (optarg, (char **) NULL, 16);
934 break;
936 case OPTION_USE_TEMP_FILE:
937 use_temp_file = 1;
938 break;
940 case OPTION_NO_USE_TEMP_FILE:
941 use_temp_file = 0;
942 break;
944 #ifdef YYDEBUG
945 case OPTION_YYDEBUG:
946 yydebug = 1;
947 break;
948 #endif
950 case 'h':
951 case 'H':
952 usage (stdout, 0);
953 break;
955 case 'V':
956 print_version ("windres");
957 break;
959 default:
960 usage (stderr, 1);
961 break;
965 if (input_filename == NULL && optind < argc)
967 input_filename = argv[optind];
968 ++optind;
971 if (output_filename == NULL && optind < argc)
973 output_filename = argv[optind];
974 ++optind;
977 if (argc != optind)
978 usage (stderr, 1);
980 if (input_format == RES_FORMAT_UNKNOWN)
982 if (input_filename == NULL)
983 input_format = RES_FORMAT_RC;
984 else
985 input_format = format_from_filename (input_filename, 1);
988 if (output_format == RES_FORMAT_UNKNOWN)
990 if (output_filename == NULL)
991 output_format = RES_FORMAT_RC;
992 else
993 output_format = format_from_filename (output_filename, 0);
996 /* Read the input file. */
997 switch (input_format)
999 default:
1000 abort ();
1001 case RES_FORMAT_RC:
1002 resources = read_rc_file (input_filename, preprocessor, preprocargs,
1003 language, use_temp_file);
1004 break;
1005 case RES_FORMAT_RES:
1006 resources = read_res_file (input_filename);
1007 break;
1008 case RES_FORMAT_COFF:
1009 resources = read_coff_rsrc (input_filename, target);
1010 break;
1013 if (resources == NULL)
1014 fatal (_("no resources"));
1016 /* Sort the resources. This is required for COFF, convenient for
1017 rc, and unimportant for res. */
1018 resources = sort_resources (resources);
1020 /* Write the output file. */
1021 reswr_init ();
1023 switch (output_format)
1025 default:
1026 abort ();
1027 case RES_FORMAT_RC:
1028 write_rc_file (output_filename, resources);
1029 break;
1030 case RES_FORMAT_RES:
1031 write_res_file (output_filename, resources);
1032 break;
1033 case RES_FORMAT_COFF:
1034 write_coff_file (output_filename, target, resources);
1035 break;
1038 xexit (0);
1039 return 0;