]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f | 3 | /* |
46f4442e | 4 | ******************************************************************************* |
2ca993e8 | 5 | * Copyright (C) 1999-2016, International Business Machines |
46f4442e A |
6 | * Corporation and others. All Rights Reserved. |
7 | ******************************************************************************* | |
8 | * file name: gennames.c | |
f3c0d7a5 | 9 | * encoding: UTF-8 |
46f4442e A |
10 | * tab size: 8 (not used) |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 1999nov01 | |
14 | * created by: Markus W. Scherer | |
15 | * | |
16 | * This program reads a binary file and creates a C source code file | |
17 | * with a byte array that contains the data of the binary file. | |
18 | * | |
19 | * 12/09/1999 weiv Added multiple file handling | |
20 | */ | |
b75a7d8f | 21 | |
73c04bcf A |
22 | #include "unicode/utypes.h" |
23 | ||
4388f060 | 24 | #if U_PLATFORM_HAS_WIN32_API |
b75a7d8f A |
25 | # define VC_EXTRALEAN |
26 | # define WIN32_LEAN_AND_MEAN | |
b75a7d8f A |
27 | # define NOUSER |
28 | # define NOSERVICE | |
29 | # define NOIME | |
30 | # define NOMCX | |
31 | #include <windows.h> | |
32 | #include <time.h> | |
46f4442e | 33 | #endif |
b75a7d8f | 34 | |
57a6839d | 35 | #if U_PLATFORM_IS_LINUX_BASED && U_HAVE_ELF_H |
46f4442e | 36 | # define U_ELF |
b75a7d8f A |
37 | #endif |
38 | ||
46f4442e A |
39 | #ifdef U_ELF |
40 | # include <elf.h> | |
41 | # if defined(ELFCLASS64) | |
42 | # define U_ELF64 | |
43 | # endif | |
44 | /* Old elf.h headers may not have EM_X86_64, or have EM_X8664 instead. */ | |
45 | # ifndef EM_X86_64 | |
46 | # define EM_X86_64 62 | |
47 | # endif | |
48 | # define ICU_ENTRY_OFFSET 0 | |
b75a7d8f A |
49 | #endif |
50 | ||
51 | #include <stdio.h> | |
52 | #include <stdlib.h> | |
b75a7d8f A |
53 | #include "unicode/putil.h" |
54 | #include "cmemory.h" | |
55 | #include "cstring.h" | |
56 | #include "filestrm.h" | |
57 | #include "toolutil.h" | |
374ca955 | 58 | #include "unicode/uclean.h" |
b75a7d8f | 59 | #include "uoptions.h" |
729e4ab9 | 60 | #include "pkg_genc.h" |
b75a7d8f | 61 | |
729e4ab9 | 62 | enum { |
374ca955 A |
63 | kOptHelpH = 0, |
64 | kOptHelpQuestionMark, | |
65 | kOptDestDir, | |
340931cb | 66 | kOptQuiet, |
374ca955 A |
67 | kOptName, |
68 | kOptEntryPoint, | |
69 | #ifdef CAN_GENERATE_OBJECTS | |
70 | kOptObject, | |
46f4442e | 71 | kOptMatchArch, |
374ca955 A |
72 | #endif |
73 | kOptFilename, | |
74 | kOptAssembly | |
75 | }; | |
76 | ||
b75a7d8f A |
77 | static UOption options[]={ |
78 | /*0*/UOPTION_HELP_H, | |
79 | UOPTION_HELP_QUESTION_MARK, | |
80 | UOPTION_DESTDIR, | |
340931cb | 81 | UOPTION_QUIET, |
b75a7d8f | 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 */ | |
2ca993e8 | 105 | argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), 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" | |
340931cb | 121 | "\t-q or --quiet do not display warnings and progress\n" |
b75a7d8f | 122 | "\t-n or --name symbol prefix, followed by the prefix\n" |
46f4442e | 123 | "\t-e or --entrypoint entry point name, followed by the name (_dat will be appended)\n" |
b75a7d8f | 124 | "\t-r or --revision Specify a version\n" |
46f4442e | 125 | , argv[0]); |
b75a7d8f | 126 | #ifdef CAN_GENERATE_OBJECTS |
46f4442e | 127 | fprintf(stderr, |
b75a7d8f | 128 | "\t-o or --object write a .obj file instead of .c\n" |
46f4442e A |
129 | "\t-m or --match-arch file.o match the architecture (CPU, 32/64 bits) of the specified .o\n" |
130 | "\t ELF format defaults to i386. Windows defaults to the native platform.\n"); | |
b75a7d8f | 131 | #endif |
374ca955 | 132 | fprintf(stderr, |
46f4442e | 133 | "\t-f or --filename Specify an alternate base filename. (default: symbolname_typ)\n" |
374ca955 A |
134 | "\t-a or --assembly Create assembly file. (possible values are: "); |
135 | ||
729e4ab9 | 136 | printAssemblyHeadersToStdErr(); |
b75a7d8f A |
137 | } else { |
138 | const char *message, *filename; | |
729e4ab9 | 139 | /* TODO: remove void (*writeCode)(const char *, const char *); */ |
374ca955 A |
140 | |
141 | if(options[kOptAssembly].doesOccur) { | |
142 | message="generating assembly code for %s\n"; | |
729e4ab9 A |
143 | writeCode = CALL_WRITEASSEMBLY; |
144 | /* TODO: remove writeCode=&writeAssemblyCode; */ | |
145 | ||
146 | if (!checkAssemblyHeaderName(options[kOptAssembly].value)) { | |
374ca955 A |
147 | fprintf(stderr, |
148 | "Assembly type \"%s\" is unknown.\n", options[kOptAssembly].value); | |
149 | return -1; | |
150 | } | |
151 | } | |
b75a7d8f | 152 | #ifdef CAN_GENERATE_OBJECTS |
374ca955 | 153 | else if(options[kOptObject].doesOccur) { |
b75a7d8f | 154 | message="generating object code for %s\n"; |
729e4ab9 A |
155 | writeCode = CALL_WRITEOBJECT; |
156 | /* TODO: remove writeCode=&writeObjectCode; */ | |
374ca955 | 157 | } |
b75a7d8f | 158 | #endif |
374ca955 | 159 | else |
b75a7d8f A |
160 | { |
161 | message="generating C code for %s\n"; | |
729e4ab9 A |
162 | writeCode = CALL_WRITECCODE; |
163 | /* TODO: remove writeCode=&writeCCode; */ | |
b75a7d8f | 164 | } |
340931cb A |
165 | if (options[kOptQuiet].doesOccur) { |
166 | verbose = FALSE; | |
167 | } | |
b75a7d8f A |
168 | while(--argc) { |
169 | filename=getLongPathname(argv[argc]); | |
170 | if (verbose) { | |
171 | fprintf(stdout, message, filename); | |
172 | } | |
b75a7d8f | 173 | |
729e4ab9 A |
174 | switch (writeCode) { |
175 | case CALL_WRITECCODE: | |
176 | writeCCode(filename, options[kOptDestDir].value, | |
177 | options[kOptName].doesOccur ? options[kOptName].value : NULL, | |
178 | options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL, | |
340931cb A |
179 | NULL, |
180 | 0); | |
729e4ab9 A |
181 | break; |
182 | case CALL_WRITEASSEMBLY: | |
183 | writeAssemblyCode(filename, options[kOptDestDir].value, | |
184 | options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL, | |
185 | options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL, | |
340931cb A |
186 | NULL, |
187 | 0); | |
729e4ab9 | 188 | break; |
b75a7d8f | 189 | #ifdef CAN_GENERATE_OBJECTS |
729e4ab9 A |
190 | case CALL_WRITEOBJECT: |
191 | writeObjectCode(filename, options[kOptDestDir].value, | |
192 | options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL, | |
193 | options[kOptMatchArch].doesOccur ? options[kOptMatchArch].value : NULL, | |
194 | options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL, | |
340931cb A |
195 | NULL, |
196 | 0); | |
729e4ab9 A |
197 | break; |
198 | #endif | |
199 | default: | |
200 | /* Should never occur. */ | |
201 | break; | |
b75a7d8f | 202 | } |
729e4ab9 | 203 | /* TODO: remove writeCode(filename, options[kOptDestDir].value); */ |
374ca955 | 204 | } |
b75a7d8f | 205 | } |
b75a7d8f | 206 | |
729e4ab9 | 207 | return 0; |
b75a7d8f | 208 | } |