Next / Previous / Contents / TCC Help System / NM Tech homepage

4.5. Main program

Here is the main program. The source code comments in square brackets are intended functions for the Cleanroom software development methodology.

manweb.cgi
# - - -   m a i n

def main():
    """Main program: display a man page in HTML.

      [ if (this script is being executed by the CGI protocol) and
        (its arguments are valid) and
        (the arguments described an existing man page ->
          sys.stdout  +:=  the output of man2html reformatting
                           that page
        else ->
          sys.stdout  +:=  an error message as HTML ]
    """

We must check that the protocol is CGI, retrieve the arguments, and check them for validity. See Section 4.6, “processArguments(): Retrieve and check the CGI arguments”, which does not return unless everything is valid.

manweb.cgi
    #-- 1 --
    # [ if (this script is being executed by the CGI protocol) and
    #   (its arguments are valid) ->
    #     pageName  :=  the page name argument
    #     section  :=  the section number argument, defaulting to "1"
    #   else ->
    #     sys.stdout  +:=  an error message as HTML
    #     stop execution ]
    pageName, section, suffix  =  processArguments()

Next we build the absolute path name to the input file and check to see if it exists. See Section 4.7, “findManFile(): Locate the input file”.

manweb.cgi
    #-- 3 --
    # [ if the man file specified by pagename and section exists ->
    #     manPath  :=  the absolute pathname to that file
    #   else ->
    #     sys.stdout  +:=  an error message as HTML
    #     stop execution ]
    manPath  =  findManFile ( pageName, section, suffix )

At this point we can fire up man2html, feed it the file, and route its output back to our standard output stream. Things can still go wrong, however. See Section 4.8, “convert: Convert to HTML and output”.

manweb.cgi
    #-- 4 --
    # [ if manPath specifies a readable, valid man file ->
    #     sys.stdout  +:=  the output of man2html operating on
    #                      that file
    #   else ->
    #     sys.stdout  +:=  an error message as HTML ]
    convert ( manPath )