From 7489bc210520c027e95f0ee8c8df8d0805f50f67 Mon Sep 17 00:00:00 2001
From: Mattias Andrée <maandree@kth.se>
Date: Thu, 1 Dec 2016 13:41:15 +0100
Subject: Fix arg parsing
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Mattias Andrée <maandree@kth.se>
---
 src/cg-base.c       | 54 +++++++++++++++++++++++++++++++++++------------------
 src/cg-base.h       | 15 +++++----------
 src/cg-brilliance.c | 21 +++++----------------
 src/cg-darkroom.c   | 21 +++++----------------
 src/cg-gamma.c      | 21 +++++----------------
 src/cg-icc.c        | 21 +++++----------------
 src/cg-limits.c     | 19 ++++---------------
 src/cg-negative.c   | 21 +++++----------------
 src/cg-rainbow.c    | 22 ++++++----------------
 src/cg-sleepmode.c  | 22 ++++++----------------
 10 files changed, 82 insertions(+), 155 deletions(-)

diff --git a/src/cg-base.c b/src/cg-base.c
index e62f178..d75e70d 100644
--- a/src/cg-base.c
+++ b/src/cg-base.c
@@ -117,6 +117,19 @@ struct crtc_sort_data
 
 
 
+/**
+ * Compare two strings
+ * 
+ * @param   a  Return -1 if this string is `NULL` or less than `b`
+ * @param   b  Return +1 if this string is less than `a`
+ * @return     See `a` and `b`, 0 is returned if `a` and `b` are equal
+ */
+static int nulstrcmp(const char *a, const char *b)
+{
+  return (a == NULL) ? -1 : strcmp(a, b);
+}
+
+
 /**
  * Compare two instances of `crtc_sort_data`
  * 
@@ -550,6 +563,7 @@ int main(int argc, char* argv[])
   char* rule = NULL;
   char* class = default_class;
   int explicit_crtcs = 0;
+  int have_crtc_q = 0;
   
   argv0 = *argv++, argc--;
   
@@ -595,6 +609,8 @@ int main(int argc, char* argv[])
 		usage();
 	      crtcs[crtcs_i++] = arg;
 	      explicit_crtcs = 1;
+	      if (!have_crtc_q && !strcmp(arg, "?"))
+		have_crtc_q = 1;
 	    }
 	  else if (!strcmp(opt, "-p"))
 	    {
@@ -625,10 +641,13 @@ int main(int argc, char* argv[])
   
   crtcs_n = crtcs_i;
   crtcs[crtcs_i] = NULL;
-  if (handle_args(argc, argv, method, site, crtcs, prio, rule) < 0)
-    goto fail;
+  if (!have_crtc_q &&
+      nulstrcmp(prio, "?") && nulstrcmp(rule, "??") &&
+      nulstrcmp(rule, "?") && nulstrcmp(method, "?"))
+    if (handle_args(argc, argv, prio) < 0)
+      goto fail;
   
-  if ((prio != NULL) && !strcmp(prio, "?"))
+  if (!nulstrcmp(prio, "?"))
     {
       printf("%" PRIi64 "\n", priority);
       return 0;
@@ -636,12 +655,12 @@ int main(int argc, char* argv[])
   else if (prio != NULL)
     priority = (int64_t)atoll(prio);
   
-  if ((rule != NULL) && !strcmp(rule, "??"))
+  if (!nulstrcmp(rule, "??"))
     {
       printf("%s\n", class);
       return 0;
     }
-  else if ((rule != NULL) && !strcmp(rule, "?"))
+  else if (!nulstrcmp(rule, "?"))
     {
       printf("%s\n", strstr(strstr(class, "::") + 2, "::") + 2);
       return 0;
@@ -660,7 +679,7 @@ int main(int argc, char* argv[])
 	}
     }
   
-  if ((method != NULL) && !strcmp(method, "?"))
+  if (!nulstrcmp(method, "?"))
     {
       if (list_methods() < 0)
 	goto fail;
@@ -676,18 +695,17 @@ int main(int argc, char* argv[])
       goto custom_fail;
     }
   stage++;
-  
-  while (crtcs_i--)
-    if (!strcmp(crtcs[crtcs_i], "?"))
-      switch (list_crtcs())
-	{
-	case 0:
-	  goto done;
-	case -1:
-	  goto fail;
-	default:
-	  goto cg_fail;
-	}
+
+  if (have_crtc_q)
+    switch (list_crtcs())
+      {
+      case 0:
+	goto done;
+      case -1:
+	goto fail;
+      default:
+	goto cg_fail;
+      }
   
   if (crtcs_n == 0)
     {
diff --git a/src/cg-base.h b/src/cg-base.h
index 8d39f4a..713c9df 100644
--- a/src/cg-base.h
+++ b/src/cg-base.h
@@ -206,20 +206,15 @@ extern int handle_opt(char* opt, char* arg);
  * This function is called after the last
  * call to `handle_opt`
  * 
- * @param   argc    The number of unparsed arguments
- * @param   argv    `NULL` terminated list of unparsed arguments
- * @param   method  The argument associated with the "-M" option
- * @param   site    The argument associated with the "-S" option
- * @param   crtcs   The arguments associated with the "-c" options, `NULL`-terminated
- * @param   prio    The argument associated with the "-p" option
- * @param   rule    The argument associated with the "-R" option
- * @return          Zero on success, -1 on error
+ * @param   argc  The number of unparsed arguments
+ * @param   argv  `NULL` terminated list of unparsed arguments
+ * @param   prio  The argument associated with the "-p" option
+ * @return        Zero on success, -1 on error
  */
 #if defined(__GNUC__)
 __attribute__((__nonnull__(2)))
 #endif
