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 "CommandParser.h"
17 #include <cstring>
18 #include <algorithm>
19 #include <cstdlib>
20 #include <regex>
21 #include "FileSystem.h"
22 #include "PreviewerEngineLog.h"
23 #include "TraceTool.h"
24
25 using namespace std;
26 CommandParser* CommandParser::example = nullptr;
CommandParser()27 CommandParser::CommandParser()
28 : isSendJSHeap(true),
29 orignalResolutionWidth(0),
30 orignalResolutionHeight(0),
31 compressionResolutionWidth(0),
32 compressionResolutionHeight(0),
33 jsHeapSize(MAX_JSHEAPSIZE),
34 deviceType("liteWearable"),
35 screenShape("circle"),
36 appName("undefined"),
37 configPath(""),
38 isRegionRefresh(false),
39 isCardDisplay(false),
40 projectID(""),
41 screenMode(CommandParser::ScreenMode::DYNAMIC),
42 configChanges(""),
43 appResourcePath(""),
44 projectModel("FA"),
45 pages("main_pages"),
46 containerSdkPath(""),
47 isComponentMode(false),
48 abilityPath(""),
49 staticCard(false)
50 {
51 Register("-j", 1, "Launch the js app in <directory>.");
52 Register("-n", 1, "Set the js app name show on <window title>.");
53 Register("-d", 0, "Run in debug mode and start debug server.");
54 Register("-p", 1, "Config debug server to listen <port>.");
55 Register("-s", 1, "Local socket name <socket-name> for command line interface.");
56 Register("-v", 0, "Print the periviewer engine version.");
57 Register("-h", 0, "Print the usage help.");
58 Register("-or", 2, "Original resolution <width> <height>"); // 2 arguments
59 Register("-cr", 2, "Compression resolution <width> <height>"); // 2 arguments
60 Register("-f", 1, "config path <path>");
61 Register("-hs", 1, "JS Heap <size>");
62 Register("-hf", 1, "JS Send Heap <flag>");
63 Register("-shape", 1, "Screen shape <shape>");
64 Register("-device", 1, "Device type <type>");
65 Register("-url", 1, "temp url");
66 Register("-refresh", 1, "Screen <refresh mode>, support region and full");
67 Register("-card", 1, "Controls the display <type> to switch between the app and card.");
68 Register("-projectID", 1, "the ID of current project.");
69 Register("-ts", 1, "Trace socket name");
70 Register("-cm", 1, "Set colormode for the theme.");
71 Register("-o", 1, "Set orientation for the display.");
72 Register("-lws", 1, "Listening port of WebSocket");
73 Register("-av", 1, "Set ace version.");
74 Register("-l", 1, "Set language for startParam.");
75 Register("-sd", 1, "Set screenDensity for Previewer.");
76 Register("-sm", 1, "Set Screen picture transport mode, support dynamic and static");
77 Register("-cc", 1, "Set Resource configChanges.");
78 Register("-arp", 1, "Set App ResourcePath.");
79 Register("-fs", 1, "Select Fonts sources.");
80 Register("-pm", 1, "Set project model type.");
81 Register("-pages", 1, "Set project's router config file path.");
82 Register("-hsp", 1, "Set container sdk path.");
83 Register("-cpm", 1, "Set previewer start mode.");
84 Register("-abp", 1, "Set abilityPath for debug.");
85 Register("-staticCard", 1, "Set card mode.");
86 }
87
GetInstance()88 CommandParser& CommandParser::GetInstance()
89 {
90 static CommandParser instance;
91 return instance;
92 }
93
94 /*
95 * Parse user input and check parameter validity
96 */
ProcessCommand(std::vector<std::string> strs)97 bool CommandParser::ProcessCommand(std::vector<std::string> strs)
98 {
99 ProcessingCommand(strs);
100
101 if (IsSet("v")) {
102 ELOG("ProcessCommand Set -v!");
103 return false;
104 }
105
106 if (IsSet("h")) {
107 ELOG("ProcessCommand Set -h!");
108 ELOG(HelpText().c_str());
109 return false;
110 }
111
112 return true;
113 }
114
IsCommandValid()115 bool CommandParser::IsCommandValid()
116 {
117 bool partRet = IsDebugPortValid() && IsAppPathValid() && IsAppNameValid() && IsResolutionValid();
118 partRet = partRet && IsConfigPathValid() && IsJsHeapValid() && IsJsHeapFlagValid() && IsScreenShapeValid();
119 partRet = partRet && IsDeviceValid() && IsUrlValid() && IsRefreshValid() && IsCardValid() && IsProjectIDValid();
120 partRet = partRet && IsColorModeValid() && IsOrientationValid() && IsWebSocketPortValid() && IsAceVersionValid();
121 partRet = partRet && IsScreenModeValid() && IsAppResourcePathValid();
122 partRet = partRet && IsProjectModelValid() && IsPagesValid() && IsContainerSdkPathValid();
123 partRet = partRet && IsComponentModeValid() && IsAbilityPathValid() && IsStaticCardValid();
124 if (partRet) {
125 return true;
126 }
127 ELOG(errorInfo.c_str());
128 ILOG(HelpText().c_str());
129 TraceTool::GetInstance().HandleTrace("Invalid startup parameters");
130 return false;
131 }
132
IsSet(string key)133 bool CommandParser::IsSet(string key)
134 {
135 if (argsMap.find(string("-") + key) == argsMap.end()) {
136 return false;
137 }
138 return true;
139 }
140
Value(string key)141 string CommandParser::Value(string key)
142 {
143 auto args = argsMap[string("-") + key];
144 if (args.size() > 0) {
145 return args[0];
146 }
147 return string();
148 }
149
Values(string key)150 vector<string> CommandParser::Values(string key)
151 {
152 if (argsMap.find(key) == argsMap.end()) {
153 return vector<string>();
154 }
155 vector<string> args = argsMap[key];
156 return args;
157 }
158
Register(string key,uint32_t argc,string help)159 void CommandParser::Register(string key, uint32_t argc, string help)
160 {
161 regsArgsCountMap[key] = argc;
162 regsHelpMap[key] = help;
163 }
164
IsResolutionValid(int32_t resolution) const165 bool CommandParser::IsResolutionValid(int32_t resolution) const
166 {
167 if (resolution >= MIN_RESOLUTION && resolution <= MAX_RESOLUTION) {
168 return true;
169 }
170 return false;
171 }
172
GetDeviceType() const173 string CommandParser::GetDeviceType() const
174 {
175 return deviceType;
176 }
177
IsRegionRefresh() const178 bool CommandParser::IsRegionRefresh() const
179 {
180 return isRegionRefresh;
181 }
182
IsCardDisplay() const183 bool CommandParser::IsCardDisplay() const
184 {
185 return isCardDisplay;
186 }
187
GetConfigPath() const188 string CommandParser::GetConfigPath() const
189 {
190 return configPath;
191 }
192
GetProjectID() const193 string CommandParser::GetProjectID() const
194 {
195 return projectID;
196 }
197
GetAppResourcePath() const198 string CommandParser::GetAppResourcePath() const
199 {
200 return appResourcePath;
201 }
202
GetScreenShape() const203 string CommandParser::GetScreenShape() const
204 {
205 return screenShape;
206 }
207
GetProjectModel() const208 string CommandParser::GetProjectModel() const
209 {
210 return projectModel;
211 }
212
GetPages() const213 string CommandParser::GetPages() const
214 {
215 return pages;
216 }
217
GetContainerSdkPath() const218 string CommandParser::GetContainerSdkPath() const
219 {
220 return containerSdkPath;
221 }
222
GetScreenMode() const223 CommandParser::ScreenMode CommandParser::GetScreenMode() const
224 {
225 return screenMode;
226 }
227
GetConfigChanges() const228 string CommandParser::GetConfigChanges() const
229 {
230 return configChanges;
231 }
232
GetOrignalResolutionWidth() const233 int32_t CommandParser::GetOrignalResolutionWidth() const
234 {
235 return orignalResolutionWidth;
236 }
237
GetOrignalResolutionHeight() const238 int32_t CommandParser::GetOrignalResolutionHeight() const
239 {
240 return orignalResolutionHeight;
241 }
242
GetCompressionResolutionWidth() const243 int32_t CommandParser::GetCompressionResolutionWidth() const
244 {
245 return compressionResolutionWidth;
246 }
247
GetCompressionResolutionHeight() const248 int32_t CommandParser::GetCompressionResolutionHeight() const
249 {
250 return compressionResolutionHeight;
251 }
252
GetJsHeapSize() const253 uint32_t CommandParser::GetJsHeapSize() const
254 {
255 return jsHeapSize;
256 }
257
GetAppName() const258 string CommandParser::GetAppName() const
259 {
260 return appName;
261 }
262
IsSendJSHeap() const263 bool CommandParser::IsSendJSHeap() const
264 {
265 return isSendJSHeap;
266 }
267
IsComponentMode() const268 bool CommandParser::IsComponentMode() const
269 {
270 return isComponentMode;
271 }
272
GetAbilityPath() const273 string CommandParser::GetAbilityPath() const
274 {
275 return abilityPath;
276 }
277
IsStaticCard() const278 bool CommandParser::IsStaticCard() const
279 {
280 return staticCard;
281 }
282
IsDebugPortValid()283 bool CommandParser::IsDebugPortValid()
284 {
285 if (IsSet("p")) {
286 if (CheckParamInvalidity(Value("p"), true)) {
287 errorInfo = "Launch -p parameters is not match regex.";
288 return false;
289 }
290 int port = atoi(Value("p").c_str());
291 if (port < MIN_PORT || port > MAX_PORT) {
292 errorInfo =
293 string("Debug server port out of range: " + to_string(MIN_PORT) + "-" + to_string(MAX_PORT) + ".");
294 ELOG("Launch -p parameters abnormal!");
295 return false;
296 }
297 }
298 ILOG("CommandParser debug port: %s", Value("p").c_str());
299 return true;
300 }
301
IsAppPathValid()302 bool CommandParser::IsAppPathValid()
303 {
304 if (!IsSet("j")) {
305 errorInfo = string("No app path specified.");
306 ELOG("Launch -j parameters abnormal!");
307 return false;
308 }
309 string path = Value("j");
310 if (!FileSystem::IsDirectoryExists(path)) {
311 errorInfo = string("Js app path not exist.");
312 ELOG("Launch -j parameters abnormal!");
313 return false;
314 }
315
316 return true;
317 }
318
IsAppNameValid()319 bool CommandParser::IsAppNameValid()
320 {
321 if (IsSet("n")) {
322 if (CheckParamInvalidity(Value("n"), false)) {
323 errorInfo = "Launch -n parameters is not match regex.";
324 return false;
325 }
326 size_t size = Value("n").size();
327 if (size > MAX_NAME_LENGTH) {
328 errorInfo = string("Js app name it too long, max: " + to_string(MAX_NAME_LENGTH) + ".");
329 return false;
330 }
331 appName = Value("n");
332 }
333 ILOG("CommandParser app name: %s", appName.c_str());
334 return true;
335 }
336
IsResolutionValid()337 bool CommandParser::IsResolutionValid()
338 {
339 if (IsSet("or") && IsSet("cr")) {
340 if (IsResolutionArgValid(string("-or")) && IsResolutionArgValid(string("-cr"))) {
341 orignalResolutionWidth = atoi(Values("-or")[0].c_str());
342 orignalResolutionHeight = atoi(Values("-or")[1].c_str());
343 compressionResolutionWidth = atoi(Values("-cr")[0].c_str());
344 compressionResolutionHeight = atoi(Values("-cr")[1].c_str());
345 ILOG("CommandParser resolution: %d %d %d %d", orignalResolutionWidth, orignalResolutionHeight,
346 compressionResolutionWidth, compressionResolutionHeight);
347 return true;
348 }
349 ELOG("Launch -cr/-or parameters abnormal!");
350 return false;
351 }
352 ELOG("Launch -cr/-or parameters abnormal!");
353 errorInfo = string("Origin resolution and compress resolution must be setted.");
354 return false;
355 }
356
IsJsHeapValid()357 bool CommandParser::IsJsHeapValid()
358 {
359 if (IsSet("hs")) {
360 if (CheckParamInvalidity(Value("hs"), true)) {
361 errorInfo = "Launch -hs parameters is not match regex.";
362 return false;
363 }
364 int size = atoi(Value("hs").c_str());
365 if (size < MIN_JSHEAPSIZE || size > MAX_JSHEAPSIZE) {
366 errorInfo = string("JS heap size out of range: " + to_string(MIN_JSHEAPSIZE) + "-" +
367 to_string(MAX_JSHEAPSIZE) + ".");
368 ELOG("Launch -hs parameters abnormal!");
369 return false;
370 }
371 jsHeapSize = static_cast<uint32_t>(size);
372 }
373 ILOG("CommandParser js heap: %d", jsHeapSize);
374 return true;
375 }
376
IsJsHeapFlagValid()377 bool CommandParser::IsJsHeapFlagValid()
378 {
379 if (IsSet("hf")) {
380 string flag = Value("hf");
381 if (flag != "true" && flag != "false") {
382 errorInfo = string("JS heap flag suported: true or false");
383 ELOG("Launch -hs parameters abnormal!");
384 return false;
385 }
386 isSendJSHeap = (flag == "true");
387 }
388 ILOG("CommandParser is send JS heap: %d", isSendJSHeap);
389 return true;
390 }
391
IsScreenShapeValid()392 bool CommandParser::IsScreenShapeValid()
393 {
394 if (IsSet("shape")) {
395 string shape = Value("shape");
396 if (shape != "rect" && shape != "circle") {
397 errorInfo = string("Screen shape suported: rect or circle");
398 ELOG("The current device does not support, please upgrade the SDK!");
399 return false;
400 }
401 screenShape = shape;
402 }
403 ILOG("CommandParser screen shape: %s", screenShape.c_str());
404 return true;
405 }
406
IsDeviceValid()407 bool CommandParser::IsDeviceValid()
408 {
409 if (IsSet("device")) {
410 auto iter = find(supportedDevices.begin(), supportedDevices.end(), Value("device"));
411 if (iter == supportedDevices.end()) {
412 errorInfo += string("Device type unsupport, please upgrade the Previewer SDK!");
413 ELOG("Device type unsupport!");
414 return false;
415 }
416 }
417 deviceType = Value("device");
418 ILOG("CommandParser device: %s", deviceType.c_str());
419 return true;
420 }
421
IsUrlValid()422 bool CommandParser::IsUrlValid()
423 {
424 urlPath = Value("url");
425 if (urlPath.empty()) {
426 errorInfo = "Launch -url parameters is empty.";
427 return false;
428 }
429 ILOG("CommandParser url: %s", urlPath.c_str());
430 return true;
431 }
432
IsConfigPathValid()433 bool CommandParser::IsConfigPathValid()
434 {
435 if (!IsSet("f")) {
436 return true;
437 }
438
439 string path = Value("f");
440 if (!FileSystem::IsFileExists(path)) {
441 errorInfo = string("The configuration file path does not exist.");
442 ELOG("Launch -f parameters abnormal!");
443 return false;
444 }
445 configPath = path;
446 return true;
447 }
448
IsAppResourcePathValid()449 bool CommandParser::IsAppResourcePathValid()
450 {
451 if (!IsSet("arp")) {
452 return true;
453 }
454
455 string path = Value("arp");
456 if (!FileSystem::IsDirectoryExists(path)) {
457 errorInfo = string("The configuration appResource path does not exist.");
458 ELOG("Launch -arp parameters abnormal!");
459 return false;
460 }
461 appResourcePath = path;
462 return true;
463 }
464
IsProjectModelValid()465 bool CommandParser::IsProjectModelValid()
466 {
467 if (!IsSet("pm")) {
468 return true;
469 }
470
471 string projectModelStr = Value("pm");
472 auto iter = find(projectModels.begin(), projectModels.end(), projectModelStr);
473 if (iter == projectModels.end()) {
474 errorInfo = string("The project model does not exist.");
475 ELOG("Launch -pm parameters abnormal!");
476 return false;
477 }
478
479 projectModel = projectModelStr;
480 ILOG("CommandParser projectModel: %s", projectModelStr.c_str());
481 return true;
482 }
483
IsPagesValid()484 bool CommandParser::IsPagesValid()
485 {
486 if (!IsSet("pages")) {
487 return true;
488 }
489 pages = Value("pages");
490 if (CheckParamInvalidity(pages, false)) {
491 errorInfo = "Launch -pages parameters is not match regex.";
492 return false;
493 }
494 ILOG("CommandParser pages: %s", pages.c_str());
495 return true;
496 }
497
IsResolutionArgValid(string command)498 bool CommandParser::IsResolutionArgValid(string command)
499 {
500 vector<string> value = Values(command);
501 uint32_t size = regsArgsCountMap[command];
502 if (value.size() != size) {
503 errorInfo = string("Invalid argument's count.");
504 return false;
505 }
506 if (IsResolutionRangeValid(value[0]) && IsResolutionRangeValid(value[1])) {
507 return true;
508 }
509 return false;
510 }
511
IsResolutionRangeValid(string value)512 bool CommandParser::IsResolutionRangeValid(string value)
513 {
514 if (CheckParamInvalidity(value, true)) {
515 errorInfo = "Launch -or/-cr parameters is not match regex.";
516 return false;
517 }
518 int32_t temp = atoi(value.c_str());
519 if (!IsResolutionValid(temp)) {
520 errorInfo = string("Resolution range " + to_string(MIN_RESOLUTION) + "-" + to_string(MAX_RESOLUTION) + ".");
521 return false;
522 }
523 return true;
524 }
525
IsRefreshValid()526 bool CommandParser::IsRefreshValid()
527 {
528 if (!IsSet("refresh")) {
529 return true;
530 }
531
532 string refresh = Value("refresh");
533 if (refresh != "region" && refresh != "full") {
534 errorInfo = string("The refresh argument unsupported.");
535 ELOG("Launch -refresh parameters abnormal!");
536 return false;
537 }
538 if (refresh == "region") {
539 isRegionRefresh = true;
540 }
541 return true;
542 }
543
IsCardValid()544 bool CommandParser::IsCardValid()
545 {
546 if (!IsSet("card")) {
547 return true;
548 }
549
550 string card = Value("card");
551 if (card != "true" && card != "false") {
552 errorInfo = string("The card argument unsupported.");
553 ELOG("Launch -card parameters abnormal!");
554 return false;
555 }
556
557 std::string devicetype = GetDeviceType();
558 auto iter = find(cardDisplayDevices.begin(), cardDisplayDevices.end(), devicetype);
559 if (iter != cardDisplayDevices.end() && card == "true") {
560 isCardDisplay = true;
561 }
562 return true;
563 }
564
IsProjectIDValid()565 bool CommandParser::IsProjectIDValid()
566 {
567 if (IsSet("projectID")) {
568 projectID = Value("projectID");
569 if (CheckParamInvalidity(projectID, true)) {
570 errorInfo = "Launch -projectID parameters is not match regex.";
571 return false;
572 }
573 }
574 return true;
575 }
576
IsColorModeValid()577 bool CommandParser::IsColorModeValid()
578 {
579 if (!IsSet("cm")) {
580 return true;
581 }
582
583 string colorMode = Value("cm");
584 if (colorMode != "dark" && colorMode != "light") {
585 errorInfo = string("The colormode argument unsupported.");
586 ELOG("Launch -cm parameters abnormal!");
587 return false;
588 }
589 return true;
590 }
591
IsAceVersionValid()592 bool CommandParser::IsAceVersionValid()
593 {
594 if (!IsSet("av")) {
595 return true;
596 }
597
598 string aceVersion = Value("av");
599 if (aceVersion != "ACE_1_0" && aceVersion != "ACE_2_0") {
600 errorInfo = string("The aceVersion argument unsupported.");
601 ELOG("Launch -av parameters abnormal!");
602 return false;
603 }
604 return true;
605 }
606
IsOrientationValid()607 bool CommandParser::IsOrientationValid()
608 {
609 if (!IsSet("o")) {
610 return true;
611 }
612
613 string orientation = Value("o");
614 if (orientation != "portrait" && orientation != "landscape") {
615 errorInfo = string("The orientation argument unsupported.");
616 ELOG("Launch -o parameters abnormal!");
617 return false;
618 }
619 return true;
620 }
621
IsWebSocketPortValid()622 bool CommandParser::IsWebSocketPortValid()
623 {
624 if (IsSet("lws")) {
625 if (CheckParamInvalidity(Value("lws"), true)) {
626 errorInfo = "Launch -lws parameters is not match regex.";
627 return false;
628 }
629 int port = atoi(Value("lws").c_str());
630 if (port < MIN_PORT || port > MAX_PORT) {
631 errorInfo = string("WebSocket listening port out of range: " + to_string(MIN_PORT) + "-" +
632 to_string(MAX_PORT) + ".");
633 ELOG("Launch -lws parameters abnormal!");
634 return false;
635 }
636 }
637 ILOG("CommandParser WebSocket listening port: %s", Value("lws").c_str());
638 return true;
639 }
640
IsScreenModeValid()641 bool CommandParser::IsScreenModeValid()
642 {
643 string mode("dynamic");
644 if (IsSet("sm")) {
645 mode = Value("sm");
646 if (mode != "dynamic" && mode != "static") {
647 errorInfo = string("Screen picture transport mode suported: dynamic or static");
648 ELOG("Launch -sm parameters abnormal!");
649 return false;
650 }
651 screenMode = (mode == "static" ? CommandParser::ScreenMode::STATIC :
652 CommandParser::ScreenMode::DYNAMIC);
653 }
654 ILOG("CommandParser screen mode: %s", mode.c_str());
655 return true;
656 }
657
IsLanguageValid()658 bool CommandParser::IsLanguageValid()
659 {
660 if (!IsSet("l")) {
661 return true;
662 }
663 string lan = Value("pages");
664 if (CheckParamInvalidity(lan, false)) {
665 errorInfo = "Launch -l parameters is not match regex.";
666 return false;
667 }
668 ILOG("CommandParser l: %s", lan.c_str());
669 return true;
670 }
671
IsTracePipeNameValid()672 bool CommandParser::IsTracePipeNameValid()
673 {
674 if (!IsSet("ts")) {
675 return true;
676 }
677 string tsName = Value("ts");
678 if (CheckParamInvalidity(tsName, false)) {
679 errorInfo = "Launch -ts parameters is not match regex.";
680 return false;
681 }
682 ILOG("CommandParser ts: %s", tsName.c_str());
683 return true;
684 }
685
IsLocalSocketNameValid()686 bool CommandParser::IsLocalSocketNameValid()
687 {
688 if (!IsSet("s")) {
689 return true;
690 }
691 string socketName = Value("s");
692 if (CheckParamInvalidity(socketName, false)) {
693 errorInfo = "Launch -s parameters is not match regex.";
694 return false;
695 }
696 ILOG("CommandParser s: %s", socketName.c_str());
697 return true;
698 }
699
IsConfigChangesValid()700 bool CommandParser::IsConfigChangesValid()
701 {
702 if (!IsSet("cc")) {
703 return true;
704 }
705 string configChange = Value("cc");
706 if (CheckParamInvalidity(configChange, false)) {
707 ELOG("Launch -cc parameters is not match regex.");
708 return false;
709 }
710 ILOG("CommandParser cc: %s", configChange.c_str());
711 return true;
712 }
713
IsScreenDensityValid()714 bool CommandParser::IsScreenDensityValid()
715 {
716 if (!IsSet("sd")) {
717 return true;
718 }
719 string density = Value("sd");
720 if (CheckParamInvalidity(density, true)) {
721 errorInfo = "Launch -sd parameters is not match regex.";
722 return false;
723 }
724 ILOG("CommandParser sd: %s", density.c_str());
725 return true;
726 }
727
IsContainerSdkPathValid()728 bool CommandParser::IsContainerSdkPathValid()
729 {
730 if (!IsSet("hsp")) {
731 return true;
732 }
733
734 string path = Value("hsp");
735 if (!FileSystem::IsDirectoryExists(path)) {
736 errorInfo = string("The container sdk path does not exist.");
737 ELOG("Launch -hsp parameters abnormal!");
738 return false;
739 }
740 containerSdkPath = path;
741 return true;
742 }
743
HelpText()744 string CommandParser::HelpText()
745 {
746 string helpText = "Usage:\n";
747 for (auto index = regsHelpMap.begin(); index != regsHelpMap.end(); index++) {
748 helpText += "-" + index->first + " ";
749 helpText += index->second + "\n";
750 }
751 return helpText;
752 }
753
ProcessingCommand(const std::vector<string> & strs)754 void CommandParser::ProcessingCommand(const std::vector<string>& strs)
755 {
756 for (unsigned int i = 0; i < strs.size(); ++i) {
757 string index = strs[i];
758 auto regInfo = regsArgsCountMap.find(strs[i]);
759 if (regInfo == regsArgsCountMap.end()) {
760 continue;
761 }
762
763 vector<string> args;
764 for (uint32_t j = 0; j < regInfo->second; ++j) {
765 if (i == strs.size() - 1 || strs[i + 1][0] == '-') {
766 args.push_back("");
767 break;
768 }
769 args.push_back(strs[++i]);
770 }
771 argsMap[index] = args;
772 }
773 }
774
GetProjectModelEnumValue() const775 int CommandParser::GetProjectModelEnumValue() const
776 {
777 auto idxVal = std::distance(projectModels.begin(),
778 find(projectModels.begin(), projectModels.end(), projectModel));
779 idxVal = (idxVal >= projectModels.size()) ? 0 : idxVal;
780 return idxVal;
781 }
782
GetProjectModelEnumName(int enumValue) const783 string CommandParser::GetProjectModelEnumName(int enumValue) const
784 {
785 if (enumValue < 0 || enumValue >= projectModels.size()) {
786 enumValue = 0;
787 }
788 return projectModels[enumValue];
789 }
790
CheckParamInvalidity(string param,bool isNum=false)791 bool CommandParser::CheckParamInvalidity(string param, bool isNum = false)
792 {
793 regex reg(isNum ? regex4Num : regex4Str);
794 return !regex_match(param.cbegin(), param.cend(), reg);
795 }
796
IsComponentModeValid()797 bool CommandParser::IsComponentModeValid()
798 {
799 if (!IsSet("cpm")) {
800 return true;
801 }
802
803 string cpm = Value("cpm");
804 if (cpm != "true" && cpm != "false") {
805 errorInfo = string("The component mode argument unsupported.");
806 ELOG("Launch -cpm parameters abnormal!");
807 return false;
808 }
809
810 isComponentMode = cpm == "true" ? true : false;
811 return true;
812 }
813
IsAbilityPathValid()814 bool CommandParser::IsAbilityPathValid()
815 {
816 if (!IsSet("d")) {
817 return true;
818 }
819 if (deviceType == "liteWearable" || deviceType == "smartVision") {
820 return true;
821 }
822 if (!IsSet("abp")) {
823 errorInfo = "Launch -d parameters without -abp parameters.";
824 return false;
825 }
826 string path = Value("abp");
827 if (path.empty()) {
828 errorInfo = string("The ability path is empty.");
829 ELOG("Launch -abp parameters abnormal!");
830 return false;
831 }
832 abilityPath = path;
833 return true;
834 }
835
IsStaticCardValid()836 bool CommandParser::IsStaticCardValid()
837 {
838 if (!IsSet("staticCard")) {
839 return true;
840 }
841 string val = Value("staticCard");
842 if (val != "true" && val != "false") {
843 errorInfo = string("The staticCard argument unsupported.");
844 ELOG("Launch -staticCard parameters abnormal!");
845 return false;
846 }
847 if (val == "true") {
848 staticCard = true;
849 }
850 return true;
851 }
852
IsMainArgLengthInvalid(const char * str) const853 bool CommandParser::IsMainArgLengthInvalid(const char* str) const
854 {
855 size_t argLength = strlen(str);
856 if (argLength > maxMainArgLength) {
857 ELOG("param size is more than %d", maxMainArgLength);
858 return true;
859 }
860 return false;
861 }