Kannel: Open Source WAP and SMS gateway  svn-r5335
wmlsc.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  *
59  * wmlsc.c
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  * All rights reserved.
65  *
66  * Main for the WMLScript compiler.
67  *
68  */
69 
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <errno.h>
74 
75 #if HAVE_UNISTD_H
76 #include <unistd.h>
77 #endif /* HAVE_UNISTD_H */
78 
79 #include <sys/types.h>
80 #include <sys/stat.h>
81 
82 #include "gwlib/gwlib.h"
83 #include "ws.h"
84 
85 
86 /* XXX This module, as well, should use the gwmem wrappers. We'll change
87  this later. --liw */
88 #undef malloc
89 #undef realloc
90 #undef free
91 
92 
93 /********************* Prototypes for static functions ******************/
94 
95 /* Print usage message to the stdout. */
96 static void usage(void);
97 
98 /* A callback function to receive the meta-pragmas. */
99 static void pragma_meta(const WsUtf8String *property_name,
100  const WsUtf8String *content,
101  const WsUtf8String *scheme,
102  void *context);
103 
104 /********************* Static variables *********************************/
105 
106 /* The name of the compiler program. */
107 static char *program;
108 
109 /* Use ws_compile_data() instead of ws_compile_file(). */
110 static int eval_data = 0;
111 
112 /********************* Global functions *********************************/
113 
114 int main(int argc, char *argv[])
115 {
116  int i;
117  WsCompilerParams params;
118  WsCompilerPtr compiler;
119  WsResult result;
120  int opt;
121 
122  program = strrchr(argv[0], '/');
123  if (program)
124  program++;
125  else
126  program = argv[0];
127 
128  /* Initialize the parameters structure. The command line options
129  modify this directly. */
130  memset(&params, 0, sizeof(params));
131 
132  /* Process command line arguments. */
133  while ((opt = getopt(argc, argv, "adhsv")) != EOF) {
134  switch (opt) {
135  case 'a':
136  params.print_assembler = 1;
137  break;
138 
139  case 'd':
140  eval_data = 1;
141  break;
142 
143  case 'h':
144  usage();
145  exit(0);
146  break;
147 
148  case 'l':
149  params.use_latin1_strings = 1;
150  break;
151 
152  case 'p':
153  params.meta_name_cb = pragma_meta;
154  params.meta_name_cb_context = "meta name";
155 
157  params.meta_http_equiv_cb_context = "meta http equiv";
158  break;
159 
160  case 's':
161  params.print_symbolic_assembler = 1;
162  break;
163 
164  case 'v':
165  params.verbose = 1;
166  break;
167 
168  case '?':
169  printf("Try `%s -h' for a complete list of options.\n",
170  program);
171  exit(1);
172  }
173  }
174 
175  /* Create compiler. */
176 
177  compiler = ws_create(&params);
178  if (compiler == NULL) {
179  fprintf(stderr, "wsc: could not create compiler\n");
180  exit(1);
181  }
182 
183  for (i = optind; i < argc; i++) {
184  FILE *ifp, *ofp;
185  char *outname;
186 
187  ifp = fopen(argv[i], "rb");
188  if (ifp == NULL) {
189  fprintf(stderr, "wsc: could not open input file `%s': %s'\n",
190  argv[i], strerror(errno));
191  exit(1);
192  }
193 
194  /* Create the output name. */
195  outname = malloc(strlen(argv[i]) + 1 + 1);
196  if (outname == NULL) {
197  fprintf(stderr, "wmlsc: could not create output file name: %s\n",
198  strerror(errno));
199  exit(1);
200  }
201  strcpy(outname, argv[i]);
202  strcat(outname, "c");
203 
204  ofp = fopen(outname, "wb");
205  if (ofp == NULL) {
206  fprintf(stderr, "wsc: could not create output file `%s': %s\n",
207  outname, strerror(errno));
208  exit(1);
209  }
210 
211  if (eval_data) {
212  /* Use the ws_compile_data() interface. */
213  struct stat stat_st;
214  unsigned char *data;
215  unsigned char *output;
216  size_t output_len;
217 
218  if (stat(argv[i], &stat_st) == -1) {
219  fprintf(stderr, "wsc: could not stat input file `%s': %s\n",
220  argv[i], strerror(errno));
221  exit(1);
222  }
223 
224  /* Allocate the input buffer. */
225  data = malloc(stat_st.st_size);
226  if (data == NULL) {
227  fprintf(stderr, "wsc: could not allocate input buffer: %s\n",
228  strerror(errno));
229  exit(1);
230  }
231  if (fread(data, 1, stat_st.st_size, ifp) < (size_t) stat_st.st_size) {
232  fprintf(stderr, "wsc: could not read data: %s\n",
233  strerror(errno));
234  exit(1);
235  }
236  result = ws_compile_data(compiler, argv[i], data, stat_st.st_size,
237  &output, &output_len);
238  if (result == WS_OK) {
239  /* Save the output to `ofp'. */
240  if (fwrite(output, 1, output_len, ofp) != output_len) {
241  fprintf(stderr,
242  "wsc: could not save output to file `%s': %s\n",
243  outname, strerror(errno));
244  exit(1);
245  }
246  }
247  free(data);
248  ws_free_byte_code(output);
249  } else {
250  /* Use the ws_compile_file() interface. */
251  result = ws_compile_file(compiler, argv[i], ifp, ofp);
252  }
253 
254  /* Common cleanup. */
255  fclose(ifp);
256  fclose(ofp);
257 
258  if (result != WS_OK) {
259  remove(outname);
260  fprintf(stderr, "wsc: compilation failed: %s\n",
261  ws_result_to_string(result));
262  exit(1);
263  }
264  free(outname);
265  }
266 
267  ws_destroy(compiler);
268 
269  return 0;
270 }
271 
272 /********************* Static functions *********************************/
273 
274 static void usage(void)
275 {
276  printf("Usage: %s OPTION... FILE...\n\
277  \n\
278  -a disassemble resulting byte-code and print it to the\n\
279  standard output\n\
280  -d use ws_eval_data() function instead of ws_eval_file()\n\
281  -h print this help message and exit successfully\n\
282  -l encode strings in ISO-8859/1 (ISO latin1) instead of using\n\
283  UTF-8\n\
284  -p print pragmas\n\
285  -s print symbolic byte-code assembler to the standard output\n\
286  -v print verbose progress messages\n\
287  \n",
288  program);
289 }
290 
291 
292 static void pragma_meta(const WsUtf8String *property_name,
293  const WsUtf8String *content,
294  const WsUtf8String *scheme, void *context)
295 {
296  FILE *fp = stdout;
297  char *what = (char *) context;
298  char *property_name_l = (char *) ws_utf8_to_latin1(property_name, '?', NULL);
299  char *content_l = (char *) ws_utf8_to_latin1(content, '?', NULL);
300  char *scheme_l = (char *) ws_utf8_to_latin1(scheme, '?', NULL);
301 
302  fprintf(fp, "%s: name=\"%s\", content=\"%s\",",
303  what,
304  property_name_l ? property_name_l : "",
305  content_l ? content_l : "");
306 
307  if (scheme)
308  fprintf(fp, ", scheme=\"%s\"",
309  scheme_l ? scheme_l : "");
310 
311  fprintf(fp, "\n");
312 
313  ws_utf8_free_data((unsigned char *) property_name_l);
314  ws_utf8_free_data((unsigned char *) content_l);
315  ws_utf8_free_data((unsigned char *) scheme_l);
316 }
static void pragma_meta(const WsUtf8String *property_name, const WsUtf8String *content, const WsUtf8String *scheme, void *context)
Definition: wmlsc.c:292
static void usage(void)
Definition: wmlsc.c:274
unsigned int print_symbolic_assembler
Definition: ws.h:154
void ws_free_byte_code(unsigned char *byte_code)
Definition: ws.c:228
Definition: parse.c:65
WsResult ws_compile_file(WsCompilerPtr compiler, const char *input_name, FILE *input, FILE *output)
Definition: ws.c:177
int optind
Definition: attgetopt.c:80
WsCompilerPtr ws_create(WsCompilerParams *params)
Definition: ws.c:135
unsigned int use_latin1_strings
Definition: ws.h:109
void * malloc(YYSIZE_T)
static char * program
Definition: wmlsc.c:107
WsResult
Definition: ws.h:203
int getopt(int argc, char **argv, char *opts)
Definition: attgetopt.c:84
static int eval_data
Definition: wmlsc.c:110
void ws_destroy(WsCompilerPtr compiler)
Definition: ws.c:163
WsResult ws_compile_data(WsCompilerPtr compiler, const char *input_name, const unsigned char *input, size_t input_len, unsigned char **output_return, size_t *output_len_return)
Definition: ws.c:206
void free(void *)
Definition: ws.h:206
void ws_utf8_free_data(unsigned char *data)
Definition: wsutf8.c:368
unsigned int print_assembler
Definition: ws.h:157
void * meta_name_cb_context
Definition: ws.h:174
WsPragmaMetaProc meta_name_cb
Definition: ws.h:173
int main(int argc, char *argv[])
Definition: wmlsc.c:114
WsPragmaMetaProc meta_http_equiv_cb
Definition: ws.h:178
const char * ws_result_to_string(WsResult result)
Definition: ws.c:234
void * meta_http_equiv_cb_context
Definition: ws.h:179
unsigned int verbose
Definition: ws.h:151
unsigned char * ws_utf8_to_latin1(const WsUtf8String *string, unsigned char unknown_char, size_t *len_return)
Definition: wsutf8.c:332
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.