command_parser.c 15.2 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
3 4 5 6
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
O
overweight 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
10
 * See the Mulan PSL v2 for more details.
O
overweight 已提交
11 12 13 14 15 16
 * Author: tanyifeng
 * Create: 2017-11-22
 * Description: provide command functions
 ******************************************************************************/

#define _GNU_SOURCE
L
lifeng68 已提交
17
#include "command_parser.h"
O
overweight 已提交
18 19 20 21

#include <stdio.h>
#include <string.h>
#include <regex.h>
22
#include <stdlib.h>
23
#include <stdint.h>
L
lifeng68 已提交
24
#include <ctype.h>
O
overweight 已提交
25

26
#include "constants.h"
O
overweight 已提交
27
#include "utils.h"
H
haozi007 已提交
28
#include "isula_libutils/log.h"
29 30 31 32
#include "utils_array.h"
#include "utils_convert.h"
#include "utils_string.h"
#include "utils_verify.h"
O
overweight 已提交
33

L
LiuHao 已提交
34
void command_help_isulad_head()
O
overweight 已提交
35
{
L
LiuHao 已提交
36
    fprintf(stdout, "isulad\n\nlightweight container runtime daemon\n");
O
overweight 已提交
37 38 39 40 41 42 43 44 45 46 47 48
}

int compare_options(const void *s1, const void *s2)
{
    return strcmp((*(const command_option_t *)s1).large, (*(const command_option_t *)s2).large);
}
void print_options(int options_len, const command_option_t *options)
{
    int i = 0;
    int max_opt_len = 0;

    for (i = 0; i < options_len; i++) {
49
        const command_option_t *option = &(options[i]);
O
overweight 已提交
50 51 52
        // -s
        int len = 2;
        // -s, --large, 6 is the length of "-s, --".
53 54
        if (option->large != NULL) {
            len = (int)(strlen(option->large) + 6);
O
overweight 已提交
55 56 57 58 59 60 61 62 63 64 65
        }
        if (len > max_opt_len) {
            max_opt_len = len;
        }
    }

    // format: "  -s, --large    description"
    // 6 is the total length of black before "-s" and "description"
    max_opt_len += 6;

    for (i = 0; i < options_len; i++) {
66
        const command_option_t *option = &(options[i]);
O
overweight 已提交
67 68 69 70
        int curindex = 0;
        int space_left = 0;

        curindex = fprintf(stdout, "  ");
71 72
        if (option->small) {
            curindex += fprintf(stdout, "-%c", (char)option->small);
O
overweight 已提交
73 74
        }

75 76 77
        if (option->large != NULL) {
            if (option->small) {
                curindex += fprintf(stdout, ", --%s", option->large);
O
overweight 已提交
78
            } else {
79
                curindex += fprintf(stdout, "    --%s", option->large);
O
overweight 已提交
80 81 82 83 84 85 86
            }
        }

        if (curindex <= max_opt_len) {
            space_left = max_opt_len - curindex;
        }

87
        fprintf(stdout, "%*s%s\n", space_left, "", option->description);
O
overweight 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
    }
    fputc('\n', stdout);
}

void command_help(command_t *self)
{
    const char *progname = strrchr(self->name, '/');
    if (progname == NULL) {
        progname = self->name;
    } else {
        progname++;
    }

L
LiuHao 已提交
101 102
    if (self->type != NULL && strcmp(self->type, "isulad") == 0) {
        command_help_isulad_head();
O
overweight 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    }
    fprintf(stdout, "\nUsage:  %s %s\n\n", progname, self->usage);
    fprintf(stdout, "%s\n\n", self->description);
    qsort(self->options, (size_t)self->option_count, sizeof(self->options[0]), compare_options);
    print_options(self->option_count, self->options);
}

int command_valid_socket(command_option_t *option, const char *arg)
{
    if (!util_validate_socket(arg)) {
        COMMAND_ERROR("Invalid socket name : %s", arg);
        return -1;
    }
    return 0;
}

