1 /* resrc.c -- read and write Windows rc files.
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003 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 file contains functions that read and write Windows rc files.
23 These are text files that represent resources. */
27 #include "libiberty.h"
28 #include "safe-ctype.h"
38 #ifdef HAVE_SYS_WAIT_H
40 #else /* ! HAVE_SYS_WAIT_H */
41 #if ! defined (_WIN32) || defined (__CYGWIN__)
43 #define WIFEXITED(w) (((w)&0377) == 0)
46 #define WIFSIGNALED(w) (((w)&0377) != 0177 && ((w)&~0377) == 0)
49 #define WTERMSIG(w) ((w) & 0177)
52 #define WEXITSTATUS(w) (((w) >> 8) & 0377)
54 #else /* defined (_WIN32) && ! defined (__CYGWIN__) */
56 #define WIFEXITED(w) (((w) & 0xff) == 0)
59 #define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
62 #define WTERMSIG(w) ((w) & 0x7f)
65 #define WEXITSTATUS(w) (((w) & 0xff00) >> 8)
67 #endif /* defined (_WIN32) && ! defined (__CYGWIN__) */
68 #endif /* ! HAVE_SYS_WAIT_H */
71 #define STDOUT_FILENO 1
74 #if defined (_WIN32) && ! defined (__CYGWIN__)
76 #define pclose _pclose
79 /* The default preprocessor. */
81 #define DEFAULT_PREPROCESSOR "gcc -E -xc -DRC_INVOKED"
83 /* We read the directory entries in a cursor or icon file into
84 instances of this structure. */
90 /* Height of image. */
92 /* Number of colors in image. */
93 unsigned char colorcount;
99 unsigned short planes;
100 /* Bits per pixel. */
105 /* X coordinate of hotspot. */
106 unsigned short xhotspot;
107 /* Y coordinate of hotspot. */
108 unsigned short yhotspot;
111 /* Bytes in image. */
113 /* File offset of image. */
114 unsigned long offset;
117 /* The name of the rc file we are reading. */
121 /* The line number in the rc file. */
125 /* The pipe we are reading from, so that we can close it if we exit. */
127 static FILE *cpp_pipe;
129 /* The temporary file used if we're not using popen, so we can delete it
132 static char *cpp_temp_file;
134 /* Input stream is either a file or a pipe. */
136 static enum {ISTREAM_PIPE, ISTREAM_FILE} istream_type;
138 /* As we read the rc file, we attach information to this structure. */
140 static struct res_directory *resources;
142 /* The number of cursor resources we have written out. */
146 /* The number of font resources we have written out. */
150 /* Font directory information. */
152 struct fontdir *fontdirs;
154 /* Resource info to use for fontdirs. */
156 struct res_res_info fontdirs_resinfo;
158 /* The number of icon resources we have written out. */
162 /* Local functions. */
164 static int run_cmd (char *, const char *);
165 static FILE *open_input_stream (char *);
166 static FILE *look_for_default
167 (char *, const char *, int, const char *, const char *);
168 static void close_input_stream (void);
169 static void unexpected_eof (const char *);
170 static int get_word (FILE *, const char *);
171 static unsigned long get_long (FILE *, const char *);
172 static void get_data (FILE *, unsigned char *, unsigned long, const char *);
173 static void define_fontdirs (void);
175 /* Run `cmd' and redirect the output to `redir'. */
178 run_cmd (char *cmd, const char *redir)
181 int pid, wait_status, retcode;
184 char *errmsg_fmt, *errmsg_arg;
185 char *temp_base = choose_temp_base ();
188 int redir_handle = -1;
189 int stdout_save = -1;
191 /* Count the args. */
194 for (s = cmd; *s; s++)
199 argv = alloca (sizeof (char *) * (i + 3));
205 while (*s == ' ' && *s != 0)
211 in_quote = (*s == '\'' || *s == '"');
212 sep = (in_quote) ? *s++ : ' ';
215 while (*s != sep && *s != 0)
228 /* Setup the redirection. We can't use the usual fork/exec and redirect
229 since we may be running on non-POSIX Windows host. */
234 /* Open temporary output file. */
235 redir_handle = open (redir, O_WRONLY | O_TRUNC | O_CREAT, 0666);
236 if (redir_handle == -1)
237 fatal (_("can't open temporary file `%s': %s"), redir,
240 /* Duplicate the stdout file handle so it can be restored later. */
241 stdout_save = dup (STDOUT_FILENO);
242 if (stdout_save == -1)
243 fatal (_("can't redirect stdout: `%s': %s"), redir, strerror (errno));
245 /* Redirect stdout to our output file. */
246 dup2 (redir_handle, STDOUT_FILENO);
248 pid = pexecute (argv[0], (char * const *) argv, program_name, temp_base,
249 &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH);
251 /* Restore stdout to its previous setting. */
252 dup2 (stdout_save, STDOUT_FILENO);
254 /* Close reponse file. */
255 close (redir_handle);
259 fatal (_("%s %s: %s"), errmsg_fmt, errmsg_arg, strerror (errno));
264 pid = pwait (pid, &wait_status, 0);
268 fatal (_("wait: %s"), strerror (errno));
271 else if (WIFSIGNALED (wait_status))
273 fatal (_("subprocess got fatal signal %d"), WTERMSIG (wait_status));
276 else if (WIFEXITED (wait_status))
278 if (WEXITSTATUS (wait_status) != 0)
280 fatal (_("%s exited with status %d"), cmd,
281 WEXITSTATUS (wait_status));
292 open_input_stream (char *cmd)
294 if (istream_type == ISTREAM_FILE)
298 fileprefix = choose_temp_base ();
299 cpp_temp_file = (char *) xmalloc (strlen (fileprefix) + 5);
300 sprintf (cpp_temp_file, "%s.irc", fileprefix);
303 if (run_cmd (cmd, cpp_temp_file))
304 fatal (_("can't execute `%s': %s"), cmd, strerror (errno));
306 cpp_pipe = fopen (cpp_temp_file, FOPEN_RT);;
307 if (cpp_pipe == NULL)
308 fatal (_("can't open temporary file `%s': %s"),
309 cpp_temp_file, strerror (errno));
313 _("Using temporary file `%s' to read preprocessor output\n"),
318 cpp_pipe = popen (cmd, FOPEN_RT);
319 if (cpp_pipe == NULL)
320 fatal (_("can't popen `%s': %s"), cmd, strerror (errno));
322 fprintf (stderr, _("Using popen to read preprocessor output\n"));
325 xatexit (close_input_stream);
329 /* look for the preprocessor program */
332 look_for_default (char *cmd, const char *prefix, int end_prefix,
333 const char *preprocargs, const char *filename)
339 strcpy (cmd, prefix);
341 sprintf (cmd + end_prefix, "%s", DEFAULT_PREPROCESSOR);
342 space = strchr (cmd + end_prefix, ' ');
347 #if defined (__DJGPP__) || defined (__CYGWIN__) || defined (_WIN32)
348 strchr (cmd, '\\') ||
352 found = (stat (cmd, &s) == 0
353 #ifdef HAVE_EXECUTABLE_SUFFIX
354 || stat (strcat (cmd, EXECUTABLE_SUFFIX), &s) == 0
361 fprintf (stderr, _("Tried `%s'\n"), cmd);
366 strcpy (cmd, prefix);
368 sprintf (cmd + end_prefix, "%s %s %s",
369 DEFAULT_PREPROCESSOR, preprocargs, filename);
372 fprintf (stderr, _("Using `%s'\n"), cmd);
374 cpp_pipe = open_input_stream (cmd);
378 /* Read an rc file. */
380 struct res_directory *
381 read_rc_file (const char *filename, const char *preprocessor,
382 const char *preprocargs, int language, int use_temp_file)
386 istream_type = (use_temp_file) ? ISTREAM_FILE : ISTREAM_PIPE;
388 if (preprocargs == NULL)
390 if (filename == NULL)
395 cmd = xmalloc (strlen (preprocessor)
396 + strlen (preprocargs)
399 sprintf (cmd, "%s %s %s", preprocessor, preprocargs, filename);
401 cpp_pipe = open_input_stream (cmd);
405 char *dash, *slash, *cp;
407 preprocessor = DEFAULT_PREPROCESSOR;
409 cmd = xmalloc (strlen (program_name)
410 + strlen (preprocessor)
411 + strlen (preprocargs)
413 #ifdef HAVE_EXECUTABLE_SUFFIX
414 + strlen (EXECUTABLE_SUFFIX)
420 for (cp = program_name; *cp; cp++)
425 #if defined (__DJGPP__) || defined (__CYGWIN__) || defined(_WIN32)
426 *cp == ':' || *cp == '\\' ||
439 /* First, try looking for a prefixed gcc in the windres
440 directory, with the same prefix as windres */
442 cpp_pipe = look_for_default (cmd, program_name, dash-program_name+1,
443 preprocargs, filename);
446 if (slash && !cpp_pipe)
448 /* Next, try looking for a gcc in the same directory as
451 cpp_pipe = look_for_default (cmd, program_name, slash-program_name+1,
452 preprocargs, filename);
457 /* Sigh, try the default */
459 cpp_pipe = look_for_default (cmd, "", 0, preprocargs, filename);
466 rc_filename = xstrdup (filename);
469 rcparse_set_language (language);
472 rcparse_discard_strings ();
474 close_input_stream ();
476 if (fontdirs != NULL)
485 /* Close the input stream if it is open. */
488 close_input_stream (void)
490 if (istream_type == ISTREAM_FILE)
492 if (cpp_pipe != NULL)
495 if (cpp_temp_file != NULL)
497 int errno_save = errno;
499 unlink (cpp_temp_file);
501 free (cpp_temp_file);
506 if (cpp_pipe != NULL)
510 /* Since this is also run via xatexit, safeguard. */
512 cpp_temp_file = NULL;
515 /* Report an error while reading an rc file. */
518 yyerror (const char *msg)
520 fatal ("%s:%d: %s", rc_filename, rc_lineno, msg);
523 /* Issue a warning while reading an rc file. */
526 rcparse_warning (const char *msg)
528 fprintf (stderr, _("%s:%d: %s\n"), rc_filename, rc_lineno, msg);
531 /* Die if we get an unexpected end of file. */
534 unexpected_eof (const char *msg)
536 fatal (_("%s: unexpected EOF"), msg);
539 /* Read a 16 bit word from a file. The data is assumed to be little
543 get_word (FILE *e, const char *msg)
550 unexpected_eof (msg);
551 return ((b2 & 0xff) << 8) | (b1 & 0xff);
554 /* Read a 32 bit word from a file. The data is assumed to be little
558 get_long (FILE *e, const char *msg)
567 unexpected_eof (msg);
568 return (((((((b4 & 0xff) << 8)
574 /* Read data from a file. This is a wrapper to do error checking. */
577 get_data (FILE *e, unsigned char *p, unsigned long c, const char *msg)
581 got = fread (p, 1, c, e);
585 fatal (_("%s: read of %lu returned %lu"), msg, c, got);
588 /* Define an accelerator resource. */
591 define_accelerator (struct res_id id, const struct res_res_info *resinfo,
592 struct accelerator *data)
594 struct res_resource *r;
596 r = define_standard_resource (&resources, RT_ACCELERATOR, id,
597 resinfo->language, 0);
598 r->type = RES_TYPE_ACCELERATOR;
600 r->res_info = *resinfo;
603 /* Define a bitmap resource. Bitmap data is stored in a file. The
604 first 14 bytes of the file are a standard header, which is not
605 included in the resource data. */
607 #define BITMAP_SKIP (14)
610 define_bitmap (struct res_id id, const struct res_res_info *resinfo,
611 const char *filename)
618 struct res_resource *r;
620 e = open_file_search (filename, FOPEN_RB, "bitmap file", &real_filename);
622 if (stat (real_filename, &s) < 0)
623 fatal (_("stat failed on bitmap file `%s': %s"), real_filename,
626 data = (unsigned char *) res_alloc (s.st_size - BITMAP_SKIP);
628 for (i = 0; i < BITMAP_SKIP; i++)
631 get_data (e, data, s.st_size - BITMAP_SKIP, real_filename);
634 free (real_filename);
636 r = define_standard_resource (&resources, RT_BITMAP, id,
637 resinfo->language, 0);
639 r->type = RES_TYPE_BITMAP;
640 r->u.data.length = s.st_size - BITMAP_SKIP;
641 r->u.data.data = data;
642 r->res_info = *resinfo;
645 /* Define a cursor resource. A cursor file may contain a set of
646 bitmaps, each representing the same cursor at various different
647 resolutions. They each get written out with a different ID. The
648 real cursor resource is then a group resource which can be used to
649 select one of the actual cursors. */
652 define_cursor (struct res_id id, const struct res_res_info *resinfo,
653 const char *filename)
658 struct icondir *icondirs;
660 struct res_resource *r;
661 struct group_cursor *first, **pp;
663 e = open_file_search (filename, FOPEN_RB, "cursor file", &real_filename);
665 /* A cursor file is basically an icon file. The start of the file
666 is a three word structure. The first word is ignored. The
667 second word is the type of data. The third word is the number of
670 get_word (e, real_filename);
671 type = get_word (e, real_filename);
672 count = get_word (e, real_filename);
674 fatal (_("cursor file `%s' does not contain cursor data"), real_filename);
676 /* Read in the icon directory entries. */
678 icondirs = (struct icondir *) xmalloc (count * sizeof *icondirs);
680 for (i = 0; i < count; i++)
682 icondirs[i].width = getc (e);
683 icondirs[i].height = getc (e);
684 icondirs[i].colorcount = getc (e);
686 icondirs[i].u.cursor.xhotspot = get_word (e, real_filename);
687 icondirs[i].u.cursor.yhotspot = get_word (e, real_filename);
688 icondirs[i].bytes = get_long (e, real_filename);
689 icondirs[i].offset = get_long (e, real_filename);
692 unexpected_eof (real_filename);
695 /* Define each cursor as a unique resource. */
697 first_cursor = cursors;
699 for (i = 0; i < count; i++)
705 if (fseek (e, icondirs[i].offset, SEEK_SET) != 0)
706 fatal (_("%s: fseek to %lu failed: %s"), real_filename,
707 icondirs[i].offset, strerror (errno));
709 data = (unsigned char *) res_alloc (icondirs[i].bytes);
711 get_data (e, data, icondirs[i].bytes, real_filename);
713 c = (struct cursor *) res_alloc (sizeof *c);
714 c->xhotspot = icondirs[i].u.cursor.xhotspot;
715 c->yhotspot = icondirs[i].u.cursor.yhotspot;
716 c->length = icondirs[i].bytes;
724 r = define_standard_resource (&resources, RT_CURSOR, name,
725 resinfo->language, 0);
726 r->type = RES_TYPE_CURSOR;
728 r->res_info = *resinfo;
732 free (real_filename);
734 /* Define a cursor group resource. */
738 for (i = 0; i < count; i++)
740 struct group_cursor *cg;
742 cg = (struct group_cursor *) res_alloc (sizeof *cg);
744 cg->width = icondirs[i].width;
745 cg->height = 2 * icondirs[i].height;
747 /* FIXME: What should these be set to? */
751 cg->bytes = icondirs[i].bytes + 4;
752 cg->index = first_cursor + i + 1;
760 r = define_standard_resource (&resources, RT_GROUP_CURSOR, id,
761 resinfo->language, 0);
762 r->type = RES_TYPE_GROUP_CURSOR;
763 r->u.group_cursor = first;
764 r->res_info = *resinfo;
767 /* Define a dialog resource. */
770 define_dialog (struct res_id id, const struct res_res_info *resinfo,
771 const struct dialog *dialog)
774 struct res_resource *r;
776 copy = (struct dialog *) res_alloc (sizeof *copy);
779 r = define_standard_resource (&resources, RT_DIALOG, id,
780 resinfo->language, 0);
781 r->type = RES_TYPE_DIALOG;
783 r->res_info = *resinfo;
786 /* Define a dialog control. This does not define a resource, but
787 merely allocates and fills in a structure. */
789 struct dialog_control *
790 define_control (const struct res_id iid, unsigned long id, unsigned long x,
791 unsigned long y, unsigned long width, unsigned long height,
792 unsigned long class, unsigned long style,
793 unsigned long exstyle)
795 struct dialog_control *n;
797 n = (struct dialog_control *) res_alloc (sizeof *n);
801 n->exstyle = exstyle;
807 n->class.u.id = class;
815 struct dialog_control *
816 define_icon_control (struct res_id iid, unsigned long id, unsigned long x,
817 unsigned long y, unsigned long style,
818 unsigned long exstyle, unsigned long help,
819 struct rcdata_item *data, struct dialog_ex *ex)
821 struct dialog_control *n;
825 style = SS_ICON | WS_CHILD | WS_VISIBLE;
826 res_string_to_id (&tid, "");
827 n = define_control (tid, id, x, y, 0, 0, CTL_STATIC, style, exstyle);
830 rcparse_warning (_("help ID requires DIALOGEX"));
832 rcparse_warning (_("control data requires DIALOGEX"));
839 /* Define a font resource. */
842 define_font (struct res_id id, const struct res_res_info *resinfo,
843 const char *filename)
849 struct res_resource *r;
852 unsigned char *fontdata;
854 const char *device, *face;
857 e = open_file_search (filename, FOPEN_RB, "font file", &real_filename);
859 if (stat (real_filename, &s) < 0)
860 fatal (_("stat failed on bitmap file `%s': %s"), real_filename,
863 data = (unsigned char *) res_alloc (s.st_size);
865 get_data (e, data, s.st_size, real_filename);
868 free (real_filename);
870 r = define_standard_resource (&resources, RT_FONT, id,
871 resinfo->language, 0);
873 r->type = RES_TYPE_FONT;
874 r->u.data.length = s.st_size;
875 r->u.data.data = data;
876 r->res_info = *resinfo;
878 /* For each font resource, we must add an entry in the FONTDIR
879 resource. The FONTDIR resource includes some strings in the font
880 file. To find them, we have to do some magic on the data we have
883 offset = ((((((data[47] << 8)
887 if (offset > 0 && offset < s.st_size)
888 device = (char *) data + offset;
892 offset = ((((((data[51] << 8)
896 if (offset > 0 && offset < s.st_size)
897 face = (char *) data + offset;
903 fontdatalength = 58 + strlen (device) + strlen (face);
904 fontdata = (unsigned char *) res_alloc (fontdatalength);
905 memcpy (fontdata, data, 56);
906 strcpy ((char *) fontdata + 56, device);
907 strcpy ((char *) fontdata + 57 + strlen (device), face);
909 fd = (struct fontdir *) res_alloc (sizeof *fd);
912 fd->length = fontdatalength;
915 for (pp = &fontdirs; *pp != NULL; pp = &(*pp)->next)
919 /* For the single fontdirs resource, we always use the resource
920 information of the last font. I don't know what else to do. */
921 fontdirs_resinfo = *resinfo;
924 /* Define the fontdirs resource. This is called after the entire rc
925 file has been parsed, if any font resources were seen. */
928 define_fontdirs (void)
930 struct res_resource *r;
936 r = define_standard_resource (&resources, RT_FONTDIR, id, 0x409, 0);
938 r->type = RES_TYPE_FONTDIR;
939 r->u.fontdir = fontdirs;
940 r->res_info = fontdirs_resinfo;
943 /* Define an icon resource. An icon file may contain a set of
944 bitmaps, each representing the same icon at various different
945 resolutions. They each get written out with a different ID. The
946 real icon resource is then a group resource which can be used to
947 select one of the actual icon bitmaps. */
950 define_icon (struct res_id id, const struct res_res_info *resinfo,
951 const char *filename)
956 struct icondir *icondirs;
958 struct res_resource *r;
959 struct group_icon *first, **pp;
961 e = open_file_search (filename, FOPEN_RB, "icon file", &real_filename);
963 /* The start of an icon file is a three word structure. The first
964 word is ignored. The second word is the type of data. The third
965 word is the number of entries. */
967 get_word (e, real_filename);
968 type = get_word (e, real_filename);
969 count = get_word (e, real_filename);
971 fatal (_("icon file `%s' does not contain icon data"), real_filename);
973 /* Read in the icon directory entries. */
975 icondirs = (struct icondir *) xmalloc (count * sizeof *icondirs);
977 for (i = 0; i < count; i++)
979 icondirs[i].width = getc (e);
980 icondirs[i].height = getc (e);
981 icondirs[i].colorcount = getc (e);
983 icondirs[i].u.icon.planes = get_word (e, real_filename);
984 icondirs[i].u.icon.bits = get_word (e, real_filename);
985 icondirs[i].bytes = get_long (e, real_filename);
986 icondirs[i].offset = get_long (e, real_filename);
989 unexpected_eof (real_filename);
992 /* Define each icon as a unique resource. */
996 for (i = 0; i < count; i++)
1001 if (fseek (e, icondirs[i].offset, SEEK_SET) != 0)
1002 fatal (_("%s: fseek to %lu failed: %s"), real_filename,
1003 icondirs[i].offset, strerror (errno));
1005 data = (unsigned char *) res_alloc (icondirs[i].bytes);
1007 get_data (e, data, icondirs[i].bytes, real_filename);
1014 r = define_standard_resource (&resources, RT_ICON, name,
1015 resinfo->language, 0);
1016 r->type = RES_TYPE_ICON;
1017 r->u.data.length = icondirs[i].bytes;
1018 r->u.data.data = data;
1019 r->res_info = *resinfo;
1023 free (real_filename);
1025 /* Define an icon group resource. */
1029 for (i = 0; i < count; i++)
1031 struct group_icon *cg;
1033 /* For some reason, at least in some files the planes and bits
1034 are zero. We instead set them from the color. This is
1037 cg = (struct group_icon *) res_alloc (sizeof *cg);
1039 cg->width = icondirs[i].width;
1040 cg->height = icondirs[i].height;
1041 cg->colors = icondirs[i].colorcount;
1043 if (icondirs[i].u.icon.planes)
1044 cg->planes = icondirs[i].u.icon.planes;
1048 if (icondirs[i].u.icon.bits)
1049 cg->bits = icondirs[i].u.icon.bits;
1054 while ((1L << cg->bits) < cg->colors)
1058 cg->bytes = icondirs[i].bytes;
1059 cg->index = first_icon + i + 1;
1067 r = define_standard_resource (&resources, RT_GROUP_ICON, id,
1068 resinfo->language, 0);
1069 r->type = RES_TYPE_GROUP_ICON;
1070 r->u.group_icon = first;
1071 r->res_info = *resinfo;
1074 /* Define a menu resource. */
1077 define_menu (struct res_id id, const struct res_res_info *resinfo,
1078 struct menuitem *menuitems)
1081 struct res_resource *r;
1083 m = (struct menu *) res_alloc (sizeof *m);
1084 m->items = menuitems;
1087 r = define_standard_resource (&resources, RT_MENU, id, resinfo->language, 0);
1088 r->type = RES_TYPE_MENU;
1090 r->res_info = *resinfo;
1093 /* Define a menu item. This does not define a resource, but merely
1094 allocates and fills in a structure. */
1097 define_menuitem (const char *text, int menuid, unsigned long type,
1098 unsigned long state, unsigned long help,
1099 struct menuitem *menuitems)
1101 struct menuitem *mi;
1103 mi = (struct menuitem *) res_alloc (sizeof *mi);
1111 unicode_from_ascii ((int *) NULL, &mi->text, text);
1113 mi->popup = menuitems;
1117 /* Define a messagetable resource. */
1120 define_messagetable (struct res_id id, const struct res_res_info *resinfo,
1121 const char *filename)
1124 char *real_filename;
1126 unsigned char *data;
1127 struct res_resource *r;
1129 e = open_file_search (filename, FOPEN_RB, "messagetable file",
1132 if (stat (real_filename, &s) < 0)
1133 fatal (_("stat failed on bitmap file `%s': %s"), real_filename,
1136 data = (unsigned char *) res_alloc (s.st_size);
1138 get_data (e, data, s.st_size, real_filename);
1141 free (real_filename);
1143 r = define_standard_resource (&resources, RT_MESSAGETABLE, id,
1144 resinfo->language, 0);
1146 r->type = RES_TYPE_MESSAGETABLE;
1147 r->u.data.length = s.st_size;
1148 r->u.data.data = data;
1149 r->res_info = *resinfo;
1152 /* Define an rcdata resource. */
1155 define_rcdata (struct res_id id, const struct res_res_info *resinfo,
1156 struct rcdata_item *data)
1158 struct res_resource *r;
1160 r = define_standard_resource (&resources, RT_RCDATA, id,
1161 resinfo->language, 0);
1162 r->type = RES_TYPE_RCDATA;
1164 r->res_info = *resinfo;
1167 /* Create an rcdata item holding a string. */
1169 struct rcdata_item *
1170 define_rcdata_string (const char *string, unsigned long len)
1172 struct rcdata_item *ri;
1175 ri = (struct rcdata_item *) res_alloc (sizeof *ri);
1177 ri->type = RCDATA_STRING;
1178 ri->u.string.length = len;
1179 s = (char *) res_alloc (len);
1180 memcpy (s, string, len);
1186 /* Create an rcdata item holding a number. */
1188 struct rcdata_item *
1189 define_rcdata_number (unsigned long val, int dword)
1191 struct rcdata_item *ri;
1193 ri = (struct rcdata_item *) res_alloc (sizeof *ri);
1195 ri->type = dword ? RCDATA_DWORD : RCDATA_WORD;
1201 /* Define a stringtable resource. This is called for each string
1202 which appears in a STRINGTABLE statement. */
1205 define_stringtable (const struct res_res_info *resinfo,
1206 unsigned long stringid, const char *string)
1209 struct res_resource *r;
1212 id.u.id = (stringid >> 4) + 1;
1213 r = define_standard_resource (&resources, RT_STRING, id,
1214 resinfo->language, 1);
1216 if (r->type == RES_TYPE_UNINITIALIZED)
1220 r->type = RES_TYPE_STRINGTABLE;
1221 r->u.stringtable = ((struct stringtable *)
1222 res_alloc (sizeof (struct stringtable)));
1223 for (i = 0; i < 16; i++)
1225 r->u.stringtable->strings[i].length = 0;
1226 r->u.stringtable->strings[i].string = NULL;
1229 r->res_info = *resinfo;
1232 unicode_from_ascii (&r->u.stringtable->strings[stringid & 0xf].length,
1233 &r->u.stringtable->strings[stringid & 0xf].string,
1237 /* Define a user data resource where the data is in the rc file. */
1240 define_user_data (struct res_id id, struct res_id type,
1241 const struct res_res_info *resinfo,
1242 struct rcdata_item *data)
1244 struct res_id ids[3];
1245 struct res_resource *r;
1250 ids[2].u.id = resinfo->language;
1252 r = define_resource (&resources, 3, ids, 0);
1253 r->type = RES_TYPE_USERDATA;
1254 r->u.userdata = data;
1255 r->res_info = *resinfo;
1258 /* Define a user data resource where the data is in a file. */
1261 define_user_file (struct res_id id, struct res_id type,
1262 const struct res_res_info *resinfo, const char *filename)
1265 char *real_filename;
1267 unsigned char *data;
1268 struct res_id ids[3];
1269 struct res_resource *r;
1271 e = open_file_search (filename, FOPEN_RB, "font file", &real_filename);
1273 if (stat (real_filename, &s) < 0)
1274 fatal (_("stat failed on bitmap file `%s': %s"), real_filename,
1277 data = (unsigned char *) res_alloc (s.st_size);
1279 get_data (e, data, s.st_size, real_filename);
1282 free (real_filename);
1287 ids[2].u.id = resinfo->language;
1289 r = define_resource (&resources, 3, ids, 0);
1290 r->type = RES_TYPE_USERDATA;
1291 r->u.userdata = ((struct rcdata_item *)
1292 res_alloc (sizeof (struct rcdata_item)));
1293 r->u.userdata->next = NULL;
1294 r->u.userdata->type = RCDATA_BUFFER;
1295 r->u.userdata->u.buffer.length = s.st_size;
1296 r->u.userdata->u.buffer.data = data;
1297 r->res_info = *resinfo;
1300 /* Define a versioninfo resource. */
1303 define_versioninfo (struct res_id id, int language,
1304 struct fixed_versioninfo *fixedverinfo,
1305 struct ver_info *verinfo)
1307 struct res_resource *r;
1309 r = define_standard_resource (&resources, RT_VERSION, id, language, 0);
1310 r->type = RES_TYPE_VERSIONINFO;
1311 r->u.versioninfo = ((struct versioninfo *)
1312 res_alloc (sizeof (struct versioninfo)));
1313 r->u.versioninfo->fixed = fixedverinfo;
1314 r->u.versioninfo->var = verinfo;
1315 r->res_info.language = language;
1318 /* Add string version info to a list of version information. */
1321 append_ver_stringfileinfo (struct ver_info *verinfo, const char *language,
1322 struct ver_stringinfo *strings)
1324 struct ver_info *vi, **pp;
1326 vi = (struct ver_info *) res_alloc (sizeof *vi);
1328 vi->type = VERINFO_STRING;
1329 unicode_from_ascii ((int *) NULL, &vi->u.string.language, language);
1330 vi->u.string.strings = strings;
1332 for (pp = &verinfo; *pp != NULL; pp = &(*pp)->next)
1339 /* Add variable version info to a list of version information. */
1342 append_ver_varfileinfo (struct ver_info *verinfo, const char *key,
1343 struct ver_varinfo *var)
1345 struct ver_info *vi, **pp;
1347 vi = (struct ver_info *) res_alloc (sizeof *vi);
1349 vi->type = VERINFO_VAR;
1350 unicode_from_ascii ((int *) NULL, &vi->u.var.key, key);
1351 vi->u.var.var = var;
1353 for (pp = &verinfo; *pp != NULL; pp = &(*pp)->next)
1360 /* Append version string information to a list. */
1362 struct ver_stringinfo *
1363 append_verval (struct ver_stringinfo *strings, const char *key,
1366 struct ver_stringinfo *vs, **pp;
1368 vs = (struct ver_stringinfo *) res_alloc (sizeof *vs);
1370 unicode_from_ascii ((int *) NULL, &vs->key, key);
1371 unicode_from_ascii ((int *) NULL, &vs->value, value);
1373 for (pp = &strings; *pp != NULL; pp = &(*pp)->next)
1380 /* Append version variable information to a list. */
1382 struct ver_varinfo *
1383 append_vertrans (struct ver_varinfo *var, unsigned long language,
1384 unsigned long charset)
1386 struct ver_varinfo *vv, **pp;
1388 vv = (struct ver_varinfo *) res_alloc (sizeof *vv);
1390 vv->language = language;
1391 vv->charset = charset;
1393 for (pp = &var; *pp != NULL; pp = &(*pp)->next)
1400 /* Local functions used to write out an rc file. */
1402 static void indent (FILE *, int);
1403 static void write_rc_directory
1404 (FILE *, const struct res_directory *, const struct res_id *,
1405 const struct res_id *, int *, int);
1406 static void write_rc_subdir
1407 (FILE *, const struct res_entry *, const struct res_id *,
1408 const struct res_id *, int *, int);
1409 static void write_rc_resource
1410 (FILE *, const struct res_id *, const struct res_id *,
1411 const struct res_resource *, int *);
1412 static void write_rc_accelerators (FILE *, const struct accelerator *);
1413 static void write_rc_cursor (FILE *, const struct cursor *);
1414 static void write_rc_group_cursor (FILE *, const struct group_cursor *);
1415 static void write_rc_dialog (FILE *, const struct dialog *);
1416 static void write_rc_dialog_control (FILE *, const struct dialog_control *);
1417 static void write_rc_fontdir (FILE *, const struct fontdir *);
1418 static void write_rc_group_icon (FILE *, const struct group_icon *);
1419 static void write_rc_menu (FILE *, const struct menu *, int);
1420 static void write_rc_menuitems (FILE *, const struct menuitem *, int, int);
1421 static void write_rc_rcdata (FILE *, const struct rcdata_item *, int);
1422 static void write_rc_stringtable
1423 (FILE *, const struct res_id *, const struct stringtable *);
1424 static void write_rc_versioninfo (FILE *, const struct versioninfo *);
1425 static void write_rc_filedata (FILE *, unsigned long, const unsigned char *);
1427 /* Indent a given number of spaces. */
1430 indent (FILE *e, int c)
1434 for (i = 0; i < c; i++)
1438 /* Dump the resources we have read in the format of an rc file.
1440 Actually, we don't use the format of an rc file, because it's way
1441 too much of a pain--for example, we'd have to write icon resources
1442 into a file and refer to that file. We just generate a readable
1443 format that kind of looks like an rc file, and is useful for
1444 understanding the contents of a resource file. Someday we may want
1445 to generate an rc file which the rc compiler can read; if that day
1446 comes, this code will have to be fixed up. */
1449 write_rc_file (const char *filename, const struct res_directory *resources)
1454 if (filename == NULL)
1458 e = fopen (filename, FOPEN_WT);
1460 fatal (_("can't open `%s' for output: %s"), filename, strerror (errno));
1464 write_rc_directory (e, resources, (const struct res_id *) NULL,
1465 (const struct res_id *) NULL, &language, 1);
1468 /* Write out a directory. E is the file to write to. RD is the
1469 directory. TYPE is a pointer to the level 1 ID which serves as the
1470 resource type. NAME is a pointer to the level 2 ID which serves as
1471 an individual resource name. LANGUAGE is a pointer to the current
1472 language. LEVEL is the level in the tree. */
1475 write_rc_directory (FILE *e, const struct res_directory *rd,
1476 const struct res_id *type, const struct res_id *name,
1477 int *language, int level)
1479 const struct res_entry *re;
1481 /* Print out some COFF information that rc files can't represent. */
1484 fprintf (e, "// Time stamp: %lu\n", rd->time);
1485 if (rd->characteristics != 0)
1486 fprintf (e, "// Characteristics: %lu\n", rd->characteristics);
1487 if (rd->major != 0 || rd->minor != 0)
1488 fprintf (e, "// Version: %d %d\n", rd->major, rd->minor);
1490 for (re = rd->entries; re != NULL; re = re->next)
1495 /* If we're at level 1, the key of this resource is the
1496 type. This normally duplicates the information we have
1497 stored with the resource itself, but we need to remember
1498 the type if this is a user define resource type. */
1503 /* If we're at level 2, the key of this resource is the name
1504 we are going to use in the rc printout. */
1509 /* If we're at level 3, then this key represents a language.
1510 Use it to update the current language. */
1512 && re->id.u.id != (unsigned long) (unsigned int) *language
1513 && (re->id.u.id & 0xffff) == re->id.u.id)
1515 fprintf (e, "LANGUAGE %lu, %lu\n",
1516 re->id.u.id & ((1 << SUBLANG_SHIFT) - 1),
1517 (re->id.u.id >> SUBLANG_SHIFT) & 0xff);
1518 *language = re->id.u.id;
1527 write_rc_subdir (e, re, type, name, language, level);
1532 /* This is the normal case: the three levels are
1533 TYPE/NAME/LANGUAGE. NAME will have been set at level
1534 2, and represents the name to use. We probably just
1535 set LANGUAGE, and it will probably match what the
1536 resource itself records if anything. */
1537 write_rc_resource (e, type, name, re->u.res, language);
1541 fprintf (e, "// Resource at unexpected level %d\n", level);
1542 write_rc_resource (e, type, (struct res_id *) NULL, re->u.res,
1549 /* Write out a subdirectory entry. E is the file to write to. RE is
1550 the subdirectory entry. TYPE and NAME are pointers to higher level
1551 IDs, or NULL. LANGUAGE is a pointer to the current language.
1552 LEVEL is the level in the tree. */
1555 write_rc_subdir (FILE *e, const struct res_entry *re,
1556 const struct res_id *type, const struct res_id *name,
1557 int *language, int level)
1563 fprintf (e, "// Type: ");
1565 res_id_print (e, re->id, 1);
1570 switch (re->id.u.id)
1572 case RT_CURSOR: s = "cursor"; break;
1573 case RT_BITMAP: s = "bitmap"; break;
1574 case RT_ICON: s = "icon"; break;
1575 case RT_MENU: s = "menu"; break;
1576 case RT_DIALOG: s = "dialog"; break;
1577 case RT_STRING: s = "stringtable"; break;
1578 case RT_FONTDIR: s = "fontdir"; break;
1579 case RT_FONT: s = "font"; break;
1580 case RT_ACCELERATOR: s = "accelerators"; break;
1581 case RT_RCDATA: s = "rcdata"; break;
1582 case RT_MESSAGETABLE: s = "messagetable"; break;
1583 case RT_GROUP_CURSOR: s = "group cursor"; break;
1584 case RT_GROUP_ICON: s = "group icon"; break;
1585 case RT_VERSION: s = "version"; break;
1586 case RT_DLGINCLUDE: s = "dlginclude"; break;
1587 case RT_PLUGPLAY: s = "plugplay"; break;
1588 case RT_VXD: s = "vxd"; break;
1589 case RT_ANICURSOR: s = "anicursor"; break;
1590 case RT_ANIICON: s = "aniicon"; break;
1591 default: s = NULL; break;
1595 fprintf (e, "%s", s);
1597 res_id_print (e, re->id, 1);
1603 fprintf (e, "// Name: ");
1604 res_id_print (e, re->id, 1);
1609 fprintf (e, "// Language: ");
1610 res_id_print (e, re->id, 1);
1615 fprintf (e, "// Level %d: ", level);
1616 res_id_print (e, re->id, 1);
1620 write_rc_directory (e, re->u.dir, type, name, language, level + 1);
1623 /* Write out a single resource. E is the file to write to. TYPE is a
1624 pointer to the type of the resource. NAME is a pointer to the name
1625 of the resource; it will be NULL if there is a level mismatch. RES
1626 is the resource data. LANGUAGE is a pointer to the current
1630 write_rc_resource (FILE *e, const struct res_id *type,
1631 const struct res_id *name, const struct res_resource *res,
1645 case RES_TYPE_ACCELERATOR:
1647 rt = RT_ACCELERATOR;
1650 case RES_TYPE_BITMAP:
1655 case RES_TYPE_CURSOR:
1660 case RES_TYPE_GROUP_CURSOR:
1662 rt = RT_GROUP_CURSOR;
1665 case RES_TYPE_DIALOG:
1666 if (extended_dialog (res->u.dialog))
1678 case RES_TYPE_FONTDIR:
1688 case RES_TYPE_GROUP_ICON:
1694 if (extended_menu (res->u.menu))
1707 case RES_TYPE_MESSAGETABLE:
1709 rt = RT_MESSAGETABLE;
1712 case RES_TYPE_RCDATA:
1717 case RES_TYPE_STRINGTABLE:
1722 case RES_TYPE_USERDATA:
1727 case RES_TYPE_VERSIONINFO:
1735 && (type->named || type->u.id != (unsigned long) rt))
1737 fprintf (e, "// Unexpected resource type mismatch: ");
1738 res_id_print (e, *type, 1);
1739 fprintf (e, " != %d", rt);
1742 if (res->coff_info.codepage != 0)
1743 fprintf (e, "// Code page: %lu\n", res->coff_info.codepage);
1744 if (res->coff_info.reserved != 0)
1745 fprintf (e, "// COFF reserved value: %lu\n", res->coff_info.reserved);
1748 res_id_print (e, *name, 0);
1750 fprintf (e, "??Unknown-Name??");
1754 fprintf (e, "%s", s);
1755 else if (type != NULL)
1756 res_id_print (e, *type, 0);
1758 fprintf (e, "??Unknown-Type??");
1760 if (res->res_info.memflags != 0)
1762 if ((res->res_info.memflags & MEMFLAG_MOVEABLE) != 0)
1763 fprintf (e, " MOVEABLE");
1764 if ((res->res_info.memflags & MEMFLAG_PURE) != 0)
1765 fprintf (e, " PURE");
1766 if ((res->res_info.memflags & MEMFLAG_PRELOAD) != 0)
1767 fprintf (e, " PRELOAD");
1768 if ((res->res_info.memflags & MEMFLAG_DISCARDABLE) != 0)
1769 fprintf (e, " DISCARDABLE");
1772 if (res->type == RES_TYPE_DIALOG)
1774 fprintf (e, " %d, %d, %d, %d", res->u.dialog->x, res->u.dialog->y,
1775 res->u.dialog->width, res->u.dialog->height);
1776 if (res->u.dialog->ex != NULL
1777 && res->u.dialog->ex->help != 0)
1778 fprintf (e, ", %lu", res->u.dialog->ex->help);
1783 if ((res->res_info.language != 0 && res->res_info.language != *language)
1784 || res->res_info.characteristics != 0
1785 || res->res_info.version != 0)
1791 case RES_TYPE_ACCELERATOR:
1792 case RES_TYPE_DIALOG:
1794 case RES_TYPE_RCDATA:
1795 case RES_TYPE_STRINGTABLE:
1804 if (res->res_info.language != 0 && res->res_info.language != *language)
1805 fprintf (e, "%sLANGUAGE %d, %d\n",
1806 modifiers ? "// " : "",
1807 res->res_info.language & ((1<<SUBLANG_SHIFT)-1),
1808 (res->res_info.language >> SUBLANG_SHIFT) & 0xff);
1809 if (res->res_info.characteristics != 0)
1810 fprintf (e, "%sCHARACTERISTICS %lu\n",
1811 modifiers ? "// " : "",
1812 res->res_info.characteristics);
1813 if (res->res_info.version != 0)
1814 fprintf (e, "%sVERSION %lu\n",
1815 modifiers ? "// " : "",
1816 res->res_info.version);
1824 case RES_TYPE_ACCELERATOR:
1825 write_rc_accelerators (e, res->u.acc);
1828 case RES_TYPE_CURSOR:
1829 write_rc_cursor (e, res->u.cursor);
1832 case RES_TYPE_GROUP_CURSOR:
1833 write_rc_group_cursor (e, res->u.group_cursor);
1836 case RES_TYPE_DIALOG:
1837 write_rc_dialog (e, res->u.dialog);
1840 case RES_TYPE_FONTDIR:
1841 write_rc_fontdir (e, res->u.fontdir);
1844 case RES_TYPE_GROUP_ICON:
1845 write_rc_group_icon (e, res->u.group_icon);
1849 write_rc_menu (e, res->u.menu, menuex);
1852 case RES_TYPE_RCDATA:
1853 write_rc_rcdata (e, res->u.rcdata, 0);
1856 case RES_TYPE_STRINGTABLE:
1857 write_rc_stringtable (e, name, res->u.stringtable);
1860 case RES_TYPE_USERDATA:
1861 write_rc_rcdata (e, res->u.userdata, 0);
1864 case RES_TYPE_VERSIONINFO:
1865 write_rc_versioninfo (e, res->u.versioninfo);
1868 case RES_TYPE_BITMAP:
1871 case RES_TYPE_MESSAGETABLE:
1872 write_rc_filedata (e, res->u.data.length, res->u.data.data);
1877 /* Write out accelerator information. */
1880 write_rc_accelerators (FILE *e, const struct accelerator *accelerators)
1882 const struct accelerator *acc;
1884 fprintf (e, "BEGIN\n");
1885 for (acc = accelerators; acc != NULL; acc = acc->next)
1891 if ((acc->key & 0x7f) == acc->key
1892 && ISPRINT (acc->key)
1893 && (acc->flags & ACC_VIRTKEY) == 0)
1895 fprintf (e, "\"%c\"", acc->key);
1900 fprintf (e, "%d", acc->key);
1904 fprintf (e, ", %d", acc->id);
1908 if ((acc->flags & ACC_VIRTKEY) != 0)
1909 fprintf (e, ", VIRTKEY");
1911 fprintf (e, ", ASCII");
1914 if ((acc->flags & ACC_SHIFT) != 0)
1915 fprintf (e, ", SHIFT");
1916 if ((acc->flags & ACC_CONTROL) != 0)
1917 fprintf (e, ", CONTROL");
1918 if ((acc->flags & ACC_ALT) != 0)
1919 fprintf (e, ", ALT");
1924 fprintf (e, "END\n");
1927 /* Write out cursor information. This would normally be in a separate
1928 file, which the rc file would include. */
1931 write_rc_cursor (FILE *e, const struct cursor *cursor)
1933 fprintf (e, "// Hotspot: x: %d; y: %d\n", cursor->xhotspot,
1935 write_rc_filedata (e, cursor->length, cursor->data);
1938 /* Write out group cursor data. This would normally be built from the
1942 write_rc_group_cursor (FILE *e, const struct group_cursor *group_cursor)
1944 const struct group_cursor *gc;
1946 for (gc = group_cursor; gc != NULL; gc = gc->next)
1948 fprintf (e, "// width: %d; height %d; planes %d; bits %d\n",
1949 gc->width, gc->height, gc->planes, gc->bits);
1950 fprintf (e, "// data bytes: %lu; index: %d\n",
1951 gc->bytes, gc->index);
1955 /* Write dialog data. */
1958 write_rc_dialog (FILE *e, const struct dialog *dialog)
1960 const struct dialog_control *control;
1962 fprintf (e, "STYLE 0x%lx\n", dialog->style);
1964 if (dialog->exstyle != 0)
1965 fprintf (e, "EXSTYLE 0x%lx\n", dialog->exstyle);
1967 if ((dialog->class.named && dialog->class.u.n.length > 0)
1968 || dialog->class.u.id != 0)
1970 fprintf (e, "CLASS ");
1971 res_id_print (e, dialog->class, 1);
1975 if (dialog->caption != NULL)
1977 fprintf (e, "CAPTION \"");
1978 unicode_print (e, dialog->caption, -1);
1979 fprintf (e, "\"\n");
1982 if ((dialog->menu.named && dialog->menu.u.n.length > 0)
1983 || dialog->menu.u.id != 0)
1985 fprintf (e, "MENU ");
1986 res_id_print (e, dialog->menu, 0);
1990 if (dialog->font != NULL)
1992 fprintf (e, "FONT %d, \"", dialog->pointsize);
1993 unicode_print (e, dialog->font, -1);
1995 if (dialog->ex != NULL
1996 && (dialog->ex->weight != 0
1997 || dialog->ex->italic != 0
1998 || dialog->ex->charset != 1))
1999 fprintf (e, ", %d, %d, %d",
2000 dialog->ex->weight, dialog->ex->italic, dialog->ex->charset);
2004 fprintf (e, "BEGIN\n");
2006 for (control = dialog->controls; control != NULL; control = control->next)
2007 write_rc_dialog_control (e, control);
2009 fprintf (e, "END\n");
2012 /* For each predefined control keyword, this table provides the class
2018 unsigned short class;
2019 unsigned long style;
2022 static const struct control_info control_info[] =
2024 { "AUTO3STATE", CTL_BUTTON, BS_AUTO3STATE },
2025 { "AUTOCHECKBOX", CTL_BUTTON, BS_AUTOCHECKBOX },
2026 { "AUTORADIOBUTTON", CTL_BUTTON, BS_AUTORADIOBUTTON },
2027 { "CHECKBOX", CTL_BUTTON, BS_CHECKBOX },
2028 { "COMBOBOX", CTL_COMBOBOX, (unsigned long) -1 },
2029 { "CTEXT", CTL_STATIC, SS_CENTER },
2030 { "DEFPUSHBUTTON", CTL_BUTTON, BS_DEFPUSHBUTTON },
2031 { "EDITTEXT", CTL_EDIT, (unsigned long) -1 },
2032 { "GROUPBOX", CTL_BUTTON, BS_GROUPBOX },
2033 { "ICON", CTL_STATIC, SS_ICON },
2034 { "LISTBOX", CTL_LISTBOX, (unsigned long) -1 },
2035 { "LTEXT", CTL_STATIC, SS_LEFT },
2036 { "PUSHBOX", CTL_BUTTON, BS_PUSHBOX },
2037 { "PUSHBUTTON", CTL_BUTTON, BS_PUSHBUTTON },
2038 { "RADIOBUTTON", CTL_BUTTON, BS_RADIOBUTTON },
2039 { "RTEXT", CTL_STATIC, SS_RIGHT },
2040 { "SCROLLBAR", CTL_SCROLLBAR, (unsigned long) -1 },
2041 { "STATE3", CTL_BUTTON, BS_3STATE },
2042 /* It's important that USERBUTTON come after all the other button
2043 types, so that it won't be matched too early. */
2044 { "USERBUTTON", CTL_BUTTON, (unsigned long) -1 },
2048 /* Write a dialog control. */
2051 write_rc_dialog_control (FILE *e, const struct dialog_control *control)
2053 const struct control_info *ci;
2057 if (control->class.named)
2061 for (ci = control_info; ci->name != NULL; ++ci)
2062 if (ci->class == control->class.u.id
2063 && (ci->style == (unsigned long) -1
2064 || ci->style == (control->style & 0xff)))
2068 fprintf (e, "CONTROL");
2069 else if (ci->name != NULL)
2070 fprintf (e, "%s", ci->name);
2072 fprintf (e, "CONTROL");
2074 if (control->text.named || control->text.u.id != 0)
2077 res_id_print (e, control->text, 1);
2081 fprintf (e, " %d, ", control->id);
2085 if (control->class.named)
2087 res_id_print (e, control->class, 0);
2088 if (control->class.named)
2090 fprintf (e, ", 0x%lx, ", control->style);
2093 fprintf (e, "%d, %d", control->x, control->y);
2095 if (control->style != SS_ICON
2096 || control->exstyle != 0
2097 || control->width != 0
2098 || control->height != 0
2099 || control->help != 0)
2101 fprintf (e, ", %d, %d", control->width, control->height);
2103 /* FIXME: We don't need to print the style if it is the default.
2104 More importantly, in certain cases we actually need to turn
2105 off parts of the forced style, by using NOT. */
2106 fprintf (e, ", 0x%lx", control->style);
2108 if (control->exstyle != 0 || control->help != 0)
2109 fprintf (e, ", 0x%lx, %lu", control->exstyle, control->help);
2114 if (control->data != NULL)
2115 write_rc_rcdata (e, control->data, 2);
2118 /* Write out font directory data. This would normally be built from
2122 write_rc_fontdir (FILE *e, const struct fontdir *fontdir)
2124 const struct fontdir *fc;
2126 for (fc = fontdir; fc != NULL; fc = fc->next)
2128 fprintf (e, "// Font index: %d\n", fc->index);
2129 write_rc_filedata (e, fc->length, fc->data);
2133 /* Write out group icon data. This would normally be built from the
2137 write_rc_group_icon (FILE *e, const struct group_icon *group_icon)
2139 const struct group_icon *gi;
2141 for (gi = group_icon; gi != NULL; gi = gi->next)
2143 fprintf (e, "// width: %d; height %d; colors: %d; planes %d; bits %d\n",
2144 gi->width, gi->height, gi->colors, gi->planes, gi->bits);
2145 fprintf (e, "// data bytes: %lu; index: %d\n",
2146 gi->bytes, gi->index);
2150 /* Write out a menu resource. */
2153 write_rc_menu (FILE *e, const struct menu *menu, int menuex)
2155 if (menu->help != 0)
2156 fprintf (e, "// Help ID: %lu\n", menu->help);
2157 write_rc_menuitems (e, menu->items, menuex, 0);
2160 /* Write out menuitems. */
2163 write_rc_menuitems (FILE *e, const struct menuitem *menuitems, int menuex,
2166 const struct menuitem *mi;
2169 fprintf (e, "BEGIN\n");
2171 for (mi = menuitems; mi != NULL; mi = mi->next)
2173 indent (e, ind + 2);
2175 if (mi->popup == NULL)
2176 fprintf (e, "MENUITEM");
2178 fprintf (e, "POPUP");
2181 && mi->popup == NULL
2186 fprintf (e, " SEPARATOR\n");
2190 if (mi->text == NULL)
2191 fprintf (e, " \"\"");
2195 unicode_print (e, mi->text, -1);
2201 if (mi->popup == NULL)
2202 fprintf (e, ", %d", mi->id);
2204 if ((mi->type & MENUITEM_CHECKED) != 0)
2205 fprintf (e, ", CHECKED");
2206 if ((mi->type & MENUITEM_GRAYED) != 0)
2207 fprintf (e, ", GRAYED");
2208 if ((mi->type & MENUITEM_HELP) != 0)
2209 fprintf (e, ", HELP");
2210 if ((mi->type & MENUITEM_INACTIVE) != 0)
2211 fprintf (e, ", INACTIVE");
2212 if ((mi->type & MENUITEM_MENUBARBREAK) != 0)
2213 fprintf (e, ", MENUBARBREAK");
2214 if ((mi->type & MENUITEM_MENUBREAK) != 0)
2215 fprintf (e, ", MENUBREAK");
2219 if (mi->id != 0 || mi->type != 0 || mi->state != 0 || mi->help != 0)
2221 fprintf (e, ", %d", mi->id);
2222 if (mi->type != 0 || mi->state != 0 || mi->help != 0)
2224 fprintf (e, ", %lu", mi->type);
2225 if (mi->state != 0 || mi->help != 0)
2227 fprintf (e, ", %lu", mi->state);
2229 fprintf (e, ", %lu", mi->help);
2237 if (mi->popup != NULL)
2238 write_rc_menuitems (e, mi->popup, menuex, ind + 2);
2242 fprintf (e, "END\n");
2245 /* Write out an rcdata resource. This is also used for other types of
2246 resources that need to print arbitrary data. */
2249 write_rc_rcdata (FILE *e, const struct rcdata_item *rcdata, int ind)
2251 const struct rcdata_item *ri;
2254 fprintf (e, "BEGIN\n");
2256 for (ri = rcdata; ri != NULL; ri = ri->next)
2258 if (ri->type == RCDATA_BUFFER && ri->u.buffer.length == 0)
2261 indent (e, ind + 2);
2269 fprintf (e, "%d", ri->u.word);
2273 fprintf (e, "%luL", ri->u.dword);
2283 for (i = 0; i < ri->u.string.length; i++)
2288 fprintf (e, "\\%03o", *s);
2294 case RCDATA_WSTRING:
2296 unicode_print (e, ri->u.wstring.w, ri->u.wstring.length);
2305 /* Assume little endian data. */
2308 for (i = 0; i + 3 < ri->u.buffer.length; i += 4)
2314 indent (e, ind + 2);
2315 l = ((((((ri->u.buffer.data[i + 3] << 8)
2316 | ri->u.buffer.data[i + 2]) << 8)
2317 | ri->u.buffer.data[i + 1]) << 8)
2318 | ri->u.buffer.data[i]);
2319 fprintf (e, "%luL", l);
2320 if (i + 4 < ri->u.buffer.length || ri->next != NULL)
2322 for (j = 0; j < 4; ++j)
2323 if (! ISPRINT (ri->u.buffer.data[i + j])
2324 && ri->u.buffer.data[i + j] != 0)
2328 fprintf (e, "\t// ");
2329 for (j = 0; j < 4; ++j)
2331 if (! ISPRINT (ri->u.buffer.data[i + j]))
2332 fprintf (e, "\\%03o", ri->u.buffer.data[i + j]);
2335 if (ri->u.buffer.data[i + j] == '\\')
2337 fprintf (e, "%c", ri->u.buffer.data[i + j]);
2345 if (i + 1 < ri->u.buffer.length)
2351 indent (e, ind + 2);
2352 s = (ri->u.buffer.data[i + 1] << 8) | ri->u.buffer.data[i];
2353 fprintf (e, "%d", s);
2354 if (i + 2 < ri->u.buffer.length || ri->next != NULL)
2356 for (j = 0; j < 2; ++j)
2357 if (! ISPRINT (ri->u.buffer.data[i + j])
2358 && ri->u.buffer.data[i + j] != 0)
2362 fprintf (e, "\t// ");
2363 for (j = 0; j < 2; ++j)
2365 if (! ISPRINT (ri->u.buffer.data[i + j]))
2366 fprintf (e, "\\%03o", ri->u.buffer.data[i + j]);
2369 if (ri->u.buffer.data[i + j] == '\\')
2371 fprintf (e, "%c", ri->u.buffer.data[i + j]);
2380 if (i < ri->u.buffer.length)
2383 indent (e, ind + 2);
2384 if ((ri->u.buffer.data[i] & 0x7f) == ri->u.buffer.data[i]
2385 && ISPRINT (ri->u.buffer.data[i]))
2386 fprintf (e, "\"%c\"", ri->u.buffer.data[i]);
2388 fprintf (e, "\"\\%03o\"", ri->u.buffer.data[i]);
2389 if (ri->next != NULL)
2399 if (ri->type != RCDATA_BUFFER)
2401 if (ri->next != NULL)
2408 fprintf (e, "END\n");
2411 /* Write out a stringtable resource. */
2414 write_rc_stringtable (FILE *e, const struct res_id *name,
2415 const struct stringtable *stringtable)
2417 unsigned long offset;
2420 if (name != NULL && ! name->named)
2421 offset = (name->u.id - 1) << 4;
2424 fprintf (e, "// %s string table name\n",
2425 name == NULL ? "Missing" : "Invalid");
2429 fprintf (e, "BEGIN\n");
2431 for (i = 0; i < 16; i++)
2433 if (stringtable->strings[i].length != 0)
2435 fprintf (e, " %lu, \"", offset + i);
2436 unicode_print (e, stringtable->strings[i].string,
2437 stringtable->strings[i].length);
2438 fprintf (e, "\"\n");
2442 fprintf (e, "END\n");
2445 /* Write out a versioninfo resource. */
2448 write_rc_versioninfo (FILE *e, const struct versioninfo *versioninfo)
2450 const struct fixed_versioninfo *f;
2451 const struct ver_info *vi;
2453 f = versioninfo->fixed;
2454 if (f->file_version_ms != 0 || f->file_version_ls != 0)
2455 fprintf (e, " FILEVERSION %lu, %lu, %lu, %lu\n",
2456 (f->file_version_ms >> 16) & 0xffff,
2457 f->file_version_ms & 0xffff,
2458 (f->file_version_ls >> 16) & 0xffff,
2459 f->file_version_ls & 0xffff);
2460 if (f->product_version_ms != 0 || f->product_version_ls != 0)
2461 fprintf (e, " PRODUCTVERSION %lu, %lu, %lu, %lu\n",
2462 (f->product_version_ms >> 16) & 0xffff,
2463 f->product_version_ms & 0xffff,
2464 (f->product_version_ls >> 16) & 0xffff,
2465 f->product_version_ls & 0xffff);
2466 if (f->file_flags_mask != 0)
2467 fprintf (e, " FILEFLAGSMASK 0x%lx\n", f->file_flags_mask);
2468 if (f->file_flags != 0)
2469 fprintf (e, " FILEFLAGS 0x%lx\n", f->file_flags);
2470 if (f->file_os != 0)
2471 fprintf (e, " FILEOS 0x%lx\n", f->file_os);
2472 if (f->file_type != 0)
2473 fprintf (e, " FILETYPE 0x%lx\n", f->file_type);
2474 if (f->file_subtype != 0)
2475 fprintf (e, " FILESUBTYPE 0x%lx\n", f->file_subtype);
2476 if (f->file_date_ms != 0 || f->file_date_ls != 0)
2477 fprintf (e, "// Date: %lu, %lu\n", f->file_date_ms, f->file_date_ls);
2479 fprintf (e, "BEGIN\n");
2481 for (vi = versioninfo->var; vi != NULL; vi = vi->next)
2485 case VERINFO_STRING:
2487 const struct ver_stringinfo *vs;
2489 fprintf (e, " BLOCK \"StringFileInfo\"\n");
2490 fprintf (e, " BEGIN\n");
2491 fprintf (e, " BLOCK \"");
2492 unicode_print (e, vi->u.string.language, -1);
2493 fprintf (e, "\"\n");
2494 fprintf (e, " BEGIN\n");
2496 for (vs = vi->u.string.strings; vs != NULL; vs = vs->next)
2498 fprintf (e, " VALUE \"");
2499 unicode_print (e, vs->key, -1);
2500 fprintf (e, "\", \"");
2501 unicode_print (e, vs->value, -1);
2502 fprintf (e, "\"\n");
2505 fprintf (e, " END\n");
2506 fprintf (e, " END\n");
2512 const struct ver_varinfo *vv;
2514 fprintf (e, " BLOCK \"VarFileInfo\"\n");
2515 fprintf (e, " BEGIN\n");
2516 fprintf (e, " VALUE \"");
2517 unicode_print (e, vi->u.var.key, -1);
2520 for (vv = vi->u.var.var; vv != NULL; vv = vv->next)
2521 fprintf (e, ", 0x%x, %d", (unsigned int) vv->language,
2524 fprintf (e, "\n END\n");
2531 fprintf (e, "END\n");
2534 /* Write out data which would normally be read from a file. */
2537 write_rc_filedata (FILE *e, unsigned long length, const unsigned char *data)
2541 for (i = 0; i + 15 < length; i += 16)
2543 fprintf (e, "// %4lx: ", i);
2544 fprintf (e, "%02x %02x %02x %02x %02x %02x %02x %02x ",
2545 data[i + 0], data[i + 1], data[i + 2], data[i + 3],
2546 data[i + 4], data[i + 5], data[i + 6], data[i + 7]);
2547 fprintf (e, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
2548 data[i + 8], data[i + 9], data[i + 10], data[i + 11],
2549 data[i + 12], data[i + 13], data[i + 14], data[i + 15]);
2554 fprintf (e, "// %4lx:", i);
2557 fprintf (e, " %02x", data[i]);