[hackers] [slock][PATCH] Remove arg.h, simplify option parsing

From: Klemens Nanni <kl3_AT_posteo.org>
Date: Fri, 28 Oct 2016 22:56:14 +0200

arg.h is really ugly code and way to complex for tools like slock that
have such minimal synopsis.

Regardless of that, usage() does not have to be a function of it's own
if it's called just once.

This (hopefully) is the final commit implementing proper command-line
pargsing adhering to the POSIX Utility Syntax Guidelines[0] after
submitting two incomplete patches to the hackers_AT_suckless mailinglist.

Thanks to quinq and emg for pointing out this out.

0: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02
---
 arg.h   | 65 -----------------------------------------------------------------
 slock.c | 32 ++++++++++++++------------------
 2 files changed, 14 insertions(+), 83 deletions(-)
 delete mode 100644 arg.h
diff --git a/arg.h b/arg.h
deleted file mode 100644
index 0b23c53..0000000
--- a/arg.h
+++ /dev/null
_AT_@ -1,65 +0,0 @@
-/*
- * 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][0] == '-'\
-					&& argv[0][1];\
-					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()	(brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
-
-#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])))
-
-#define LNGARG()	&argv[0][0]
-
-#endif
diff --git a/slock.c b/slock.c
index ad539dc..283b04e 100644
--- a/slock.c
+++ b/slock.c
_AT_@ -19,11 +19,8 @@
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 
-#include "arg.h"
 #include "util.h"
 
-char *argv0;
-
 enum {
 	INIT,
 	INPUT,
_AT_@ -289,14 +286,8 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
 	return NULL;
 }
 
-static void
-usage(void)
-{
-	die("usage: slock [-v] [cmd [arg ...]]\n");
-}
-
 int
-main(int argc, char **argv) {
+main(int argc, char *argv[]) {
 	struct xrandr rr;
 	struct lock **locks;
 	struct passwd *pwd;
_AT_@ -307,13 +298,17 @@ main(int argc, char **argv) {
 	Display *dpy;
 	int s, nlocks, nscreens;
 
-	ARGBEGIN {
-	case 'v':
-		fprintf(stderr, "slock-"VERSION"\n");
-		return 0;
-	default:
-		usage();
-	} ARGEND
+	if (argc > 1 && !strncmp("-", argv[1], 1)) {
+		if ((argc == 2 && !strcmp("-v", argv[1])) ||
+		    (argc == 3 && !strcmp("-v", argv[1]) && !strcmp("--", argv[2]))) {
+			fputs("slock-"VERSION"\n", stderr);
+			return 0;
+		} else if (!strcmp("--", argv[1])) {
+			--argc;
+			++argv;
+		} else
+			die("usage: slock [-v] [cmd [arg ...]]\n");
+	}
 
 	/* validate drop-user and -group */
 	errno = 0;
_AT_@ -367,13 +362,14 @@ main(int argc, char **argv) {
 		return 1;
 
 	/* run post-lock command */
-	if (argc > 0) {
+	if (argc > 1) {
 		switch (fork()) {
 		case -1:
 			die("slock: fork failed: %s\n", strerror(errno));
 		case 0:
 			if (close(ConnectionNumber(dpy)) < 0)
 				die("slock: close: %s\n", strerror(errno));
+			++argv;
 			execvp(argv[0], argv);
 			fprintf(stderr, "slock: execvp %s: %s\n", argv[0], strerror(errno));
 			_exit(1);
-- 
2.8.3
Received on Fri Oct 28 2016 - 22:56:14 CEST

This archive was generated by hypermail 2.3.0 : Fri Oct 28 2016 - 23:00:17 CEST