blob: cfebbf0cd06a3e79227604ebd599bee83041d828 [file] [log] [blame]
San Mehatdc266072009-05-06 11:16:52 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
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 */
San Mehat3c5a6f02009-05-22 15:36:13 -070016
San Mehatdc266072009-05-06 11:16:52 -070017#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <signal.h>
22#include <errno.h>
23#include <fcntl.h>
24
25#include <sys/socket.h>
26#include <sys/select.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <sys/un.h>
30
31#include <cutils/sockets.h>
32
33#include <private/android_filesystem_config.h>
34
San Mehatdc266072009-05-06 11:16:52 -070035int main(int argc, char **argv) {
36 int sock;
37
38 if ((sock = socket_local_client("nexus",
39 ANDROID_SOCKET_NAMESPACE_RESERVED,
40 SOCK_STREAM)) < 0) {
41 fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
42 exit(1);
43 }
44
45 printf("Connected to nexus\n");
46
San Mehat69772dc2009-05-10 09:27:07 -070047 char line[255];
48 char *buffer = malloc(4096);
49 int cursor = 0;
50 int col = 0;
51
San Mehatdc266072009-05-06 11:16:52 -070052 while(1) {
53 fd_set read_fds;
54 struct timeval to;
55 int rc = 0;
56
San Mehat1441e762009-05-07 11:37:10 -070057 to.tv_sec = 10;
San Mehatdc266072009-05-06 11:16:52 -070058 to.tv_usec = 0;
San Mehat69772dc2009-05-10 09:27:07 -070059
San Mehatdc266072009-05-06 11:16:52 -070060 FD_ZERO(&read_fds);
61 FD_SET(sock, &read_fds);
San Mehat69772dc2009-05-10 09:27:07 -070062 FD_SET(0, &read_fds);
63
64 if (col == 0) {
65 fprintf(stdout, "-> ");
66 fflush(stdout);
67 col = 3;
68 }
San Mehatdc266072009-05-06 11:16:52 -070069
San Mehatdc266072009-05-06 11:16:52 -070070 if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
San Mehatdc266072009-05-06 11:16:52 -070071 fprintf(stderr, "Error in select (%s)\n", strerror(errno));
72 exit(2);
73 } else if (!rc) {
San Mehatdc266072009-05-06 11:16:52 -070074 continue;
75 } else if (FD_ISSET(sock, &read_fds)) {
San Mehat69772dc2009-05-10 09:27:07 -070076 memset(buffer, 0, 4096);
77 if ((rc = read(sock, buffer, 4096)) <= 0) {
San Mehatdc266072009-05-06 11:16:52 -070078 fprintf(stderr, "Error reading response (%s)\n", strerror(errno));
79 exit(2);
San Mehat69772dc2009-05-10 09:27:07 -070080 }
81 int i;
82 for (i = 0; i < col; i++) {
83 fprintf(stdout, "%c", 8);
84 }
85
86 printf("%s", buffer);
87 printf("-> ");
88 for (i = 0; i < cursor; i++) {
89 fprintf(stdout, "%c", line[i]);
90 }
91 fflush(stdout);
92 } else if (FD_ISSET(0, &read_fds)) {
93 char c;
94
95 if ((rc = read(0, &c, 1)) < 0) {
96 fprintf(stderr, "Error reading from terminal (%s)\n", strerror(errno));
97 exit(2);
98 } else if (!rc) {
99 fprintf(stderr, "0 length read from terminal\n");
100 exit(2);
101 }
102
103 fprintf(stdout, "%c", c);
104 fflush(stdout);
105
106 line[cursor] = c;
107
108 if (c == '\n') {
109 if ((rc = write(sock, line, strlen(line))) < 0) {
110 fprintf(stderr, "Error writing to nexus (%s)\n", strerror(errno));
111 exit(2);
112 }
113 memset(line, 0, sizeof(line));
114 cursor = 0;
115 col = 0;
116 } else {
117 cursor++;
118 col++;
119 }
San Mehatdc266072009-05-06 11:16:52 -0700120 }
121 }
122
San Mehatdc266072009-05-06 11:16:52 -0700123 exit(0);
San Mehatdc266072009-05-06 11:16:52 -0700124}