Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wmlsdasm.c File Reference
#include "wsint.h"
#include "gwlib/gwlib.h"
#include <sys/stat.h>

Go to the source code of this file.

Functions

static void usage (void)
 
const char * lookup_function (WsBc *bc, WsUInt8 index)
 
int main (int argc, char *argv[])
 

Variables

static char * program
 

Function Documentation

const char * lookup_function ( WsBc bc,
WsUInt8  index 
)

Definition at line 311 of file wmlsdasm.c.

References WsBcRec::function_names, WsBcFunctionNameRec::index, WsBcFunctionNameRec::name, and WsBcRec::num_function_names.

Referenced by main().

312 {
313  WsUInt8 i;
314 
315  for (i = 0; i < bc->num_function_names; i++)
316  if (bc->function_names[i].index == index)
317  return bc->function_names[i].name;
318 
319  return NULL;
320 }
WsBcFunctionName * function_names
Definition: wsbc.h:204
unsigned char WsUInt8
Definition: wsint.h:116
WsUInt8 num_function_names
Definition: wsbc.h:203
WsUInt8 index
Definition: wsbc.h:165
int main ( int  argc,
char *  argv[] 
)

Definition at line 97 of file wmlsdasm.c.

References WsBcFunctionRec::code, WsBcFunctionRec::code_size, WsBcRec::constants, WsBcRec::function_names, WsBcRec::functions, getopt(), WsBcFunctionNameRec::index, lookup_function(), WsBcFunctionNameRec::name, name, WsBcRec::num_constants, WsBcRec::num_function_names, WsBcRec::num_functions, WsBcRec::num_pragmas, optind, program, WsBcConstantRec::type, WsBcConstantRec::u, usage(), WsBcConstantRec::v_float, WsBcConstantRec::v_int, WsBcConstantRec::v_string, ws_asm_dasm(), WS_BC_CONST_TYPE_EMPTY_STRING, WS_BC_CONST_TYPE_FLOAT32, WS_BC_CONST_TYPE_FLOAT32_NAN, WS_BC_CONST_TYPE_FLOAT32_NEGATIVE_INF, WS_BC_CONST_TYPE_FLOAT32_POSITIVE_INF, WS_BC_CONST_TYPE_INT, WS_BC_CONST_TYPE_UTF8_STRING, ws_bc_decode(), ws_bc_free(), ws_create(), ws_destroy(), WS_FALSE, ws_malloc(), WS_TRUE, and ws_utf8_get_char().

