#include <acess/sys.h>\r
#include <stdio.h>\r
#include <net.h>\r
+#include <readline.h>\r
+#include <string.h>\r
\r
#define BUFSIZ 128\r
\r
{\r
int server_fd, rv;\r
int client_running = 1;\r
+ int bUseReadline = !!argv[3]; // HACK: If third argument is present, use ReadLine\r
+ tReadline *readline_info;\r
\r
// Connect to the remove server\r
server_fd = OpenTCP( argv[1], atoi(argv[2]) );\r
return -1;\r
}\r
\r
+ // Create a ReadLine instance with no history\r
+ readline_info = Readline_Init(0);\r
+ \r
while( client_running )\r
{\r
fd_set fds;\r
char buffer[BUFSIZ];\r
int len;\r
\r
+ // Prepare FD sets\r
FD_ZERO(&fds); FD_ZERO(&err_fds);\r
FD_SET(0, &fds); FD_SET(0, &err_fds);\r
FD_SET(server_fd, &fds); FD_SET(server_fd, &err_fds);\r
\r
+ // Wait for data (no timeout)\r
rv = select(server_fd+1, &fds, NULL, &err_fds, NULL);\r
if( rv < 0 ) break;\r
\r
+ // Check for remote data avaliable\r
if( FD_ISSET(server_fd, &fds) )\r
{\r
// Read from server, and write to stdout\r
} while( len == BUFSIZ );\r
}\r
\r
+ // Check for local data input\r
if( FD_ISSET(0, &fds) )\r
{\r
// Read from stdin, and write to server\r
- do\r
+ if( bUseReadline )\r
{\r
- len = read(0, BUFSIZ, buffer);\r
- write(server_fd, len, buffer);\r
- write(1, len, buffer);\r
- } while( len == BUFSIZ );\r
+ char *line = Readline_NonBlock(readline_info);\r
+ if( line )\r
+ {\r
+ write(server_fd, strlen(line), line);\r
+ write(server_fd, 1, "\n");\r
+ }\r
+ }\r
+ else\r
+ {\r
+ do\r
+ {\r
+ len = read(0, BUFSIZ, buffer);\r
+ write(server_fd, len, buffer);\r
+ write(1, len, buffer);\r
+ } while( len == BUFSIZ );\r
+ }\r
}\r
\r
+ // If there was an error, quit\r
if( FD_ISSET(server_fd, &err_fds) )\r
{\r
+ printf("\nRemote connection lost\n");\r
break ;\r
}\r
}\r
{\r
int fd, addrType;\r
char *iface;\r
- char addrBuffer[8];\r
+ uint8_t addrBuffer[16];\r
\r
// Parse IP Address\r
addrType = Net_ParseAddress(AddressString, addrBuffer);\r
fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString);\r
return -1;\r
}\r
- \r
- // Finds the interface for the destination address\r
- iface = Net_GetInterface(addrType, addrBuffer);\r
- if( iface == NULL ) {\r
- fprintf(stderr, "Unable to find a route to '%s'\n", AddressString);\r
- return -1;\r
- }\r
- \r
- printf("iface = '%s'\n", iface);\r
- \r
- // Open client socket\r
- // TODO: Move this out to libnet?\r
+\r
+ // Opens a R/W handle\r
+ fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");\r
+ if( fd == -1 )\r
{\r
- int len = snprintf(NULL, 100, "/Devices/ip/%s/tcpc", iface);\r
- char path[len+1];\r
- snprintf(path, 100, "/Devices/ip/%s/tcpc", iface);\r
- fd = open(path, OPENFLAG_READ|OPENFLAG_WRITE);\r
- }\r
- \r
- free(iface);\r
- \r
- if( fd == -1 ) {\r
- fprintf(stderr, "Unable to open TCP Client for reading\n");\r
+ fprintf(stderr, "Unable to open TCP Client\n");\r
return -1;\r
}\r
\r
// Set remote port and address\r
- printf("Setting port and remote address\n");\r
ioctl(fd, 5, &PortNumber);\r
ioctl(fd, 6, addrBuffer);\r
\r
// Connect\r
- printf("Initiating connection\n");\r
if( ioctl(fd, 7, NULL) == 0 ) {\r
- // Shouldn't happen :(\r
fprintf(stderr, "Unable to start connection\n");\r
return -1;\r
}\r
\r
+ printf("Connection opened\n");\r
+ \r
// Return descriptor\r
return fd;\r
}\r