shell_bas.c 24.1 KB
Newer Older
X
xionglei6 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "shell_bas.h"
#include <fcntl.h>
#include <signal.h>

#include "init_utils.h"
#include "shell_utils.h"

char *BShellEnvErrString(BShellHandle handle, int32_t err)
{
    static BShellErrInfo shellErrString[] = {
        {BSH_SHELL_INFO, "\r\n\r\n"
                        "+=========================================================+\r\n"
                        "|               Parameter shell v"BSH_VERSION"                    |\r\n"
                        "+=========================================================+\r\n"
        },
        {BSH_CMD_TOO_LONG, "\r\nWarnig: Command is too long\r\n"},
        {BSH_SHOW_CMD_LIST, "Command list:\r\n"},
        {BSH_CMD_NOT_EXIST, "Command not found\r\n"}
    };
    for (size_t i = 0; i < sizeof(shellErrString) / sizeof(shellErrString[0]); i++) {
        if ((int32_t)shellErrString[i].err == err) {
            return shellErrString[i].desc;
        }
    }
S
shiyuju 已提交
39
    BSH_CHECK(handle != NULL, return "System unknown err", "Invalid shell env");
X
xionglei6 已提交
40
    BShellEnv *shell = (BShellEnv *)handle;
S
shiyuju 已提交
41
    int len = sprintf_s(shell->data, sizeof(shell->data) - 1, "System unknown err 0x%08x", err);
weiying440's avatar
weiying440 已提交
42 43 44
    if (len <= 0) {
        BSH_LOGE("Write shell data size failed.");
    }
X
xionglei6 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    return shell->data;
}

static void BShellCmdOutputCmdHelp(BShellHandle handle, BShellCommand *cmd)
{
    BShellEnvOutputString(handle, "    ");
    int32_t spaceLength = BShellEnvOutputString(handle, cmd->help);
    spaceLength = BSH_CMD_NAME_END - spaceLength;
    spaceLength = (spaceLength > 0) ? spaceLength : 4; // 4 min
    do {
        BShellEnvOutputString(handle, " ");
    } while (--spaceLength);
    BShellEnvOutputString(handle, "--");
    BShellEnvOutputString(handle, cmd->desc);
    BShellEnvOutputString(handle, "\r\n");
}

int32_t BShellCmdHelp(BShellHandle handle, int32_t argc, char *argv[])
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    BShellEnvOutputString(handle, BShellEnvErrString(handle, BSH_SHOW_CMD_LIST));

    int show = 0;
    BShellCommand *cmd = shell->command;
    while (cmd != NULL) {
        if ((argc >= 1) &&
            (strncmp(argv[0], cmd->name, strlen(argv[0])) == 0) &&
            (strncmp(argv[0], "help", strlen(argv[0])) != 0)) {
            BShellCmdOutputCmdHelp(handle, cmd);
            show = 1;
        }
        cmd = cmd->next;
    }
    if (show) {
        return 0;
    }
    cmd = shell->command;
    while (cmd != NULL) {
        BShellCmdOutputCmdHelp(handle, cmd);
        cmd = cmd->next;
    }
    return 0;
}

static int32_t BShellCmdExit(BShellHandle handle, int32_t argc, char *argv[])
{
X
xlei1030 已提交
92
#ifndef STARTUP_INIT_TEST
X
xionglei6 已提交
93
    kill(getpid(), SIGINT);
X
xlei1030 已提交
94
#endif
X
xionglei6 已提交
95 96 97 98 99 100
    return 0;
}

int32_t BShellEnvOutput(BShellHandle handle, char *fmt, ...)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
M
Mupceet 已提交
101
    va_list list;
X
xionglei6 已提交
102
    va_start(list, fmt);
M
Mupceet 已提交
103
    int len = vfprintf(stdout, fmt, list);
X
xionglei6 已提交
104
    va_end(list);
M
Mupceet 已提交
105
    (void)fflush(stdout);
M
Mupceet 已提交
106
    return len;
X
xionglei6 已提交
107 108 109 110 111
}

