Use the new formating code to print the user summary.
authorAndre Noll <maan@systemlinux.org>
Sun, 26 Oct 2008 13:19:27 +0000 (14:19 +0100)
committerAndre Noll <maan@systemlinux.org>
Sun, 26 Oct 2008 13:19:27 +0000 (14:19 +0100)
We don't need gengetopt's string parser any more, so don't create the
code for it.

Makefile
adu.h
create.c
interactive.c
select.c
select.ggo

index 2f1fb47..1a2761c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -41,8 +41,7 @@ cmdline.c cmdline.h: adu.ggo
        $(CC) -c $(CPPFLAGS) $(DEBUG_CPPFLAGS) $<
 
 select.cmdline.c select.cmdline.h: select.ggo
-       gengetopt --string-parser \
-               --set-package=select \
+       gengetopt --set-package=select \
                --no-handle-help \
                --no-handle-error \
                --no-handle-version \
diff --git a/adu.h b/adu.h
index 8e62969..7e0d34c 100644 (file)
--- a/adu.h
+++ b/adu.h
@@ -195,9 +195,12 @@ int for_each_admissible_user(int (*func)(struct user_info *, void *),
 void sort_hash_table(int (*comp)(const void *, const void *));
 
 /* select.c */
+struct select_format_info {
+       struct format_info *user_summary_fi;
+};
 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
-               struct uid_range **admissible_uids);
-int run_select_query(struct uid_range *admissible_uids);
+               struct uid_range **admissible_uids, struct select_format_info *sfi);
+int run_select_query(struct uid_range *admissible_uids, struct select_format_info *sfi);
 int com_select(void);
 
 /* create.h */
index 9b26f72..022129b 100644 (file)
--- a/create.c
+++ b/create.c
@@ -7,6 +7,7 @@
 /** \file create.c The create mode of adu. */
 
 #include <dirent.h> /* readdir() */
+#include "format.h"
 #include "adu.h"
 #include "gcc-compat.h"
 #include "cmdline.h"
index 295be37..35b9b48 100644 (file)
@@ -1,3 +1,4 @@
+#include "format.h"
 #include "adu.h"
 #include "string.h"
 #include "error.h"
@@ -10,6 +11,7 @@ struct interactive_command {
 };
 
 static struct uid_range *admissible_uids;
+static struct select_format_info sfi;
 
 #define INTERACTIVE_COMMANDS \
        INTERACTIVE_COMMAND(dump, "dump the current configuration") \
@@ -47,7 +49,7 @@ static int read_input_line(char *line, size_t size)
 
 static int icom_run(__a_unused char *line)
 {
-       return run_select_query(admissible_uids);
+       return run_select_query(admissible_uids, &sfi);
 }
 
 static int icom_help(__a_unused char *line)
@@ -74,7 +76,7 @@ static int icom_set(char *line)
                .check_ambiguity = 0,
                .print_errors = 1
        };
-       return parse_select_options(line, &params, &admissible_uids);
+       return parse_select_options(line, &params, &admissible_uids, &sfi);
 }
 
 static int icom_dump(__a_unused char *line)
index 943b8fd..362b1a6 100644 (file)
--- a/select.c
+++ b/select.c
@@ -7,6 +7,7 @@
 /** \file select.c The select mode of adu. */
 
 #include <dirent.h> /* readdir() */
+#include "format.h"
 #include "adu.h"
 #include "gcc-compat.h"
 #include "cmdline.h"
@@ -22,9 +23,27 @@ static uint64_t num_files;
 /** Global bytes count. */
 static uint64_t num_bytes;
 
+
 /** The decimal representation of an uint64_t never exceeds that size. */
 #define FORMATED_VALUE_SIZE 25
 
+#define USER_SUMMARY_ATOMS \
+       ATOM(pw_name, STRING) \
+       ATOM(uid, ID) \
+       ATOM(dirs, COUNT) \
+       ATOM(files, COUNT) \
+       ATOM(size, SIZE)
+
+#define ATOM(x, y) { .name = #x, .type = AT_ ## y},
+struct atom user_summary_atoms[] = {
+       USER_SUMMARY_ATOMS
+       {.name = NULL}
+};
+#undef ATOM
+#define ATOM(x, y) usa_ ## x,
+enum user_summary_atoms {USER_SUMMARY_ATOMS};
+#undef ATOM
+
 /* these get filled in by the select command. */
 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
 
