• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "los_memory.h"
34 
35 
36 /*
37  * Filter out double quote or single-quoted strings at both ends
38  */
OsCmdParseStrdup(const CHAR * str)39 LITE_OS_SEC_TEXT_MINOR CHAR *OsCmdParseStrdup(const CHAR *str)
40 {
41     CHAR *tempStr = NULL;
42     CHAR *newStr = NULL;
43 
44     newStr = (CHAR *)LOS_MemAlloc(m_aucSysMem0, 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 
OsCmdParseParaGet(CHAR ** value,const CHAR * paraTokenStr)61 LITE_OS_SEC_TEXT_MINOR UINT32 OsCmdParseParaGet(CHAR **value, const CHAR *paraTokenStr)
62 {
63     if ((paraTokenStr == NULL) || (value == NULL)) {
64         return (UINT32)OS_ERROR;
65     }
66 
67     *value = OsCmdParseStrdup(paraTokenStr);
68     if (*value == NULL) {
69         return LOS_NOK;
70     }
71     return LOS_OK;
72 }
73 
OsCmdParseOneToken(CmdParsed * cmdParsed,UINT32 index,const CHAR * token)74 LITE_OS_SEC_TEXT_MINOR UINT32 OsCmdParseOneToken(CmdParsed *cmdParsed, UINT32 index, const CHAR *token)
75 {
76     UINT32 ret = LOS_OK;
77     UINT32 tempLen;
78 
79     if (cmdParsed == NULL) {
80         return (UINT32)OS_ERROR;
81     }
82 
83     if (index == 0) {
84         if (cmdParsed->cmdType != CMD_TYPE_STD) {
85             return ret;
86         }
87     }
88 
89     if ((token != NULL) && (cmdParsed->paramCnt < CMD_MAX_PARAS)) {
90         tempLen = cmdParsed->paramCnt;
91         ret = OsCmdParseParaGet(&(cmdParsed->paramArray[tempLen]), token);
92         if (ret != LOS_OK) {
93             return ret;
94         }
95         cmdParsed->paramCnt++;
96     }
97     return ret;
98 }
99 
OsCmdTokenSplit(CHAR * cmdStr,CHAR split,CmdParsed * cmdParsed)100 LITE_OS_SEC_TEXT_MINOR UINT32 OsCmdTokenSplit(CHAR *cmdStr, CHAR split, CmdParsed *cmdParsed)
101 {
102     enum {
103         STAT_INIT,
104         STAT_TOKEN_IN,
105         STAT_TOKEN_OUT
106     } state = STAT_INIT;
107     UINT32 count = 0;
108     CHAR *p = NULL;
109     CHAR *token = cmdStr;
110     UINT32 ret = LOS_OK;
111     BOOL quotes = FALSE;
112 
113     if (cmdStr == NULL) {
114         return (UINT32)OS_ERROR;
115     }
116 
117     for (p = cmdStr; (*p != '\0') && (ret == LOS_OK); p++) {
118         if (*p == '\"') {
119             SWITCH_QUOTES_STATUS(quotes);
120         }
121         switch (state) {
122             case STAT_INIT:
123             case STAT_TOKEN_IN:
124                 if ((*p == split) && QUOTES_STATUS_CLOSE(quotes)) {
125                     *p = '\0';
126                     ret = OsCmdParseOneToken(cmdParsed, count++, token);
127                     state = STAT_TOKEN_OUT;
128                 }
129                 break;
130             case STAT_TOKEN_OUT:
131                 if (*p != split) {
132                     token = p;
133                     state = STAT_TOKEN_IN;
134                 }
135                 break;
136             default:
137                 break;
138         }
139     }
140 
141     if (((ret == LOS_OK) && (state == STAT_TOKEN_IN)) || (state == STAT_INIT)) {
142         ret = OsCmdParseOneToken(cmdParsed, count, token);
143     }
144 
145     return ret;
146 }
147 
OsCmdParse(CHAR * cmdStr,CmdParsed * cmdParsed)148 LITE_OS_SEC_TEXT_MINOR UINT32 OsCmdParse(CHAR *cmdStr, CmdParsed *cmdParsed)
149 {
150     if ((cmdStr == NULL) || (cmdParsed == NULL) || (strlen(cmdStr) == 0)) {
151         return (UINT32)OS_ERROR;
152     }
153     return OsCmdTokenSplit(cmdStr, ' ', cmdParsed);
154 }
155 
156