int32_t BShellEnvOutputString(BShellHandle handle, const char *string)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
M
Mupceet 已提交
112 113
    printf("%s", string);
    (void)fflush(stdout);
M
Mupceet 已提交
114
    return strlen(string);
X
xionglei6 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
}

int32_t BShellEnvOutputPrompt(BShellHandle handle, const char *prompt)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BSH_CHECK(prompt != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    if (shell->prompt != NULL) {
        free(shell->prompt);
    }
    size_t promptLen = strlen(prompt);
    if (promptLen > BSH_CMD_NAME_END) {
        shell->prompt = strdup(prompt + promptLen - BSH_CMD_NAME_END);
        if (shell->prompt != NULL) {
            shell->prompt[0] = '.';
            shell->prompt[1] = '.';
            shell->prompt[2] = '.'; // 2 index
        }
    } else {
        shell->prompt = strdup(prompt);
    }
    return 0;
}

void BShellEnvOutputByte(BShellHandle handle, char data)
{
    BSH_CHECK(handle != NULL, return, "Invalid shell env");
M
Mupceet 已提交
142 143
    printf("%c", data);
    (void)fflush(stdout);
X
xionglei6 已提交
144 145 146 147 148 149 150
}

void BShellEnvOutputResult(BShellHandle handle, int32_t result)
{
    if (result == 0) {
        return;
    }
M
Mupceet 已提交
151 152
    printf("result: 0x%08x\r\n", result);
    (void)fflush(stdout);
X
xionglei6 已提交
153 154 155 156
}

static void BShellEnvOutputParam(BShellHandle handle, char *var)
{
X
xionglei6 已提交
157
    BShellEnvOutput(handle, (var[0] == '$') ? var + 1 : var);
X
xionglei6 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    BShellEnvOutputString(handle, " = ");
    BShellEnvOutputString(handle, BShellEnvGetStringParam(handle, var));
}

void BShellEnvBackspace(BShellHandle handle, uint32_t length)
{
    for (uint32_t i = 0; i < length; i++) {
        BShellEnvOutputString(handle, "\b \b");
    }
}

static void BShellEnvParseParam(BShellEnv *shell)
{
    uint8_t quotes = 0;
    uint8_t record = 1;
    shell->argc = 0;
    for (uint16_t i = 0; i < shell->length; i++) {
        char data = *(shell->buffer + i);
        if ((quotes != 0 || (data != ' ' && data != '\t' && data != ',')) && data != 0) {
            if (data == '\"') {
                quotes = quotes ? 0 : 1;
            }
            if (record == 1) {
M
Mupceet 已提交
181
                BSH_CHECK(shell->argc < BSH_PARAMETER_MAX_NUMBER, return, "argc out of range");
X
xionglei6 已提交
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
                shell->args[shell->argc++] = shell->buffer + i;
                record = 0;
            }
            if (*(shell->buffer + i) == '\\' && *(shell->buffer + i + 1) != 0) {
                i++;
            }
        } else {
            *(shell->buffer + i) = 0;
            record = 1;
        }
    }
}

static int32_t BShellEnvExcuteCmd(BShellEnv *shell, BShellCommand *cmd)
{
    return cmd->executer((BShellHandle)shell, shell->argc - cmd->argStart, &shell->args[cmd->argStart]);
}