void command_init(command_t *self, command_option_t *opts, int opts_len, int argc, const char **argv,
                  const char *description, const char *usage)
{
O
openeuler-iSula 已提交
122
    (void)memset(self, 0, sizeof(command_t));
O
overweight 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    self->name = argv[0];
    self->argc = argc - 2;
    self->argv = argv + 2;
    self->usage = usage;
    self->description = description;
    self->options = opts;
    self->option_count = opts_len;
}

void command_option(command_t *self, command_option_type_t type, void *data, int small, const char *large,
                    const char *desc, command_callback_t cb)
{
    if (self->option_count == COMMANDER_MAX_OPTIONS) {
        COMMAND_ERROR("Maximum option definitions exceeded");
        exit(EINVALIDARGS);
    }
    int n = self->option_count++;
    command_option_t *opt = &self->options[n];
    opt->type = type;
    opt->data = data;
    opt->cb = cb;
    opt->small = small;
    opt->description = desc;
    opt->large = large;
}

static int read_option_arg(command_t *self, command_option_t *opt, const char **opt_arg, const char **readed)
{
    if (self == NULL || opt == NULL || opt_arg == NULL) {
        return -1;
    }
    if (opt->hasdata) {
        *readed = *opt_arg;
        *opt_arg = NULL;
    }
    if (!opt->hasdata && self->argc > 1) {
        opt->hasdata = true;
        *readed = *++self->argv;
        self->argc--;
    }
    if (!opt->hasdata) {
        COMMAND_ERROR("Flag needs an argument: --%s", opt->large);
        return -1;
    }
    return 0;
}

static int command_get_bool_option_data(command_option_t *option, const char **opt_arg)
{
    bool converted_bool = (option->type == CMD_OPT_TYPE_BOOL) ? true : false;

    if (option->hasdata) {
        int ret = util_str_to_bool(*opt_arg, &converted_bool);
        if (ret != 0) {
            COMMAND_ERROR("Invalid boolean value \"%s\" for flag --%s", *opt_arg, option->large);
            return -1;
        }
        *opt_arg = NULL;
    }

    *(bool *)option->data = converted_bool;

    return 0;
}

static int command_get_string_option_data(command_t *self, command_option_t *option, const char **opt_arg)
{
    if (read_option_arg(self, option, opt_arg, (const char **)option->data)) {
        return -1;
    }
    if (option->cb != NULL) {
        return option->cb(option, *(char **)option->data);
    }
    return 0;
}

static int command_get_string_dup_option_data(command_t *self, command_option_t *option, const char **opt_arg)
{
    const char *readed_item = NULL;

    if (read_option_arg(self, option, opt_arg, &readed_item) != 0) {
        return -1;
    }
    if (*(char **)option->data != NULL) {
        free(*(char **)option->data);
    }
    *(char **)option->data = util_strdup_s(readed_item);
    if (option->cb != NULL) {
        return option->cb(option, readed_item);
    }
    return 0;
}

static int command_get_callback_option_data(command_t *self, command_option_t *option, const char **opt_arg)
{
    const char *readed_item = NULL;

    if (read_option_arg(self, option, opt_arg, &readed_item)) {
        return -1;
    }
    if (option->cb == NULL) {
        COMMAND_ERROR("Must specify callback for type array");
        return -1;
    }
    return option->cb(option, readed_item);
}

static int command_get_option_data(command_t *self, command_option_t *option, const char **opt_arg)
{
    if (option == NULL) {
        return -1;
    }
    switch (option->type) {
        case CMD_OPT_TYPE_BOOL:
        case CMD_OPT_TYPE_BOOL_FALSE:
            return command_get_bool_option_data(option, opt_arg);
        case CMD_OPT_TYPE_STRING:
            return command_get_string_option_data(self, option, opt_arg);
        case CMD_OPT_TYPE_STRING_DUP:
            return command_get_string_dup_option_data(self, option, opt_arg);
        case CMD_OPT_TYPE_CALLBACK:
            return command_get_callback_option_data(self, option, opt_arg);
        default:
            COMMAND_ERROR("Unkown command option type:%d", (int)(option->type));
            return -1;
    }
}

