]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include: [strutils.c] add list parsers
authorKarel Zak <kzak@redhat.com>
Wed, 27 Jul 2011 14:41:21 +0000 (16:41 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 27 Jul 2011 14:41:21 +0000 (16:41 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
lib/strutils.c
lib/tt.c
misc-utils/findmnt.c
misc-utils/lsblk.c
partx/partx.c
sys-utils/dmesg.c

index aa5ea7cc8c88cdb9297df7ca178950c03bac624b..08ba09c01c6faaf1f3fc2ed879ed5a9cc67ff769 100644 (file)
@@ -39,4 +39,9 @@ enum
 
 extern char *size_to_human_string(int options, uint64_t bytes);
 
+extern int string_to_idarray(const char *list, int ary[], size_t arysz,
+                          int (name2id)(const char *, size_t));
+extern int string_to_bitarray(const char *list, char *ary,
+                           int (*name2bit)(const char *, size_t));
+
 #endif
index e7a2454a03d9d6533acdc152e2265c12fbf4aeb3..31a487228ce1dbb0105a6f89f5408fad5576af0f 100644 (file)
 #include <sys/stat.h>
 #include <locale.h>
 #include <string.h>
+
 #include "c.h"
 #include "strutils.h"
+#include "bitops.h"
 
 static int do_scale_by_power (uintmax_t *x, int base, int power)
 {
@@ -341,6 +343,99 @@ char *size_to_human_string(int options, uint64_t bytes)
        return strdup(buf);
 }
 
+/*
+ * Parses comma delimited list to array with IDs, for example:
+ *
+ * "aaa,bbb,ccc" --> ary[0] = FOO_AAA;
+ *                   ary[1] = FOO_BBB;
+ *                   ary[3] = FOO_CCC;
+ *
+ * The function name2id() provides conversion from string to ID.
+ *
+ * Returns: >= 0  : number of items added to ary[]
+ *            -1  : parse error or unknown item
+ *            -2  : arysz reached
+ */
+int string_to_idarray(const char *list, int ary[], size_t arysz,
+                       int (name2id)(const char *, size_t))
+{
+       const char *begin = NULL, *p;
+       int n = 0;
+
+       if (!list || !*list || !ary || !arysz || !name2id)
+               return -1;
+
+       for (p = list; p && *p; p++) {
+               const char *end = NULL;
+               int id;
+
+               if (!begin)
+                       begin = p;              /* begin of the column name */
+               if (*p == ',')
+                       end = p;                /* terminate the name */
+               if (*(p + 1) == '\0')
+                       end = p + 1;            /* end of string */
+               if (!begin || !end)
+                       continue;
+               if (end <= begin)
+                       return -1;
+
+               id = name2id(begin, end - begin);
+               if (id == -1)
+                       return -1;
+               ary[ n++ ] = id;
+               if (n >= arysz)
+                       return -2;
+               begin = NULL;
+               if (end && !*end)
+                       break;
+       }
+       return n;
+}
+
+/*
+ * LIST ::= <item> [, <item>]
+ *
+ * The <item> is translated to 'id' by name2id() function and the 'id' is used
+ * as a possition in the 'ary' bit array. It means that the 'id' has to be in
+ * range <0..N> where N < sizeof(ary) * NBBY.
+ *
+ * Returns: 0 on sucess, <0 on error.
+ */
+int string_to_bitarray(const char *list,
+                    char *ary,
+                    int (*name2bit)(const char *, size_t))
+{
+       const char *begin = NULL, *p;
+
+       if (!list || !name2bit || !ary)
+               return -EINVAL;
+
+       for (p = list; p && *p; p++) {
+               const char *end = NULL;
+               int bit;
+
+               if (!begin)
+                       begin = p;              /* begin of the level name */
+               if (*p == ',')
+                       end = p;                /* terminate the name */
+               if (*(p + 1) == '\0')
+                       end = p + 1;            /* end of string */
+               if (!begin || !end)
+                       continue;
+               if (end <= begin)
+                       return -1;
+
+               bit = name2bit(begin, end - begin);
+               if (bit < 0)
+                       return bit;
+               setbit(ary, bit);
+               begin = NULL;
+               if (end && !*end)
+                       break;
+       }
+       return 0;
+}
 
 #ifdef TEST_PROGRAM
 
index 81f7dbd09ef3a01c69bef59119b280afd272ef00..22db3139cedd640e5fc83208a6c0ea54e819eeac 100644 (file)
--- a/lib/tt.c
+++ b/lib/tt.c
@@ -661,47 +661,6 @@ int tt_print_table(struct tt *tb)
        return 0;
 }
 
-/* Returns: >= 0  : number of items added to ary[]
- *            -1  : parse error or unknown item
- *            -2  : arysz reached
- */
-int tt_parse_columns_list(const char *list, int ary[], size_t arysz,
-                       int (name2id)(const char *, size_t))
-{
-       const char *begin = NULL, *p;
-       int n = 0;
-
-       if (!list || !*list || !ary || !arysz || !name2id)
-               return -1;
-
-       for (p = list; p && *p; p++) {
-               const char *end = NULL;
-               int id;
-
-               if (!begin)
-                       begin = p;              /* begin of the column name */
-               if (*p == ',')
-                       end = p;                /* terminate the name */
-               if (*(p + 1) == '\0')
-                       end = p + 1;            /* end of string */
-               if (!begin || !end)
-                       continue;
-               if (end <= begin)
-                       return -1;
-
-               id = name2id(begin, end - begin);
-               if (id == -1)
-                       return -1;
-               ary[ n++ ] = id;
-               if (n >= arysz)
-                       return -2;
-               begin = NULL;
-               if (end && !*end)
-                       break;
-       }
-       return n;
-}
-
 #ifdef TEST_PROGRAM
 #include <errno.h>
 
index 2dbb60e5ab26a1415a5cba45ffe642eeb7f9efa1..4d00291868d6142a3f21d7905b732ca819f82c13 100644 (file)
@@ -897,8 +897,7 @@ int main(int argc, char *argv[])
                        disable_columns_truncate();
                        break;
                case 'o':
-                       ncolumns = tt_parse_columns_list(
-                                               optarg,
+                       ncolumns = string_to_idarray(optarg,
                                                columns, ARRAY_SIZE(columns),
                                                column_name_to_id);
                        if (ncolumns < 0)
@@ -909,8 +908,7 @@ int main(int argc, char *argv[])
                        break;
                case 'p':
                        if (optarg) {
-                               nactions = tt_parse_columns_list(
-                                               optarg,
+                               nactions = string_to_idarray(optarg,
                                                actions, ARRAY_SIZE(actions),
                                                poll_action_name_to_id);
                                if (nactions < 0)
index b458486f0acfc9dca18b433443c5e8f0e8f2ccce..e8e9a62c1bf652fc5d540964a4447b90e4efe1b2 100644 (file)
@@ -971,8 +971,7 @@ int main(int argc, char *argv[])
                        tt_flags |= TT_FL_NOHEADINGS;
                        break;
                case 'o':
-                       ncolumns = tt_parse_columns_list(
-                                               optarg,
+                       ncolumns = string_to_idarray(optarg,
                                                columns, ARRAY_SIZE(columns),
                                                column_name_to_id);
                        if (ncolumns < 0)
index 2451993e41343a2891f31d7d7c945aee4ee79839..a42c1894f8b4ba95897ed4317954c4bb88bfea12 100644 (file)
@@ -676,8 +676,7 @@ int main(int argc, char **argv)
                                errx(EXIT_FAILURE, _("failed to parse --nr <M-N> range"));
                        break;
                case 'o':
-                       ncolumns = tt_parse_columns_list(
-                                               optarg,
+                       ncolumns = string_to_idarray(optarg,
                                                columns, ARRAY_SIZE(columns),
                                                column_name_to_id);
                        if (ncolumns < 0)
index 7123c7fb62b652d6e933c4074dd7643606059736..d9beb9c5d99bcacf0b7b7049f2d8633bdd31cf77 100644 (file)
@@ -311,45 +311,6 @@ static double time_diff(struct timeval *a, struct timeval *b)
        return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / 1E6;
 }
 
-/*
- * LIST ::= <item> [, <item>]
- *
- * The <item> is translated to 'id' by name2id() function and the 'id' is used
- * as a possition in the 'ary' bit array. It means that the 'id' has to be in
- * range <0..N> where N < sizeof(ary) * NBBY.
- */
-static int list_to_bitarray(const char *list,
-                           int (*name2id)(const char *name, size_t namesz),
-                           char *ary)
-{
-       const char *begin = NULL, *p;
-
-       for (p = list; p && *p; p++) {
-               const char *end = NULL;
-               int id;
-
-               if (!begin)
-                       begin = p;              /* begin of the level name */
-               if (*p == ',')
-                       end = p;                /* terminate the name */
-               if (*(p + 1) == '\0')
-                       end = p + 1;            /* end of string */
-               if (!begin || !end)
-                       continue;
-               if (end <= begin)
-                       return -1;
-
-               id = name2id(begin, end - begin);
-               if (id < 0)
-                       return id;
-               setbit(ary, id);
-               begin = NULL;
-               if (end && !*end)
-                       break;
-       }
-       return 0;
-}
-
 static int get_buffer_size()
 {
        int n = klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
@@ -674,7 +635,9 @@ int main(int argc, char *argv[])
                        break;
                case 'f':
                        ctl.fltr_fac = 1;
-                       list_to_bitarray(optarg, parse_facility, ctl.facilities);
+                       if (string_to_bitarray(optarg,
+                                            ctl.facilities, parse_facility) < 0)
+                               return EXIT_FAILURE;
                        break;
                case 'h':
                        usage(stdout);
@@ -685,7 +648,9 @@ int main(int argc, char *argv[])
                        break;
                case 'l':
                        ctl.fltr_lev= 1;
-                       list_to_bitarray(optarg, parse_level, ctl.levels);
+                       if (string_to_bitarray(optarg,
+                                            ctl.levels, parse_level) < 0)
+                               return EXIT_FAILURE;
                        break;
                case 'n':
                        cmd = SYSLOG_ACTION_CONSOLE_LEVEL;