Author Archives: yrangana

ILE RPG, RPG IV Specifications (H, F, D, I, C, O and P)

Specifications in RPGLE

The first thing you should know in RPG IV is that RPG IV is a positional language. This means that we are allowed to write code at specific columns only. In RPGLE every line of code begins with the declaration of specifications. Actually in RPG IV all types of statements have been categorized. The specification tells the compiler which category the coming statement falls into. That’s the RPGLE compiler interprets a specific line of code on the basis of the specification. In RPG IV we have the following main specifications. All these specifications begin on the 6th column.
There are the following specifications in RPG IV (ILE RPG) .

  • The H SpecificationThis specification is used to give the instructions to compiler. For example the data format to be used within the program. Whether source debugging is allowed or not etc. Now-a-days most of these compiler instructions are given while compiling the program only. However, the most frequently used compiler instruction in RPGLE is NOMAIN.
  • The F SpecificationThis specification is also known as the File specification. Here we declare all the files which we will be using in the program. The files might be any of the physical file, logical file, display file or the printer file. Message files are not declared in the F specification.
  • The D SpecificationWe declare all the variables to be used in the program in the D specs. Though we can also declare the variables on the ad-hoc basis, this is strongly discouraged for it makes the maintenance of huge programs a miserable task.
  • The I SpecificationI specifications are used to rename some fields or record format names. Now a days renaming of record formats is done in F-Specs only. To rename a record format in F-Spec we use the keyword (Rename) as explained in the F Specification section of this tutorial.
  • The C SpecificationThis is the most widely used specification. All the calculations take place here. All the file operations, calculations, Calls to other programs, calls to procedures etc. are done in this specification only. A typical C-Specification statement looks as below.
  • Output specifications, also called “O specs”. These define the output record layouts for your files. The RPG compiler generates O specs for externally-described output-capable files.
  • The P SpecificationThese statements mark the boundary of the Procedure. We will learn more about procedures in the Modules and Procedures and Sub procedures sections. Now that we have had a little idea of an RPGLE specification we can proceed to our first RPGLE program.

The specifications must appear in the order given above, but it is not necessary to code all the specifications. A module could contain just a single C spec.

Most RPG specifications contain the specification type in column 6. The exception is free-form calculation specifications which are coded between /FREE and /END-FREE directives. Free-form calculations have blanks in column 6 and 7. The examples given in chapter 1 all used free-form calculations.

RPG cycle

RPG was originally created to handle files. The intention was that the program would read a record from the “primary file” and for each record, it would perform the calculations. When it reached the last record, it would close the file and end the program.

To artificially create the “last record” situation, the program could set on the “Last Record” indicator, called *INLR.

For programs that do not have a primary file, it is still necessary to stop the program from looping through the calculations. This can be done two ways:

  1. By using the RETURN operation.
  2. By setting on the Last Record indicator.
       *inlr = '1';

There are subtle differences in these two mechanisms that you will learn in later

 

Advertisements

Simple RPGLE Program (Hello World)

First RPGLE Program

Create a member “helloworld” in your working source physical file. Give the type of this member as “RPGLE”. Now type the following code in the source member.

HELLO WORLD

HELLO WORLD

Now compile the module. Create the program and call it, It Will display the phrase Hello World.

That is a basic RPGLE program but you learn several things as discussed below.

  • The C specs is mentioned on the 6th place. Other specifications are also specified at this place only. Other specifications are H, F,D, I and P. (Specifications O and E are RPG specifications)
  • The text Hello world is written at ‘Factor one’ position. i.e at the 12th place. At this place we specify the factor one of any operation code in RPGLE. You will use this factor frequently for declaring keylists of file and defining parameter list.
  • The Op codes in RPGLE. The opcodes are placed at the position starting from 26. In the example above we have used two opcodes dsply and Return.
  • Dsply – This opcode is used to display any text as the output. The program does not stop execution but halts for a small time. When you press the return key. The subsequent statements are executed. This opcode can be used to display a text of maximum 52 length.
  • Return – This opcode is a required keyword. When the program runs this statement, it terminates its execution and returns to the calling program immediately. In the example above the caller is the system as we have called this program from command line, hence the program will end when it executes the return statement.

