]>
Commit | Line | Data |
---|---|---|
b75a7d8f | 1 | /* |
46f4442e | 2 | ******************************************************************************* |
729e4ab9 | 3 | * Copyright (C) 1999-2008, International Business Machines |
46f4442e A |
4 | * Corporation and others. All Rights Reserved. |
5 | ******************************************************************************* | |
6 | * file name: gennames.c | |
7 | * encoding: US-ASCII | |
8 | * tab size: 8 (not used) | |
9 | * indentation:4 | |
10 | * | |
11 | * created on: 1999nov01 | |
12 | * created by: Markus W. Scherer | |
13 | * | |
14 | * This program reads a binary file and creates a C source code file | |
15 | * with a byte array that contains the data of the binary file. | |
16 | * | |
17 | * 12/09/1999 weiv Added multiple file handling | |
18 | */ | |
b75a7d8f | 19 | |
73c04bcf A |
20 | #include "unicode/utypes.h" |
21 | ||
22 | #ifdef U_WINDOWS | |
b75a7d8f A |
23 | # define VC_EXTRALEAN |
24 | # define WIN32_LEAN_AND_MEAN | |
b75a7d8f A |
25 | # define NOUSER |
26 | # define NOSERVICE | |
27 | # define NOIME | |
28 | # define NOMCX | |
29 | #include <windows.h> | |
30 | #include <time.h> | |
46f4442e | 31 | #endif |
b75a7d8f | 32 | |
46f4442e A |
33 | #ifdef U_LINUX |
34 | # define U_ELF | |
b75a7d8f A |
35 | #endif |
36 | ||
46f4442e A |
37 | #ifdef U_ELF |
38 | # include <elf.h> | |
39 | # if defined(ELFCLASS64) | |
40 | # define U_ELF64 | |
41 | # endif | |
42 | /* Old elf.h headers may not have EM_X86_64, or have EM_X8664 instead. */ | |
43 | # ifndef EM_X86_64 | |
44 | # define EM_X86_64 62 | |
45 | # endif | |
46 | # define ICU_ENTRY_OFFSET 0 | |
b75a7d8f A |
47 | #endif |
48 | ||
49 | #include <stdio.h> | |
50 | #include <stdlib.h> | |
b75a7d8f A |
51 | #include "unicode/putil.h" |
52 | #include "cmemory.h" | |
53 | #include "cstring.h" | |
54 | #include "filestrm.h" | |
55 | #include "toolutil.h" | |
374ca955 | 56 | #include "unicode/uclean.h" |
b75a7d8f | 57 | #include "uoptions.h" |
729e4ab9 | 58 | #include "pkg_genc.h" |
b75a7d8f | 59 | |
46f4442e | 60 | #if defined(U_WINDOWS) || defined(U_ELF) |
b75a7d8f A |
61 | #define CAN_GENERATE_OBJECTS |
62 | #endif | |
63 | ||
729e4ab9 | 64 | enum { |
374ca955 A |
65 | kOptHelpH = 0, |
66 | kOptHelpQuestionMark, | |
67 | kOptDestDir, | |
68 | kOptName, | |
69 | kOptEntryPoint, | |
70 | #ifdef CAN_GENERATE_OBJECTS | |
71 | kOptObject, | |
46f4442e | 72 | kOptMatchArch, |
374ca955 A |
73 | #endif |
74 | kOptFilename, | |
75 | kOptAssembly | |
76 | }; | |
77 | ||
b75a7d8f A |
78 | static UOption options[]={ |
79 | /*0*/UOPTION_HELP_H, | |
80 | UOPTION_HELP_QUESTION_MARK, | |
81 | UOPTION_DESTDIR, | |
82 | UOPTION_DEF("name", 'n', UOPT_REQUIRES_ARG), | |
374ca955 | 83 | UOPTION_DEF("entrypoint", 'e', UOPT_REQUIRES_ARG), |
b75a7d8f | 84 | #ifdef CAN_GENERATE_OBJECTS |
374ca955 | 85 | /*5*/UOPTION_DEF("object", 'o', UOPT_NO_ARG), |
46f4442e | 86 | UOPTION_DEF("match-arch", 'm', UOPT_REQUIRES_ARG), |
b75a7d8f | 87 | #endif |
374ca955 A |
88 | UOPTION_DEF("filename", 'f', UOPT_REQUIRES_ARG), |
89 | UOPTION_DEF("assembly", 'a', UOPT_REQUIRES_ARG) | |
b75a7d8f A |
90 | }; |
91 | ||
729e4ab9 A |
92 | #define CALL_WRITECCODE 'c' |
93 | #define CALL_WRITEASSEMBLY 'a' | |
94 | #define CALL_WRITEOBJECT 'o' | |
b75a7d8f A |
95 | extern int |
96 | main(int argc, char* argv[]) { | |
97 | UBool verbose = TRUE; | |
729e4ab9 | 98 | char writeCode; |
b75a7d8f A |
99 | |
100 | U_MAIN_INIT_ARGS(argc, argv); | |
101 | ||
374ca955 | 102 | options[kOptDestDir].value = "."; |
b75a7d8f A |
103 | |
104 | /* read command line options */ | |
105 | argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); | |
46f4442e | 106 | |
b75a7d8f A |
107 | /* error handling, printing usage message */ |
108 | if(argc<0) { | |
109 | fprintf(stderr, | |
110 | "error in command line argument \"%s\"\n", | |
111 | argv[-argc]); | |
112 | } | |
374ca955 | 113 | if(argc<0 || options[kOptHelpH].doesOccur || options[kOptHelpQuestionMark].doesOccur) { |
b75a7d8f A |
114 | fprintf(stderr, |
115 | "usage: %s [-options] filename1 filename2 ...\n" | |
116 | "\tread each binary input file and \n" | |
117 | "\tcreate a .c file with a byte array that contains the input file's data\n" | |
118 | "options:\n" | |
119 | "\t-h or -? or --help this usage text\n" | |
120 | "\t-d or --destdir destination directory, followed by the path\n" | |
121 | "\t-n or --name symbol prefix, followed by the prefix\n" | |
46f4442e | 122 | "\t-e or --entrypoint entry point name, followed by the name (_dat will be appended)\n" |
b75a7d8f | 123 | "\t-r or --revision Specify a version\n" |
46f4442e | 124 | , argv[0]); |
b75a7d8f | 125 | #ifdef CAN_GENERATE_OBJECTS |
46f4442e | 126 | fprintf(stderr, |
b75a7d8f | 127 | "\t-o or --object write a .obj file instead of .c\n" |
46f4442e A |
128 | "\t-m or --match-arch file.o match the architecture (CPU, 32/64 bits) of the specified .o\n" |
129 | "\t ELF format defaults to i386. Windows defaults to the native platform.\n"); | |
b75a7d8f | 130 | #endif |
374ca955 | 131 | fprintf(stderr, |
46f4442e | 132 | "\t-f or --filename Specify an alternate base filename. (default: symbolname_typ)\n" |
374ca955 A |
133 | "\t-a or --assembly Create assembly file. (possible values are: "); |
134 | ||
729e4ab9 | 135 | printAssemblyHeadersToStdErr(); |
b75a7d8f A |
136 | } else { |
137 | const char *message, *filename; | |
729e4ab9 | 138 | /* TODO: remove void (*writeCode)(const char *, const char *); */ |
374ca955 A |
139 | |
140 | if(options[kOptAssembly].doesOccur) { | |
141 | message="generating assembly code for %s\n"; | |
729e4ab9 A |
142 | writeCode = CALL_WRITEASSEMBLY; |
143 | /* TODO: remove writeCode=&writeAssemblyCode; */ | |
144 | ||
145 | if (!checkAssemblyHeaderName(options[kOptAssembly].value)) { | |
374ca955 A |
146 | fprintf(stderr, |
147 | "Assembly type \"%s\" is unknown.\n", options[kOptAssembly].value); | |
148 | return -1; | |
149 | } | |
150 | } | |
b75a7d8f | 151 | #ifdef CAN_GENERATE_OBJECTS |
374ca955 | 152 | else if(options[kOptObject].doesOccur) { |
b75a7d8f | 153 | message="generating object code for %s\n"; |
729e4ab9 A |
154 | writeCode = CALL_WRITEOBJECT; |
155 | /* TODO: remove writeCode=&writeObjectCode; */ | |
374ca955 | 156 | } |
b75a7d8f | 157 | #endif |
374ca955 | 158 | else |
b75a7d8f A |
159 | { |
160 | message="generating C code for %s\n"; | |
729e4ab9 A |
161 | writeCode = CALL_WRITECCODE; |
162 | /* TODO: remove writeCode=&writeCCode; */ | |
b75a7d8f A |
163 | } |
164 | while(--argc) { | |
165 | filename=getLongPathname(argv[argc]); | |
166 | if (verbose) { | |
167 | fprintf(stdout, message, filename); | |
168 | } | |
b75a7d8f | 169 | |
729e4ab9 A |
170 | switch (writeCode) { |
171 | case CALL_WRITECCODE: | |
172 | writeCCode(filename, options[kOptDestDir].value, | |
173 | options[kOptName].doesOccur ? options[kOptName].value : NULL, | |
174 | options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL, | |
175 | NULL); | |
176 | break; | |
177 | case CALL_WRITEASSEMBLY: | |
178 | writeAssemblyCode(filename, options[kOptDestDir].value, | |
179 | options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL, | |
180 | options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL, | |
181 | NULL); | |
182 | break; | |
b75a7d8f | 183 | #ifdef CAN_GENERATE_OBJECTS |
729e4ab9 A |
184 | case CALL_WRITEOBJECT: |
185 | writeObjectCode(filename, options[kOptDestDir].value, | |
186 | options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL, | |
187 | options[kOptMatchArch].doesOccur ? options[kOptMatchArch].value : NULL, | |
188 | options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL, | |
189 | NULL); | |
190 | break; | |
191 | #endif | |
192 | default: | |
193 | /* Should never occur. */ | |
194 | break; | |
b75a7d8f | 195 | } |
729e4ab9 | 196 | /* TODO: remove writeCode(filename, options[kOptDestDir].value); */ |
374ca955 | 197 | } |
b75a7d8f | 198 | } |
b75a7d8f | 199 | |
729e4ab9 | 200 | return 0; |
b75a7d8f | 201 | } |