Add format string doku, simplify format string handling.
[adu.git] / select.c
index f929c35..949fd92 100644 (file)
--- a/select.c
+++ b/select.c
@@ -565,28 +565,17 @@ static int print_global_list(struct format_info *fi)
                global_list_loop_function, &gli.ret, &gli.osl_errno);
 }
 
-static int print_statistics(struct select_format_info *sli)
+static int print_statistics(struct format_info *fi)
 {
-       int ret;
-
        switch (select_conf.select_mode_arg) {
                case select_mode_arg_global_list:
-                       ret = print_global_list(sli->global_list_fi);
-                       free_format_info(sli->global_list_fi);
-                       return ret;
+                       return print_global_list(fi);
                case select_mode_arg_global_summary:
-                       ret = print_global_summary(sli->global_summary_fi);
-                       free_format_info(sli->global_summary_fi);
-                       return ret;
+                       return print_global_summary(fi);
                case select_mode_arg_user_list:
-                       ret = for_each_admissible_user(print_user_list,
-                               sli->user_list_fi);
-                       free_format_info(sli->user_list_fi);
-                       return ret;
+                       return for_each_admissible_user(print_user_list, fi);
                case select_mode_arg_user_summary:
-                       ret = print_user_summary(sli->user_summary_fi);
-                       free_format_info(sli->user_summary_fi);
-                       return ret;
+                       return print_user_summary(fi);
        };
        ERROR_LOG("bad select mode\n");
        return ERRNO_TO_ERROR(-EINVAL);
@@ -627,8 +616,7 @@ out:
        return ret;
 }
 
-int run_select_query(struct uid_range *admissible_uids,
-               struct select_format_info *sfi)
+int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
 {
        int ret;
 
@@ -647,7 +635,7 @@ int run_select_query(struct uid_range *admissible_uids,
        if (ret < 0)
                goto out;
        check_signals();
-       ret = print_statistics(sfi);
+       ret = print_statistics(fi);
 out:
        close_all_tables();
        if (output_file != stdout)
@@ -655,12 +643,19 @@ out:
        return ret;
 }
 
+#define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
+#define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
+#define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
+#define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
+
 /* 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 select_format_info *sfi)
+               struct uid_range **admissible_uids, struct format_info **fi)
 {
        int ret;
        const char **line;
+       char *fmt = NULL;
+       struct atom *atoms;
 
        if (conf.select_options_given) {
                int argc;
@@ -676,35 +671,32 @@ int parse_select_options(char *string, struct select_cmdline_parser_params *para
                        return -E_SYNTAX;
                if (select_conf.help_given || select_conf.detailed_help_given)
                        goto help;
+               fmt = select_conf.format_arg;
 
        }
        ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
        if (ret < 0)
                return ret;
-       ret = parse_format_string(select_conf.user_summary_format_arg,
-               user_summary_atoms, &sfi->user_summary_fi);
-       if (ret < 0)
-               return ret;
-       ret = parse_format_string(select_conf.global_summary_format_arg,
-               global_summary_atoms, &sfi->global_summary_fi);
-       if (ret < 0)
-               goto global_summary_err;
-       ret = parse_format_string(select_conf.global_list_format_arg,
-               global_list_atoms, &sfi->global_list_fi);
-       if (ret < 0)
-               goto global_list_err;
-       ret = parse_format_string(select_conf.user_list_format_arg,
-               user_list_atoms, &sfi->user_list_fi);
-       if (ret < 0)
-               goto user_list_err;
-       return 1;
-user_list_err:
-       free_format_info(sfi->global_list_fi);
-global_list_err:
-       free_format_info(sfi->global_summary_fi);
-global_summary_err:
-       free_format_info(sfi->user_summary_fi);
-       return ret;
+
+       switch (select_conf.select_mode_arg) {
+               case select_mode_arg_global_list:
+                       if (!fmt)
+                               fmt = GLOBAL_LIST_DFLT_FMT;
+                       atoms = global_list_atoms;
+               case select_mode_arg_global_summary:
+                       if (!fmt)
+                               fmt = GLOBAL_SUMMARY_DFLT_FMT;
+                       atoms = global_summary_atoms;
+               case select_mode_arg_user_list:
+                       if (!fmt)
+                               fmt = USER_LIST_DFLT_FMT;
+                       atoms = user_list_atoms;
+               case select_mode_arg_user_summary:
+                       if (!fmt)
+                               fmt = USER_SUMMARY_DFLT_FMT;
+                       atoms = user_summary_atoms;
+       };
+       return parse_format_string(fmt, atoms, fi);
 help:
        line = select_conf.detailed_help_given?
                select_args_info_detailed_help : select_args_info_help;
@@ -721,8 +713,8 @@ help:
 int com_select(void)
 {
        struct uid_range *admissible_uids = NULL;
-       struct select_format_info sfi;
        int ret;
+       struct format_info *fi;
        struct select_cmdline_parser_params params = {
                .override = 1,
                .initialize = 1,
@@ -733,8 +725,10 @@ int com_select(void)
 
        select_cmdline_parser_init(&select_conf);
        ret = parse_select_options(conf.select_options_arg, &params,
-               &admissible_uids, &sfi);
+               &admissible_uids, &fi);
        if (ret <= 0) /* do not run query if help was given */
                return ret;
-       return run_select_query(admissible_uids, &sfi);
+       ret = run_select_query(admissible_uids, fi);
+       free_format_info(fi);
+       return ret;
 }