@@ -381,26 +400,17 @@ static int print_global_summary(void)
 
 static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
 {
-       char formated_dir_count[FORMATED_VALUE_SIZE],
-               formated_file_count[FORMATED_VALUE_SIZE],
-               formated_bytes[FORMATED_VALUE_SIZE ];
-
-       format_count_value(select_conf.count_unit_arg, ui->dirs,
-               select_conf.count_unit_arg == count_unit_arg_h,
-               formated_dir_count);
-       format_count_value(select_conf.count_unit_arg, ui->files,
-               select_conf.count_unit_arg == count_unit_arg_h,
-               formated_file_count);
-       format_size_value(select_conf.size_unit_arg, ui->bytes,
-               select_conf.size_unit_arg == size_unit_arg_h,
-               formated_bytes);
-       return output("\t%s\t%u\t%s\t%s\t%s\n",
-               ui->pw_name? ui->pw_name : "?",
-               (unsigned)ui->uid,
-               formated_dir_count,
-               formated_file_count,
-               formated_bytes
-       );
+       struct format_info *fi = data;
+       union atom_value values[] = {
+               [usa_pw_name] = {.string_value = ui->pw_name?
+                       ui->pw_name : "?"},
+               [usa_uid] = {.num_value = (long long unsigned)ui->uid},
+               [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
+               [usa_files] = {.num_value =  (long long unsigned)ui->files},
+               [usa_size] = {.num_value =  (long long unsigned)ui->bytes}
+       };
+       char *buf = format_items(fi, values);
+       return output("%s", buf);
 }
 
 static int name_comp(const void *a, const void *b)
@@ -453,19 +463,17 @@ static int (*summary_comparators[])(const void *, const void *) = {
        [user_summary_sort_arg_size] = size_comp,
 };
 
-static int print_user_summary(void)
+static int print_user_summary(struct format_info *fi)
 {
        if (select_conf.no_user_summary_given)
                return 1;
        if (!select_conf.no_headers_given) {
-               int ret = output("User summary "
-                       "(pw_name/uid/dirs%s/files%s/size%s):\n",
-                       count_unit_buf, count_unit_buf, size_unit_buf);
+               int ret = output("User summary\n");
                if (ret < 0)
                        return ret;
        }
        sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
-       return for_each_admissible_user(print_user_summary_line, NULL);
+       return for_each_admissible_user(print_user_summary_line, fi);
 }
 
 static int print_user_list(struct user_info *ui, __a_unused void *data)
@@ -585,7 +593,7 @@ static int print_global_lists(void)
        return 1;
 }
 
-static int print_statistics(void)
+static int print_statistics(struct select_format_info *sli)
 {
        int ret;
 
@@ -598,7 +606,7 @@ static int print_statistics(void)
        ret = print_user_lists();
        if (ret < 0)
                return ret;
-       ret = print_user_summary();
+       ret = print_user_summary(sli->user_summary_fi);
        if (ret < 0)
                return ret;
        return 1;
@@ -639,7 +647,8 @@ out:
        return ret;
 }
 
-int run_select_query(struct uid_range *admissible_uids)
+int run_select_query(struct uid_range *admissible_uids,
+               struct select_format_info *sfi)
 {
        int ret;
 
@@ -667,7 +676,7 @@ int run_select_query(struct uid_range *admissible_uids)
        if (ret < 0)
                goto out;
        check_signals();
-       ret = print_statistics();
+       ret = print_statistics(sfi);
 out:
        close_all_tables();
        if (output_file != stdout)
@@ -677,20 +686,36 @@ out:
 
 /* return: < 0: error, >0: OK, == 0: help given */
 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
-               struct uid_range **admissible_uids)
+               struct uid_range **admissible_uids, struct select_format_info *sfi)
 {
        int ret;
        const char **line;
 
-       if (select_cmdline_parser_string_ext(string, &select_conf, "select",
-                       params))
-               return -E_SYNTAX;
+       if (conf.select_options_given) {
+               int argc;
+               char **argv;
+
+               ret = create_argv(string, &argv);
+               if (ret < 0)
+                       return ret;
+               argc = ret;
+               ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
+               free_argv(argv);
+               if (ret)
+                       return -E_SYNTAX;
+               if (select_conf.help_given || select_conf.detailed_help_given)
+                       goto help;
+
+       }
        ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
        if (ret < 0)
                return ret;
-       if (!select_conf.help_given && !select_conf.detailed_help_given)
-               return 1;
-
+       ret = parse_format_string(select_conf.user_summary_format_arg,
+               user_summary_atoms, &sfi->user_summary_fi);
+       if (ret < 0)
+               return ret;
+       return 1;
+help:
        line = select_conf.detailed_help_given?
                select_args_info_detailed_help : select_args_info_help;
        if (!output_file)
@@ -706,20 +731,20 @@ int parse_select_options(char *string, struct select_cmdline_parser_params *para
 int com_select(void)
 {
        struct uid_range *admissible_uids = NULL;
-
-       if (conf.select_options_given) {
-               int ret;
-               struct select_cmdline_parser_params params = {
-                       .override = 1,
-                       .initialize = 1,
-                       .check_required = 1,
-                       .check_ambiguity = 1,
-                       .print_errors = 1
-               };
-               ret = parse_select_options(conf.select_options_arg, &params,
-                       &admissible_uids);
-               if (ret <= 0) /* do not run query if help was given */
-                       return ret;
-       }
-       return run_select_query(admissible_uids);
+       struct select_format_info sfi;
+       int ret;
+       struct select_cmdline_parser_params params = {
+               .override = 1,
+               .initialize = 1,
+               .check_required = 1,
+               .check_ambiguity = 1,
+               .print_errors = 1
+       };
+
+       select_cmdline_parser_init(&select_conf);
+       ret = parse_select_options(conf.select_options_arg, &params,
+               &admissible_uids, &sfi);
+       if (ret <= 0) /* do not run query if help was given */
+               return ret;
+       return run_select_query(admissible_uids, &sfi);
 }
index f4d090b..1093d46 100644 (file)
@@ -154,3 +154,21 @@ details="
        with the base directory. The default is to print paths relative
        to the base dir.
 "
+
+########################
+section "Format strings"
+########################
+
+option "user-summary-format" -
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+"How to format the user summary"
+string typestr="<format>"
+default="%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
+details="
+       pw_name: The user name
+       uid: The user id
+       dirs: The number of directories
+       files: The number of files
+       size: Total size of all files
+"
+optional