-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListComPort.pb
343 lines (279 loc) · 13.8 KB
/
ListComPort.pb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
;{
; * ListComPort.pb
; Version: 2.1.0-tmp
; Author: Herwin Bozet
;
; License: Unlicense (Public Domain)
;}
;- Notes
; No notes currently available.
;- Compiler Directives
EnableExplicit
CompilerIf #PB_Compiler_ExecutableFormat <> #PB_Compiler_Console
CompilerError("this program need to be compiled as a console application !")
CompilerEndIf
XIncludeFile "./Includes/ListComPortLocales.pbi"
XIncludeFile "./Includes/ListComPortErrorCodes.pbi"
XIncludeFile "./Includes/PB-Arguments/Arguments.pbi"
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
XIncludeFile "./Includes/ComPortHelper.pbi"
CompilerElse
CompilerError "Non-windows platforms are not supported !"
CompilerEndIf
;- Constants
#Version$ = "2.1.0"
;- Code
;-> Setup
If Not OpenConsole("lscom")
MessageBeep_(#MB_ICONERROR)
MessageRequester(#LSCOM_Locale_Error_MBTitle$, #LSCOM_Locale_Error_MBText$,
#PB_MessageRequester_Ok | #PB_MessageRequester_Error)
End #LSCOM_ErrorCode_NoTerminal
EndIf
;-> Preparing globals
Global ExitCode.i = #LSCOM_ErrorCode_NoError
Global ShouldPrintRawNames.b = #True
Global ShouldPrintDeviceNames.b = #False
Global ShouldPrintFriendlyNames.b = #False
Global SortingOrder.b = ComPortHelper::#Sort_Order_None
Global PaddingString$ = #Null$
;-> Preparing argument parser
Procedure VerifyOption(*Option, OptionName$, *HasRegisteredArgumentsCorrectly)
If Not Arguments::RegisterOption(*Option)
ConsoleError(ReplaceString(#LSCOM_Locale_Error_ArgumentDefinitionFailure$, "%0", OptionName$))
Arguments::FreeOption(*Option)
PokeB(*HasRegisteredArgumentsCorrectly, #False)
EndIf
EndProcedure
If Arguments::Init()
Define HasRegisteredArgumentsCorrectly.b = #True
Define *NameAllOption.Arguments::Option = Arguments::CreateOption('a', "show-all", #LSCOM_Locale_ArgumentDesc_ShowAll$)
Define *NameDeviceOption.Arguments::Option = Arguments::CreateOption('d', "show-device", #LSCOM_Locale_ArgumentDesc_ShowDevice$)
Define *DividerCharOption.Arguments::Option = Arguments::CreateOption('D', "divider", #LSCOM_Locale_ArgumentDesc_Divider$, Arguments::#Option_HasValue)
Define *NameFriendlyOption.Arguments::Option = Arguments::CreateOption('f', "show-friendly", #LSCOM_Locale_ArgumentDesc_ShowFriendly$)
Define *HelpOption.Arguments::Option = Arguments::CreateOption('h', "help", #LSCOM_Locale_ArgumentDesc_Help$)
Define *NameRawOption.Arguments::Option = Arguments::CreateOption('n', "show-name-raw", #LSCOM_Locale_ArgumentDesc_ShowRaw$)
Define *NoPrettyAscOption.Arguments::Option = Arguments::CreateOption('P', "no-pretty", #LSCOM_Locale_ArgumentDesc_NoPretty$)
Define *SortAscOption.Arguments::Option = Arguments::CreateOption('s', "sort", #LSCOM_Locale_ArgumentDesc_Sort$)
Define *SortDescOption.Arguments::Option = Arguments::CreateOption('S', "sort-reverse", #LSCOM_Locale_ArgumentDesc_SortReverse$)
Define *NameTabPaddingOption.Arguments::Option = Arguments::CreateOption('t', "tab-padding", #LSCOM_Locale_ArgumentDesc_TabDivider$)
Define *VersionOption.Arguments::Option = Arguments::CreateOption('v', "version", #LSCOM_Locale_ArgumentDesc_Version$)
Define *VersionOnlyOption.Arguments::Option = Arguments::CreateOption('V', "version-only", #LSCOM_Locale_ArgumentDesc_VersionOnly$)
VerifyOption(*NameAllOption, "*NameAllOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*NameDeviceOption, "*NameDeviceOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*DividerCharOption, "*DividerCharOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*NameFriendlyOption, "*NameFriendlyOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*HelpOption, "*HelpOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*NameRawOption, "*NameRawOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*NoPrettyAscOption, "*NoPrettyAscOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*SortAscOption, "*SortAscOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*SortDescOption, "*SortDescOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*NameTabPaddingOption, "*NameTabPaddingOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*VersionOption, "*VersionOption", @HasRegisteredArgumentsCorrectly)
VerifyOption(*VersionOnlyOption, "*VersionOnlyOption", @HasRegisteredArgumentsCorrectly)
If HasRegisteredArgumentsCorrectly
If Not Arguments::ParseArguments(0, CountProgramParameters())
If *HelpOption\WasUsed
PrintN("lscom.exe [-a|--show-all] [-d|--show-device] [-D <str>|--divider <str>] [-f|--show-friendly]")
PrintN(" [-h|--help] [-n|--show-name-raw] [-P|--no-pretty] [-s|--sort] [-S|--sort-reverse]")
PrintN(" [-t|--tab-padding] [-v|--version] [-V|--version-only]")
PrintN("")
PrintN(#LSCOM_Locale_HelpSection_LaunchArgs$+":")
PrintN(" -a, --show-all "+#LSCOM_Locale_ArgumentDesc_ShowAll$)
PrintN(" -d, --show-device "+#LSCOM_Locale_ArgumentDesc_ShowDevice$)
PrintN(" -D <str>, --divider <str> "+#LSCOM_Locale_ArgumentDesc_Divider$)
PrintN(" -f, --show-friendly "+#LSCOM_Locale_ArgumentDesc_ShowFriendly$)
PrintN(" -h, --help "+#LSCOM_Locale_ArgumentDesc_Help$)
PrintN(" -n, --show-name-raw "+#LSCOM_Locale_ArgumentDesc_ShowRaw$)
PrintN(" -P, --no-pretty "+#LSCOM_Locale_ArgumentDesc_NoPretty$)
PrintN(" -s, --sort "+#LSCOM_Locale_ArgumentDesc_Sort$)
PrintN(" -S, --sort-reverse "+#LSCOM_Locale_ArgumentDesc_SortReverse$)
PrintN(" -t, --tab-padding "+#LSCOM_Locale_ArgumentDesc_TabDivider$)
PrintN(" -v, --version "+#LSCOM_Locale_ArgumentDesc_Version$)
PrintN(" -V, --version-only "+#LSCOM_Locale_ArgumentDesc_VersionOnly$)
PrintN("")
PrintN(#LSCOM_Locale_HelpSection_Remarks$+":")
PrintN(" * "+#LSCOM_Locale_Remark_NamePartsAndRawDefault$)
PrintN(" * "+#LSCOM_Locale_Remark_NoPrettyPrinting$)
PrintN(" * "+#LSCOM_Locale_Remark_DefaultSorting$)
PrintN(" * "+#LSCOM_Locale_Remark_NameRaw$)
PrintN(" * "+#LSCOM_Locale_Remark_NameDevice$)
PrintN(" * "+#LSCOM_Locale_Remark_NameFriendly$)
PrintN(" * "+#LSCOM_Locale_Remark_ErrorsFatal$)
PrintN(" * "+#LSCOM_Locale_Remark_ErrorsNonFatal$)
PrintN("")
PrintN(#LSCOM_Locale_HelpSection_Formatting$+":")
PrintN(" *┬> "+#LSCOM_Locale_Expression_NoArguments$+":")
PrintN(" └──> ${"+#LSCOM_Locale_Expression_RawName$+"}"+#TAB$+"=> COM1")
PrintN(" *┬> '-d' "+#LSCOM_Locale_Expression_LowerCase_Or$+" '-f' ")
PrintN(" ├──> ${"+#LSCOM_Locale_Expression_DeviceName$+"}"+#TAB$+"=> \Device\Serial1")
PrintN(" └──> ${"+#LSCOM_Locale_Expression_FriendlyName$+"}"+#TAB$+"=> Communications Port")
PrintN(" *┬> '-d' "+#LSCOM_Locale_Expression_LowerCase_And$+" '-f' ")
PrintN(" └──> ${"+#LSCOM_Locale_Expression_FriendlyName$+"} [${"+#LSCOM_Locale_Expression_DeviceName$+"}]"+#TAB$+"=> Communications Port [\Device\Serial1]")
PrintN(" *┬> '-n' "+#LSCOM_Locale_Expression_LowerCase_And$+" '-d'")
PrintN(" └──> ${"+#LSCOM_Locale_Expression_RawName$+"} [$DeviceName]"+#TAB$+"=> COM1 [\Device\Serial1]")
PrintN(" *┬> '-n' "+#LSCOM_Locale_Expression_LowerCase_And$+" '-f'")
PrintN(" └──> ${"+#LSCOM_Locale_Expression_RawName$+"} - ${"+#LSCOM_Locale_Expression_FriendlyName$+"}"+#TAB$+"=> COM1 - Communications Port")
PrintN(" *┬> '-ndf' "+#LSCOM_Locale_Expression_LowerCase_Or$+" '-a' ")
PrintN(" └──> ${"+#LSCOM_Locale_Expression_RawName$+"} - ${"+#LSCOM_Locale_Expression_FriendlyName$+"} [${"+#LSCOM_Locale_Expression_DeviceName$+"}]"+
#TAB$+"=> COM1 - Communications Port [\Device\Serial1]")
PrintN(" *┬> '-ndfp' "+#LSCOM_Locale_Expression_LowerCase_Or$+" '-ap' ")
PrintN(" └──> ${"+#LSCOM_Locale_Expression_RawName$+"} ${"+#LSCOM_Locale_Expression_FriendlyName$+"} ${"+#LSCOM_Locale_Expression_DeviceName$+"}"+
#TAB$+"=> COM1 Communications Port \Device\Serial1")
PrintN(" *┬> '-ndfD "+#DQUOTE$+";"+#DQUOTE$+"' "+#LSCOM_Locale_Expression_LowerCase_Or$+" '-aD "+#DQUOTE$+";"+#DQUOTE$+"' ")
PrintN(" └──> ${"+#LSCOM_Locale_Expression_RawName$+"};${"+#LSCOM_Locale_Expression_FriendlyName$+"};${"+#LSCOM_Locale_Expression_DeviceName$+"}"+
#TAB$+"=> COM1;Communications Port;\Device\Serial1")
PrintN("")
PrintN(#LSCOM_Locale_HelpSection_ErrorCodes$+":")
PrintN(#LSCOM_Locale_HelpSectionFormatted_ErrorCodes_Fatal$)
PrintN(" * "+Str(#LSCOM_ErrorCode_NoTerminal)+" - "+#LSCOM_Locale_ErrorExplaination_NoTerminal$)
PrintN(" * "+Str(#LSCOM_ErrorCode_NoRequiredWinApiFunction)+" - "+#LSCOM_Locale_ErrorExplaination_WinApiMissingFunction$)
PrintN(#LSCOM_Locale_HelpSectionFormatted_ErrorCodes_Internal$)
PrintN(" * "+Str(#LSCOM_ErrorCode_ArgumentParsingFailure)+" - "+#LSCOM_Locale_ErrorExplaination_ArgumentParsingFailure$)
PrintN(" * "+Str(#LSCOM_ErrorCode_ArgumentDefinitionFailure)+" - "+#LSCOM_Locale_ErrorExplaination_ArgumentDefinitionFailure$)
PrintN(" * "+Str(#LSCOM_ErrorCode_ArgumentInitFailure)+" - "+#LSCOM_Locale_ErrorExplaination_ArgumentInitFailure$)
PrintN(#LSCOM_Locale_HelpSectionFormatted_ErrorCodes_External$)
PrintN(" * "+Str(#LSCOM_ErrorCode_NoPaddingValue)+" - "+#LSCOM_Locale_ErrorExplaination_NoPaddingValue$)
PrintN(#LSCOM_Locale_HelpSectionFormatted_ErrorCodes_Application$)
PrintN(" * "+Str(#LSCOM_ErrorCode_NoFriendlyNames)+" - "+#LSCOM_Locale_ErrorExplaination_NoFriendlyNames$)
PrintN(" * "+Str(#LSCOM_ErrorCode_NoComPorts)+" - "+#LSCOM_Locale_ErrorExplaination_NoComPorts$)
End ExitCode
EndIf
If *VersionOnlyOption\WasUsed
Print(#Version$)
End ExitCode
EndIf
If *VersionOption\WasUsed
PrintN("PB-ListComPort (lscom) v"+#Version$)
PrintN(" Arguments v"+Arguments::#Version$)
PrintN(" ComPortHelper v"+ComPortHelper::#Version$)
PrintN(" RegistryHelper v"+RegistryHelper::#Version$)
PrintN(" WinTypes v"+WinTypes::#Version$)
PrintN("")
PrintN(#LSCOM_Locale_Expression_Language$+": "+#LSCOM_Locale_LangName$+" ("+#PBListComPortLang+")")
PrintN("GitHub: https://github.com/aziascreations/PB-ListComPort")
End ExitCode
EndIf
If *NameDeviceOption\WasUsed
ShouldPrintRawNames = #False
ShouldPrintDeviceNames = #True
EndIf
If *NameFriendlyOption\WasUsed
ShouldPrintRawNames = #False
ShouldPrintFriendlyNames = #True
EndIf
If *NameRawOption\WasUsed
ShouldPrintRawNames = #True
EndIf
If *NameAllOption\WasUsed
ShouldPrintDeviceNames = #True
ShouldPrintFriendlyNames = #True
ShouldPrintRawNames = #True
EndIf
If *SortAscOption\WasUsed
SortingOrder = ComPortHelper::#Sort_Order_Ascending
EndIf
If *SortDescOption\WasUsed
SortingOrder = ComPortHelper::#Sort_Order_Descending
EndIf
If *NoPrettyAscOption\WasUsed
PaddingString$ = " "
EndIf
If *DividerCharOption\WasUsed
If ListSize(*DividerCharOption\Arguments()) = 0
ConsoleError(#LSCOM_Locale_ErrorExplaination_NoPaddingValue$)
ExitCode = #LSCOM_ErrorCode_NoPaddingValue
Else
FirstElement(*DividerCharOption\Arguments())
PaddingString$ = *DividerCharOption\Arguments()
EndIf
EndIf
If *NameTabPaddingOption\WasUsed
PaddingString$ = #TAB$
EndIf
Else
ConsoleError(#LSCOM_Locale_ErrorExplaination_ArgumentParsingFailure$)
ExitCode = #LSCOM_ErrorCode_ArgumentParsingFailure
EndIf
Else
ConsoleError(#LSCOM_Locale_ErrorExplaination_ArgumentDefinitionFailure$)
ExitCode = #LSCOM_ErrorCode_ArgumentDefinitionFailure
EndIf
; Clearing the memory for the argument parser...
Arguments::Finish()
Else
ConsoleError(#LSCOM_Locale_ErrorExplaination_ArgumentInitFailure$)
ExitCode = #LSCOM_ErrorCode_ArgumentInitFailure
EndIf
;-> Listing ports
Global IsDoingFine.b = #True
Global RawToFriendlySeparator$ = " - "
Global UseDeviceBrackets.b = #True
Global NewList ComPortDeviceNames.s()
Global NewList ComPortRawNames.s()
; May not be used depending on the options used.
Global NewMap ComPortFriendlyNames.s()
If PaddingString$ = #Null$
; No custom padding char was used
PaddingString$ = " "
Else
; Custom padding char was used
RawToFriendlySeparator$ = PaddingString$
UseDeviceBrackets = #False
EndIf
If ComPortHelper::GetComPortAndDeviceNameLists(ComPortDeviceNames(), ComPortRawNames()) <> -1
If ShouldPrintFriendlyNames
If ComPortHelper::GetComPortMappedFriendlyName(ComPortRawNames(), ComPortFriendlyNames(), #True) = -1
ConsoleError(#LSCOM_Locale_ErrorExplaination_NoFriendlyNames$)
IsDoingFine = #False
ExitCode = #LSCOM_ErrorCode_NoFriendlyNames
EndIf
EndIf
Else
ConsoleError(#LSCOM_Locale_ErrorExplaination_NoComPorts$)
IsDoingFine = #False
ExitCode = #LSCOM_ErrorCode_NoComPorts
EndIf
If IsDoingFine
ComPortHelper::SortDeviceAndRawNameLists(ComPortDeviceNames(), ComPortRawNames(), SortingOrder)
ForEach ComPortRawNames()
If ShouldPrintRawNames
Print(ComPortRawNames())
If ShouldPrintFriendlyNames
Print(RawToFriendlySeparator$+ComPortFriendlyNames(ComPortRawNames()))
EndIf
If ShouldPrintDeviceNames
SelectElement(ComPortDeviceNames(), ListIndex(ComPortRawNames()))
If UseDeviceBrackets
PrintN(PaddingString$+"["+ComPortDeviceNames()+"]")
Else
PrintN(PaddingString$+ComPortDeviceNames())
EndIf
Else
Print(#CRLF$)
EndIf
Else
If ShouldPrintFriendlyNames
Print(ComPortFriendlyNames(ComPortRawNames()))
If ShouldPrintDeviceNames
SelectElement(ComPortDeviceNames(), ListIndex(ComPortRawNames()))
If UseDeviceBrackets
PrintN(PaddingString$+"["+ComPortDeviceNames()+"]")
Else
PrintN(PaddingString$+ComPortDeviceNames())
EndIf
Else
Print(#CRLF$)
EndIf
Else
SelectElement(ComPortDeviceNames(), ListIndex(ComPortRawNames()))
PrintN(ComPortDeviceNames())
EndIf
EndIf
Next
EndIf
; Cleaning...
FreeMap(ComPortFriendlyNames())
FreeList(ComPortRawNames())
FreeList(ComPortDeviceNames())
End ExitCode