Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

wml_tester.c

Go to the documentation of this file.
00001 /* ==================================================================== 
00002  * The Kannel Software License, Version 1.0 
00003  * 
00004  * Copyright (c) 2001-2008 Kannel Group  
00005  * Copyright (c) 1998-2001 WapIT Ltd.   
00006  * All rights reserved. 
00007  * 
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions 
00010  * are met: 
00011  * 
00012  * 1. Redistributions of source code must retain the above copyright 
00013  *    notice, this list of conditions and the following disclaimer. 
00014  * 
00015  * 2. Redistributions in binary form must reproduce the above copyright 
00016  *    notice, this list of conditions and the following disclaimer in 
00017  *    the documentation and/or other materials provided with the 
00018  *    distribution. 
00019  * 
00020  * 3. The end-user documentation included with the redistribution, 
00021  *    if any, must include the following acknowledgment: 
00022  *       "This product includes software developed by the 
00023  *        Kannel Group (http://www.kannel.org/)." 
00024  *    Alternately, this acknowledgment may appear in the software itself, 
00025  *    if and wherever such third-party acknowledgments normally appear. 
00026  * 
00027  * 4. The names "Kannel" and "Kannel Group" must not be used to 
00028  *    endorse or promote products derived from this software without 
00029  *    prior written permission. For written permission, please  
00030  *    contact org@kannel.org. 
00031  * 
00032  * 5. Products derived from this software may not be called "Kannel", 
00033  *    nor may "Kannel" appear in their name, without prior written 
00034  *    permission of the Kannel Group. 
00035  * 
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00039  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
00040  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
00041  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
00042  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
00043  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
00044  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
00045  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
00046  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00047  * ==================================================================== 
00048  * 
00049  * This software consists of voluntary contributions made by many 
00050  * individuals on behalf of the Kannel Group.  For more information on  
00051  * the Kannel Group, please see <http://www.kannel.org/>. 
00052  * 
00053  * Portions of this software are based upon software originally written at  
00054  * WapIT Ltd., Helsinki, Finland for the Kannel project.  
00055  */ 
00056 
00057 /*
00058  * wml_tester.c - a simple program to test the WML-compiler module
00059  *
00060  * Tuomas Luttinen <tuo@wapit.com>
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     /* You can give an wml text file as an argument './wml_tester main.wml' */
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         /* Error in the octstr_parse_long */
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.