HP 3000 Manuals

Passing the Command Line [ Micro Focus COBOL for UNIX COBOL User Guide ] MPE/iX 5.0 Documentation


Micro Focus COBOL for UNIX COBOL User Guide

Passing the Command Line 

UNIX                  This section describes the various ways in which
                      you can call a program and pass the command line to
                      the main program as a parameter.  The main program
                      in a run-unit is the first program within it; that
                      is, the program which is called directly by the
                      COBOL system.  The COBOL system treats the main
                      program in a run-unit as a special case.  It can be
                      regarded as being called by the system shell, with
                      the command line representing the one parameter to
                      the call.

As the maximum size, form and contents of the command line vary between
operating systems, these issues need to be considered if you want to port
your application between environments.  For example, to ensure
portability, you might want to use a maximum command line size of 128
characters rather than the system shell limit.  Similarly, only
alphabetic and numeric characters together with the equals sign (=) and
space characters should be used for entering the command line for a
program if you want to guarantee portability.

The UNIX system might modify the user entered command line whenever
metacharacters such as the asterisk (*), question mark (?), parentheses (
), backslash (\) or quotation marks (") are entered on the command line.
The parentheses are often used in command lines in non-UNIX environments,
so the cobrun command allows the equals sign (=) to be used to represent
parentheses.  For example, if you enter a command line in the following
format:

     cobrun prog param1 param2=param3 param4=  param5

the command is actually passed to the COBOL program in the following
format:

     param1 param2(param3) param4() param5

(This is only supported if you are using the cobrun command.)

The command line can be accessed from COBOL in one of the following ways:

       ACCEPT...FROM COMMAND-LINE

       ACCEPT...FROM ARGUMENT-NUMBER/ARGUMENT-VALUE

       Linkage Section

       ACCEPT...FROM CONSOLE

       READ from stdin

Each of these methods is discussed in the following sections.

ACCEPT...FROM COMMAND-LINE 

The ACCEPT data-name FROM COMMAND-LINE statement causes the complete
contents of the command line to be moved to data-name.  See your Language 
Reference for complete details.

ACCEPT...FROM ARGUMENT NUMBER/ARGUMENT-VALUE 

This statement processes the command line as a series of space-delimited
parameters.  See your Language Reference for details.

Linkage Section 

This method enables you to access command lines of unlimited length
without allocating a fixed-length buffer.

Set up a record of the following format in the Linkage Section of your
main program:

      01 cmd-line.
          05 argc     pic 9(4) comp.
          05 arg.
              10 args pic x
                      occurs 0 to 65535 depending on argc.

and use the following Procedure Division header:

      procedure division using cmd-line.

This causes the main program to be invoked as though it had been called
from a COBOL program with a CALL statement of the form:

          call "program-name" using cmd-line.

You can substitute your own names for the items shown in the above
example, but you must use a similar format.

The data item argc contains a count of the actual number of occurrences
of args, that is, the number of characters on the command line.  We
recommend you to test that the length field contains a non-zero value
which does not exceed the maximum limit of the occurs.

You should take care not to access data beyond the end of the command
line (for example, by defining a fixed length field and then moving it)
as this would be an illegal reference and could give you a memory
violation on some systems.

Consider the following example:

      working-storage section.
      01 argv                 pic x(20).
      01 argv-length          pic 9(4) comp.
      01 argv-max-length      pic 9(4) comp value 20.
      01 next-argv            pic 9(4) comp value 1.
      linkage section.
      01 cmd-param.
          03 cmd-length       pic 9(4) comp-x.
          03 cmd-line.
              05 cmd-char     pic x
                              occurs 1 to 999 depending on cmd-length.
      procedure division using cmd-param.
      a000 section.
          if cmd-length = 0
              stop "No command line               "
          end-if
          if cmd-length > 999
              display "Command line too long" stop run
          end-if
          perform until next-argv > cmd-length
              unstring cmd-line delimited by space into argv
                  count in argv-length
                  with pointer next-argv
              if argv-length > argv-max-length
                  display "Argument too long"
              else
                  perform process-argv
              end-if
          end-perform
         ...
      process-argv.
         ...

To ensure that your program is portable, you must use the OCCURS
DEPENDING clause.  If you do not use this clause, characters after the
end of the specified command line length might be accessed, thereby
giving a memory violation error on some systems.

The length of the command line is held as a two-byte integer which can
hold values larger than the COBOL picture.

ACCEPT...FROM CONSOLE and READ from stdin 

The following formats are provided for compatibility with earlier COBOL
versions and can be enabled by setting the appropriate cobconfig value
(see the chapter Run-time Configuration in your COBOL System Reference 
for details).

The first ACCEPT data-name [FROM CONSOLE] executed in a program behaves
in the same way as ACCEPT data-name FROM COMMAND-LINE. Subsequent
ACCEPT...FROM CONSOLE statements read the standard input.

A LINE SEQUENTIAL file named :CI: or stdin reads standard input, except
for the first read, which returns the command line into the input buffer.



MPE/iX 5.0 Documentation