Kannel: Open Source WAP and SMS gateway  svn-r5335
wml_tester.c File Reference
#include <stdlib.h>
#include <unistd.h>
#include "gwlib/gwlib.h"
#include "gw/wml_compiler.h"

Go to the source code of this file.

Enumerations

enum  output_t { NORMAL_OUT, SOURCE_OUT, BINARY_OUT }
 

Functions

static void help (void)
 
static void set_zero (Octstr *ostr)
 
int main (int argc, char **argv)
 

Enumeration Type Documentation

◆ output_t

enum output_t
Enumerator
NORMAL_OUT 
SOURCE_OUT 
BINARY_OUT 

Definition at line 70 of file wml_tester.c.

Function Documentation

◆ help()

static void help ( void  )
static

Definition at line 72 of file wml_tester.c.

References info().

Referenced by main().

72  {
73  info(0, "Usage: wml_tester [-hsbzr] [-n number] [-f file] "
74  "[-c charset] file.wml\n"
75  "where\n"
76  " -h this text\n"
77  " -s output also the WML source, cannot be used with b\n"
78  " -b output only the compiled binary, cannot be used with s\n"
79  " -z insert a '\\0'-character in the middle of the input\n"
80  " -r run XML parser in relaxed mode to recover from errors\n"
81  " -n number the number of times the compiling is done\n"
82  " -f file direct the output into a file\n"
83  " -c charset character set as given by the http");
84 }
void info(int err, const char *fmt,...)
Definition: log.c:672

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 94 of file wml_tester.c.

References BINARY_OUT, charset, error(), file, filename, getopt(), GW_NON_EXCL, GW_PANIC, gwlib_init(), gwlib_shutdown(), help(), log_close_all(), log_open(), log_set_output_level(), NORMAL_OUT, octstr_append(), octstr_append_char(), octstr_create, octstr_destroy(), octstr_dump, octstr_format(), octstr_get_cstr, octstr_imm(), octstr_insert(), octstr_len(), octstr_parse_long(), octstr_pretty_print(), octstr_print(), octstr_read_file(), optarg, optind, panic, set_zero(), SOURCE_OUT, wml_compile(), wml_init(), and wml_shutdown().

