logger: Simplify getopt() handling in server 23/143823/1
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 10 Aug 2017 13:05:36 +0000 (15:05 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 11 Aug 2017 11:22:57 +0000 (13:22 +0200)
Change-Id: I5c6e6f47a0b8ed9df599cdeb52e1ea6651f40716
Signed-off-by: Karol Lewandowski <k.lewandowsk@samsung.com>
src/logger/logger.c

index 66766a6..294d678 100644 (file)
@@ -1173,6 +1173,8 @@ static int parse_command_line(const char* cmdl, struct writer* wr, struct reader
        reader->service_reader = NULL;
 
        while ((option = getopt(argc, argv, "cdt:gsf:r:n:v:b:ku:")) != -1) {
+               int err_arg_nondigit = 0;
+
                switch (option) {
                        break;
                case 'd':
@@ -1180,55 +1182,39 @@ static int parse_command_line(const char* cmdl, struct writer* wr, struct reader
                        reader->dumpcount = -1;
                        break;
                case 'f':
-                       if (!optarg) {
-                               retval = -EINVAL;
-                               goto cleanup;
-                       }
-                       reader->file.path = strdup(optarg);
-                       if (!reader->file.path) {
-                               retval = -ENOMEM;
+                       retval = logfile_set_path(&reader->file, optarg);
+                       if (retval < 0)
                                goto cleanup;
-                       }
                        break;
                case 'b':
-                       if (!optarg) {
-                               retval = -EINVAL;
-                               goto cleanup;
-                       }
                        assert(g_backend.reader_init);
                        retval = g_backend.reader_init(reader, optarg);
                        if (retval != 0)
                                goto cleanup;
                        break;
                case 'r':
-                       if (!optarg || !isdigit(optarg[0])) {
-                               retval = -EINVAL;
-                               goto cleanup;
-                       }
-                       reader->file.rotate_size_kbytes = atoi(optarg);
+                       reader->file.rotate_size_kbytes = atoi_check_numeric(optarg, &err_arg_nondigit);
                        break;
                case 'n':
-                       if (!optarg || !isdigit(optarg[0])) {
-                               retval = -EINVAL;
-                               goto cleanup;
-                       }
-                       reader->file.max_rotated = atoi(optarg);
+                       reader->file.max_rotated = atoi_check_numeric(optarg, &err_arg_nondigit);
                        break;
                case 'v':
-                       if (!optarg) {
-                               retval = -EINVAL;
-                               goto cleanup;
-                       }
                        log_set_print_format(reader->file.format, log_format_from_string(optarg));
                        break;
                case 's':
                        silence = 1;
                        log_add_filter_string(reader->file.format, "*:s");
                        break;
+               case '?':
+                       // invalid option or missing argument - pass to default and break
                default:
                        // everything else gets handled in util directly
                        break;
                }
+               if (err_arg_nondigit || option == '?') {
+                       retval = -EINVAL;
+                       goto cleanup;
+               }
        }
 
        if (wr && wr->buf_ptr) {