2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include "launchpad_common.h"
26 #include "debugger_info.h"
28 #define TAG_DEBUGGER "[DEBUGGER]"
29 #define TAG_NAME "NAME"
31 #define TAG_APP_TYPE "APP_TYPE"
32 #define TAG_EXTRA_KEY "EXTRA_KEY"
33 #define TAG_EXTRA_ENV "EXTRA_ENV"
34 #define TAG_UNLINK "UNLINK"
35 #define TAG_ATTACH "ATTACH"
36 #define TAG_LAST_EXTRA_KEY "LAST_EXTRA_KEY"
37 #define TAG_DEFAULT_OPT "DEFAULT_OPT"
39 struct debugger_info_s {
43 GList *extra_key_list;
44 GList *extra_env_list;
47 GList *last_extra_key_list;
48 GList *default_opt_list;
51 static struct debugger_info_s *__create_debugger_info(void)
53 struct debugger_info_s *info;
55 info = calloc(1, sizeof(struct debugger_info_s));
64 static void __destroy_debugger_info(gpointer data)
66 struct debugger_info_s *info = (struct debugger_info_s *)data;
71 if (info->default_opt_list)
72 g_list_free_full(info->default_opt_list, free);
73 if (info->last_extra_key_list)
74 g_list_free_full(info->last_extra_key_list, free);
77 if (info->unlink_list)
78 g_list_free_full(info->unlink_list, free);
79 if (info->extra_env_list)
80 g_list_free_full(info->extra_env_list, free);
81 if (info->extra_key_list)
82 g_list_free_full(info->extra_key_list, free);
84 g_list_free_full(info->app_types, free);
92 static void __parse_app_types(struct debugger_info_s *info, char *line)
97 token = strtok_r(line, " |\t\r\n", &saveptr);
99 info->app_types = g_list_append(info->app_types, strdup(token));
100 token = strtok_r(NULL, " |\t\r\n", &saveptr);
104 static GList *__parse_file(GList *list, const char *path)
110 struct debugger_info_s *info = NULL;
112 fp = fopen(path, "rt");
116 while (fgets(buf, sizeof(buf), fp) != NULL) {
119 sscanf(buf, "%ms %ms", &tok1, &tok2);
120 if (tok1 && strcasecmp(TAG_DEBUGGER, tok1) == 0) {
122 _D("name: %s, exe: %s", info->name, info->exe);
123 list = g_list_append(list, info);
126 info = __create_debugger_info();
135 if (tok1[0] == '\0' || tok2[0] == '\0' || tok1[0] == '#')
140 if (strcasecmp(TAG_NAME, tok1) == 0) {
141 info->name = strdup(tok2);
142 if (info->name == NULL) {
144 __destroy_debugger_info(info);
148 } else if (strcasecmp(TAG_EXE, tok1) == 0) {
149 info->exe = strdup(tok2);
150 if (info->exe == NULL) {
152 __destroy_debugger_info(info);
156 } else if (strcasecmp(TAG_APP_TYPE, tok1) == 0) {
157 __parse_app_types(info, &buf[strlen(tok1)]);
158 if (info->app_types == NULL) {
159 _E("app_types is NULL");
160 __destroy_debugger_info(info);
164 } else if (strcasecmp(TAG_EXTRA_KEY, tok1) == 0) {
165 info->extra_key_list = g_list_append(
166 info->extra_key_list, strdup(tok2));
167 } else if (strcasecmp(TAG_EXTRA_ENV, tok1) == 0) {
168 info->extra_env_list = g_list_append(
169 info->extra_env_list, strdup(tok2));
170 } else if (strcasecmp(TAG_UNLINK, tok1) == 0) {
171 info->unlink_list = g_list_append(info->unlink_list,
173 } else if (strcasecmp(TAG_ATTACH, tok1) == 0) {
174 info->attach = strdup(tok2);
175 if (info->attach == NULL) {
176 _E("attach is NULL");
177 __destroy_debugger_info(info);
181 } else if (strcasecmp(TAG_LAST_EXTRA_KEY, tok1) == 0) {
182 info->last_extra_key_list = g_list_append(
183 info->last_extra_key_list,
185 } else if (strcasecmp(TAG_DEFAULT_OPT, tok1) == 0) {
186 info->default_opt_list = g_list_append(
187 info->default_opt_list,
194 _D("name: %s, exe: %s", info->name, info->exe);
195 list = g_list_append(list, info);
206 GList *_debugger_info_load(const char *path)
209 struct dirent *dentry = NULL;
221 while ((dentry = readdir(dp)) != NULL) {
222 if (dentry->d_name[0] == '.')
225 ext = strrchr(dentry->d_name, '.');
226 if (ext && strcmp(ext, ".debugger") == 0) {
227 snprintf(buf, sizeof(buf), "%s/%s",
228 path, dentry->d_name);
229 list = __parse_file(list, buf);
237 void _debugger_info_unload(GList *info)
242 g_list_free_full(info, __destroy_debugger_info);
245 static int __comp_name(gconstpointer a, gconstpointer b)
247 struct debugger_info_s *info = (struct debugger_info_s *)a;
249 if (info == NULL || info->name == NULL || b == NULL)
252 if (strcasecmp(info->name, b) == 0)
258 debugger_info_h _debugger_info_find(GList *info_list, const char *name)
262 if (info_list == NULL || name == NULL)
265 list = g_list_find_custom(info_list, name, __comp_name);
269 return (debugger_info_h)list->data;
272 const char *_debugger_info_get_exe(debugger_info_h info)
280 GList *_debugger_info_get_extra_key_list(debugger_info_h info)
285 return info->extra_key_list;
288 GList *_debugger_info_get_extra_env_list(debugger_info_h info)
293 return info->extra_env_list;
296 GList *_debugger_info_get_unlink_list(debugger_info_h info)
301 return info->unlink_list;
304 const char *_debugger_info_get_attach(debugger_info_h info)
312 GList *_debugger_info_get_last_extra_key_list(debugger_info_h info)
317 return info->last_extra_key_list;
320 GList *_debugger_info_get_default_opt_list(debugger_info_h info)
325 return info->default_opt_list;