-extern int handle_args(int argc, char* argv[], char* method, char* site, char** crtcs,
-		       char* prio, char* rule);
+extern int handle_args(int argc, char* argv[], char* prio);
 
 /**
  * The main function for the program-specific code
diff --git a/src/cg-brilliance.c b/src/cg-brilliance.c
index 2039cdc..0d5c930 100644
--- a/src/cg-brilliance.c
+++ b/src/cg-brilliance.c
@@ -142,27 +142,17 @@ static int parse_double(double* restrict out, const char* restrict str)
  * This function is called after the last
  * call to `handle_opt`
  * 
- * @param   argc    The number of unparsed arguments
- * @param   argv    `NULL` terminated list of unparsed arguments
- * @param   method  The argument associated with the "-M" option
- * @param   site    The argument associated with the "-S" option
- * @param   crtcs_  The arguments associated with the "-c" options, `NULL`-terminated
- * @param   prio    The argument associated with the "-p" option
- * @param   rule    The argument associated with the "-R" option
- * @return          Zero on success, -1 on error
+ * @param   argc  The number of unparsed arguments
+ * @param   argv  `NULL` terminated list of unparsed arguments
+ * @param   prio  The argument associated with the "-p" option
+ * @return        Zero on success, -1 on error
  */
-int handle_args(int argc, char* argv[], char* method, char* site,
-		char** crtcs_, char* prio, char* rule)
+int handle_args(int argc, char* argv[], char* prio)
 {
   char* red;
   char* green;
   char* blue;
   int q = xflag + dflag;
-  q += (method != NULL) &&  !strcmp(method, "?");
-  q += (prio   != NULL) &&  !strcmp(prio, "?");
-  q += (rule   != NULL) && (!strcmp(rule, "?") || !strcmp(rule, "??"));
-  for (; *crtcs_; crtcs_++)
-    q += !strcmp(*crtcs_, "?");
   if ((q > 1) || (xflag && (prio != NULL || argc)))
     usage();
   if (argc == 1)
@@ -185,7 +175,6 @@ int handle_args(int argc, char* argv[], char* method, char* site,
 	usage();
     }
   return 0;
-  (void) site;
 }
 
 
diff --git a/src/cg-darkroom.c b/src/cg-darkroom.c
index e9e1b2a..03222a7 100644
--- a/src/cg-darkroom.c
+++ b/src/cg-darkroom.c
@@ -132,24 +132,14 @@ static int parse_double(double* restrict out, const char* restrict str)
  * This function is called after the last
  * call to `handle_opt`
  * 
- * @param   argc    The number of unparsed arguments
- * @param   argv    `NULL` terminated list of unparsed arguments
- * @param   method  The argument associated with the "-M" option
- * @param   site    The argument associated with the "-S" option
- * @param   crtcs_  The arguments associated with the "-c" options, `NULL`-terminated
- * @param   prio    The argument associated with the "-p" option
- * @param   rule    The argument associated with the "-R" option
- * @return          Zero on success, -1 on error
+ * @param   argc  The number of unparsed arguments
+ * @param   argv  `NULL` terminated list of unparsed arguments
+ * @param   prio  The argument associated with the "-p" option
+ * @return        Zero on success, -1 on error
  */
