Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
test_ota.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_ota.c: A simple program to test ota tokenizer
59  *
60  * By Aarno Syvšnen for Wiral Ltd
61  */
62 
63 #include <stdio.h>
64 
65 #include "gwlib/gwlib.h"
66 #include "gw/ota_compiler.h"
67 
68 Octstr *charset = NULL;
69 Octstr *file_name = NULL;
70 
71 static void help (void)
72 {
73  info(0, "Usage test_ota [options] ota_source");
74  info(0, "where options are");
75  info(0, "-h - print this text");
76  info(0, "-f <file> - output binary to 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, file, have_charset, ret;
84  FILE *fp;
85  Octstr *ota_doc, *ota_binary;
86 
87  file = 0;
88  have_charset = 0;
89  fp = NULL;
90 
91  gwlib_init();
92 
93  while ((opt = getopt(argc, argv, "hf:c:v:")) != EOF) {
94  switch (opt) {
95  case 'h':
96  help();
97  exit(1);
98  break;
99 
100  case 'f':
101  file = 1;
102  file_name = octstr_create(optarg);
103  fp = fopen(optarg, "a");
104  if (fp == NULL)
105  panic(0, "Cannot open output file");
106  break;
107 
108  case 'c':
109  have_charset = 1;
110  charset = octstr_create(optarg);
111  break;
112 
113  case 'v':
115  break;
116 
117  case '?':
118  default:
119  error(0, "Invalid option %c", opt);
120  help();
121  panic(0, "Stopping");
122  break;
123  }
124  }
125 
126  if (optind >= argc) {
127  error(0, "Missing arguments");
128  help();
129  panic(0, "Stopping");
130  }
131 
132  ota_doc = octstr_read_file(argv[optind]);
133  if (ota_doc == NULL)
134  panic(0, "Cannot read the ota document");
135 
136  if (!have_charset)
137  charset = NULL;
138 
139  /* run compiler */
140  ret = ota_compile(ota_doc, charset, &ota_binary);
141  debug("test.ota", 0, "ota compiler returned %d", ret);
142 
143  if (ret == 0) {
144  if (fp == NULL)
145  fp = stdout;
146 
147  if (file) {
148  octstr_print(fp, ota_binary);
149  } else {
150  debug("test.ota", 0, "ota binary was");
151  octstr_dump(ota_binary, 0);
152  }
153  }
154 
155  if (have_charset)
156  octstr_destroy(charset);
157  if (file) {
158  fclose(fp);
159  octstr_destroy(file_name);
160  }
161 
162  octstr_destroy(ota_doc);
163  if (ret == 0)
164  octstr_destroy(ota_binary);
165 
166  gwlib_shutdown();
167  return 0;
168 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void info(int err, const char *fmt,...)
Definition: log.c:636
int ota_compile(Octstr *ota_doc, Octstr *charset, Octstr **ota_binary)
Definition: ota_compiler.c:473
int main(int argc, char **argv)
Definition: test_ota.c:81
int optind
Definition: attgetopt.c:80
int octstr_print(FILE *f, Octstr *ostr)
Definition: octstr.c:1189
Octstr * charset
Definition: test_ota.c:68
FILE * file
Definition: log.c:133
int getopt(int argc, char **argv, char *opts)
Definition: attgetopt.c:84
void log_set_output_level(enum output_level level)
Definition: log.c:217
#define octstr_dump(ostr, level,...)
Definition: octstr.h:564
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
static void help(void)
Definition: test_ota.c:71
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
void gwlib_init(void)
Definition: gwlib.c:78
Octstr * file_name
Definition: test_ota.c:69
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.