• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "napi/native_api.h"
17 #include <cstring>
18 #include <fcntl.h>
19 #include <getopt.h>
20 #include <glob.h>
21 #include <js_native_api_types.h>
22 #include <sys/random.h>
23 
24 #define SUCCESS 1
25 #define FAIL (-1)
26 #define NO_ERRS 0
27 #define LENGTH 64
28 #define DEFAULT_VALUE 0
29 #define TEST_ID_VALUE 100
30 #define PARAM_UNNORMAL (-1)
31 #define PARAM_0 0
32 #define PARAM_1 1
33 #define PARAM_2 2
34 
GetoptLong(napi_env env,napi_callback_info info)35 static napi_value GetoptLong(napi_env env, napi_callback_info info)
36 {
37     int argcc = PARAM_2;
38     char argvl[] = "-l";
39 
40     char argvn[] = "-n";
41     char *argv[2] = {argvn, argvl};
42     char shortOptions[] = "nbl:";
43     struct option long_options[] = {
44         {"name", DEFAULT_VALUE, nullptr, 'n'},
45         {"bf_name", DEFAULT_VALUE, nullptr, 'b'},
46         {"love", PARAM_1, nullptr, 'l'},
47         {DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE},
48     };
49     int ret;
50     ret = getopt_long(argcc, argv, shortOptions, long_options, nullptr);
51 
52     napi_value result = nullptr;
53     if (ret == PARAM_UNNORMAL) {
54         napi_create_int32(env, FAIL, &result);
55     } else {
56         napi_create_int32(env, NO_ERRS, &result);
57     }
58     return result;
59 }
60 
GetoptLongOnly(napi_env env,napi_callback_info info)61 static napi_value GetoptLongOnly(napi_env env, napi_callback_info info)
62 {
63     optind = PARAM_0;
64     int optArgc = PARAM_2;
65     char cmdline[50] = "./parse_cmdline";
66     char optArgv1[] = "a";
67     char *optArgv[] = {cmdline, optArgv1};
68 
69     char optString[50] = "ab:c:d";
70     int ret = PARAM_0;
71     struct option long_options[] = {
72         {"name", PARAM_0, nullptr, 'a'},
73         {"bf_name", PARAM_0, nullptr, 'b'},
74         {"love", PARAM_1, nullptr, 'c'},
75         {PARAM_0, PARAM_0, PARAM_0, PARAM_0},
76     };
77     getopt_long_only(optArgc, optArgv, optString, long_options, nullptr);
78     napi_value result = nullptr;
79     if (ret == PARAM_UNNORMAL) {
80         napi_create_int32(env, FAIL, &result);
81     } else {
82         napi_create_int32(env, DEFAULT_VALUE, &result);
83     }
84     return result;
85 }
86 
87 EXTERN_C_START
Init(napi_env env,napi_value exports)88 static napi_value Init(napi_env env, napi_value exports)
89 {
90     napi_property_descriptor desc[] = {
91         {"getoptLong", nullptr, GetoptLong, nullptr, nullptr, nullptr, napi_default, nullptr},
92         {"getoptLongOnly", nullptr, GetoptLongOnly, nullptr, nullptr, nullptr, napi_default, nullptr},
93     };
94     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
95     return exports;
96 }
97 EXTERN_C_END
98 
99 static napi_module demoModule = {
100     .nm_version = 1,
101     .nm_flags = 0,
102     .nm_filename = nullptr,
103     .nm_register_func = Init,
104     .nm_modname = "libunistd",
105     .nm_priv = ((void *)0),
106     .reserved = {0},
107 };
108 
RegisterModule(void)109 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
110