CL Procedures

A CL procedure is a group of CL commands that tells the system where to get input, how to process it, and where to place the results. The procedure is assigned a name by which it can then be called by other procedures or bound into a program and called. As with other kinds of procedures, you must enter CL procedure source statements, compile, and bind them before you can run the procedure.

When you enter CL commands individually (from the Command Entry display, for instance, or as individual commands in an input stream), each command is separately processed. When you enter CL commands as source statements for a CL procedure, the source remains for later modification if you choose, and the commands are compiled into a module. This module remains as a permanent system object that can be bound into other programs and run. Thus, CL is actually a high-level programming language for system functions. CL procedures ensure consistent processing of groups of commands. You can perform functions with a CL procedure that you cannot perform by entering commands individually, and the CL program or procedure provides better performance at run time than the processing of several separate commands.

CL procedures can be used in batch or interactive processing. Certain commands or functions are restricted to either batch or interactive jobs.

CL source statements consist of CL commands. You cannot use all CL commands as CL source statements, and you can use some of them only in CL procedures or OPM programs. You can determine what restrictions you want placed on the use of CL commands. You can do this by checking the box in the upper right-hand corner of the syntax diagram of a command. An example that uses the Program (PGM) command is shown below:

PGM: B,I

PGM: B,I

Notes:
1 A maximum of 40 repetitions.
2 All parameters preceding this point can be specified positionally.

The Pgm: B,I in the syntax diagram for the PGM command shows that this command can be used in either batch or interactive jobs, but can be used only within a CL program or procedure.

The commands that you can use only as source statements in CL programs and procedures will have Pgm: in the box. If the box does not contain this indicator, you cannot use the command as source for a CL program or procedure. IBM has online information about how to read a syntax diagram.

CL source statements can be entered in a database source member either interactively from a work station or in a batch job input stream from a device. To create a program using CL source statements, you must enter the source statements into a database source member. You can then create an ILE program by compiling the source member into a module and binding the module into a program object.

CL procedures can be written for many purposes, including:

  • To control the sequence of processing and calling of other programs or procedures.
  • To display a menu and run commands based on options selected from that menu. This makes the work station user’s job easier and reduces errors.
  • To read a database file.
  • To handle error conditions issued from commands, programs or procedures, by monitoring for specific messages.
  • To control the operation of an application by establishing variables used in the application, such as date, time, and external indicators.
  • To provide predefined functions for the system operator, such as starting a subsystem or saving files. This reduces the number of commands the operator uses regularly, and it ensures that system operations are performed consistently.

There are many advantages in using CL procedures for an application. For example:

  • Because the commands are stored in a form that can be processed when the program is created, using programs is faster than entering and running the commands individually.
  • CL procedures are flexible. Parameters can be passed to CL procedures to adapt the operations performed by the procedure to the requirements of a particular use.
  • CL procedures can be tested and debugged like other high-level language programs and procedures.
  • CL procedures and programs can incorporate conditional logic and special functions not available when commands are entered individually.
  • CL procedures can be bound with procedures of other languages.

You cannot use CL procedures to:

  • Add or update records in database files.
  • Use printer or ICF files.
  • Use sub files within display files.
  • Use program-described display files.

Parts of a CL Procedure
While each source statement entered as part of a CL procedure is actually a CL command, the source can be divided into the following basic parts used in many typical CL procedures.

PGM command
PGM PARM(&A)
Optional PGM command beginning the procedure and identifying any parameters received.

Declare commands
(DCL, DCLF)
Mandatory declaration of procedure variables when variables are used. The declare commands must precede all other commands except the PGM command.

