commit 1d85b767bee1ecfbb90bf78ad3a990de84b0fb25
parent 7445d3804d48a3cd0e1f2348dea8c354a9df3b49
Author: Christoph Lohmann <20h@r-36.net>
Date: Sat, 4 Aug 2012 22:16:33 +0200
Adding more flexible ARGBEGIN parameter handling.
Diffstat:
3 files changed, 61 insertions(+), 26 deletions(-)
diff --git a/Makefile b/Makefile
@@ -39,7 +39,7 @@ clean:
dist: clean
@echo creating dist tarball
@mkdir -p ${NAME}-${VERSION}
- @cp -R LICENSE LICENSE.orig ${NAME}.1 Makefile config.mk \
+ @cp -R LICENSE LICENSE.orig arg.h ${NAME}.1 Makefile config.mk \
${SRC} *.h ${NAME}-${VERSION}
@tar -cf ${NAME}-${VERSION}.tar ${NAME}-${VERSION}
@gzip ${NAME}-${VERSION}.tar
diff --git a/arg.h b/arg.h
@@ -0,0 +1,41 @@
+/*
+ * Copy me if you can.
+ * by 20h
+ */
+
+#ifndef __ARG_H__
+#define __ARG_H__
+
+extern char *argv0;
+
+#define USED(x) ((void)(x))
+
+#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
+ argv[0] && argv[0][1]\
+ && argv[0][0] == '-';\
+ argc--, argv++) {\
+ char _argc;\
+ char **_argv;\
+ if (argv[0][1] == '-' && argv[0][2] == '\0') {\
+ argv++;\
+ argc--;\
+ break;\
+ }\
+ for (argv[0]++, _argv = argv; argv[0][0];\
+ argv[0]++) {\
+ if (_argv != argv)\
+ break;\
+ _argc = argv[0][0];\
+ switch (_argc)
+
+#define ARGEND }\
+ USED(_argc);\
+ }\
+ USED(argv);\
+ USED(argc);
+
+#define EARGF(x) ((argv[1] == NULL)? ((x), abort(), (char *)0) :\
+ (argc--, argv++, argv[0]))
+
+#endif
+
diff --git a/thinglaunch.c b/thinglaunch.c
@@ -20,6 +20,7 @@
#include <libgen.h>
#include <wchar.h>
+#include "arg.h"
#include "config.h"
unsigned long getcolor(const char *colstr);
@@ -46,6 +47,8 @@ int screen, issecret = 0, tostdout = 0;
unsigned long fgcol, bgcol;
static char *name = "thinglaunch";
+char *argv0;
+
#define MAXCMD 255
#define WINWIDTH 640
#define WINHEIGHT 25
@@ -56,7 +59,7 @@ wchar_t secret[MAXCMD+1];
char cbuf[MAXCMD*4+1];
void
-usage(char *argv0)
+usage(void)
{
fprintf(stderr, "usage: %s [-hos] [-p prompt]\n", argv0);
exit(1);
@@ -65,8 +68,6 @@ usage(char *argv0)
int
main(int argc, char *argv[])
{
- int i;
-
if (strstr(argv[0], "thingaskpass")) {
issecret = 1;
tostdout = 1;
@@ -83,28 +84,21 @@ main(int argc, char *argv[])
}
if (argc > 1) {
- for (i = 1; argv[i]; i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'o':
- tostdout = 1;
- break;
- case 's':
- issecret = 1;
- break;
- case 'p':
- if (!argv[i+1])
- usage(argv[0]);
- prompt = argv[i+1];
- i++;
- break;
- default:
- case 'h':
- usage(argv[0]);
- break;
- }
- }
- }
+ ARGBEGIN {
+ case 'o':
+ tostdout = 1;
+ break;
+ case 's':
+ issecret = 1;
+ break;
+ case 'p':
+ prompt = EARGF(usage());
+ break;
+ default:
+ case 'h':
+ usage();
+ break;
+ } ARGEND;
}
bzero(command, sizeof(command));