Kannel: Open Source WAP and SMS gateway  svn-r5335
wmlsc.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "gwlib/gwlib.h"
#include "ws.h"

Go to the source code of this file.

Functions

static void usage (void)
 
static void pragma_meta (const WsUtf8String *property_name, const WsUtf8String *content, const WsUtf8String *scheme, void *context)
 
int main (int argc, char *argv[])
 

Variables

static char * program
 
static int eval_data = 0
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 114 of file wmlsc.c.

References eval_data, free(), getopt(), malloc(), WsCompilerParamsRec::meta_http_equiv_cb, WsCompilerParamsRec::meta_http_equiv_cb_context, WsCompilerParamsRec::meta_name_cb, WsCompilerParamsRec::meta_name_cb_context, optind, pragma_meta(), WsCompilerParamsRec::print_assembler, WsCompilerParamsRec::print_symbolic_assembler, program, usage(), WsCompilerParamsRec::use_latin1_strings, WsCompilerParamsRec::verbose, ws_compile_data(), ws_compile_file(), ws_create(), ws_destroy(), ws_free_byte_code(), WS_OK, and ws_result_to_string().

115 {
116  int i;
117  WsCompilerParams params;
118  WsCompilerPtr compiler;
119  WsResult result;
120  int opt;
121 
122  program = strrchr(argv[0], '/');
123  if (program)
124  program++;
125  else
126  program = argv[0];
127 
128  /* Initialize the parameters structure. The command line options
129  modify this directly. */
130  memset(&params, 0, sizeof(params));
131 
132  /* Process command line arguments. */
133  while ((opt = getopt(argc, argv, "adhsv")) != EOF) {
134  switch (opt) {
135  case 'a':
136  params.print_assembler = 1;
137  break;
138 
139  case 'd':
140  eval_data = 1;
141  break;
142 
143  case 'h':
144  usage();
145  exit(0);
146  break;
147 
148  case 'l':
149  params.use_latin1_strings = 1;
150  break;
151 
152  case 'p':
153  params.meta_name_cb = pragma_meta;
154  params.meta_name_cb_context = "meta name";
155 
157  params.meta_http_equiv_cb_context = "meta http equiv";
158  break;
159 
160  case 's':
161  params.print_symbolic_assembler = 1;
162  break;
163 
164  case 'v':
165  params.verbose = 1;
166  break;
167 
168  case '?':
169  printf("Try `%s -h' for a complete list of options.\n",
170  program);
171  exit(1);
172  }
173  }
174 
175  /* Create compiler. */
176 
177  compiler = ws_create(&params);
178  if (compiler == NULL) {
179  fprintf(stderr, "wsc: could not create compiler\n");
180  exit(1);
181  }
182 
183  for (i = optind; i < argc; i++) {
184  FILE *ifp, *ofp;
185  char *outname;
186 
187  ifp = fopen(argv[i], "rb");
188  if (ifp == NULL) {
189  fprintf(stderr, "wsc: could not open input file `%s': %s'\n",
190  argv[i], strerror(errno));
191  exit(1);
192  }
193 
194  /* Create the output name. */
195  outname = malloc(strlen(argv[i]) + 1 + 1);
196  if (outname == NULL) {
197  fprintf(stderr, "wmlsc: could not create output file name: %s\n",
198  strerror(errno));
199  exit(1);
200  }
201  strcpy(outname, argv[i]);
202  strcat(outname, "c");
203 
204  ofp = fopen(outname, "wb");
205  if (ofp == NULL) {
206  fprintf(stderr, "wsc: could not create output file `%s': %s\n",
207  outname, strerror(errno));
208  exit(1);
209  }
210 
211  if (eval_data) {
212  /* Use the ws_compile_data() interface. */
213  struct stat stat_st;
214  unsigned char *data;
215  unsigned char *output;
216  size_t output_len;
217 
218  if (stat(argv[i], &stat_st) == -1) {
219  fprintf(stderr, "wsc: could not stat input file `%s': %s\n",
220  argv[i], strerror(errno));
221  exit(1);
222  }
223 
224  /* Allocate the input buffer. */
225  data = malloc(stat_st.st_size);
226  if (data == NULL) {
227  fprintf(stderr, "wsc: could not allocate input buffer: %s\n",
228  strerror(errno));
229  exit(1);
230  }
231  if (fread(data, 1, stat_st.st_size, ifp) < (size_t) stat_st.st_size) {
232  fprintf(stderr, "wsc: could not read data: %s\n",
233  strerror(errno));
234  exit(1);
235  }
236  result = ws_compile_data(compiler, argv[i], data, stat_st.st_size,
237  &output, &output_len);
238  if (result == WS_OK) {
239  /* Save the output to `ofp'. */
240  if (fwrite(output, 1, output_len, ofp) != output_len) {
241  fprintf(stderr,
242  "wsc: could not save output to file `%s': %s\n",
243  outname, strerror(errno));
244  exit(1);
245  }
246  }
247  free(data);
248  ws_free_byte_code(output);
249  } else {
250  /* Use the ws_compile_file() interface. */
251  result = ws_compile_file(compiler, argv[i], ifp, ofp);
252  }
253 
254  /* Common cleanup. */
255  fclose(ifp);
256  fclose(ofp);
257 
258  if (result != WS_OK) {
259  remove(outname);
260  fprintf(stderr, "wsc: compilation failed: %s\n",
261  ws_result_to_string(result));
262  exit(1);
263  }
264  free(outname);
265  }
266 
267  ws_destroy(compiler);
268 
269  return 0;
270 }
static void pragma_meta(const WsUtf8String *property_name, const WsUtf8String *content, const WsUtf8String *scheme, void *context)
Definition: wmlsc.c:292
static void usage(void)
Definition: wmlsc.c:274
unsigned int print_symbolic_assembler
Definition: ws.h:154
void ws_free_byte_code(unsigned char *byte_code)
Definition: ws.c:228
WsResult ws_compile_file(WsCompilerPtr compiler, const char *input_name, FILE *input, FILE *output)
Definition: ws.c:177
int optind
Definition: attgetopt.c:80
WsCompilerPtr ws_create(WsCompilerParams *params)
Definition: ws.c:135
unsigned int use_latin1_strings
Definition: ws.h:109
void * malloc(YYSIZE_T)
static char * program
Definition: wmlsc.c:107
WsResult
Definition: ws.h:203
int getopt(int argc, char **argv, char *opts)
Definition: attgetopt.c:84
static int eval_data
Definition: wmlsc.c:110
void ws_destroy(WsCompilerPtr compiler)
Definition: ws.c:163
WsResult ws_compile_data(WsCompilerPtr compiler, const char *input_name, const unsigned char *input, size_t input_len, unsigned char **output_return, size_t *output_len_return)
Definition: ws.c:206
void free(void *)
Definition: ws.h:206
unsigned int print_assembler
Definition: ws.h:157
void * meta_name_cb_context
Definition: ws.h:174
WsPragmaMetaProc meta_name_cb
Definition: ws.h:173
WsPragmaMetaProc meta_http_equiv_cb
Definition: ws.h:178
const char * ws_result_to_string(WsResult result)
Definition: ws.c:234
void * meta_http_equiv_cb_context
Definition: ws.h:179
unsigned int verbose
Definition: ws.h:151