-int handle_args(int argc, char* argv[], char* method, char* site,
-		char** crtcs_, char* prio, char* rule)
+int handle_args(int argc, char* argv[], char* prio)
 {
   int q = xflag + dflag;
-  q += (method != NULL) &&  !strcmp(method, "?");
-  q += (prio   != NULL) &&  !strcmp(prio, "?");
-  q += (rule   != NULL) && (!strcmp(rule, "?") || !strcmp(rule, "??"));
-  for (; *crtcs_; crtcs_++)
-    q += !strcmp(*crtcs_, "?");
   if ((q > 1) || (xflag && (prio != NULL || argc)))
     usage();
   if (argc == 1)
@@ -160,7 +150,6 @@ int handle_args(int argc, char* argv[], char* method, char* site,
   else if (argc)
     usage();
   return 0;
-  (void) site;
 }
 
 
diff --git a/src/cg-gamma.c b/src/cg-gamma.c
index 2894f71..b72cd21 100644
--- a/src/cg-gamma.c
+++ b/src/cg-gamma.c
@@ -372,25 +372,15 @@ static int parse_gamma_file(const char* restrict pathname)
  * This function is called after the last
  * call to `handle_opt`
  * 
- * @param   argc    The number of unparsed arguments
- * @param   argv    `NULL` terminated list of unparsed arguments
- * @param   method  The argument associated with the "-M" option
- * @param   site    The argument associated with the "-S" option
- * @param   crtcs_  The arguments associated with the "-c" options, `NULL`-terminated
- * @param   prio    The argument associated with the "-p" option
- * @param   rule    The argument associated with the "-R" option
- * @return          Zero on success, -1 on error
+ * @param   argc  The number of unparsed arguments
+ * @param   argv  `NULL` terminated list of unparsed arguments
+ * @param   prio  The argument associated with the "-p" option
+ * @return        Zero on success, -1 on error
  */
-int handle_args(int argc, char* argv[], char* method, char* site,
-		char** crtcs_, char* prio, char* rule)
+int handle_args(int argc, char* argv[], char* prio)
 {
   int free_fflag = 0, saved_errno;
   int q = xflag + dflag;
-  q += (method != NULL) &&  !strcmp(method, "?");
-  q += (prio   != NULL) &&  !strcmp(prio, "?");
-  q += (rule   != NULL) && (!strcmp(rule, "?") || !strcmp(rule, "??"));
-  for (; *crtcs_; crtcs_++)
-    q += !strcmp(*crtcs_, "?");
   if ((q > 1) || (fflag && argc) || (xflag && ((fflag != NULL) || (argc > 0) || (prio != NULL))))
     usage();
   if (argc == 1)
@@ -429,7 +419,6 @@ int handle_args(int argc, char* argv[], char* method, char* site,
     free(fflag), fflag = NULL;
   errno = saved_errno;
   return cleanup(-1);
-  (void) site;
 }
 
 
diff --git a/src/cg-icc.c b/src/cg-icc.c
index d3dffe9..5e56acf 100644
--- a/src/cg-icc.c
+++ b/src/cg-icc.c
@@ -313,27 +313,17 @@ static int load_icc_table(int fd, const char *dirname)
  * This function is called after the last
  * call to `handle_opt`
  * 
- * @param   argc    The number of unparsed arguments
- * @param   argv    `NULL` terminated list of unparsed arguments
- * @param   method  The argument associated with the "-M" option
- * @param   site    The argument associated with the "-S" option
- * @param   crtcs_  The arguments associated with the "-c" options, `NULL`-terminated
- * @param   prio    The argument associated with the "-p" option
- * @param   rule    The argument associated with the "-R" option
- * @return          Zero on success, -1 on error
+ * @param   argc  The number of unparsed arguments
+ * @param   argv  `NULL` terminated list of unparsed arguments
+ * @param   prio  The argument associated with the "-p" option
+ * @return        Zero on success, -1 on error
  */
-int handle_args(int argc, char* argv[], char* method, char* site,
-		char** crtcs_, char* prio, char* rule)
+int handle_args(int argc, char* argv[], char* prio)
 {
   struct passwd* pw;
   char* path = NULL;
   int saved_errno;
   int fd = -1, q = xflag + dflag;
-  q += (method != NULL) &&  !strcmp(method, "?");
-  q += (prio   != NULL) &&  !strcmp(prio, "?");
-  q += (rule   != NULL) && (!strcmp(rule, "?") || !strcmp(rule, "??"));
-  for (; *crtcs_; crtcs_++)
-    q += !strcmp(*crtcs_, "?");
   if ((q > 1) || (xflag && ((argc > 0) || (prio != NULL))) || (argc > 1))
     usage();
   icc_pathname = *argv;
@@ -375,7 +365,6 @@ int handle_args(int argc, char* argv[], char* method, char* site,
     close(fd);
   errno = saved_errno;
   return cleanup(-1);
-  (void) site;
 }
 
 
diff --git a/src/cg-limits.c b/src/cg-limits.c
index 5df54e3..78f0b6c 100644
--- a/src/cg-limits.c
+++ b/src/cg-limits.c
@@ -481,25 +481,15 @@ static int parse_contrast_file(const char* restrict pathname)
  * This function is called after the last
  * call to `handle_opt`
  * 
- * @param   argc    The number of unparsed arguments
- * @param   argv    `NULL` terminated list of unparsed arguments
- * @param   method  The argument associated with the "-M" option
- * @param   site    The argument associated with the "-S" option
- * @param   crtcs_  The arguments associated with the "-c" options, `NULL`-terminated
- * @param   prio    The argument associated with the "-p" option
- * @param   rule    The argument associated with the "-R" option
+ * @param   argc  The number of unparsed arguments
+ * @param   argv  `NULL` terminated list of unparsed arguments
+ * @param   prio  The argument associated with the "-p" option
  * @return          Zero on success, -1 on error
  */
-int handle_args(int argc, char* argv[], char* method, char* site,
-		char** crtcs_, char* prio, char* rule)
+int handle_args(int argc, char* argv[], char* prio)
 {
   int free_Bflag = 0, free_Cflag = 0, saved_errno;
   int q = xflag + dflag;
-  q += (method != NULL) &&  !strcmp(method, "?");
-  q += (prio   != NULL) &&  !strcmp(prio, "?");
-  q += (rule   != NULL) && (!strcmp(rule, "?") || !strcmp(rule, "??"));
-  for (; *crtcs_; crtcs_++)
-    q += !strcmp(*crtcs_, "?");
   if ((q > 1) || (xflag && ((Bflag != NULL) || (Cflag != NULL) || (argc > 0) || (prio != NULL))))
     usage();
   if ((Bflag || Cflag) && argc)
@@ -559,7 +549,6 @@ int handle_args(int argc, char* argv[], char* method, char* site,
     free(Cflag), Cflag = NULL;
   errno = saved_errno;
   return cleanup(-1);
-  (void) site;
 }
 
 
diff --git a/src/cg-negative.c b/src/cg-negative.c
index 73f7e8c..176d8db 100644
--- a/src/cg-negative.c
+++ b/src/cg-negative.c
@@ -140,29 +140,18 @@ int handle_opt(char* opt, char* arg)
  * This function is called after the last
  * call to `handle_opt`
  * 
- * @param   argc    The number of unparsed arguments
- * @param   argv    `NULL` terminated list of unparsed arguments
- * @param   method  The argument associated with the "-M" option
- * @param   site    The argument associated with the "-S" option
- * @param   crtcs_  The arguments associated with the "-c" options, `NULL`-terminated
- * @param   prio    The argument associated with the "-p" option
- * @param   rule    The argument associated with the "-R" option
- * @return          Zero on success, -1 on error
+ * @param   argc  The number of unparsed arguments
+ * @param   argv  `NULL` terminated list of unparsed arguments
+ * @param   prio  The argument associated with the "-p" option
+ * @return        Zero on success, -1 on error
  */
-int handle_args(int argc, char* argv[], char* method, char* site,
-		char** crtcs_, char* prio, char* rule)
+int handle_args(int argc, char* argv[], char* prio)
 {
   int q = xflag + (dflag | rplus | gplus | bplus);
-  q += (method != NULL) &&  !strcmp(method, "?");
-  q += (prio   != NULL) &&  !strcmp(prio, "?");
-  q += (rule   != NULL) && (!strcmp(rule, "?") || !strcmp(rule, "??"));
-  for (; *crtcs_; crtcs_++)
-    q += !strcmp(*crtcs_, "?");
   if (argc || (q > 1) || (xflag && (prio != NULL)))
     usage();
   return 0;
   (void) argv;
-  (void) site;
 }
 
 
diff --git a/src/cg-rainbow.c b/src/cg-rainbow.c
index 5f2a117..8aaa2cb 100644
--- a/src/cg-rainbow.c
+++ b/src/cg-rainbow.c
@@ -137,24 +137,14 @@ static int parse_double(double* restrict out, const char* restrict str)
  * This function is called after the last
  * call to `handle_opt`
  * 
- * @param   argc    The number of unparsed arguments
- * @param   argv    `NULL` terminated list of unparsed arguments
- * @param   method  The argument associated with the "-M" option
- * @param   site    The argument associated with the "-S" option
- * @param   crtcs_  The arguments associated with the "-c" options, `NULL`-terminated
- * @param   prio    The argument associated with the "-p" option
- * @param   rule    The argument associated with the "-R" option
- * @return          Zero on success, -1 on error
+ * @param   argc  The number of unparsed arguments
+ * @param   argv  `NULL` terminated list of unparsed arguments
+ * @param   prio  The argument associated with the "-p" option
+ * @return        Zero on success, -1 on error
  */
-int handle_args(int argc, char* argv[], char* method, char* site,
-		char** crtcs_, char* prio, char* rule)
+int handle_args(int argc, char* argv[], char* prio)
 {
   int q = (lflag || sflag);
-  q += (method != NULL) &&  !strcmp(method, "?");
-  q += (prio   != NULL) &&  !strcmp(prio, "?");
-  q += (rule   != NULL) && (!strcmp(rule, "?") || !strcmp(rule, "??"));
-  for (; *crtcs_; crtcs_++)
-    q += !strcmp(*crtcs_, "?");
   if ((q > 1) || argc)
     usage();
   if (sflag != NULL)
@@ -169,8 +159,8 @@ int handle_args(int argc, char* argv[], char* method, char* site,
 	usage();
     }
   return 0;
-  (void) site;
   (void) argv;
+  (void) prio;
 }
 
 
diff --git a/src/cg-sleepmode.c b/src/cg-sleepmode.c
index 757b7ef..42fea0d 100644
--- a/src/cg-sleepmode.c
+++ b/src/cg-sleepmode.c
@@ -188,24 +188,14 @@ static int parse_double(double* restrict out, const char* restrict str)
  * This function is called after the last
  * call to `handle_opt`
  * 
- * @param   argc    The number of unparsed arguments
- * @param   argv    `NULL` terminated list of unparsed arguments
- * @param   method  The argument associated with the "-M" option
- * @param   site    The argument associated with the "-S" option
- * @param   crtcs_  The arguments associated with the "-c" options, `NULL`-terminated
- * @param   prio    The argument associated with the "-p" option
- * @param   rule    The argument associated with the "-R" option
- * @return          Zero on success, -1 on error
+ * @param   argc  The number of unparsed arguments
+ * @param   argv  `NULL` terminated list of unparsed arguments
+ * @param   prio  The argument associated with the "-p" option
+ * @return        Zero on success, -1 on error
  */
-int handle_args(int argc, char* argv[], char* method, char* site,
-		char** crtcs_, char* prio, char* rule)
+int handle_args(int argc, char* argv[], char* prio)
 {
   int q = (rflag || gflag || bflag || argc);
-  q += (method != NULL) &&  !strcmp(method, "?");
-  q += (prio   != NULL) &&  !strcmp(prio, "?");
-  q += (rule   != NULL) && (!strcmp(rule, "?") || !strcmp(rule, "??"));
-  for (; *crtcs_; crtcs_++)
-    q += !strcmp(*crtcs_, "?");
   if ((q > 1) || (argc > 3))
     usage();
   if (rflag != NULL)
@@ -233,7 +223,7 @@ int handle_args(int argc, char* argv[], char* method, char* site,
   if (blue_target >= 1)
     blue_time = 0;
   return 0;
-  (void) site;
+  (void) prio;
 }
 
 
-- 
cgit v1.2.3-70-g09d2