CL processing commands
CHGVAR, SNDPGMMSG, OVRDBF, DLTF, …
CL commands used as source statements to manipulate constants or variables (this is a partial list).

Logic control commands
IF, THEN, ELSE, DO, ENDDO, GOTO
Commands used to control processing within the CL procedure.

Built-in functions
%SUBSTRING (%SST), %SWITCH, and %BINARY (%BIN)
Built-in functions and operators used in arithmetic, relational or logical expressions.

Program control commands
CALL, RETURN
CL commands used to pass control to other programs.

Procedure control commands
CALLPRC, RETURN
CL commands used to pass control to other procedures.

ENDPGM command
ENDPGM
Optional End Program command.

The sequence, combination, and extent of these components are determined by the logic and design of your application.

Reference : OS/400 CL Programming V5R2

Using the SEU Editor

The IBM AS/400 provides an integrated set of Application Development Tools (ADT) to design, develop and maintain applications. One such tool is the Programming Development Manager (PDM) that offers the following:

  • Integrated application development environment.
  • List-oriented selection of items for development or maintenance.
  • Extendable interface to tools through user-defined options.

Another tool is the Source Entry Utility (SEU) that offers a full screen editor providing syntax checking of source statements. PDM is one tool that may be used to access the SEU.

Starting the Editor

Type STRPDM (Start Program Development Manager) on any command line, and press <Enter> . The PDM menu is displayed. Choose option 3 to Work with members. The “Work with members” screen shows up. You can also reach this screen by typing WRKMBRPDM (Work with members using PDM) on any command line and pressing . In either case, supply the necessary source file (SRCFILE) and library (YOURLIB) information and press Enter.

Type 2 and press Enter on the “Opt” line to edit the source member “SRCMBR”. the screen will be like shown.

SEU1

Type I (for insert) on the first line as shown and press <Enter> .

SEU Line commands

Similarly you can have number of SEU Line commands to perform various operations. Following is a list of valid commands.

  • A=After Move or copy records after this record.
  • An=After with repeat n times Move or copy records after this record and repeat these records n times.
  • B=Before Move or copy records before this record.
  • Bn=Before with repeat n times Move or copy records before this record and repeat these records n times.
  • C=Copy Line Copy this line to a specified target.
  • Cn=Copy n lines Copy this line plus the next n-1 lines to the specified target.
  • CC=Block Copy Copy all lines between the boundaries formed by the two CC commands
  • CR=Copy records and retain command Copy this record to the specified targets and keep this command on the display.
  • CRn=Copy n records and retain command. Copy n records to the specified targets and keep this command on the display.
  • CCR=Copy block records and retain command Copy the block of records defined by a pair of CCR commands to the specified targets and keep this command on the display.
  • D=Delete Delete the current line.
  • Dn=Delete n lines Delete the current line and the next n-1 lines.
  • DD=Block Delete Delete all lines between the two DD boundaries.
  • F=Display format line
  • Display a format line.
  • I=Insert a line Insert a blank line after the current record.
  • In=Insert n lines Insert n blank lines after this record.
  • IP=Insert line and prompt Insert a blank line and display the line in a prompt.
  • L=Shift data 1 char left Shift data in this record one character position to the left without losing data.
  • Ln=Shift data n chars left Shift data in this record n character positions to the left without losing data.
  • LL=Shift block 1 char to the left Shift data defined by the boundary between and including the two LL line commands one character position to the left without losing data.
  • LLn=Shift block n chars to the left Shift data defined by the boundary between and including the LLn and LL line commands n character positions to the left.
  • M=Move a line Move a line to a specified target.
  • Mn=Move n lines Move the current line and the following n-1 lines to a specified target.
  • MM=Block Move Move all records between and including the boundaries defined by the MM line commands to a specified target.
  • O=Overlay Overlay the current line with the first line defined by the move, copy, or copy repeated line command.
  • On=Overlay n lines Overlay this line and the following n-1 lines with the first n lines defined by the move, copy, or copy repeated line command.
  • OO=Block overlay Overlay all the records between and including the boundaries defined by two OO line commands with the lines defined by the move, copy, or copy repeated line command.
  • P=Display prompt Type P to display this line in a prompt.
  • R=Shift data 1 char right Shift data in the current record one character position to the right without losing data.
  • Rn=Shift data n chars right Shift data in the current record n character positions to the right without losing data.
  • RR=Block shift 1 char to the right Shift data defined by the boundary between and including the two RR line commands one character position to the right without losing data.
  • RRn=Block shift n chars to the right Shift data defined by the boundary between and including the RRn and RR line commands n character positions to the right, without losing data.
  • RP=Repeat line Repeat the current line once before the following line.
  • RPn=Repeat line n times Repeat the current line n times before the following line.
  • RPP=Block repeat Repeat all lines defined by the boundary between the two RPP line commands.
  • RPPn=Block repeat Repeat all lines defined by the boundary between the two RPP line commands n times.
  • SF=Show first record Show the first record of the exclude group.
  • SFn=Show first n records Show the first n records of the exclude group.
  • SL=Show last record Show the last record of the exclude group
  • SLn=Show last n records Show the last n records of the exclude group.
  • W=Display member from column 1 Display the member beginning in column 1.
  • Wn=Display member from column n Display the member beginning in column n.
  • X=Exclude Exclude the current record from the display.
  • Xn=Exclude plus Exclude the the current record and the next n-1 records.
  • XX=Block Exclude Exclude all lines between the boundaries formed by the two XX line commands.
  • + (plus sign)=Roll member forward 1 line Roll the member forward one line.
  • +n=Roll member forward n lines Roll the member forward n lines.
  • – (minus sign)=Roll member backward 1 line Roll the member backward one line.
  • -n=Roll member backward n lines Roll the member backward n lines.
  • n=Absolute Positioning Position the line identified by the sequence number(n) as the first record on the display.