int have_short_options(command_t *self, char arg)
{
    int i;

    for (i = 0; i < self->option_count; i++) {
        if (self->options[i].small == arg) {
            return 0;
        }
    }

    return -1;
}

static int command_parse_options(command_t *self, const char **opt_arg, bool *found)
{
    int j = 0;

    for (j = 0; j < self->option_count; ++j) {
        command_option_t *opt = &self->options[j];
        opt->hasdata = false;
        if (opt->small != (*opt_arg)[0]) {
            continue;
        }
        // match flag
        *found = true;
        if ((*opt_arg)[1]) {
            if ((*opt_arg)[1] == '=') {
                *opt_arg = *opt_arg + 2;
                opt->hasdata = true;
            } else {
                *opt_arg = *opt_arg + 1;
            }
        } else {
            *opt_arg = NULL;
        }
        if (command_get_option_data(self, opt, opt_arg)) {
            return -1;
        }
        break;
    }

    return 0;
}

static int command_parse_short_arg(command_t *self, const char *arg)
{
    bool found = false;
    const char *opt_arg = arg;

    do {
        found = false;
        if (command_parse_options(self, &opt_arg, &found)) {
            return -1;
        }
    } while (found && opt_arg != NULL);

    if (opt_arg != NULL) {
        COMMAND_ERROR("Unkown flag found:'%c'", opt_arg[0]);
        exit(EINVALIDARGS);
    }
    return 0;
}

static int command_parse_long_arg(command_t *self, const char *arg)
{
    int j = 0;

    if (strcmp(arg, "help") == 0) {
        command_help(self);
        exit(0);
    }

    for (j = 0; j < self->option_count; ++j) {
        command_option_t *opt = &self->options[j];
        const char *opt_arg = NULL;
        opt->hasdata = false;

        if (opt->large == NULL) {
            continue;
        }

        opt_arg = str_skip_str(arg, opt->large);
        if (opt_arg == NULL) {
            continue;
        }

        if (opt_arg[0]) {
            if (opt_arg[0] != '=') {
                continue;
            }
            opt_arg = opt_arg + 1;
            opt->hasdata = true;
        } else {
            opt_arg = NULL;
        }
        if (command_get_option_data(self, opt, &opt_arg)) {
            return -1;
        }
        return 0;
    }
    COMMAND_ERROR("Unkown flag found:'--%s'\n", arg);
    exit(EINVALIDARGS);
}

355
int command_parse_args(command_t *self, int *argc, char * const **argv)
O
overweight 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
{
    int ret = 0;

    for (; self->argc; self->argc--, self->argv++) {
        const char *arg_opt = self->argv[0];
        if (arg_opt[0] != '-' || !arg_opt[1]) {
            break;
        }

        // short option
        if (arg_opt[1] != '-') {
            arg_opt = arg_opt + 1;
            ret = command_parse_short_arg(self, arg_opt);
            if (!ret) {
                continue;
            }
            break;
        }

        // --
        if (!arg_opt[2]) {
            self->argc--;
            self->argv++;
            break;
        }

        // long option
        arg_opt = arg_opt + 2;
        ret = command_parse_long_arg(self, arg_opt);
        if (ret == 0) {
            continue;
        }
        break;
    }
    if (self->argc > 0) {
        *argc = self->argc;
392
        *argv = (char * const *)self->argv;
O
overweight 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
    }
    return ret;
}

int command_valid_socket_append_array(command_option_t *option, const char *arg)
{
    int len;
    char **pos = NULL;

    if (option == NULL) {
        return -1;
    }
    if (!util_validate_socket(arg)) {
        COMMAND_ERROR("Invalid socket name : %s", arg);
        return -1;
    }

    for (pos = *(char ***)(option->data), len = 0; pos != NULL && *pos != NULL; pos++, len++) {
        if (strcmp(*pos, arg) == 0) {
            break;
        }
    }
    if (pos != NULL && *pos != NULL) {
        return 0;
    }

    if (util_array_append(option->data, arg) != 0) {
        ERROR("merge hosts config failed");
        return -1;
    }
    len++;
    if (len > MAX_HOSTS) {
        COMMAND_ERROR("Too many hosts, the max number is %d", MAX_HOSTS);
        return -1;
    }

    return 0;
}

