michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 1999-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: gennames.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 1999nov01 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * This program reads a binary file and creates a C source code file michael@0: * with a byte array that contains the data of the binary file. michael@0: * michael@0: * 12/09/1999 weiv Added multiple file handling michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if U_PLATFORM_HAS_WIN32_API michael@0: # define VC_EXTRALEAN michael@0: # define WIN32_LEAN_AND_MEAN michael@0: # define NOUSER michael@0: # define NOSERVICE michael@0: # define NOIME michael@0: # define NOMCX michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #if U_PLATFORM_IS_LINUX_BASED && U_HAVE_ELF_H michael@0: # define U_ELF michael@0: #endif michael@0: michael@0: #ifdef U_ELF michael@0: # include michael@0: # if defined(ELFCLASS64) michael@0: # define U_ELF64 michael@0: # endif michael@0: /* Old elf.h headers may not have EM_X86_64, or have EM_X8664 instead. */ michael@0: # ifndef EM_X86_64 michael@0: # define EM_X86_64 62 michael@0: # endif michael@0: # define ICU_ENTRY_OFFSET 0 michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include "unicode/putil.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "filestrm.h" michael@0: #include "toolutil.h" michael@0: #include "unicode/uclean.h" michael@0: #include "uoptions.h" michael@0: #include "pkg_genc.h" michael@0: michael@0: enum { michael@0: kOptHelpH = 0, michael@0: kOptHelpQuestionMark, michael@0: kOptDestDir, michael@0: kOptName, michael@0: kOptEntryPoint, michael@0: #ifdef CAN_GENERATE_OBJECTS michael@0: kOptObject, michael@0: kOptMatchArch, michael@0: #endif michael@0: kOptFilename, michael@0: kOptAssembly michael@0: }; michael@0: michael@0: static UOption options[]={ michael@0: /*0*/UOPTION_HELP_H, michael@0: UOPTION_HELP_QUESTION_MARK, michael@0: UOPTION_DESTDIR, michael@0: UOPTION_DEF("name", 'n', UOPT_REQUIRES_ARG), michael@0: UOPTION_DEF("entrypoint", 'e', UOPT_REQUIRES_ARG), michael@0: #ifdef CAN_GENERATE_OBJECTS michael@0: /*5*/UOPTION_DEF("object", 'o', UOPT_NO_ARG), michael@0: UOPTION_DEF("match-arch", 'm', UOPT_REQUIRES_ARG), michael@0: #endif michael@0: UOPTION_DEF("filename", 'f', UOPT_REQUIRES_ARG), michael@0: UOPTION_DEF("assembly", 'a', UOPT_REQUIRES_ARG) michael@0: }; michael@0: michael@0: #define CALL_WRITECCODE 'c' michael@0: #define CALL_WRITEASSEMBLY 'a' michael@0: #define CALL_WRITEOBJECT 'o' michael@0: extern int michael@0: main(int argc, char* argv[]) { michael@0: UBool verbose = TRUE; michael@0: char writeCode; michael@0: michael@0: U_MAIN_INIT_ARGS(argc, argv); michael@0: michael@0: options[kOptDestDir].value = "."; michael@0: michael@0: /* read command line options */ michael@0: argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); michael@0: michael@0: /* error handling, printing usage message */ michael@0: if(argc<0) { michael@0: fprintf(stderr, michael@0: "error in command line argument \"%s\"\n", michael@0: argv[-argc]); michael@0: } michael@0: if(argc<0 || options[kOptHelpH].doesOccur || options[kOptHelpQuestionMark].doesOccur) { michael@0: fprintf(stderr, michael@0: "usage: %s [-options] filename1 filename2 ...\n" michael@0: "\tread each binary input file and \n" michael@0: "\tcreate a .c file with a byte array that contains the input file's data\n" michael@0: "options:\n" michael@0: "\t-h or -? or --help this usage text\n" michael@0: "\t-d or --destdir destination directory, followed by the path\n" michael@0: "\t-n or --name symbol prefix, followed by the prefix\n" michael@0: "\t-e or --entrypoint entry point name, followed by the name (_dat will be appended)\n" michael@0: "\t-r or --revision Specify a version\n" michael@0: , argv[0]); michael@0: #ifdef CAN_GENERATE_OBJECTS michael@0: fprintf(stderr, michael@0: "\t-o or --object write a .obj file instead of .c\n" michael@0: "\t-m or --match-arch file.o match the architecture (CPU, 32/64 bits) of the specified .o\n" michael@0: "\t ELF format defaults to i386. Windows defaults to the native platform.\n"); michael@0: #endif michael@0: fprintf(stderr, michael@0: "\t-f or --filename Specify an alternate base filename. (default: symbolname_typ)\n" michael@0: "\t-a or --assembly Create assembly file. (possible values are: "); michael@0: michael@0: printAssemblyHeadersToStdErr(); michael@0: } else { michael@0: const char *message, *filename; michael@0: /* TODO: remove void (*writeCode)(const char *, const char *); */ michael@0: michael@0: if(options[kOptAssembly].doesOccur) { michael@0: message="generating assembly code for %s\n"; michael@0: writeCode = CALL_WRITEASSEMBLY; michael@0: /* TODO: remove writeCode=&writeAssemblyCode; */ michael@0: michael@0: if (!checkAssemblyHeaderName(options[kOptAssembly].value)) { michael@0: fprintf(stderr, michael@0: "Assembly type \"%s\" is unknown.\n", options[kOptAssembly].value); michael@0: return -1; michael@0: } michael@0: } michael@0: #ifdef CAN_GENERATE_OBJECTS michael@0: else if(options[kOptObject].doesOccur) { michael@0: message="generating object code for %s\n"; michael@0: writeCode = CALL_WRITEOBJECT; michael@0: /* TODO: remove writeCode=&writeObjectCode; */ michael@0: } michael@0: #endif michael@0: else michael@0: { michael@0: message="generating C code for %s\n"; michael@0: writeCode = CALL_WRITECCODE; michael@0: /* TODO: remove writeCode=&writeCCode; */ michael@0: } michael@0: while(--argc) { michael@0: filename=getLongPathname(argv[argc]); michael@0: if (verbose) { michael@0: fprintf(stdout, message, filename); michael@0: } michael@0: michael@0: switch (writeCode) { michael@0: case CALL_WRITECCODE: michael@0: writeCCode(filename, options[kOptDestDir].value, michael@0: options[kOptName].doesOccur ? options[kOptName].value : NULL, michael@0: options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL, michael@0: NULL); michael@0: break; michael@0: case CALL_WRITEASSEMBLY: michael@0: writeAssemblyCode(filename, options[kOptDestDir].value, michael@0: options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL, michael@0: options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL, michael@0: NULL); michael@0: break; michael@0: #ifdef CAN_GENERATE_OBJECTS michael@0: case CALL_WRITEOBJECT: michael@0: writeObjectCode(filename, options[kOptDestDir].value, michael@0: options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL, michael@0: options[kOptMatchArch].doesOccur ? options[kOptMatchArch].value : NULL, michael@0: options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL, michael@0: NULL); michael@0: break; michael@0: #endif michael@0: default: michael@0: /* Should never occur. */ michael@0: break; michael@0: } michael@0: /* TODO: remove writeCode(filename, options[kOptDestDir].value); */ michael@0: } michael@0: } michael@0: michael@0: return 0; michael@0: }