Fix dlog format argument error
[platform/core/uifw/voice-control.git] / common / vc_command.c
1 /*
2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #define _GNU_SOURCE
18
19 #include <cynara-client.h>
20 #include <cynara-error.h>
21 #include <cynara-session.h>
22 #include <libintl.h>
23 #include <stdlib.h>
24 #include <system_info.h>
25
26 #include "vc_command.h"
27 #include "vc_info_parser.h"
28 #include "vc_main.h"
29 #include "vc_regex_rule.h"
30 #include "vc_config_mgr.h"
31 #include "voice_control_command.h"
32 #include "voice_control_command_expand.h"
33 #include "voice_control_common.h"
34 #include "voice_control_key_defines.h"
35
36 static int g_feature_enabled = -1;
37
38 static int g_privilege_allowed = 1; /* Always True */
39 static cynara *p_cynara = NULL;
40 static GList *g_cmdlist_list = NULL;
41
42
43 // For getting timestamp using regular expression
44 static regex_t reg[MAX_NUM_REGEX];
45 static time_t t_now;            //time_t is based on UTC
46 static struct tm td_now;        //if use localtime function, the value follows the local time zone, otherwise it follows the UTC.
47
48 static int g_time_flag;
49 static int g_date_flag;
50
51 static int g_data_sidx;
52 static int g_data_eidx;
53
54 static int __vc_cmd_get_feature_enabled()
55 {
56         if (0 == g_feature_enabled) {
57                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Voice control feature NOT supported");
58                 return VC_ERROR_NOT_SUPPORTED;
59         } else if (-1 == g_feature_enabled) {
60                 bool vc_supported = false;
61                 bool mic_supported = false;
62                 if (0 == system_info_get_platform_bool(VC_FEATURE_PATH, &vc_supported)) {
63                         if (0 == system_info_get_platform_bool(VC_MIC_FEATURE_PATH, &mic_supported)) {
64                                 if (false == vc_supported || false == mic_supported) {
65                                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Voice control feature NOT supported");
66                                         g_feature_enabled = 0;
67                                         return VC_ERROR_NOT_SUPPORTED;
68                                 }
69
70                                 g_feature_enabled = 1;
71                         } else {
72                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get feature value");
73                                 return VC_ERROR_NOT_SUPPORTED;
74                         }
75                 } else {
76                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get feature value");
77                         return VC_ERROR_NOT_SUPPORTED;
78                 }
79         }
80
81         return 0;
82 }
83
84 static int __check_privilege_initialize()
85 {
86         int ret = cynara_initialize(&p_cynara, NULL);
87         if (CYNARA_API_SUCCESS != ret)
88                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] fail to initialize");
89
90         return ret == CYNARA_API_SUCCESS;
91 }
92
93 static int __check_privilege(const char* uid, const char * privilege)
94 {
95         FILE *fp = NULL;
96         char label_path[1024] = "/proc/self/attr/current";
97         char smack_label[1024] = {'\0',};
98
99         if (!p_cynara) {
100             return false;
101         }
102
103         fp = fopen(label_path, "r");
104         if (fp != NULL) {
105             if (fread(smack_label, 1, sizeof(smack_label), fp) <= 0)
106                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] fail to fread");
107
108             fclose(fp);
109         }
110
111         pid_t pid = getpid();
112         char *session = cynara_session_from_pid(pid);
113         int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
114         SLOG(LOG_DEBUG, TAG_VCCMD, "[Client]cynara_check returned %d(%s)", ret, (CYNARA_API_ACCESS_ALLOWED == ret) ? "Allowed" : "Denied");
115         if (session)
116             free(session);
117
118         if (ret != CYNARA_API_ACCESS_ALLOWED)
119             return false;
120         return true;
121 }
122
123 static void __check_privilege_deinitialize()
124 {
125         if (p_cynara)
126                 cynara_finish(p_cynara);
127         p_cynara = NULL;
128 }
129
130 static int __vc_cmd_check_privilege()
131 {
132         char uid[16];
133
134         if (0 == g_privilege_allowed) {
135                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Permission is denied");
136                 return VC_ERROR_PERMISSION_DENIED;
137         } else if (-1 == g_privilege_allowed) {
138                 if (false == __check_privilege_initialize()) {
139                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] privilege initialize is failed");
140                         return VC_ERROR_PERMISSION_DENIED;
141                 }
142                 snprintf(uid, 16, "%d", getuid());
143                 if (false == __check_privilege(uid, VC_PRIVILEGE)) {
144                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Permission is denied");
145                         g_privilege_allowed = 0;
146                         __check_privilege_deinitialize();
147                         return VC_ERROR_PERMISSION_DENIED;
148                 }
149                 __check_privilege_deinitialize();
150         }
151
152         g_privilege_allowed = 1;
153
154         return VC_ERROR_NONE;
155 }
156
157 int vc_cmd_list_create(vc_cmd_list_h* vc_cmd_list)
158 {
159         if (0 != __vc_cmd_get_feature_enabled()) {
160                 return VC_ERROR_NOT_SUPPORTED;
161         }
162         if (0 != __vc_cmd_check_privilege()) {
163                 return VC_ERROR_PERMISSION_DENIED;
164         }
165
166         if (NULL == vc_cmd_list) {
167                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
168                 return VC_ERROR_INVALID_PARAMETER;
169         }
170
171         vc_cmd_list_s* list = (vc_cmd_list_s*)calloc(1, sizeof(vc_cmd_list_s));
172
173         if (NULL == list) {
174                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Not enough memory");
175                 return VC_ERROR_OUT_OF_MEMORY;
176         }
177
178         list->index = -1;
179         list->list = NULL;
180
181         *vc_cmd_list = (vc_cmd_list_h)list;
182
183         g_cmdlist_list = g_list_append(g_cmdlist_list, list);
184
185         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p)", *vc_cmd_list);
186
187         return VC_ERROR_NONE;
188 }
189
190 int vc_cmd_list_destroy(vc_cmd_list_h vc_cmd_list, bool release_command)
191 {
192         if (0 != __vc_cmd_get_feature_enabled()) {
193                 return VC_ERROR_NOT_SUPPORTED;
194         }
195         if (0 != __vc_cmd_check_privilege()) {
196                 return VC_ERROR_PERMISSION_DENIED;
197         }
198
199         if (NULL == vc_cmd_list) {
200                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
201                 return VC_ERROR_INVALID_PARAMETER;
202         }
203
204         vc_cmd_list_remove_all(vc_cmd_list, release_command);
205
206         vc_cmd_list_s* list = NULL;
207         list = (vc_cmd_list_s*)vc_cmd_list;
208
209         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p)", list);
210
211         GList *iter = NULL;
212         vc_cmd_list_s *data = NULL;
213
214         /* if list have item */
215         if (g_list_length(g_cmdlist_list) > 0) {
216                 /* Get a first item */
217                 iter = g_list_first(g_cmdlist_list);
218
219                 while (NULL != iter) {
220                         data = iter->data;
221                         if (data && list == data) {
222                                 g_cmdlist_list = g_list_remove_link(g_cmdlist_list, iter);
223
224                                 free(data);
225                                 data = NULL;
226
227                                 SLOG(LOG_DEBUG, TAG_VCCMD, "Client destroy");
228                                 g_list_free(iter);
229
230                                 return VC_ERROR_NONE;
231                         }
232                         /* Next item */
233                         iter = g_list_next(iter);
234                 }
235         }
236         SLOG(LOG_ERROR, TAG_VCCMD, "Fail to destroy client : handle is not valid");
237
238         return VC_ERROR_INVALID_PARAMETER;
239 }
240
241 int vc_cmd_list_get_count(vc_cmd_list_h vc_cmd_list, int* count)
242 {
243         if (0 != __vc_cmd_get_feature_enabled()) {
244                 return VC_ERROR_NOT_SUPPORTED;
245         }
246         if (0 != __vc_cmd_check_privilege()) {
247                 return VC_ERROR_PERMISSION_DENIED;
248         }
249
250         if (NULL == vc_cmd_list || NULL == count) {
251                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Get command count : Input parameter is NULL");
252                 return VC_ERROR_INVALID_PARAMETER;
253         }
254
255         vc_cmd_list_s* list = NULL;
256         list = (vc_cmd_list_s*)vc_cmd_list;
257
258         *count = g_slist_length(list->list);
259
260         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p), count(%d)", list, *count);
261
262         return VC_ERROR_NONE;
263 }
264
265 int vc_cmd_list_add(vc_cmd_list_h vc_cmd_list, vc_cmd_h vc_command)
266 {
267         if (0 != __vc_cmd_get_feature_enabled()) {
268                 return VC_ERROR_NOT_SUPPORTED;
269         }
270         if (0 != __vc_cmd_check_privilege()) {
271                 return VC_ERROR_PERMISSION_DENIED;
272         }
273
274         if (NULL == vc_cmd_list || NULL == vc_command) {
275                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
276                 return VC_ERROR_INVALID_PARAMETER;
277         }
278
279         vc_cmd_list_s* list = NULL;
280         list = (vc_cmd_list_s*)vc_cmd_list;
281
282         vc_cmd_s* cmd = NULL;
283         cmd = (vc_cmd_s*)vc_command;
284
285         list->list = g_slist_append(list->list, cmd);
286
287         if (1 == g_slist_length(list->list)) {
288                 list->index = 0;
289         }
290
291         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p), command(%p)", list, cmd);
292
293         return VC_ERROR_NONE;
294 }
295
296 int vc_cmd_list_remove(vc_cmd_list_h vc_cmd_list, vc_cmd_h vc_command)
297 {
298         if (0 != __vc_cmd_get_feature_enabled()) {
299                 return VC_ERROR_NOT_SUPPORTED;
300         }
301         if (0 != __vc_cmd_check_privilege()) {
302                 return VC_ERROR_PERMISSION_DENIED;
303         }
304
305         if (NULL == vc_cmd_list || NULL == vc_command) {
306                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
307                 return VC_ERROR_INVALID_PARAMETER;
308         }
309
310         vc_cmd_list_s* list = NULL;
311         list = (vc_cmd_list_s*)vc_cmd_list;
312
313         vc_cmd_s* cmd = NULL;
314         cmd = (vc_cmd_s*)vc_command;
315
316         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p), command(%p)", list, cmd);
317
318         vc_cmd_s* temp_cmd = NULL;
319         GSList *iter = NULL;
320
321         iter = g_slist_nth(list->list, 0);
322
323         while (NULL != iter) {
324                 temp_cmd = iter->data;
325
326                 if (NULL != temp_cmd && cmd == temp_cmd) {
327                         list->list = g_slist_remove(list->list, temp_cmd);
328                         /*
329                         if (true == release_command) {
330                                 SLOG(LOG_DEBUG, TAG_VCCMD, "Release command data");
331                                 if (NULL != temp_cmd->command)          free(temp_cmd->command);
332                                 if (NULL != temp_cmd->parameter)        free(temp_cmd->parameter);
333                                 free(temp_cmd);
334                                 temp_cmd = NULL;
335                         }
336                         */
337                 }
338
339                 iter = g_slist_next(iter);
340         }
341
342         int count = g_slist_length(list->list);
343
344         if (0 == count) {
345                 list->index = -1;
346         } else if (list->index == count) {
347                 list->index = count - 1;
348         }
349
350         return VC_ERROR_NONE;
351 }
352
353 int vc_cmd_list_remove_all(vc_cmd_list_h vc_cmd_list, bool release_command)
354 {
355         if (0 != __vc_cmd_get_feature_enabled()) {
356                 return VC_ERROR_NOT_SUPPORTED;
357         }
358         if (0 != __vc_cmd_check_privilege()) {
359                 return VC_ERROR_PERMISSION_DENIED;
360         }
361
362         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Destroy all command");
363
364         if (NULL == vc_cmd_list) {
365                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
366                 return VC_ERROR_INVALID_PARAMETER;
367         }
368
369         vc_cmd_list_s* list = NULL;
370         list = (vc_cmd_list_s*)vc_cmd_list;
371
372         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list (%p), release command (%s)"
373                  , list, release_command ? "true" : "false");
374
375         int count = g_slist_length(list->list);
376
377         int i ;
378         vc_cmd_s *temp_cmd;
379
380         for (i = 0; i < count ; i++) {
381                 temp_cmd = g_slist_nth_data(list->list, 0);
382
383                 if (NULL != temp_cmd) {
384                         list->list = g_slist_remove(list->list, temp_cmd);
385
386                         if (true == release_command) {
387                                 SLOG(LOG_DEBUG, TAG_VCCMD, "Free command(%p)", temp_cmd);
388                                 if (NULL != temp_cmd->command)          free(temp_cmd->command);
389                                 if (NULL != temp_cmd->parameter)        free(temp_cmd->parameter);
390                                 if (NULL != temp_cmd->appid)            free(temp_cmd->appid);
391                                 if (NULL != temp_cmd->invocation_name)  free(temp_cmd->invocation_name);
392                                 if (NULL != temp_cmd->fixed)            free(temp_cmd->fixed);
393                                 free(temp_cmd);
394                                 temp_cmd = NULL;
395                         }
396                 }
397         }
398         list->list = NULL;
399         list->index = -1;
400
401         SLOG(LOG_DEBUG, TAG_VCCMD, "=====");
402         SLOG(LOG_DEBUG, TAG_VCCMD, " ");
403
404         return VC_ERROR_NONE;
405 }
406
407 int vc_cmd_list_foreach_commands(vc_cmd_list_h vc_cmd_list, vc_cmd_list_cb callback, void* user_data)
408 {
409         if (0 != __vc_cmd_get_feature_enabled()) {
410                 return VC_ERROR_NOT_SUPPORTED;
411         }
412         if (0 != __vc_cmd_check_privilege()) {
413                 return VC_ERROR_PERMISSION_DENIED;
414         }
415
416         if (NULL == vc_cmd_list) {
417                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
418                 return VC_ERROR_INVALID_PARAMETER;
419         }
420
421         vc_cmd_list_s* list = NULL;
422         list = (vc_cmd_list_s*)vc_cmd_list;
423
424         int count = g_slist_length(list->list);
425         int i ;
426
427         GSList *iter = NULL;
428         vc_cmd_s *temp_cmd;
429
430         iter = g_slist_nth(list->list, 0);
431
432         for (i = 0; i < count; i++) {
433                 if (NULL == iter) {
434                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] No command in list");
435                         return VC_ERROR_OPERATION_FAILED;
436                 }
437
438                 temp_cmd = iter->data;
439
440                 if (NULL != temp_cmd) {
441                         if (false == callback((vc_cmd_h)temp_cmd, user_data)) {
442                                 break;
443                         }
444
445                 }
446                 iter = g_slist_next(iter);
447         }
448
449         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Foreach commands Done");
450
451         return VC_ERROR_NONE;
452 }
453
454 int vc_cmd_list_filter_by_type(vc_cmd_list_h original, int type, vc_cmd_list_h* filtered)
455 {
456         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Filter by type");
457
458         if (0 != __vc_cmd_get_feature_enabled()) {
459                 return VC_ERROR_NOT_SUPPORTED;
460         }
461         if (0 != __vc_cmd_check_privilege()) {
462                 return VC_ERROR_PERMISSION_DENIED;
463         }
464
465         if (NULL == original) {
466                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
467                 return VC_ERROR_INVALID_PARAMETER;
468         }
469
470         if (VC_COMMAND_TYPE_NONE >= type || VC_COMMAND_TYPE_EXCLUSIVE < type) {
471                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid type");
472                 return VC_ERROR_INVALID_PARAMETER;
473         }
474
475         vc_cmd_list_s* list = NULL;
476         list = (vc_cmd_list_s*)original;
477
478         vc_cmd_list_h temp_list;
479         if (0 != vc_cmd_list_create(&temp_list)) {
480                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to list create");
481                 return VC_ERROR_OPERATION_FAILED;
482         }
483
484         int count = g_slist_length(list->list);
485         int i;
486
487         GSList *iter = NULL;
488         vc_cmd_s *iter_cmd;
489
490         iter = g_slist_nth(list->list, 0);
491
492         for (i = 0; i < count; i++) {
493                 if (NULL == iter) {
494                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] No command in list");
495                         return VC_ERROR_OPERATION_FAILED;
496                 }
497                 if (NULL != iter->data) {
498                         iter_cmd = iter->data;
499
500                         if (NULL != iter_cmd) {
501                                 int iter_type;
502                                 if (0 != vc_cmd_get_type((vc_cmd_h)iter_cmd, &iter_type)) {
503                                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get command type");
504                                         continue;
505                                 }
506
507                                 if (iter_type == type) {
508                                         vc_cmd_h temp_cmd;
509                                         if (0 != vc_cmd_create(&temp_cmd)) {
510                                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to create cmd");
511                                                 continue;
512                                         }
513
514                                         memcpy(temp_cmd, iter_cmd, sizeof(vc_cmd_s));
515                                         if (NULL != iter_cmd->command) {
516                                                 ((vc_cmd_s*)temp_cmd)->command = strdup(iter_cmd->command);
517                                         }
518                                         if (NULL != iter_cmd->parameter) {
519                                                 ((vc_cmd_s*)temp_cmd)->parameter = strdup(iter_cmd->parameter);
520                                         }
521
522                                         if (0 != vc_cmd_list_add(temp_list, temp_cmd)) {
523                                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to cmd list add");
524                                                 vc_cmd_destroy(temp_cmd);
525                                                 continue;
526                                         }
527                                 }
528                         }
529                 }
530                 iter = g_slist_next(iter);
531         }
532
533         count = 0;
534         if (0 != vc_cmd_list_get_count(temp_list, &count)) {
535                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get count");
536         } else {
537                 SLOG(LOG_DEBUG, TAG_VCCMD, "Filtering result : (%d) command", count);
538         }
539
540         *filtered = temp_list;
541
542         SLOG(LOG_DEBUG, TAG_VCCMD, "=====");
543
544         return VC_ERROR_NONE;
545 }
546
547 int vc_cmd_list_first(vc_cmd_list_h vc_cmd_list)
548 {
549         if (0 != __vc_cmd_get_feature_enabled()) {
550                 return VC_ERROR_NOT_SUPPORTED;
551         }
552         if (0 != __vc_cmd_check_privilege()) {
553                 return VC_ERROR_PERMISSION_DENIED;
554         }
555
556         if (NULL == vc_cmd_list) {
557                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
558                 return VC_ERROR_INVALID_PARAMETER;
559         }
560
561         vc_cmd_list_s* list = NULL;
562         list = (vc_cmd_list_s*)vc_cmd_list;
563
564         if (0 == g_slist_length(list->list)) {
565                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] List is empty");
566                 return VC_ERROR_EMPTY;
567         }
568
569         list->index = 0;
570
571         return VC_ERROR_NONE;
572 }
573
574 int vc_cmd_list_last(vc_cmd_list_h vc_cmd_list)
575 {
576         if (0 != __vc_cmd_get_feature_enabled()) {
577                 return VC_ERROR_NOT_SUPPORTED;
578         }
579         if (0 != __vc_cmd_check_privilege()) {
580                 return VC_ERROR_PERMISSION_DENIED;
581         }
582
583         if (NULL == vc_cmd_list) {
584                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
585                 return VC_ERROR_INVALID_PARAMETER;
586         }
587
588         vc_cmd_list_s* list = NULL;
589         list = (vc_cmd_list_s*)vc_cmd_list;
590
591         int count = g_slist_length(list->list);
592
593         if (0 == count) {
594                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] List is empty");
595                 return VC_ERROR_EMPTY;
596         } else {
597                 list->index = count - 1;
598                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
599         }
600
601         return VC_ERROR_NONE;
602 }
603
604 int vc_cmd_list_next(vc_cmd_list_h vc_cmd_list)
605 {
606         if (0 != __vc_cmd_get_feature_enabled()) {
607                 return VC_ERROR_NOT_SUPPORTED;
608         }
609         if (0 != __vc_cmd_check_privilege()) {
610                 return VC_ERROR_PERMISSION_DENIED;
611         }
612
613         if (NULL == vc_cmd_list) {
614                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
615                 return VC_ERROR_INVALID_PARAMETER;
616         }
617
618         vc_cmd_list_s* list = NULL;
619         list = (vc_cmd_list_s*)vc_cmd_list;
620
621         int count = g_slist_length(list->list);
622
623         if (list->index < count - 1) {
624                 list->index = list->index + 1;
625                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
626         } else {
627                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
628                 return VC_ERROR_ITERATION_END;
629         }
630
631         return VC_ERROR_NONE;
632 }
633
634 int vc_cmd_list_prev(vc_cmd_list_h vc_cmd_list)
635 {
636         if (0 != __vc_cmd_get_feature_enabled()) {
637                 return VC_ERROR_NOT_SUPPORTED;
638         }
639         if (0 != __vc_cmd_check_privilege()) {
640                 return VC_ERROR_PERMISSION_DENIED;
641         }
642
643         if (NULL == vc_cmd_list) {
644                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
645                 return VC_ERROR_INVALID_PARAMETER;
646         }
647
648         vc_cmd_list_s* list = NULL;
649         list = (vc_cmd_list_s*)vc_cmd_list;
650
651         if (list->index > 0) {
652                 list->index = list->index - 1;
653                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
654         } else {
655                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
656                 return VC_ERROR_ITERATION_END;
657         }
658
659         return VC_ERROR_NONE;
660 }
661
662 int vc_cmd_list_get_current(vc_cmd_list_h vc_cmd_list, vc_cmd_h* vc_command)
663 {
664         if (0 != __vc_cmd_get_feature_enabled()) {
665                 return VC_ERROR_NOT_SUPPORTED;
666         }
667         if (0 != __vc_cmd_check_privilege()) {
668                 return VC_ERROR_PERMISSION_DENIED;
669         }
670
671         if (NULL == vc_cmd_list || NULL == vc_command) {
672                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
673                 return VC_ERROR_INVALID_PARAMETER;
674         }
675
676         vc_cmd_list_s* list = NULL;
677         list = (vc_cmd_list_s*)vc_cmd_list;
678
679         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list (%p), index (%d)", list, list->index);
680
681         if (0 == g_slist_length(list->list)) {
682                 SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list is empty");
683                 *vc_command = NULL;
684                 return VC_ERROR_EMPTY;
685         }
686
687         vc_cmd_s *temp_cmd = NULL;
688         temp_cmd = g_slist_nth_data(list->list, list->index);
689
690         *vc_command = (vc_cmd_h)temp_cmd;
691
692         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] Get current command (%p)", *vc_command);
693
694         return VC_ERROR_NONE;
695 }
696
697
698 int vc_cmd_create(vc_cmd_h* vc_command)
699 {
700         if (0 != __vc_cmd_get_feature_enabled()) {
701                 return VC_ERROR_NOT_SUPPORTED;
702         }
703         if (0 != __vc_cmd_check_privilege()) {
704                 return VC_ERROR_PERMISSION_DENIED;
705         }
706
707         if (NULL == vc_command) {
708                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
709                 return VC_ERROR_INVALID_PARAMETER;
710         }
711
712         vc_cmd_s* command = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
713
714         if (NULL == command) {
715                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Not enough memory");
716                 return VC_ERROR_OUT_OF_MEMORY;
717         }
718
719         command->pid = 0;
720         command->id = 0;
721         command->index = 0;
722         command->type = VC_COMMAND_TYPE_NONE;
723         command->format = VC_CMD_FORMAT_FIXED;
724         command->command = NULL;
725         command->parameter = NULL;
726         command->domain = 0;
727         command->priority = 0;
728         command->key = VC_KEY_NONE;
729         command->modifier = VC_MODIFIER_NONE;
730         command->invocation_name = NULL;
731         command->appid = NULL;
732         command->fixed = NULL;
733
734         *vc_command = (vc_cmd_h)command;
735
736         SLOG(LOG_DEBUG, TAG_VCCMD, "[Create command][%p]", *vc_command);
737
738         return VC_ERROR_NONE;
739 }
740
741 int vc_cmd_destroy(vc_cmd_h vc_command)
742 {
743         if (0 != __vc_cmd_get_feature_enabled()) {
744                 return VC_ERROR_NOT_SUPPORTED;
745         }
746         if (0 != __vc_cmd_check_privilege()) {
747                 return VC_ERROR_PERMISSION_DENIED;
748         }
749
750         if (NULL == vc_command) {
751                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
752                 return VC_ERROR_INVALID_PARAMETER;
753         }
754
755         vc_cmd_s* command = NULL;
756         command = (vc_cmd_s*)vc_command;
757
758         SLOG(LOG_DEBUG, TAG_VCCMD, "[Destroy command][%p]", command);
759
760         if (NULL != command) {
761                 if (NULL != command->command) {
762                         free(command->command);
763                         command->command = NULL;
764                 }
765                 if (NULL != command->parameter) {
766                         free(command->parameter);
767                         command->parameter = NULL;
768                 }
769                 if (NULL != command->invocation_name) {
770                         free(command->invocation_name);
771                         command->invocation_name = NULL;
772                 }
773                 if (NULL != command->appid) {
774                         free(command->appid);
775                         command->appid = NULL;
776                 }
777                 if (NULL != command->fixed) {
778                         free(command->fixed);
779                         command->fixed = NULL;
780                 }
781                 free(command);
782                 command = NULL;
783         }
784
785         return VC_ERROR_NONE;
786 }
787
788 int vc_cmd_set_id(vc_cmd_h vc_command, int id)
789 {
790         if (0 != __vc_cmd_get_feature_enabled()) {
791                 return VC_ERROR_NOT_SUPPORTED;
792         }
793         if (0 != __vc_cmd_check_privilege()) {
794                 return VC_ERROR_PERMISSION_DENIED;
795         }
796
797         if (NULL == vc_command) {
798                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
799                 return VC_ERROR_INVALID_PARAMETER;
800         }
801
802         vc_cmd_s* cmd = NULL;
803         cmd = (vc_cmd_s*)vc_command;
804
805         if (NULL != cmd) {
806                 cmd->id = id;
807                 SLOG(LOG_DEBUG, TAG_VCCMD, "[Set id][%p] id(%d)", vc_command, cmd->id);
808         }
809
810         return 0;
811 }
812
813 int vc_cmd_get_id(vc_cmd_h vc_command, int* id)
814 {
815         if (0 != __vc_cmd_get_feature_enabled()) {
816                 return VC_ERROR_NOT_SUPPORTED;
817         }
818         if (0 != __vc_cmd_check_privilege()) {
819                 return VC_ERROR_PERMISSION_DENIED;
820         }
821
822         if (NULL == vc_command || NULL == id) {
823                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid handle ");
824                 return VC_ERROR_INVALID_PARAMETER;
825         }
826
827         vc_cmd_s* cmd = NULL;
828         cmd = (vc_cmd_s*)vc_command;
829
830         if (NULL != cmd) {
831                 *id = cmd->id;
832                 SLOG(LOG_DEBUG, TAG_VCCMD, "[Get id][%p] id(%d)", vc_command, *id);
833         }
834
835         return 0;
836 }
837
838 int vc_cmd_set_appid(vc_cmd_h vc_command, const char* appid)
839 {
840         if (0 != __vc_cmd_get_feature_enabled()) {
841                 return VC_ERROR_NOT_SUPPORTED;
842         }
843
844         if (NULL == vc_command) {
845                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter, vc_command is NULL");
846                 return VC_ERROR_INVALID_PARAMETER;
847         }
848
849         if (NULL == appid) {
850                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter, appid is NULL");
851                 return VC_ERROR_INVALID_PARAMETER;
852         }
853
854         vc_cmd_s* cmd = NULL;
855         cmd = (vc_cmd_s*)vc_command;
856
857         if (NULL != cmd->appid) {
858                 free(cmd->appid);
859                 cmd->appid = NULL;
860         }
861
862         cmd->appid = strdup(appid);
863
864         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set appid][%p] appid(%s)", vc_command, cmd->appid);
865         return 0;
866 }
867
868 int vc_cmd_get_appid(vc_cmd_h vc_command, char** appid)
869 {
870         if (0 != __vc_cmd_get_feature_enabled()) {
871                 return VC_ERROR_NOT_SUPPORTED;
872         }
873
874         if (NULL == vc_command || NULL == appid) {
875                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid handle ");
876                 return VC_ERROR_INVALID_PARAMETER;
877         }
878
879         vc_cmd_s* cmd = NULL;
880         cmd = (vc_cmd_s*)vc_command;
881
882         if (NULL != cmd->appid) {
883                 *appid = strdup(gettext(cmd->appid));
884         }
885
886         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get appid][%p] appid(%s)", vc_command, *appid);
887         return 0;
888 }
889
890 int vc_cmd_set_command(vc_cmd_h vc_command, const char* command)
891 {
892         if (0 != __vc_cmd_get_feature_enabled()) {
893                 return VC_ERROR_NOT_SUPPORTED;
894         }
895         if (0 != __vc_cmd_check_privilege()) {
896                 return VC_ERROR_PERMISSION_DENIED;
897         }
898
899         if (NULL == vc_command) {
900                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
901                 return VC_ERROR_INVALID_PARAMETER;
902         }
903
904         vc_cmd_s* cmd = NULL;
905         cmd = (vc_cmd_s*)vc_command;
906
907         if (NULL != cmd->command) {
908                 free(cmd->command);
909         }
910
911         cmd->command = NULL;
912
913         if (NULL != command) {
914                 cmd->command = strdup(command);
915         }
916
917         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set command][%p] Command(%s)", vc_command, cmd->command);
918
919         return 0;
920 }
921
922 int vc_cmd_get_command(vc_cmd_h vc_command, char** command)
923 {
924         if (0 != __vc_cmd_get_feature_enabled()) {
925                 return VC_ERROR_NOT_SUPPORTED;
926         }
927         if (0 != __vc_cmd_check_privilege()) {
928                 return VC_ERROR_PERMISSION_DENIED;
929         }
930
931         if (NULL == vc_command || NULL == command) {
932                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid handle ");
933                 return VC_ERROR_INVALID_PARAMETER;
934         }
935
936         vc_cmd_s* cmd = NULL;
937         cmd = (vc_cmd_s*)vc_command;
938
939         if (NULL != cmd->command) {
940                 *command = strdup(gettext(cmd->command));
941         }
942
943         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get command][%p] Command(%s)", vc_command, *command);
944
945         return 0;
946 }
947
948 int vc_cmd_set_unfixed_command(vc_cmd_h vc_command, const char* command)
949 {
950         if (0 != __vc_cmd_get_feature_enabled()) {
951                 return VC_ERROR_NOT_SUPPORTED;
952         }
953         if (0 != __vc_cmd_check_privilege()) {
954                 return VC_ERROR_PERMISSION_DENIED;
955         }
956
957         if (NULL == vc_command) {
958                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
959                 return VC_ERROR_INVALID_PARAMETER;
960         }
961
962         vc_cmd_s* cmd = NULL;
963         cmd = (vc_cmd_s*)vc_command;
964
965         if (NULL != cmd->parameter) {
966                 free(cmd->parameter);
967         }
968
969         cmd->parameter = NULL;
970
971         if (NULL != command) {
972                 cmd->parameter = strdup(command);
973                 SLOG(LOG_DEBUG, TAG_VCCMD, "[Set unfixed command][%p] unfixed command(%s)", vc_command, cmd->parameter);
974         }
975
976         return 0;
977 }
978
979 int vc_cmd_get_unfixed_command(vc_cmd_h vc_command, char** command)
980 {
981         if (0 != __vc_cmd_get_feature_enabled()) {
982                 return VC_ERROR_NOT_SUPPORTED;
983         }
984
985         if (NULL == vc_command || NULL == command) {
986                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid handle ");
987                 return VC_ERROR_INVALID_PARAMETER;
988         }
989
990         vc_cmd_s* cmd = NULL;
991         cmd = (vc_cmd_s*)vc_command;
992
993         if (NULL != cmd->parameter) {
994                 *command = strdup(gettext(cmd->parameter));
995                 SLOG(LOG_DEBUG, TAG_VCCMD, "[Get unfixed command][%p] unfixed command(%s)", vc_command, *command);
996         }
997
998         return 0;
999 }
1000
1001 int vc_cmd_set_fixed_command(vc_cmd_h vc_command, const char* fixed)
1002 {
1003         if (0 != __vc_cmd_get_feature_enabled()) {
1004                 return VC_ERROR_NOT_SUPPORTED;
1005         }
1006
1007         if (NULL == vc_command) {
1008                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1009                 return VC_ERROR_INVALID_PARAMETER;
1010         }
1011
1012         if (NULL == fixed) {
1013                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter, fixed is NULL");
1014                 return VC_ERROR_INVALID_PARAMETER;
1015         }
1016
1017         vc_cmd_s* cmd = NULL;
1018         cmd = (vc_cmd_s*)vc_command;
1019
1020         if (NULL != cmd->fixed) {
1021                 free(cmd->fixed);
1022                 cmd->fixed = NULL;
1023         }
1024
1025         cmd->fixed = strdup(fixed);
1026
1027         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set parameter][%p] fixed command(%s)", vc_command, cmd->fixed);
1028         return 0;
1029 }
1030
1031 int vc_cmd_get_fixed_command(vc_cmd_h vc_command, char** fixed)
1032 {
1033         if (0 != __vc_cmd_get_feature_enabled()) {
1034                 return VC_ERROR_NOT_SUPPORTED;
1035         }
1036
1037         if (NULL == vc_command || NULL == fixed) {
1038                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid handle ");
1039                 return VC_ERROR_INVALID_PARAMETER;
1040         }
1041
1042         vc_cmd_s* cmd = NULL;
1043         cmd = (vc_cmd_s*)vc_command;
1044
1045         if (NULL != cmd->fixed) {
1046                 *fixed = strdup(gettext(cmd->fixed));
1047                 SLOG(LOG_DEBUG, TAG_VCCMD, "[Get fixed command][%p] fixed command(%s)", vc_command, *fixed);
1048         }
1049
1050         return 0;
1051 }
1052
1053 int vc_cmd_set_invocation_name(vc_cmd_h vc_command, const char* invocation_name)
1054 {
1055         if (0 != __vc_cmd_get_feature_enabled()) {
1056                 return VC_ERROR_NOT_SUPPORTED;
1057         }
1058
1059         if (NULL == vc_command) {
1060                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter, vc_command is NULL");
1061                 return VC_ERROR_INVALID_PARAMETER;
1062         }
1063
1064         if (NULL == invocation_name) {
1065                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter, invocation_name is NULL");
1066                 return VC_ERROR_INVALID_PARAMETER;
1067         }
1068
1069         vc_cmd_s* cmd = NULL;
1070         cmd = (vc_cmd_s*)vc_command;
1071
1072         if (NULL != cmd->invocation_name) {
1073                 free(cmd->invocation_name);
1074                 cmd->invocation_name = NULL;
1075         }
1076
1077         cmd->invocation_name = strdup(invocation_name);
1078
1079         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set invocation name][%p] invocation_name(%s)", vc_command, cmd->invocation_name);
1080         return 0;
1081 }
1082
1083 int vc_cmd_get_invocation_name(vc_cmd_h vc_command, char** invocation_name)
1084 {
1085         if (0 != __vc_cmd_get_feature_enabled()) {
1086                 return VC_ERROR_NOT_SUPPORTED;
1087         }
1088
1089         if (NULL == vc_command || NULL == invocation_name) {
1090                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid handle ");
1091                 return VC_ERROR_INVALID_PARAMETER;
1092         }
1093
1094         vc_cmd_s* cmd = NULL;
1095         cmd = (vc_cmd_s*)vc_command;
1096
1097         if (NULL != cmd->invocation_name) {
1098                 *invocation_name = strdup(gettext(cmd->invocation_name));
1099         }
1100
1101         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get invocation name][%p] invocation_name(%s)", vc_command, *invocation_name);
1102         return 0;
1103 }
1104
1105 int vc_cmd_set_type(vc_cmd_h vc_command, int type)
1106 {
1107         if (0 != __vc_cmd_get_feature_enabled()) {
1108                 return VC_ERROR_NOT_SUPPORTED;
1109         }
1110         if (0 != __vc_cmd_check_privilege()) {
1111                 return VC_ERROR_PERMISSION_DENIED;
1112         }
1113
1114         if (NULL == vc_command) {
1115                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1116                 return VC_ERROR_INVALID_PARAMETER;
1117         }
1118
1119         vc_cmd_s* cmd = NULL;
1120         cmd = (vc_cmd_s*)vc_command;
1121
1122         cmd->type = type;
1123
1124         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set type][%p] type(%d)", vc_command, cmd->type);
1125
1126         return 0;
1127 }
1128
1129 int vc_cmd_get_type(vc_cmd_h vc_command, int* type)
1130 {
1131         if (0 != __vc_cmd_get_feature_enabled()) {
1132                 return VC_ERROR_NOT_SUPPORTED;
1133         }
1134         if (0 != __vc_cmd_check_privilege()) {
1135                 return VC_ERROR_PERMISSION_DENIED;
1136         }
1137
1138         if (NULL == vc_command || NULL == type) {
1139                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1140                 return VC_ERROR_INVALID_PARAMETER;
1141         }
1142
1143         vc_cmd_s* cmd = NULL;
1144         cmd = (vc_cmd_s*)vc_command;
1145
1146         *type = cmd->type;
1147
1148         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get type][%p] type(%d)", vc_command, *type);
1149
1150         return 0;
1151 }
1152
1153 int vc_cmd_set_format(vc_cmd_h vc_command, int format)
1154 {
1155         if (0 != __vc_cmd_get_feature_enabled()) {
1156                 return VC_ERROR_NOT_SUPPORTED;
1157         }
1158
1159         if (NULL == vc_command) {
1160                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1161                 return VC_ERROR_INVALID_PARAMETER;
1162         }
1163
1164         vc_cmd_s* cmd = NULL;
1165         cmd = (vc_cmd_s*)vc_command;
1166
1167         cmd->format = format;
1168
1169         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set format][%p] format(%d)", vc_command, format);
1170
1171         return 0;
1172 }
1173
1174 int vc_cmd_get_format(vc_cmd_h vc_command, int* format)
1175 {
1176         if (0 != __vc_cmd_get_feature_enabled()) {
1177                 return VC_ERROR_NOT_SUPPORTED;
1178         }
1179
1180         if (NULL == vc_command || NULL == format) {
1181                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1182                 return VC_ERROR_INVALID_PARAMETER;
1183         }
1184
1185         vc_cmd_s* cmd = NULL;
1186         cmd = (vc_cmd_s*)vc_command;
1187
1188         *format = cmd->format;
1189
1190         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get format][%p] format(%d)", vc_command, *format);
1191
1192         return 0;
1193 }
1194
1195 int vc_cmd_set_pid(vc_cmd_h vc_command, int pid)
1196 {
1197         if (0 != __vc_cmd_get_feature_enabled()) {
1198                 return VC_ERROR_NOT_SUPPORTED;
1199         }
1200         if (0 != __vc_cmd_check_privilege()) {
1201                 return VC_ERROR_PERMISSION_DENIED;
1202         }
1203
1204         if (NULL == vc_command) {
1205                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1206                 return VC_ERROR_INVALID_PARAMETER;
1207         }
1208
1209         vc_cmd_s* cmd = NULL;
1210         cmd = (vc_cmd_s*)vc_command;
1211
1212         cmd->pid = pid;
1213
1214         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set pid][%p] pid(%d)", vc_command, cmd->pid);
1215
1216         return 0;
1217 }
1218
1219 int vc_cmd_get_pid(vc_cmd_h vc_command, int* pid)
1220 {
1221         if (0 != __vc_cmd_get_feature_enabled()) {
1222                 return VC_ERROR_NOT_SUPPORTED;
1223         }
1224         if (0 != __vc_cmd_check_privilege()) {
1225                 return VC_ERROR_PERMISSION_DENIED;
1226         }
1227
1228         if (NULL == vc_command || NULL == pid) {
1229                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1230                 return VC_ERROR_INVALID_PARAMETER;
1231         }
1232
1233         vc_cmd_s* cmd = NULL;
1234         cmd = (vc_cmd_s*)vc_command;
1235
1236         *pid = cmd->pid;
1237
1238         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get pid][%p] pid(%d)", vc_command, *pid);
1239
1240         return 0;
1241 }
1242
1243 int vc_cmd_set_domain(vc_cmd_h vc_command, int domain)
1244 {
1245         if (0 != __vc_cmd_get_feature_enabled()) {
1246                 return VC_ERROR_NOT_SUPPORTED;
1247         }
1248         if (0 != __vc_cmd_check_privilege()) {
1249                 return VC_ERROR_PERMISSION_DENIED;
1250         }
1251
1252         if (NULL == vc_command) {
1253                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1254                 return VC_ERROR_INVALID_PARAMETER;
1255         }
1256
1257         vc_cmd_s* cmd = NULL;
1258         cmd = (vc_cmd_s*)vc_command;
1259
1260         cmd->domain = domain;
1261
1262         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set domain] domain : %d", domain);
1263
1264         return 0;
1265 }
1266
1267 int vc_cmd_get_domain(vc_cmd_h vc_command, int* domain)
1268 {
1269         if (0 != __vc_cmd_get_feature_enabled()) {
1270                 return VC_ERROR_NOT_SUPPORTED;
1271         }
1272         if (0 != __vc_cmd_check_privilege()) {
1273                 return VC_ERROR_PERMISSION_DENIED;
1274         }
1275
1276         if (NULL == vc_command || NULL == domain) {
1277                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1278                 return VC_ERROR_INVALID_PARAMETER;
1279         }
1280
1281         vc_cmd_s* cmd = NULL;
1282         cmd = (vc_cmd_s*)vc_command;
1283
1284         *domain = cmd->domain;
1285
1286         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get domain] domain : %d", *domain);
1287
1288         return 0;
1289 }
1290
1291 /**
1292 * @brief Sets key value of command.
1293 *
1294 * @param[in] vc_command Command handle
1295 * @param[in] key key value
1296 * @param[in] modifier modifier value
1297 *
1298 * @return 0 on success, otherwise a negative error value
1299 * @retval #VC_ERROR_NONE Successful
1300 * @retval #VC_ERROR_INVALID_PARAMETER Invalid parameter
1301 *
1302 * @see vc_cmd_get_result_key()
1303 */
1304 int vc_cmd_set_result_key(vc_cmd_h vc_command, int key, int modifier)
1305 {
1306         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Set result key");
1307
1308         if (NULL == vc_command) {
1309                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1310                 return VC_ERROR_INVALID_PARAMETER;
1311         }
1312
1313         vc_cmd_s* cmd = NULL;
1314         cmd = (vc_cmd_s*)vc_command;
1315
1316         SLOG(LOG_DEBUG, TAG_VCCMD, "key : %d, modifier : %d", key, modifier);
1317
1318         cmd->key = key;
1319         cmd->modifier = modifier;
1320
1321         SLOG(LOG_DEBUG, TAG_VCCMD, "=====");
1322         SLOG(LOG_DEBUG, TAG_VCCMD, " ");
1323
1324         return 0;
1325 }
1326
1327 /**
1328 * @brief Gets key value of command.
1329 *
1330 * @param[in] vc_command Command handle
1331 * @param[out] key key value
1332 * @param[out] modifier modifier value
1333 *
1334 * @return 0 on success, otherwise a negative error value
1335 * @retval #VC_ERROR_NONE Successful
1336 * @retval #VC_ERROR_INVALID_PARAMETER Invalid parameter
1337 *
1338 * @see vc_cmd_add_result_key()
1339 */
1340 int vc_cmd_get_result_key(vc_cmd_h vc_command, int* key, int* modifier)
1341 {
1342         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Get result key");
1343
1344         if (NULL == vc_command || NULL == key || NULL == modifier) {
1345                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1346                 return VC_ERROR_INVALID_PARAMETER;
1347         }
1348
1349         vc_cmd_s* cmd = NULL;
1350         cmd = (vc_cmd_s*)vc_command;
1351
1352         *key = cmd->key;
1353         *modifier = cmd->modifier;
1354
1355         SLOG(LOG_DEBUG, TAG_VCCMD, "=====");
1356         SLOG(LOG_DEBUG, TAG_VCCMD, " ");
1357
1358         return 0;
1359 }
1360
1361 int vc_cmd_print_list(vc_cmd_list_h vc_cmd_list)
1362 {
1363         if (NULL == vc_cmd_list) {
1364                 return -1;
1365         }
1366
1367         vc_cmd_list_s* list = NULL;
1368         list = (vc_cmd_list_s*)vc_cmd_list;
1369
1370         SLOG(LOG_DEBUG, TAG_VCCMD, "=== Command List ===");
1371         SLOG(LOG_DEBUG, TAG_VCCMD, "[List][%p]", list);
1372
1373         int count = g_slist_length(list->list);
1374
1375         int i;
1376         vc_cmd_s *cmd = NULL;
1377
1378         for (i = 0; i < count ; i++) {
1379                 cmd = g_slist_nth_data(list->list, i);
1380
1381                 if (NULL != cmd) {
1382                         SLOG(LOG_DEBUG, TAG_VCCMD, "  [%d][%p] PID(%d) ID(%d) Type(%d) Format(%d) Command(%s) Param(%s) Appid(%s) Invocation(%s) Fixed(%s)",
1383                          i, cmd, cmd->pid, cmd->index, cmd->type, cmd->format, cmd->command, cmd->parameter, cmd->appid, cmd->invocation_name, cmd->fixed);
1384                 }
1385         }
1386
1387         SLOG(LOG_DEBUG, TAG_VCCMD, "==================");
1388         SLOG(LOG_DEBUG, TAG_VCCMD, " ");
1389
1390         return 0;
1391 }
1392
1393 int vc_cmd_get_nlu_json(vc_cmd_h vc_cmd, char** json)
1394 {
1395         if (0 != __vc_cmd_get_feature_enabled()) {
1396                 return VC_ERROR_NOT_SUPPORTED;
1397         }
1398
1399         if (NULL == vc_cmd || NULL == json) {
1400                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] NULL parameter");
1401                 return VC_ERROR_INVALID_PARAMETER;
1402         }
1403
1404         vc_cmd_s* cmd = NULL;
1405         cmd = (vc_cmd_s*)vc_cmd;
1406
1407         if (VC_CMD_FORMAT_ACTION != cmd->format) {
1408                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Not Action format");
1409                 return VC_ERROR_INVALID_PARAMETER;
1410         }
1411
1412         if (0 != vc_info_parser_get_nlu_result(json)) {
1413                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get nlu result");
1414                 return VC_ERROR_OPERATION_FAILED;
1415         }
1416
1417         return 0;
1418 }
1419
1420 static void __vc_cmd_regex_deinit(int num_regex)
1421 {
1422         SLOG(LOG_DEBUG, TAG_VCCMD, "==== Start Deinitialize regex ====");
1423         int i;
1424
1425         for (i = 0; num_regex > i; i++) {
1426                 regfree(&reg[i]);
1427         }
1428
1429         SLOG(LOG_DEBUG, TAG_VCCMD, "====");
1430         SLOG(LOG_DEBUG, TAG_VCCMD, "");
1431 }
1432
1433 static int __vc_cmd_regex_init()
1434 {
1435         SLOG(LOG_DEBUG, TAG_VCCMD, "==== Initialize regular expression ====");
1436
1437         int cflags = REG_EXTENDED | REG_ICASE;
1438         int ret;
1439         char errStr[128];
1440         char *lang = NULL;
1441         int lang_type = 1;
1442
1443         vc_config_mgr_get_default_language(&lang);
1444         if (NULL == lang) {
1445                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get current language");
1446                 return VC_ERROR_OPERATION_FAILED;
1447         }
1448
1449         if (!strcmp("en_US", lang)) {
1450                 lang_type = 1;
1451         } else if (!strcmp("ko_KR", lang)) {
1452                 lang_type = 0;
1453         } else {
1454                 free(lang);
1455                 lang = NULL;
1456
1457                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Not supported language type");
1458                 return VC_ERROR_INVALID_LANGUAGE;
1459         }
1460
1461         free(lang);
1462         lang = NULL;
1463
1464         SLOG(LOG_DEBUG, TAG_VCCMD, "==== lang type > %d ====", lang_type);
1465
1466         re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1467
1468         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s" , TIME_ABS1_REGEX[lang_type]);
1469         ret = regcomp(&reg[0], TIME_ABS1_REGEX[lang_type], cflags);
1470         if (0 != ret) {
1471                 regerror(ret, &reg[0], errStr, sizeof(errStr));
1472
1473                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1474                 return VC_ERROR_OPERATION_FAILED;
1475         }
1476
1477         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", TIME_ABS2_REGEX[lang_type]);
1478         ret = regcomp(&reg[1], TIME_ABS2_REGEX[lang_type], cflags);
1479         if (0 != ret) {
1480                 regerror(ret, &reg[1], errStr, sizeof(errStr));
1481                 __vc_cmd_regex_deinit(1);
1482
1483                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1484                 return VC_ERROR_OPERATION_FAILED;
1485         }
1486
1487         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", TIME_ABS3_REGEX[lang_type]);
1488         ret = regcomp(&reg[2], TIME_ABS3_REGEX[lang_type], cflags);
1489         if (0 != ret) {
1490                 regerror(ret, &reg[2], errStr, sizeof(errStr));
1491                 __vc_cmd_regex_deinit(2);
1492
1493                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1494                 return VC_ERROR_OPERATION_FAILED;
1495         }
1496
1497         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", TIME_REL1_REGEX[lang_type]);
1498         ret = regcomp(&reg[3], TIME_REL1_REGEX[lang_type], cflags);
1499         if (0 != ret) {
1500                 regerror(ret, &reg[3], errStr, sizeof(errStr));
1501                 __vc_cmd_regex_deinit(3);
1502
1503                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1504                 return VC_ERROR_OPERATION_FAILED;
1505         }
1506
1507         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", TIME_REL2_REGEX[lang_type]);
1508         ret = regcomp(&reg[4], TIME_REL2_REGEX[lang_type], cflags);
1509         if (0 != ret) {
1510                 regerror(ret, &reg[4], errStr, sizeof(errStr));
1511                 __vc_cmd_regex_deinit(4);
1512
1513                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1514                 return VC_ERROR_OPERATION_FAILED;
1515         }
1516
1517         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", TIME_REL3_REGEX[lang_type]);
1518         ret = regcomp(&reg[5], TIME_REL3_REGEX[lang_type], cflags);
1519         if (0 != ret) {
1520                 regerror(ret, &reg[5], errStr, sizeof(errStr));
1521                 __vc_cmd_regex_deinit(5);
1522
1523                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1524                 return VC_ERROR_OPERATION_FAILED;
1525         }
1526
1527         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", TIME_PHR_REGEX[lang_type]);
1528         ret = regcomp(&reg[6], TIME_PHR_REGEX[lang_type], cflags);
1529         if (0 != ret) {
1530                 regerror(ret, &reg[6], errStr, sizeof(errStr));
1531                 __vc_cmd_regex_deinit(6);
1532
1533                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1534                 return VC_ERROR_OPERATION_FAILED;
1535         }
1536
1537         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", DATE_ABS1_REGEX[lang_type]);
1538         ret = regcomp(&reg[7], DATE_ABS1_REGEX[lang_type], cflags);
1539         if (0 != ret) {
1540                 regerror(ret, &reg[7], errStr, sizeof(errStr));
1541                 __vc_cmd_regex_deinit(7);
1542
1543                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1544                 return VC_ERROR_OPERATION_FAILED;
1545         }
1546
1547         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", DATE_ABS2_REGEX[lang_type]);
1548         ret = regcomp(&reg[8], DATE_ABS2_REGEX[lang_type], cflags);
1549         if (0 != ret) {
1550                 regerror(ret, &reg[8], errStr, sizeof(errStr));
1551                 __vc_cmd_regex_deinit(8);
1552
1553                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1554                 return VC_ERROR_OPERATION_FAILED;
1555         }
1556
1557         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", DATE_ABS3_REGEX[lang_type]);
1558         ret = regcomp(&reg[9], DATE_ABS3_REGEX[lang_type], cflags);
1559         if (0 != ret) {
1560                 regerror(ret, &reg[9], errStr, sizeof(errStr));
1561                 __vc_cmd_regex_deinit(9);
1562
1563                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1564                 return VC_ERROR_OPERATION_FAILED;
1565         }
1566
1567         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", DATE_PHR1_REGEX[lang_type]);
1568         ret = regcomp(&reg[10], DATE_PHR1_REGEX[lang_type], cflags);
1569         if (0 != ret) {
1570                 regerror(ret, &reg[10], errStr, sizeof(errStr));
1571                 __vc_cmd_regex_deinit(10);
1572
1573                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1574                 return VC_ERROR_OPERATION_FAILED;
1575         }
1576
1577         SLOG(LOG_DEBUG, TAG_VCCMD, "Regular expression > %s", DATE_PHR2_REGEX[lang_type]);
1578         ret = regcomp(&reg[11], DATE_PHR2_REGEX[lang_type], cflags);
1579         if (0 != ret) {
1580                 regerror(ret, &reg[11], errStr, sizeof(errStr));
1581                 __vc_cmd_regex_deinit(11);
1582
1583                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] regcomp() error > %s", errStr);
1584                 return VC_ERROR_OPERATION_FAILED;
1585         }
1586
1587         SLOG(LOG_DEBUG, TAG_VCCMD, "====");
1588         SLOG(LOG_DEBUG, TAG_VCCMD, "");
1589
1590         return VC_ERROR_NONE;
1591 }
1592
1593 static void __vc_cmd_add_year(struct tm *td, int year)
1594 {
1595         td->tm_year += year;
1596 }
1597
1598 static void __vc_cmd_add_mon(struct tm *td, int mon)
1599 {
1600         int year = 0;
1601
1602         mon = td->tm_mon + mon;
1603         year = mon / 12;
1604
1605         if (0 < year) {
1606                 __vc_cmd_add_year(td, year);
1607         }
1608
1609         td->tm_mon = mon % 12;
1610 }
1611
1612 static void __vc_cmd_add_mday(struct tm *td, int mday)
1613 {
1614         int max_day[12] = {31, 28, 31, 30, 31, 30, 31, 30, 30, 31, 30, 31};
1615         int year = td->tm_year + 1900;
1616
1617         int mon = 0;
1618
1619         if ((0 == year % 4 && 0 != year % 100) || 0 == year % 400) max_day[1] = 29;
1620
1621         mday = td->tm_mday + mday;
1622
1623         for (mon = td->tm_mon; mday >= max_day[mon % 12]; mon++) {
1624                 mday -= max_day[mon % 12];
1625
1626                 if (11 == mon % 12) {
1627                         year++;
1628
1629                         if ((0 == year % 4 && 0 != year % 100) || 0 == year % 400) {
1630                                 max_day[1] = 29;
1631                         } else {
1632                                 max_day[1] = 28;
1633                         }
1634                 }
1635         }
1636
1637         mon = mon - td->tm_mon;
1638
1639         if (0 < mon) {
1640                 __vc_cmd_add_mon(td, mon);
1641         }
1642
1643         td->tm_mday = mday;
1644 }
1645
1646 static void __vc_cmd_add_hour(struct tm *td, int hour)
1647 {
1648         int day = 0;
1649
1650         hour = td->tm_hour + hour;
1651         day = hour / 24;
1652
1653         if (0 < day) {
1654                 __vc_cmd_add_mday(td, day);
1655         }
1656
1657         td->tm_hour = hour % 24;
1658 }
1659
1660 static void __vc_cmd_add_min(struct tm *td, int min)
1661 {
1662         int hour = 0;
1663
1664         min = td->tm_min + min;
1665         hour = min / 60;
1666
1667         if (0 < hour) {
1668                 __vc_cmd_add_hour(td, hour);
1669         }
1670
1671         td->tm_min = min % 60;
1672 }
1673
1674 static void __copy_struct_tm(struct tm *des, struct tm *src)
1675 {
1676         SLOG(LOG_DEBUG, TAG_VCCMD, "==== Start to copy struct tm ====");
1677
1678         des->tm_sec     = src->tm_sec;
1679         des->tm_min     = src->tm_min;
1680         des->tm_hour    = src->tm_hour;
1681         des->tm_mday    = src->tm_mday;
1682         des->tm_mon     = src->tm_mon;
1683         des->tm_year    = src->tm_year;
1684         des->tm_wday    = src->tm_wday;
1685         des->tm_yday    = src->tm_yday;
1686         des->tm_isdst   = src->tm_isdst;
1687
1688         des->tm_gmtoff  = src->tm_gmtoff;
1689         des->tm_zone    = src->tm_zone;
1690 }
1691
1692 static void __update_data_sidx(int idx)
1693 {
1694         if (0 > g_data_sidx || idx < g_data_sidx) g_data_sidx = idx;
1695 }
1696
1697 static void __update_data_eidx(int idx)
1698 {
1699         if (0 > g_data_eidx || idx > g_data_eidx) g_data_eidx = idx;
1700 }
1701
1702 static int __vc_cmd_tphrase_check(const char *str, struct tm *td, int *exist)
1703 {
1704         regmatch_t pmatch[3];
1705         int ret;
1706         int len;
1707         int idx;
1708
1709         *exist = 0;
1710         ret = regexec(&reg[6], str, 3, pmatch, 0);
1711         if (0 == ret) {
1712                 idx = 1;
1713                 len = pmatch[idx].rm_eo - pmatch[idx].rm_so;
1714                 if (0 < len) {
1715                         if (12 < td->tm_hour) {
1716                                 __vc_cmd_add_mday(td, 1);
1717                         }
1718
1719                         td->tm_hour = 12;
1720                 } else {
1721                         idx = 2;
1722                         len = pmatch[idx].rm_eo - pmatch[idx].rm_so;
1723
1724                         __vc_cmd_add_mday(td, 1);
1725                 }
1726
1727                 td->tm_min = 0;
1728                 td->tm_sec = 0;
1729                 SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", len, str + pmatch[0].rm_so);
1730
1731                 __update_data_sidx(pmatch[0].rm_so);
1732                 __update_data_eidx(pmatch[0].rm_eo);
1733
1734                 *exist = 1;
1735                 return VC_ERROR_NONE;
1736         }
1737
1738         SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] There is no matched string");
1739         return VC_ERROR_NONE;
1740 }
1741
1742 static int __vc_cmd_trelative_check(const char *str, struct tm *td, int *exist)
1743 {
1744         regmatch_t pmatch[2];
1745         int ret;
1746         int len;
1747         int sidx = -1;
1748         int eidx = -1;
1749         int hour = -1;
1750         int min = -1;
1751
1752         char *tempstr = NULL;
1753
1754         *exist = 0;
1755         ret = regexec(&reg[3], str, 1, pmatch, 0);
1756         if (0 == ret) {
1757                 SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", pmatch[0].rm_eo - pmatch[0].rm_so, str+pmatch[0].rm_so);
1758                 hour = min = -1;
1759
1760                 sidx = pmatch[0].rm_so;
1761                 eidx = pmatch[0].rm_eo;
1762
1763                 ret = regexec(&reg[4], str, 2, pmatch, 0);
1764                 if (0 == ret) {
1765                         len = pmatch[1].rm_eo - pmatch[1].rm_so;
1766
1767                         if (0 > len) {
1768                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length");
1769                                 return VC_ERROR_OPERATION_FAILED;
1770                         }
1771                         tempstr = strndup(str + pmatch[1].rm_so, (size_t)len);
1772
1773                         if (NULL == tempstr) {
1774                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed");
1775                                 return VC_ERROR_OUT_OF_MEMORY;
1776                         }
1777
1778                         hour = atoi(tempstr);
1779
1780                         free(tempstr);
1781                         tempstr = NULL;
1782
1783                         SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", len, str + pmatch[0].rm_so);
1784
1785                         if (pmatch[0].rm_so < sidx) sidx = pmatch[0].rm_so;
1786                         if (pmatch[0].rm_eo > eidx) eidx = pmatch[0].rm_eo;
1787                 }
1788
1789                 ret = regexec(&reg[5], str, 2, pmatch, 0);
1790                 if (0 == ret) {
1791                         len = pmatch[1].rm_eo - pmatch[1].rm_so;
1792
1793                         if (0 > len) {
1794                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length");
1795                                 return VC_ERROR_OPERATION_FAILED;
1796                         }
1797                         tempstr = strndup(str + pmatch[1].rm_so, (size_t)len);
1798
1799                         if (NULL == tempstr) {
1800                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed");
1801                                 return VC_ERROR_OUT_OF_MEMORY;
1802                         }
1803
1804                         min = atoi(tempstr);
1805
1806                         free(tempstr);
1807                         tempstr = NULL;
1808
1809                         SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", len, str + pmatch[0].rm_so);
1810
1811                         if (pmatch[0].rm_so < sidx) sidx = pmatch[0].rm_so;
1812                         if (pmatch[0].rm_eo > eidx) eidx = pmatch[0].rm_eo;
1813                 }
1814
1815                 if (hour < 0 && min < 0) {
1816                         SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] There is no matched string");
1817                         return VC_ERROR_NONE;
1818                 }
1819
1820                 hour = 0 > hour ? 0 : hour;
1821                 min = 0 > min ? 0 : min;
1822
1823                 min = min + (hour * 60);
1824
1825                 __vc_cmd_add_min(td, min);
1826                 td->tm_sec = 0;
1827
1828                 __update_data_sidx(sidx);
1829                 __update_data_eidx(eidx);
1830
1831                 *exist = 1;
1832                 return VC_ERROR_NONE;
1833         }
1834
1835         SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] There is no matched string");
1836         return VC_ERROR_NONE;
1837 }
1838
1839 static int __vc_cmd_tabsolute_check(const char *str, struct tm *td, int *exist)
1840 {
1841         regmatch_t pmatch[5];
1842         int ret;
1843         int len;
1844         int idx;
1845         int flag = -1;
1846         int hour = -1;
1847         int min = -1;
1848         int sidx = -1;
1849         int eidx = -1;
1850         char *tempstr = NULL;
1851
1852         *exist = 0;
1853         ret = regexec(&reg[0], str, 5, pmatch, 0);
1854         if (0 == ret) {
1855                 for (idx = 1; 5 > idx && 0 >= pmatch[idx].rm_eo - pmatch[idx].rm_so; idx++);
1856
1857                 flag = idx & 1;
1858
1859                 sidx = pmatch[0].rm_so;
1860                 eidx = pmatch[0].rm_eo;
1861         }
1862
1863         ret = regexec(&reg[1], str, 2, pmatch, 0);
1864         if (0 == ret) {
1865                 len = pmatch[1].rm_eo - pmatch[1].rm_so;
1866
1867                 if (0 > len) {
1868                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length");
1869                         return VC_ERROR_OPERATION_FAILED;
1870                 }
1871                 tempstr = strndup(str + pmatch[1].rm_so, (size_t)len);
1872
1873                 if (NULL == tempstr) {
1874                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed");
1875                         return VC_ERROR_OUT_OF_MEMORY;
1876                 }
1877
1878                 hour = atoi(tempstr);
1879
1880                 if (0 <= flag) {
1881                         hour = hour + 12 * flag;
1882
1883                         if (12 == hour) hour = 0;
1884                         else if (24 == hour) hour = 12;
1885                 }
1886
1887                 if (0 > hour || 24 <= hour || (0 == flag && 12 < hour)) {
1888                         SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] Incomming sentence is weird");
1889                         return VC_ERROR_NONE;
1890                 }
1891
1892                 free(tempstr);
1893                 tempstr = NULL;
1894
1895                 SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", len, str + pmatch[0].rm_so);
1896
1897                 if (0 > sidx || pmatch[0].rm_so < sidx) sidx = pmatch[0].rm_so;
1898                 if (0 > eidx || pmatch[0].rm_eo > eidx) eidx = pmatch[0].rm_eo;
1899         } else if (0 < flag) {
1900                 SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] Incomming sentence is weird");
1901                 return VC_ERROR_NONE;
1902         }
1903
1904         ret = regexec(&reg[2], str, 2, pmatch, 0);
1905         if (0 == ret) {
1906                 idx = 1;
1907                 len = pmatch[idx].rm_eo - pmatch[idx].rm_so;
1908                 if (0 < len) {
1909                         tempstr = strndup(str + pmatch[idx].rm_so, (size_t)len);
1910
1911                         if (NULL == tempstr) {
1912                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed");
1913                                 return VC_ERROR_OUT_OF_MEMORY;
1914                         }
1915
1916                         min = atoi(tempstr);
1917
1918                         if (0 > min || 60 <= min) {
1919                                 SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] Incomming sentence is weird");
1920                                 return VC_ERROR_NONE;
1921                         }
1922
1923                         td->tm_sec = 0;
1924
1925                         free(tempstr);
1926                         tempstr = NULL;
1927                 } else {
1928                         idx = 0;
1929                         min = 30;
1930                 }
1931
1932                 SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", pmatch[0].rm_eo - pmatch[0].rm_so, str + pmatch[0].rm_so);
1933                 if (0 > sidx || pmatch[0].rm_so < sidx) sidx = pmatch[0].rm_so;
1934                 if (0 > eidx || pmatch[0].rm_eo > eidx) eidx = pmatch[0].rm_eo;
1935         }
1936
1937         if (0 > hour && 0 > min) {
1938                 SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] There is no matched string");
1939                 return VC_ERROR_NONE;
1940         }
1941
1942         if (0 <= min && 0 <= hour) {
1943                 if (hour < td->tm_hour || (hour == td->tm_hour && min <= td->tm_min)) __vc_cmd_add_mday(td, 1);
1944
1945                 td->tm_hour = hour;
1946                 td->tm_min = min;
1947         } else if (0 <= min) {
1948                 char *lang = NULL;
1949                 vc_config_mgr_get_default_language(&lang);
1950                 if (NULL == lang) {
1951                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get current language");
1952                         return VC_ERROR_OPERATION_FAILED;
1953                 }
1954
1955                 if (!strcmp("en_US", lang)) {
1956                         SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] Incomming sentence is weird");
1957                         free(lang);
1958                         lang = NULL;
1959                         return VC_ERROR_NONE;
1960                 }
1961                 if (min <= td->tm_min) __vc_cmd_add_hour(td, 1);
1962
1963                 td->tm_min = min;
1964
1965                 free(lang);
1966                 lang = NULL;
1967         } else {
1968                 if (hour <= td->tm_hour) __vc_cmd_add_mday(td, 1);
1969
1970                 td->tm_hour = hour;
1971                 td->tm_min = 0;
1972         }
1973
1974         td->tm_sec = 0;
1975
1976         __update_data_sidx(sidx);
1977         __update_data_eidx(eidx);
1978
1979         *exist = 1;
1980         return VC_ERROR_NONE;
1981 }
1982
1983 static int __vc_cmd_dphrase_check(const char *str, struct tm *td, int *exist)
1984 {
1985         regmatch_t pmatch[10];
1986         int ret;
1987         int len;
1988         int idx;
1989
1990         *exist = 0;
1991         ret = regexec(&reg[10], str, 5, pmatch, 0);
1992         if (0 == ret) {
1993                 for (idx = 1; 5 > idx && 0 >= pmatch[idx].rm_eo - pmatch[idx].rm_so; idx++);
1994
1995                 len = pmatch[idx].rm_eo - pmatch[idx].rm_so;
1996
1997                 td->tm_year = td_now.tm_year;
1998                 td->tm_mon = td_now.tm_mon;
1999                 td->tm_mday = td_now.tm_mday;
2000
2001                 __vc_cmd_add_mday(td, idx - 1);
2002
2003                 SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", len, str + pmatch[0].rm_so);
2004
2005                 __update_data_sidx(pmatch[0].rm_so);
2006                 __update_data_eidx(pmatch[0].rm_eo);
2007
2008                 *exist = 1;
2009                 return VC_ERROR_NONE;
2010         }
2011
2012         ret = regexec(&reg[11], str, 10, pmatch, 0);
2013         if (0 == ret) {
2014                 for (idx = 1; 10 > idx; idx++) {
2015                         len = pmatch[idx].rm_eo - pmatch[idx].rm_so;
2016
2017                         if (0 < len) break;
2018                 }
2019
2020                 td->tm_year = td_now.tm_year;
2021                 td->tm_mon = td_now.tm_mon;
2022                 td->tm_mday = td_now.tm_mday;
2023
2024                 __vc_cmd_add_mday(td, idx + 1);
2025
2026                 SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", len, str + pmatch[0].rm_so);
2027
2028                 __update_data_sidx(pmatch[0].rm_so);
2029                 __update_data_eidx(pmatch[0].rm_eo);
2030
2031                 *exist = 1;
2032                 return VC_ERROR_NONE;
2033         }
2034
2035         SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] There is no matched string");
2036         return VC_ERROR_NONE;
2037 }
2038
2039 static int __vc_cmd_dabsolute_check(const char *str, struct tm *td, int *exist)
2040 {
2041         regmatch_t pmatch[13];
2042         int ret;
2043         int len;
2044         int idx;
2045         int sidx = -1;
2046         int eidx = -1;
2047         int y_flag = 0;
2048         int m_flag = 0;
2049         int year = 0;
2050         int mon = 0;
2051         int day = 0;
2052         char *tempstr = NULL;
2053
2054         *exist = 0;
2055         ret = regexec(&reg[9], str, 2, pmatch, 0);
2056         if (0 == ret) {
2057                 len = pmatch[1].rm_eo - pmatch[1].rm_so;
2058
2059                 if (0 > len) {
2060                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length");
2061                         return VC_ERROR_OPERATION_FAILED;
2062                 }
2063                 tempstr = strndup(str + pmatch[1].rm_so, (size_t)len);
2064
2065                 if (NULL == tempstr) {
2066                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed");
2067                         return VC_ERROR_OUT_OF_MEMORY;
2068                 }
2069
2070                 day = atoi(tempstr);
2071
2072                 free(tempstr);
2073                 tempstr = NULL;
2074
2075                 SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", len, str + pmatch[0].rm_so);
2076
2077                 sidx = pmatch[0].rm_so;
2078                 eidx = pmatch[0].rm_eo;
2079         } else {
2080                 SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] Incomming sentence is weird");
2081                 return VC_ERROR_NONE;
2082         }
2083
2084         ret = regexec(&reg[8], str, 13, pmatch, 0);
2085         if (0 == ret) {
2086                 for (idx = 1; 13 > idx; idx++) {
2087                         len = pmatch[idx].rm_eo - pmatch[idx].rm_so;
2088
2089                         if (0 < len) {
2090                                 mon = idx - 1;
2091                                 break;
2092                         }
2093                 }
2094
2095                 m_flag = 1;
2096
2097                 SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", len, str + pmatch[0].rm_so);
2098
2099                 if (0 > sidx || pmatch[0].rm_so < sidx) sidx = pmatch[0].rm_so;
2100                 if (0 > eidx || pmatch[0].rm_eo > eidx) eidx = pmatch[0].rm_eo;
2101         } else {
2102                 char *lang = NULL;
2103                 vc_config_mgr_get_default_language(&lang);
2104                 if (NULL == lang) {
2105                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get current language");
2106                         return VC_ERROR_OPERATION_FAILED;
2107                 }
2108
2109                 if (!strcmp("en_US", lang)) {
2110                         SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] Incomming sentence is weird");
2111                         free(lang);
2112                         lang = NULL;
2113                         return VC_ERROR_NONE;
2114                 }
2115
2116                 free(lang);
2117                 lang = NULL;
2118
2119                 mon = td->tm_mon;
2120         }
2121
2122         ret = regexec(&reg[7], str, 3, pmatch, 0);
2123         if (0 == ret) {
2124                 if (!m_flag) return -1;
2125
2126                 len = pmatch[2].rm_eo - pmatch[2].rm_so;
2127
2128                 if (0 > len) {
2129                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length");
2130                         return VC_ERROR_OPERATION_FAILED;
2131                 }
2132                 tempstr = strndup(str + pmatch[2].rm_so, (size_t)len);
2133
2134                 if (NULL == tempstr) {
2135                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed");
2136                         return VC_ERROR_OUT_OF_MEMORY;
2137                 }
2138
2139                 year = atoi(tempstr);
2140                 year = 1900 < year ? year - 1900 : year + 100;
2141
2142                 free(tempstr);
2143                 tempstr = NULL;
2144
2145                 y_flag = 1;
2146                 SLOG(LOG_DEBUG, TAG_VCCMD, "Matched string > %.*s", len, str + pmatch[0].rm_so);
2147
2148                 if (0 > sidx || pmatch[0].rm_so < sidx) sidx = pmatch[0].rm_so;
2149                 if (0 > eidx || pmatch[0].rm_eo > eidx) eidx = pmatch[0].rm_eo;
2150         } else {
2151                 year = td->tm_year;
2152         }
2153
2154         if (0 > g_time_flag) {
2155                 td->tm_hour = 0;
2156                 td->tm_min = 0;
2157                 td->tm_sec = 0;
2158         } else if (2 == g_time_flag) {
2159                 SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] Incomming sentence is weird");
2160                 return VC_ERROR_NONE;
2161         }
2162
2163         int max_day[12] = {31, 28, 31, 30, 31, 30, 31, 30, 30, 31, 30, 31};
2164         if ((0 == (year + 1900) % 4 && 0 != (year + 1900) % 100) || 0 == (year + 1900) % 400) max_day[1] = 29;
2165
2166         if (max_day[mon] < day || 1 > day) {
2167                 SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] Incomming sentence is weird");
2168                 return VC_ERROR_NONE;
2169         }
2170
2171         td->tm_year = year;
2172         td->tm_mon = mon;
2173         td->tm_mday = day;
2174
2175         if (!y_flag) {
2176                 if (!m_flag) {
2177                         if (day < td_now.tm_mday) __vc_cmd_add_mon(td, 1);
2178                 } else {
2179                         if (mon < td_now.tm_mon) __vc_cmd_add_year(td, 1);
2180                         else if (mon == td_now.tm_mon && day < td_now.tm_mday) __vc_cmd_add_year(td, 1);
2181                 }
2182         }
2183
2184         __update_data_sidx(sidx);
2185         __update_data_eidx(eidx);
2186
2187         *exist = 1;
2188         return VC_ERROR_NONE;
2189 }
2190
2191 static int __vc_cmd_time_check(const char *str, struct tm *td)
2192 {
2193         SLOG(LOG_DEBUG, TAG_VCCMD, "==== Check time value in string \"%s\"", str);
2194
2195         vc_error_e ret;
2196         int exist = 0;
2197
2198         ret = __vc_cmd_tphrase_check(str, td, &exist);
2199         if (1 == exist) {
2200                 g_time_flag = 1;
2201
2202                 SLOG(LOG_DEBUG, TAG_VCCMD, "==== Time value is exist");
2203                 return ret;
2204         } else if (VC_ERROR_NONE != ret) {
2205                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Error is occured > (%d)", ret);
2206                 return ret;
2207         }
2208
2209         ret = __vc_cmd_trelative_check(str, td, &exist);
2210         if (1 == exist) {
2211                 g_time_flag = 2;
2212
2213                 SLOG(LOG_DEBUG, TAG_VCCMD, "==== Time value is exist");
2214                 return ret;
2215         } else if (VC_ERROR_NONE != ret) {
2216                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Error is occured > (%d)", ret);
2217                 return ret;
2218         }
2219
2220         ret = __vc_cmd_tabsolute_check(str, td, &exist);
2221         if (1 == exist) {
2222                 g_time_flag = 3;
2223
2224                 SLOG(LOG_DEBUG, TAG_VCCMD, "==== Time value is exist");
2225                 return ret;
2226         } else if (VC_ERROR_NONE != ret) {
2227                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Error is occured > (%d)", ret);
2228                 return ret;
2229         }
2230
2231         SLOG(LOG_DEBUG, TAG_VCCMD, "==== There is no time value");
2232         return VC_ERROR_NONE;
2233 }
2234
2235 static int __vc_cmd_date_check(const char *str, struct tm *td)
2236 {
2237         SLOG(LOG_DEBUG, TAG_VCCMD, "==== Check date value in string \"%s\"", str);
2238
2239         vc_error_e ret;
2240         int exist = 0;
2241
2242         ret = __vc_cmd_dphrase_check(str, td, &exist);
2243         if (1 == exist) {
2244                 g_date_flag = 1;
2245
2246                 SLOG(LOG_DEBUG, TAG_VCCMD, "==== Date value is exist");
2247                 return ret;
2248         } else if (VC_ERROR_NONE != ret) {
2249                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Error is occured > (%d)", ret);
2250                 return ret;
2251         }
2252
2253         ret = __vc_cmd_dabsolute_check(str, td, &exist);
2254         if (1 == exist) {
2255                 g_date_flag = 1;
2256
2257                 SLOG(LOG_DEBUG, TAG_VCCMD, "==== Date value is exist");
2258                 return ret;
2259         } else if (VC_ERROR_NONE != ret) {
2260                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Error is occured > (%d)", ret);
2261                 return ret;
2262         }
2263
2264         SLOG(LOG_DEBUG, TAG_VCCMD, "==== There is no date value");
2265         return VC_ERROR_NONE;
2266 }
2267
2268 int vc_cmd_get_datetime(const char *text, time_t *result, char **remain)
2269 {
2270         SLOG(LOG_DEBUG, TAG_VCCMD, "==== Get timestamp data");
2271
2272         struct tm td;
2273         const char *day_name[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
2274         vc_error_e ret;
2275
2276         if (NULL == text || NULL == result || NULL == remain) {
2277                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter");
2278                 return VC_ERROR_INVALID_PARAMETER;
2279         }
2280
2281         *result = -1;
2282         ret = __vc_cmd_regex_init();
2283         if (VC_ERROR_NONE != ret) {
2284                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] initialize regex failed");
2285                 return ret;
2286         }
2287
2288         g_data_sidx = g_data_eidx = -1;
2289
2290         t_now = time(NULL);
2291         localtime_r(&t_now, &td_now);
2292         SLOG(LOG_DEBUG, TAG_VCCMD, "Current timestamp = %d", (int)t_now);
2293
2294         __copy_struct_tm(&td, &td_now);
2295         SLOG(LOG_DEBUG, TAG_VCCMD, "%d-%d-%d (%s),  %d:%d:%d",
2296                 td.tm_year + 1900, td.tm_mon + 1, td.tm_mday, day_name[td.tm_wday], td.tm_hour, td.tm_min, td.tm_sec);
2297
2298         g_time_flag = g_date_flag = -1;
2299
2300         ret = __vc_cmd_time_check(text, &td);
2301         if (VC_ERROR_NONE != ret) {
2302                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Error is occured in the check > (%d)", ret);
2303                 return ret;
2304         }
2305
2306         ret = __vc_cmd_date_check(text, &td);
2307         if (VC_ERROR_NONE != ret) {
2308                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Error is occured in the check > (%d)", ret);
2309                 return ret;
2310         }
2311
2312         __vc_cmd_regex_deinit(12);
2313
2314         if (g_time_flag > 0 || g_date_flag > 0) {
2315                 *result = mktime(&td);
2316
2317                 SLOG(LOG_DEBUG, TAG_VCCMD, "Timestamp in the text = %ld", *result);
2318                 SLOG(LOG_DEBUG, TAG_VCCMD, "%d-%d-%d (%s),  %d:%d:%d",
2319                         td.tm_year + 1900, td.tm_mon + 1, td.tm_mday, day_name[td.tm_wday], td.tm_hour, td.tm_min, td.tm_sec, td.tm_yday);
2320
2321                 *remain = (char *)calloc(sizeof(char), (strlen(text) + 1 - g_data_eidx + g_data_sidx));
2322
2323                 if (NULL == *remain) {
2324                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Out of memory error");
2325                         return VC_ERROR_OUT_OF_MEMORY;
2326                 }
2327
2328                 strncpy(*remain, text, g_data_sidx);
2329                 strncat(*remain, text + g_data_eidx, strlen(text) - g_data_eidx);
2330         } else {
2331                 SLOG(LOG_DEBUG, TAG_VCCMD, "[REGEX] There is no data in the text");
2332         }
2333
2334         SLOG(LOG_DEBUG, TAG_VCCMD, "====");
2335         SLOG(LOG_DEBUG, TAG_VCCMD, "");
2336
2337         return VC_ERROR_NONE;
2338 }