San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <string.h> |
| 20 | #include <signal.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | |
| 24 | #include <sys/socket.h> |
| 25 | #include <sys/select.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/un.h> |
| 29 | |
| 30 | #include <cutils/sockets.h> |
| 31 | |
| 32 | #include <private/android_filesystem_config.h> |
| 33 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 34 | int main(int argc, char **argv) { |
| 35 | int sock; |
| 36 | |
| 37 | if ((sock = socket_local_client("nexus", |
| 38 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 39 | SOCK_STREAM)) < 0) { |
| 40 | fprintf(stderr, "Error connecting (%s)\n", strerror(errno)); |
| 41 | exit(1); |
| 42 | } |
| 43 | |
| 44 | printf("Connected to nexus\n"); |
| 45 | |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame^] | 46 | char line[255]; |
| 47 | char *buffer = malloc(4096); |
| 48 | int cursor = 0; |
| 49 | int col = 0; |
| 50 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 51 | while(1) { |
| 52 | fd_set read_fds; |
| 53 | struct timeval to; |
| 54 | int rc = 0; |
| 55 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 56 | to.tv_sec = 10; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 57 | to.tv_usec = 0; |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame^] | 58 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 59 | FD_ZERO(&read_fds); |
| 60 | FD_SET(sock, &read_fds); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame^] | 61 | FD_SET(0, &read_fds); |
| 62 | |
| 63 | if (col == 0) { |
| 64 | fprintf(stdout, "-> "); |
| 65 | fflush(stdout); |
| 66 | col = 3; |
| 67 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 68 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 69 | if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 70 | fprintf(stderr, "Error in select (%s)\n", strerror(errno)); |
| 71 | exit(2); |
| 72 | } else if (!rc) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 73 | continue; |
| 74 | } else if (FD_ISSET(sock, &read_fds)) { |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame^] | 75 | memset(buffer, 0, 4096); |
| 76 | if ((rc = read(sock, buffer, 4096)) <= 0) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 77 | fprintf(stderr, "Error reading response (%s)\n", strerror(errno)); |
| 78 | exit(2); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame^] | 79 | } |
| 80 | int i; |
| 81 | for (i = 0; i < col; i++) { |
| 82 | fprintf(stdout, "%c", 8); |
| 83 | } |
| 84 | |
| 85 | printf("%s", buffer); |
| 86 | printf("-> "); |
| 87 | for (i = 0; i < cursor; i++) { |
| 88 | fprintf(stdout, "%c", line[i]); |
| 89 | } |
| 90 | fflush(stdout); |
| 91 | } else if (FD_ISSET(0, &read_fds)) { |
| 92 | char c; |
| 93 | |
| 94 | if ((rc = read(0, &c, 1)) < 0) { |
| 95 | fprintf(stderr, "Error reading from terminal (%s)\n", strerror(errno)); |
| 96 | exit(2); |
| 97 | } else if (!rc) { |
| 98 | fprintf(stderr, "0 length read from terminal\n"); |
| 99 | exit(2); |
| 100 | } |
| 101 | |
| 102 | fprintf(stdout, "%c", c); |
| 103 | fflush(stdout); |
| 104 | |
| 105 | line[cursor] = c; |
| 106 | |
| 107 | if (c == '\n') { |
| 108 | if ((rc = write(sock, line, strlen(line))) < 0) { |
| 109 | fprintf(stderr, "Error writing to nexus (%s)\n", strerror(errno)); |
| 110 | exit(2); |
| 111 | } |
| 112 | memset(line, 0, sizeof(line)); |
| 113 | cursor = 0; |
| 114 | col = 0; |
| 115 | } else { |
| 116 | cursor++; |
| 117 | col++; |
| 118 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 122 | exit(0); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 123 | } |