static int32_t BShellEnvHandleEnter(BShellHandle handle, uint8_t data)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    if (shell->length == 0) {
        BShellEnvOutputString(shell, "\n");
        BShellEnvOutputString(handle, shell->prompt);
        return 0;
    }

    *(shell->buffer + shell->length++) = 0;
    BShellEnvParseParam(shell);
    shell->length = 0;
    shell->cursor = 0;
    if (shell->argc == 0) {
        BShellEnvOutputString(shell, shell->prompt);
        return 0;
    }

    BShellEnvOutputString(shell, "\n");
    if (strcmp((const char *)shell->args[0], "help") == 0) {
        BShellCmdHelp(handle, shell->argc, shell->args);
        BShellEnvOutputString(shell, shell->prompt);
        return 0;
    }
    if (shell->args[0][0] == '$') {
        BShellEnvOutputParam(shell, shell->args[0]);
        BShellEnvOutputString(shell, shell->prompt);
        return 0;
    }

    BShellCommand *cmd = BShellEnvGetCmd(handle, (uint32_t)shell->argc, shell->args);
    if (cmd != NULL) {
        int32_t ret = BShellEnvExcuteCmd(shell, cmd);
        BShellEnvOutputResult(shell, ret);
    } else {
        BShellEnvOutputString(shell, BShellEnvErrString(handle, BSH_CMD_NOT_EXIST));
    }
    BShellEnvOutputString(shell, shell->prompt);
    return 0;
}

static int32_t BShellEnvHandleBackspace(BShellHandle handle, uint8_t code)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    if (shell->length == 0) {
        return 0;
    }
    if (shell->cursor == shell->length) {
        shell->length--;
        shell->cursor--;
        shell->buffer[shell->length] = 0;
        BShellEnvBackspace(handle, 1);
    } else if (shell->cursor > 0) {
        for (short i = 0; i < shell->length - shell->cursor; i++) {
            shell->buffer[shell->cursor + i - 1] = shell->buffer[shell->cursor + i];
        }
        shell->length--;
        shell->cursor--;
        shell->buffer[shell->length] = 0;
        BShellEnvOutputByte(shell, '\b');
        for (short i = shell->cursor; i < shell->length; i++) {
            BShellEnvOutputByte(shell, shell->buffer[i]);
        }
        BShellEnvOutputByte(shell, ' ');
        for (short i = shell->length - shell->cursor + 1; i > 0; i--) {
            BShellEnvOutputByte(shell, '\b');
        }
    }
    return 0;
}

static int32_t BShellEnvHandleTab(BShellHandle handle, uint8_t code)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    return 0;
}

static void BShellEnvHandleNormal(BShellHandle handle, uint8_t data)
{
    BSH_CHECK(handle != NULL, return, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    if (data == 0) {
        return;
    }
    if (shell->length < BSH_COMMAND_MAX_LENGTH - 1) {
        if (shell->length == shell->cursor) {
            shell->buffer[shell->length++] = data;
            shell->cursor++;
            BShellEnvOutputByte(shell, data);
        } else {
            for (uint16_t i = shell->length - shell->cursor; i > 0; i--) {
                shell->buffer[shell->cursor + i] = shell->buffer[shell->cursor + i - 1];
            }
            shell->buffer[shell->cursor++] = data;
            shell->buffer[++shell->length] = 0;
            for (uint16_t i = shell->cursor - 1; i < shell->length; i++) {
                BShellEnvOutputByte(shell, shell->buffer[i]);
            }
            for (uint16_t i = shell->length - shell->cursor; i > 0; i--) {
                BShellEnvOutputByte(shell, '\b');
            }
        }
    } else {
        BShellEnvOutputString(shell, BShellEnvErrString(handle, BSH_CMD_TOO_LONG));
        BShellEnvOutputString(shell, shell->prompt);
A
an1212 已提交
307

X
xionglei6 已提交
308 309 310 311 312 313 314 315
        shell->cursor = shell->length;
    }
}

static int32_t BShellEnvHandleCtrC(BShellHandle handle, uint8_t code)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BSH_LOGV("BShellEnvHandleCtrC %d", getpid());
X
xlei1030 已提交
316
#ifndef STARTUP_INIT_TEST
X
xionglei6 已提交
317
    kill(getpid(), SIGKILL);
X
xlei1030 已提交
318
#endif
X
xionglei6 已提交
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
    return 0;
}

static int32_t BShellEnvHandleEsc(BShellHandle handle, uint8_t code)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    shell->shellState = BSH_ANSI_ESC;
    return 0;
}

