intl/icu/source/tools/genccode/genccode.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/tools/genccode/genccode.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,197 @@
     1.4 +/*
     1.5 + *******************************************************************************
     1.6 + *   Copyright (C) 1999-2013, International Business Machines
     1.7 + *   Corporation and others.  All Rights Reserved.
     1.8 + *******************************************************************************
     1.9 + *   file name:  gennames.c
    1.10 + *   encoding:   US-ASCII
    1.11 + *   tab size:   8 (not used)
    1.12 + *   indentation:4
    1.13 + *
    1.14 + *   created on: 1999nov01
    1.15 + *   created by: Markus W. Scherer
    1.16 + *
    1.17 + *   This program reads a binary file and creates a C source code file
    1.18 + *   with a byte array that contains the data of the binary file.
    1.19 + *
    1.20 + *   12/09/1999  weiv    Added multiple file handling
    1.21 + */
    1.22 +
    1.23 +#include "unicode/utypes.h"
    1.24 +
    1.25 +#if U_PLATFORM_HAS_WIN32_API
    1.26 +#   define VC_EXTRALEAN
    1.27 +#   define WIN32_LEAN_AND_MEAN
    1.28 +#   define NOUSER
    1.29 +#   define NOSERVICE
    1.30 +#   define NOIME
    1.31 +#   define NOMCX
    1.32 +#include <windows.h>
    1.33 +#include <time.h>
    1.34 +#endif
    1.35 +
    1.36 +#if U_PLATFORM_IS_LINUX_BASED && U_HAVE_ELF_H
    1.37 +#   define U_ELF
    1.38 +#endif
    1.39 +
    1.40 +#ifdef U_ELF
    1.41 +#   include <elf.h>
    1.42 +#   if defined(ELFCLASS64)
    1.43 +#       define U_ELF64
    1.44 +#   endif
    1.45 +    /* Old elf.h headers may not have EM_X86_64, or have EM_X8664 instead. */
    1.46 +#   ifndef EM_X86_64
    1.47 +#       define EM_X86_64 62
    1.48 +#   endif
    1.49 +#   define ICU_ENTRY_OFFSET 0
    1.50 +#endif
    1.51 +
    1.52 +#include <stdio.h>
    1.53 +#include <stdlib.h>
    1.54 +#include "unicode/putil.h"
    1.55 +#include "cmemory.h"
    1.56 +#include "cstring.h"
    1.57 +#include "filestrm.h"
    1.58 +#include "toolutil.h"
    1.59 +#include "unicode/uclean.h"
    1.60 +#include "uoptions.h"
    1.61 +#include "pkg_genc.h"
    1.62 +
    1.63 +enum {
    1.64 +  kOptHelpH = 0,
    1.65 +  kOptHelpQuestionMark,
    1.66 +  kOptDestDir,
    1.67 +  kOptName,
    1.68 +  kOptEntryPoint,
    1.69 +#ifdef CAN_GENERATE_OBJECTS
    1.70 +  kOptObject,
    1.71 +  kOptMatchArch,
    1.72 +#endif
    1.73 +  kOptFilename,
    1.74 +  kOptAssembly
    1.75 +};
    1.76 +
    1.77 +static UOption options[]={
    1.78 +/*0*/UOPTION_HELP_H,
    1.79 +     UOPTION_HELP_QUESTION_MARK,
    1.80 +     UOPTION_DESTDIR,
    1.81 +     UOPTION_DEF("name", 'n', UOPT_REQUIRES_ARG),
    1.82 +     UOPTION_DEF("entrypoint", 'e', UOPT_REQUIRES_ARG),
    1.83 +#ifdef CAN_GENERATE_OBJECTS
    1.84 +/*5*/UOPTION_DEF("object", 'o', UOPT_NO_ARG),
    1.85 +     UOPTION_DEF("match-arch", 'm', UOPT_REQUIRES_ARG),
    1.86 +#endif
    1.87 +     UOPTION_DEF("filename", 'f', UOPT_REQUIRES_ARG),
    1.88 +     UOPTION_DEF("assembly", 'a', UOPT_REQUIRES_ARG)
    1.89 +};
    1.90 +
    1.91 +#define CALL_WRITECCODE     'c'
    1.92 +#define CALL_WRITEASSEMBLY  'a'
    1.93 +#define CALL_WRITEOBJECT    'o'
    1.94 +extern int
    1.95 +main(int argc, char* argv[]) {
    1.96 +    UBool verbose = TRUE;
    1.97 +    char writeCode;
    1.98 +
    1.99 +    U_MAIN_INIT_ARGS(argc, argv);
   1.100 +
   1.101 +    options[kOptDestDir].value = ".";
   1.102 +
   1.103 +    /* read command line options */
   1.104 +    argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
   1.105 +
   1.106 +    /* error handling, printing usage message */
   1.107 +    if(argc<0) {
   1.108 +        fprintf(stderr,
   1.109 +            "error in command line argument \"%s\"\n",
   1.110 +            argv[-argc]);
   1.111 +    }
   1.112 +    if(argc<0 || options[kOptHelpH].doesOccur || options[kOptHelpQuestionMark].doesOccur) {
   1.113 +        fprintf(stderr,
   1.114 +            "usage: %s [-options] filename1 filename2 ...\n"
   1.115 +            "\tread each binary input file and \n"
   1.116 +            "\tcreate a .c file with a byte array that contains the input file's data\n"
   1.117 +            "options:\n"
   1.118 +            "\t-h or -? or --help  this usage text\n"
   1.119 +            "\t-d or --destdir     destination directory, followed by the path\n"
   1.120 +            "\t-n or --name        symbol prefix, followed by the prefix\n"
   1.121 +            "\t-e or --entrypoint  entry point name, followed by the name (_dat will be appended)\n"
   1.122 +            "\t-r or --revision    Specify a version\n"
   1.123 +            , argv[0]);
   1.124 +#ifdef CAN_GENERATE_OBJECTS
   1.125 +        fprintf(stderr,
   1.126 +            "\t-o or --object      write a .obj file instead of .c\n"
   1.127 +            "\t-m or --match-arch file.o  match the architecture (CPU, 32/64 bits) of the specified .o\n"
   1.128 +            "\t                    ELF format defaults to i386. Windows defaults to the native platform.\n");
   1.129 +#endif
   1.130 +        fprintf(stderr,
   1.131 +            "\t-f or --filename    Specify an alternate base filename. (default: symbolname_typ)\n"
   1.132 +            "\t-a or --assembly    Create assembly file. (possible values are: ");
   1.133 +
   1.134 +        printAssemblyHeadersToStdErr();
   1.135 +    } else {
   1.136 +        const char *message, *filename;
   1.137 +        /* TODO: remove void (*writeCode)(const char *, const char *); */
   1.138 +
   1.139 +        if(options[kOptAssembly].doesOccur) {
   1.140 +            message="generating assembly code for %s\n";
   1.141 +            writeCode = CALL_WRITEASSEMBLY;
   1.142 +            /* TODO: remove writeCode=&writeAssemblyCode; */
   1.143 +
   1.144 +            if (!checkAssemblyHeaderName(options[kOptAssembly].value)) {
   1.145 +                fprintf(stderr,
   1.146 +                    "Assembly type \"%s\" is unknown.\n", options[kOptAssembly].value);
   1.147 +                return -1;
   1.148 +            }
   1.149 +        }
   1.150 +#ifdef CAN_GENERATE_OBJECTS
   1.151 +        else if(options[kOptObject].doesOccur) {
   1.152 +            message="generating object code for %s\n";
   1.153 +            writeCode = CALL_WRITEOBJECT;
   1.154 +            /* TODO: remove writeCode=&writeObjectCode; */
   1.155 +        }
   1.156 +#endif
   1.157 +        else
   1.158 +        {
   1.159 +            message="generating C code for %s\n";
   1.160 +            writeCode = CALL_WRITECCODE;
   1.161 +            /* TODO: remove writeCode=&writeCCode; */
   1.162 +        }
   1.163 +        while(--argc) {
   1.164 +            filename=getLongPathname(argv[argc]);
   1.165 +            if (verbose) {
   1.166 +                fprintf(stdout, message, filename);
   1.167 +            }
   1.168 +
   1.169 +            switch (writeCode) {
   1.170 +            case CALL_WRITECCODE:
   1.171 +                writeCCode(filename, options[kOptDestDir].value,
   1.172 +                           options[kOptName].doesOccur ? options[kOptName].value : NULL,
   1.173 +                           options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL,
   1.174 +                           NULL);
   1.175 +                break;
   1.176 +            case CALL_WRITEASSEMBLY:
   1.177 +                writeAssemblyCode(filename, options[kOptDestDir].value,
   1.178 +                                  options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL,
   1.179 +                                  options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL,
   1.180 +                                  NULL);
   1.181 +                break;
   1.182 +#ifdef CAN_GENERATE_OBJECTS
   1.183 +            case CALL_WRITEOBJECT:
   1.184 +                writeObjectCode(filename, options[kOptDestDir].value,
   1.185 +                                options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL,
   1.186 +                                options[kOptMatchArch].doesOccur ? options[kOptMatchArch].value : NULL,
   1.187 +                                options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL,
   1.188 +                                NULL);
   1.189 +                break;
   1.190 +#endif
   1.191 +            default:
   1.192 +                /* Should never occur. */
   1.193 +                break;
   1.194 +            }
   1.195 +            /* TODO: remove writeCode(filename, options[kOptDestDir].value); */
   1.196 +        }
   1.197 +    }
   1.198 +
   1.199 +    return 0;
   1.200 +}

mercurial