3f984d72cc6834435b7aaac8d3fb525988ffd4a4
[apps/livebox/data-provider-master.git] / util_liveinfo / src / liveinfo.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 #include <stdio.h>
18 #include <unistd.h>
19 #include <getopt.h>
20 #include <errno.h>
21 #include <libgen.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <termios.h>
27
28 #include <X11/Xlib.h>
29 #include <X11/extensions/Xdamage.h>
30 #include <X11/extensions/Xfixes.h>
31
32 #include <glib.h>
33 #include <glib-object.h>
34
35 #include <packet.h>
36 #include <com-core_packet.h>
37 #include <com-core.h>
38
39 #include <livebox-service.h>
40
41 #include <Ecore.h>
42
43 #include "liveinfo.h"
44 #include "node.h"
45
46 #define PROMPT "liveinfo "
47
48 struct package {
49         int primary;
50         char *pkgid;
51
52         int pid;
53         char *slavename;
54         char *abi;
55         int refcnt;
56         int fault_count;
57         int inst_count;
58 };
59
60 struct instance {
61         char *id;
62         char *cluster;
63         char *category;
64         double period;
65         char *state;
66         int width;
67         int height;
68 };
69
70 struct slave {
71         int pid;
72         char *pkgname;
73         char *abi;
74         int secured;
75         int refcnt;
76         int fault_count;
77         char *state;
78         int loaded_inst;
79         int loaded_pkg;
80         double ttl;
81 };
82
83 enum command {
84         NOP,
85         PKG_LIST,
86         INST_LIST,
87         SLAVE_LIST,     
88         INST_CTRL,
89         SLAVE_CTRL,
90         MASTER_CTRL,
91 };
92
93 static struct info {
94         int fifo_handle;
95         int fd;
96         Ecore_Fd_Handler *fd_handler;
97         Ecore_Fd_Handler *in_handler;
98
99         struct node *rootdir;
100         struct node *curdir;
101         struct node *targetdir;
102
103         enum command cmd;
104
105         int input_fd;
106         int verbose;
107
108         int age;
109
110         char *history[1024];
111         int history_top;
112         int history_idx;
113
114         struct node *quick_search_node;
115         int quick_idx;
116 } s_info = {
117         .fifo_handle = -EINVAL,
118         .fd = -EINVAL,
119         .fd_handler = NULL,
120         .in_handler = NULL,
121         .rootdir = NULL,
122         .curdir = NULL,
123         .targetdir = NULL,
124         .cmd = NOP,
125         .input_fd = STDIN_FILENO,
126         .verbose = 0,
127         .age = 0,
128         .history = { 0, },
129         .history_top = 0,
130         .history_idx = 0,
131         .quick_search_node = NULL,
132         .quick_idx = 0,
133 };
134
135 char *optarg;
136 int errno;
137 int optind;
138 int optopt;
139 int opterr;
140
141 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler);
142
143 static Eina_Bool process_line_cb(void *data)
144 {
145         input_cb(NULL, NULL);
146         return ECORE_CALLBACK_CANCEL;
147 }
148
149 static inline void prompt(const char *cmdline)
150 {
151         char *path;
152
153         if (s_info.input_fd != STDIN_FILENO) {
154                 /* To prevent recursive call, add function to the main loop (idler) */
155                 ecore_idler_add(process_line_cb, NULL);
156                 return;
157         }
158
159         path = node_to_abspath(s_info.curdir);
160         printf(PROMPT"%s # %s", path, cmdline ? cmdline : "");
161         free(path);
162 }
163
164 static void provider_del_cb(struct node *node)
165 {
166         struct slave *info;
167
168         info = node_data(node);
169         if (!info)
170                 return;
171
172         free(info->pkgname);
173         free(info->abi);
174         free(info->state);
175         free(info);
176 }
177
178 static void package_del_cb(struct node *node)
179 {
180         struct package *info;
181
182         info = node_data(node);
183         if (!info)
184                 return;
185
186         free(info->pkgid);
187         free(info->slavename);
188         free(info->abi);
189         free(info);
190 }
191
192 static void inst_del_cb(struct node *node)
193 {
194         struct instance *info;
195
196         info = node_data(node);
197         if (!info)
198                 return;
199
200         free(info->id);
201         free(info->cluster);
202         free(info->category);
203         free(info->state);
204         free(info);
205 }
206
207 static inline void ls(void)
208 {
209         struct node *node;
210         int cnt = 0;
211         int is_package;
212         int is_provider;
213         int is_instance;
214         struct node *next_node;
215
216         if (!(node_mode(s_info.targetdir) & NODE_READ)) {
217                 printf("Access denied\n");
218                 return;
219         }
220
221         is_package = node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), "package");
222         is_provider = !is_package && node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), "provider");
223         is_instance = !is_package && !is_provider && node_parent(s_info.targetdir) && node_name(node_parent(s_info.targetdir)) && !strcmp(node_name(node_parent(s_info.targetdir)), "package");
224
225         node = node_child(s_info.targetdir);
226         while (node) {
227                 if (is_package) {
228                         struct package *info;
229
230                         next_node = node_next_sibling(node);
231                         if (node_age(node) != s_info.age) {
232                                 node_delete(node, package_del_cb);
233                                 node = next_node;
234                                 continue;
235                         }
236
237                         info = node_data(node);
238                         printf(" %3d %20s %5s ", info->inst_count, info->slavename ? info->slavename : "(none)", info->abi ? info->abi : "?");
239                 } else if (is_provider) {
240                         struct slave *info;
241
242                         next_node = node_next_sibling(node);
243                         if (node_age(node) != s_info.age) {
244                                 node_delete(node, provider_del_cb);
245                                 node = next_node;
246                                 continue;
247                         }
248
249                         info = node_data(node);
250                         printf("%6d %3d %5s %5.2f ", info->pid, info->loaded_inst, info->abi ? info->abi : "?", info->ttl);
251                 } else if (is_instance) {
252                         struct instance *info;
253                         struct stat stat;
254                         char buf[4096];
255
256                         next_node = node_next_sibling(node);
257
258                         if (node_age(node) != s_info.age) {
259                                 node_delete(node, inst_del_cb);
260                                 node = next_node;
261                                 continue;
262                         }
263
264                         info = node_data(node);
265
266                         printf(" %5.2f %6s %10s %10s %4dx%-4d ", info->period, info->state, info->cluster, info->category, info->width, info->height);
267                         snprintf(buf, sizeof(buf), "/opt/usr/share/live_magazine/reader/%s", node_name(node));
268                         if (lstat(buf, &stat) < 0)
269                                 printf("%3d ERR ", errno);
270                         else
271                                 printf("%2.2lf KB ", (double)stat.st_size / 1024.0f);
272                 }
273
274                 if (node_type(node) == NODE_DIR)
275                         printf("%s/", node_name(node));
276                 else if (node_type(node) == NODE_FILE)
277                         printf("%s", node_name(node));
278
279                 printf("\n");
280                 node = node_next_sibling(node);
281                 cnt++;
282         }
283
284         printf("Total: %d\n", cnt);
285 }
286
287 static void send_slave_list(void)
288 {
289         struct packet *packet;
290
291         if (s_info.cmd != NOP) {
292                 printf("Previous command is not finished\n");
293                 return;
294         }
295
296         packet = packet_create_noack("slave_list", "d", 0.0f);
297         if (!packet) {
298                 printf("Failed to create a packet\n");
299                 return;
300         }
301
302         com_core_packet_send_only(s_info.fd, packet);
303         packet_destroy(packet);
304         s_info.cmd = SLAVE_LIST;
305         s_info.age++;
306 }
307
308 /*!
309  * var = debug, slave_max_load
310  * cmd = set / get
311  */
312 static void send_command(const char *cmd, const char *var, const char *val)
313 {
314         struct packet *packet;
315
316         if (s_info.cmd != NOP) {
317                 printf("Previous command is not finished\n");
318                 return;
319         }
320
321         packet = packet_create_noack("master_ctrl", "sss", cmd, var, val);
322         com_core_packet_send_only(s_info.fd, packet);
323         packet_destroy(packet);
324         s_info.cmd = MASTER_CTRL;
325         s_info.age++;
326 }
327
328 static int pkglist_cb(const char *appid, const char *lbid, int is_prime, void *data)
329 {
330         struct node *parent = data;
331         struct node *node;
332         struct package *info;
333
334         node = node_find(parent, lbid);
335         if (node) {
336                 info = node_data(node);
337                 if (!info) {
338                         printf("Invalid node\n");
339                         return -EINVAL;
340                 }
341
342                 free(info->pkgid);
343                 info->pkgid = strdup(appid);
344                 if (!info->pkgid) {
345                         printf("Error: %s\n", strerror(errno));
346                         return -ENOMEM;
347                 }
348
349                 node_set_age(node, s_info.age);
350                 return 0;
351         }
352
353         info = calloc(1, sizeof(*info));
354         if (!info) {
355                 printf("Error: %s\n", strerror(errno));
356                 return -ENOMEM;
357         }
358
359         info->pkgid = strdup(appid);
360         if (!info->pkgid) {
361                 printf("Error: %s\n", strerror(errno));
362                 free(info);
363                 return -ENOMEM;
364         }
365
366         info->primary = is_prime;
367
368         node = node_create(parent, lbid, NODE_DIR);
369         if (!node) {
370                 free(info->pkgid);
371                 free(info);
372                 return -ENOMEM;
373         }
374
375         node_set_mode(node, NODE_READ | NODE_EXEC);
376         node_set_data(node, info);
377         node_set_age(node, s_info.age);
378         return 0;
379 }
380
381 static void send_pkg_list(void)
382 {
383         struct packet *packet;
384
385         if (s_info.cmd != NOP) {
386                 printf("Previous command is not finished\n");
387                 return;
388         }
389
390         packet = packet_create_noack("pkg_list", "d", 0.0f);
391         if (!packet) {
392                 printf("Failed to create a packet\n");
393                 return;
394         }
395
396         com_core_packet_send_only(s_info.fd, packet);
397         packet_destroy(packet);
398         s_info.cmd = PKG_LIST;
399         s_info.age++;
400
401         livebox_service_get_pkglist(pkglist_cb, s_info.targetdir);
402 }
403
404 static void send_inst_delete(void)
405 {
406         struct packet *packet;
407         struct node *parent;
408         const char *name;
409         struct instance *inst;
410
411         if (s_info.cmd != NOP) {
412                 printf("Previous command is not finished\n");
413                 return;
414         }
415
416         parent = node_parent(s_info.targetdir);
417         if (!parent) {
418                 printf("Invalid argument\n");
419                 return;
420         }
421
422         if (!node_parent(parent)) {
423                 printf("Invalid argument\n");
424                 return;
425         }
426
427         name = node_name(node_parent(parent));
428         if (!name || strcmp(name, "package")) {
429                 printf("Invalid argument\n");
430                 return;
431         }
432
433         inst = node_data(s_info.targetdir);
434         name = node_name(parent);
435
436         packet = packet_create_noack("pkg_ctrl", "sss", "rminst", name, inst->id);
437         com_core_packet_send_only(s_info.fd, packet);
438         packet_destroy(packet);
439         s_info.cmd = INST_CTRL;
440         s_info.age++;
441 }
442
443 static void send_inst_list(const char *pkgname)
444 {
445         struct packet *packet;
446
447         if (s_info.cmd != NOP) {
448                 printf("Previous command is not finished\n");
449                 return;
450         }
451
452         packet = packet_create_noack("inst_list", "s", pkgname);
453         if (!packet) {
454                 printf("Failed to create a packet\n");
455                 return;
456         }
457
458         com_core_packet_send_only(s_info.fd, packet);
459         packet_destroy(packet);
460         s_info.cmd = INST_LIST;
461         s_info.age++;
462 }
463
464 static inline void help(void)
465 {
466         printf("liveinfo - Livebox utility\n");
467         printf("------------------------------ [Option] ------------------------------\n");
468         printf("-b Batch mode\n");
469         printf("------------------------------ [Command list] ------------------------------\n");
470         printf("\e[32mcd [PATH] - Change directory\e[0m\n");
471         printf("\e[32mls [ | PATH] - List up content as a file\e[0m\n");
472         printf("\e[32mrm [PKG_ID|INST_ID] - Delete package or instance\e[0m\n");
473         printf("\e[32mstat [path] - Display the information of given path\e[0m\n");
474         printf("\e[32mset [debug] [on|off] Set the control variable of master provider\e[0m\n");
475         printf("\e[32mx damage Pix x y w h - Create damage event for given pixmap\e[0m\n");
476         printf("\e[32mx move Pix x y - Move the window\e[0m\n");
477         printf("\e[32mx resize Pix w h - Resize the window\e[0m\n");
478         printf("\e[32mx map Pix - Show the window\e[0m\n");
479         printf("\e[32mx unmap Pix - Hide the window\e[0m\n");
480         printf("\e[32msh [command] Execute shell command, [command] should be abspath\e[0m\n");
481         printf("\e[32mexit - \e[0m\n");
482         printf("\e[32mquit - \e[0m\n");
483         printf("----------------------------------------------------------------------------\n");
484 }
485
486 static inline void init_directory(void)
487 {
488         struct node *node;
489         s_info.rootdir = node_create(NULL, NULL, NODE_DIR);
490         if (!s_info.rootdir)
491                 return;
492         node_set_mode(s_info.rootdir, NODE_READ | NODE_EXEC);
493
494         node = node_create(s_info.rootdir, "provider", NODE_DIR);
495         if (!node) {
496                 node_destroy(s_info.rootdir);
497                 s_info.rootdir = NULL;
498                 return;
499         }
500         node_set_mode(node, NODE_READ | NODE_EXEC);
501
502         node = node_create(s_info.rootdir, "package", NODE_DIR);
503         if (!node) {
504                 node_destroy(node_child(s_info.rootdir));
505                 node_destroy(s_info.rootdir);
506                 s_info.rootdir = NULL;
507                 return;
508         }
509         node_set_mode(node, NODE_READ | NODE_EXEC);
510
511         s_info.curdir = s_info.rootdir;
512         return;
513 }
514
515 static inline void fini_directory(void)
516 {
517 }
518
519 static inline struct node *update_target_dir(const char *cmd)
520 {
521         struct node *node;
522
523         node = (*cmd == '/') ? s_info.rootdir : s_info.curdir;
524         node = node_find(node, cmd);
525
526         return node;
527 }
528
529 static int get_token(const char *src, char *out)
530 {
531         int len = 0;
532         while (*src && *src == ' ') src++;
533
534         if (!*src)
535                 return 0;
536
537         while (*src && *src != ' ') {
538                 *out++ = *src++;
539                 len++;
540         }
541
542         *out = '\0';
543         return len;
544 }
545
546 static inline int do_stat(const char *cmd)
547 {
548         int i;
549         struct node *node;
550         struct node *parent;
551         char *tmp;
552         enum stat_type {
553                 PKG_INSTANCE = 0x01,
554                 PKG,
555                 PROVIDER_INSTANCE,
556                 PROVIDER,
557                 ROOT,
558         } type;
559
560         cmd += 5;
561         while (*cmd && *cmd == ' ') cmd++;
562
563         if (!*cmd){
564                 printf("Invalid argument\n");
565                 return -EINVAL;
566         }
567
568         node = node_find(*cmd == '/' ? s_info.rootdir : s_info.curdir, cmd);
569         if (!node) {
570                 printf("Invalid path\n");
571                 return -EINVAL;
572         }
573
574         i = 0;
575         type = ROOT;
576         parent = node_parent(node);
577         while (parent) {
578                 if (!node_name(parent)) {
579                         printf("%s has no info\n", node_name(node));
580                         return -EINVAL;
581                 } else if (!strcmp(node_name(parent), "package")) {
582                         type = (i == 0) ? PKG : PKG_INSTANCE;
583                         break;
584                 } else if (!strcmp(node_name(parent), "provider")){
585                         type = (i == 0) ? PROVIDER : PROVIDER_INSTANCE;
586                         break;
587                 }
588
589                 parent = node_parent(parent);
590                 i++;
591                 if (i > 1) {
592                         printf("%s is invalid path\n", node_name(node));
593                         return -EINVAL;
594                 }
595         }
596
597         switch (type){
598         case PKG:
599                 tmp = livebox_service_i18n_name(node_name(node), NULL);
600                 printf("Name: %s (", tmp);
601                 free(tmp);
602
603                 i = livebox_service_is_enabled(node_name(node));
604                 printf("%s)\n", i ? "enabled" : "disabled");
605
606                 tmp = livebox_service_i18n_icon(node_name(node), NULL);
607                 printf("Icon: %s\n", tmp);
608                 free(tmp);
609
610                 tmp = livebox_service_provider_name(node_name(node));
611                 printf("Provider: %s (content:", tmp);
612                 free(tmp);
613
614                 tmp = livebox_service_content(node_name(node));
615                 printf("%s)\n", tmp);
616                 free(tmp);
617
618                 tmp = livebox_service_lb_script_path(node_name(node));
619                 printf("LB Script: %s (", tmp);
620                 free(tmp);
621
622                 tmp = livebox_service_lb_script_group(node_name(node));
623                 printf("%s)\n", tmp);
624                 free(tmp);
625
626                 tmp = livebox_service_pd_script_path(node_name(node));
627                 printf("PD Script: %s (", tmp);
628                 free(tmp);
629
630                 tmp = livebox_service_pd_script_group(node_name(node));
631                 printf("%s)\n", tmp);
632                 free(tmp);
633
634                 i = livebox_service_mouse_event(node_name(node));
635                 printf("Mouse event: %s\n", i ? "enabled" : "disabled");
636
637                 i = livebox_service_touch_effect(node_name(node));
638                 printf("Touch effect: %s\n", i ? "enabled" : "disabled");
639                 break;
640         case PROVIDER:
641                 printf("Not supported yet\n");
642                 break;
643         case PKG_INSTANCE:
644                 printf("Not supported yet\n");
645                 break;
646         case PROVIDER_INSTANCE:
647                 printf("Not supported yet\n");
648                 break;
649         case ROOT:
650                 printf("Not supported yet\n");
651                 break;
652         default:
653                 printf("Invalid type\n");
654                 return -EFAULT;
655         }
656
657         return 0;
658 }
659
660 static inline int do_set(const char *cmd)
661 {
662         int i;
663         char variable[4096] = { '0', };
664
665         cmd += 4;
666         i = get_token(cmd, variable);
667
668         cmd += i;
669         while (*cmd && *cmd == ' ') cmd++;
670
671         if (!i || !*cmd) {
672                 printf("Invalid argument(%s): set [VAR] [VAL]\n", cmd);
673                 return -EINVAL;
674         }
675
676         send_command("set", variable, cmd);
677         return 0;
678 }
679
680 static inline int do_get(const char *cmd)
681 {
682         cmd += 4;
683
684         while (*cmd && *cmd == ' ') cmd++;
685         if (!*cmd) {
686                 printf("Invalid argument(%s): get [VAR]\n", cmd);
687                 return -EINVAL;
688         }
689
690         send_command("get", cmd, "");
691         return 0;
692 }
693
694 static inline int do_ls(const char *cmd)
695 {
696         const char *name;
697         struct node *parent;
698
699         cmd += 2;
700
701         while (*cmd && *cmd == ' ')
702                 cmd++;
703
704         s_info.targetdir = *cmd ? update_target_dir(cmd) : s_info.curdir;
705         if (!s_info.targetdir) {
706                 printf("%s is not exists\n", cmd);
707                 return -ENOENT;
708         }
709
710         name = node_name(s_info.targetdir);
711         if (name) {
712                 if (!strcmp(name, "package")) {
713                         if (s_info.cmd == NOP) {
714                                 send_pkg_list();
715                                 return 0;
716                         }
717
718                         printf("Waiting the server response\n");
719                         return -EBUSY;
720                 } else if (!strcmp(name, "provider")) {
721                         if (s_info.cmd == NOP) {
722                                 send_slave_list();
723                                 return 0;
724                         }
725
726                         printf("Waiting the server response\n");
727                         return -EBUSY;
728                 }
729         }
730
731         parent = node_parent(s_info.targetdir);
732         if (parent && node_name(parent)) {
733                 if (!strcmp(node_name(parent), "package")) {
734                         if (s_info.cmd != NOP) {
735                                 printf("Waiting the server response\n");
736                                 return -EBUSY;
737                         }
738
739                         send_inst_list(name);
740                         return 0;
741                 }
742         }
743
744         ls();
745         return -1;
746 }
747
748 static inline int do_cd(const char *cmd)
749 {
750         cmd += 2;
751
752         while (*cmd && *cmd == ' ')
753                  cmd++;
754
755         if (!*cmd)
756                 return -1;
757
758         if (s_info.cmd != NOP) {
759                 printf("Waiting the server response\n");
760                 return -EBUSY;
761         }
762
763         s_info.targetdir = update_target_dir(cmd);
764         if (!s_info.targetdir) {
765                 printf("%s is not exists\n", cmd);
766                 return -ENOENT;
767         }
768
769         if (node_type(s_info.targetdir) != NODE_DIR) {
770                 printf("Unable change directory to %s\n", cmd);
771                 return -EINVAL;
772         }
773
774         if (!(node_mode(s_info.targetdir) & NODE_EXEC)) {
775                 printf("Access denied %s\n", cmd);
776                 return -EACCES;
777         }
778
779         s_info.curdir = s_info.targetdir;
780         return -1;
781 }
782
783 static inline int do_rm(const char *cmd)
784 {
785         cmd += 2;
786         while (*cmd && *cmd == ' ') cmd++;
787         if (!*cmd)
788                 return -1;
789
790         if (s_info.cmd != NOP) {
791                 printf("Waiting the server response\n");
792                 return -EBUSY;
793         }
794
795         s_info.targetdir = update_target_dir(cmd);
796         if (!s_info.targetdir) {
797                 printf("%s is not exists\n", cmd);
798                 return -ENOENT;
799         }
800
801         if (!(node_mode(s_info.targetdir) & NODE_WRITE)) {
802                 printf("Access denied %s\n", cmd);
803                 return -EACCES;
804         }
805
806         send_inst_delete();
807         return 0;
808 }
809
810 #if !defined(WCOREDUMP)
811 #define WCOREDUMP(a)    0
812 #endif
813
814 static inline void do_sh(const char *cmd)
815 {
816         pid_t pid;
817
818         cmd += 3;
819
820         while (*cmd && *cmd == ' ') cmd++;
821         if (!*cmd)
822                 return;
823
824         pid = fork();
825         if (pid == 0) {
826                 char command[256];
827                 int idx;
828                 idx = 0;
829
830                 while (idx < sizeof(command) && *cmd && *cmd != ' ')
831                         command[idx++] = *cmd++;
832                 command[idx] = '\0';
833
834                 if (execl(command, cmd, NULL) < 0)
835                         printf("Failed to execute: %s\n", strerror(errno));
836
837                 exit(0);
838         } else if (pid < 0) {
839                 printf("Failed to create a new process: %s\n", strerror(errno));
840         } else {
841                 int status;
842                 if (waitpid(pid, &status, 0) < 0) {
843                         printf("error: %s\n", strerror(errno));
844                 } else {
845                         if (WIFEXITED(status)) {
846                                 printf("Exit: %d\n", WEXITSTATUS(status));
847                         } else if (WIFSIGNALED(status)) {
848                                 printf("Terminated by %d %s\n", WTERMSIG(status), WCOREDUMP(status) ? " - core generated" : "");
849                         } else if (WIFSTOPPED(status)) {
850                                 printf("Stopped by %d\n", WSTOPSIG(status));
851                         } else if (WIFCONTINUED(status)) {
852                                 printf("Child is resumed\n");
853                         }
854                 }
855         }
856 }
857
858 static inline void do_x(const char *cmd)
859 {
860         Display *disp;
861
862         cmd += 2;
863
864         while (*cmd && *cmd == ' ') cmd++;
865         if (!*cmd)
866                 return;
867
868         disp = XOpenDisplay(NULL);
869         if (!disp) {
870                 printf("Failed to connect to the X\n");
871                 return;
872         }
873
874         if (!strncasecmp(cmd, "damage ", 7)) {
875                 unsigned int winId;
876                 XRectangle rect;
877                 XserverRegion region;
878                 int x, y, w, h;
879
880                 cmd += 7;
881
882                 if (sscanf(cmd, "%u %d %d %d %d", &winId, &x, &y, &w, &h) != 5) {
883                         printf("Invalid argument\nx damage WINID_DEC X Y W H\n");
884                         return;
885                 }
886                 rect.x = x;
887                 rect.y = y;
888                 rect.width = w;
889                 rect.height = h;
890                 region = XFixesCreateRegion(disp, &rect, 1);
891                 XDamageAdd(disp, winId, region);
892                 XFixesDestroyRegion(disp, region);
893                 XFlush(disp);
894
895                 printf("Damage: %u %d %d %d %d\n", winId, x, y, w, h);
896         } else if (!strncasecmp(cmd, "resize ", 7)) {
897                 unsigned int winId;
898                 int w;
899                 int h;
900
901                 cmd += 7;
902
903                 if (sscanf(cmd, "%u %d %d", &winId, &w, &h) != 3) {
904                         printf("Invalid argument\nx resize WINID_DEC W H\n");
905                         return;
906                 }
907
908                 XResizeWindow(disp, winId, w, h);
909                 printf("Resize: %u %d %d\n", winId, w, h);
910         } else if (!strncasecmp(cmd, "move ", 5)) {
911                 unsigned int winId;
912                 int x;
913                 int y;
914
915                 cmd += 5;
916                 if (sscanf(cmd, "%u %d %d", &winId, &x, &y) != 3) {
917                         printf("Invalid argument\nx move WINID_DEC X Y\n");
918                         return;
919                 }
920
921                 XMoveWindow(disp, winId, x, y);
922                 printf("Move: %u %d %d\n", winId, x, y);
923         } else if (!strncasecmp(cmd, "map ", 4)) {
924                 unsigned int winId;
925                 cmd += 4;
926                 if (sscanf(cmd, "%u", &winId) != 1) {
927                         printf("Invalid argument\nx map WINID_DEC\n");
928                         return;
929                 }
930                 XMapRaised(disp, winId);
931                 printf("Map: %u\n", winId);
932         } else if (!strncasecmp(cmd, "unmap ", 6)) {
933                 unsigned int winId;
934                 cmd += 6;
935                 if (sscanf(cmd, "%u", &winId) != 1) {
936                         printf("Invalid argument\nx unmap WINID_DEC\n");
937                         return;
938                 }
939                 XUnmapWindow(disp, winId);
940                 printf("Unmap: %u\n", winId);
941         } else {
942                 printf("Unknown command\n");
943         }
944
945         XCloseDisplay(disp);
946 }
947
948 static inline void put_command(const char *cmd)
949 {
950         if (s_info.history[s_info.history_top]) {
951                 free(s_info.history[s_info.history_top]);
952                 s_info.history[s_info.history_top] = NULL;
953         }
954
955         s_info.history[s_info.history_top] = strdup(cmd);
956         s_info.history_top = (s_info.history_top + !!s_info.history[s_info.history_top]) % (sizeof(s_info.history) / sizeof(s_info.history[0]));
957 }
958
959 static inline const char *get_command(int idx)
960 {
961         idx = s_info.history_top + idx;
962         while (idx < 0)
963                 idx += (sizeof(s_info.history) / sizeof(s_info.history[0]));
964
965         return s_info.history[idx];
966 }
967
968 static inline void do_command(const char *cmd)
969 {
970         /* Skip the first spaces */
971         while (*cmd && *cmd == ' ') cmd++;
972
973         if (strlen(cmd) && *cmd != '#') {
974                 if (!strncasecmp(cmd, "exit", 4) || !strncasecmp(cmd, "quit", 4)) {
975                         ecore_main_loop_quit();
976                 } else if (!strncasecmp(cmd, "set ", 4)) {
977                         if (do_set(cmd) == 0)
978                                 return;
979                 } else if (!strncasecmp(cmd, "stat ", 5)) {
980                         do_stat(cmd);
981                 } else if (!strncasecmp(cmd, "get ", 4)) {
982                         if (do_get(cmd) == 0)
983                                 return;
984                 } else if (!strncasecmp(cmd, "ls", 2)) {
985                         if (do_ls(cmd) == 0)
986                                 return;
987                 } else if (!strncasecmp(cmd, "cd", 2)) {
988                         if (do_cd(cmd) == 0)
989                                 return;
990                 } else if (!strncasecmp(cmd, "rm", 2)) {
991                         if (do_rm(cmd) == 0)
992                                 return;
993                 } else if (!strncasecmp(cmd, "sh ", 3)) {
994                         do_sh(cmd);
995                 } else if (!strncasecmp(cmd, "x ", 2)) {
996                         do_x(cmd);
997                 } else {
998                         help();
999                 }
1000         }
1001
1002         prompt(NULL);
1003         return;
1004 }
1005
1006 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
1007 {
1008         static int idx = 0;
1009         static char cmd_buffer[256];
1010         char ch;
1011         int fd;
1012         int ret;
1013         const char escape_str[] = { 0x1b, 0x5b, 0x0 };
1014         const char *escape_ptr = escape_str;
1015         const char *tmp;
1016
1017         if (fd_handler) {
1018                 fd = ecore_main_fd_handler_fd_get(fd_handler);
1019                 if (fd < 0) {
1020                         printf("FD is not valid: %d\n", fd);
1021                         return ECORE_CALLBACK_CANCEL;
1022                 }
1023         } else {
1024                 fd = s_info.input_fd;
1025         }
1026
1027         /*!
1028          * \note
1029          * Using this routine, we can implement the command recommend algorithm.
1030          * When a few more characters are matched with history of command, we can show it to user
1031          * Then the user will choose one or write new command
1032          */
1033
1034         /* Silly.. Silly */
1035         while ((ret = read(fd, &ch, sizeof(ch))) == sizeof(ch)) {
1036                 if (*escape_ptr == '\0') {
1037                         /* Function key */
1038                         switch (ch) {
1039                         case 0x41: /* UP */
1040                                 printf("%s2K%s1G", escape_str, escape_str);
1041                                 tmp = get_command(--s_info.history_idx);
1042                                 if (!tmp) {
1043                                         s_info.history_idx = 0;
1044                                         cmd_buffer[0] = '\0';
1045                                         prompt(NULL);
1046                                 } else {
1047                                         strcpy(cmd_buffer, tmp);
1048                                         idx = strlen(cmd_buffer);
1049                                         prompt(cmd_buffer);
1050                                 }
1051                                 break;
1052                         case 0x42: /* DOWN */
1053                                 if (s_info.history_idx >= 0)
1054                                         break;
1055
1056                                 printf("%s2K%s1G", escape_str, escape_str);
1057                                 tmp = get_command(++s_info.history_idx);
1058                                 if (s_info.history_idx == 0) {
1059                                         s_info.history_idx = 0;
1060                                         cmd_buffer[0] = '\0';
1061                                         prompt(NULL);
1062                                 } else {
1063                                         strcpy(cmd_buffer, tmp);
1064                                         idx = strlen(cmd_buffer);
1065                                         prompt(cmd_buffer);
1066                                 }
1067                                 break;
1068                         case 0x43: /* RIGHT */
1069                                 break;
1070                         case 0x44: /* LEFT */
1071                                 break;
1072                         default:
1073                                 break;
1074                         }
1075
1076                         escape_ptr = escape_str;
1077                         continue;
1078                 } else if (ch == *escape_ptr) {
1079                         escape_ptr++;
1080                         continue;
1081                 }
1082
1083                 switch (ch) {
1084                 case 0x08: /* BKSP */
1085                         cmd_buffer[idx] = '\0';
1086                         if (idx > 0) {
1087                                 idx--;
1088                                 cmd_buffer[idx] = ' ';
1089                                 putc('\r', stdout);
1090                                 prompt(cmd_buffer);
1091                         }
1092
1093                         cmd_buffer[idx] = '\0';
1094                         putc('\r', stdout);
1095                         prompt(cmd_buffer);
1096                         break;
1097                 case 0x09: /* TAB */
1098                         if (!s_info.quick_search_node) {
1099                                 s_info.quick_search_node = node_child(s_info.curdir);
1100                                 s_info.quick_idx = idx;
1101                         } else {
1102                                 s_info.quick_search_node = node_next_sibling(s_info.quick_search_node);
1103                                 idx = s_info.quick_idx;
1104                         }
1105
1106                         if (!s_info.quick_search_node)
1107                                 break;
1108
1109                         printf("%s2K%s1G", escape_str, escape_str);
1110                         strcpy(cmd_buffer + idx, node_name(s_info.quick_search_node));
1111                         idx += strlen(node_name(s_info.quick_search_node));
1112                         prompt(cmd_buffer);
1113                         break;
1114                 case '\n':
1115                 case '\r':
1116                         cmd_buffer[idx] = '\0';
1117                         idx = 0;
1118                         if (s_info.input_fd == STDIN_FILENO || s_info.verbose)
1119                                 putc((int)'\n', stdout);
1120                         do_command(cmd_buffer);
1121                         put_command(cmd_buffer);
1122                         memset(cmd_buffer, 0, sizeof(cmd_buffer));
1123                         s_info.history_idx = 0;
1124                         s_info.quick_search_node = NULL;
1125
1126                         /* Make a main loop processing for command handling */
1127                         return ECORE_CALLBACK_RENEW;
1128                 default:
1129                         cmd_buffer[idx++] = ch;
1130
1131                         if (s_info.input_fd == STDIN_FILENO || s_info.verbose)
1132                                 putc((int)ch, stdout);
1133
1134                         if (idx == sizeof(cmd_buffer) - 1) {
1135                                 cmd_buffer[idx] = '\0';
1136                                 printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
1137                                 idx = 0;
1138                         }
1139                         break;
1140                 }
1141         }
1142
1143         if (ret < 0 && !fd_handler)
1144                 ecore_main_loop_quit();
1145
1146         return ECORE_CALLBACK_RENEW;
1147 }
1148
1149 static void processing_line_buffer(const char *buffer)
1150 {
1151         int pid;
1152         char slavename[256];
1153         char pkgname[256];
1154         char abi[256];
1155         char inst_id[4096];
1156         char cluster[256];
1157         char category[256];
1158         char state[10];
1159         int refcnt;
1160         int fault_count;
1161         int list_count;
1162         int loaded_inst;
1163         int loaded_pkg;
1164         double ttl;
1165         int secured;
1166         double period;
1167         int width;
1168         int height;
1169         struct node *node;
1170         struct package *pkginfo;
1171         struct instance *instinfo;
1172         struct slave *slaveinfo;
1173         int i;
1174
1175         switch (s_info.cmd) {
1176         case PKG_LIST:
1177                 if (sscanf(buffer, "%d %255[^ ] %255[^ ] %255[^ ] %d %d %d", &pid, slavename, pkgname, abi, &refcnt, &fault_count, &list_count) != 7) {
1178                         printf("Invalid format : [%s]\n", buffer);
1179                         return;
1180                 }
1181
1182                 node = node_find(s_info.targetdir, pkgname);
1183                 if (!node) {
1184                         pkginfo = calloc(1, sizeof(*pkginfo));
1185                         if (!pkginfo) {
1186                                 printf("Error: %s\n", strerror(errno));
1187                                 return;
1188                         }
1189
1190                         pkginfo->pkgid = strdup("conf.file");
1191                         if (!pkginfo->pkgid)
1192                                 printf("Error: %s\n", strerror(errno));
1193
1194                         pkginfo->primary = 1;
1195
1196                         node = node_create(s_info.targetdir, pkgname, NODE_DIR);
1197                         if (!node) {
1198                                 free(pkginfo->pkgid);
1199                                 free(pkginfo);
1200                                 printf("Failed to create a new node (%s)\n", pkgname);
1201                                 return;
1202                         }
1203
1204                         node_set_mode(node, NODE_READ | NODE_EXEC);
1205                         node_set_data(node, pkginfo);
1206                 } else {
1207                         pkginfo = node_data(node);
1208                         if (!pkginfo) {
1209                                 printf("Package info is inavlid\n");
1210                                 return;
1211                         }
1212
1213                         free(pkginfo->slavename);
1214                         free(pkginfo->abi);
1215
1216                         pkginfo->slavename = NULL;
1217                         pkginfo->abi = NULL;
1218                 }
1219
1220                 node_set_age(node, s_info.age);
1221
1222                 pkginfo->slavename = strdup(slavename);
1223                 if (!pkginfo->slavename)
1224                         printf("Error: %s\n", strerror(errno));
1225
1226                 pkginfo->abi = strdup(abi);
1227                 if (!pkginfo->abi)
1228                         printf("Error: %s\n", strerror(errno));
1229
1230                 pkginfo->pid = pid;
1231                 pkginfo->refcnt = refcnt;
1232                 pkginfo->fault_count = fault_count;
1233                 pkginfo->inst_count = list_count;
1234                 break;
1235         case SLAVE_LIST:
1236                 if (sscanf(buffer, "%d %[^ ] %[^ ] %[^ ] %d %d %d %[^ ] %d %d %lf", &pid, slavename, pkgname, abi, &secured, &refcnt, &fault_count, state, &loaded_inst, &loaded_pkg, &ttl) != 11) {
1237                         printf("Invalid format : [%s]\n", buffer);
1238                         return;
1239                 }
1240                 node = node_find(s_info.targetdir, slavename);
1241                 if (!node) {
1242                         slaveinfo = calloc(1, sizeof(*slaveinfo));
1243                         if (!slaveinfo) {
1244                                 printf("Error: %s\n", strerror(errno));
1245                                 return;
1246                         }
1247
1248                         node = node_create(s_info.targetdir, slavename, NODE_DIR);
1249                         if (!node) {
1250                                 free(slaveinfo);
1251                                 return;
1252                         }
1253
1254                         node_set_mode(node, NODE_READ | NODE_EXEC);
1255                         node_set_data(node, slaveinfo);
1256                 } else {
1257                         slaveinfo = node_data(node);
1258                 }
1259
1260                 node_set_age(node, s_info.age);
1261
1262                 free(slaveinfo->pkgname);
1263                 free(slaveinfo->abi);
1264                 free(slaveinfo->state);
1265
1266                 slaveinfo->pkgname = strdup(pkgname);
1267                 if (!slaveinfo->pkgname)
1268                         printf("Error: %s\n", strerror(errno));
1269
1270                 slaveinfo->abi = strdup(abi);
1271                 if (!slaveinfo->abi)
1272                         printf("Error: %s\n", strerror(errno));
1273
1274                 slaveinfo->state = strdup(state);
1275                 if (!slaveinfo->state)
1276                         printf("Error: %s\n", strerror(errno));
1277
1278                 slaveinfo->pid = pid;
1279                 slaveinfo->secured = secured;
1280                 slaveinfo->refcnt = refcnt;
1281                 slaveinfo->fault_count = fault_count;
1282                 slaveinfo->loaded_inst = loaded_inst;
1283                 slaveinfo->loaded_pkg = loaded_pkg;
1284                 slaveinfo->ttl = ttl;
1285                 break;
1286         case INST_LIST:
1287                 if (sscanf(buffer, "%[^ ] %[^ ] %[^ ] %lf %[^ ] %d %d", inst_id, cluster, category, &period, state, &width, &height) != 7) {
1288                         printf("Invalid format : [%s]\n", buffer);
1289                         return;
1290                 }
1291
1292                 for (i = strlen(inst_id); i > 0 && inst_id[i] != '/'; i--);
1293                 i += (inst_id[i] == '/');
1294
1295                 node = node_find(s_info.targetdir, inst_id + i);
1296                 if (!node) {
1297                         instinfo = calloc(1, sizeof(*instinfo));
1298                         if (!instinfo) {
1299                                 printf("Error: %s\n", strerror(errno));
1300                                 return;
1301                         }
1302
1303                         node = node_create(s_info.targetdir, inst_id + i, NODE_FILE);
1304                         if (!node) {
1305                                 free(instinfo);
1306                                 return;
1307                         }
1308
1309                         node_set_mode(node, NODE_READ | NODE_WRITE);
1310                         node_set_data(node, instinfo);
1311                 } else {
1312                         instinfo = node_data(node);
1313                 }
1314
1315                 node_set_age(node, s_info.age);
1316
1317                 free(instinfo->id);
1318                 free(instinfo->cluster);
1319                 free(instinfo->category);
1320                 free(instinfo->state);
1321
1322                 instinfo->id = strdup(inst_id);
1323                 if (!instinfo->id)
1324                         printf("Error: %s\n", strerror(errno));
1325
1326                 instinfo->cluster = strdup(cluster);
1327                 if (!instinfo->cluster)
1328                         printf("Error: %s\n", strerror(errno));
1329
1330                 instinfo->category = strdup(category);
1331                 if (!instinfo->category)
1332                         printf("Error: %s\n", strerror(errno));
1333
1334                 instinfo->state = strdup(state);
1335                 if (!instinfo->state)
1336                         printf("Error: %s\n", strerror(errno));
1337
1338                 instinfo->period = period;
1339                 instinfo->width = width;
1340                 instinfo->height = height;
1341                 break;
1342         case INST_CTRL:
1343                 sscanf(buffer, "%d", &i);
1344                 printf("%s\n", strerror(i));
1345                 printf("Result: %d\n", i);
1346                 break;
1347         case SLAVE_CTRL:
1348                 sscanf(buffer, "%d", &i);
1349                 printf("Result: %d\n", i);
1350                 break;
1351         case MASTER_CTRL:
1352                 sscanf(buffer, "%d", &i);
1353                 printf("Result: %d\n", i);
1354                 break;
1355         default:
1356                 break;
1357         }
1358 }
1359
1360 static inline void do_line_command(void)
1361 {
1362         switch (s_info.cmd) {
1363         case PKG_LIST:
1364                 ls();
1365                 break;
1366         case INST_LIST:
1367                 ls();
1368                 break;
1369         case SLAVE_LIST:
1370                 ls();
1371                 break;
1372         case INST_CTRL:
1373                 break;
1374         case SLAVE_CTRL:
1375                 break;
1376         case MASTER_CTRL:
1377                 break;
1378         default:
1379                 break;
1380         }
1381         prompt(NULL);
1382 }
1383
1384 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
1385 {
1386         int fd;
1387         static char *line_buffer = NULL;
1388         static int line_index = 0;
1389         static int bufsz = 256;
1390         char ch;
1391
1392         fd = ecore_main_fd_handler_fd_get(fd_handler);
1393         if (fd < 0) {
1394                 printf("FD is not valid: %d\n", fd);
1395                 return ECORE_CALLBACK_CANCEL;
1396         }
1397
1398         if (read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
1399                 printf("Error: %s\n", strerror(errno));
1400                 return ECORE_CALLBACK_CANCEL;
1401         }
1402
1403         if (!line_buffer) {
1404                 line_index = 0;
1405                 line_buffer = malloc(bufsz);
1406                 if (!line_buffer) {
1407                         printf("Error: %s\n", strerror(errno));
1408                         return ECORE_CALLBACK_CANCEL;
1409                 }
1410         }       
1411
1412         if (ch == '\n') { /* End of a line */
1413                 if (line_index == bufsz - 1) {
1414                         char *new_buf;
1415                         new_buf = realloc(line_buffer, bufsz + 2);
1416                         if (!new_buf) {
1417                                 printf("Error: %s\n", strerror(errno));
1418                                 free(line_buffer);
1419                                 line_buffer = NULL;
1420                                 line_index = 0;
1421                                 bufsz = 256;
1422                                 return ECORE_CALLBACK_CANCEL;
1423                         }
1424
1425                         line_buffer = new_buf;
1426                 }
1427
1428                 line_buffer[line_index] = '\0';
1429
1430                 if (!strcmp(line_buffer, "EOD")) {
1431                         do_line_command();
1432                         s_info.cmd = NOP;
1433                 } else {
1434                         processing_line_buffer(line_buffer);
1435                 }
1436
1437                 free(line_buffer);
1438                 line_buffer = NULL;
1439                 line_index = 0;
1440                 bufsz = 256;
1441         } else {
1442                 char *new_buf;
1443
1444                 line_buffer[line_index++] = ch;
1445                 if (line_index == bufsz - 1) {
1446                         bufsz += 256;
1447                         new_buf = realloc(line_buffer, bufsz);
1448                         if (!new_buf) {
1449                                 printf("Error: %s\n", strerror(errno));
1450                                 free(line_buffer);
1451                                 line_buffer = NULL;
1452                                 line_index = 0;
1453                                 bufsz = 256;
1454                                 return ECORE_CALLBACK_CANCEL;
1455                         }
1456
1457                         line_buffer = new_buf;
1458                 }
1459         }
1460
1461         return ECORE_CALLBACK_RENEW;
1462 }
1463
1464 static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
1465 {
1466         const char *fifo_name;
1467         int ret;
1468
1469         if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
1470                 printf("Invalid packet\n");
1471                 return -EFAULT;
1472         }
1473
1474         if (ret != 0) {
1475                 printf("Returns %d\n", ret);
1476                 return ret;
1477         }
1478
1479         printf("FIFO: %s\n", fifo_name);
1480
1481         s_info.fifo_handle = open(fifo_name, O_RDONLY | O_NONBLOCK);
1482         if (s_info.fifo_handle < 0) {
1483                 printf("Error: %s\n", strerror(errno));
1484                 s_info.fifo_handle = -EINVAL;
1485                 ecore_main_loop_quit();
1486                 return -EINVAL;
1487         }
1488
1489         s_info.fd_handler = ecore_main_fd_handler_add(s_info.fifo_handle, ECORE_FD_READ, read_cb, NULL, NULL, NULL);
1490         if (!s_info.fd_handler) {
1491                 printf("Failed to add a fd handler\n");
1492                 close(s_info.fifo_handle);
1493                 s_info.fifo_handle = -EINVAL;
1494                 ecore_main_loop_quit();
1495                 return -EFAULT;
1496         }
1497
1498         prompt(NULL);
1499
1500         if (s_info.input_fd == STDIN_FILENO) {
1501                 if (fcntl(s_info.input_fd, F_SETFL, O_NONBLOCK) < 0)
1502                         printf("Error: %s\n", strerror(errno));
1503
1504                 s_info.in_handler = ecore_main_fd_handler_add(s_info.input_fd, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
1505                 if (!s_info.in_handler) {
1506                         printf("Failed to add a input handler\n");
1507                         ecore_main_loop_quit();
1508                         return -EFAULT;
1509                 }
1510         }
1511
1512         return 0;
1513 }
1514
1515 static int disconnected_cb(int handle, void *data)
1516 {
1517         printf("Disconnected\n");
1518         ecore_main_loop_quit();
1519         return 0;
1520 }
1521
1522 static int connected_cb(int handle, void *data)
1523 {
1524         struct packet *packet;
1525
1526         printf("Connected\n");
1527
1528         packet = packet_create("liveinfo_hello", "d", 0.0f);
1529         if (!packet) {
1530                 printf("Failed to build a packet for hello\n");
1531                 com_core_packet_client_fini(s_info.fd);
1532                 s_info.fd = -EINVAL;
1533                 return -EFAULT;
1534         }
1535
1536         s_info.fd = handle;
1537
1538         if (com_core_packet_async_send(s_info.fd, packet, 0.0f, ret_cb, NULL) < 0) {
1539                 printf("Failed to send a packet hello\n");
1540                 packet_destroy(packet);
1541                 com_core_packet_client_fini(s_info.fd);
1542                 s_info.fd = -EINVAL;
1543                 return -EFAULT;
1544         }
1545
1546         packet_destroy(packet);
1547         return 0;
1548 }
1549
1550 int main(int argc, char *argv[])
1551 {
1552         struct termios ttystate;
1553         static struct method s_table[] = {
1554                 {
1555                         .cmd = NULL,
1556                         .handler = NULL,
1557                 },
1558         };
1559         static struct option long_options[] = {
1560                 { "batchmode", required_argument, 0, 'b' },
1561                 { "help", no_argument, 0, 'h' },
1562                 { "verbose", required_argument, 0, 'v' },
1563                 { 0, 0, 0, 0 }
1564         };
1565         int option_index;
1566         int c;
1567
1568         do {
1569                 c = getopt_long(argc, argv, "b:hv:", long_options, &option_index);
1570                 switch (c) {
1571                 case 'b':
1572                         if (!optarg || !*optarg) {
1573                                 printf("Invalid argument\n");
1574                                 help();
1575                                 return -EINVAL;
1576                         }
1577
1578                         if (s_info.input_fd != STDIN_FILENO) {
1579                                 /* Close the previously, opened file */
1580                                 close(s_info.input_fd);
1581                         }
1582
1583                         s_info.input_fd = open(optarg, O_RDONLY);
1584                         if (s_info.input_fd < 0) {
1585                                 printf("Unable to access %s (%s)\n", optarg, strerror(errno));
1586                                 return -EIO;
1587                         }
1588                         break;
1589                 case 'h':
1590                         help();
1591                         return 0;
1592                 case 'v':
1593                         if (!optarg || !*optarg) {
1594                                 printf("Invalid argument\n");
1595                                 help();
1596                                 return -EINVAL;
1597                         }
1598
1599                         s_info.verbose = !strcmp(optarg, "true");
1600                         break;
1601                 default:
1602                         break;
1603                 }
1604         } while (c != -1);
1605
1606         ecore_init();
1607         g_type_init();
1608
1609         com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1610         com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1611         livebox_service_init();
1612
1613         s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
1614         if (s_info.fd < 0) {
1615                 printf("Failed to make a connection\n");
1616                 return -EIO;
1617         }
1618
1619         if (s_info.input_fd == STDIN_FILENO) {
1620                 printf("Type your command on below empty line\n");
1621
1622                 if (tcgetattr(s_info.input_fd, &ttystate) < 0) {
1623                         printf("Error: %s\n", strerror(errno));
1624                 } else {
1625                         ttystate.c_lflag &= ~(ICANON | ECHO);
1626                         ttystate.c_cc[VMIN] = 1;
1627
1628                         if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0)
1629                                 printf("Error: %s\n", strerror(errno));
1630                 }
1631         } else {
1632                 printf("Batch mode enabled\n");
1633         }
1634
1635         if (setvbuf(stdout, (char *)NULL, _IONBF, 0) != 0)
1636                 printf("Error: %s\n", strerror(errno));
1637
1638         init_directory();
1639
1640         ecore_main_loop_begin();
1641
1642         fini_directory();
1643         livebox_service_fini();
1644
1645         if (s_info.fd > 0) {
1646                 com_core_packet_client_fini(s_info.fd);
1647                 s_info.fd = -EINVAL;
1648         }
1649
1650         if (s_info.fd_handler) {
1651                 ecore_main_fd_handler_del(s_info.fd_handler);
1652                 s_info.fd_handler = NULL;
1653         }
1654
1655         if (s_info.input_fd == STDIN_FILENO) {
1656                 ttystate.c_lflag |= ICANON | ECHO;
1657                 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0)
1658                         printf("Error: %s\n", strerror(errno));
1659         } else {
1660                 close(s_info.input_fd);
1661         }
1662
1663         if (s_info.fifo_handle > 0) {
1664                 close(s_info.fifo_handle);
1665                 s_info.fifo_handle = -EINVAL;
1666         }
1667
1668         if (s_info.in_handler) {
1669                 ecore_main_fd_handler_del(s_info.in_handler);
1670                 s_info.in_handler = NULL;
1671         }
1672
1673         ecore_shutdown();
1674         putc((int)'\n', stdout);
1675         return 0;
1676 }
1677
1678 /* End of a file */