BShellKey *BShellEnvGetDefaultKey(uint8_t code)
{
    static BShellKey defaultKeys[] = {
        {BSH_KEY_LF, BShellEnvHandleEnter, NULL},
        {BSH_KEY_CR, BShellEnvHandleEnter, NULL},
        {BSH_KEY_TAB, BShellEnvHandleTab, NULL},
        {BSH_KEY_BACKSPACE, BShellEnvHandleBackspace, NULL},
        {BSH_KEY_DELETE, BShellEnvHandleBackspace, NULL},
        {BSH_KEY_CTRLC, BShellEnvHandleCtrC, NULL},
        {BSH_KEY_ESC, BShellEnvHandleEsc, NULL},
    };
    for (size_t i = 0; i < sizeof(defaultKeys) / sizeof(defaultKeys[0]); i++) {
        if (defaultKeys[i].keyCode == code) {
            return &defaultKeys[i];
        }
    }
    return NULL;
}

X
xlei1030 已提交
349
SHELLSTATIC void BShellEnvProcessInput(BShellHandle handle, char data)
X
xionglei6 已提交
350 351 352 353 354 355 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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
{
    BSH_CHECK(handle != NULL, return, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    if (shell->shellState == BSH_IN_NORMAL) {
        BShellKey *key = BShellEnvGetKey(handle, data);
        if (key != NULL) {
            key->keyHandle(shell, (uint8_t)data);
            return;
        }
        key = BShellEnvGetDefaultKey(data);
        if (key != NULL) {
            key->keyHandle(shell, (uint8_t)data);
            return;
        }
        BShellEnvHandleNormal(shell, data);
    } else if (shell->shellState == BSH_ANSI_CSI) {
        switch (data) {
            case 0x41:  // up
                break;
            case 0x42:  // down
                break;
            case 0x43:  // ->
                if (shell->cursor < shell->length) {
                    BShellEnvOutputByte(handle, shell->buffer[shell->cursor]);
                    shell->cursor++;
                }
                break;
            case 0x44:  // <-
                if (shell->cursor > 0) {
                    BShellEnvOutputByte(shell, '\b');
                    shell->cursor--;
                }
                break;
            default:
                break;
        }
        shell->shellState = BSH_IN_NORMAL;
    } else if (shell->shellState == BSH_ANSI_ESC) {
        if (data == 0x5B) { // input up/down
            shell->shellState = BSH_ANSI_CSI;
        } else {
            shell->shellState = BSH_IN_NORMAL;
        }
    }
}

void BShellEnvLoop(BShellHandle handle)
{
    BSH_CHECK(handle != NULL, return, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    BSH_CHECK(shell->input != NULL, return, "Invalid shell input");
    while (1) {
        char data = 0;
        if (shell->input(&data, 1) == 1) {
            BShellEnvProcessInput(shell, data);
        }
    }
}

M
Mupceet 已提交
409
int32_t BShellEnvInit(BShellHandle *handle, const BShellInfo *info)
X
xionglei6 已提交
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
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BSH_CHECK(info != NULL && info->prompt != NULL, return BSH_INVALID_PARAM, "Invalid cmd name");

    BShellEnv *shell = (BShellEnv *)calloc(1, sizeof(BShellEnv));
    BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Failed to create shell env");
    shell->length = 0;
    shell->cursor = 0;
    shell->shellState = BSH_IN_NORMAL;
    shell->input = info->input;
    shell->prompt = strdup(info->prompt);
    shell->command = NULL;
    shell->param = NULL;
    shell->keyHandle = NULL;
    shell->execMode = BSH_EXEC_INDEPENDENT;
    *handle = (BShellHandle)shell;
    return 0;
}

int BShellEnvStart(BShellHandle handle)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    shell->execMode = BSH_EXEC_TASK;
    BShellEnvOutputString(handle, BShellEnvErrString(handle, BSH_SHELL_INFO));
    BShellEnvOutputString(handle, shell->prompt);

M
Mupceet 已提交
437
    const CmdInfo infos[] = {
X
xionglei6 已提交
438 439 440 441
        {"exit", BShellCmdExit, "exit parameter shell", "exit"},
        {"help", BShellCmdHelp, "help command", "help"}
    };
    for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
C
cheng_jinsong 已提交
442
        BShellEnvRegisterCmd(handle, &infos[i]);
X
xionglei6 已提交
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
    }
    return 0;
}

