Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
test_sl.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 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  * test_sl.c: A simple program to test sl tokenizer
59  *
60  * By Aarno Syvšnen for Wiral Ltd
61  */
62 
63 #include <stdio.h>
64 
65 #include "gwlib/gwlib.h"
67 
68 Octstr *charset = NULL;
69 Octstr *file_name = NULL;
70 
71 static void help (void)
72 {
73  info(0, "Usage test_sl [option] sl_source");
74  info(0, "where options are");
75  info(0, "-h print this text");
76  info(0, "-f file output binary to the file");
77  info(0, "-c charset charset given by http");
78  info(0, "-v level set log level for stderr logging");
79 }
80 
81 int main(int argc, char **argv)
82 {
83  int opt,
84  file,
85  have_charset,
86  ret;
87  FILE *fp;
88  Octstr *output,
89  *sl_doc,
90  *sl_binary;
91 
92  gwlib_init();
93  file = 0;
94  have_charset = 0;
95  fp = NULL;
96 
97  while ((opt = getopt(argc, argv, "hf:c:v:")) != EOF) {
98  switch (opt) {
99  case 'h':
100  help();
101  exit(1);
102  break;
103 
104  case 'f':
105  file = 1;
106  file_name = octstr_create(optarg);
107  fp = fopen(optarg, "a");
108  if (fp == NULL)
109  panic(0, "Cannot open output file");
110  break;
111 
112  case 'c':
113  have_charset = 1;
114  charset = octstr_create(optarg);
115  break;
116 
117  case 'v':
119  break;
120 
121  case '?':
122  default:
123  error(0, "Invalid option %c", opt);
124  help();
125  panic(0, "Stopping");
126  break;
127  }
128  }
129 
130  if (optind >= argc) {
131  error(0, "Missing arguments");
132  help();
133  panic(0, "Stopping");
134  }
135 
136  sl_doc = octstr_read_file(argv[optind]);
137  if (sl_doc == NULL)
138  panic(0, "Cannot read the sl document");
139 
140  if (!have_charset)
141  charset = NULL;
142  ret = sl_compile(sl_doc, charset, &sl_binary);
143  output = octstr_format("%s", "sl compiler returned %d\n", ret);
144 
145  if (ret == 0) {
146  if (fp == NULL)
147  fp = stdout;
148  octstr_append(output, octstr_imm("content being\n"));
149  octstr_append(output, sl_binary);
150  }
151 
152  if (file)
153  octstr_pretty_print(fp, output);
154  else {
155  debug("test.sl", 0, "sl binary was");
156  octstr_dump(sl_binary, 0);
157  }
158 
159  if (have_charset)
160  octstr_destroy(charset);
161  if (file) {
162  fclose(fp);
163  octstr_destroy(file_name);
164  }
165 
166  octstr_destroy(sl_doc);
167  octstr_destroy(sl_binary);
168  octstr_destroy(output);
169  gwlib_shutdown();
170  return 0;
171 }
172 
173 
174 
175 
176 
void error(int err, const char *fmt,...)
Definition: log.c:612
void info(int err, const char *fmt,...)
Definition: log.c:636
int sl_compile(Octstr *sl_doc, Octstr *charset, Octstr **sl_binary)
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1502
int optind
Definition: attgetopt.c:80
FILE * file
Definition: log.c:133
int getopt(int argc, char **argv, char *opts)
Definition: attgetopt.c:84
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
Octstr * charset
Definition: test_sl.c:68
int main(int argc, char **argv)
Definition: test_sl.c:81
void log_set_output_level(enum output_level level)
Definition: log.c:217
#define octstr_dump(ostr, level,...)
Definition: octstr.h:564
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
Octstr * octstr_read_file(const char *filename)
Definition: octstr.c:1546
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
char * optarg
Definition: attgetopt.c:82
#define panic
Definition: log.h:87
void gwlib_shutdown(void)
Definition: gwlib.c:94
Octstr * file_name
Definition: test_sl.c:69
void gwlib_init(void)
Definition: gwlib.c:78
int octstr_pretty_print(FILE *f, Octstr *ostr)
Definition: octstr.c:1204
static void help(void)
Definition: test_sl.c:71
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.