SEU Commands

SEU commands are entered in the “SEU==>” field at the top of the screen.

FIND

Search for an occurrence of a character string. If the string contains embedded blanks, apostrophes, or quotation marks, it should be enclosed in quotation marks. Several direction parameters are available: N (Next), P (Previous), F (First), L (Last), and A (All). The scope of the search can be specified using search parameters: X (excluded records) and NX (non-excluded records). The syntax of FIND is FIND string [N,P,A,F,L] [X,NX] [column parameter]. Examples:

  • F XYZ
  • F ‘hello there’
  • F *ERR A find all syntax errors
  • F “‘XYZ'”

Use the F16 “Repeat Find” key to find the next occurrence of a string.

CHANGE

Find all occurrences of a character string and replace it with another string. The syntax is CHANGE fstring cstring [N,P,A,F,L] [X,NX] [column parameter]

TOP

Go to the first page of the work screen.

BOTTOM

Go to the last page of the work screen.

SAVE

Save changes and continue editing. SAVE [ [ [ library/ ] file ] member ]

FILE

Save changes and exit from edit session. FILE [ [ [ library/ ] file ] member ]

CANCEL

Cancel session and exit session.

SET

See the IBM SEU manual for more information.

Program Development Manager (PDM)

Programs are created from source statements. On the AS/400, source statements are entered using PDM, the Program Development manager. Technically, PDM uses the Source Entry Utility (SEU) as the editor. But you don’t have to think of SEU as something different from PDM.

PDM provides a way to enter program source statements and then to compile the statements into executable programs. The source statements are needed only for the compiler. The compiled programs are the objects that are actually run on the AS/400. The source code and the compiled objects are separate things. But it is easy to confuse the two. At times, you may try to open the compiled object with PDM when you really mean to open the source code used to create the compiled object.

Source code is kept in a member of a file. The file is kept in a library. Each file can have many members. Each member is the source code for a program.

Starting PDM