static void BShellParamFree(BShellParam *param)
{
    if (param->desc != NULL) {
        free(param->desc);
    }
    if (param->type == PARAM_STRING && param->value.string != NULL) {
        free(param->value.string);
    }
    free(param);
}

static void BShellCmdFree(BShellCommand *cmd)
{
    if (cmd->desc != NULL) {
        free(cmd->desc);
        cmd->desc = NULL;
    }
    if (cmd->help != NULL) {
        free(cmd->help);
        cmd->help = NULL;
    }
    if (cmd->multikey != NULL) {
        free(cmd->multikey);
        cmd->multikey = NULL;
    }
    free(cmd);
}

void BShellEnvDestory(BShellHandle handle)
{
    BSH_CHECK(handle != NULL, return, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;

    BShellCommand *cmd = shell->command;
    while (cmd != NULL) {
        shell->command = cmd->next;
        BShellCmdFree(cmd);
        cmd = shell->command;
    }

    BShellParam *param = shell->param;
    while (param != NULL) {
        shell->param = param->next;
        BShellParamFree(param);
        param = shell->param;
    }

    BShellKey *key = shell->keyHandle;
    while (key != NULL) {
        shell->keyHandle = key->next;
        free(key);
        key = shell->keyHandle;
    }
    if (shell->prompt != NULL) {
        free(shell->prompt);
    }
    free(shell);
}

C
cheng_jinsong 已提交
506
int32_t BShellEnvRegisterCmd(BShellHandle handle, const CmdInfo *cmdInfo)
X
xionglei6 已提交
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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BSH_CHECK(cmdInfo != NULL && cmdInfo->name != NULL, return BSH_INVALID_PARAM, "Invalid cmd name");
    BSH_CHECK(cmdInfo->executer != NULL, return BSH_INVALID_PARAM, "Invalid cmd executer");
    BShellEnv *shell = (BShellEnv *)handle;
    size_t nameLen = strlen(cmdInfo->name) + 1;
    BShellCommand *cmd = (BShellCommand *)calloc(1, sizeof(BShellCommand) + nameLen);
    BSH_CHECK(cmd != NULL, return BSH_INVALID_PARAM, "Failed to alloc cmd name %s", cmdInfo->name);
    cmd->executer = cmdInfo->executer;
    cmd->argStart = 0;
    int32_t ret = 0;
    do {
        ret = strcpy_s(cmd->name, nameLen, cmdInfo->name);
        BSH_CHECK(ret == 0, break, "Failed to copy name %s", cmdInfo->name);

        ret = BSH_SYSTEM_ERR;
        if (cmdInfo->desc != NULL) {
            cmd->desc = strdup(cmdInfo->desc);
            BSH_CHECK(cmd->desc != NULL, break, "Failed to copy desc %s", cmdInfo->name);
        }
        if (cmdInfo->help != NULL) {
            cmd->help = strdup(cmdInfo->help);
            BSH_CHECK(cmd->help != NULL, break, "Failed to copy help %s", cmdInfo->name);
        }
        cmd->multikey = NULL;
        if (cmdInfo->multikey != NULL && strlen(cmdInfo->multikey) > nameLen) {
            cmd->multikey = strdup(cmdInfo->multikey);
            BSH_CHECK(cmd->multikey != NULL, break, "Failed to copy multikey %s", cmdInfo->name);
            int argc = SplitString(cmd->multikey, " ", cmd->multikeys, (int)ARRAY_LENGTH(cmd->multikeys));
            BSH_CHECK(argc <= (int)ARRAY_LENGTH(cmd->multikeys) && argc > 0, break, "Invalid multikey");
            cmd->argStart = argc - 1;
            if (argc == 1) {
                free(cmd->multikey);
                cmd->multikey = NULL;
            }
        }
        ret = 0;
    } while (0);
    if (ret != 0) {
        BShellCmdFree(cmd);
        return ret;
    }
    cmd->next = shell->command;
    shell->command = cmd;
    return 0;
}