98 {
99  int i;
100  int opt;
101  WsBool print_constants = WS_FALSE;
102  WsBool print_function_names = WS_FALSE;
103  WsBool print_functions = WS_FALSE;
104  WsUInt8 k;
105  WsUInt16 j;
106 
107  program = strrchr(argv[0], '/');
108  if (program)
109  program++;
110  else
111  program = argv[0];
112 
113  /* Process command line arguments. */
114  while ((opt = getopt(argc, argv, "cfnh")) != EOF) {
115  switch (opt) {
116  case 'c':
117  print_constants = WS_TRUE;
118  break;
119 
120  case 'f':
121  print_functions = WS_TRUE;
122  break;
123 
124  case 'n':
125  print_function_names = WS_TRUE;
126  break;
127 
128  case 'h':
129  usage();
130  exit(0);
131  break;
132 
133  case '?':
134  printf("Try `%s -h' for a complete list of options.\n",
135  program);
136  exit(1);
137  }
138  }
139 
140  for (i = optind; i < argc; i++) {
141  FILE *fp;
142  struct stat stat_st;
143  unsigned char *data;
144  size_t data_len;
145  WsBc *bc;
146 
147  if (stat(argv[i], &stat_st) < 0) {
148  fprintf(stderr, "%s: could not access `%s': %s\n",
149  program, argv[i], strerror(errno));
150  exit(1);
151  }
152  data_len = stat_st.st_size;
153 
154  data = ws_malloc(data_len);
155  if (data == NULL) {
156  fprintf(stderr, "%s: out of memory: %s\n",
157  program, strerror(errno));
158  exit(1);
159  }
160 
161  fp = fopen(argv[i], "rb");
162  if (fp == NULL) {
163  fprintf(stderr, "%s: could not open input file `%s': %s\n",
164  program, argv[i], strerror(errno));
165  exit(1);
166  }
167 
168  if (fread(data, 1, data_len, fp) != data_len) {
169  fprintf(stderr, "%s: could not read file `%s': %s\n",
170  program, argv[i], strerror(errno));
171  exit(1);
172  }
173  fclose(fp);
174 
175  /* Decode byte-code. */
176  bc = ws_bc_decode(data, data_len);
177  if (bc == NULL) {
178  fprintf(stderr, "%s: invalid byte-code file `%s'\n",
179  program, argv[i]);
180  continue;
181  }
182 
183  /* Print all requested data. */
184  printf("\n%s:\t%lu bytes\n\n", argv[i], (unsigned long) data_len);
185 
186  if (print_constants) {
187  printf("Disassembly of section Constants:\n\n");
188  for (j = 0; j < bc->num_constants; j++) {
189  printf("%4d:\t", j);
190  switch (bc->constants[j].type) {
192  printf("%ld\n", (long) bc->constants[j].u.v_int);
193  break;
194 
196  printf("%f\n", bc->constants[j].u.v_float);
197  break;
198 
200  printf("NaN\n");
201  break;
202 
204  printf("+infinity\n");
205  break;
206 
208  printf("-infinity\n");
209  break;
210 
212  {
213  size_t pos = 0;
214  size_t column = 8;
215  unsigned long ch;
216 
217  printf("\"");
218 
219  while (ws_utf8_get_char(&bc->constants[j].u.v_string,
220  &ch, &pos)) {
221  if (ch < ' ') {
222  printf("\\%02lx", ch);
223  column += 3;
224  } else if (ch <= 0xff) {
225  printf("%c", (unsigned char) ch);
226  column++;
227  } else {
228  printf("\\u%04lx", ch);
229  column += 5;
230  }
231  if (column >= 75) {
232  printf("\"\n\t\"");
233  column = 8;
234  }
235  }
236  printf("\"\n");
237  }
238  break;
239 
241  printf("\"\"\n");
242  break;
243  }
244  }
245  }
246 
247  if (print_function_names) {
248  printf("Disassembly of section Function names:\n\n");
249  for (k = 0; k < bc->num_function_names; k++)
250  printf(" %-40.40s%8d\n",
251  bc->function_names[k].name,
252  bc->function_names[k].index);
253  }
254 
255  if (print_functions) {
256  WsCompilerPtr compiler = ws_create(NULL);
257 
258  printf("Disassembly of section Functions:\n");
259 
260  for (k = 0; k < bc->num_functions; k++) {
261  const char *name = lookup_function(bc, k);
262 
263  printf("\nFunction %u", (unsigned int) k);
264 
265  if (name)
266  printf(" <%s>", name);
267 
268  printf(":\n");
269 
270  ws_asm_dasm(compiler, bc->functions[k].code,
271  bc->functions[k].code_size);
272  }
273 
274  ws_destroy(compiler);
275  }
276 
277  if (!print_constants && !print_function_names && !print_functions) {
278  printf("Sections:\n\
279  Name\t\t Items\n\
280  Constants\t%8d\n\
281  Pragmas\t%8d\n\
282  Function names\t%8d\n\
283  Functions\t%8d\n",
284  bc->num_constants,
285  bc->num_pragmas,
286  bc->num_function_names,
287  bc->num_functions);
288  }
289 
290  ws_bc_free(bc);
291  }
292 
293  return 0;
294 }
WsBcConstantType type
Definition: wsbc.h:121
WsUInt16 num_constants
Definition: wsbc.h:194
Definition: wsint.h:131
static char * program
Definition: wmlsdasm.c:93
WsBcFunction * functions
Definition: wsbc.h:207
WsInt32 v_int
Definition: wsbc.h:125
int optind
Definition: attgetopt.c:80
WsCompilerPtr ws_create(WsCompilerParams *params)
Definition: ws.c:135
WsUInt8 num_functions
Definition: wsbc.h:206
WsBcConstant * constants
Definition: wsbc.h:195
WsBcFunctionName * function_names
Definition: wsbc.h:204
WsUtf8String v_string
Definition: wsbc.h:127
WsFloat v_float
Definition: wsbc.h:126
static void usage(void)
Definition: wmlsdasm.c:298
unsigned char WsUInt8
Definition: wsint.h:116
int getopt(int argc, char **argv, char *opts)
Definition: attgetopt.c:84
WsUInt8 num_function_names
Definition: wsbc.h:203
void ws_destroy(WsCompilerPtr compiler)
Definition: ws.c:163
unsigned short WsUInt16
Definition: wsint.h:119
void ws_bc_free(WsBc *bc)
Definition: wsbc.c:97
WsUInt32 code_size
Definition: wsbc.h:179
void ws_asm_dasm(WsCompilerPtr compiler, const unsigned char *code, size_t len)
Definition: wsasm.c:185
char * name
Definition: smsc_cimd2.c:212
int ws_utf8_get_char(const WsUtf8String *string, unsigned long *ch_return, size_t *posp)
Definition: wsutf8.c:293
WsBool
Definition: wsint.h:128
WsUInt16 num_pragmas
Definition: wsbc.h:198
union WsBcConstantRec::@116 u
unsigned char * code
Definition: wsbc.h:180
Definition: wsbc.h:186
const char * lookup_function(WsBc *bc, WsUInt8 index)
Definition: wmlsdasm.c:311
void * ws_malloc(size_t size)
Definition: wsalloc.c:77
WsBc * ws_bc_decode(const unsigned char *data, size_t data_len)
Definition: wsbc.c:457
WsUInt8 index
Definition: wsbc.h:165
static void usage ( void  )
static

Definition at line 298 of file wmlsdasm.c.

References program.

Referenced by main().

299 {
300  printf("Usage: %s OPTION... FILE...\n\
301  \n\
302  -c print constants\n\
303  -f disassemble functions\n\
304  -n print function names\n\
305  -h print this help message and exit successfully\n\
306  \n",
307  program);
308 }
static char * program
Definition: wmlsdasm.c:93

Variable Documentation

char* program
static

Definition at line 93 of file wmlsdasm.c.

Referenced by main(), and usage().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.