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 | */ |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 16 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 17 | #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 Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 35 | int 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 Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 47 | char line[255]; |
| 48 | char *buffer = malloc(4096); |
| 49 | int cursor = 0; |
| 50 | int col = 0; |
| 51 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 52 | while(1) { |
| 53 | fd_set read_fds; |
| 54 | struct timeval to; |
| 55 | int rc = 0; |
| 56 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 57 | to.tv_sec = 10; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 58 | to.tv_usec = 0; |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 59 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 60 | FD_ZERO(&read_fds); |
| 61 | FD_SET(sock, &read_fds); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 62 | FD_SET(0, &read_fds); |
| 63 | |
| 64 | if (col == 0) { |
| 65 | fprintf(stdout, "-> "); |
| 66 | fflush(stdout); |
| 67 | col = 3; |
| 68 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 69 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 70 | if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 71 | fprintf(stderr, "Error in select (%s)\n", strerror(errno)); |
| 72 | exit(2); |
| 73 | } else if (!rc) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 74 | continue; |
| 75 | } else if (FD_ISSET(sock, &read_fds)) { |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 76 | memset(buffer, 0, 4096); |
| 77 | if ((rc = read(sock, buffer, 4096)) <= 0) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 78 | fprintf(stderr, "Error reading response (%s)\n", strerror(errno)); |
| 79 | exit(2); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 80 | } |
| 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 Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 123 | exit(0); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 124 | } |