commit 8ad8815654b9fcac4caee27f11b3af6ce336a5ac
parent 2db1fb806740d79cf6c48a2a2e18e810741c766f
Author: Claudio Alessi <smoppy@gmail.com>
Date: Thu, 3 Mar 2016 20:01:20 +0100
Add command line arguments.
Diffstat:
A | arg.h | | | 41 | +++++++++++++++++++++++++++++++++++++++++ |
M | core.c | | | 23 | +++++++++++++++++++++-- |
2 files changed, 62 insertions(+), 2 deletions(-)
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/core.c b/core.c
@@ -12,13 +12,15 @@
#include <string.h>
#include <stdlib.h>
#include <signal.h>
-#include <curses.h> /* For curs_set() */
#include <mysql.h>
#include <stfl.h>
-
#include <langinfo.h>
#include <locale.h>
+#include <curses.h> /* For curs_set() */
+
+#include "arg.h"
+char *argv0;
#define LENGTH(X) (sizeof X / sizeof X[0])
#define QUOTE(S) (stfl_ipool_fromwc(ipool, stfl_quote(stfl_ipool_towc(ipool, S))))
@@ -529,6 +531,11 @@ text(void) {
}
void
+usage(void) {
+ die("Usage: %s [-hup <arg>]\n", argv0);
+}
+
+void
usedb(const Arg *arg) {
Item *item = getitem();
mysql_select_db(mysql, item->fields[0]);
@@ -548,6 +555,18 @@ viewprev(const Arg *arg) {
int
main(int argc, char **argv) {
+ ARGBEGIN {
+ case 'h':
+ dbhost = EARGF(usage());
+ break;
+ case 'u':
+ dbuser = EARGF(usage());
+ break;
+ case 'p':
+ dbpass = EARGF(usage());
+ break;
+ } ARGEND;
+
setup();
run();
cleanup();