2 Provides interface to shell MAN file parser.
4 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
5 Copyright 2015 Dell Inc.
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Convert a Unicode character to upper case only if
20 it maps to a valid small-case ASCII character.
22 This internal function only deal with Unicode character
23 which maps to a valid small-case ASCII character, i.e.
24 L'a' to L'z'. For other Unicode character, the input character
27 @param Char The character to convert.
29 @retval LowerCharacter If the Char is with range L'a' to L'z'.
30 @retval Unchanged Otherwise.
35 InternalShellCharToUpper (
40 Verifies that the filename has .MAN on the end.
42 allocates a new buffer and copies the name (appending .MAN if necessary)
44 ASSERT if ManFileName is NULL
46 @param[in] ManFileName original filename
48 @return the new filename with .man as the extension.
53 IN CONST CHAR16
*ManFileName
57 if (ManFileName
== NULL
) {
63 if (StrnCmp(ManFileName
+StrLen(ManFileName
)-4, L
".man", 4)==0) {
64 Buffer
= AllocateCopyPool(StrSize(ManFileName
), ManFileName
);
66 Buffer
= AllocateZeroPool(StrSize(ManFileName
) + 4*sizeof(CHAR16
));
69 (StrSize(ManFileName
) + 4*sizeof(CHAR16
))/sizeof(CHAR16
),
74 (StrSize(ManFileName
) + 4*sizeof(CHAR16
))/sizeof(CHAR16
),
84 Search the path environment variable for possible locations and test for
85 which one contains a man file with the name specified. If a valid file is found
86 stop searching and return the (opened) SHELL_FILE_HANDLE for that file.
88 @param[in] FileName Name of the file to find and open.
89 @param[out] Handle Pointer to the handle of the found file. The
90 value of this is undefined for return values
93 @retval EFI_SUCCESS The file was found. Handle is a valid SHELL_FILE_HANDLE
94 @retval EFI_INVALID_PARAMETER A parameter had an invalid value.
95 @retval EFI_NOT_FOUND The file was not found.
100 IN CONST CHAR16
*FileName
,
101 OUT SHELL_FILE_HANDLE
*Handle
104 CHAR16
*FullFileName
;
107 if ( FileName
== NULL
109 || StrLen(FileName
) == 0
111 return (EFI_INVALID_PARAMETER
);
114 FullFileName
= ShellFindFilePath(FileName
);
115 if (FullFileName
== NULL
) {
116 return (EFI_NOT_FOUND
);
120 // now open that file
122 Status
= EfiShellOpenFileByName(FullFileName
, Handle
, EFI_FILE_MODE_READ
);
123 FreePool(FullFileName
);
129 parses through Buffer (which is MAN file formatted) and returns the
130 detailed help for any sub section specified in the comma seperated list of
131 sections provided. If the end of the file or a .TH section is found then
134 Upon a sucessful return the caller is responsible to free the memory in *HelpText
136 @param[in] Buffer Buffer to read from
137 @param[in] Sections name of command's sub sections to find
138 @param[in] HelpText pointer to pointer to string where text goes.
139 @param[in] HelpSize pointer to size of allocated HelpText (may be updated)
141 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
142 @retval EFI_SUCCESS the section was found and its description sotred in
147 ManBufferFindSections(
148 IN CONST CHAR16
*Buffer
,
149 IN CONST CHAR16
*Sections
,
150 IN CHAR16
**HelpText
,
155 CONST CHAR16
*CurrentLocation
;
156 BOOLEAN CurrentlyReading
;
167 return (EFI_INVALID_PARAMETER
);
170 Status
= EFI_SUCCESS
;
171 CurrentlyReading
= FALSE
;
174 for (CurrentLocation
= Buffer
,TempString
= NULL
175 ; CurrentLocation
!= NULL
&& *CurrentLocation
!= CHAR_NULL
176 ; CurrentLocation
=StrStr(CurrentLocation
, L
"\r\n"),TempString
= NULL
178 while(CurrentLocation
[0] == L
'\r' || CurrentLocation
[0] == L
'\n') {
181 if (CurrentLocation
[0] == L
'#') {
183 // Skip comment lines
187 if (StrnCmp(CurrentLocation
, L
".TH", 3) == 0) {
189 // we hit the end of this commands section so stop.
193 if (StrnCmp(CurrentLocation
, L
".SH ", 4) == 0) {
194 if (Sections
== NULL
) {
195 CurrentlyReading
= TRUE
;
197 } else if (CurrentlyReading
) {
198 CurrentlyReading
= FALSE
;
200 CurrentLocation
+= 4;
202 // is this a section we want to read in?
204 if (StrLen(CurrentLocation
)!=0) {
205 TempString2
= StrStr(CurrentLocation
, L
" ");
206 TempString2
= MIN(TempString2
, StrStr(CurrentLocation
, L
"\r"));
207 TempString2
= MIN(TempString2
, StrStr(CurrentLocation
, L
"\n"));
208 ASSERT(TempString
== NULL
);
209 TempString
= StrnCatGrow(&TempString
, NULL
, CurrentLocation
, TempString2
==NULL
?0:TempString2
- CurrentLocation
);
210 if (TempString
== NULL
) {
211 Status
= EFI_OUT_OF_RESOURCES
;
214 SectionName
= TempString
;
215 SectionLen
= StrLen(SectionName
);
216 SectionName
= StrStr(Sections
, SectionName
);
217 if (SectionName
== NULL
) {
220 if (*(SectionName
+ SectionLen
) == CHAR_NULL
|| *(SectionName
+ SectionLen
) == L
',') {
221 CurrentlyReading
= TRUE
;
224 } else if (CurrentlyReading
) {
226 if (StrLen(CurrentLocation
)!=0) {
227 TempString2
= StrStr(CurrentLocation
, L
"\r");
228 TempString2
= MIN(TempString2
, StrStr(CurrentLocation
, L
"\n"));
229 ASSERT(TempString
== NULL
);
230 TempString
= StrnCatGrow(&TempString
, NULL
, CurrentLocation
, TempString2
==NULL
?0:TempString2
- CurrentLocation
);
231 if (TempString
== NULL
) {
232 Status
= EFI_OUT_OF_RESOURCES
;
236 // copy and save the current line.
238 ASSERT((*HelpText
== NULL
&& *HelpSize
== 0) || (*HelpText
!= NULL
));
239 StrnCatGrow (HelpText
, HelpSize
, TempString
, 0);
240 if (HelpText
== NULL
) {
241 Status
= EFI_OUT_OF_RESOURCES
;
244 StrnCatGrow (HelpText
, HelpSize
, L
"\r\n", 0);
245 if (HelpText
== NULL
) {
246 Status
= EFI_OUT_OF_RESOURCES
;
251 SHELL_FREE_NON_NULL(TempString
);
253 if (!Found
&& !EFI_ERROR(Status
)) {
254 return (EFI_NOT_FOUND
);
260 parses through the MAN file specified by SHELL_FILE_HANDLE and returns the
261 detailed help for any sub section specified in the comma seperated list of
262 sections provided. If the end of the file or a .TH section is found then
265 Upon a sucessful return the caller is responsible to free the memory in *HelpText
267 @param[in] Handle FileHandle to read from
268 @param[in] Sections name of command's sub sections to find
269 @param[out] HelpText pointer to pointer to string where text goes.
270 @param[out] HelpSize pointer to size of allocated HelpText (may be updated)
271 @param[in] Ascii TRUE if the file is ASCII, FALSE otherwise.
273 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
274 @retval EFI_SUCCESS the section was found and its description sotred in
280 IN SHELL_FILE_HANDLE Handle
,
281 IN CONST CHAR16
*Sections
,
282 OUT CHAR16
**HelpText
,
290 BOOLEAN CurrentlyReading
;
299 return (EFI_INVALID_PARAMETER
);
302 Status
= EFI_SUCCESS
;
303 CurrentlyReading
= FALSE
;
307 ReadLine
= AllocateZeroPool(Size
);
308 if (ReadLine
== NULL
) {
309 return (EFI_OUT_OF_RESOURCES
);
312 for (;!ShellFileHandleEof(Handle
);Size
= 1024) {
313 Status
= ShellFileHandleReadLine(Handle
, ReadLine
, &Size
, TRUE
, &Ascii
);
314 if (ReadLine
[0] == L
'#') {
316 // Skip comment lines
321 // ignore too small of buffer...
323 if (Status
== EFI_BUFFER_TOO_SMALL
) {
324 Status
= EFI_SUCCESS
;
326 if (EFI_ERROR(Status
)) {
328 } else if (StrnCmp(ReadLine
, L
".TH", 3) == 0) {
330 // we hit the end of this commands section so stop.
333 } else if (StrnCmp(ReadLine
, L
".SH", 3) == 0) {
334 if (Sections
== NULL
) {
335 CurrentlyReading
= TRUE
;
339 // we found a section
341 if (CurrentlyReading
) {
342 CurrentlyReading
= FALSE
;
345 // is this a section we want to read in?
347 for ( SectionName
= ReadLine
+ 3
348 ; *SectionName
== L
' '
350 SectionLen
= StrLen(SectionName
);
351 SectionName
= StrStr(Sections
, SectionName
);
352 if (SectionName
== NULL
) {
355 if (*(SectionName
+ SectionLen
) == CHAR_NULL
|| *(SectionName
+ SectionLen
) == L
',') {
356 CurrentlyReading
= TRUE
;
358 } else if (CurrentlyReading
) {
361 // copy and save the current line.
363 ASSERT((*HelpText
== NULL
&& *HelpSize
== 0) || (*HelpText
!= NULL
));
364 StrnCatGrow (HelpText
, HelpSize
, ReadLine
, 0);
365 StrnCatGrow (HelpText
, HelpSize
, L
"\r\n", 0);
369 if (!Found
&& !EFI_ERROR(Status
)) {
370 return (EFI_NOT_FOUND
);
376 parses through the MAN file formatted Buffer and returns the
377 "Brief Description" for the .TH section as specified by Command. If the
378 command section is not found return EFI_NOT_FOUND.
380 Upon a sucessful return the caller is responsible to free the memory in *BriefDesc
382 @param[in] Handle Buffer to read from
383 @param[in] Command name of command's section to find
384 @param[in] BriefDesc pointer to pointer to string where description goes.
385 @param[in] BriefSize pointer to size of allocated BriefDesc
387 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
388 @retval EFI_SUCCESS the section was found and its description sotred in
393 ManBufferFindTitleSection(
395 IN CONST CHAR16
*Command
,
396 IN CHAR16
**BriefDesc
,
403 CHAR16
*CurrentLocation
;
405 CONST CHAR16 StartString
[] = L
".TH ";
406 CONST CHAR16 EndString
[] = L
" 0 ";
410 || (BriefDesc
!= NULL
&& BriefSize
== NULL
)
412 return (EFI_INVALID_PARAMETER
);
415 Status
= EFI_SUCCESS
;
418 // more characters for StartString and EndString
420 TitleLength
= StrSize(Command
) + (StrLen(StartString
) + StrLen(EndString
)) * sizeof(CHAR16
);
421 TitleString
= AllocateZeroPool(TitleLength
);
422 if (TitleString
== NULL
) {
423 return (EFI_OUT_OF_RESOURCES
);
425 StrCpyS(TitleString
, TitleLength
/sizeof(CHAR16
), StartString
);
426 StrCatS(TitleString
, TitleLength
/sizeof(CHAR16
), Command
);
427 StrCatS(TitleString
, TitleLength
/sizeof(CHAR16
), EndString
);
429 CurrentLocation
= StrStr(*Buffer
, TitleString
);
430 if (CurrentLocation
== NULL
){
431 Status
= EFI_NOT_FOUND
;
434 // we found it so copy out the rest of the line into BriefDesc
435 // After skipping any spaces or zeroes
437 for (CurrentLocation
+= StrLen(TitleString
)
438 ; *CurrentLocation
== L
' ' || *CurrentLocation
== L
'0' || *CurrentLocation
== L
'1' || *CurrentLocation
== L
'\"'
439 ; CurrentLocation
++);
441 TitleEnd
= StrStr(CurrentLocation
, L
"\"");
442 if (TitleEnd
== NULL
) {
443 Status
= EFI_DEVICE_ERROR
;
445 if (BriefDesc
!= NULL
) {
446 *BriefSize
= StrSize(TitleEnd
);
447 *BriefDesc
= AllocateZeroPool(*BriefSize
);
448 if (*BriefDesc
== NULL
) {
449 Status
= EFI_OUT_OF_RESOURCES
;
451 StrnCpyS(*BriefDesc
, (*BriefSize
)/sizeof(CHAR16
), CurrentLocation
, TitleEnd
-CurrentLocation
);
455 for (CurrentLocation
= TitleEnd
456 ; *CurrentLocation
!= L
'\n'
457 ; CurrentLocation
++);
459 ; *CurrentLocation
== L
' ' || *CurrentLocation
== L
'\n' || *CurrentLocation
== L
'\r'
460 ; CurrentLocation
++);
461 *Buffer
= CurrentLocation
;
465 FreePool(TitleString
);
470 Parses a line from a MAN file to see if it is the Title Header. If it is, then
471 if the "Brief Description" is desired, allocate a buffer for it and return a
472 copy. Upon a sucessful return the caller is responsible to free the memory in
475 Uses a simple state machine that allows "unlimited" whitespace before and after the
476 ".TH", compares Command and the MAN file commnd name without respect to case, and
477 allows "unlimited" whitespace and '0' and '1' characters before the Short Description.
478 The PCRE regex describing this functionality is: ^\s*\.TH\s+(\S)\s[\s01]*(.*)$
479 where group 1 is the Command Name and group 2 is the Short Description.
481 @param[in] Command name of command whose MAN file we think Line came from
482 @param[in] Line Pointer to a line from the MAN file
483 @param[out] BriefDesc pointer to pointer to string where description goes.
484 @param[out] BriefSize pointer to size of allocated BriefDesc
485 @param[out] Found TRUE if the Title Header was found and it belongs to Command
487 @retval TRUE Line contained the Title Header
488 @retval FALSE Line did not contain the Title Header
492 IN CONST CHAR16
*Command
,
494 OUT CHAR16
**BriefDesc OPTIONAL
,
495 OUT UINTN
*BriefSize OPTIONAL
,
499 // The states of a simple state machine used to recognize a title header line
500 // and to extract the Short Description, if desired.
502 LookForThMacro
, LookForCommandName
, CompareCommands
, GetBriefDescription
, Final
506 UINTN CommandIndex
; // Indexes Command as we compare its chars to the MAN file.
507 BOOLEAN ReturnValue
; // TRUE if this the Title Header line of *some* MAN file.
508 BOOLEAN ReturnFound
; // TRUE if this the Title Header line of *the desired* MAN file.
513 State
= LookForThMacro
;
517 if (*Line
== L
'\0') {
523 // Handle "^\s*.TH\s"
524 // Go to state LookForCommandName if the title header macro is present; otherwise,
525 // eat white space. If we see something other than white space, this is not a
526 // title header line.
528 if (StrnCmp (L
".TH ", Line
, 4) == 0 || StrnCmp (L
".TH\t", Line
, 4) == 0) {
530 State
= LookForCommandName
;
532 else if (*Line
== L
' ' || *Line
== L
'\t') {
541 // Eat any "extra" whitespace after the title header macro (we have already seen
542 // at least one white space character). Go to state CompareCommands when a
543 // non-white space is seen.
544 case LookForCommandName
:
545 if (*Line
== L
' ' || *Line
== L
'\t') {
549 ReturnValue
= TRUE
; // This is *some* command's title header line.
550 State
= CompareCommands
;
551 // Do not increment Line; it points to the first character of the command
552 // name on the title header line.
557 // Compare Command to the title header command name, ignoring case. When we
558 // reach the end of the command (i.e. we see white space), the next state
559 // depends on whether the caller wants a copy of the Brief Description.
560 case CompareCommands
:
561 if (*Line
== L
' ' || *Line
== L
'\t') {
562 ReturnFound
= TRUE
; // This is the desired command's title header line.
563 State
= (BriefDesc
== NULL
) ? Final
: GetBriefDescription
;
565 else if (InternalShellCharToUpper (*Line
) != InternalShellCharToUpper (*(Command
+ CommandIndex
++))) {
571 // Handle "[\s01]*(.*)$"
572 // Skip whitespace, '0', and '1' characters, if any, prior to the brief description.
573 // Return the description to the caller.
574 case GetBriefDescription
:
575 if (*Line
!= L
' ' && *Line
!= L
'\t' && *Line
!= L
'0' && *Line
!= L
'1') {
576 *BriefSize
= StrSize(Line
);
577 *BriefDesc
= AllocateZeroPool(*BriefSize
);
578 if (*BriefDesc
!= NULL
) {
579 StrCpyS(*BriefDesc
, (*BriefSize
)/sizeof(CHAR16
), Line
);
590 } while (State
< Final
);
592 *Found
= ReturnFound
;
597 parses through the MAN file specified by SHELL_FILE_HANDLE and returns the
598 "Brief Description" for the .TH section as specified by Command. If the
599 command section is not found return EFI_NOT_FOUND.
601 Upon a sucessful return the caller is responsible to free the memory in *BriefDesc
603 @param[in] Handle FileHandle to read from
604 @param[in] Command name of command's section to find as entered on the
605 command line (may be a relative or absolute path or
606 be in any case: upper, lower, or mixed in numerous ways!).
607 @param[out] BriefDesc pointer to pointer to string where description goes.
608 @param[out] BriefSize pointer to size of allocated BriefDesc
609 @param[in, out] Ascii TRUE if the file is ASCII, FALSE otherwise, will be
610 set if the file handle is at the 0 position.
612 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
613 @retval EFI_SUCCESS the section was found and its description stored in
614 an allocated buffer if requested.
618 ManFileFindTitleSection(
619 IN SHELL_FILE_HANDLE Handle
,
620 IN CONST CHAR16
*Command
,
621 OUT CHAR16
**BriefDesc OPTIONAL
,
622 OUT UINTN
*BriefSize OPTIONAL
,
623 IN OUT BOOLEAN
*Ascii
634 || (BriefDesc
!= NULL
&& BriefSize
== NULL
)
636 return (EFI_INVALID_PARAMETER
);
639 Status
= EFI_SUCCESS
;
643 ReadLine
= AllocateZeroPool(Size
);
644 if (ReadLine
== NULL
) {
645 return (EFI_OUT_OF_RESOURCES
);
649 // Do not pass any leading path information that may be present to IsTitleHeader().
651 Start
= StrLen(Command
);
653 && (*(Command
+ Start
- 1) != L
'\\')
654 && (*(Command
+ Start
- 1) != L
'/')
655 && (*(Command
+ Start
- 1) != L
':')) {
659 for (;!ShellFileHandleEof(Handle
);Size
= 1024) {
660 Status
= ShellFileHandleReadLine(Handle
, ReadLine
, &Size
, TRUE
, Ascii
);
662 // ignore too small of buffer...
664 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
668 Status
= EFI_NOT_FOUND
;
669 if (IsTitleHeader (Command
+Start
, ReadLine
, BriefDesc
, BriefSize
, &Found
)) {
670 Status
= Found
? EFI_SUCCESS
: EFI_NOT_FOUND
;
680 This function returns the help information for the specified command. The help text
681 will be parsed from a UEFI Shell manual page. (see UEFI Shell 2.0 Appendix B)
683 If Sections is specified, then each section name listed will be compared in a casesensitive
684 manner, to the section names described in Appendix B. If the section exists,
685 it will be appended to the returned help text. If the section does not exist, no
686 information will be returned. If Sections is NULL, then all help text information
687 available will be returned.
689 if BriefDesc is NULL, then the breif description will not be savedd seperatly,
690 but placed first in the main HelpText.
692 @param[in] ManFileName Points to the NULL-terminated UEFI Shell MAN file name.
693 @param[in] Command Points to the NULL-terminated UEFI Shell command name.
694 @param[in] Sections Points to the NULL-terminated comma-delimited
695 section names to return. If NULL, then all
696 sections will be returned.
697 @param[out] BriefDesc On return, points to a callee-allocated buffer
698 containing brief description text.
699 @param[out] HelpText On return, points to a callee-allocated buffer
700 containing all specified help text.
702 @retval EFI_SUCCESS The help text was returned.
703 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
705 @retval EFI_INVALID_PARAMETER HelpText is NULL.
706 @retval EFI_INVALID_PARAMETER ManFileName is invalid.
707 @retval EFI_NOT_FOUND There is no help text available for Command.
712 IN CONST CHAR16
*ManFileName
,
713 IN CONST CHAR16
*Command
,
714 IN CONST CHAR16
*Sections OPTIONAL
,
715 OUT CHAR16
**BriefDesc OPTIONAL
,
716 OUT CHAR16
**HelpText
720 SHELL_FILE_HANDLE FileHandle
;
726 EFI_DEVICE_PATH_PROTOCOL
*FileDevPath
;
727 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
729 if ( ManFileName
== NULL
733 return (EFI_INVALID_PARAMETER
);
741 // See if it's in HII first
743 TempString
= ShellCommandGetCommandHelp(Command
);
744 if (TempString
!= NULL
) {
745 TempString2
= TempString
;
746 Status
= ManBufferFindTitleSection(&TempString2
, Command
, BriefDesc
, &BriefSize
);
747 if (!EFI_ERROR(Status
) && HelpText
!= NULL
){
748 Status
= ManBufferFindSections(TempString2
, Sections
, HelpText
, &HelpSize
);
752 TempString
= GetManFileName(ManFileName
);
753 if (TempString
== NULL
) {
754 return (EFI_INVALID_PARAMETER
);
757 Status
= SearchPathForFile(TempString
, &FileHandle
);
758 if (EFI_ERROR(Status
)) {
759 FileDevPath
= FileDevicePath(NULL
, TempString
);
760 DevPath
= AppendDevicePath (ShellInfoObject
.ImageDevPath
, FileDevPath
);
761 Status
= InternalOpenFileDevicePath(DevPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
762 FreePool(FileDevPath
);
766 if (!EFI_ERROR(Status
)) {
769 Status
= ManFileFindTitleSection(FileHandle
, Command
, BriefDesc
, &BriefSize
, &Ascii
);
770 if (!EFI_ERROR(Status
) && HelpText
!= NULL
){
771 Status
= ManFileFindSections(FileHandle
, Sections
, HelpText
, &HelpSize
, Ascii
);
773 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
778 if (TempString
!= NULL
) {
779 FreePool(TempString
);