• 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     size_t argc = PARAM_1;
38     napi_value args[1] = {nullptr};
39     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
40     int param;
41     napi_get_value_int32(env, args[0], &param);
42 
43     int ret;
44     if (param == PARAM_0) {
45         int argcc = PARAM_2;
46         char argvl[] = "-l";
47         char argvn[] = "-n";
48         char *argv[2] = {argvn, argvl};
49         char short_options[] = "nbl:";
50         struct option longOptions[] = {
51             {"name", DEFAULT_VALUE, nullptr, 'n'},
52             {"bf_name", DEFAULT_VALUE, nullptr, 'b'},
53             {"love", PARAM_1, nullptr, 'l'},
54             {DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE},
55         };
56         ret = getopt_long(argcc, argv, short_options, longOptions, nullptr);
57     } else {
58         ret = getopt_long(PARAM_0, PARAM_0, PARAM_0, PARAM_0, nullptr);
59     }
60 
61     napi_value result = nullptr;
62     if (ret == PARAM_UNNORMAL) {
63         napi_create_int32(env, FAIL, &result);
64     } else {
65         napi_create_int32(env, NO_ERRS, &result);
66     }
67     return result;
68 }
69 
GetoptLongOnly(napi_env env,napi_callback_info info)70 static napi_value GetoptLongOnly(napi_env env, napi_callback_info info)
71 {
72     size_t argc = PARAM_1;
73     napi_value args[1] = {nullptr};
74     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
75     int param;
76     napi_get_value_int32(env, args[0], &param);
77 
78     int ret;
79     if (param == PARAM_0) {
80         optind = PARAM_0;
81         int optArgc = PARAM_2;
82         char cmdline[50] = "./parse_cmdline";
83         char optArgv1[] = "a";
84         char *optArgv[] = {cmdline, optArgv1};
85 
86         char optString[50] = "ab:c:d";
87         struct option longOptions[] = {
88             {"name", PARAM_0, nullptr, 'a'},
89             {"bf_name", PARAM_0, nullptr, 'b'},
90             {"love", PARAM_1, nullptr, 'c'},
91             {PARAM_0, PARAM_0, PARAM_0, PARAM_0},
92         };
93         ret = getopt_long_only(optArgc, optArgv, optString, longOptions, nullptr);
94     } else {
95         ret = getopt_long_only(PARAM_0, PARAM_0, PARAM_0, PARAM_0, nullptr);
96     }
97 
98     napi_value result = nullptr;
99     if (ret == PARAM_UNNORMAL) {
100         napi_create_int32(env, FAIL, &result);
101     } else {
102         napi_create_int32(env, DEFAULT_VALUE, &result);
103     }
104     return result;
105 }
106 
107 EXTERN_C_START
Init(napi_env env,napi_value exports)108 static napi_value Init(napi_env env, napi_value exports)
109 {
110     napi_property_descriptor desc[] = {
111         {"getoptLong", nullptr, GetoptLong, nullptr, nullptr, nullptr, napi_default, nullptr},
112         {"getoptLongOnly", nullptr, GetoptLongOnly, nullptr, nullptr, nullptr, napi_default, nullptr},
113     };
114     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
115     return exports;
116 }
117 EXTERN_C_END
118 
119 static napi_module demoModule = {
120     .nm_version = 1,
121     .nm_flags = 0,
122     .nm_filename = nullptr,
123     .nm_register_func = Init,
124     .nm_modname = "libunistd1",
125     .nm_priv = ((void *)0),
126     .reserved = {0},
127 };
128 
RegisterModule(void)129 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
130