• 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 "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("-abn", 1, "Set abilityName for debug.");
86     Register("-staticCard", 1, "Set card mode.");
87     Register("-foldable", 1, "Set foldable for Previewer.");
88     Register("-foldStatus", 1, "Set fold status for Previewer.");
89     Register("-fr", 2, "Fold resolution <width> <height>"); // 2 arguments
90     Register("-ljPath", 1, "Set loader.json path for Previewer");
91 }
92 
GetInstance()93 CommandParser& CommandParser::GetInstance()
94 {
95     static CommandParser instance;
96     return instance;
97 }
98 
99 /*
100  * Parse user input and check parameter validity
101  */
ProcessCommand(std::vector<std::string> strs)102 bool CommandParser::ProcessCommand(std::vector<std::string> strs)
103 {
104     ProcessingCommand(strs);
105 
106     if (IsSet("v")) {
107         ELOG("ProcessCommand Set -v!");
108         return false;
109     }
110 
111     if (IsSet("h")) {
112         ELOG("ProcessCommand Set -h!");
113         ELOG(HelpText().c_str());
114         return false;
115     }
116 
117     return true;
118 }
119 
IsCommandValid()120 bool CommandParser::IsCommandValid()
121 {
122     bool partRet = IsDebugPortValid() && IsAppPathValid() && IsAppNameValid() && IsResolutionValid();
123     partRet = partRet && IsConfigPathValid() && IsJsHeapValid() && IsJsHeapFlagValid() && IsScreenShapeValid();
124     partRet = partRet && IsDeviceValid() && IsUrlValid() && IsRefreshValid() && IsCardValid() && IsProjectIDValid();
125     partRet = partRet && IsColorModeValid() && IsOrientationValid() && IsWebSocketPortValid() && IsAceVersionValid();
126     partRet = partRet && IsScreenModeValid() && IsAppResourcePathValid() && IsLoaderJsonPathValid();
127     partRet = partRet && IsProjectModelValid() && IsPagesValid() && IsContainerSdkPathValid();
128     partRet = partRet && IsComponentModeValid() && IsAbilityPathValid() && IsStaticCardValid();
129     partRet = partRet && IsFoldableValid() && IsFoldStatusValid() && IsFoldResolutionValid();
130     partRet = partRet && IsAbilityNameValid();
131     if (partRet) {
132         return true;
133     }
134     ELOG(errorInfo.c_str());
135     ILOG(HelpText().c_str());
136     TraceTool::GetInstance().HandleTrace("Invalid startup parameters");
137     return false;
138 }
139 
IsSet(string key)140 bool CommandParser::IsSet(string key)
141 {
142     if (argsMap.find(string("-") + key) == argsMap.end()) {
143         return false;
144     }
145     return true;
146 }
147 
Value(string key)148 string CommandParser::Value(string key)
149 {
150     auto args = argsMap[string("-") + key];
151     if (args.size() > 0) {
152         return args[0];
153     }
154     return string();
155 }
156 
Values(string key)157 vector<string> CommandParser::Values(string key)
158 {
159     if (argsMap.find(key) == argsMap.end()) {
160         return vector<string>();
161     }
162     vector<string> args = argsMap[key];
163     return args;
164 }
165 
Register(string key,uint32_t argc,string help)166 void CommandParser::Register(string key, uint32_t argc, string help)
167 {
168     regsArgsCountMap[key] = argc;
169     regsHelpMap[key] = help;
170 }
171 
IsResolutionValid(int32_t resolution) const172 bool CommandParser::IsResolutionValid(int32_t resolution) const
173 {
174     if (resolution >= MIN_RESOLUTION && resolution <= MAX_RESOLUTION) {
175         return true;
176     }
177     return false;
178 }
179 
GetDeviceType() const180 string CommandParser::GetDeviceType() const
181 {
182     return deviceType;
183 }
184 
IsRegionRefresh() const185 bool CommandParser::IsRegionRefresh() const
186 {
187     return isRegionRefresh;
188 }
189 
IsCardDisplay() const190 bool CommandParser::IsCardDisplay() const
191 {
192     return isCardDisplay;
193 }
194 
GetConfigPath() const195 string CommandParser::GetConfigPath() const
196 {
197     return configPath;
198 }
199 
GetProjectID() const200 string CommandParser::GetProjectID() const
201 {
202     return projectID;
203 }
204 
GetAppResourcePath() const205 string CommandParser::GetAppResourcePath() const
206 {
207     return appResourcePath;
208 }
209 
GetScreenShape() const210 string CommandParser::GetScreenShape() const
211 {
212     return screenShape;
213 }
214 
GetProjectModel() const215 string CommandParser::GetProjectModel() const
216 {
217     return projectModel;
218 }
219 
GetPages() const220 string CommandParser::GetPages() const
221 {
222     return pages;
223 }
224 
GetContainerSdkPath() const225 string CommandParser::GetContainerSdkPath() const
226 {
227     return containerSdkPath;
228 }
229 
GetScreenMode() const230 CommandParser::ScreenMode CommandParser::GetScreenMode() const
231 {
232     return screenMode;
233 }
234 
GetConfigChanges() const235 string CommandParser::GetConfigChanges() const
236 {
237     return configChanges;
238 }
239 
GetOrignalResolutionWidth() const240 int32_t CommandParser::GetOrignalResolutionWidth() const
241 {
242     return orignalResolutionWidth;
243 }
244 
GetOrignalResolutionHeight() const245 int32_t CommandParser::GetOrignalResolutionHeight() const
246 {
247     return orignalResolutionHeight;
248 }
249 
GetCompressionResolutionWidth() const250 int32_t CommandParser::GetCompressionResolutionWidth() const
251 {
252     return compressionResolutionWidth;
253 }
254 
GetCompressionResolutionHeight() const255 int32_t CommandParser::GetCompressionResolutionHeight() const
256 {
257     return compressionResolutionHeight;
258 }
259 
GetJsHeapSize() const260 uint32_t CommandParser::GetJsHeapSize() const
261 {
262     return jsHeapSize;
263 }
264 
GetAppName() const265 string CommandParser::GetAppName() const
266 {
267     return appName;
268 }
269 
IsSendJSHeap() const270 bool CommandParser::IsSendJSHeap() const
271 {
272     return isSendJSHeap;
273 }
274 
IsComponentMode() const275 bool CommandParser::IsComponentMode() const
276 {
277     return isComponentMode;
278 }
279 
GetAbilityPath() const280 string CommandParser::GetAbilityPath() const
281 {
282     return abilityPath;
283 }
284 
GetAbilityName() const285 string CommandParser::GetAbilityName() const
286 {
287     return abilityName;
288 }
289 
IsStaticCard() const290 bool CommandParser::IsStaticCard() const
291 {
292     return staticCard;
293 }
294 
IsDebugPortValid()295 bool CommandParser::IsDebugPortValid()
296 {
297     if (IsSet("p")) {
298         if (CheckParamInvalidity(Value("p"), true)) {
299             errorInfo = "Launch -p parameters is not match regex.";
300             return false;
301         }
302         int port = atoi(Value("p").c_str());
303         if (port < MIN_PORT || port > MAX_PORT) {
304             errorInfo =
305                 string("Debug server port out of range: " + to_string(MIN_PORT) + "-" + to_string(MAX_PORT) + ".");
306             ELOG("Launch -p parameters abnormal!");
307             return false;
308         }
309     }
310     ILOG("CommandParser debug port: %s", Value("p").c_str());
311     return true;
312 }
313 
IsAppPathValid()314 bool CommandParser::IsAppPathValid()
315 {
316     if (!IsSet("j")) {
317         errorInfo = string("No app path specified.");
318         ELOG("Launch -j parameters abnormal!");
319         return false;
320     }
321     string path = Value("j");
322     if (!FileSystem::IsDirectoryExists(path)) {
323         errorInfo = string("Js app path not exist.");
324         ELOG("Launch -j parameters abnormal!");
325         return false;
326     }
327 
328     return true;
329 }
330 
IsAppNameValid()331 bool CommandParser::IsAppNameValid()
332 {
333     if (IsSet("n")) {
334         if (CheckParamInvalidity(Value("n"), false)) {
335             errorInfo = "Launch -n parameters is not match regex.";
336             return false;
337         }
338         size_t size = Value("n").size();
339         if (size > MAX_NAME_LENGTH) {
340             errorInfo = string("Js app name it too long, max: " + to_string(MAX_NAME_LENGTH) + ".");
341             return false;
342         }
343         appName = Value("n");
344     }
345     ILOG("CommandParser app name: %s", appName.c_str());
346     return true;
347 }
348 
IsResolutionValid()349 bool CommandParser::IsResolutionValid()
350 {
351     if (IsSet("or") && IsSet("cr")) {
352         if (IsResolutionArgValid(string("-or")) && IsResolutionArgValid(string("-cr"))) {
353             orignalResolutionWidth = atoi(Values("-or")[0].c_str());
354             orignalResolutionHeight = atoi(Values("-or")[1].c_str());
355             compressionResolutionWidth = atoi(Values("-cr")[0].c_str());
356             compressionResolutionHeight = atoi(Values("-cr")[1].c_str());
357             ILOG("CommandParser resolution: %d %d %d %d", orignalResolutionWidth, orignalResolutionHeight,
358                  compressionResolutionWidth, compressionResolutionHeight);
359             return true;
360         }
361         ELOG("Launch -cr/-or parameters abnormal!");
362         return false;
363     }
364     ELOG("Launch -cr/-or parameters abnormal!");
365     errorInfo = string("Origin resolution and compress resolution must be setted.");
366     return false;
367 }
368 
IsJsHeapValid()369 bool CommandParser::IsJsHeapValid()
370 {
371     if (IsSet("hs")) {
372         if (CheckParamInvalidity(Value("hs"), true)) {
373             errorInfo = "Launch -hs parameters is not match regex.";
374             return false;
375         }
376         int size = atoi(Value("hs").c_str());
377         if (size < MIN_JSHEAPSIZE || size > MAX_JSHEAPSIZE) {
378             errorInfo = string("JS heap size out of range: " + to_string(MIN_JSHEAPSIZE) + "-" +
379                                to_string(MAX_JSHEAPSIZE) + ".");
380             ELOG("Launch -hs parameters abnormal!");
381             return false;
382         }
383         jsHeapSize = static_cast<uint32_t>(size);
384     }
385     ILOG("CommandParser js heap: %d", jsHeapSize);
386     return true;
387 }
388 
IsJsHeapFlagValid()389 bool CommandParser::IsJsHeapFlagValid()
390 {
391     if (IsSet("hf")) {
392         string flag = Value("hf");
393         if (flag != "true" && flag != "false") {
394             errorInfo = string("JS heap flag suported: true or false");
395             ELOG("Launch -hs parameters abnormal!");
396             return false;
397         }
398         isSendJSHeap = (flag == "true");
399     }
400     ILOG("CommandParser is send JS heap: %d", isSendJSHeap);
401     return true;
402 }
403 
IsScreenShapeValid()404 bool CommandParser::IsScreenShapeValid()
405 {
406     if (IsSet("shape")) {
407         string shape = Value("shape");
408         if (shape != "rect" && shape != "circle") {
409             errorInfo = string("Screen shape suported: rect or circle");
410             ELOG("The current device does not support, please upgrade the SDK!");
411             return false;
412         }
413         screenShape = shape;
414     }
415     ILOG("CommandParser screen shape: %s", screenShape.c_str());
416     return true;
417 }
418 
IsDeviceValid()419 bool CommandParser::IsDeviceValid()
420 {
421     if (IsSet("device")) {
422         auto iter = find(supportedDevices.begin(), supportedDevices.end(), Value("device"));
423         if (iter == supportedDevices.end()) {
424             errorInfo += string("Device type unsupport, please upgrade the Previewer SDK!");
425             ELOG("Device type unsupport!");
426             return false;
427         }
428     }
429     deviceType = Value("device");
430     ILOG("CommandParser device: %s", deviceType.c_str());
431     return true;
432 }
433 
IsUrlValid()434 bool CommandParser::IsUrlValid()
435 {
436     urlPath = Value("url");
437     if (urlPath.empty()) {
438         errorInfo = "Launch -url parameters is empty.";
439         return false;
440     }
441     ILOG("CommandParser url: %s", urlPath.c_str());
442     return true;
443 }
444 
IsConfigPathValid()445 bool CommandParser::IsConfigPathValid()
446 {
447     if (!IsSet("f")) {
448         return true;
449     }
450 
451     string path = Value("f");
452     if (!FileSystem::IsFileExists(path)) {
453         errorInfo = string("The configuration file path does not exist.");
454         ELOG("Launch -f parameters abnormal!");
455         return false;
456     }
457     configPath = path;
458     return true;
459 }
460 
IsAppResourcePathValid()461 bool CommandParser::IsAppResourcePathValid()
462 {
463     if (!IsSet("arp")) {
464         return true;
465     }
466 
467     string path = Value("arp");
468     if (!FileSystem::IsDirectoryExists(path)) {
469         errorInfo = string("The configuration appResource path does not exist.");
470         ELOG("Launch -arp parameters abnormal!");
471         return false;
472     }
473     appResourcePath = path;
474     return true;
475 }
476 
IsProjectModelValid()477 bool CommandParser::IsProjectModelValid()
478 {
479     if (!IsSet("pm")) {
480         return true;
481     }
482 
483     string projectModelStr = Value("pm");
484     auto iter = find(projectModels.begin(), projectModels.end(), projectModelStr);
485     if (iter == projectModels.end()) {
486         errorInfo = string("The project model does not exist.");
487         ELOG("Launch -pm parameters abnormal!");
488         return false;
489     }
490 
491     projectModel = projectModelStr;
492     ILOG("CommandParser projectModel: %s", projectModelStr.c_str());
493     return true;
494 }
495 
IsPagesValid()496 bool CommandParser::IsPagesValid()
497 {
498     if (!IsSet("pages")) {
499         return true;
500     }
501     pages = Value("pages");
502     if (CheckParamInvalidity(pages, false)) {
503         errorInfo = "Launch -pages parameters is not match regex.";
504         return false;
505     }
506     ILOG("CommandParser pages: %s", pages.c_str());
507     return true;
508 }
509 
IsResolutionArgValid(string command)510 bool CommandParser::IsResolutionArgValid(string command)
511 {
512     vector<string> value = Values(command);
513     uint32_t size = regsArgsCountMap[command];
514     if (value.size() != size) {
515         errorInfo = string("Invalid argument's count.");
516         return false;
517     }
518     if (IsResolutionRangeValid(value[0]) && IsResolutionRangeValid(value[1])) {
519         return true;
520     }
521     return false;
522 }
523 
IsResolutionRangeValid(string value)524 bool CommandParser::IsResolutionRangeValid(string value)
525 {
526     if (CheckParamInvalidity(value, true)) {
527         errorInfo = "Launch -or/-cr or -fr parameters is not match regex.";
528         return false;
529     }
530     int32_t temp = atoi(value.c_str());
531     if (!IsResolutionValid(temp)) {
532         errorInfo = string("Resolution range " + to_string(MIN_RESOLUTION) + "-" + to_string(MAX_RESOLUTION) + ".");
533         return false;
534     }
535     return true;
536 }
537 
IsRefreshValid()538 bool CommandParser::IsRefreshValid()
539 {
540     if (!IsSet("refresh")) {
541         return true;
542     }
543 
544     string refresh = Value("refresh");
545     if (refresh != "region" && refresh != "full") {
546         errorInfo = string("The refresh argument unsupported.");
547         ELOG("Launch -refresh parameters abnormal!");
548         return false;
549     }
550     if (refresh == "region") {
551         isRegionRefresh = true;
552     }
553     return true;
554 }
555 
IsCardValid()556 bool CommandParser::IsCardValid()
557 {
558     if (!IsSet("card")) {
559         return true;
560     }
561 
562     string card = Value("card");
563     if (card != "true" && card != "false") {
564         errorInfo = string("The card argument unsupported.");
565         ELOG("Launch -card parameters abnormal!");
566         return false;
567     }
568 
569     std::string devicetype = GetDeviceType();
570     auto iter = find(cardDisplayDevices.begin(), cardDisplayDevices.end(), devicetype);
571     if (iter != cardDisplayDevices.end() && card == "true") {
572         isCardDisplay = true;
573     }
574     return true;
575 }
576 
IsProjectIDValid()577 bool CommandParser::IsProjectIDValid()
578 {
579     if (IsSet("projectID")) {
580         projectID = Value("projectID");
581         if (CheckParamInvalidity(projectID, true)) {
582             errorInfo = "Launch -projectID parameters is not match regex.";
583             return false;
584         }
585     }
586     return true;
587 }
588 
IsColorModeValid()589 bool CommandParser::IsColorModeValid()
590 {
591     if (!IsSet("cm")) {
592         return true;
593     }
594 
595     string colorMode = Value("cm");
596     if (colorMode != "dark" && colorMode != "light") {
597         errorInfo = string("The colormode argument unsupported.");
598         ELOG("Launch -cm parameters abnormal!");
599         return false;
600     }
601     return true;
602 }
603 
IsAceVersionValid()604 bool CommandParser::IsAceVersionValid()
605 {
606     if (!IsSet("av")) {
607         return true;
608     }
609 
610     string aceVersion = Value("av");
611     if (aceVersion != "ACE_1_0" && aceVersion != "ACE_2_0") {
612         errorInfo = string("The aceVersion argument unsupported.");
613         ELOG("Launch -av parameters abnormal!");
614         return false;
615     }
616     return true;
617 }
618 
IsOrientationValid()619 bool CommandParser::IsOrientationValid()
620 {
621     if (!IsSet("o")) {
622         return true;
623     }
624 
625     string orientation = Value("o");
626     if (orientation != "portrait" && orientation != "landscape") {
627         errorInfo = string("The orientation argument unsupported.");
628         ELOG("Launch -o parameters abnormal!");
629         return false;
630     }
631     return true;
632 }
633 
IsWebSocketPortValid()634 bool CommandParser::IsWebSocketPortValid()
635 {
636     if (IsSet("lws")) {
637         if (CheckParamInvalidity(Value("lws"), true)) {
638             errorInfo = "Launch -lws parameters is not match regex.";
639             return false;
640         }
641         int port = atoi(Value("lws").c_str());
642         if (port < MIN_PORT || port > MAX_PORT) {
643             errorInfo = string("WebSocket listening port out of range: " + to_string(MIN_PORT) + "-" +
644                                to_string(MAX_PORT) + ".");
645             ELOG("Launch -lws parameters abnormal!");
646             return false;
647         }
648     }
649     ILOG("CommandParser WebSocket listening port: %s", Value("lws").c_str());
650     return true;
651 }
652 
IsScreenModeValid()653 bool CommandParser::IsScreenModeValid()
654 {
655     string mode("dynamic");
656     if (IsSet("sm")) {
657         mode = Value("sm");
658         if (mode != "dynamic" && mode != "static") {
659             errorInfo = string("Screen picture transport mode suported: dynamic or static");
660             ELOG("Launch -sm parameters abnormal!");
661             return false;
662         }
663         screenMode = (mode == "static" ? CommandParser::ScreenMode::STATIC :
664                       CommandParser::ScreenMode::DYNAMIC);
665     }
666     ILOG("CommandParser screen mode: %s", mode.c_str());
667     return true;
668 }
669 
IsLanguageValid()670 bool CommandParser::IsLanguageValid()
671 {
672     if (!IsSet("l")) {
673         return true;
674     }
675     string lan = Value("pages");
676     if (CheckParamInvalidity(lan, false)) {
677         errorInfo = "Launch -l parameters is not match regex.";
678         return false;
679     }
680     ILOG("CommandParser l: %s", lan.c_str());
681     return true;
682 }
683 
IsTracePipeNameValid()684 bool CommandParser::IsTracePipeNameValid()
685 {
686     if (!IsSet("ts")) {
687         return true;
688     }
689     string tsName = Value("ts");
690     if (CheckParamInvalidity(tsName, false)) {
691         errorInfo = "Launch -ts parameters is not match regex.";
692         return false;
693     }
694     ILOG("CommandParser ts: %s", tsName.c_str());
695     return true;
696 }
697 
IsLocalSocketNameValid()698 bool CommandParser::IsLocalSocketNameValid()
699 {
700     if (!IsSet("s")) {
701         return true;
702     }
703     string socketName = Value("s");
704     if (CheckParamInvalidity(socketName, false)) {
705         errorInfo = "Launch -s parameters is not match regex.";
706         return false;
707     }
708     ILOG("CommandParser s: %s", socketName.c_str());
709     return true;
710 }
711 
IsConfigChangesValid()712 bool CommandParser::IsConfigChangesValid()
713 {
714     if (!IsSet("cc")) {
715         return true;
716     }
717     string configChange = Value("cc");
718     if (CheckParamInvalidity(configChange, false)) {
719         ELOG("Launch -cc parameters is not match regex.");
720         return false;
721     }
722     ILOG("CommandParser cc: %s", configChange.c_str());
723     return true;
724 }
725 
IsScreenDensityValid()726 bool CommandParser::IsScreenDensityValid()
727 {
728     if (!IsSet("sd")) {
729         return true;
730     }
731     string density = Value("sd");
732     if (CheckParamInvalidity(density, true)) {
733         errorInfo = "Launch -sd parameters is not match regex.";
734         return false;
735     }
736     ILOG("CommandParser sd: %s", density.c_str());
737     return true;
738 }
739 
IsContainerSdkPathValid()740 bool CommandParser::IsContainerSdkPathValid()
741 {
742     if (!IsSet("hsp")) {
743         return true;
744     }
745 
746     string path = Value("hsp");
747     if (!FileSystem::IsDirectoryExists(path)) {
748         errorInfo = string("The container sdk path does not exist.");
749         ELOG("Launch -hsp parameters abnormal!");
750         return false;
751     }
752     containerSdkPath = path;
753     return true;
754 }
755 
HelpText()756 string CommandParser::HelpText()
757 {
758     string helpText = "Usage:\n";
759     for (auto index = regsHelpMap.begin(); index != regsHelpMap.end(); index++) {
760         helpText += "-" + index->first + " ";
761         helpText += index->second + "\n";
762     }
763     return helpText;
764 }
765 
ProcessingCommand(const std::vector<string> & strs)766 void CommandParser::ProcessingCommand(const std::vector<string>& strs)
767 {
768     for (unsigned int i = 0; i < strs.size(); ++i) {
769         string index = strs[i];
770         auto regInfo = regsArgsCountMap.find(strs[i]);
771         if (regInfo == regsArgsCountMap.end()) {
772             continue;
773         }
774 
775         vector<string> args;
776         for (uint32_t j = 0; j < regInfo->second; ++j) {
777             if (i == strs.size() - 1  || strs[i + 1][0] == '-') {
778                 args.push_back("");
779                 break;
780             }
781             args.push_back(strs[++i]);
782         }
783         argsMap[index] = args;
784     }
785 }
786 
GetProjectModelEnumValue() const787 int CommandParser::GetProjectModelEnumValue() const
788 {
789     auto idxVal = std::distance(projectModels.begin(),
790                                 find(projectModels.begin(), projectModels.end(), projectModel));
791     idxVal = (idxVal >= projectModels.size()) ? 0 : idxVal;
792     return idxVal;
793 }
794 
GetProjectModelEnumName(int enumValue) const795 string CommandParser::GetProjectModelEnumName(int enumValue) const
796 {
797     if (enumValue < 0 || enumValue >= projectModels.size()) {
798         enumValue = 0;
799     }
800     return projectModels[enumValue];
801 }
802 
CheckParamInvalidity(string param,bool isNum=false)803 bool CommandParser::CheckParamInvalidity(string param, bool isNum = false)
804 {
805     regex reg(isNum ? regex4Num : regex4Str);
806     return !regex_match(param.cbegin(), param.cend(), reg);
807 }
808 
IsComponentModeValid()809 bool CommandParser::IsComponentModeValid()
810 {
811     if (!IsSet("cpm")) {
812         return true;
813     }
814 
815     string cpm = Value("cpm");
816     if (cpm != "true" && cpm != "false") {
817         errorInfo = string("The component mode argument unsupported.");
818         ELOG("Launch -cpm parameters abnormal!");
819         return false;
820     }
821 
822     isComponentMode = cpm == "true" ? true : false;
823     return true;
824 }
825 
IsAbilityPathValid()826 bool CommandParser::IsAbilityPathValid()
827 {
828     if (!IsSet("d")) {
829         return true;
830     }
831     if (deviceType == "liteWearable" || deviceType == "smartVision") {
832         return true;
833     }
834     if (!IsSet("abp")) {
835         errorInfo = "Launch -d parameters without -abp parameters.";
836         return false;
837     }
838     string path = Value("abp");
839     if (path.empty()) {
840         errorInfo = string("The ability path is empty.");
841         ELOG("Launch -abp parameters abnormal!");
842         return false;
843     }
844     abilityPath = path;
845     return true;
846 }
847 
IsAbilityNameValid()848 bool CommandParser::IsAbilityNameValid()
849 {
850     if (!IsSet("d")) {
851         return true;
852     }
853     if (deviceType == "liteWearable" || deviceType == "smartVision") {
854         return true;
855     }
856     if (!IsSet("abn")) {
857         ELOG("Launch -d parameters without -abn parameters.");
858         return true; // 兼容老版本IDE(沒有abn参数)
859     }
860     string name = Value("abn");
861     if (name.empty()) {
862         errorInfo = string("The ability name is empty.");
863         ELOG("Launch -abn parameters abnormal!");
864         return false;
865     }
866     abilityName = name;
867     return true;
868 }
869 
IsStaticCardValid()870 bool CommandParser::IsStaticCardValid()
871 {
872     if (!IsSet("staticCard")) {
873         return true;
874     }
875     string val = Value("staticCard");
876     if (val != "true" && val != "false") {
877         errorInfo = string("The staticCard argument unsupported.");
878         ELOG("Launch -staticCard parameters abnormal!");
879         return false;
880     }
881     if (val == "true") {
882         staticCard = true;
883     }
884     return true;
885 }
886 
IsMainArgLengthInvalid(const char * str) const887 bool CommandParser::IsMainArgLengthInvalid(const char* str) const
888 {
889     size_t argLength = strlen(str);
890     if (argLength > maxMainArgLength) {
891         ELOG("param size is more than %d", maxMainArgLength);
892         return true;
893     }
894     return false;
895 }
896 
IsFoldableValid()897 bool CommandParser::IsFoldableValid()
898 {
899     ELOG("param size is more than %d", maxMainArgLength);
900     if (!IsSet("foldable")) {
901         return true;
902     }
903     string val = Value("foldable");
904     if (val != "true" && val != "false") {
905         errorInfo = string("The foldable argument unsupported.");
906         ELOG("Launch -foldable parameters abnormal!");
907         return false;
908     }
909     if (val == "true") {
910         foldable = true;
911     }
912     return true;
913 }
914 
IsFoldStatusValid()915 bool CommandParser::IsFoldStatusValid()
916 {
917     if ((!IsSet("foldable")) || Value("foldable") != "true") {
918         return true;
919     }
920     if (IsSet("foldStatus")) {
921         if (Value("foldStatus") == "fold" || Value("foldStatus") == "unfold" ||
922             Value("foldStatus") == "unknown" || Value("foldStatus") == "half_fold") {
923             foldStatus = Value("foldStatus");
924             return true;
925         }
926     }
927     ELOG("Launch -foldStatus parameters abnormal!");
928     return false;
929 }
930 
IsFoldResolutionValid()931 bool CommandParser::IsFoldResolutionValid()
932 {
933     if ((!IsSet("foldable")) || Value("foldable") != "true") {
934         return true;
935     }
936     if (IsSet("fr")) {
937         if (IsResolutionArgValid(string("-fr"))) {
938             foldResolutionWidth = atoi(Values("-fr")[0].c_str());
939             foldResolutionHeight = atoi(Values("-fr")[1].c_str());
940             ILOG("CommandParser fold resolution: %d %d", foldResolutionWidth, foldResolutionHeight);
941             return true;
942         }
943         ELOG("Launch -fr parameters abnormal!");
944         return false;
945     }
946     ELOG("Launch -fr parameters abnormal!");
947     errorInfo = string("Fold resolution must be setted.");
948     return false;
949 }
950 
IsFoldable() const951 bool CommandParser::IsFoldable() const
952 {
953     return foldable;
954 }
955 
GetFoldStatus() const956 std::string CommandParser::GetFoldStatus() const
957 {
958     return foldStatus;
959 }
960 
GetFoldResolutionWidth() const961 int32_t CommandParser::GetFoldResolutionWidth() const
962 {
963     return foldResolutionWidth;
964 }
965 
GetFoldResolutionHeight() const966 int32_t CommandParser::GetFoldResolutionHeight() const
967 {
968     return foldResolutionHeight;
969 }
970 
GetLoaderJsonPath() const971 string CommandParser::GetLoaderJsonPath() const
972 {
973     return loaderJsonPath;
974 }
975 
IsLoaderJsonPathValid()976 bool CommandParser::IsLoaderJsonPathValid()
977 {
978     if (!IsSet("ljPath")) {
979         return true;
980     }
981     string path = Value("ljPath");
982     if (!FileSystem::IsFileExists(path)) {
983         errorInfo = string("The configuration loader.json path does not exist.");
984         ELOG("Launch -ljPath parameters abnormal!");
985         return false;
986     }
987     loaderJsonPath = path;
988     return true;
989 }