X
xionglei6 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
static const char *GetRealCmdName(const char *name)
{
    int i = 0;
    int last = 0;
    while (*(name + i) != '\0') {
        if (*(name + i) == '/') {
            last = i;
        }
        i++;
    }
    if ((last != 0) && (name + last != NULL)) {
        return name + last + 1;
    } else {
        return name;
    }
}

X
xionglei6 已提交
571 572 573 574
BShellCommand *BShellEnvGetCmd(BShellHandle handle, int32_t argc, char *argv[])
{
    BSH_CHECK(handle != NULL, return NULL, "Invalid shell env");
    BSH_CHECK(argc >= 1, return NULL, "Invalid argc");
X
xionglei6 已提交
575 576
    const char *cmdName = GetRealCmdName(argv[0]);
    BSH_LOGV("BShellEnvGetCmd %s cmd %s", argv[0], cmdName);
X
xionglei6 已提交
577 578 579
    BShellEnv *shell = (BShellEnv *)handle;
    BShellCommand *cmd = shell->command;
    while (cmd != NULL) {
X
xionglei6 已提交
580
        if (strcmp(cmd->name, cmdName) != 0) {
X
xionglei6 已提交
581 582 583 584 585 586 587 588 589 590 591
            cmd = cmd->next;
            continue;
        }
        if (cmd->multikey == NULL) {
            return cmd;
        }
        int32_t i = 0;
        for (i = 0; i < (int32_t)ARRAY_LENGTH(cmd->multikeys) && i < argc; i++) {
            if (cmd->multikeys[i] == NULL) {
                return cmd;
            }
X
xionglei6 已提交
592 593 594 595 596
            char *tmp = argv[i];
            if (i == 0) {
                tmp = (char *)cmdName;
            }
            if (strcmp(cmd->multikeys[i], tmp) != 0) {
X
xionglei6 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
                break;
            }
        }
        if (i >= (int32_t)ARRAY_LENGTH(cmd->multikeys)) {
            return cmd;
        }
        if (i >= argc) {
            if (cmd->multikeys[i] == NULL) {
                return cmd;
            }
        }
        cmd = cmd->next;
    }
    return NULL;
}

int32_t BShellEnvRegisterKeyHandle(BShellHandle handle, uint8_t code, BShellkeyHandle keyHandle)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BSH_CHECK(keyHandle != NULL, return BSH_INVALID_PARAM, "Invalid cmd name");
    BShellEnv *shell = (BShellEnv *)handle;

    BShellKey *key = (BShellKey *)calloc(1, sizeof(BShellKey));
    BSH_CHECK(key != NULL, return BSH_INVALID_PARAM, "Failed to alloc key code %d", code);
    key->keyCode = code;
    key->keyHandle = keyHandle;
    key->next = shell->keyHandle;
    shell->keyHandle = key;
    return 0;
}

