00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #include <stdlib.h>
00064 #include <unistd.h>
00065
00066 #include "gwlib/gwlib.h"
00067 #include "gw/wml_compiler.h"
00068
00069
00070 typedef enum { NORMAL_OUT, SOURCE_OUT, BINARY_OUT } output_t;
00071
00072 static void help(void) {
00073 info(0, "Usage: wml_tester [-hsbzr] [-n number] [-f file] "
00074 "[-c charset] file.wml\n"
00075 "where\n"
00076 " -h this text\n"
00077 " -s output also the WML source, cannot be used with b\n"
00078 " -b output only the compiled binary, cannot be used with s\n"
00079 " -z insert a '\\0'-character in the middle of the input\n"
00080 " -r run XML parser in relaxed mode to recover from errors\n"
00081 " -n number the number of times the compiling is done\n"
00082 " -f file direct the output into a file\n"
00083 " -c charset character set as given by the http");
00084 }
00085
00086
00087 static void set_zero(Octstr *ostr)
00088 {
00089 octstr_set_char(ostr, (1 + (int) (octstr_len(ostr) *gw_rand()/
00090 (RAND_MAX+1.0))), '\0');
00091 }
00092
00093
00094 int main(int argc, char **argv)
00095 {
00096 output_t outputti = NORMAL_OUT;
00097 FILE *fp = NULL;
00098 Octstr *output = NULL;
00099 Octstr *filename = NULL;
00100 Octstr *wml_text = NULL;
00101 Octstr *charset = NULL;
00102 Octstr *wml_binary = NULL;
00103 int i, ret = 0, opt, file = 0, zero = 0, numstatus = 0, wml_strict = 1;
00104 long num = 0;
00105
00106
00107
00108 gwlib_init();
00109
00110 while ((opt = getopt(argc, argv, "hsbzrn:f:c:")) != EOF) {
00111 switch (opt) {
00112 case 'h':
00113 help();
00114 exit(0);
00115 case 's':
00116 if (outputti == NORMAL_OUT)
00117 outputti = SOURCE_OUT;
00118 else {
00119 help();
00120 exit(0);
00121 }
00122 break;
00123 case 'b':
00124 if (outputti == NORMAL_OUT)
00125 outputti = BINARY_OUT;
00126 else {
00127 help();
00128 exit(0);
00129 }
00130 break;
00131 case 'z':
00132 zero = 1;
00133 break;
00134 case 'r':
00135 wml_strict = 0;
00136 break;
00137 case 'n':
00138 numstatus = octstr_parse_long(&num, octstr_imm(optarg), 0, 0);
00139 if (numstatus == -1) {
00140
00141 error(num, "Error in the handling of argument to option n");
00142 help();
00143 panic(0, "Stopping.");
00144 }
00145 break;
00146 case 'f':
00147 file = 1;
00148 filename = octstr_create(optarg);
00149 fp = fopen(optarg, "a");
00150 if (fp == NULL)
00151 panic(0, "Couldn't open output file.");
00152 break;
00153 case 'c':
00154 charset = octstr_create(optarg);
00155 break;
00156 case '?':
00157 default:
00158 error(0, "Invalid option %c", opt);
00159 help();
00160 panic(0, "Stopping.");
00161 }
00162 }
00163
00164 if (optind >= argc) {
00165 error(0, "Missing arguments.");
00166 help();
00167 panic(0, "Stopping.");
00168 }
00169
00170 if (outputti == BINARY_OUT)
00171 log_set_output_level(GW_PANIC);
00172 wml_init(wml_strict);
00173
00174 while (optind < argc) {
00175 wml_text = octstr_read_file(argv[optind]);
00176 if (wml_text == NULL)
00177 panic(0, "Couldn't read WML source file.");
00178
00179 if (zero)
00180 set_zero(wml_text);
00181
00182 for (i = 0; i <= num; i++) {
00183 ret = wml_compile(wml_text, charset, &wml_binary, NULL);
00184 if (i < num)
00185 octstr_destroy(wml_binary);
00186 }
00187 optind++;
00188
00189 output = octstr_format("wml_compile returned: %d\n\n", ret);
00190
00191 if (ret == 0) {
00192 if (fp == NULL)
00193 fp = stdout;
00194
00195 if (outputti != BINARY_OUT) {
00196 if (outputti == SOURCE_OUT) {
00197 octstr_insert(output, wml_text, octstr_len(output));
00198 octstr_append_char(output, '\n');
00199 }
00200
00201 octstr_append(output, octstr_imm(
00202 "Here's the binary output: \n\n"));
00203 octstr_print(fp, output);
00204 }
00205
00206 if (file && outputti != BINARY_OUT) {
00207 fclose(fp);
00208 log_open(octstr_get_cstr(filename), 0, GW_NON_EXCL);
00209 octstr_dump(wml_binary, 0);
00210 log_close_all();
00211 fp = fopen(octstr_get_cstr(filename), "a");
00212 } else if (outputti != BINARY_OUT)
00213 octstr_dump(wml_binary, 0);
00214 else
00215 octstr_print(fp, wml_binary);
00216
00217 if (outputti != BINARY_OUT) {
00218 octstr_destroy(output);
00219 output = octstr_format("\n And as a text: \n\n");
00220 octstr_print(fp, output);
00221
00222 octstr_pretty_print(fp, wml_binary);
00223 octstr_destroy(output);
00224 output = octstr_format("\n\n");
00225 octstr_print(fp, output);
00226 }
00227 }
00228
00229 octstr_destroy(wml_text);
00230 octstr_destroy(output);
00231 octstr_destroy(wml_binary);
00232 }
00233
00234 if (file) {
00235 fclose(fp);
00236 octstr_destroy(filename);
00237 }
00238
00239 if (charset != NULL)
00240 octstr_destroy(charset);
00241
00242 wml_shutdown();
00243 gwlib_shutdown();
00244
00245 return ret;
00246 }
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.