util: extract getopt from main 80/166080/3
authorMichal Bloch <m.bloch@samsung.com>
Fri, 5 Jan 2018 14:14:37 +0000 (15:14 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Mon, 15 Jan 2018 14:18:27 +0000 (15:18 +0100)
Change-Id: I66f219bf5324fc5ade188afe060598379ac82a34
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/logutil/logutil.c

index 7c57459..ae72e38 100755 (executable)
@@ -71,6 +71,12 @@ struct sorting_vector {
        long timeout;
 };
 
+typedef enum action_e {
+       ACTION_PRINT = 0,
+       ACTION_GETSIZE,
+       ACTION_CLEAR,
+} action_e;
+
 typedef enum {
        PIPE,
        BINARY_FILE,
@@ -802,51 +808,18 @@ cleanup:
        return ret;
 }
 
-static int validate_buffer_set(int *enabled_buffers, int files_cnt)
+int parse_options(int argc, char **argv, struct log_file *l_file, struct sorting_vector *logs, int *enabled_buffers, list_head *file_input_names, action_e *action, int *dump, int *tag_cnt)
 {
+       assert(argv);
+       assert(logs);
+       assert(l_file);
        assert(enabled_buffers);
+       assert(file_input_names);
+       assert(action);
+       assert(dump);
+       assert(tag_cnt);
 
-       if (files_cnt != 0 && *enabled_buffers != 0) {
-               printf("Mixing --dumpfile and -b not allowed!\n");
-               return 0;
-       }
-
-       if (files_cnt == 0 && *enabled_buffers == 0)
-               *enabled_buffers = default_buffers;
-
-       return 1;
-}
-
-int main(int argc, char **argv)
-{
-       int enabled_buffers = 0; // bitset
-       __attribute__ ((cleanup(list_clear))) list_head file_input_names = NULL;
-       const char * conf_value;
-       __attribute__ ((cleanup(log_config_free))) struct log_config conf = {.begin = NULL, .last = NULL};
-       int should_clear = 0;
-       int should_getsize = 0;
-       int dump = 0;
        int silence = 0;
-       __attribute__ ((cleanup(fdi_array_free))) struct fd_info **fdi_ptrs = NULL;
-       int fdi_cnt = 0;
-       int i;
-       int tag_cnt = 0;
-       __attribute__ ((cleanup(logfile_free))) struct log_file l_file;
-       __attribute__ ((cleanup(sorting_vector_free))) struct sorting_vector logs;
-       int r;
-
-       sorting_vector_init(&logs);
-
-       r = logfile_init(&l_file);
-       if (r < 0)
-               goto err_nomem;
-       logfile_set_fd(&l_file, fileno(stdout), 0);
-
-       r = log_config_read(&conf);
-       if (r < 0)
-            printf("Warning: unable to read config file: %s\n", strerror(-r));
-       else
-               apply_config(&conf, &logs);
 
        while (1) {
                static const struct option long_options[] = {
@@ -862,52 +835,53 @@ int main(int argc, char **argv)
 
                switch (option) {
                case 0:
-                       list_add(&file_input_names, optarg);
+                       list_add(file_input_names, optarg);
                        break;
                case 'd':
-                       dump = -1;
+                       *dump = -1;
                        break;
                case 't':
-                       if (sscanf(optarg, "%d", &dump) != 1)
+                       if (sscanf(optarg, "%d", dump) != 1)
                                err_arg_nondigit = 1;
                        break;
                case 'c':
-                       should_clear = 1;
+                       *action = ACTION_CLEAR;
                        break;
                case 'g':
-                       should_getsize = 1;
+                       *action = ACTION_GETSIZE;
                        break;
                case 'b': {
                        log_id_t id = log_id_by_name(optarg);
                        if (id == LOG_ID_INVALID) {
                                printf("There is no buffer \"%s\"\n", optarg);
-                               return 1;
+                               return -ENOENT;
                        }
-                       bit_set(&enabled_buffers, id);
+                       bit_set(enabled_buffers, id);
                        break;
                }
                case 'u':
-                       if (sscanf(optarg, "%d", &logs.size) != 1)
+                       if (sscanf(optarg, "%d", &logs->size) != 1)
                                err_arg_nondigit = 1;
                        break;
                case 'f':
-                       r = logfile_set_path(&l_file, optarg);
-                       if (r < 0)
-                               goto err_nomem;
+                       if (logfile_set_path(l_file, optarg) < 0) {
+                               printf("Error: not enough memory\n");
+                               return -ENOMEM;
+                       }
                        break;
                case 'v':
-                       log_set_print_format(l_file.format, log_format_from_string(optarg));
+                       log_set_print_format(l_file->format, log_format_from_string(optarg));
                        break;
                case 's':
                        silence = 1;
-                       log_add_filter_string(l_file.format, "*:s");
+                       log_add_filter_string(l_file->format, "*:s");
                        break;
                case 'r':
-                       if (sscanf(optarg, "%zu", &l_file.rotate_size_kbytes) != 1)
+                       if (sscanf(optarg, "%zu", &l_file->rotate_size_kbytes) != 1)
                                err_arg_nondigit = 1;
                        break;
                case 'n':
-                       if (sscanf(optarg, "%zu", &l_file.max_rotated) != 1)
+                       if (sscanf(optarg, "%zu", &l_file->max_rotated) != 1)
                                err_arg_nondigit = 1;
                        break;
 
@@ -917,24 +891,74 @@ int main(int argc, char **argv)
                        // help - pass thru & break
                default:
                        show_help(argv[0]);
-                       return 1;
+                       return -EINVAL;
                }
 
                if (err_arg_nondigit) {
                        printf("Error: -%c requires a numerical parameter\n", option);
-                       return 1;
+                       return -EINVAL;
                }
        }
 
        if (optind < argc)
                while (optind < argc) {
-                       log_add_filter_string(l_file.format, argv[optind]);
+                       log_add_filter_string(l_file->format, argv[optind]);
                        if (argv[optind][0] != '*')
-                               tag_cnt++;
+                               ++*tag_cnt;
                        optind++;
                }
        else if (!silence)
-               log_add_filter_string(l_file.format, "*:D");
+               log_add_filter_string(l_file->format, "*:D");
+
+       return 0;
+}
+
+static int validate_buffer_set(int *enabled_buffers, int files_cnt)
+{
+       assert(enabled_buffers);
+
+       if (files_cnt != 0 && *enabled_buffers != 0) {
+               printf("Mixing --dumpfile and -b not allowed!\n");
+               return 0;
+       }
+
+       if (files_cnt == 0 && *enabled_buffers == 0)
+               *enabled_buffers = default_buffers;
+
+       return 1;
+}
+
+int main(int argc, char **argv)
+{
+       int enabled_buffers = 0; // bitset
+       __attribute__ ((cleanup(list_clear))) list_head file_input_names = NULL;
+       const char * conf_value;
+       __attribute__ ((cleanup(log_config_free))) struct log_config conf = {.begin = NULL, .last = NULL};
+       action_e action = ACTION_PRINT;
+       int dump = 0;
+       __attribute__ ((cleanup(fdi_array_free))) struct fd_info **fdi_ptrs = NULL;
+       int fdi_cnt = 0;
+       int i;
+       int tag_cnt = 0;
+       __attribute__ ((cleanup(logfile_free))) struct log_file l_file;
+       __attribute__ ((cleanup(sorting_vector_free))) struct sorting_vector logs;
+       int r;
+
+       sorting_vector_init(&logs);
+
+       r = logfile_init(&l_file);
+       if (r < 0)
+               goto err_nomem;
+       logfile_set_fd(&l_file, fileno(stdout), 0);
+
+       r = log_config_read(&conf);
+       if (r < 0)
+            printf("Warning: unable to read config file: %s\n", strerror(-r));
+       else
+               apply_config(&conf, &logs);
+
+       if (parse_options(argc, argv, &l_file, &logs, &enabled_buffers, &file_input_names, &action, &dump, &tag_cnt) < 0)
+               return 1;
 
        if (!validate_buffer_set(&enabled_buffers, list_count(file_input_names)))
                return 1;
@@ -942,7 +966,7 @@ int main(int argc, char **argv)
        if (!sorting_vector_finalize(&logs))
                goto err_nomem;
 
-       if (should_getsize)
+       if (action == ACTION_GETSIZE)
                return do_getsize(&conf, enabled_buffers);
 
        conf_value = log_config_get(&conf, "backend");
@@ -972,9 +996,9 @@ int main(int argc, char **argv)
                if (!strcmp(conf_value, "pipe")
                        || !strcmp(bufname, "syslog")
                        || !strcmp(bufname, "kmsg"))
-                       fdi = process_buffer_pipe(bufname, should_clear, &conf, argc, argv);
+                       fdi = process_buffer_pipe(bufname, action == ACTION_CLEAR, &conf, argc, argv);
                else
-                       fdi = process_buffer_nonpipe(bufname, should_clear, &conf);
+                       fdi = process_buffer_nonpipe(bufname, action == ACTION_CLEAR, &conf);
                if (fdi) {
                        fdi_ptrs[fdi_cnt++] = fdi;
                        if (bit_count(enabled_buffers) == 1 || tag_cnt == 1 || !IS_VECTOR_SIZE_SORTABLE(logs.size))
@@ -983,7 +1007,7 @@ int main(int argc, char **argv)
        }
 
 
-       if (should_clear)
+       if (action == ACTION_CLEAR)
                return 0;
 
        for (list_head iter = file_input_names; iter; list_next(&iter)) {