aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-07-10 23:18:59 +0200
committerMattias Andrée <maandree@kth.se>2016-07-10 23:18:59 +0200
commit4e794f661261994c8247a2f399c371aad9c71f7a (patch)
tree609722081f17ba4907d4edfb7e4e91beb3e475fa
parentAdd adjustment method name parser (diff)
downloadcoopgammad-4e794f661261994c8247a2f399c371aad9c71f7a.tar.gz
coopgammad-4e794f661261994c8247a2f399c371aad9c71f7a.tar.bz2
coopgammad-4e794f661261994c8247a2f399c371aad9c71f7a.tar.xz
Add command line parser
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--src/arg.h65
-rw-r--r--src/gammad.c42
-rw-r--r--src/ramps.c2
3 files changed, 102 insertions, 7 deletions
diff --git a/src/arg.h b/src/arg.h
new file mode 100644
index 0000000..0b23c53
--- /dev/null
+++ b/src/arg.h
@@ -0,0 +1,65 @@
+/*
+ * 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/src/gammad.c b/src/gammad.c
index 15e8359..97f2d56 100644
--- a/src/gammad.c
+++ b/src/gammad.c
@@ -24,6 +24,7 @@
#include <string.h>
#include <unistd.h>
+#include "arg.h"
#include "output.h"
#include "util.h"
@@ -32,7 +33,7 @@
/**
* The name of the process
*/
-const char* argv0;
+char* argv0;
@@ -161,25 +162,54 @@ static char* get_crtc_name(libgamma_crtc_information_t* info, libgamma_crtc_stat
}
+/**
+ * Print usage information and exit
+ */
+static void usage(void)
+{
+ printf("Usage: %s [-m method] [-s site] [-p]\n", argv0);
+ exit(1);
+}
+
+
int main(int argc, char** argv)
{
- int method, gerror, rc = 1;
+ int method = -1, gerror, rc = 1, preserve = 0;
+ char *sitename = NULL;
libgamma_site_state_t site;
libgamma_partition_state_t* partitions = NULL;
libgamma_crtc_state_t* crtcs = NULL;
struct output* outputs = NULL;
size_t i, j, n, n0, crtcs_n = 0;
- argv0 = argc ? argv[0] : "gammad";
-
memset(&site, 0, sizeof(site));
+ ARGBEGIN
+ {
+ case 's':
+ sitename = EARGF(usage());
+ break;
+ case 'm':
+ method = get_method(EARGF(usage()));
+ if (method < 0)
+ goto fail;
+ break;
+ case 'p':
+ preserve = 1;
+ break;
+ default:
+ usage();
+ }
+ ARGEND;
+ if (argc > 0)
+ usage();
+
/* Get method */
- if (libgamma_list_methods(&method, 1, 0) < 1)
+ if ((method < 0) && (libgamma_list_methods(&method, 1, 0) < 1))
return fprintf(stderr, "%s: no adjustment method available\n", argv0), 1;
/* Get site */
- if ((gerror = libgamma_site_initialise(&site, method, NULL)))
+ if ((gerror = libgamma_site_initialise(&site, method, sitename)))
goto fail_libgamma;
/* Get partitions */
diff --git a/src/ramps.c b/src/ramps.c
index 0bd2b78..2c24b52 100644
--- a/src/ramps.c
+++ b/src/ramps.c
@@ -26,7 +26,7 @@
/**
* The name of the process
*/
-extern const char* argv0;
+extern char* argv0;