• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "nstackx_getopt.h"
17 #include "nstackx_error.h"
18 #include "nstackx_log.h"
19 #include "nstackx_util.h"
20 
21 #define TAG "nStackXGetOpt"
22 #define NSTACK_GETOPT_MAX_ARGC 100
23 
NstackInitGetOptMsg(NstackGetOptMsg * optMsg)24 int32_t NstackInitGetOptMsg(NstackGetOptMsg *optMsg)
25 {
26     if (optMsg == NULL) {
27         LOGE(TAG, "optMsg is NULL");
28         return NSTACKX_EFAILED;
29     }
30     optMsg->argvIdx = 1;
31     optMsg->argvOffset = 1;
32     optMsg->attachArg = NULL;
33     return NSTACKX_EOK;
34 }
35 
GetOptCheckInputArg(NstackGetOptMsg * optMsg,int argc,const char * const * argv)36 static int32_t GetOptCheckInputArg(NstackGetOptMsg *optMsg, int argc, const char *const *argv)
37 {
38     if (optMsg->argvIdx >= argc || argv[optMsg->argvIdx][0] != '-' || argv[optMsg->argvIdx][1] == '\0') {
39         return NSTACKX_EFAILED;
40     } else if (!strcmp(argv[optMsg->argvIdx], "--")) {
41         optMsg->argvIdx++;
42         return NSTACKX_EFAILED;
43     }
44     return NSTACKX_EOK;
45 }
46 
GetOptCheckCurrentOpt(NstackGetOptMsg * optMsg,int32_t currentOpt,char * currentOptIdxInOpts,char nextChar)47 static int32_t GetOptCheckCurrentOpt(NstackGetOptMsg *optMsg, int32_t currentOpt,
48     char *currentOptIdxInOpts, char nextChar)
49 {
50     if (currentOpt == ':' || currentOptIdxInOpts == NULL) {
51         LOGE(TAG, ": illegal option -- %c", currentOpt);
52         optMsg->argvOffset++;
53         if (nextChar == '\0') {
54             optMsg->argvIdx++;
55             optMsg->argvOffset = 1;
56         }
57         return NSTACKX_EFAILED;
58     }
59     return NSTACKX_EOK;
60 }
61 
GetOptParseAttachArg(NstackGetOptMsg * optMsg,int32_t argc,const char * const * argv,char currentOpt,const char * currentOptIdxInOpts)62 static int32_t GetOptParseAttachArg(NstackGetOptMsg *optMsg, int32_t argc, const char *const *argv,
63     char currentOpt, const char *currentOptIdxInOpts)
64 {
65     if (*(currentOptIdxInOpts + 1) == ':') {
66         if (argv[optMsg->argvIdx][optMsg->argvOffset + 1] != '\0') {
67             optMsg->attachArg = &argv[optMsg->argvIdx++][optMsg->argvOffset + 1];
68         } else if (++optMsg->argvIdx >= argc) {
69             LOGE(TAG, ": option requires an argument -- %c", currentOpt);
70             optMsg->argvOffset = 1;
71             return NSTACKX_EFAILED;
72         } else {
73             optMsg->attachArg = argv[optMsg->argvIdx++];
74         }
75         optMsg->argvOffset = 1;
76     } else {
77         if (argv[optMsg->argvIdx][++(optMsg->argvOffset)] == '\0') {
78             optMsg->argvOffset = 1;
79             optMsg->argvIdx++;
80         }
81         optMsg->attachArg = NULL;
82     }
83     return NSTACKX_EOK;
84 }
85 
NstackCheckArg(const NstackGetOptMsg * optMsg,int32_t argc,const char * const * argv)86 static int32_t NstackCheckArg(const NstackGetOptMsg *optMsg, int32_t argc, const char *const *argv)
87 {
88     if (optMsg == NULL) {
89         LOGE(TAG, "optMsg is NULL");
90         return NSTACKX_EFAILED;
91     }
92     if (argc <= 1 || argc > NSTACK_GETOPT_MAX_ARGC) {
93         LOGE(TAG, "argc is invalid %u", argc);
94         return NSTACKX_EFAILED;
95     }
96     if (argv == NULL) {
97         LOGE(TAG, "argv is NULL");
98         return NSTACKX_EFAILED;
99     }
100     int32_t i;
101     for (i = 0; i < argc; i++) {
102         if (argv[i] == NULL) {
103             LOGE(TAG, "argv[%d] is NULL", i);
104             return NSTACKX_EFAILED;
105         }
106     }
107     return NSTACKX_EOK;
108 }
109 
NstackGetOpt(NstackGetOptMsg * optMsg,int32_t argc,const char * const * argv,const char * opts)110 int32_t NstackGetOpt(NstackGetOptMsg *optMsg, int32_t argc, const char *const *argv, const char *opts)
111 {
112     if (NstackCheckArg(optMsg, argc, argv) != NSTACKX_EOK) {
113         return NSTACK_GETOPT_END_OF_STR;
114     }
115     int32_t currentOpt;
116     if (optMsg->argvOffset == 1 && GetOptCheckInputArg(optMsg, argc, argv) != NSTACKX_EOK) {
117         return NSTACK_GETOPT_END_OF_STR;
118     }
119     currentOpt = argv[optMsg->argvIdx][optMsg->argvOffset];
120     char *currentOptIdxInOpts = strchr(opts, currentOpt);
121     if (GetOptCheckCurrentOpt(optMsg, currentOpt, currentOptIdxInOpts,
122         argv[optMsg->argvIdx][optMsg->argvOffset + 1]) != NSTACKX_EOK) {
123         return NSTACK_GETOPT_UNKNOW_OPT;
124     }
125     if (GetOptParseAttachArg(optMsg, argc, argv, currentOpt, currentOptIdxInOpts) != NSTACKX_EOK) {
126         return NSTACK_GETOPT_UNKNOW_OPT;
127     }
128     return currentOpt;
129 }
130 
NstackGetOptArgs(const NstackGetOptMsg * optMsg)131 const char *NstackGetOptArgs(const NstackGetOptMsg *optMsg)
132 {
133     if (optMsg == NULL) {
134         LOGE(TAG, "optMsg is NULL");
135         return NULL;
136     }
137     return optMsg->attachArg;
138 }
139 
140