Tizen 2.1 base
[sdk/target/sdbd.git] / src / console.c
1 /*
2  * Copyright (c) 2011 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 #include "sysdeps.h"
18 #include "sdb.h"
19 #include "sdb_client.h"
20 #include <stdio.h>
21
22 static int  connect_to_console(void)
23 {
24     int  fd, port;
25
26     port = sdb_get_emulator_console_port();
27     if (port < 0) {
28         if (port == -2)
29             fprintf(stderr, "error: more than one emulator detected. use -s option\n");
30         else
31             fprintf(stderr, "error: no emulator detected\n");
32         return -1;
33     }
34     fd = socket_loopback_client( port, SOCK_STREAM );
35     if (fd < 0) {
36         fprintf(stderr, "error: could not connect to TCP port %d\n", port);
37         return -1;
38     }
39     return  fd;
40 }
41
42
43 int  sdb_send_emulator_command(int  argc, char**  argv)
44 {
45     int   fd, nn;
46
47     fd = connect_to_console();
48     if (fd < 0)
49         return 1;
50
51 #define  QUIT  "quit\n"
52
53     for (nn = 1; nn < argc; nn++) {
54         sdb_write( fd, argv[nn], strlen(argv[nn]) );
55         sdb_write( fd, (nn == argc-1) ? "\n" : " ", 1 );
56     }
57     sdb_write( fd, QUIT, sizeof(QUIT)-1 );
58     sdb_close(fd);
59
60     return 0;
61 }