• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4  *******************************************************************************
5  *   Copyright (C) 1999-2016, International Business Machines
6  *   Corporation and others.  All Rights Reserved.
7  *******************************************************************************
8  *   file name:  gennames.c
9  *   encoding:   UTF-8
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  */
21 
22 #include "unicode/utypes.h"
23 
24 #if U_PLATFORM_HAS_WIN32_API
25 #   define VC_EXTRALEAN
26 #   define WIN32_LEAN_AND_MEAN
27 #   define NOUSER
28 #   define NOSERVICE
29 #   define NOIME
30 #   define NOMCX
31 #include <windows.h>
32 #include <time.h>
33 #endif
34 
35 #if U_PLATFORM_IS_LINUX_BASED && U_HAVE_ELF_H
36 #   define U_ELF
37 #endif
38 
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
49 #endif
50 
51 #include <stdbool.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include "unicode/putil.h"
55 #include "cmemory.h"
56 #include "cstring.h"
57 #include "filestrm.h"
58 #include "toolutil.h"
59 #include "unicode/uclean.h"
60 #include "uoptions.h"
61 #include "pkg_genc.h"
62 
63 enum {
64   kOptHelpH = 0,
65   kOptHelpQuestionMark,
66   kOptDestDir,
67   kOptQuiet,
68   kOptName,
69   kOptEntryPoint,
70 #ifdef CAN_GENERATE_OBJECTS
71   kOptObject,
72   kOptMatchArch,
73   kOptSkipDllExport,
74 #endif
75   kOptFilename,
76   kOptAssembly
77 };
78 
79 static UOption options[]={
80 /*0*/UOPTION_HELP_H,
81      UOPTION_HELP_QUESTION_MARK,
82      UOPTION_DESTDIR,
83      UOPTION_QUIET,
84      UOPTION_DEF("name", 'n', UOPT_REQUIRES_ARG),
85      UOPTION_DEF("entrypoint", 'e', UOPT_REQUIRES_ARG),
86 #ifdef CAN_GENERATE_OBJECTS
87 /*6*/UOPTION_DEF("object", 'o', UOPT_NO_ARG),
88      UOPTION_DEF("match-arch", 'm', UOPT_REQUIRES_ARG),
89      UOPTION_DEF("skip-dll-export", '\0', UOPT_NO_ARG),
90 #endif
91      UOPTION_DEF("filename", 'f', UOPT_REQUIRES_ARG),
92      UOPTION_DEF("assembly", 'a', UOPT_REQUIRES_ARG)
93 };
94 
95 #define CALL_WRITECCODE     'c'
96 #define CALL_WRITEASSEMBLY  'a'
97 #define CALL_WRITEOBJECT    'o'
98 extern int
main(int argc,char * argv[])99 main(int argc, char* argv[]) {
100     UBool verbose = true;
101     char writeCode;
102 
103     U_MAIN_INIT_ARGS(argc, argv);
104 
105     options[kOptDestDir].value = ".";
106 
107     /* read command line options */
108     argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
109 
110     /* error handling, printing usage message */
111     if(argc<0) {
112         fprintf(stderr,
113             "error in command line argument \"%s\"\n",
114             argv[-argc]);
115     }
116     if(argc<0 || options[kOptHelpH].doesOccur || options[kOptHelpQuestionMark].doesOccur) {
117         fprintf(stderr,
118             "usage: %s [-options] filename1 filename2 ...\n"
119             "\tread each binary input file and \n"
120             "\tcreate a .c file with a byte array that contains the input file's data\n"
121             "options:\n"
122             "\t-h or -? or --help  this usage text\n"
123             "\t-d or --destdir     destination directory, followed by the path\n"
124             "\t-q or --quiet       do not display warnings and progress\n"
125             "\t-n or --name        symbol prefix, followed by the prefix\n"
126             "\t-e or --entrypoint  entry point name, followed by the name (_dat will be appended)\n"
127             "\t-r or --revision    Specify a version\n"
128             , argv[0]);
129 #ifdef CAN_GENERATE_OBJECTS
130         fprintf(stderr,
131             "\t-o or --object      write a .obj file instead of .c\n"
132             "\t-m or --match-arch file.o  match the architecture (CPU, 32/64 bits) of the specified .o\n"
133             "\t                    ELF format defaults to i386. Windows defaults to the native platform.\n"
134             "\t--skip-dll-export   Don't export the ICU data entry point symbol (for use when statically linking)\n");
135 #endif
136         fprintf(stderr,
137             "\t-f or --filename    Specify an alternate base filename. (default: symbolname_typ)\n"
138             "\t-a or --assembly    Create assembly file. (possible values are: ");
139 
140         printAssemblyHeadersToStdErr();
141     } else {
142         const char *message, *filename;
143         /* TODO: remove void (*writeCode)(const char *, const char *); */
144 
145         if(options[kOptAssembly].doesOccur) {
146             message="generating assembly code for %s\n";
147             writeCode = CALL_WRITEASSEMBLY;
148             /* TODO: remove writeCode=&writeAssemblyCode; */
149 
150             if (!checkAssemblyHeaderName(options[kOptAssembly].value)) {
151                 fprintf(stderr,
152                     "Assembly type \"%s\" is unknown.\n", options[kOptAssembly].value);
153                 return -1;
154             }
155         }
156 #ifdef CAN_GENERATE_OBJECTS
157         else if(options[kOptObject].doesOccur) {
158             message="generating object code for %s\n";
159             writeCode = CALL_WRITEOBJECT;
160             /* TODO: remove writeCode=&writeObjectCode; */
161         }
162 #endif
163         else
164         {
165             message="generating C code for %s\n";
166             writeCode = CALL_WRITECCODE;
167             /* TODO: remove writeCode=&writeCCode; */
168         }
169         if (options[kOptQuiet].doesOccur) {
170             verbose = false;
171         }
172         while(--argc) {
173             filename=getLongPathname(argv[argc]);
174             if (verbose) {
175                 fprintf(stdout, message, filename);
176             }
177 
178             switch (writeCode) {
179             case CALL_WRITECCODE:
180                 writeCCode(filename, options[kOptDestDir].value,
181                            options[kOptName].doesOccur ? options[kOptName].value : NULL,
182                            options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL,
183                            NULL,
184                            0);
185                 break;
186             case CALL_WRITEASSEMBLY:
187                 writeAssemblyCode(filename, options[kOptDestDir].value,
188                                   options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL,
189                                   options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL,
190                                   NULL,
191                                   0);
192                 break;
193 #ifdef CAN_GENERATE_OBJECTS
194             case CALL_WRITEOBJECT:
195                 writeObjectCode(filename, options[kOptDestDir].value,
196                                 options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL,
197                                 options[kOptMatchArch].doesOccur ? options[kOptMatchArch].value : NULL,
198                                 options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL,
199                                 NULL,
200                                 0,
201                                 !options[kOptSkipDllExport].doesOccur);
202                 break;
203 #endif
204             default:
205                 /* Should never occur. */
206                 break;
207             }
208             /* TODO: remove writeCode(filename, options[kOptDestDir].value); */
209         }
210     }
211 
212     return 0;
213 }
214