xref: /kernel/liteos_a/apps/shell/src/shcmdparse.c (revision 0d163575)
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 *    conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 *    of conditions and the following disclaimer in the documentation and/or other materials
13 *    provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 *    to endorse or promote products derived from this software without specific prior written
17 *    permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "shcmd.h"
33#include "sherr.h"
34
35
36/*
37 * Filter out double quote or single-quoted strings at both ends
38 */
39char *OsCmdParseStrdup(const char *str)
40{
41    char *tempStr = NULL;
42    char *newStr = NULL;
43
44    newStr = (char *)malloc(strlen(str) + 1);
45    if (newStr == NULL) {
46        return NULL;
47    }
48
49    tempStr = newStr;
50    for (; *str != '\0'; str++) {
51        if ((*str == '\"') || (*str == '\'')) {
52            continue;
53        }
54        *newStr = *str;
55        newStr++;
56    }
57    *newStr = '\0';
58    return tempStr;
59}
60
61unsigned int OsCmdParseParaGet(char **value, const char *paraTokenStr)
62{
63    if ((paraTokenStr == NULL) || (value == NULL)) {
64        return (unsigned int)SH_ERROR;
65    }
66    *value = OsCmdParseStrdup(paraTokenStr);
67    if (*value == NULL) {
68        return SH_NOK;
69    }
70    return SH_OK;
71}
72
73unsigned int OsCmdParseOneToken(CmdParsed *cmdParsed, unsigned int index, const char *token)
74{
75    unsigned int ret = SH_OK;
76    unsigned int tempLen;
77
78    if (cmdParsed == NULL) {
79        return (unsigned int)SH_ERROR;
80    }
81
82    if (index == 0) {
83        if (cmdParsed->cmdType != CMD_TYPE_STD) {
84            return ret;
85        }
86    }
87
88    if ((token != NULL) && (cmdParsed->paramCnt < CMD_MAX_PARAS)) {
89        tempLen = cmdParsed->paramCnt;
90        ret = OsCmdParseParaGet(&(cmdParsed->paramArray[tempLen]), token);
91        if (ret != SH_OK) {
92            return ret;
93        }
94        cmdParsed->paramCnt++;
95    }
96    return ret;
97}
98
99unsigned int OsCmdTokenSplit(char *cmdStr, char split, CmdParsed *cmdParsed)
100{
101    enum {
102        STAT_INIT,
103        STAT_TOKEN_IN,
104        STAT_TOKEN_OUT
105    } state = STAT_INIT;
106    unsigned int count = 0;
107    char *p = NULL;
108    char *token = cmdStr;
109    unsigned int ret = SH_OK;
110    bool quotes = FALSE;
111
112    if (cmdStr == NULL) {
113        return (unsigned int)SH_ERROR;
114    }
115
116    for (p = cmdStr; (*p != '\0') && (ret == SH_OK); p++) {
117        if (*p == '\"') {
118            SWITCH_QUOTES_STATUS(quotes);
119        }
120        switch (state) {
121            case STAT_INIT:
122            case STAT_TOKEN_IN:
123                if ((*p == split) && QUOTES_STATUS_CLOSE(quotes)) {
124                    *p = '\0';
125                    ret = OsCmdParseOneToken(cmdParsed, count++, token);
126                    state = STAT_TOKEN_OUT;
127                }
128                break;
129            case STAT_TOKEN_OUT:
130                if (*p != split) {
131                    token = p;
132                    state = STAT_TOKEN_IN;
133                }
134                break;
135            default:
136                break;
137        }
138    }
139
140    if (((ret == SH_OK) && (state == STAT_TOKEN_IN)) || (state == STAT_INIT)) {
141        ret = OsCmdParseOneToken(cmdParsed, count, token);
142    }
143
144    return ret;
145}
146
147unsigned int OsCmdParse(char *cmdStr, CmdParsed *cmdParsed)
148{
149    if ((cmdStr == NULL) || (cmdParsed == NULL) || (strlen(cmdStr) == 0)) {
150        return (unsigned int)SH_ERROR;
151    }
152    return OsCmdTokenSplit(cmdStr, ' ', cmdParsed);
153}
154
155