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