BShellKey *BShellEnvGetKey(BShellHandle handle, uint8_t code)
{
    BSH_CHECK(handle != NULL, return NULL, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    BShellKey *key = shell->keyHandle;
    while (key != NULL) {
        if (key->keyCode == code) {
            return key;
        }
        key = key->next;
    }
    return NULL;
}

static int32_t BShellParamSetValue(BShellParam *param, void *value)
{
    static uint32_t paramValueLens[] = {
        sizeof(uint8_t), sizeof(uint16_t), sizeof(uint32_t), sizeof(char *)
    };
    if (param->type == PARAM_STRING) {
        param->value.string = strdup((char *)value);
        BSH_CHECK(param->value.string != NULL, return BSH_SYSTEM_ERR, "Failed to copy value for %s", param->name);
    } else if (param->type < PARAM_STRING) {
        int ret = memcpy_s(&param->value, sizeof(param->value), value, paramValueLens[param->type]);
        BSH_CHECK(ret == 0, return BSH_SYSTEM_ERR, "Failed to copy value for %s", param->name);
    }
    return 0;
}

int32_t BShellEnvSetParam(BShellHandle handle, const char *name, const char *desc, BShellParamType type, void *value)
{
    BSH_CHECK(handle != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
    BSH_CHECK(name != NULL, return BSH_INVALID_PARAM, "Invalid param name");
    BSH_CHECK(value != NULL, return BSH_INVALID_PARAM, "Invalid cmd value");
    BSH_CHECK(type <= PARAM_STRING, return BSH_INVALID_PARAM, "Invalid param type");
    BShellEnv *shell = (BShellEnv *)handle;
    const BShellParam *tmp = BShellEnvGetParam(handle, name);
    if (tmp != NULL) {
        BSH_CHECK(tmp->type <= type, return BSH_INVALID_PARAM, "Invalid param type %d", tmp->type);
        return BShellParamSetValue((BShellParam *)tmp, value);
    }
    int32_t ret = 0;
    BShellParam *param = NULL;
    do {
        size_t nameLen = strlen(name) + 1;
        param = (BShellParam *)calloc(1, sizeof(BShellParam) + nameLen);
        BSH_CHECK(param != NULL, return BSH_SYSTEM_ERR, "Failed to alloc cmd name %s", name);
        param->type = type;
        ret = strcpy_s(param->name, nameLen, name);
        BSH_CHECK(ret == 0, break, "Failed to copy name %s", name);
        if (desc != NULL) {
            param->desc = strdup(desc);
        }
        ret = BShellParamSetValue(param, value);
        BSH_CHECK(ret == 0, break, "Failed set value for %s", name);

        param->next = shell->param;
        shell->param = param;
    } while (0);
    if (ret != 0) {
        BShellParamFree(param);
    }
    return ret;
}

const BShellParam *BShellEnvGetParam(BShellHandle handle, const char *name)
{
    BSH_CHECK(handle != NULL, return NULL, "Invalid shell env");
    BShellEnv *shell = (BShellEnv *)handle;
    BShellParam *param = shell->param;
    while (param != NULL) {
        if (strcmp(name, param->name) == 0) {
            return param;
        }
        param = param->next;
    }
    return NULL;
}

const char *BShellEnvGetStringParam(BShellHandle handle, const char *name)
{
    BSH_CHECK(handle != NULL, return "", "Invalid shell env");
    const BShellParam *param = BShellEnvGetParam(handle, name);
    if (param == NULL) {
        return "";
    }
    switch (param->type) {
        case PARAM_STRING:
            return param->value.string;
        default:
            break;
    }
    return "";
}

const ParamInfo *BShellEnvGetReservedParam(BShellHandle handle, const char *name)
{
    BSH_CHECK(handle != NULL, return NULL, "Invalid shell env");
    static ParamInfo reservedParams[] = {
727
        {PARAM_REVERESD_NAME_CURR_PARAMETER, "current parameter", PARAM_STRING}
X
xionglei6 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    };
    for (size_t i = 0; i < sizeof(reservedParams) / sizeof(reservedParams[0]); i++) {
        if (strcmp(name, reservedParams[i].name) == 0) {
            return &reservedParams[i];
        }
    }
    return NULL;
}

int32_t BShellEnvDirectExecute(BShellHandle handle, int argc, char *args[])
{
    BSH_CHECK(handle != NULL, return -1, "Invalid shell env");
    BShellCommand *cmd = BShellEnvGetCmd(handle, argc, args);
    if (cmd != NULL) {
        int32_t ret = cmd->executer(handle, argc - cmd->argStart, &args[cmd->argStart]);
        BShellEnvOutputResult(handle, ret);
    } else {
        if (argc > 1) {
            BShellEnvOutputString(handle, BShellEnvErrString(handle, BSH_CMD_NOT_EXIST));
        }
        BShellCmdHelp(handle, argc, args);
    }
    return 0;
751
}