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

wmlsc.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  *
00059  * wmlsc.c
00060  *
00061  * Author: Markku Rossi <mtr@iki.fi>
00062  *
00063  * Copyright (c) 1999-2000 WAPIT OY LTD.
00064  *       All rights reserved.
00065  *
00066  * Main for the WMLScript compiler.
00067  *
00068  */
00069 
00070 #include <stdio.h>
00071 #include <stdlib.h>
00072 #include <string.h>
00073 #include <errno.h>
00074 
00075 #if HAVE_UNISTD_H
00076 #include <unistd.h>
00077 #endif /* HAVE_UNISTD_H */
00078 
00079 #include <sys/types.h>
00080 #include <sys/stat.h>
00081 
00082 #include "gwlib/gwlib.h"
00083 #include "ws.h"
00084 
00085 
00086 /* XXX This module, as well, should use the gwmem wrappers. We'll change
00087    this later. --liw */
00088 #undef malloc
00089 #undef realloc
00090 #undef free
00091 
00092 
00093 /********************* Prototypes for static functions ******************/
00094 
00095 /* Print usage message to the stdout. */
00096 static void usage(void);
00097 
00098 /* A callback function to receive the meta-pragmas. */
00099 static void pragma_meta(const WsUtf8String *property_name,
00100                         const WsUtf8String *content,
00101                         const WsUtf8String *scheme,
00102                         void *context);
00103 
00104 /********************* Static variables *********************************/
00105 
00106 /* The name of the compiler program. */
00107 static char *program;
00108 
00109 /* Use ws_compile_data() instead of ws_compile_file(). */
00110 static int eval_data = 0;
00111 
00112 /********************* Global functions *********************************/
00113 
00114 int main(int argc, char *argv[])
00115 {
00116     int i;
00117     WsCompilerParams params;
00118     WsCompilerPtr compiler;
00119     WsResult result;
00120     int opt;
00121 
00122     program = strrchr(argv[0], '/');
00123     if (program)
00124         program++;
00125     else
00126         program = argv[0];
00127 
00128     /* Initialize the parameters structure.  The command line options
00129        modify this directly. */
00130     memset(&params, 0, sizeof(params));
00131 
00132     /* Process command line arguments. */
00133     while ((opt = getopt(argc, argv, "adhsv")) != EOF) {
00134         switch (opt) {
00135         case 'a':
00136             params.print_assembler = 1;
00137             break;
00138 
00139         case 'd':
00140             eval_data = 1;
00141             break;
00142 
00143         case 'h':
00144             usage();
00145             exit(0);
00146             break;
00147 
00148         case 'l':
00149             params.use_latin1_strings = 1;
00150             break;
00151 
00152         case 'p':
00153             params.meta_name_cb = pragma_meta;
00154             params.meta_name_cb_context = "meta name";
00155 
00156             params.meta_http_equiv_cb = pragma_meta;
00157             params.meta_http_equiv_cb_context = "meta http equiv";
00158             break;
00159 
00160         case 's':
00161             params.print_symbolic_assembler = 1;
00162             break;
00163 
00164         case 'v':
00165             params.verbose = 1;
00166             break;
00167 
00168         case '?':
00169             printf("Try `%s -h' for a complete list of options.\n",
00170                    program);
00171             exit(1);
00172         }
00173     }
00174 
00175     /* Create compiler. */
00176 
00177     compiler = ws_create(&params);
00178     if (compiler == NULL) {
00179         fprintf(stderr, "wsc: could not create compiler\n");
00180         exit(1);
00181     }
00182 
00183     for (i = optind; i < argc; i++) {
00184         FILE *ifp, *ofp;
00185         char *outname;
00186 
00187         ifp = fopen(argv[i], "rb");
00188         if (ifp == NULL) {
00189             fprintf(stderr, "wsc: could not open input file `%s': %s'\n",
00190                     argv[i], strerror(errno));
00191             exit(1);
00192         }
00193 
00194         /* Create the output name. */
00195         outname = malloc(strlen(argv[i]) + 1 + 1);
00196         if (outname == NULL) {
00197             fprintf(stderr, "wmlsc: could not create output file name: %s\n",
00198                     strerror(errno));
00199             exit(1);
00200         }
00201         strcpy(outname, argv[i]);
00202         strcat(outname, "c");
00203 
00204         ofp = fopen(outname, "wb");
00205         if (ofp == NULL) {
00206             fprintf(stderr, "wsc: could not create output file `%s': %s\n",
00207                     outname, strerror(errno));
00208             exit(1);
00209         }
00210 
00211         if (eval_data) {
00212             /* Use the ws_compile_data() interface. */
00213             struct stat stat_st;
00214             unsigned char *data;
00215             unsigned char *output;
00216             size_t output_len;
00217 
00218             if (stat(argv[i], &stat_st) == -1) {
00219                 fprintf(stderr, "wsc: could not stat input file `%s': %s\n",
00220                         argv[i], strerror(errno));
00221                 exit(1);
00222             }
00223 
00224             /* Allocate the input buffer. */
00225             data = malloc(stat_st.st_size);
00226             if (data == NULL) {
00227                 fprintf(stderr, "wsc: could not allocate input buffer: %s\n",
00228                         strerror(errno));
00229                 exit(1);
00230             }
00231             if (fread(data, 1, stat_st.st_size, ifp) < (size_t) stat_st.st_size) {
00232                 fprintf(stderr, "wsc: could not read data: %s\n",
00233                         strerror(errno));
00234                 exit(1);
00235             }
00236             result = ws_compile_data(compiler, argv[i], data, stat_st.st_size,
00237                                      &output, &output_len);
00238             if (result == WS_OK) {
00239                 /* Save the output to `ofp'. */
00240                 if (fwrite(output, 1, output_len, ofp) != output_len) {
00241                     fprintf(stderr,
00242                             "wsc: could not save output to file `%s': %s\n",
00243                             outname, strerror(errno));
00244                     exit(1);
00245                 }
00246             }
00247             free(data);
00248             ws_free_byte_code(output);
00249         } else {
00250             /* Use the ws_compile_file() interface. */
00251             result = ws_compile_file(compiler, argv[i], ifp, ofp);
00252         }
00253 
00254         /* Common cleanup. */
00255         fclose(ifp);
00256         fclose(ofp);
00257 
00258         if (result != WS_OK) {
00259             remove(outname);
00260             fprintf(stderr, "wsc: compilation failed: %s\n",
00261                     ws_result_to_string(result));
00262             exit(1);
00263         }
00264         free(outname);
00265     }
00266 
00267     ws_destroy(compiler);
00268 
00269     return 0;
00270 }
00271 
00272 /********************* Static functions *********************************/
00273 
00274 static void usage(void)
00275 {
00276     printf("Usage: %s OPTION... FILE...\n\
00277            \n\
00278            -a            disassemble resulting byte-code and print it to the\n\
00279            standard output\n\
00280            -d        use ws_eval_data() function instead of ws_eval_file()\n\
00281            -h            print this help message and exit successfully\n\
00282            -l            encode strings in ISO-8859/1 (ISO latin1) instead of using\n\
00283            UTF-8\n\
00284            -p            print pragmas\n\
00285            -s            print symbolic byte-code assembler to the standard output\n\
00286            -v            print verbose progress messages\n\
00287            \n",
00288            program);
00289 }
00290 
00291 
00292 static void pragma_meta(const WsUtf8String *property_name,
00293                         const WsUtf8String *content,
00294                         const WsUtf8String *scheme, void *context)
00295 {
00296     FILE *fp = stdout;
00297     char *what = (char *) context;
00298     char *property_name_l = (char *) ws_utf8_to_latin1(property_name, '?', NULL);
00299     char *content_l = (char *) ws_utf8_to_latin1(content, '?', NULL);
00300     char *scheme_l = (char *) ws_utf8_to_latin1(scheme, '?', NULL);
00301 
00302     fprintf(fp, "%s: name=\"%s\", content=\"%s\",",
00303             what,
00304             property_name_l ? property_name_l : "",
00305             content_l ? content_l : "");
00306 
00307     if (scheme)
00308         fprintf(fp, ", scheme=\"%s\"",
00309                 scheme_l ? scheme_l : "");
00310 
00311     fprintf(fp, "\n");
00312 
00313     ws_utf8_free_data((unsigned char *) property_name_l);
00314     ws_utf8_free_data((unsigned char *) content_l);
00315     ws_utf8_free_data((unsigned char *) scheme_l);
00316 }
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.