xref: /openbsd-src/gnu/usr.bin/binutils-2.17/binutils/windres.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* windres.c -- a program to manipulate Windows resources
2*3d8817e4Smiod    Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3*3d8817e4Smiod    Free Software Foundation, Inc.
4*3d8817e4Smiod    Written by Ian Lance Taylor, Cygnus Support.
5*3d8817e4Smiod 
6*3d8817e4Smiod    This file is part of GNU Binutils.
7*3d8817e4Smiod 
8*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
9*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
10*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
11*3d8817e4Smiod    (at your option) any later version.
12*3d8817e4Smiod 
13*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
14*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*3d8817e4Smiod    GNU General Public License for more details.
17*3d8817e4Smiod 
18*3d8817e4Smiod    You should have received a copy of the GNU General Public License
19*3d8817e4Smiod    along with this program; if not, write to the Free Software
20*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21*3d8817e4Smiod    02110-1301, USA.  */
22*3d8817e4Smiod 
23*3d8817e4Smiod /* This program can read and write Windows resources in various
24*3d8817e4Smiod    formats.  In particular, it can act like the rc resource compiler
25*3d8817e4Smiod    program, and it can act like the cvtres res to COFF conversion
26*3d8817e4Smiod    program.
27*3d8817e4Smiod 
28*3d8817e4Smiod    It is based on information taken from the following sources:
29*3d8817e4Smiod 
30*3d8817e4Smiod    * Microsoft documentation.
31*3d8817e4Smiod 
32*3d8817e4Smiod    * The rcl program, written by Gunther Ebert
33*3d8817e4Smiod      <gunther.ebert@ixos-leipzig.de>.
34*3d8817e4Smiod 
35*3d8817e4Smiod    * The res2coff program, written by Pedro A. Aranda <paag@tid.es>.  */
36*3d8817e4Smiod 
37*3d8817e4Smiod #include "config.h"
38*3d8817e4Smiod #ifdef HAVE_UNISTD_H
39*3d8817e4Smiod #include <unistd.h>
40*3d8817e4Smiod #endif
41*3d8817e4Smiod #include <assert.h>
42*3d8817e4Smiod #include <time.h>
43*3d8817e4Smiod #include "bfd.h"
44*3d8817e4Smiod #include "getopt.h"
45*3d8817e4Smiod #include "bucomm.h"
46*3d8817e4Smiod #include "libiberty.h"
47*3d8817e4Smiod #include "safe-ctype.h"
48*3d8817e4Smiod #include "obstack.h"
49*3d8817e4Smiod #include "windres.h"
50*3d8817e4Smiod 
51*3d8817e4Smiod /* Used by resrc.c at least.  */
52*3d8817e4Smiod 
53*3d8817e4Smiod int verbose = 0;
54*3d8817e4Smiod 
55*3d8817e4Smiod /* An enumeration of format types.  */
56*3d8817e4Smiod 
57*3d8817e4Smiod enum res_format
58*3d8817e4Smiod {
59*3d8817e4Smiod   /* Unknown format.  */
60*3d8817e4Smiod   RES_FORMAT_UNKNOWN,
61*3d8817e4Smiod   /* Textual RC file.  */
62*3d8817e4Smiod   RES_FORMAT_RC,
63*3d8817e4Smiod   /* Binary RES file.  */
64*3d8817e4Smiod   RES_FORMAT_RES,
65*3d8817e4Smiod   /* COFF file.  */
66*3d8817e4Smiod   RES_FORMAT_COFF
67*3d8817e4Smiod };
68*3d8817e4Smiod 
69*3d8817e4Smiod /* A structure used to map between format types and strings.  */
70*3d8817e4Smiod 
71*3d8817e4Smiod struct format_map
72*3d8817e4Smiod {
73*3d8817e4Smiod   const char *name;
74*3d8817e4Smiod   enum res_format format;
75*3d8817e4Smiod };
76*3d8817e4Smiod 
77*3d8817e4Smiod /* A mapping between names and format types.  */
78*3d8817e4Smiod 
79*3d8817e4Smiod static const struct format_map format_names[] =
80*3d8817e4Smiod {
81*3d8817e4Smiod   { "rc", RES_FORMAT_RC },
82*3d8817e4Smiod   { "res", RES_FORMAT_RES },
83*3d8817e4Smiod   { "coff", RES_FORMAT_COFF },
84*3d8817e4Smiod   { NULL, RES_FORMAT_UNKNOWN }
85*3d8817e4Smiod };
86*3d8817e4Smiod 
87*3d8817e4Smiod /* A mapping from file extensions to format types.  */
88*3d8817e4Smiod 
89*3d8817e4Smiod static const struct format_map format_fileexts[] =
90*3d8817e4Smiod {
91*3d8817e4Smiod   { "rc", RES_FORMAT_RC },
92*3d8817e4Smiod   { "res", RES_FORMAT_RES },
93*3d8817e4Smiod   { "exe", RES_FORMAT_COFF },
94*3d8817e4Smiod   { "obj", RES_FORMAT_COFF },
95*3d8817e4Smiod   { "o", RES_FORMAT_COFF },
96*3d8817e4Smiod   { NULL, RES_FORMAT_UNKNOWN }
97*3d8817e4Smiod };
98*3d8817e4Smiod 
99*3d8817e4Smiod /* A list of include directories.  */
100*3d8817e4Smiod 
101*3d8817e4Smiod struct include_dir
102*3d8817e4Smiod {
103*3d8817e4Smiod   struct include_dir *next;
104*3d8817e4Smiod   char *dir;
105*3d8817e4Smiod };
106*3d8817e4Smiod 
107*3d8817e4Smiod static struct include_dir *include_dirs;
108*3d8817e4Smiod 
109*3d8817e4Smiod /* Static functions.  */
110*3d8817e4Smiod 
111*3d8817e4Smiod static void res_init (void);
112*3d8817e4Smiod static int extended_menuitems (const struct menuitem *);
113*3d8817e4Smiod static enum res_format format_from_name (const char *, int);
114*3d8817e4Smiod static enum res_format format_from_filename (const char *, int);
115*3d8817e4Smiod static void usage (FILE *, int);
116*3d8817e4Smiod static int cmp_res_entry (const void *, const void *);
117*3d8817e4Smiod static struct res_directory *sort_resources (struct res_directory *);
118*3d8817e4Smiod static void reswr_init (void);
119*3d8817e4Smiod static const char * quot (const char *);
120*3d8817e4Smiod 
121*3d8817e4Smiod /* When we are building a resource tree, we allocate everything onto
122*3d8817e4Smiod    an obstack, so that we can free it all at once if we want.  */
123*3d8817e4Smiod 
124*3d8817e4Smiod #define obstack_chunk_alloc xmalloc
125*3d8817e4Smiod #define obstack_chunk_free free
126*3d8817e4Smiod 
127*3d8817e4Smiod /* The resource building obstack.  */
128*3d8817e4Smiod 
129*3d8817e4Smiod static struct obstack res_obstack;
130*3d8817e4Smiod 
131*3d8817e4Smiod /* Initialize the resource building obstack.  */
132*3d8817e4Smiod 
133*3d8817e4Smiod static void
res_init(void)134*3d8817e4Smiod res_init (void)
135*3d8817e4Smiod {
136*3d8817e4Smiod   obstack_init (&res_obstack);
137*3d8817e4Smiod }
138*3d8817e4Smiod 
139*3d8817e4Smiod /* Allocate space on the resource building obstack.  */
140*3d8817e4Smiod 
141*3d8817e4Smiod void *
res_alloc(size_t bytes)142*3d8817e4Smiod res_alloc (size_t bytes)
143*3d8817e4Smiod {
144*3d8817e4Smiod   return (void *) obstack_alloc (&res_obstack, bytes);
145*3d8817e4Smiod }
146*3d8817e4Smiod 
147*3d8817e4Smiod /* We also use an obstack to save memory used while writing out a set
148*3d8817e4Smiod    of resources.  */
149*3d8817e4Smiod 
150*3d8817e4Smiod static struct obstack reswr_obstack;
151*3d8817e4Smiod 
152*3d8817e4Smiod /* Initialize the resource writing obstack.  */
153*3d8817e4Smiod 
154*3d8817e4Smiod static void
reswr_init(void)155*3d8817e4Smiod reswr_init (void)
156*3d8817e4Smiod {
157*3d8817e4Smiod   obstack_init (&reswr_obstack);
158*3d8817e4Smiod }
159*3d8817e4Smiod 
160*3d8817e4Smiod /* Allocate space on the resource writing obstack.  */
161*3d8817e4Smiod 
162*3d8817e4Smiod void *
reswr_alloc(size_t bytes)163*3d8817e4Smiod reswr_alloc (size_t bytes)
164*3d8817e4Smiod {
165*3d8817e4Smiod   return (void *) obstack_alloc (&reswr_obstack, bytes);
166*3d8817e4Smiod }
167*3d8817e4Smiod 
168*3d8817e4Smiod /* Open a file using the include directory search list.  */
169*3d8817e4Smiod 
170*3d8817e4Smiod FILE *
open_file_search(const char * filename,const char * mode,const char * errmsg,char ** real_filename)171*3d8817e4Smiod open_file_search (const char *filename, const char *mode, const char *errmsg,
172*3d8817e4Smiod 		  char **real_filename)
173*3d8817e4Smiod {
174*3d8817e4Smiod   FILE *e;
175*3d8817e4Smiod   struct include_dir *d;
176*3d8817e4Smiod 
177*3d8817e4Smiod   e = fopen (filename, mode);
178*3d8817e4Smiod   if (e != NULL)
179*3d8817e4Smiod     {
180*3d8817e4Smiod       *real_filename = xstrdup (filename);
181*3d8817e4Smiod       return e;
182*3d8817e4Smiod     }
183*3d8817e4Smiod 
184*3d8817e4Smiod   if (errno == ENOENT)
185*3d8817e4Smiod     {
186*3d8817e4Smiod       for (d = include_dirs; d != NULL; d = d->next)
187*3d8817e4Smiod 	{
188*3d8817e4Smiod 	  char *n;
189*3d8817e4Smiod 
190*3d8817e4Smiod 	  n = (char *) xmalloc (strlen (d->dir) + strlen (filename) + 2);
191*3d8817e4Smiod 	  sprintf (n, "%s/%s", d->dir, filename);
192*3d8817e4Smiod 	  e = fopen (n, mode);
193*3d8817e4Smiod 	  if (e != NULL)
194*3d8817e4Smiod 	    {
195*3d8817e4Smiod 	      *real_filename = n;
196*3d8817e4Smiod 	      return e;
197*3d8817e4Smiod 	    }
198*3d8817e4Smiod 
199*3d8817e4Smiod 	  if (errno != ENOENT)
200*3d8817e4Smiod 	    break;
201*3d8817e4Smiod 	}
202*3d8817e4Smiod     }
203*3d8817e4Smiod 
204*3d8817e4Smiod   fatal (_("can't open %s `%s': %s"), errmsg, filename, strerror (errno));
205*3d8817e4Smiod 
206*3d8817e4Smiod   /* Return a value to avoid a compiler warning.  */
207*3d8817e4Smiod   return NULL;
208*3d8817e4Smiod }
209*3d8817e4Smiod 
210*3d8817e4Smiod /* Compare two resource ID's.  We consider name entries to come before
211*3d8817e4Smiod    numeric entries, because that is how they appear in the COFF .rsrc
212*3d8817e4Smiod    section.  */
213*3d8817e4Smiod 
214*3d8817e4Smiod int
res_id_cmp(struct res_id a,struct res_id b)215*3d8817e4Smiod res_id_cmp (struct res_id a, struct res_id b)
216*3d8817e4Smiod {
217*3d8817e4Smiod   if (! a.named)
218*3d8817e4Smiod     {
219*3d8817e4Smiod       if (b.named)
220*3d8817e4Smiod 	return 1;
221*3d8817e4Smiod       if (a.u.id > b.u.id)
222*3d8817e4Smiod 	return 1;
223*3d8817e4Smiod       else if (a.u.id < b.u.id)
224*3d8817e4Smiod 	return -1;
225*3d8817e4Smiod       else
226*3d8817e4Smiod 	return 0;
227*3d8817e4Smiod     }
228*3d8817e4Smiod   else
229*3d8817e4Smiod     {
230*3d8817e4Smiod       unichar *as, *ase, *bs, *bse;
231*3d8817e4Smiod 
232*3d8817e4Smiod       if (! b.named)
233*3d8817e4Smiod 	return -1;
234*3d8817e4Smiod 
235*3d8817e4Smiod       as = a.u.n.name;
236*3d8817e4Smiod       ase = as + a.u.n.length;
237*3d8817e4Smiod       bs = b.u.n.name;
238*3d8817e4Smiod       bse = bs + b.u.n.length;
239*3d8817e4Smiod 
240*3d8817e4Smiod       while (as < ase)
241*3d8817e4Smiod 	{
242*3d8817e4Smiod 	  int i;
243*3d8817e4Smiod 
244*3d8817e4Smiod 	  if (bs >= bse)
245*3d8817e4Smiod 	    return 1;
246*3d8817e4Smiod 	  i = (int) *as - (int) *bs;
247*3d8817e4Smiod 	  if (i != 0)
248*3d8817e4Smiod 	    return i;
249*3d8817e4Smiod 	  ++as;
250*3d8817e4Smiod 	  ++bs;
251*3d8817e4Smiod 	}
252*3d8817e4Smiod 
253*3d8817e4Smiod       if (bs < bse)
254*3d8817e4Smiod 	return -1;
255*3d8817e4Smiod 
256*3d8817e4Smiod       return 0;
257*3d8817e4Smiod     }
258*3d8817e4Smiod }
259*3d8817e4Smiod 
260*3d8817e4Smiod /* Print a resource ID.  */
261*3d8817e4Smiod 
262*3d8817e4Smiod void
res_id_print(FILE * stream,struct res_id id,int quote)263*3d8817e4Smiod res_id_print (FILE *stream, struct res_id id, int quote)
264*3d8817e4Smiod {
265*3d8817e4Smiod   if (! id.named)
266*3d8817e4Smiod     fprintf (stream, "%lu", id.u.id);
267*3d8817e4Smiod   else
268*3d8817e4Smiod     {
269*3d8817e4Smiod       if (quote)
270*3d8817e4Smiod 	putc ('"', stream);
271*3d8817e4Smiod       unicode_print (stream, id.u.n.name, id.u.n.length);
272*3d8817e4Smiod       if (quote)
273*3d8817e4Smiod 	putc ('"', stream);
274*3d8817e4Smiod     }
275*3d8817e4Smiod }
276*3d8817e4Smiod 
277*3d8817e4Smiod /* Print a list of resource ID's.  */
278*3d8817e4Smiod 
279*3d8817e4Smiod void
res_ids_print(FILE * stream,int cids,const struct res_id * ids)280*3d8817e4Smiod res_ids_print (FILE *stream, int cids, const struct res_id *ids)
281*3d8817e4Smiod {
282*3d8817e4Smiod   int i;
283*3d8817e4Smiod 
284*3d8817e4Smiod   for (i = 0; i < cids; i++)
285*3d8817e4Smiod     {
286*3d8817e4Smiod       res_id_print (stream, ids[i], 1);
287*3d8817e4Smiod       if (i + 1 < cids)
288*3d8817e4Smiod 	fprintf (stream, ": ");
289*3d8817e4Smiod     }
290*3d8817e4Smiod }
291*3d8817e4Smiod 
292*3d8817e4Smiod /* Convert an ASCII string to a resource ID.  */
293*3d8817e4Smiod 
294*3d8817e4Smiod void
res_string_to_id(struct res_id * res_id,const char * string)295*3d8817e4Smiod res_string_to_id (struct res_id *res_id, const char *string)
296*3d8817e4Smiod {
297*3d8817e4Smiod   res_id->named = 1;
298*3d8817e4Smiod   unicode_from_ascii (&res_id->u.n.length, &res_id->u.n.name, string);
299*3d8817e4Smiod }
300*3d8817e4Smiod 
301*3d8817e4Smiod /* Define a resource.  The arguments are the resource tree, RESOURCES,
302*3d8817e4Smiod    and the location at which to put it in the tree, CIDS and IDS.
303*3d8817e4Smiod    This returns a newly allocated res_resource structure, which the
304*3d8817e4Smiod    caller is expected to initialize.  If DUPOK is non-zero, then if a
305*3d8817e4Smiod    resource with this ID exists, it is returned.  Otherwise, a warning
306*3d8817e4Smiod    is issued, and a new resource is created replacing the existing
307*3d8817e4Smiod    one.  */
308*3d8817e4Smiod 
309*3d8817e4Smiod struct res_resource *
define_resource(struct res_directory ** resources,int cids,const struct res_id * ids,int dupok)310*3d8817e4Smiod define_resource (struct res_directory **resources, int cids,
311*3d8817e4Smiod 		 const struct res_id *ids, int dupok)
312*3d8817e4Smiod {
313*3d8817e4Smiod   struct res_entry *re = NULL;
314*3d8817e4Smiod   int i;
315*3d8817e4Smiod 
316*3d8817e4Smiod   assert (cids > 0);
317*3d8817e4Smiod   for (i = 0; i < cids; i++)
318*3d8817e4Smiod     {
319*3d8817e4Smiod       struct res_entry **pp;
320*3d8817e4Smiod 
321*3d8817e4Smiod       if (*resources == NULL)
322*3d8817e4Smiod 	{
323*3d8817e4Smiod 	  static unsigned long timeval;
324*3d8817e4Smiod 
325*3d8817e4Smiod 	  /* Use the same timestamp for every resource created in a
326*3d8817e4Smiod              single run.  */
327*3d8817e4Smiod 	  if (timeval == 0)
328*3d8817e4Smiod 	    timeval = time (NULL);
329*3d8817e4Smiod 
330*3d8817e4Smiod 	  *resources = ((struct res_directory *)
331*3d8817e4Smiod 			res_alloc (sizeof **resources));
332*3d8817e4Smiod 	  (*resources)->characteristics = 0;
333*3d8817e4Smiod 	  (*resources)->time = timeval;
334*3d8817e4Smiod 	  (*resources)->major = 0;
335*3d8817e4Smiod 	  (*resources)->minor = 0;
336*3d8817e4Smiod 	  (*resources)->entries = NULL;
337*3d8817e4Smiod 	}
338*3d8817e4Smiod 
339*3d8817e4Smiod       for (pp = &(*resources)->entries; *pp != NULL; pp = &(*pp)->next)
340*3d8817e4Smiod 	if (res_id_cmp ((*pp)->id, ids[i]) == 0)
341*3d8817e4Smiod 	  break;
342*3d8817e4Smiod 
343*3d8817e4Smiod       if (*pp != NULL)
344*3d8817e4Smiod 	re = *pp;
345*3d8817e4Smiod       else
346*3d8817e4Smiod 	{
347*3d8817e4Smiod 	  re = (struct res_entry *) res_alloc (sizeof *re);
348*3d8817e4Smiod 	  re->next = NULL;
349*3d8817e4Smiod 	  re->id = ids[i];
350*3d8817e4Smiod 	  if ((i + 1) < cids)
351*3d8817e4Smiod 	    {
352*3d8817e4Smiod 	      re->subdir = 1;
353*3d8817e4Smiod 	      re->u.dir = NULL;
354*3d8817e4Smiod 	    }
355*3d8817e4Smiod 	  else
356*3d8817e4Smiod 	    {
357*3d8817e4Smiod 	      re->subdir = 0;
358*3d8817e4Smiod 	      re->u.res = NULL;
359*3d8817e4Smiod 	    }
360*3d8817e4Smiod 
361*3d8817e4Smiod 	  *pp = re;
362*3d8817e4Smiod 	}
363*3d8817e4Smiod 
364*3d8817e4Smiod       if ((i + 1) < cids)
365*3d8817e4Smiod 	{
366*3d8817e4Smiod 	  if (! re->subdir)
367*3d8817e4Smiod 	    {
368*3d8817e4Smiod 	      fprintf (stderr, "%s: ", program_name);
369*3d8817e4Smiod 	      res_ids_print (stderr, i, ids);
370*3d8817e4Smiod 	      fprintf (stderr, _(": expected to be a directory\n"));
371*3d8817e4Smiod 	      xexit (1);
372*3d8817e4Smiod 	    }
373*3d8817e4Smiod 
374*3d8817e4Smiod 	  resources = &re->u.dir;
375*3d8817e4Smiod 	}
376*3d8817e4Smiod     }
377*3d8817e4Smiod 
378*3d8817e4Smiod   if (re->subdir)
379*3d8817e4Smiod     {
380*3d8817e4Smiod       fprintf (stderr, "%s: ", program_name);
381*3d8817e4Smiod       res_ids_print (stderr, cids, ids);
382*3d8817e4Smiod       fprintf (stderr, _(": expected to be a leaf\n"));
383*3d8817e4Smiod       xexit (1);
384*3d8817e4Smiod     }
385*3d8817e4Smiod 
386*3d8817e4Smiod   if (re->u.res != NULL)
387*3d8817e4Smiod     {
388*3d8817e4Smiod       if (dupok)
389*3d8817e4Smiod 	return re->u.res;
390*3d8817e4Smiod 
391*3d8817e4Smiod       fprintf (stderr, _("%s: warning: "), program_name);
392*3d8817e4Smiod       res_ids_print (stderr, cids, ids);
393*3d8817e4Smiod       fprintf (stderr, _(": duplicate value\n"));
394*3d8817e4Smiod     }
395*3d8817e4Smiod 
396*3d8817e4Smiod   re->u.res = ((struct res_resource *)
397*3d8817e4Smiod 	       res_alloc (sizeof (struct res_resource)));
398*3d8817e4Smiod   memset (re->u.res, 0, sizeof (struct res_resource));
399*3d8817e4Smiod 
400*3d8817e4Smiod   re->u.res->type = RES_TYPE_UNINITIALIZED;
401*3d8817e4Smiod   return re->u.res;
402*3d8817e4Smiod }
403*3d8817e4Smiod 
404*3d8817e4Smiod /* Define a standard resource.  This is a version of define_resource
405*3d8817e4Smiod    that just takes type, name, and language arguments.  */
406*3d8817e4Smiod 
407*3d8817e4Smiod struct res_resource *
define_standard_resource(struct res_directory ** resources,int type,struct res_id name,int language,int dupok)408*3d8817e4Smiod define_standard_resource (struct res_directory **resources, int type,
409*3d8817e4Smiod 			  struct res_id name, int language, int dupok)
410*3d8817e4Smiod {
411*3d8817e4Smiod   struct res_id a[3];
412*3d8817e4Smiod 
413*3d8817e4Smiod   a[0].named = 0;
414*3d8817e4Smiod   a[0].u.id = type;
415*3d8817e4Smiod   a[1] = name;
416*3d8817e4Smiod   a[2].named = 0;
417*3d8817e4Smiod   a[2].u.id = language;
418*3d8817e4Smiod   return define_resource (resources, 3, a, dupok);
419*3d8817e4Smiod }
420*3d8817e4Smiod 
421*3d8817e4Smiod /* Comparison routine for resource sorting.  */
422*3d8817e4Smiod 
423*3d8817e4Smiod static int
cmp_res_entry(const void * p1,const void * p2)424*3d8817e4Smiod cmp_res_entry (const void *p1, const void *p2)
425*3d8817e4Smiod {
426*3d8817e4Smiod   const struct res_entry **re1, **re2;
427*3d8817e4Smiod 
428*3d8817e4Smiod   re1 = (const struct res_entry **) p1;
429*3d8817e4Smiod   re2 = (const struct res_entry **) p2;
430*3d8817e4Smiod   return res_id_cmp ((*re1)->id, (*re2)->id);
431*3d8817e4Smiod }
432*3d8817e4Smiod 
433*3d8817e4Smiod /* Sort the resources.  */
434*3d8817e4Smiod 
435*3d8817e4Smiod static struct res_directory *
sort_resources(struct res_directory * resdir)436*3d8817e4Smiod sort_resources (struct res_directory *resdir)
437*3d8817e4Smiod {
438*3d8817e4Smiod   int c, i;
439*3d8817e4Smiod   struct res_entry *re;
440*3d8817e4Smiod   struct res_entry **a;
441*3d8817e4Smiod 
442*3d8817e4Smiod   if (resdir->entries == NULL)
443*3d8817e4Smiod     return resdir;
444*3d8817e4Smiod 
445*3d8817e4Smiod   c = 0;
446*3d8817e4Smiod   for (re = resdir->entries; re != NULL; re = re->next)
447*3d8817e4Smiod     ++c;
448*3d8817e4Smiod 
449*3d8817e4Smiod   /* This is a recursive routine, so using xmalloc is probably better
450*3d8817e4Smiod      than alloca.  */
451*3d8817e4Smiod   a = (struct res_entry **) xmalloc (c * sizeof (struct res_entry *));
452*3d8817e4Smiod 
453*3d8817e4Smiod   for (i = 0, re = resdir->entries; re != NULL; re = re->next, i++)
454*3d8817e4Smiod     a[i] = re;
455*3d8817e4Smiod 
456*3d8817e4Smiod   qsort (a, c, sizeof (struct res_entry *), cmp_res_entry);
457*3d8817e4Smiod 
458*3d8817e4Smiod   resdir->entries = a[0];
459*3d8817e4Smiod   for (i = 0; i < c - 1; i++)
460*3d8817e4Smiod     a[i]->next = a[i + 1];
461*3d8817e4Smiod   a[i]->next = NULL;
462*3d8817e4Smiod 
463*3d8817e4Smiod   free (a);
464*3d8817e4Smiod 
465*3d8817e4Smiod   /* Now sort the subdirectories.  */
466*3d8817e4Smiod 
467*3d8817e4Smiod   for (re = resdir->entries; re != NULL; re = re->next)
468*3d8817e4Smiod     if (re->subdir)
469*3d8817e4Smiod       re->u.dir = sort_resources (re->u.dir);
470*3d8817e4Smiod 
471*3d8817e4Smiod   return resdir;
472*3d8817e4Smiod }
473*3d8817e4Smiod 
474*3d8817e4Smiod /* Return whether the dialog resource DIALOG is a DIALOG or a
475*3d8817e4Smiod    DIALOGEX.  */
476*3d8817e4Smiod 
477*3d8817e4Smiod int
extended_dialog(const struct dialog * dialog)478*3d8817e4Smiod extended_dialog (const struct dialog *dialog)
479*3d8817e4Smiod {
480*3d8817e4Smiod   const struct dialog_control *c;
481*3d8817e4Smiod 
482*3d8817e4Smiod   if (dialog->ex != NULL)
483*3d8817e4Smiod     return 1;
484*3d8817e4Smiod 
485*3d8817e4Smiod   for (c = dialog->controls; c != NULL; c = c->next)
486*3d8817e4Smiod     if (c->data != NULL || c->help != 0)
487*3d8817e4Smiod       return 1;
488*3d8817e4Smiod 
489*3d8817e4Smiod   return 0;
490*3d8817e4Smiod }
491*3d8817e4Smiod 
492*3d8817e4Smiod /* Return whether MENUITEMS are a MENU or a MENUEX.  */
493*3d8817e4Smiod 
494*3d8817e4Smiod int
extended_menu(const struct menu * menu)495*3d8817e4Smiod extended_menu (const struct menu *menu)
496*3d8817e4Smiod {
497*3d8817e4Smiod   return extended_menuitems (menu->items);
498*3d8817e4Smiod }
499*3d8817e4Smiod 
500*3d8817e4Smiod static int
extended_menuitems(const struct menuitem * menuitems)501*3d8817e4Smiod extended_menuitems (const struct menuitem *menuitems)
502*3d8817e4Smiod {
503*3d8817e4Smiod   const struct menuitem *mi;
504*3d8817e4Smiod 
505*3d8817e4Smiod   for (mi = menuitems; mi != NULL; mi = mi->next)
506*3d8817e4Smiod     {
507*3d8817e4Smiod       if (mi->help != 0 || mi->state != 0)
508*3d8817e4Smiod 	return 1;
509*3d8817e4Smiod       if (mi->popup != NULL && mi->id != 0)
510*3d8817e4Smiod 	return 1;
511*3d8817e4Smiod       if ((mi->type
512*3d8817e4Smiod 	   & ~ (MENUITEM_CHECKED
513*3d8817e4Smiod 		| MENUITEM_GRAYED
514*3d8817e4Smiod 		| MENUITEM_HELP
515*3d8817e4Smiod 		| MENUITEM_INACTIVE
516*3d8817e4Smiod 		| MENUITEM_MENUBARBREAK
517*3d8817e4Smiod 		| MENUITEM_MENUBREAK))
518*3d8817e4Smiod 	  != 0)
519*3d8817e4Smiod 	return 1;
520*3d8817e4Smiod       if (mi->popup != NULL)
521*3d8817e4Smiod 	{
522*3d8817e4Smiod 	  if (extended_menuitems (mi->popup))
523*3d8817e4Smiod 	    return 1;
524*3d8817e4Smiod 	}
525*3d8817e4Smiod     }
526*3d8817e4Smiod 
527*3d8817e4Smiod   return 0;
528*3d8817e4Smiod }
529*3d8817e4Smiod 
530*3d8817e4Smiod /* Convert a string to a format type, or exit if it can't be done.  */
531*3d8817e4Smiod 
532*3d8817e4Smiod static enum res_format
format_from_name(const char * name,int exit_on_error)533*3d8817e4Smiod format_from_name (const char *name, int exit_on_error)
534*3d8817e4Smiod {
535*3d8817e4Smiod   const struct format_map *m;
536*3d8817e4Smiod 
537*3d8817e4Smiod   for (m = format_names; m->name != NULL; m++)
538*3d8817e4Smiod     if (strcasecmp (m->name, name) == 0)
539*3d8817e4Smiod       break;
540*3d8817e4Smiod 
541*3d8817e4Smiod   if (m->name == NULL && exit_on_error)
542*3d8817e4Smiod     {
543*3d8817e4Smiod       non_fatal (_("unknown format type `%s'"), name);
544*3d8817e4Smiod       fprintf (stderr, _("%s: supported formats:"), program_name);
545*3d8817e4Smiod       for (m = format_names; m->name != NULL; m++)
546*3d8817e4Smiod 	fprintf (stderr, " %s", m->name);
547*3d8817e4Smiod       fprintf (stderr, "\n");
548*3d8817e4Smiod       xexit (1);
549*3d8817e4Smiod     }
550*3d8817e4Smiod 
551*3d8817e4Smiod   return m->format;
552*3d8817e4Smiod }
553*3d8817e4Smiod 
554*3d8817e4Smiod /* Work out a format type given a file name.  If INPUT is non-zero,
555*3d8817e4Smiod    it's OK to look at the file itself.  */
556*3d8817e4Smiod 
557*3d8817e4Smiod static enum res_format
format_from_filename(const char * filename,int input)558*3d8817e4Smiod format_from_filename (const char *filename, int input)
559*3d8817e4Smiod {
560*3d8817e4Smiod   const char *ext;
561*3d8817e4Smiod   FILE *e;
562*3d8817e4Smiod   unsigned char b1, b2, b3, b4, b5;
563*3d8817e4Smiod   int magic;
564*3d8817e4Smiod 
565*3d8817e4Smiod   /* If we have an extension, see if we recognize it as implying a
566*3d8817e4Smiod      particular format.  */
567*3d8817e4Smiod   ext = strrchr (filename, '.');
568*3d8817e4Smiod   if (ext != NULL)
569*3d8817e4Smiod     {
570*3d8817e4Smiod       const struct format_map *m;
571*3d8817e4Smiod 
572*3d8817e4Smiod       ++ext;
573*3d8817e4Smiod       for (m = format_fileexts; m->name != NULL; m++)
574*3d8817e4Smiod 	if (strcasecmp (m->name, ext) == 0)
575*3d8817e4Smiod 	  return m->format;
576*3d8817e4Smiod     }
577*3d8817e4Smiod 
578*3d8817e4Smiod   /* If we don't recognize the name of an output file, assume it's a
579*3d8817e4Smiod      COFF file.  */
580*3d8817e4Smiod   if (! input)
581*3d8817e4Smiod     return RES_FORMAT_COFF;
582*3d8817e4Smiod 
583*3d8817e4Smiod   /* Read the first few bytes of the file to see if we can guess what
584*3d8817e4Smiod      it is.  */
585*3d8817e4Smiod   e = fopen (filename, FOPEN_RB);
586*3d8817e4Smiod   if (e == NULL)
587*3d8817e4Smiod     fatal ("%s: %s", filename, strerror (errno));
588*3d8817e4Smiod 
589*3d8817e4Smiod   b1 = getc (e);
590*3d8817e4Smiod   b2 = getc (e);
591*3d8817e4Smiod   b3 = getc (e);
592*3d8817e4Smiod   b4 = getc (e);
593*3d8817e4Smiod   b5 = getc (e);
594*3d8817e4Smiod 
595*3d8817e4Smiod   fclose (e);
596*3d8817e4Smiod 
597*3d8817e4Smiod   /* A PE executable starts with 0x4d 0x5a.  */
598*3d8817e4Smiod   if (b1 == 0x4d && b2 == 0x5a)
599*3d8817e4Smiod     return RES_FORMAT_COFF;
600*3d8817e4Smiod 
601*3d8817e4Smiod   /* A COFF .o file starts with a COFF magic number.  */
602*3d8817e4Smiod   magic = (b2 << 8) | b1;
603*3d8817e4Smiod   switch (magic)
604*3d8817e4Smiod     {
605*3d8817e4Smiod     case 0x14c: /* i386 */
606*3d8817e4Smiod     case 0x166: /* MIPS */
607*3d8817e4Smiod     case 0x184: /* Alpha */
608*3d8817e4Smiod     case 0x268: /* 68k */
609*3d8817e4Smiod     case 0x1f0: /* PowerPC */
610*3d8817e4Smiod     case 0x290: /* PA */
611*3d8817e4Smiod       return RES_FORMAT_COFF;
612*3d8817e4Smiod     }
613*3d8817e4Smiod 
614*3d8817e4Smiod   /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0.  */
615*3d8817e4Smiod   if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0 && b5 == 0x20)
616*3d8817e4Smiod     return RES_FORMAT_RES;
617*3d8817e4Smiod 
618*3d8817e4Smiod   /* If every character is printable or space, assume it's an RC file.  */
619*3d8817e4Smiod   if ((ISPRINT (b1) || ISSPACE (b1))
620*3d8817e4Smiod       && (ISPRINT (b2) || ISSPACE (b2))
621*3d8817e4Smiod       && (ISPRINT (b3) || ISSPACE (b3))
622*3d8817e4Smiod       && (ISPRINT (b4) || ISSPACE (b4))
623*3d8817e4Smiod       && (ISPRINT (b5) || ISSPACE (b5)))
624*3d8817e4Smiod     return RES_FORMAT_RC;
625*3d8817e4Smiod 
626*3d8817e4Smiod   /* Otherwise, we give up.  */
627*3d8817e4Smiod   fatal (_("can not determine type of file `%s'; use the -J option"),
628*3d8817e4Smiod 	 filename);
629*3d8817e4Smiod 
630*3d8817e4Smiod   /* Return something to silence the compiler warning.  */
631*3d8817e4Smiod   return RES_FORMAT_UNKNOWN;
632*3d8817e4Smiod }
633*3d8817e4Smiod 
634*3d8817e4Smiod /* Print a usage message and exit.  */
635*3d8817e4Smiod 
636*3d8817e4Smiod static void
usage(FILE * stream,int status)637*3d8817e4Smiod usage (FILE *stream, int status)
638*3d8817e4Smiod {
639*3d8817e4Smiod   fprintf (stream, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
640*3d8817e4Smiod 	   program_name);
641*3d8817e4Smiod   fprintf (stream, _(" The options are:\n\
642*3d8817e4Smiod   -i --input=<file>            Name input file\n\
643*3d8817e4Smiod   -o --output=<file>           Name output file\n\
644*3d8817e4Smiod   -J --input-format=<format>   Specify input format\n\
645*3d8817e4Smiod   -O --output-format=<format>  Specify output format\n\
646*3d8817e4Smiod   -F --target=<target>         Specify COFF target\n\
647*3d8817e4Smiod      --preprocessor=<program>  Program to use to preprocess rc file\n\
648*3d8817e4Smiod   -I --include-dir=<dir>       Include directory when preprocessing rc file\n\
649*3d8817e4Smiod   -D --define <sym>[=<val>]    Define SYM when preprocessing rc file\n\
650*3d8817e4Smiod   -U --undefine <sym>          Undefine SYM when preprocessing rc file\n\
651*3d8817e4Smiod   -v --verbose                 Verbose - tells you what it's doing\n\
652*3d8817e4Smiod   -l --language=<val>          Set language when reading rc file\n\
653*3d8817e4Smiod      --use-temp-file           Use a temporary file instead of popen to read\n\
654*3d8817e4Smiod                                the preprocessor output\n\
655*3d8817e4Smiod      --no-use-temp-file        Use popen (default)\n"));
656*3d8817e4Smiod #ifdef YYDEBUG
657*3d8817e4Smiod   fprintf (stream, _("\
658*3d8817e4Smiod      --yydebug                 Turn on parser debugging\n"));
659*3d8817e4Smiod #endif
660*3d8817e4Smiod   fprintf (stream, _("\
661*3d8817e4Smiod   -r                           Ignored for compatibility with rc\n\
662*3d8817e4Smiod   @<file>                      Read options from <file>\n\
663*3d8817e4Smiod   -h --help                    Print this help message\n\
664*3d8817e4Smiod   -V --version                 Print version information\n"));
665*3d8817e4Smiod   fprintf (stream, _("\
666*3d8817e4Smiod FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
667*3d8817e4Smiod extension if not specified.  A single file name is an input file.\n\
668*3d8817e4Smiod No input-file is stdin, default rc.  No output-file is stdout, default rc.\n"));
669*3d8817e4Smiod 
670*3d8817e4Smiod   list_supported_targets (program_name, stream);
671*3d8817e4Smiod 
672*3d8817e4Smiod   if (status == 0)
673*3d8817e4Smiod     fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
674*3d8817e4Smiod 
675*3d8817e4Smiod   exit (status);
676*3d8817e4Smiod }
677*3d8817e4Smiod 
678*3d8817e4Smiod /* Quote characters that will confuse the shell when we run the preprocessor.  */
679*3d8817e4Smiod 
680*3d8817e4Smiod static const char *
quot(const char * string)681*3d8817e4Smiod quot (const char *string)
682*3d8817e4Smiod {
683*3d8817e4Smiod   static char *buf = 0;
684*3d8817e4Smiod   static int buflen = 0;
685*3d8817e4Smiod   int slen = strlen (string);
686*3d8817e4Smiod   const char *src;
687*3d8817e4Smiod   char *dest;
688*3d8817e4Smiod 
689*3d8817e4Smiod   if ((buflen < slen * 2 + 2) || !buf)
690*3d8817e4Smiod     {
691*3d8817e4Smiod       buflen = slen * 2 + 2;
692*3d8817e4Smiod       if (buf)
693*3d8817e4Smiod 	free (buf);
694*3d8817e4Smiod       buf = (char *) xmalloc (buflen);
695*3d8817e4Smiod     }
696*3d8817e4Smiod 
697*3d8817e4Smiod   for (src=string, dest=buf; *src; src++, dest++)
698*3d8817e4Smiod     {
699*3d8817e4Smiod       if (*src == '(' || *src == ')' || *src == ' ')
700*3d8817e4Smiod 	*dest++ = '\\';
701*3d8817e4Smiod       *dest = *src;
702*3d8817e4Smiod     }
703*3d8817e4Smiod   *dest = 0;
704*3d8817e4Smiod   return buf;
705*3d8817e4Smiod }
706*3d8817e4Smiod 
707*3d8817e4Smiod /* Long options.  */
708*3d8817e4Smiod 
709*3d8817e4Smiod /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
710*3d8817e4Smiod 
711*3d8817e4Smiod #define OPTION_PREPROCESSOR	150
712*3d8817e4Smiod #define OPTION_USE_TEMP_FILE	(OPTION_PREPROCESSOR + 1)
713*3d8817e4Smiod #define OPTION_NO_USE_TEMP_FILE	(OPTION_USE_TEMP_FILE + 1)
714*3d8817e4Smiod #define OPTION_YYDEBUG		(OPTION_NO_USE_TEMP_FILE + 1)
715*3d8817e4Smiod 
716*3d8817e4Smiod static const struct option long_options[] =
717*3d8817e4Smiod {
718*3d8817e4Smiod   {"input", required_argument, 0, 'i'},
719*3d8817e4Smiod   {"output", required_argument, 0, 'o'},
720*3d8817e4Smiod   {"input-format", required_argument, 0, 'J'},
721*3d8817e4Smiod   {"output-format", required_argument, 0, 'O'},
722*3d8817e4Smiod   {"target", required_argument, 0, 'F'},
723*3d8817e4Smiod   {"preprocessor", required_argument, 0, OPTION_PREPROCESSOR},
724*3d8817e4Smiod   {"include-dir", required_argument, 0, 'I'},
725*3d8817e4Smiod   {"define", required_argument, 0, 'D'},
726*3d8817e4Smiod   {"undefine", required_argument, 0, 'U'},
727*3d8817e4Smiod   {"verbose", no_argument, 0, 'v'},
728*3d8817e4Smiod   {"language", required_argument, 0, 'l'},
729*3d8817e4Smiod   {"use-temp-file", no_argument, 0, OPTION_USE_TEMP_FILE},
730*3d8817e4Smiod   {"no-use-temp-file", no_argument, 0, OPTION_NO_USE_TEMP_FILE},
731*3d8817e4Smiod   {"yydebug", no_argument, 0, OPTION_YYDEBUG},
732*3d8817e4Smiod   {"version", no_argument, 0, 'V'},
733*3d8817e4Smiod   {"help", no_argument, 0, 'h'},
734*3d8817e4Smiod   {0, no_argument, 0, 0}
735*3d8817e4Smiod };
736*3d8817e4Smiod 
737*3d8817e4Smiod /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes.  */
738*3d8817e4Smiod int main (int, char **);
739*3d8817e4Smiod 
740*3d8817e4Smiod /* The main function.  */
741*3d8817e4Smiod 
742*3d8817e4Smiod int
main(int argc,char ** argv)743*3d8817e4Smiod main (int argc, char **argv)
744*3d8817e4Smiod {
745*3d8817e4Smiod   int c;
746*3d8817e4Smiod   char *input_filename;
747*3d8817e4Smiod   char *output_filename;
748*3d8817e4Smiod   enum res_format input_format;
749*3d8817e4Smiod   enum res_format input_format_tmp;
750*3d8817e4Smiod   enum res_format output_format;
751*3d8817e4Smiod   char *target;
752*3d8817e4Smiod   char *preprocessor;
753*3d8817e4Smiod   char *preprocargs;
754*3d8817e4Smiod   const char *quotedarg;
755*3d8817e4Smiod   int language;
756*3d8817e4Smiod   struct res_directory *resources;
757*3d8817e4Smiod   int use_temp_file;
758*3d8817e4Smiod 
759*3d8817e4Smiod #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
760*3d8817e4Smiod   setlocale (LC_MESSAGES, "");
761*3d8817e4Smiod #endif
762*3d8817e4Smiod #if defined (HAVE_SETLOCALE)
763*3d8817e4Smiod   setlocale (LC_CTYPE, "");
764*3d8817e4Smiod #endif
765*3d8817e4Smiod   bindtextdomain (PACKAGE, LOCALEDIR);
766*3d8817e4Smiod   textdomain (PACKAGE);
767*3d8817e4Smiod 
768*3d8817e4Smiod   program_name = argv[0];
769*3d8817e4Smiod   xmalloc_set_program_name (program_name);
770*3d8817e4Smiod 
771*3d8817e4Smiod   expandargv (&argc, &argv);
772*3d8817e4Smiod 
773*3d8817e4Smiod   bfd_init ();
774*3d8817e4Smiod   set_default_bfd_target ();
775*3d8817e4Smiod 
776*3d8817e4Smiod   res_init ();
777*3d8817e4Smiod 
778*3d8817e4Smiod   input_filename = NULL;
779*3d8817e4Smiod   output_filename = NULL;
780*3d8817e4Smiod   input_format = RES_FORMAT_UNKNOWN;
781*3d8817e4Smiod   output_format = RES_FORMAT_UNKNOWN;
782*3d8817e4Smiod   target = NULL;
783*3d8817e4Smiod   preprocessor = NULL;
784*3d8817e4Smiod   preprocargs = NULL;
785*3d8817e4Smiod   language = 0x409;   /* LANG_ENGLISH, SUBLANG_ENGLISH_US.  */
786*3d8817e4Smiod   use_temp_file = 0;
787*3d8817e4Smiod 
788*3d8817e4Smiod   while ((c = getopt_long (argc, argv, "f:i:l:o:I:J:O:F:D:U:rhHvV", long_options,
789*3d8817e4Smiod 			   (int *) 0)) != EOF)
790*3d8817e4Smiod     {
791*3d8817e4Smiod       switch (c)
792*3d8817e4Smiod 	{
793*3d8817e4Smiod 	case 'i':
794*3d8817e4Smiod 	  input_filename = optarg;
795*3d8817e4Smiod 	  break;
796*3d8817e4Smiod 
797*3d8817e4Smiod 	case 'f':
798*3d8817e4Smiod 	  /* For compatibility with rc we accept "-fo <name>" as being the
799*3d8817e4Smiod 	     equivalent of "-o <name>".  We do not advertise this fact
800*3d8817e4Smiod 	     though, as we do not want users to use non-GNU like command
801*3d8817e4Smiod 	     line switches.  */
802*3d8817e4Smiod 	  if (*optarg != 'o')
803*3d8817e4Smiod 	    fatal (_("invalid option -f\n"));
804*3d8817e4Smiod 	  optarg++;
805*3d8817e4Smiod 	  if (* optarg == 0)
806*3d8817e4Smiod 	    {
807*3d8817e4Smiod 	      if (optind == argc)
808*3d8817e4Smiod 		fatal (_("No filename following the -fo option.\n"));
809*3d8817e4Smiod 	      optarg = argv [optind++];
810*3d8817e4Smiod 	    }
811*3d8817e4Smiod 	  /* Fall through.  */
812*3d8817e4Smiod 
813*3d8817e4Smiod 	case 'o':
814*3d8817e4Smiod 	  output_filename = optarg;
815*3d8817e4Smiod 	  break;
816*3d8817e4Smiod 
817*3d8817e4Smiod 	case 'J':
818*3d8817e4Smiod 	  input_format = format_from_name (optarg, 1);
819*3d8817e4Smiod 	  break;
820*3d8817e4Smiod 
821*3d8817e4Smiod 	case 'O':
822*3d8817e4Smiod 	  output_format = format_from_name (optarg, 1);
823*3d8817e4Smiod 	  break;
824*3d8817e4Smiod 
825*3d8817e4Smiod 	case 'F':
826*3d8817e4Smiod 	  target = optarg;
827*3d8817e4Smiod 	  break;
828*3d8817e4Smiod 
829*3d8817e4Smiod 	case OPTION_PREPROCESSOR:
830*3d8817e4Smiod 	  preprocessor = optarg;
831*3d8817e4Smiod 	  break;
832*3d8817e4Smiod 
833*3d8817e4Smiod 	case 'D':
834*3d8817e4Smiod 	case 'U':
835*3d8817e4Smiod 	  if (preprocargs == NULL)
836*3d8817e4Smiod 	    {
837*3d8817e4Smiod 	      quotedarg = quot (optarg);
838*3d8817e4Smiod 	      preprocargs = xmalloc (strlen (quotedarg) + 3);
839*3d8817e4Smiod 	      sprintf (preprocargs, "-%c%s", c, quotedarg);
840*3d8817e4Smiod 	    }
841*3d8817e4Smiod 	  else
842*3d8817e4Smiod 	    {
843*3d8817e4Smiod 	      char *n;
844*3d8817e4Smiod 
845*3d8817e4Smiod 	      quotedarg = quot (optarg);
846*3d8817e4Smiod 	      n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
847*3d8817e4Smiod 	      sprintf (n, "%s -%c%s", preprocargs, c, quotedarg);
848*3d8817e4Smiod 	      free (preprocargs);
849*3d8817e4Smiod 	      preprocargs = n;
850*3d8817e4Smiod 	    }
851*3d8817e4Smiod 	  break;
852*3d8817e4Smiod 
853*3d8817e4Smiod 	case 'r':
854*3d8817e4Smiod 	  /* Ignored for compatibility with rc.  */
855*3d8817e4Smiod 	  break;
856*3d8817e4Smiod 
857*3d8817e4Smiod 	case 'v':
858*3d8817e4Smiod 	  verbose ++;
859*3d8817e4Smiod 	  break;
860*3d8817e4Smiod 
861*3d8817e4Smiod 	case 'I':
862*3d8817e4Smiod 	  /* For backward compatibility, should be removed in the future.  */
863*3d8817e4Smiod 	  input_format_tmp = format_from_name (optarg, 0);
864*3d8817e4Smiod 	  if (input_format_tmp != RES_FORMAT_UNKNOWN)
865*3d8817e4Smiod 	    {
866*3d8817e4Smiod 	      fprintf (stderr, _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
867*3d8817e4Smiod 	      input_format = input_format_tmp;
868*3d8817e4Smiod 	      break;
869*3d8817e4Smiod 	    }
870*3d8817e4Smiod 
871*3d8817e4Smiod 	  if (preprocargs == NULL)
872*3d8817e4Smiod 	    {
873*3d8817e4Smiod 	      quotedarg = quot (optarg);
874*3d8817e4Smiod 	      preprocargs = xmalloc (strlen (quotedarg) + 3);
875*3d8817e4Smiod 	      sprintf (preprocargs, "-I%s", quotedarg);
876*3d8817e4Smiod 	    }
877*3d8817e4Smiod 	  else
878*3d8817e4Smiod 	    {
879*3d8817e4Smiod 	      char *n;
880*3d8817e4Smiod 
881*3d8817e4Smiod 	      quotedarg = quot (optarg);
882*3d8817e4Smiod 	      n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
883*3d8817e4Smiod 	      sprintf (n, "%s -I%s", preprocargs, quotedarg);
884*3d8817e4Smiod 	      free (preprocargs);
885*3d8817e4Smiod 	      preprocargs = n;
886*3d8817e4Smiod 	    }
887*3d8817e4Smiod 
888*3d8817e4Smiod 	  {
889*3d8817e4Smiod 	    struct include_dir *n, **pp;
890*3d8817e4Smiod 
891*3d8817e4Smiod 	    n = (struct include_dir *) xmalloc (sizeof *n);
892*3d8817e4Smiod 	    n->next = NULL;
893*3d8817e4Smiod 	    n->dir = optarg;
894*3d8817e4Smiod 
895*3d8817e4Smiod 	    for (pp = &include_dirs; *pp != NULL; pp = &(*pp)->next)
896*3d8817e4Smiod 	      ;
897*3d8817e4Smiod 	    *pp = n;
898*3d8817e4Smiod 	  }
899*3d8817e4Smiod 
900*3d8817e4Smiod 	  break;
901*3d8817e4Smiod 
902*3d8817e4Smiod 	case 'l':
903*3d8817e4Smiod 	  language = strtol (optarg, (char **) NULL, 16);
904*3d8817e4Smiod 	  break;
905*3d8817e4Smiod 
906*3d8817e4Smiod 	case OPTION_USE_TEMP_FILE:
907*3d8817e4Smiod 	  use_temp_file = 1;
908*3d8817e4Smiod 	  break;
909*3d8817e4Smiod 
910*3d8817e4Smiod 	case OPTION_NO_USE_TEMP_FILE:
911*3d8817e4Smiod 	  use_temp_file = 0;
912*3d8817e4Smiod 	  break;
913*3d8817e4Smiod 
914*3d8817e4Smiod #ifdef YYDEBUG
915*3d8817e4Smiod 	case OPTION_YYDEBUG:
916*3d8817e4Smiod 	  yydebug = 1;
917*3d8817e4Smiod 	  break;
918*3d8817e4Smiod #endif
919*3d8817e4Smiod 
920*3d8817e4Smiod 	case 'h':
921*3d8817e4Smiod 	case 'H':
922*3d8817e4Smiod 	  usage (stdout, 0);
923*3d8817e4Smiod 	  break;
924*3d8817e4Smiod 
925*3d8817e4Smiod 	case 'V':
926*3d8817e4Smiod 	  print_version ("windres");
927*3d8817e4Smiod 	  break;
928*3d8817e4Smiod 
929*3d8817e4Smiod 	default:
930*3d8817e4Smiod 	  usage (stderr, 1);
931*3d8817e4Smiod 	  break;
932*3d8817e4Smiod 	}
933*3d8817e4Smiod     }
934*3d8817e4Smiod 
935*3d8817e4Smiod   if (input_filename == NULL && optind < argc)
936*3d8817e4Smiod     {
937*3d8817e4Smiod       input_filename = argv[optind];
938*3d8817e4Smiod       ++optind;
939*3d8817e4Smiod     }
940*3d8817e4Smiod 
941*3d8817e4Smiod   if (output_filename == NULL && optind < argc)
942*3d8817e4Smiod     {
943*3d8817e4Smiod       output_filename = argv[optind];
944*3d8817e4Smiod       ++optind;
945*3d8817e4Smiod     }
946*3d8817e4Smiod 
947*3d8817e4Smiod   if (argc != optind)
948*3d8817e4Smiod     usage (stderr, 1);
949*3d8817e4Smiod 
950*3d8817e4Smiod   if (input_format == RES_FORMAT_UNKNOWN)
951*3d8817e4Smiod     {
952*3d8817e4Smiod       if (input_filename == NULL)
953*3d8817e4Smiod 	input_format = RES_FORMAT_RC;
954*3d8817e4Smiod       else
955*3d8817e4Smiod 	input_format = format_from_filename (input_filename, 1);
956*3d8817e4Smiod     }
957*3d8817e4Smiod 
958*3d8817e4Smiod   if (output_format == RES_FORMAT_UNKNOWN)
959*3d8817e4Smiod     {
960*3d8817e4Smiod       if (output_filename == NULL)
961*3d8817e4Smiod 	output_format = RES_FORMAT_RC;
962*3d8817e4Smiod       else
963*3d8817e4Smiod 	output_format = format_from_filename (output_filename, 0);
964*3d8817e4Smiod     }
965*3d8817e4Smiod 
966*3d8817e4Smiod   /* Read the input file.  */
967*3d8817e4Smiod   switch (input_format)
968*3d8817e4Smiod     {
969*3d8817e4Smiod     default:
970*3d8817e4Smiod       abort ();
971*3d8817e4Smiod     case RES_FORMAT_RC:
972*3d8817e4Smiod       resources = read_rc_file (input_filename, preprocessor, preprocargs,
973*3d8817e4Smiod 				language, use_temp_file);
974*3d8817e4Smiod       break;
975*3d8817e4Smiod     case RES_FORMAT_RES:
976*3d8817e4Smiod       resources = read_res_file (input_filename);
977*3d8817e4Smiod       break;
978*3d8817e4Smiod     case RES_FORMAT_COFF:
979*3d8817e4Smiod       resources = read_coff_rsrc (input_filename, target);
980*3d8817e4Smiod       break;
981*3d8817e4Smiod     }
982*3d8817e4Smiod 
983*3d8817e4Smiod   if (resources == NULL)
984*3d8817e4Smiod     fatal (_("no resources"));
985*3d8817e4Smiod 
986*3d8817e4Smiod   /* Sort the resources.  This is required for COFF, convenient for
987*3d8817e4Smiod      rc, and unimportant for res.  */
988*3d8817e4Smiod   resources = sort_resources (resources);
989*3d8817e4Smiod 
990*3d8817e4Smiod   /* Write the output file.  */
991*3d8817e4Smiod   reswr_init ();
992*3d8817e4Smiod 
993*3d8817e4Smiod   switch (output_format)
994*3d8817e4Smiod     {
995*3d8817e4Smiod     default:
996*3d8817e4Smiod       abort ();
997*3d8817e4Smiod     case RES_FORMAT_RC:
998*3d8817e4Smiod       write_rc_file (output_filename, resources);
999*3d8817e4Smiod       break;
1000*3d8817e4Smiod     case RES_FORMAT_RES:
1001*3d8817e4Smiod       write_res_file (output_filename, resources);
1002*3d8817e4Smiod       break;
1003*3d8817e4Smiod     case RES_FORMAT_COFF:
1004*3d8817e4Smiod       write_coff_file (output_filename, target, resources);
1005*3d8817e4Smiod       break;
1006*3d8817e4Smiod     }
1007*3d8817e4Smiod 
1008*3d8817e4Smiod   xexit (0);
1009*3d8817e4Smiod   return 0;
1010*3d8817e4Smiod }
1011