Jelajahi Sumber

vcc: use getopt_long instead

intirain 4 tahun lalu
induk
melakukan
3014d81004
4 mengubah file dengan 90 tambahan dan 41 penghapusan
  1. 2 2
      Makefile
  2. 1 1
      include/vcc/defserv.h
  3. 2 2
      include/vcc/version.h
  4. 85 36
      main.c

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 VCC_MAJOR 		= 1
 VCC_MINOR 		= 13
-VCC_PATCH 		= 0
+VCC_PATCH 		= 1
 VCC_STR 		= -rc 
 
 VCC_COMPILE_TIME 	= $(shell date)
@@ -13,7 +13,7 @@ target 			= vcc
 libgcrypt_cflags 	= $(shell libgcrypt-config --libs)
 CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g $(libgcrypt_cflags) -Lplugins -lplugins 
 MAKEFLAGS 		= --no-print-directory
-VCC_DEFSERV 		= 39.104.27.216 
+VCC_DEFSERV 		= 39.104.27.216
 VCC_VERSION 		= VCC $(VCC_MAJOR).$(VCC_MINOR).$(VCC_PATCH) \
 		  $(VCC_COMPILE_TIME) $(VCC_COMPILE_BY)
 

+ 1 - 1
include/vcc/defserv.h

@@ -1 +1 @@
-#define VCC_DEFSERV "39.104.27.216 "
+#define VCC_DEFSERV "39.104.27.216"

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.13.0 Sun May  1 12:35:51 CST 2022 root"
-#define VCC_SEMVER "vcc 1.13.0-rc "
+#define VCC_VERSION "VCC 1.13.1 Sun May  1 13:09:17 CST 2022 root"
+#define VCC_SEMVER "vcc 1.13.1-rc "

+ 85 - 36
main.c

@@ -34,12 +34,15 @@
 #include <arpa/inet.h>
 #include <poll.h>
 #include <signal.h>
+#include <getopt.h>
 
 #include <klist.h>
 #include <vcc/vcc.h>
 #include <vcc/version.h>
 #include <vcc/pretty.h>
 #include <vcc/defserv.h> 
+
+
 /* we tried to use libreadline, but after using that, we feel strange 
  * because... because of what? i don't know. */
 
@@ -63,7 +66,14 @@ int do_cmd_lself(char *);
 
 int init_socket(const char *server_addr, int server_port);
 
-int fd;
+int 		fd;
+
+/* normally. it belongs to the foreground pgroup, all messages write to terminal. */
+#define MODE_NORMAL 	1
+/* robot mode. we run in the background pgroup, and give all the messages to the plugin specified. */
+#define MODE_ROBOT 	2
+int 		mode = MODE_NORMAL;
+struct plugin 	*plgctl;
 
 char usrname[USRNAME_SIZE];
 struct klist_node banned_people = KLIST_NODE_INIT(&banned_people);
@@ -72,17 +82,61 @@ extern int crypt_disabled;
 extern int current_sid;
 
 static char const *help_msg = {
-	"Usage: vcc [-up] [server]\n"
+	"Usage: vcc [-auprh] [server]\n"
 	"Options: \n"
 	"  -u    --user    Set the username on the server. "
 	"If unspecified, you must input it interatively. \n"
 	"  -p    --port    Specified the port on the server. "
 	"Default port is 46. You must keep this as same "
 	"as the port on the server. \n"
-	"  [server]        Address of the server. "
+	"  -r    --robot   Enter the robot mode. \n"
+	"  -a    --address Address of the server. "
 	"As default, server is " VCC_DEFSERV ". \n"
+	"  -h    --help    Show this help information. \n"
+};
+
+static struct option longopts[] = {
+	{
+		.name = "address", 
+		.has_arg = 1, 
+		.flag = NULL, 
+		.val = 'a', 
+	}, 
+
+	{
+		.name = "user", 
+		.has_arg = 1, 
+		.flag = NULL, 
+		.val = 'u'
+	}, 
+
+	{
+		.name = "port", 
+		.has_arg = 1, 
+		.flag = NULL, 
+		.val = 'p'
+	}, 
+
+	{
+		.name = "robot", 
+		.has_arg = 0, 
+		.flag = NULL, 
+		.val = 'r'
+	}, 
+
+	{
+		.name = "help", 
+		.has_arg = 0, 
+		.flag = NULL, 
+		.val = 'h'
+	}, 
+
+	{
+		NULL, 0, NULL, 0
+	}
 };
 
+
 static int usage(void) {
 	fputs(help_msg, stderr);
 	return 1;
@@ -143,45 +197,40 @@ int vos_login(void) {
 	return 1;
 }
 
-int parse_args(char **argv, int *port, char **ip) {
-	int i = 1;
-	/*
-	 * unknown: 0
-	 * usrname: 1
-	 * port: 2
-	 */
-	int type = 0;
-	int got_usrname = 0;
-	char *arg;
-
-	while (argv[i]) {
-		arg = argv[i];
-		switch (type) {
-		case 0:
-			if (!strcmp(arg, "-u") || !strcmp(arg, "--user")) {
-				type = 1;
-			} else if (!strcmp(arg, "-p") || !strcmp(arg, "--port")) {
-				type = 2;
-			} else if (arg[0] != '-') {
-				*ip = arg;
-			} else {
-				exit(usage());
-			}
+int parse_args(int argc, char **argv, int *port, char **ip) {
+	int i, c, got_usrname = 0;
+
+	while ((c = getopt_long(argc, argv, "a:u:p:rh", longopts, &i)) != -1) {
+		switch (c) {
+		case 'a':
+			*ip = strdup(optarg);
+			printf("%s\n", *ip);
+
 			break;
-		case 1:
-			strcpy(usrname, arg);
+
+		case 'u':
+			strcpy(usrname, optarg);
 			got_usrname = 1;
-			type = 0;
+
+			printf("%s\n", usrname);
+
 			break;
-		case 2:
-			*port = atoi(arg);
-			type = 0;
+
+		case 'p':
+			*port = atoi(optarg);
+
 			break;
+
+		case 'r':
+			mode = MODE_ROBOT;
+			break;
+
+		case 'h':
 		default:
-			abort();
+			exit(usage());
 		}
-		++i;
 	}
+
 	return got_usrname;
 }
 
@@ -217,7 +266,7 @@ int main(int argc, char **argv) {
 
 	printf(VCC_VERSION "\n");
 
-	got_usrname = parse_args(argv, &port, &ip);
+	got_usrname = parse_args(argc, argv, &port, &ip);
 	init_socket(ip, port);
 	printf("Connected to server. \n");