◆ pragma_meta()

static void pragma_meta ( const WsUtf8String property_name,
const WsUtf8String content,
const WsUtf8String scheme,
void *  context 
)
static

Definition at line 292 of file wmlsc.c.

References ws_utf8_free_data(), and ws_utf8_to_latin1().

Referenced by main().

295 {
296  FILE *fp = stdout;
297  char *what = (char *) context;
298  char *property_name_l = (char *) ws_utf8_to_latin1(property_name, '?', NULL);
299  char *content_l = (char *) ws_utf8_to_latin1(content, '?', NULL);
300  char *scheme_l = (char *) ws_utf8_to_latin1(scheme, '?', NULL);
301 
302  fprintf(fp, "%s: name=\"%s\", content=\"%s\",",
303  what,
304  property_name_l ? property_name_l : "",
305  content_l ? content_l : "");
306 
307  if (scheme)
308  fprintf(fp, ", scheme=\"%s\"",
309  scheme_l ? scheme_l : "");
310 
311  fprintf(fp, "\n");
312 
313  ws_utf8_free_data((unsigned char *) property_name_l);
314  ws_utf8_free_data((unsigned char *) content_l);
315  ws_utf8_free_data((unsigned char *) scheme_l);
316 }
Definition: parse.c:65
void ws_utf8_free_data(unsigned char *data)
Definition: wsutf8.c:368
unsigned char * ws_utf8_to_latin1(const WsUtf8String *string, unsigned char unknown_char, size_t *len_return)
Definition: wsutf8.c:332

◆ usage()

static void usage ( void  )
static

Definition at line 274 of file wmlsc.c.

References program.

Referenced by main().

275 {
276  printf("Usage: %s OPTION... FILE...\n\
277  \n\
278  -a disassemble resulting byte-code and print it to the\n\
279  standard output\n\
280  -d use ws_eval_data() function instead of ws_eval_file()\n\
281  -h print this help message and exit successfully\n\
282  -l encode strings in ISO-8859/1 (ISO latin1) instead of using\n\
283  UTF-8\n\
284  -p print pragmas\n\
285  -s print symbolic byte-code assembler to the standard output\n\
286  -v print verbose progress messages\n\
287  \n",
288  program);
289 }
static char * program
Definition: wmlsc.c:107

Variable Documentation

◆ eval_data

int eval_data = 0
static

Definition at line 110 of file wmlsc.c.

Referenced by main().

◆ program

char* program
static

Definition at line 107 of file wmlsc.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.