Handling COPY Statements

Restriction: The Integrated Preprocessor is supported for native COBOL only.

If the preprocessor is not interested in the contents of a copybook, it can pass the COPY statement unmodified through to the Compiler, where it is expanded.

Only valid COBOL constructs can be passed in this way. Any non-COBOL constructs can be commented out and replaced by a valid COPY statement which the Compiler then expands.

In both of these cases, the preprocessor has no opportunity to read and process the copybook itself.

The Compiler expands all forms of COPY statement that it would expand with no preprocessor present, whether passed back as unchanged or modified lines. The following are supported:

If the preprocessor does want to examine the copybook contents, it must either expand the copybook itself, or use the CP preprocessor. The contents of the copybook itself are returned to the Compiler in the same manner as the lines in the main source file; however, the COPY statement itself receives special handling.

In the simplest case, when the COPY statement is the only statement on one or more lines, and specifies the full filename (including the extension and path if necessary), the first of these lines is passed to the Compiler marked with resp-main set to 3 and resp-more set to the column number of the start of the COPY, and all subsequent lines are passed with resp-main set to 4.

If the location of the file specified in the COPY statement is resolved by adding a filename extension or path, or if the COPY is not unique on the line(s), or is not a regular COBOL COPY statement, it is necessary to comment out (mark with resp-main set to 2) all lines containing the COPY statement and then pass through all other lines as inserted. Inserted COPY statements that the preprocessor is expanding must conform to the normal syntax rules for COBOL, and be marked with:

For example, if the source contains:

01 item-a. copy "cpy-fil.cpy".

This is first returned with resp-main set to 2 to indicate that this is a line that is about to be replaced. On the next call, the preprocessor returns:

01 item-a.

with resp-main set to 1 to indicate that this is a replacement line. On the next call, the preprocessor returns:

copy "copy-fil.cpy".

This time, resp-main is set to 11 to indicate this is a replacement line containing the COPY statement alone. resp-more is set to 20, the position of the word COPY on the original source line.

Note that when a COPY statement is returned to the Compiler, whether to be expanded by the preprocessor or Compiler, the Compiler parses it and checks for REPLACING. This REPLACING affects all lines in this and nested copybooks. The name specified in this COPY statement is read and stored for use by the Debugger. It is strongly recommended that the name be enclosed in quotes so that no unwanted case folding occurs when reading the name as, on platforms where case is significant, the Debugger might not be able to locate the file.
Not base level: resp-main=14 is used when the original word was not a COBOL COPY statements, for example ++INCLUDE or -INC. Some Compiler directives can be specified in $SET statements before any COBOL source but not after. Such statements might follow ++INCLUDE but not COPY. Use of resp-main=14 allows a ++INCLUDE to be expanded whilst at the same time allowing such directives to be set subsequently.

Value 128 should only be used for resp-main when the preprocessor has finished expanding a copybook. If it is not, the Compiler aborts the compilation. The value 0 should be used at the end of the main source file.

COPY statements in Identification Division comment entries are not always expanded. If a preprocessor signals that it is about to expand a copybook (resp-main set to 3 or 11) at a point where this is not valid, the Compiler sets response-status to non-zero on the next call to the preprocessor. The preprocessor should abandon the copybook immediately and send the end-of-copybook marker (resp-main set to 128) as though the copybook were empty.

The CP preprocessor expands copybooks so that other preprocessors do not have to do so. It generates lines as documented above.