1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2014 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  *
59  * ws.h
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  *		 All rights reserved.
65  *
66  * Public header file for the WMLScript compiler library.
67  *
68  * The compiler is written for WMLScript version 1.1.
69  *
70  */
71 
72 #ifndef WS_H
73 #define WS_H
74 
75 #include "wsutf8.h"
76 
77 /********************* Creating and destroying compiler *****************/
78 
79 /* A callback function of this type is called to output compiler's
80    diagnostic, error, and warning messages.  The argument `data' has
81    `len' bytes of data that should be output somehow to user.  The
82    argument `context' is the user-specified context data for the
83    callback function. */
84 typedef void (*WsIOProc)(const char *data, size_t len, void *context);
85 
86 /* A callback function of this type is called for each `use meta name'
87    and `use meta http equiv' pragma, found from the current
88    compilation unit.  The arguments `property_name', `content', and
89    `scheme' are the meta-body arguments of the pragma.  They can be
90    manipulated with the functions of the `wsutf8.h' module.  The
91    argument `scheme' can have the value NULL if the pragma did not
92    specify it.  The string arguments belong to the WMLScript compiler
93    and you should not modify or free them.  The argument `context' is
94    the user-specified context data for the callback function. */
95 typedef void (*WsPragmaMetaProc)(const WsUtf8String *property_name,
96                                  const WsUtf8String *content,
97                                  const WsUtf8String *scheme,
98                                  void *context);
99 
100 /* Parameters for a WMLScript copiler. */
101 struct WsCompilerParamsRec
102 {
103     /* Features. */
104 
105     /* Store string constants in ISO-8859/1 (ISO latin1) format.  The
106        default format is UTF-8.  This option makes a bit smaller
107        byte-code files but it loses information for non-latin1
108        languages. */
109     unsigned int use_latin1_strings : 1;
110 
111 
112     /* Warning flags. */
113 
114     /* Warn if a standard library function is called with mismatching
115        argument types. */
116     unsigned int warn_stdlib_type_mismatch : 1;
117 
118 
119     /* Optimization flags. */
120 
121     /* Do not perform constant folding. */
122     unsigned int no_opt_constant_folding : 1;
123 
124     /* Do not sort byte-code functions by their usage counts. */
125     unsigned int no_opt_sort_bc_functions : 1;
126 
127     /* Do not perform peephole optimization. */
128     unsigned int no_opt_peephole : 1;
129 
130     /* Do not optimize jumps to jump instructions to jump directly to
131        the target label of the next instruction. */
132     unsigned int no_opt_jumps_to_jumps : 1;
133 
134     /* Do not optimize jumps to the next instruction. */
135     unsigned int no_opt_jumps_to_next_instruction : 1;
136 
137     /* Do not remove unreachable code. */
138     unsigned int no_opt_dead_code : 1;
139 
140     /* Do not remove useless conversions */
141     unsigned int no_opt_conv : 1;
142 
143     /* Perform expensive optimizations which require liveness
144        analyzation of the local variables. */
145     unsigned int opt_analyze_variable_liveness : 1;
146 
147 
148     /* Output flags. */
149 
150     /* Print verbose progress messages. */
151     unsigned int verbose : 1;
152 
153     /* Print symbolic assembler to the stdout. */
154     unsigned int print_symbolic_assembler : 1;
155 
156     /* Disassemble the resulting byte-code instructions. */
157     unsigned int print_assembler : 1;
158 
159     /* Function pointers to receive standard output and error messages.
160        If these are unset, the outputs are directed to the system's
161        standard output and error streams. */
162 
163     /* Standard output. */
164     WsIOProc stdout_cb;
165     void *stdout_cb_context;
166 
167     /* Standard error. */
168     WsIOProc stderr_cb;
169     void *stderr_cb_context;
170 
171     /* A callback function which is called for each `use meta name'
172        pragma, found from the current compilation unit. */
173     WsPragmaMetaProc meta_name_cb;
174     void *meta_name_cb_context;
175 
176     /* A callback function which is called for each `use meta http
177        equiv' pragma, found from the current compilation unit. */
178     WsPragmaMetaProc meta_http_equiv_cb;
179     void *meta_http_equiv_cb_context;
180 };
181 
182 typedef struct WsCompilerParamsRec WsCompilerParams;
183 
184 /* A compiler handle. */
185 typedef struct WsCompilerRec *WsCompilerPtr;
186 
187 /* Create a new WMLScript compiler.  The argument `params' specifies
188    initialization parameters for the compiler.  If the argument
189    `params' is NULL or any of its fiels have value 0 or NULL, the
190    default values will be used for those parameters.  The function
191    takes a copy of the value of the `params' argument.  You can free
192    it after this call.  The function returns NULL if the operation
193    fails (out of memory). */
194 WsCompilerPtr ws_create(WsCompilerParams *params);
195 
196 /* Destroy the WMLScript compiler `compiler' and free all resources it
197    has allocated. */
198 void ws_destroy(WsCompilerPtr compiler);
199 
200 /********************* Compiling WMLScript ******************************/
201 
202 /* Returns codes for the compiler functions. */
203 typedef enum
204 {
205     /* Successful termination */
206     WS_OK,
207 
208     /* The compiler ran out of memory. */
209     WS_ERROR_OUT_OF_MEMORY,
210 
211     /* The input was not syntactically correct. */
212     WS_ERROR_SYNTAX,
213 
214     /* The input was not semantically correct. */
215     WS_ERROR_SEMANTIC,
216 
217     /* IO error. */
218     WS_ERROR_IO,
219 
220     /* A generic `catch-all' error code.  This should not be used.  More
221        descriptive error messages should be generated instead. */
222     WS_ERROR
223 } WsResult;
224 
225 /* Compile the WMLScript input file `input' with the compiler
226    `compiler' and save the generated byte-code output to the file
227    `output'.  The argument `input_name' is the name of the input file
228    `input'.  It is used in error messages. The function returns a
229    success code that describes the result of the compilation.  The
230    output file `output' is modified only if the result code is
231    `WS_OK'. */
232 WsResult ws_compile_file(WsCompilerPtr compiler, const char *input_name,
233                          FILE *input, FILE *output);
234 
235 /* Compile the `input_len' bytes of WMLScript data in `input' with the
236    compiler `compiler'.  The data is assumed to be in the ISO-8859/1
237    (ISO latin1) format.  The resulting byte-code is returned in
238    `output_return' and its length is returned in `output_len_return'.
239    The argument `input_name' is the name of the input data
240    `input_data'.  It is used in error messages.  The function returns
241    a success code that describes the result of the compilation.  The
242    output in `output_return' is valid only if the result code is
243    `WS_OK'.  The byte-code, returned in `output_return', must be freed
244    with the ws_free_byte_code() function after it is not needed
245    anymore.  It is a fatal error to free it with any other function,
246    like free(). */
247 WsResult ws_compile_data(WsCompilerPtr compiler, const char *input_name,
248                          const unsigned char *input, size_t input_len,
249                          unsigned char **output_return,
250                          size_t *output_len_return);
251 
252 /* Free the byte-code buffer `byte_code', returned by
253    ws_compiler_data() function.  The byte-code `byte_code' must not be
254    used after this function has been called. */
255 void ws_free_byte_code(unsigned char *byte_code);
256 
257 /* Convert the result code `result' into human readable 7 bit ASCII
258    string. */
259 const char *ws_result_to_string(WsResult result);
260 
261 #endif /* not WS_H */
262