gt: Add parsing of known options flags
authorPawel Szewczyk <p.szewczyk@samsung.com>
Tue, 26 Aug 2014 08:33:51 +0000 (10:33 +0200)
committerPawel Szewczyk <p.szewczyk@samsung.com>
Wed, 27 Aug 2014 10:01:57 +0000 (12:01 +0200)
Change-Id: Icfa666fbf9ecd36c5566db2344d5375035953663
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
source/base/include/common.h
source/base/include/parser.h
source/base/src/parser.c

index 6a3f01c..c962a69 100644 (file)
@@ -24,4 +24,6 @@ static inline void *zalloc(size_t size)
        return calloc(1, size);
 }
 
+#define ARRAY_SIZE(array) sizeof(array)/sizeof(*array)
+
 #endif //__GADGET_TOOL_COMMON_H__
index 3c2ffc8..77fec5c 100644 (file)
@@ -74,6 +74,28 @@ void gt_setting_cleanup(void *data);
 void gt_setting_list_cleanup(void *data);
 
 /**
+ * Definitions of options flags
+ */
+enum gt_option_flags {
+       GT_FORCE = 1,
+       GT_RECURSIVE = 2,
+       GT_VERBOSE = 4,
+       GT_OFF = 8,
+       GT_STDIN = 16,
+       GT_STDOUT = 32
+};
+
+/**
+ * @brief Get known options flags from given argv
+ * @param[out] optmask mask containing all found options
+ * @param[in] allowed_opts mask with allowed options
+ * @param[in] argc number of given arguments
+ * @param[in] argv list of arguments
+ * @return index of first non-option argument, negative number otherwise
+ **/
+int gt_get_options(int *optmask, int allowed_opts, int argc, char **argv);
+
+/**
  * @brief Split string in format <type>.<instance> into two strings
  * @param[out] type pointer to string with <type>
  * @param[out] instance pointer to string with <instance>
index 4490b72..a072ff9 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <ctype.h>
+
 
 int gt_global_help(void *data)
 {
@@ -150,3 +154,73 @@ int gt_parse_function_name(char **type, char **instance, const char *str)
 {
        return gt_split_by_char(type, instance, str, '.');
 }
+
+int gt_get_options(int *optmask, int allowed_opts, int argc, char **argv)
+{
+       static struct {
+               int flag;
+               struct option option;
+       } known_opts[] = {
+               {GT_FORCE, {"force", no_argument, 0, 'f'}},
+               {GT_VERBOSE, {"verbose", no_argument, 0, 'v'}},
+               {GT_RECURSIVE, {"recursive", no_argument, 0, 'r'}},
+               {GT_OFF, {"off", no_argument, 0, 'o'}},
+               {GT_STDIN, {"stdin", no_argument, 0, 1}},
+               {GT_STDIN, {"stdout", no_argument, 0, 2}},
+               {0, {NULL, 0, 0, 0}}
+       };
+
+       struct option opts[ARRAY_SIZE(known_opts)];
+       char optstring[ARRAY_SIZE(known_opts)];
+       int count = 0;
+       int short_count = 0;
+       int i;
+       int c;
+
+       for (i = 0; known_opts[i].flag; i++) {
+               if (allowed_opts & known_opts[i].flag) {
+                       opts[count] = known_opts[i].option;
+                       if (isalnum(opts[count].val))
+                               optstring[short_count++] = opts[count].val;
+                       count++;
+               }
+       }
+
+       /* Assign last element, which must be zero-filled */
+       opts[count] = known_opts[i].option;
+       optstring[short_count] = '\0';
+
+       *optmask = 0;
+       argc++;
+       argv--;
+       while (1) {
+               int opt_index = 0;
+               c = getopt_long(argc, argv, optstring, opts, &opt_index);
+               if (c == -1)
+                       break;
+               switch (c) {
+               case 'f':
+                       *optmask |= GT_FORCE;
+                       break;
+               case 'r':
+                       *optmask |= GT_RECURSIVE;
+                       break;
+               case 'v':
+                       *optmask |= GT_VERBOSE;
+                       break;
+               case 'o':
+                       *optmask |= GT_OFF;
+                       break;
+               case 1:
+                       *optmask |= GT_STDIN;
+                       break;
+               case 2:
+                       *optmask |= GT_STDOUT;
+                       break;
+               default:
+                       return -1;
+               }
+       }
+
+       return optind-1;
+}