[PATCH] Use arg.h + few stylistic changes

From: sin <sin_AT_2f30.org>
Date: Wed, 12 Nov 2014 16:03:09 +0000

---
 arg.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 sic.c | 64 +++++++++++++++++++++++++++++++++++-----------------------------
 2 files changed, 98 insertions(+), 29 deletions(-)
 create mode 100644 arg.h
diff --git a/arg.h b/arg.h
new file mode 100644
index 0000000..4df77a7
--- /dev/null
+++ b/arg.h
_AT_@ -0,0 +1,63 @@
+/*
+ * Copy me if you can.
+ * by 20h
+ */
+
+#ifndef ARG_H__
+#define ARG_H__
+
+extern char *argv0;
+
+/* use main(int argc, char *argv[]) */
+#define ARGBEGIN	for (argv0 = *argv, argv++, argc--;\
+					argv[0] && argv[0][1]\
+					&& argv[0][0] == '-';\
+					argc--, argv++) {\
+				char argc_;\
+				char **argv_;\
+				int brk_;\
+				if (argv[0][1] == '-' && argv[0][2] == '\0') {\
+					argv++;\
+					argc--;\
+					break;\
+				}\
+				for (brk_ = 0, argv[0]++, argv_ = argv;\
+						argv[0][0] && !brk_;\
+						argv[0]++) {\
+					if (argv_ != argv)\
+						break;\
+					argc_ = argv[0][0];\
+					switch (argc_)
+
+/* Handles obsolete -NUM syntax */
+#define ARGNUM				case '0':\
+					case '1':\
+					case '2':\
+					case '3':\
+					case '4':\
+					case '5':\
+					case '6':\
+					case '7':\
+					case '8':\
+					case '9'
+
+#define ARGEND			}\
+			}
+
+#define ARGC()		argc_
+
+#define ARGNUMF(base)	(brk_ = 1, estrtol(argv[0], (base)))
+
+#define EARGF(x)	((argv[0][1] == '\0' && argv[1] == NULL)?\
+				((x), abort(), (char *)0) :\
+				(brk_ = 1, (argv[0][1] != '\0')?\
+					(&argv[0][1]) :\
+					(argc--, argv++, argv[0])))
+
+#define ARGF()		((argv[0][1] == '\0' && argv[1] == NULL)?\
+				(char *)0 :\
+				(brk_ = 1, (argv[0][1] != '\0')?\
+					(&argv[0][1]) :\
+					(argc--, argv++, argv[0])))
+
+#endif
diff --git a/sic.c b/sic.c
index e91a426..7ef7491 100644
--- a/sic.c
+++ b/sic.c
_AT_@ -8,8 +8,10 @@
 #include <time.h>
 #include <unistd.h>
 
+#include "arg.h"
 #include "config.h"
 
+char *argv0;
 static char *host = DEFAULT_HOST;
 static char *port = DEFAULT_PORT;
 static char *password;
_AT_@ -133,40 +135,44 @@ parsesrv(char *cmd) {
 	}
 }
 
+static void
+usage(void)
+{
+	eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n", argv0);
+}
+
 int
 main(int argc, char *argv[]) {
-	int i, c;
 	struct timeval tv;
 	const char *user = getenv("USER");
+	int n;
 	fd_set rd;
 
 	strlcpy(nick, user ? user : "unknown", sizeof nick);
-	for(i = 1; i < argc; i++) {
-		c = argv[i][1];
-		if(argv[i][0] != '-' || argv[i][2])
-			c = -1;
-		switch(c) {
-		case 'h':
-			if(++i < argc) host = argv[i];
-			break;
-		case 'p':
-			if(++i < argc) port = argv[i];
-			break;
-		case 'n':
-			if(++i < argc) strlcpy(nick, argv[i], sizeof nick);
-			break;
-		case 'k':
-			if(++i < argc) password = argv[i];
-			break;
-		case 'v':
-			eprint("sic-"VERSION", © 2005-2012 Kris Maglione, Anselm R. Garbe, Nico Golde\n");
-		default:
-			eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n");
-		}
-	}
+	ARGBEGIN {
+	case 'h':
+		host = EARGF(usage());
+		break;
+	case 'p':
+		port = EARGF(usage());
+		break;
+	case 'n':
+		strlcpy(nick, EARGF(usage()), sizeof nick);
+		break;
+	case 'k':
+		password = EARGF(usage());
+		break;
+	case 'v':
+		eprint("sic-"VERSION", © 2005-2014 Kris Maglione, Anselm R. Garbe, Nico Golde\n");
+		break;
+	default:
+		usage();
+	} ARGEND;
+
 	/* init */
-	i = dial(host, port);
-	srv = fdopen(i, "r+");
+	srv = fdopen(dial(host, port), "r+");
+	if (!srv)
+		eprint("fdopen:");
 	/* login */
 	if(password)
 		sout("PASS %s", password);
_AT_@ -181,13 +187,13 @@ main(int argc, char *argv[]) {
 		FD_SET(fileno(srv), &rd);
 		tv.tv_sec = 120;
 		tv.tv_usec = 0;
-		i = select(fileno(srv) + 1, &rd, 0, 0, &tv);
-		if(i < 0) {
+		n = select(fileno(srv) + 1, &rd, 0, 0, &tv);
+		if(n < 0) {
 			if(errno == EINTR)
 				continue;
 			eprint("sic: error on select():");
 		}
-		else if(i == 0) {
+		else if(n == 0) {
 			if(time(NULL) - trespond >= 300)
 				eprint("sic shutting down: parse timeout\n");
 			sout("PING %s", host);
-- 
1.8.4
--zhXaljGHf11kAtnf--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Wed Nov 12 2014 - 17:24:04 CET