diff options
author | LAN-TW <lantw44@gmail.com> | 2013-11-10 02:27:43 +0800 |
---|---|---|
committer | b01902062 <b01902062@linux5.csie.ntu.edu.tw> | 2013-11-10 02:27:43 +0800 |
commit | 5793c733dd51289d24d68798ec828763c6b4d6e7 (patch) | |
tree | e6503ed6b0e0a61a26257b21e88c8f1ff50bd5ac | |
parent | cef946f774a6bc484ec2577b051c2d961aa1e8e0 (diff) | |
download | cn2013-5793c733dd51289d24d68798ec828763c6b4d6e7.tar.gz cn2013-5793c733dd51289d24d68798ec828763c6b4d6e7.tar.zst cn2013-5793c733dd51289d24d68798ec828763c6b4d6e7.zip |
HW1: RasClient 命令列解析
-rw-r--r-- | hw1/client-main.c | 78 | ||||
-rw-r--r-- | hw1/configure.ac | 2 | ||||
-rw-r--r-- | hw1/shell.c | 2 |
3 files changed, 76 insertions, 6 deletions
diff --git a/hw1/client-main.c b/hw1/client-main.c index 9d0e9a6..e970a8c 100644 --- a/hw1/client-main.c +++ b/hw1/client-main.c @@ -3,6 +3,7 @@ #endif #include <locale.h> +#include <stdbool.h> #include <stdio.h> #include <time.h> #include <unistd.h> @@ -11,12 +12,81 @@ int main (int argc, char* argv[]) { setlocale (LC_ALL, ""); tzset (); - if (argc < 3) { - fprintf (stderr, "Usage: %s host port\n", argv[0]); - return 1; + const char* logincmd = isatty (STDIN_FILENO) ? "LOGINTTY" : "LOGIN"; + const char* conn[2] = { NULL, NULL }; + bool allowv4 = true; + bool allowv6 = true; + int conncnt = 0; + for (int i = 1; i < argc; i++) { + if (argv[i][0] == '-') { + for (int j = 1; argv[i][j] != '\0'; j++) { + switch (argv[i][j]) { + case 't': + logincmd = "LOGINTTY"; + break; + case 'T': + logincmd = "LOGIN"; + break; + case 'I': + allowv4 = true; + allowv6 = true; + break; + case 'U': + allowv4 = false; + allowv6 = false; + break; + case '4': + allowv4 = true; + allowv6 = false; + break; + case '6': + allowv4 = false; + allowv6 = true; + break; + case 'h': + case '?': + case '-': + printf ( + "Usage: %s [-46hItTU] host|file [port]\n" + " -h View this help message\n" + " -t Force pseudo terminal allocation\n" + " -T Disable pseudo terminal allocation\n" + " -U Connect to a UNIX-domain socket file\n" + " -I Connect to the Internet\n" + " -4 Try IPv4 only\n" + " -6 Try IPv6 only\n", + argv[0]); + return 0; + break; + default: + fprintf (stderr, "%s: -%c: unknown option\n", argv[0], argv[i][j]); + return 1; + } + } + } else { + if (conncnt >= 2) { + fprintf (stderr, "%s: %s: unknown argument\n", argv[0], argv[i]); + return 1; + } + conn[conncnt++] = argv[i]; + } } - char* logincmd = isatty (STDIN_FILENO) ? "LOGINTTY" : "LOGIN"; + if (allowv4 || allowv6) { + if (conn[0] == NULL) { + fprintf (stderr, "%s: host name is required\n", argv[0]); + return 1; + } + if (conn[1] == NULL) { + fprintf (stderr, "%s: port number is requried\n", argv[0]); + return 1; + } + } else { + if (conn[0] == NULL) { + fprintf (stderr, "%s: socket file name is required\n", argv[0]); + return 1; + } + } return 0; } diff --git a/hw1/configure.ac b/hw1/configure.ac index bcf9b5c..7946ab8 100644 --- a/hw1/configure.ac +++ b/hw1/configure.ac @@ -1,7 +1,7 @@ # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. -AC_INIT([cn2013-hw1], [1]) +AC_INIT([cn2013-hw1], [2]) AC_CONFIG_SRCDIR([server-main.c]) AC_CONFIG_HEADERS([config.h]) diff --git a/hw1/shell.c b/hw1/shell.c index d0df6f4..cf71bd1 100644 --- a/hw1/shell.c +++ b/hw1/shell.c @@ -585,7 +585,7 @@ static int ras_shell_init (RasShell* shell, int argc, char* argv[]) { case '?': case '-': printf ( - "Usage: %s [-hlr]\n" + "Usage: %s [-hlrv]\n" " -h View this help message\n" " -l Make ras-shell be a login shell\n" " -r Make ras-shell be a restricted shell\n" |