int command_append_array(command_option_t *option, const char *arg)
{
    if (option == NULL) {
        return -1;
    }
    char ***array = option->data;
    return util_array_append(array, arg);
}

int command_convert_u16(command_option_t *option, const char *arg)
{
    int ret = 0;

    if (option == NULL) {
        return -1;
    }
    ret = util_safe_u16(arg, option->data);
    if (ret != 0) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s: %s", arg, option->large, strerror(-ret));
        return EINVALIDARGS;
    }
    return 0;
}

int command_convert_llong(command_option_t *opt, const char *arg)
{
    int ret;

    if (opt == NULL) {
        return -1;
    }
    ret = util_safe_llong(arg, opt->data);
    if (ret != 0) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s: %s", arg, opt->large, strerror(-ret));
        return EINVALIDARGS;
    }
    return 0;
}

int command_convert_uint(command_option_t *opt, const char *arg)
{
    int ret;
    if (opt == NULL) {
        return -1;
    }
    ret = util_safe_uint(arg, opt->data);
    if (ret != 0) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s: %s", arg, opt->large, strerror(-ret));
        return EINVALIDARGS;
    }
    return 0;
}

int command_convert_int(command_option_t *option, const char *arg)
{
    int ret = 0;

    if (option == NULL) {
        return -1;
    }
    ret = util_safe_int(arg, option->data);
    if (ret != 0) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s: %s", arg, option->large, strerror(-ret));
        return EINVALIDARGS;
    }
    return 0;
}

int command_convert_nanoseconds(command_option_t *option, const char *arg)
{
    if (option == NULL) {
        return -1;
    }
    if (util_parse_time_str_to_nanoseconds(arg, option->data)) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s", arg, option->large);
        return EINVALIDARGS;
    }
    return 0;
}

int command_convert_membytes(command_option_t *option, const char *arg)
{
    if (option == NULL) {
        return -1;
    }
    if (util_parse_byte_size_string(arg, option->data) || (*(int64_t *)(option->data)) < 0) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s", arg, option->large);
        return EINVALIDARGS;
    }
    return 0;
}

int command_convert_memswapbytes(command_option_t *option, const char *arg)
{
    if (option == NULL) {
        return -1;
    }
    if (strcmp(arg, "-1") == 0) {
        *(int64_t *)(option->data) = -1;
        return 0;
    }
    if (command_convert_membytes(option, arg)) {
        return EINVALIDARGS;
    }
    return 0;
}

L
LiFeng 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
int command_convert_swappiness(command_option_t *option, const char *arg)
{
    if (option == NULL) {
        return -1;
    }
    if (strcmp(arg, "-1") == 0) {
        *(int64_t *)(option->data) = -1;
        return 0;
    }
    if (util_parse_byte_size_string(arg, option->data) || (*(int64_t *)(option->data)) < 0 ||
        (*(int64_t *)(option->data)) > 100) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s. Valid memory swappiness range is 0-100", arg, option->large);
        return EINVALIDARGS;
    }
    return 0;
}
L
lifeng68 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

int command_convert_nanocpus(command_option_t *option, const char *arg)
{
    int ret = 0;
    char *dup = NULL;

    if (option == NULL) {
        return -1;
    }

    if (!isdigit(arg[0])) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s", arg, option->large);
        return EINVALIDARGS;
    }

    dup = util_strdup_s(arg);
    if (dup == NULL) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s", arg, option->large);
        return EINVALIDARGS;
    }

    if (util_parse_size_int_and_float(arg, 1e9, option->data)) {
        COMMAND_ERROR("Invalid value \"%s\" for flag --%s", arg, option->large);
        ret = EINVALIDARGS;
        goto out;
    }

out:
    free(dup);
    return ret;
}