Fix defects detected by static analysis tool
[platform/core/uifw/isf.git] / ism / src / scim_helper_launcher.cpp
1 /* ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable. */
2
3 /*
4  * Smart Common Input Method
5  *
6  * Copyright (c) 2004-2005 James Su <suzhe@tsinghua.org.cn>
7  * Copyright (c) 2012-2015 Samsung Electronics Co., Ltd.
8  *
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA  02111-1307  USA
24  *
25  * Modifications by Samsung Electronics Co., Ltd.
26  * 1. Call set_arg_info () before run_helper ()
27  *
28  * $Id: scim_helper_launcher.cpp,v 1.6 2005/05/16 01:25:46 suzhe Exp $
29  *
30  */
31
32 #define Uses_SCIM_HELPER_MODULE
33 #define Uses_SCIM_GLOBAL_CONFIG
34 #define Uses_SCIM_CONFIG_PATH
35 #include <stdlib.h>
36 #include "scim_private.h"
37 #include "scim.h"
38 #include <unistd.h>
39 #include <string.h>
40
41 using namespace scim;
42
43 static String sanitize_string (const char *str, int maxlen = 32)
44 {
45     String ret;
46     static char acceptables[] =
47         "abcdefghijklmnopqrstuvwxyz"
48         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
49         "1234567890_-.@:";
50
51     char *newstr = NULL;
52     if (maxlen > 0) {
53         newstr = new char[maxlen + 1];
54     }
55     int len = 0;
56     if (newstr) {
57         memset (newstr, 0x00, sizeof (char) * (maxlen + 1));
58
59         if (str) {
60             while (len < maxlen && str[len] != '\0' && strchr (acceptables, str[len]) != NULL) {
61                 newstr[len] = str[len];
62                 len++;
63             }
64             ret = newstr;
65         }
66         delete [] newstr;
67     }
68     return ret;
69 }
70
71 int main (int argc, char *argv [])
72 {
73     int i = 0;
74     int j = 0;
75     String config;
76     String display;
77     String helper;
78     String appid;
79     //bool   daemon = false;
80
81     char *p =  getenv ("DISPLAY");
82     if (p) display = String (p);
83
84     //In the normal situation, config should be set by CMD,
85     //if not provide, we assume have something wrong in
86     //server side, so using simple config as default
87     config = scim_global_config_read (SCIM_GLOBAL_CONFIG_DEFAULT_CONFIG_MODULE, String ("simple"));
88
89     while (i < argc) {
90         if (++i >= argc) break;
91
92         if (String ("-c") == argv [i] ||
93             String ("--config") == argv [i]) {
94             if (++i >= argc) {
95                 std::cerr << "No argument for option " << argv [i-1] << "\n";
96                 exit (-1);
97             }
98             config = argv [i];
99             continue;
100         }
101
102         if (String ("-d") == argv [i] ||
103             String ("--daemon") == argv [i]) {
104             //daemon = true;
105             continue;
106         }
107
108         if (String ("--display") == argv [i]) {
109             if (++i >= argc) {
110                 std::cerr << "No argument for option " << argv [i-1] << "\n";
111                 exit (-1);
112             }
113             display = argv [i];
114             continue;
115         }
116
117         if (String ("-h") == argv [i] ||
118             String ("--help") == argv [i]) {
119             std::cout << "Usage: " << argv [0] << " [options] module appid\n\n"
120                       << "The options are:\n"
121                       << "  -c, --config name    Use specified config module, default is \"simple\".\n"
122                       << "  -d, --daemon         Run as daemon.\n"
123                       << "  --display name       run setup on a specified DISPLAY.\n"
124                       << "  -h, --help           Show this help message.\n"
125                       << "module                 The name of the Helper module\n"
126                       << "appid                  The appid of the Helper to be launched.\n";
127             return 0;
128         }
129
130         if (String ("-v") == argv [i] ||
131             String ("--verbose") == argv [i]) {
132             if (++i >= argc) {
133                 std::cerr << "No argument for option " << argv [i-1] << "\n";
134                 return -1;
135             }
136             DebugOutput::set_verbose_level (atoi (argv [i]));
137             continue;
138         }
139
140         if (String ("-m") == argv [i] ||
141             String ("--mask") == argv [i]) {
142             if (++i >= argc) {
143                 std::cerr << "No argument for option " << argv [i-1] << "\n";
144                 return -1;
145             }
146             if (String (argv [i]) != "none") {
147                 std::vector<String> debug_mask_list;
148                 scim_split_string_list (debug_mask_list, argv [i], ',');
149                 DebugOutput::disable_debug (SCIM_DEBUG_AllMask);
150                 for (size_t k=0; k<debug_mask_list.size (); k++)
151                     DebugOutput::enable_debug_by_name (debug_mask_list [k]);
152             }
153             continue;
154         }
155
156         if (String ("-o") == argv [i] ||
157             String ("--output") == argv [i]) {
158             if (++i >= argc) {
159                 std::cerr << "No argument for option " << argv [i-1] << "\n";
160                 return -1;
161             }
162             DebugOutput::set_output (sanitize_string (argv [i]));
163             continue;
164         }
165
166         ++j;
167
168         if (j == 1) {
169             helper = String (argv [i]);
170             continue;
171         } else if (j == 2) {
172             appid = String (argv [i]);
173             continue;
174         }
175     }
176
177     String exec = sanitize_string (argv[0]);
178
179     if (exec != String (SCIM_HELPER_LAUNCHER_PROGRAM)) {
180         /* 3rd party shared object type IME */
181         helper = String ("lib") + exec.substr (exec.find_last_of (SCIM_PATH_DELIM) + 1);
182         appid = String ("");
183     }
184
185     SCIM_DEBUG_MAIN(1) << "scim-helper-launcher: " << config << " " << display << " " << helper << " " << appid << "\n";
186     ISF_SAVE_LOG ("Helper ISE (%s %s) is launching...", helper.c_str (), appid.c_str ());
187
188     if (!helper.length ()) {
189         ISF_SAVE_LOG ("Module name is missing");
190
191         std::cerr << "Module name is missing.\n";
192         return -1;
193     }
194
195     HelperModule helper_module (helper);
196
197     if (!helper_module.valid () || helper_module.number_of_helpers () == 0) {
198         ISF_SAVE_LOG ("Unable to load helper module(%s)", helper.c_str ());
199
200         std::cerr << "Unable to load helper module(" << helper << ")\n";
201         return -1;
202     }
203
204     ConfigPointer config_pointer = ConfigBase::get (true, config);
205
206     if (config_pointer.null ()) {
207         config_pointer = new DummyConfig ();
208     }
209
210 //    if (daemon) scim_daemon ();
211
212     helper_module.set_arg_info (argc, argv);
213     helper_module.run_helper (appid, config_pointer, display);
214     helper_module.unload ();
215
216     if (!config_pointer.null ())
217         config_pointer.reset ();
218     ConfigBase::set (0);
219     ISF_SAVE_LOG ("Helper ISE (%s) is destroyed!!!", appid.c_str ());
220 }
221
222 /*
223 vi:ts=4:nowrap:ai:expandtab
224 */