PDM may either be started using the command STRPDM (for Start PDM,) which will produce a menu of options for the level on which the user wishes to work (libraries, objects, or members,) or using one of three commands which indicate the level at which the user would like to work:

  • WRKLIBPDM — Work with libraries using PDM
  • WRKOBJPDM — Work with objects using PDM
  • WRKMBRPDM — Work with file members using PDM

Work with Objects

One of the most commonly used commands to start PDM is WRKOBJPDM. This allows the user to work with objects inside a library. Type WRKOBJPDM and press to prompt on it.

WRKOBJPDM

WRKOBJPDM

The *PRV value on the “Library” field indicates that the default is to work on the library the user last worked with. These may be replaced with the name of a specific library. The “Object” and “Object type” options allow the user to limit which objects will be displayed.

The screen resulting from command WRKOBJPDM, with the library specified as YASIRU, and *ALL on the other options, is shown below.

WORK With OBJECTS USING PDM

WORK With OBJECTS USING PDM

PDM Options

Options are displayed across the top of the screen (for a list of all the options available in PDM, see Table 1 at the end of this section.) Each option is an action which may be performed on an object by placing its number on the “Opt” line next to the object and pressing the < Enter> key. This method of specifying actions on a form listing the objects available is consistent throughout PDM.

There are two important special features which this method of specifying actions offers: First, the options support the prompting facility, and second, multiple objects may be operated on in succession. To use the prompting facility, simply press while an option number is on one of the “Opt” lines. PDM will present the prompt screen for the command associated with that option number. Multiple objects may be operated on sequentially by placing numbers on more than one Opt line. For example, assume we have two source files in “YASIRU”. You can type “5” (Display) against one and type “7” (Rename) against the other. Pressing causes PDM to process each option in turn.

More on PDM levels

Moving down levels in PDM is accomplished using the “12” (Work With…) option. At the WRKOBJPDM screen, the action of this option depends on the file type. Placing a 12 on the line next to an item on this display will:

  • If the item is of type *FILE, go to the WRKMBRPDM display for that file (there are different types of WRKMBRPDM displays for source and data files.)
  • If the item is of type *OUTQ, go to the “Work With Output Queue” display, allowing the user to work with spooled files.
  • If the item is of type *PGM, present a display allowing the user to call the program, change its attributes, etc. (programs may also be called from the WRKOBJPDM screen by using the 16 option.)

When this method of changing levels is used, pressing <Enter> or <F12> (cancel) with no options on the screen returns to the previous level.

Work with Members

The WRKMBRPDM display is the bottom level of PDM. When working with a source physical file at this level, option 2 runs the STRSEU command to begin editing a member and 14 calls the compiler for a program of that type (if specific compiler options are needed, for instance the *DEBUG option to include debugging information in the program, remember that the prompting facility may be invoked on any PDM option.) When working with a physical data file, the “2=change” option is no longer valid. However, the “18” option may be invoked to modify the file using DFU (Data File Utility).

Type “12” against “SRCFILE” to work with it. The following “Work with Members using PDM” screen appears:

WORK With MEMBERS USING PDM

WORK With MEMBERS USING PDM

Presently, there are no members in “QCLSRC”. Let’s create a member. To do that, press (Create). This will automatically invoke the SEU editor. Enter the name of the new source member and for its source type, enter “TXT” (i.e. the source member will contain only plain text). Press .

STRSEU

STRSEU

You will now be inside the SEU editor. For now, press to quit and type “Y” in the “Change/Create member” field to save the source member. On return to the WRKMBRPDM screen, you will notice that “SRCMBR” is now in “SRCFILE”.

The “Work with Members using PDM” screen can be accessed directly using the WRKMBRPDM command. Type this command and press to prompt on it. To work with the member that we have just created, enter the names of the library and file as shown below. If you have many members in your file, you can use the “Member” and/or “Member Type” fields to narrow down the number of members to work with.