95 {
96  output_t outputti = NORMAL_OUT;
97  FILE *fp = NULL;
98  Octstr *output = NULL;
99  Octstr *filename = NULL;
100  Octstr *wml_text = NULL;
101  Octstr *charset = NULL;
102  Octstr *wml_binary = NULL;
103  int i, ret = 0, opt, file = 0, zero = 0, numstatus = 0, wml_strict = 1;
104  long num = 0;
105 
106  /* You can give an wml text file as an argument './wml_tester main.wml' */
107 
108  gwlib_init();
109 
110  while ((opt = getopt(argc, argv, "hsbzrn:f:c:")) != EOF) {
111  switch (opt) {
112  case 'h':
113  help();
114  exit(0);
115  case 's':
116  if (outputti == NORMAL_OUT)
117  outputti = SOURCE_OUT;
118  else {
119  help();
120  exit(0);
121  }
122  break;
123  case 'b':
124  if (outputti == NORMAL_OUT)
125  outputti = BINARY_OUT;
126  else {
127  help();
128  exit(0);
129  }
130  break;
131  case 'z':
132  zero = 1;
133  break;
134  case 'r':
135  wml_strict = 0;
136  break;
137  case 'n':
138  numstatus = octstr_parse_long(&num, octstr_imm(optarg), 0, 0);
139  if (numstatus == -1) {
140  /* Error in the octstr_parse_long */
141  error(num, "Error in the handling of argument to option n");
142  help();
143  panic(0, "Stopping.");
144  }
145  break;
146  case 'f':
147  file = 1;
149  fp = fopen(optarg, "a");
150  if (fp == NULL)
151  panic(0, "Couldn't open output file.");
152  break;
153  case 'c':
155  break;
156  case '?':
157  default:
158  error(0, "Invalid option %c", opt);
159  help();
160  panic(0, "Stopping.");
161  }
162  }
163 
164  if (optind >= argc) {
165  error(0, "Missing arguments.");
166  help();
167  panic(0, "Stopping.");
168  }
169 
170  if (outputti == BINARY_OUT)
172  wml_init(wml_strict);
173 
174  while (optind < argc) {
175  wml_text = octstr_read_file(argv[optind]);
176  if (wml_text == NULL)
177  panic(0, "Couldn't read WML source file.");
178 
179  if (zero)
180  set_zero(wml_text);
181 
182  for (i = 0; i <= num; i++) {
183  ret = wml_compile(wml_text, charset, &wml_binary, NULL);
184  if (i < num)
185  octstr_destroy(wml_binary);
186  }
187  optind++;
188 
189  output = octstr_format("wml_compile returned: %d\n\n", ret);
190 
191  if (ret == 0) {
192  if (fp == NULL)
193  fp = stdout;
194 
195  if (outputti != BINARY_OUT) {
196  if (outputti == SOURCE_OUT) {
197  octstr_insert(output, wml_text, octstr_len(output));
198  octstr_append_char(output, '\n');
199  }
200 
201  octstr_append(output, octstr_imm(
202  "Here's the binary output: \n\n"));
203  octstr_print(fp, output);
204  }
205 
206  if (file && outputti != BINARY_OUT) {
207  fclose(fp);
209  octstr_dump(wml_binary, 0);
210  log_close_all();
211  fp = fopen(octstr_get_cstr(filename), "a");
212  } else if (outputti != BINARY_OUT)
213  octstr_dump(wml_binary, 0);
214  else
215  octstr_print(fp, wml_binary);
216 
217  if (outputti != BINARY_OUT) {
218  octstr_destroy(output);
219  output = octstr_format("\n And as a text: \n\n");
220  octstr_print(fp, output);
221 
222  octstr_pretty_print(fp, wml_binary);
223  octstr_destroy(output);
224  output = octstr_format("\n\n");
225  octstr_print(fp, output);
226  }
227  }
228 
229  octstr_destroy(wml_text);
230  octstr_destroy(output);
231  octstr_destroy(wml_binary);
232  }
233 
234  if (file) {
235  fclose(fp);
237  }
238 
239  if (charset != NULL)
241 
242  wml_shutdown();
243  gwlib_shutdown();
244 
245  return ret;
246 }
void error(int err, const char *fmt,...)
Definition: log.c:648
static void help(void)
Definition: wml_tester.c:72
void wml_shutdown()
Definition: wml_compiler.c:479
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1504
void octstr_append_char(Octstr *ostr, int ch)
Definition: octstr.c:1517
int optind
Definition: attgetopt.c:80
int octstr_print(FILE *f, Octstr *ostr)
Definition: octstr.c:1191
static void set_zero(Octstr *ostr)
Definition: wml_tester.c:87
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * charset
Definition: test_ota.c:68
FILE * file
Definition: log.c:169
output_t
Definition: wml_tester.c:70
int getopt(int argc, char **argv, char *opts)
Definition: attgetopt.c:84
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
void log_close_all(void)
Definition: log.c:341
void octstr_insert(Octstr *ostr1, const Octstr *ostr2, long pos)
Definition: octstr.c:1303
void log_set_output_level(enum output_level level)
Definition: log.c:253
#define octstr_dump(ostr, level,...)
Definition: octstr.h:564
int wml_compile(Octstr *wml_text, Octstr *charset, Octstr **wml_binary, Octstr *version)
Definition: wml_compiler.c:360
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
char filename[FILENAME_MAX+1]
Definition: log.c:171
#define octstr_create(cstr)
Definition: octstr.h:125
Octstr * octstr_read_file(const char *filename)
Definition: octstr.c:1548
int log_open(char *filename, int level, enum excl_state excl)
Definition: log.c:375
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118
char * optarg
Definition: attgetopt.c:82
#define panic
Definition: log.h:87
long octstr_parse_long(long *nump, Octstr *ostr, long pos, int base)
Definition: octstr.c:749
void gwlib_shutdown(void)
Definition: gwlib.c:94
void gwlib_init(void)
Definition: gwlib.c:78
int octstr_pretty_print(FILE *f, Octstr *ostr)
Definition: octstr.c:1206
void wml_init(int wml_xml_strict)
Definition: wml_compiler.c:429
Definition: log.h:69

◆ set_zero()

static void set_zero ( Octstr ostr)
static

Definition at line 87 of file wml_tester.c.

References gw_rand(), octstr_len(), and octstr_set_char().

Referenced by main().

88 {
89  octstr_set_char(ostr, (1 + (int) (octstr_len(ostr) *gw_rand()/
90  (RAND_MAX+1.0))), '\0');
91 }
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
void octstr_set_char(Octstr *ostr, long pos, int ch)
Definition: octstr.c:415
int gw_rand(void)
Definition: protected.c:174
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.