1 /* windmc.c -- a program to compile Windows message files.
3 Free Software Foundation, Inc.
4 Written by Kai Tietz, Onevision.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 /* This program can read and comile Windows message format.
25 It is based on information taken from the following sources:
27 * Microsoft documentation.
29 * The wmc program, written by Bertho A. Stultiens (BS). */
37 #include "libiberty.h"
38 #include "safe-ctype.h"
44 /* Defines a message compiler element item with length and offset
46 typedef struct mc_msg_item
50 struct bin_messagetable_item
*res
;
53 /* Defined in bfd/binary.c. Used to set architecture and machine of input
55 extern enum bfd_architecture bfd_external_binary_architecture
;
56 extern unsigned long bfd_external_machine
;
58 int target_is_bigendian
= 0;
59 const char *def_target_arch
;
61 /* Globals and static variable definitions. */
63 /* bfd global helper struct variable. */
71 mc_node
*mc_nodes
= NULL
;
72 static mc_node_lang
**mc_nodes_lang
= NULL
;
73 static int mc_nodes_lang_count
= 0;
74 static mc_keyword
**mc_severity_codes
= NULL
;
75 static int mc_severity_codes_count
= 0;
76 static mc_keyword
**mc_facility_codes
= NULL
;
77 static int mc_facility_codes_count
= 0;
79 /* When we are building a resource tree, we allocate everything onto
80 an obstack, so that we can free it all at once if we want. */
81 #define obstack_chunk_alloc xmalloc
82 #define obstack_chunk_free free
84 /* The resource building obstack. */
85 static struct obstack res_obstack
;
88 /* Set by -C. Set the default code page to be used for input text file. */
89 static rc_uint_type mcset_codepage_in
= 0;
91 /* Set by -O. Set the default code page to be used for output text files. */
92 static rc_uint_type mcset_codepage_out
= 0;
94 /* Set by -b. .BIN filename should have .mc filename_ included for uniqueness. */
95 static int mcset_prefix_bin
= 0;
97 /* The base name of the .mc file. */
98 static const char *mcset_mc_basename
= "unknown";
100 /* Set by -e <ext>. Specify the extension for the header file. */
101 static const char *mcset_header_ext
= ".h";
103 /* Set by -h <path>. Gives the path of where to create the C include file. */
104 static const char *mcset_header_dir
= "./";
106 /* Set by -r <path>. Gives the path of where to create the RC include file
107 and the binary message resource files it includes. */
108 static const char *mcset_rc_dir
= "./";
110 /* Modified by -a & -u. By -u input file is unicode, by -a is ASCII (default). */
111 static int mcset_text_in_is_unicode
= 0;
113 /* Modified by -A & -U. By -U bin file is unicode (default), by -A is ASCII. */
114 static int mcset_bin_out_is_unicode
= 1;
116 /* Set by -c. Sets the Customer bit in all the message ID's. */
117 int mcset_custom_bit
= 0;
119 /* Set by -o. Generate OLE2 header file. Use HRESULT definition instead of
120 status code definition. */
121 static int mcset_use_hresult
= 0;
123 /* Set by -m <msglen>. Generate a warning if the size of any message exceeds
124 maxmsglen characters. */
125 rc_uint_type mcset_max_message_length
= 0;
127 /* Set by -d. Sets message values in header to decimal initially. */
128 int mcset_out_values_are_decimal
= 0;
130 /* Set by -n. terminates all strings with null's in the message tables. */
131 static int mcset_automatic_null_termination
= 0;
133 /* The type used for message id output in header. */
134 unichar
*mcset_msg_id_typedef
= NULL
;
136 /* Set by -x path. Geberated debug C file for mapping ID's to text. */
137 static const char *mcset_dbg_dir
= NULL
;
139 /* getopt long name definitions. */
140 static const struct option long_options
[] =
142 {"binprefix", no_argument
, 0, 'b'},
143 {"target", required_argument
, 0, 'F'},
144 {"extension", required_argument
, 0, 'e'},
145 {"headerdir", required_argument
, 0, 'h'},
146 {"rcdir", required_argument
, 0, 'r'},
147 {"verbose", no_argument
, 0, 'v'},
148 {"codepage_in", required_argument
, 0, 'C'},
149 {"codepage_out", required_argument
, 0, 'O'},
150 {"maxlength", required_argument
, 0, 'm'},
151 {"ascii_in", no_argument
, 0, 'a'},
152 {"ascii_out", no_argument
, 0, 'A'},
153 {"unicode_in", no_argument
, 0, 'u'},
154 {"unicode_out", no_argument
, 0, 'U'},
155 {"customflag", no_argument
, 0, 'c'},
156 {"decimal_values", no_argument
, 0, 'd'},
157 {"hresult_use", no_argument
, 0, 'o'},
158 {"nullterminate", no_argument
, 0, 'n'},
159 {"xdbg", required_argument
, 0, 'x'},
160 {"version", no_argument
, 0, 'V'},
161 {"help", no_argument
, 0, 'H'},
162 {0, no_argument
, 0, 0}
166 /* Initialize the resource building obstack. */
170 obstack_init (&res_obstack
);
173 /* Allocate space on the resource building obstack. */
175 res_alloc (rc_uint_type bytes
)
177 return (void *) obstack_alloc (&res_obstack
, (size_t) bytes
);
181 mc_create_path_text_file (const char *path
, const char *ext
)
187 len
+= (path
!= NULL
? strlen (path
) : 0);
188 len
+= strlen (mcset_mc_basename
);
189 len
+= (ext
!= NULL
? strlen (ext
) : 0);
191 sprintf (hsz
, "%s%s%s", (path
!= NULL
? path
: ""), mcset_mc_basename
,
192 (ext
!= NULL
? ext
: ""));
193 if ((ret
= fopen (hsz
, "wb")) == NULL
)
194 fatal (_("can't create %s file ,%s' for output.\n"), (ext
? ext
: "text"), hsz
);
200 usage (FILE *stream
, int status
)
202 fprintf (stream
, _("Usage: %s [option(s)] [input-file]\n"),
204 fprintf (stream
, _(" The options are:\n\
205 -a --ascii_in Read input file as ASCII file\n\
206 -A --ascii_out Write binary messages as ASCII\n\
207 -b --binprefix .bin filename is prefixed by .mc filename_ for uniqueness.\n\
208 -c --customflag Set custom flags for messages\n\
209 -C --codepage_in=<val> Set codepage when reading mc text file\n\
210 -d --decimal_values Print values to text files decimal\n\
211 -e --extension=<extension> Set header extension used on export header file\n\
212 -F --target <target> Specify output target for endianess.\n\
213 -h --headerdir=<directory> Set the export directory for headers\n\
214 -u --unicode_in Read input file as UTF16 file\n\
215 -U --unicode_out Write binary messages as UFT16\n\
216 -m --maxlength=<val> Set the maximal allowed message length\n\
217 -n --nullterminate Automatic add a zero termination to strings\n\
218 -o --hresult_use Use HRESULT definition instead of status code definition\n\
219 -O --codepage_out=<val> Set codepage used for writing text file\n\
220 -r --rcdir=<directory> Set the export directory for rc files\n\
221 -x --xdbg=<directory> Where to create the .dbg C include file\n\
222 that maps message ID's to their symbolic name.\n\
224 fprintf (stream
, _("\
225 -H --help Print this help message\n\
226 -v --verbose Verbose - tells you what it's doing\n\
227 -V --version Print version information\n"));
229 list_supported_targets (program_name
, stream
);
231 if (REPORT_BUGS_TO
[0] && status
== 0)
232 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
238 set_endianess (bfd
*abfd
, const char *target
)
240 const bfd_target
*target_vec
;
242 def_target_arch
= NULL
;
243 target_vec
= bfd_find_target (target
, abfd
);
245 fatal ("Can't detect target endianess and architecture.");
246 target_is_bigendian
= ((target_vec
->byteorder
== BFD_ENDIAN_BIG
) ? 1 : 0);
248 const char *tname
= target_vec
->name
;
249 const char **arch
= bfd_arch_list ();
253 if (strchr (tname
, '-') != NULL
)
254 tname
= strchr (tname
, '-') + 1;
255 while (*arch
!= NULL
)
257 const char *in_a
= strstr (*arch
, tname
);
258 char end_ch
= (in_a
? in_a
[strlen (tname
)] : 0);
259 if (in_a
&& (in_a
== *arch
|| in_a
[-1] == ':')
262 def_target_arch
= *arch
;
268 if (! def_target_arch
)
269 fatal ("Can't detect architecture.");
274 probe_codepage (rc_uint_type
*cp
, int *is_uni
, const char *pswitch
, int defmode
)
285 if (*cp
!= 0 && *cp
!= CP_UTF16
)
287 fprintf (stderr
, _("%s: warning: "), program_name
);
288 fprintf (stderr
, _("A codepage was specified switch ,%s' and UTF16.\n"), pswitch
);
289 fprintf (stderr
, _("\tcodepage settings are ignored.\n"));
301 if (! unicode_is_valid_codepage (*cp
))
302 fatal ("Code page 0x%x is unknown.", (unsigned int) *cp
);
312 ret
= res_alloc (sizeof (mc_node
));
313 memset (ret
, 0, sizeof (mc_node
));
318 mc_node
*h
= mc_nodes
;
320 while (h
->next
!= NULL
)
328 mc_add_node_lang (mc_node
*root
, const mc_keyword
*lang
, rc_uint_type vid
)
330 mc_node_lang
*ret
, *h
, *p
;
332 if (! lang
|| ! root
)
333 fatal (_("try to add a ill language."));
334 ret
= res_alloc (sizeof (mc_node_lang
));
335 memset (ret
, 0, sizeof (mc_node_lang
));
338 if ((h
= root
->sub
) == NULL
)
345 if (h
->lang
->nval
> lang
->nval
)
347 if (h
->lang
->nval
== lang
->nval
)
352 fatal ("double defined message id %ld.\n", (long) vid
);
366 convert_unicode_to_ACP (const unichar
*usz
)
373 codepage_from_unicode (&l
, usz
, &s
, mcset_codepage_out
);
375 fatal ("unicode string not mappable to ASCII codepage 0x%lx.\n", (long) mcset_codepage_out
);
380 write_dbg_define (FILE *fp
, const unichar
*sym_name
, const unichar
*typecast
)
384 if (!sym_name
|| sym_name
[0] == 0)
386 sym
= convert_unicode_to_ACP (sym_name
);
389 unicode_print (fp
, typecast
, unichar_len (typecast
));
391 fprintf (fp
, "DWORD");
392 fprintf (fp
, ") %s, \"%s\" },\n", sym
, sym
);
396 write_header_define (FILE *fp
, const unichar
*sym_name
, rc_uint_type vid
, const unichar
*typecast
, mc_node_lang
*nl
)
401 if (!sym_name
|| sym_name
[0] == 0)
405 if (mcset_out_values_are_decimal
)
406 fprintf (fp
, "//\n// MessageId: 0x%lu\n//\n", (unsigned long) vid
);
408 fprintf (fp
, "//\n// MessageId: 0x%lx\n//\n", (unsigned long) vid
);
412 sym
= convert_unicode_to_ACP (sym_name
);
413 if (typecast
&& typecast
[0] != 0)
414 tdef
= convert_unicode_to_ACP (typecast
);
415 fprintf (fp
, "//\n// MessageId: %s\n//\n", sym
);
416 if (! mcset_out_values_are_decimal
)
417 fprintf (fp
, "#define %s %s%s%s 0x%lx\n\n", sym
,
418 (tdef
? "(" : ""), (tdef
? tdef
: ""), (tdef
? ")" : ""),
419 (unsigned long) vid
);
421 fprintf (fp
, "#define %s %s%s%s 0x%lu\n\n", sym
,
422 (tdef
? "(" : ""), (tdef
? tdef
: ""), (tdef
? ")" : ""),
423 (unsigned long) vid
);
427 sort_mc_node_lang (const void *l
, const void *r
)
429 const mc_node_lang
*l1
= *((const mc_node_lang
**)l
);
430 const mc_node_lang
*r1
= *((const mc_node_lang
**)r
);
434 if (l1
->lang
!= r1
->lang
)
436 if (l1
->lang
->nval
< r1
->lang
->nval
)
440 if (l1
->vid
== r1
->vid
)
442 if (l1
->vid
< r1
->vid
)
448 sort_keyword_by_nval (const void *l
, const void *r
)
450 const mc_keyword
*l1
= *((const mc_keyword
**)l
);
451 const mc_keyword
*r1
= *((const mc_keyword
**)r
);
452 rc_uint_type len1
, len2
;
457 if (l1
->nval
!= r1
->nval
)
459 if (l1
->nval
< r1
->nval
)
463 len1
= unichar_len (l1
->usz
);
464 len2
= unichar_len (r1
->usz
);
466 e
= memcmp (l1
->usz
, r1
->usz
, sizeof (unichar
) * len1
);
468 e
= memcmp (l1
->usz
, r1
->usz
, sizeof (unichar
) * len2
);
473 else if (len1
> len2
)
486 /* Sort message by their language and id ascending. */
487 mc_nodes_lang_count
= 0;
495 mc_nodes_lang_count
+=1;
501 if (mc_nodes_lang_count
!= 0)
505 mc_nodes_lang
= xmalloc (sizeof (mc_node_lang
*) * mc_nodes_lang_count
);
512 mc_nodes_lang
[i
++] = n
;
517 qsort (mc_nodes_lang
, (size_t) mc_nodes_lang_count
, sizeof (mc_node_lang
*), sort_mc_node_lang
);
519 /* Sort facility code definitions by there id ascending. */
521 while ((k
= enum_facility (i
)) != NULL
)
523 mc_facility_codes_count
= i
;
526 mc_facility_codes
= xmalloc (sizeof (mc_keyword
*) * i
);
528 while ((k
= enum_facility (i
)) != NULL
)
529 mc_facility_codes
[i
++] = (mc_keyword
*) k
;
530 qsort (mc_facility_codes
, (size_t) mc_facility_codes_count
, sizeof (mc_keyword
*), sort_keyword_by_nval
);
533 /* Sort severity code definitions by there id ascending. */
535 while ((k
= enum_severity (i
)) != NULL
)
537 mc_severity_codes_count
= i
;
540 mc_severity_codes
= xmalloc (sizeof (mc_keyword
*) * i
);
542 while ((k
= enum_severity (i
)) != NULL
)
543 mc_severity_codes
[i
++] = (mc_keyword
*) k
;
544 qsort (mc_severity_codes
, (size_t) mc_severity_codes_count
, sizeof (mc_keyword
*), sort_keyword_by_nval
);
549 mc_get_block_count (mc_node_lang
**nl
, int elems
)
562 while (i
< elems
&& nl
[i
]->vid
== exid
+ 1)
569 windmc_open_as_binary (const char *filename
)
573 abfd
= bfd_openw (filename
, "binary");
575 fatal ("can't open `%s' for output", filename
);
581 target_put_16 (void *p
, rc_uint_type value
)
585 if (target_is_bigendian
)
586 bfd_putb16 (value
, p
);
588 bfd_putl16 (value
, p
);
592 target_put_32 (void *p
, rc_uint_type value
)
596 if (target_is_bigendian
)
597 bfd_putb32 (value
, p
);
599 bfd_putl32 (value
, p
);
602 static struct bin_messagetable_item
*
603 mc_generate_bin_item (mc_node_lang
*n
, rc_uint_type
*res_len
)
605 struct bin_messagetable_item
*ret
= NULL
;
609 if (mcset_bin_out_is_unicode
== 1)
611 unichar
*ht
= n
->message
;
612 rc_uint_type txt_len
;
614 txt_len
= unichar_len (n
->message
);
615 if (mcset_automatic_null_termination
&& txt_len
!= 0)
617 while (txt_len
> 0 && ht
[txt_len
- 1] > 0 && ht
[txt_len
- 1] < 0x20)
620 txt_len
*= sizeof (unichar
);
621 len
= BIN_MESSAGETABLE_ITEM_SIZE
+ txt_len
+ sizeof (unichar
);
622 ret
= res_alloc ((len
+ 3) & ~3);
623 memset (ret
, 0, (len
+ 3) & ~3);
624 target_put_16 (ret
->length
, (len
+ 3) & ~3);
625 target_put_16 (ret
->flags
, MESSAGE_RESOURCE_UNICODE
);
629 target_put_16 (ret
->data
+ txt_len
, *ht
++);
635 rc_uint_type txt_len
, l
;
638 codepage_from_unicode( &l
, n
->message
, &cvt_txt
, n
->lang
->lang_info
.wincp
);
640 fatal ("Failed to convert message to language codepage.\n");
641 txt_len
= strlen (cvt_txt
);
642 if (mcset_automatic_null_termination
&& txt_len
> 0)
644 while (txt_len
> 0 && cvt_txt
[txt_len
- 1] > 0 && cvt_txt
[txt_len
- 1] < 0x20)
645 cvt_txt
[--txt_len
] = 0;
647 len
= BIN_MESSAGETABLE_ITEM_SIZE
+ txt_len
+ 1;
648 ret
= res_alloc ((len
+ 3) & ~3);
649 memset (ret
, 0, (len
+ 3) & ~3);
650 target_put_16 (ret
->length
, (len
+ 3) & ~3);
651 target_put_16 (ret
->flags
, 0);
652 strcpy ((char *) ret
->data
, cvt_txt
);
654 *res_len
= (len
+ 3) & ~3;
659 mc_write_blocks (struct bin_messagetable
*mtbl
, mc_node_lang
**nl
, mc_msg_item
*ml
, int elems
)
669 target_put_32 (mtbl
->items
[idx
].lowid
, nl
[i
]->vid
);
670 target_put_32 (mtbl
->items
[idx
].highid
, nl
[i
]->vid
);
671 target_put_32 (mtbl
->items
[idx
].offset
, ml
[i
].res_off
);
673 while (i
< elems
&& nl
[i
]->vid
== exid
+ 1)
675 target_put_32 (mtbl
->items
[idx
].highid
, nl
[i
]->vid
);
683 set_windmc_bfd_content (const void *data
, rc_uint_type off
, rc_uint_type length
)
685 if (! bfd_set_section_contents (mc_bfd
.abfd
, mc_bfd
.sec
, data
, off
, length
))
686 bfd_fatal ("bfd_set_section_contents");
690 windmc_write_bin (const char *filename
, mc_node_lang
**nl
, int elems
)
692 unsigned long sec_length
= 1;
695 struct bin_messagetable
*mtbl
;
696 rc_uint_type dta_off
, dta_start
;
700 mc_bfd
.abfd
= windmc_open_as_binary (filename
);
701 mc_bfd
.sec
= bfd_make_section (mc_bfd
.abfd
, ".data");
702 if (mc_bfd
.sec
== NULL
)
703 bfd_fatal ("bfd_make_section");
704 if (! bfd_set_section_flags (mc_bfd
.abfd
, mc_bfd
.sec
,
705 (SEC_HAS_CONTENTS
| SEC_ALLOC
706 | SEC_LOAD
| SEC_DATA
)))
707 bfd_fatal ("bfd_set_section_flags");
708 /* Requiring this is probably a bug in BFD. */
709 mc_bfd
.sec
->output_section
= mc_bfd
.sec
;
711 block_count
= mc_get_block_count (nl
, elems
);
713 dta_off
= (rc_uint_type
) ((BIN_MESSAGETABLE_BLOCK_SIZE
* block_count
) + BIN_MESSAGETABLE_SIZE
- 4);
714 dta_start
= dta_off
= (dta_off
+ 3) & ~3;
715 mi
= xmalloc (sizeof (mc_msg_item
) * elems
);
716 mtbl
= xmalloc (dta_start
);
718 /* Clear header region. */
719 memset (mtbl
, 0, dta_start
);
720 target_put_32 (mtbl
->cblocks
, block_count
);
721 /* Prepare items section for output. */
722 for (i
= 0; i
< elems
; i
++)
724 mi
[i
].res_off
= dta_off
;
725 mi
[i
].res
= mc_generate_bin_item (nl
[i
], &mi
[i
].res_len
);
726 dta_off
+= mi
[i
].res_len
;
728 sec_length
= (dta_off
+ 3) & ~3;
729 if (! bfd_set_section_size (mc_bfd
.abfd
, mc_bfd
.sec
, sec_length
))
730 bfd_fatal ("bfd_set_section_size");
731 /* Make sure we write the complete block. */
732 set_windmc_bfd_content ("\0", sec_length
- 1, 1);
734 /* Write block information. */
735 mc_write_blocks (mtbl
, nl
, mi
, elems
);
737 set_windmc_bfd_content (mtbl
, 0, dta_start
);
740 for (i
= 0; i
< elems
; i
++)
741 set_windmc_bfd_content (mi
[i
].res
, mi
[i
].res_off
, mi
[i
].res_len
);
745 bfd_close (mc_bfd
.abfd
);
753 mc_node_lang
*n
= NULL
;
756 if (! mc_nodes_lang_count
)
760 while (i
< mc_nodes_lang_count
)
765 if (n
&& n
->lang
== mc_nodes_lang
[i
]->lang
)
770 n
= mc_nodes_lang
[i
];
772 while (c
< mc_nodes_lang_count
&& n
->lang
== mc_nodes_lang
[c
]->lang
)
774 nd
= convert_unicode_to_ACP (n
->lang
->sval
);
776 /* Prepare filename for binary output. */
777 filename
= xmalloc (strlen (nd
) + 4 + 1 + strlen (mcset_mc_basename
) + 1 + strlen (mcset_rc_dir
));
778 strcpy (filename
, mcset_rc_dir
);
779 if (mcset_prefix_bin
)
780 sprintf (filename
+ strlen (filename
), "%s_", mcset_mc_basename
);
781 strcat (filename
, nd
);
782 strcat (filename
, ".bin");
784 /* Write message file. */
785 windmc_write_bin (filename
, &mc_nodes_lang
[i
], (c
- i
));
799 "/* Do not edit this file manually.\n"
800 " This file is autogenerated by windmc. */\n\n");
801 if (! mc_nodes_lang_count
)
805 for (l
= 0; l
< mc_nodes_lang_count
; l
++)
807 if (n
&& n
->lang
== mc_nodes_lang
[l
]->lang
)
810 n
= mc_nodes_lang
[l
];
811 fprintf (fp
, "\n// Country: %s\n// Language: %s\n#pragma code_page(%u)\n",
812 n
->lang
->lang_info
.country
, n
->lang
->lang_info
.name
,
813 (unsigned) n
->lang
->lang_info
.wincp
);
814 fprintf (fp
, "LANGUAGE 0x%lx, 0x%lx\n", (long) (n
->lang
->nval
& 0x3ff),
815 (long) ((n
->lang
->nval
& 0xffff) >> 10));
816 fprintf (fp
, "1 MESSAGETABLE \"");
817 if (mcset_prefix_bin
)
818 fprintf (fp
, "%s_", mcset_mc_basename
);
819 unicode_print (fp
, n
->lang
->sval
, unichar_len (n
->lang
->sval
));
820 fprintf (fp
, ".bin\"\n");
830 "/* Do not edit this file manually.\n"
831 " This file is autogenerated by windmc.\n\n"
832 " This file maps each message ID value in to a text string that contains\n"
833 " the symbolic name used for it. */\n\n");
836 "struct %sSymbolicName\n"
837 "{\n ", mcset_mc_basename
);
838 if (mcset_msg_id_typedef
)
839 unicode_print (fp
, mcset_msg_id_typedef
, unichar_len (mcset_msg_id_typedef
));
841 fprintf (fp
, "DWORD");
844 " char *SymbolicName;\n"
845 "} %sSymbolicNames[] =\n"
846 "{\n", mcset_mc_basename
);
851 write_dbg_define (fp
, h
->symbol
, mcset_msg_id_typedef
);
854 fprintf (fp
, " { (");
855 if (mcset_msg_id_typedef
)
856 unicode_print (fp
, mcset_msg_id_typedef
, unichar_len (mcset_msg_id_typedef
));
858 fprintf (fp
, "DWORD");
860 ") 0xffffffff, NULL }\n"
865 write_header (FILE *fp
)
869 const mc_keyword
*key
;
873 "/* Do not edit this file manually.\n"
874 " This file is autogenerated by windmc. */\n\n"
875 "//\n// The values are 32 bit layed out as follows:\n//\n"
876 "// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1\n"
877 "// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0\n"
878 "// +---+-+-+-----------------------+-------------------------------+\n"
879 "// |Sev|C|R| Facility | Code |\n"
880 "// +---+-+-+-----------------------+-------------------------------+\n//\n"
882 "// C - is the Customer code flag\n//\n"
883 "// R - is a reserved bit\n//\n"
884 "// Code - is the facility's status code\n//\n");
888 fprintf (fp
, "// Sev - is the severity code\n//\n");
889 if (mc_severity_codes_count
!= 0)
891 for (i
= 0; i
< mc_severity_codes_count
; i
++)
893 key
= mc_severity_codes
[i
];
894 fprintf (fp
, "// %s - %02lx\n", convert_unicode_to_ACP (key
->usz
),
895 (unsigned long) key
->nval
);
896 if (key
->sval
&& key
->sval
[0] != 0)
898 if (! mcset_out_values_are_decimal
)
899 fprintf (fp
, "#define %s 0x%lx\n", convert_unicode_to_ACP (key
->sval
),
900 (unsigned long) key
->nval
);
902 fprintf (fp
, "#define %s 0x%lu\n", convert_unicode_to_ACP (key
->sval
),
903 (unsigned long) key
->nval
);
906 fprintf (fp
, "//\n");
908 fprintf (fp
, "// Facility - is the facility code\n//\n");
909 if (mc_facility_codes_count
!= 0)
911 for (i
= 0; i
< mc_facility_codes_count
; i
++)
913 key
= mc_facility_codes
[i
];
914 fprintf (fp
, "// %s - %04lx\n", convert_unicode_to_ACP (key
->usz
),
915 (unsigned long) key
->nval
);
916 if (key
->sval
&& key
->sval
[0] != 0)
918 if (! mcset_out_values_are_decimal
)
919 fprintf (fp
, "#define %s 0x%lx\n", convert_unicode_to_ACP (key
->sval
),
920 (unsigned long) key
->nval
);
922 fprintf (fp
, "#define %s 0x%lu\n", convert_unicode_to_ACP (key
->sval
),
923 (unsigned long) key
->nval
);
926 fprintf (fp
, "//\n");
933 s
= convert_unicode_to_ACP (h
->user_text
);
935 fprintf (fp
, "%s", s
);
938 write_header_define (fp
, h
->symbol
, h
->vid
, mcset_msg_id_typedef
, h
->sub
);
944 mc_unify_path (const char *path
)
949 if (! path
|| *path
== 0)
951 hsz
= xmalloc (strlen (path
) + 2);
953 end
= hsz
+ strlen (hsz
);
954 if (hsz
[-1] != '/' && hsz
[-1] != '\\')
956 while ((end
= strchr (hsz
, '\\')) != NULL
)
961 int main (int, char **);
964 main (int argc
, char **argv
)
968 char *target
, *input_filename
;
971 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
972 setlocale (LC_MESSAGES
, "");
974 #if defined (HAVE_SETLOCALE)
975 setlocale (LC_CTYPE
, "");
977 bindtextdomain (PACKAGE
, LOCALEDIR
);
978 textdomain (PACKAGE
);
980 program_name
= argv
[0];
981 xmalloc_set_program_name (program_name
);
983 expandargv (&argc
, &argv
);
986 set_default_bfd_target ();
990 input_filename
= NULL
;
994 while ((c
= getopt_long (argc
, argv
, "C:F:O:h:e:m:r:x:aAbcdHunoUvV", long_options
,
1000 mcset_prefix_bin
= 1;
1004 mcset_header_ext
= optarg
;
1005 if (mcset_header_ext
[0] != '.' && mcset_header_ext
[0] != 0)
1007 char *hsz
= xmalloc (strlen (mcset_header_ext
) + 2);
1009 sprintf (hsz
, ".%s", mcset_header_ext
);
1010 mcset_header_ext
= hsz
;
1015 mcset_header_dir
= mc_unify_path (optarg
);
1018 mcset_rc_dir
= mc_unify_path (optarg
);
1021 mcset_text_in_is_unicode
= 0;
1025 mcset_dbg_dir
= mc_unify_path (optarg
);
1028 mcset_bin_out_is_unicode
= 0;
1031 mcset_out_values_are_decimal
= 1;
1034 mcset_text_in_is_unicode
= 1;
1037 mcset_bin_out_is_unicode
= 1;
1040 mcset_custom_bit
= 1;
1043 mcset_automatic_null_termination
= 1;
1046 mcset_use_hresult
= 1;
1047 fatal ("option -o is not implemented until yet.\n");
1056 mcset_max_message_length
= strtol (optarg
, (char **) NULL
, 10);
1059 mcset_codepage_in
= strtol (optarg
, (char **) NULL
, 10);
1062 mcset_codepage_out
= strtol (optarg
, (char **) NULL
, 10);
1069 print_version ("windmc");
1077 if (input_filename
== NULL
&& optind
< argc
)
1079 input_filename
= argv
[optind
];
1083 set_endianess (NULL
, target
);
1085 if (input_filename
== NULL
)
1087 fprintf (stderr
, "Error: No input file was specified.\n");
1090 mc_set_inputfile (input_filename
);
1092 if (!probe_codepage (&mcset_codepage_in
, &mcset_text_in_is_unicode
, "codepage_in", 0))
1094 if (mcset_codepage_out
== 0)
1095 mcset_codepage_out
= 1252;
1096 if (! unicode_is_valid_codepage (mcset_codepage_out
))
1097 fatal ("Code page 0x%x is unknown.", (unsigned int) mcset_codepage_out
);
1098 if (mcset_codepage_out
== CP_UTF16
)
1099 fatal ("UTF16 is no valid text output code page.");
1102 fprintf (stderr
, "// Default target is %s and it is %s endian.\n",
1103 def_target_arch
, (target_is_bigendian
? "big" : "little"));
1104 fprintf (stderr
, "// Input codepage: 0x%x\n", (unsigned int) mcset_codepage_in
);
1105 fprintf (stderr
, "// Output codepage: 0x%x\n", (unsigned int) mcset_codepage_out
);
1111 /* Initialize mcset_mc_basename. */
1113 const char *bn
, *bn2
;
1116 bn
= strrchr (input_filename
, '/');
1117 bn2
= strrchr (input_filename
, '\\');
1120 if (bn
&& bn2
&& bn
< bn2
)
1123 bn
= input_filename
;
1126 mcset_mc_basename
= hsz
= xstrdup (bn
);
1128 /* Cut of right-hand extension. */
1129 if ((hsz
= strrchr (hsz
, '.')) != NULL
)
1133 /* Load the input file and do code page transformations to UTF16. */
1139 FILE *fp
= fopen (input_filename
, "rb");
1142 fatal (_("unable to open file ,%s' for input.\n"), input_filename
);
1144 fseek (fp
, 0, SEEK_END
);
1146 fseek (fp
, 0, SEEK_SET
);
1147 buff
= malloc (flen
+ 3);
1148 memset (buff
, 0, flen
+ 3);
1149 fread (buff
, 1, flen
, fp
);
1151 if (mcset_text_in_is_unicode
!= 1)
1153 unicode_from_codepage (&ul
, &u
, buff
, mcset_codepage_in
);
1155 fatal ("Failed to convert input to UFT16\n");
1160 if ((flen
& 1) != 0)
1161 fatal (_("input file does not seems to be UFT16.\n"));
1162 mc_set_content ((unichar
*) buff
);
1172 h_fp
= mc_create_path_text_file (mcset_header_dir
, mcset_header_ext
);
1173 write_header (h_fp
);
1176 h_fp
= mc_create_path_text_file (mcset_rc_dir
, ".rc");
1180 if (mcset_dbg_dir
!= NULL
)
1182 h_fp
= mc_create_path_text_file (mcset_dbg_dir
, ".dbg");
1189 free (mc_nodes_lang
);
1190 if (mc_severity_codes
)
1191 free (mc_severity_codes
);
1192 if (mc_facility_codes
)
1193 free (mc_facility_codes
);