WScript.Echo ManToText(ReadFile(

Function ChoosePageNumber
        On Error Resume next
        ChoosePageNumber = WScript.arguments(1)
        If Err.Number = 9 Then ChoosePageNumber = 1
End Function

Function GetManFileData(filepath)
        ' wrapper to extract man file data in "clean" form.
        ' can look for redirected files
        Dim sData, manfile, manfilepath, i
        sData = ReadFile(filepath)
        If len(sData)<40 then
                ' this is probably a redirected man page, so look for original
                Do While (Left(sData,4)) = ".so "
                        i = i + 1
                        manfile = ExtractManFileBaseRedirect(sData)
                        manfilepath = manpath & "\man" & Right(manfile, 1) & "\" & manfile
                        sData = ReadFile(manfilepath)
                        if i > 8 then Exit do ' we shouldn't recurse very far
                Loop
        end if
        'WScript.echo "i:", i
        'WScript.echo "i:", i
        GetManFileData = ManToText(sData)
end function

Function ManToText(sData)
        Dim rx, sLocal
        ' first localize the data.  While doing so we strip the .SH/.Sh section
        ' headers to prevent bad line joins when we clean bolded text for output.
        sLocal = Replace(sData, ".sh ", vbCr, 1, -1, vbTextCompare)
        ' now ensure that we have uniform vbCr line terminations
        sLocal = Replace(sLocal, vbCrLf, vbCr)
        sLocal = Replace(sLocal, vbLf, vbCr)
        set rx = new regexp
        rx.multiline = true
        rx.global = true
        rx.IgnoreCase = true
        rx.Pattern = "\r\.BR (.+)\r"
        sLocal = rx.Replace(sLocal, " $2 ")
        rx.Pattern = "\r+\.[A-Z]+ (.+)\r"
        sLocal = rx.Replace(sLocal, " $1")
        rx.Pattern = "(^\.[A-Z]+)|(\\f[A-Z])|((\\)(-))|(^\.\\.+\r)"
        sLocal = rx.Replace(sLocal, "$5")
        rx.Pattern = " \.$"
        ManToText = rx.Replace(sLocal, ".")
end Function


function ExtractManFileBaseRedirect(sData)
        ' takes data read within a man page presumed to be redirected
        ' extracts the base package name for the redirect and the man #
        Dim Local, roffdata, PathSep
        roffdata = ".so " ' probable instruction if not a symlink
        PathSep = "/" 'presumed path separator in man file
        ' first step is to remove the prefix for *roff-like redirect
        Local = Replace(sData, roffdata, "")
        ' some man files have an explicit man# folder reference;
        ' we want to remove that if present
        Local = Mid(Local, InStr(Local, PathSep)+1 )
        ' split the string at the "." to get
        ' (0) -> base package name
        ' (1) -> manpage number and other garbage
        Local = Split(Local, ".")
        ' there are only 8 canonical man numbers;
        ' this means we have a single-character suffix
        Local(1) = Left(Local(1), 1)
        ExtractManFileBaseRedirect = Join(Local, ".")
end Function

function RaggedCleanMan(ByVal sData)
        ' cleans up residual leftovers from man page
        dim cruft, element
        cruft = Array("\(co","\&.","\^", "\.")
        for each element in cruft
                sData = Replace(sData, element, "")
        next
        RaggedCleanMan = sData
end function

Function ReadFile(FilePath)
        'Given the path to a file, will return entire contents
        ' works with either ANSI or Unicode
        Dim FSO, CurrentFile
        Const ForReading = 1, TristateUseDefault = -2, _
                DoNotCreateFile = False
        Set FSO = createobject("Scripting.FileSystemObject")
        If FSO.FileExists(FilePath) Then
                If FSO.GetFile(FilePath).Size>0 Then
                        Set CurrentFile = FSO.OpenTextFile(FilePath, ForReading, _
                                False, TristateUseDefault)
                        ReadFile = CurrentFile.ReadAll: CurrentFile.Close
                End If
        End If
End Function