Kannel: Open Source WAP and SMS gateway  svn-r5335
wml_tester.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * wml_tester.c - a simple program to test the WML-compiler module
59  *
60  * Tuomas Luttinen <tuo@wapit.com>
61  */
62 
63 #include <stdlib.h>
64 #include <unistd.h>
65 
66 #include "gwlib/gwlib.h"
67 #include "gw/wml_compiler.h"
68 
69 
71 
72 static void help(void) {
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 }
85 
86 
87 static void set_zero(Octstr *ostr)
88 {
89  octstr_set_char(ostr, (1 + (int) (octstr_len(ostr) *gw_rand()/
90  (RAND_MAX+1.0))), '\0');
91 }
92 
93 
94 int main(int argc, char **argv)
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
void info(int err, const char *fmt,...)
Definition: log.c:672
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
int main(int argc, char **argv)
Definition: wml_tester.c:94
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 octstr_set_char(Octstr *ostr, long pos, int ch)
Definition: octstr.c:415
int gw_rand(void)
Definition: protected.c:174
void wml_init(int wml_xml_strict)
Definition: wml_compiler.c:429
Definition: log.h:69
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.