wmlsdasm.c
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:7k
源码类别:

手机WAP编程

开发平台:

WINDOWS

  1. /*
  2.  *
  3.  * wmlsdasm.c
  4.  *
  5.  * Author: Markku Rossi <mtr@iki.fi>
  6.  *
  7.  * Copyright (c) 2000 WAPIT OY LTD.
  8.  *  All rights reserved.
  9.  *
  10.  * Disassembler for WMLScript byte-code.
  11.  *
  12.  */
  13. #include "wsint.h"
  14. #include "gwlib/gwlib.h"
  15. #include <sys/stat.h>
  16. /* TODO:
  17.      - print pragmas
  18.      - option to disassemble only a named external function
  19.      - print constants in assembler => wsasm.c */
  20. /********************* Prototypes for static functions ******************/
  21. /* Print usage message to the stdout. */
  22. static void usage(void);
  23. /* Lookup the name of the function `index' from the function names
  24.    section of the byte-code structure `bc'.  The function returns NULL
  25.    if the function is internal. */
  26. const char *lookup_function(WsBc *bc, WsUInt8 index);
  27. /********************* Static variables *********************************/
  28. /* The name of the compiler program. */
  29. static char *program;
  30. /********************* Global functions *********************************/
  31. int main(int argc, char *argv[])
  32. {
  33.     int i;
  34.     int opt;
  35.     WsBool print_constants = WS_FALSE;
  36.     WsBool print_function_names = WS_FALSE;
  37.     WsBool print_functions = WS_FALSE;
  38.     WsUInt8 k;
  39.     WsUInt16 j;
  40.     program = strrchr(argv[0], '/');
  41.     if (program)
  42.         program++;
  43.     else
  44.         program = argv[0];
  45.     /* Process command line arguments. */
  46.     while ((opt = getopt(argc, argv, "cfnh")) != EOF) {
  47.         switch (opt) {
  48.         case 'c':
  49.             print_constants = WS_TRUE;
  50.             break;
  51.         case 'f':
  52.             print_functions = WS_TRUE;
  53.             break;
  54.         case 'n':
  55.             print_function_names = WS_TRUE;
  56.             break;
  57.         case 'h':
  58.             usage();
  59.             exit(0);
  60.             break;
  61.         case '?':
  62.             printf("Try `%s -h' for a complete list of options.n",
  63.                    program);
  64.             exit(1);
  65.         }
  66.     }
  67.     for (i = optind; i < argc; i++) {
  68.         FILE *fp;
  69.         struct stat stat_st;
  70.         unsigned char *data;
  71.         size_t data_len;
  72.         WsBc *bc;
  73.         if (stat(argv[i], &stat_st) < 0) {
  74.             fprintf(stderr, "%s: could not access `%s': %sn",
  75.                     program, argv[i], strerror(errno));
  76.             exit(1);
  77.         }
  78.         data_len = stat_st.st_size;
  79.         data = ws_malloc(data_len);
  80.         if (data == NULL) {
  81.             fprintf(stderr, "%s: out of memory: %sn",
  82.                     program, strerror(errno));
  83.             exit(1);
  84.         }
  85.         fp = fopen(argv[i], "rb");
  86.         if (fp == NULL) {
  87.             fprintf(stderr, "%s: could not open input file `%s': %sn",
  88.                     program, argv[i], strerror(errno));
  89.             exit(1);
  90.         }
  91.         if (fread(data, 1, data_len, fp) != data_len) {
  92.             fprintf(stderr, "%s: could not read file `%s': %sn",
  93.                     program, argv[i], strerror(errno));
  94.             exit(1);
  95.         }
  96.         fclose(fp);
  97.         /* Decode byte-code. */
  98.         bc = ws_bc_decode(data, data_len);
  99.         if (bc == NULL) {
  100.             fprintf(stderr, "%s: invalid byte-code file `%s'n",
  101.                     program, argv[i]);
  102.             continue;
  103.         }
  104.         /* Print all requested data. */
  105.         printf("n%s:t%lu bytesnn", argv[i], (unsigned long) data_len);
  106.         if (print_constants) {
  107.             printf("Disassembly of section Constants:nn");
  108.             for (j = 0; j < bc->num_constants; j++) {
  109.                 printf("%4d:t", j);
  110.                 switch (bc->constants[j].type) {
  111.                 case WS_BC_CONST_TYPE_INT:
  112.                     printf("%ldn", (long) bc->constants[j].u.v_int);
  113.                     break;
  114.                 case WS_BC_CONST_TYPE_FLOAT32:
  115.                     printf("%fn", bc->constants[j].u.v_float);
  116.                     break;
  117.                 case WS_BC_CONST_TYPE_FLOAT32_NAN:
  118.                     printf("NaNn");
  119.                     break;
  120.                 case WS_BC_CONST_TYPE_FLOAT32_POSITIVE_INF:
  121.                     printf("+infinityn");
  122.                     break;
  123.                 case WS_BC_CONST_TYPE_FLOAT32_NEGATIVE_INF:
  124.                     printf("-infinityn");
  125.                     break;
  126.                 case WS_BC_CONST_TYPE_UTF8_STRING:
  127.                     {
  128.                         size_t pos = 0;
  129.                         size_t column = 8;
  130.                         unsigned long ch;
  131.                         printf(""");
  132.                         while (ws_utf8_get_char(&bc->constants[j].u.v_string,
  133.                                                 &ch, &pos)) {
  134.                             if (ch < ' ') {
  135.                                 printf("\%02lx", ch);
  136.                                 column += 3;
  137.                             } else if (ch <= 0xff) {
  138.                                 printf("%c", (unsigned char) ch);
  139.                                 column++;
  140.                             } else {
  141.                                 printf("\u%04lx", ch);
  142.                                 column += 5;
  143.                             }
  144.                             if (column >= 75) {
  145.                                 printf(""nt"");
  146.                                 column = 8;
  147.                             }
  148.                         }
  149.                         printf(""n");
  150.                     }
  151.                     break;
  152.                 case WS_BC_CONST_TYPE_EMPTY_STRING:
  153.                     printf("""n");
  154.                     break;
  155.                 }
  156.             }
  157.         }
  158.         if (print_function_names) {
  159.             printf("Disassembly of section Function names:nn");
  160.             for (k = 0; k < bc->num_function_names; k++)
  161.                 printf("  %-40.40s%8dn",
  162.                        bc->function_names[k].name,
  163.                        bc->function_names[k].index);
  164.         }
  165.         if (print_functions) {
  166.             WsCompilerPtr compiler = ws_create(NULL);
  167.             printf("Disassembly of section Functions:n");
  168.             for (k = 0; k < bc->num_functions; k++) {
  169.                 const char *name = lookup_function(bc, k);
  170.                 printf("nFunction %u", (unsigned int) k);
  171.                 if (name)
  172.                     printf(" <%s>", name);
  173.                 printf(":n");
  174.                 ws_asm_dasm(compiler, bc->functions[k].code,
  175.                             bc->functions[k].code_size);
  176.             }
  177.             ws_destroy(compiler);
  178.         }
  179.         if (!print_constants && !print_function_names && !print_functions) {
  180.             printf("Sections:n
  181.                    Namett   Itemsn
  182.                    Constantst%8dn
  183.                    Pragmast%8dn
  184.                    Function namest%8dn
  185.                    Functionst%8dn",
  186.                    bc->num_constants,
  187.                    bc->num_pragmas,
  188.                    bc->num_function_names,
  189.                    bc->num_functions);
  190.         }
  191.         ws_bc_free(bc);
  192.     }
  193.     return 0;
  194. }
  195. /********************* Static functions *********************************/
  196. static void usage(void)
  197. {
  198.     printf("Usage: %s OPTION... FILE...n
  199.            n
  200.            -c            print constantsn
  201.            -f            disassemble functionsn
  202.            -n            print function namesn
  203.            -h            print this help message and exit successfullyn
  204.            n",
  205.            program);
  206. }
  207. const char *lookup_function(WsBc *bc, WsUInt8 index)
  208. {
  209.     WsUInt8 i;
  210.     for (i = 0; i < bc->num_function_names; i++)
  211.         if (bc->function_names[i].index == index)
  212.             return bc->function_names[i].name;
  213.     return NULL;
  214. }