• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *******************************************************************************
3  *   Copyright (C) 1999-2008, International Business Machines
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  */
19 
20 #include "unicode/utypes.h"
21 
22 #ifdef U_WINDOWS
23 #   define VC_EXTRALEAN
24 #   define WIN32_LEAN_AND_MEAN
25 #   define NOUSER
26 #   define NOSERVICE
27 #   define NOIME
28 #   define NOMCX
29 #include <windows.h>
30 #include <time.h>
31 #endif
32 
33 #ifdef U_LINUX
34 #   define U_ELF
35 #endif
36 
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
47 #endif
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include "unicode/putil.h"
52 #include "cmemory.h"
53 #include "cstring.h"
54 #include "filestrm.h"
55 #include "toolutil.h"
56 #include "unicode/uclean.h"
57 #include "uoptions.h"
58 #include "pkg_genc.h"
59 
60 #if defined(U_WINDOWS) || defined(U_ELF)
61 #define CAN_GENERATE_OBJECTS
62 #endif
63 
64 enum {
65   kOptHelpH = 0,
66   kOptHelpQuestionMark,
67   kOptDestDir,
68   kOptName,
69   kOptEntryPoint,
70 #ifdef CAN_GENERATE_OBJECTS
71   kOptObject,
72   kOptMatchArch,
73 #endif
74   kOptFilename,
75   kOptAssembly
76 };
77 
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),
83      UOPTION_DEF("entrypoint", 'e', UOPT_REQUIRES_ARG),
84 #ifdef CAN_GENERATE_OBJECTS
85 /*5*/UOPTION_DEF("object", 'o', UOPT_NO_ARG),
86      UOPTION_DEF("match-arch", 'm', UOPT_REQUIRES_ARG),
87 #endif
88      UOPTION_DEF("filename", 'f', UOPT_REQUIRES_ARG),
89      UOPTION_DEF("assembly", 'a', UOPT_REQUIRES_ARG)
90 };
91 
92 #define CALL_WRITECCODE     'c'
93 #define CALL_WRITEASSEMBLY  'a'
94 #define CALL_WRITEOBJECT    'o'
95 extern int
main(int argc,char * argv[])96 main(int argc, char* argv[]) {
97     UBool verbose = TRUE;
98     char writeCode;
99 
100     U_MAIN_INIT_ARGS(argc, argv);
101 
102     options[kOptDestDir].value = ".";
103 
104     /* read command line options */
105     argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
106 
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     }
113     if(argc<0 || options[kOptHelpH].doesOccur || options[kOptHelpQuestionMark].doesOccur) {
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"
122             "\t-e or --entrypoint  entry point name, followed by the name (_dat will be appended)\n"
123             "\t-r or --revision    Specify a version\n"
124             , argv[0]);
125 #ifdef CAN_GENERATE_OBJECTS
126         fprintf(stderr,
127             "\t-o or --object      write a .obj file instead of .c\n"
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");
130 #endif
131         fprintf(stderr,
132             "\t-f or --filename    Specify an alternate base filename. (default: symbolname_typ)\n"
133             "\t-a or --assembly    Create assembly file. (possible values are: ");
134 
135         printAssemblyHeadersToStdErr();
136     } else {
137         const char *message, *filename;
138         /* TODO: remove void (*writeCode)(const char *, const char *); */
139 
140         if(options[kOptAssembly].doesOccur) {
141             message="generating assembly code for %s\n";
142             writeCode = CALL_WRITEASSEMBLY;
143             /* TODO: remove writeCode=&writeAssemblyCode; */
144 
145             if (!checkAssemblyHeaderName(options[kOptAssembly].value)) {
146                 fprintf(stderr,
147                     "Assembly type \"%s\" is unknown.\n", options[kOptAssembly].value);
148                 return -1;
149             }
150         }
151 #ifdef CAN_GENERATE_OBJECTS
152         else if(options[kOptObject].doesOccur) {
153             message="generating object code for %s\n";
154             writeCode = CALL_WRITEOBJECT;
155             /* TODO: remove writeCode=&writeObjectCode; */
156         }
157 #endif
158         else
159         {
160             message="generating C code for %s\n";
161             writeCode = CALL_WRITECCODE;
162             /* TODO: remove writeCode=&writeCCode; */
163         }
164         while(--argc) {
165             filename=getLongPathname(argv[argc]);
166             if (verbose) {
167                 fprintf(stdout, message, filename);
168             }
169 
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;
183 #ifdef CAN_GENERATE_OBJECTS
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;
195             }
196             /* TODO: remove writeCode(filename, options[kOptDestDir].value); */
197         }
198     }
199 
200     return 0;
201 }
202