Leave That Cursor There!
When a user presses the Enter key, the cursor normally moves to the first unprotected input field on the display. In some interactive programs, I want the cursor to remain where it was when the user pressed Enter. To do this, I retrieve the cursor location from positions 370 and 371 of the file’s information data structure (INFDS), and then I transfer the value from the INFDS fields to the fields listed in the cursor location (CSRLOC) parameter of the display file’s DDS.
The DDS and RPG IV fragments in Figure 1 illustrate how this is done. — John Albert
Recreational Equipment, Inc.
Use EVAL to Set Adjacent Indicators
Q: In RPG III, I can use MOVEA to set a string of adjacent indicators, as in Figure
2. Is there a way to do this using EVAL in RPG IV? — Carolyn Cilla
A: If the indicators are contiguous, you can use code like that in Figure 3; as a byproduct, you get named indicators without waiting for V4R2.
— Derek Butland
A: You don’t have to name the individual indicators if you don’t want to. The code in Figure 4 shows how to access all 99 general indicators or some portion thereof.
— Ted Holt Senior Technical Editor Midrange Computing
Make Inactive Interactive Programs Timeout
Users tend to leave interactive programs running unnecessarily, sometimes causing problems. Another user may need a record that’s locked, or other people may see information to which they’re not authorized.
You can easily make interactive programs timeout. The technique shown here works with both RPG III and RPG IV. I will illustrate with RPG III.
In your display file, code the INVITE keyword at the record level, as in Figure 5. Create the display file, specifying WAITRCD(x), where x is the number of seconds you want to allow the display to remain untouched before timing out.
In your RPG program, add two file continuation lines. One should have the NUM keyword, with a value of 1. (This causes RPG to treat the program as a multiple device file, which is necessary for this technique to work.) The other should have the information data structure (INFDS) keyword and the name of a file information data structure. See Figure 6 for an example.
The file INFDS can have as many subfields as you like, but one of them should be the *STATUS subfield so you can test whether the display file timed out or had some other problem.
Instead of EXFMT, use WRITE to a record format name followed by READ to a file name. In the “Lo” resulting indicator position, specify an indicator that you want to turn on when the display times out.
Follow the READ with a test of the indicator and the *STATUS code. If the display times out, the indicator will turn on and the status code will be set to 1331.
— Ted Holt Senior Technical Editor Midrange Computing
How Do You Spell Overflow?
RPG programmers are well accustomed to checking for printer overflow in RPG programs. Perhaps the most common method is by coding an indicator in the file description specification of a printer file. Another method, one that works for externally described printer files, is to code an indicator in the “Lo” resulting indicator position of a C-spec, using the WRITE operation.
There’s another way, one that gives more flexibility than either of those methods. Use the printer information data structure (IFNDS). When coding the file specification, specify a file INFDS. In the example in Figure 7, the RPG III program defines printer file PRINT with a file information data structure called PINFDS.
The data structure needs two subfields, one for overflow line and one for current line, as in Figure 8.
This comes in handy with multiple line printing. Suppose you have a group of lines, each one defined as a separate record, that should print together on the same page. If you use either the indicator on the WRITE op code or the indicator on the file specification, you will not know you hit overflow until you hit it. Part of the group might print at the bottom of one page while the rest of the group prints on the top of the next page. Using the printer IFNDS lets you determine how many lines remain until page overflow. If you determine that there is not enough room left on the page to accommodate the entire group, you can print them at the top of the next page instead.
Even when I use this method, I still code an indicator in each printer file specification I use. RPG will hard halt if you reach overflow and do not have an indicator
specified either in the file definition spec or on the WRITE op code. Since there is only one file definition spec and there are many WRITES, I opt for the easier of the two.
— Tim Johnston Technical Editor Midrange Computing
Just Right Justification?
Q: There is no op code in RPG that right justifies data. For example, if a 4-byte character variable contains the value “123 ”, I want another variable to be “ 123”. Does anyone have a routine they’d care to share with me?
— Joe Koontz
A: The best I’ve found so far is in Figure 9, but it’s pretty ugly. — John V. Thompson
Honda New Zealand
A: If you have V4R2, you can use a varying-length temporary variable, as in Figure 10. It’s a bit prettier, and it might perform a tiny bit better if there aren’t many blanks to the left of the right-justified data.
The EVAL sets temp with the value “123” and a length of 3. The MOVE right justifies “123” in var2 and pads on the left with blanks.
—Barbara Morris IBM Toronto RPG Compiler Development
Record Locking with READE/READPE
Recently, we had a record lock error pop up randomly, and the only thing we could figure was that it was on a READE or READPE statement. After referencing the ILE RPG/400 Reference manual and performing some serious debugging, we discovered what the problem was.
If the file you are performing the READE or READPE on is defined as update, a temporary lock is placed on the next record and compared to the search argument in factor
1. This happens even if the next record does not meet the search criteria. If the record is found not to match the key value, the record-not-found indicator is turned on and the lock is removed.
A simple example shows how this could cause problems. Suppose you have a file that is used to receive items into the warehouse. These items are spread out among different users so that everyone with an RF terminal can share the work. The key to the file is user ID. A user selects the menu option, and the program begins processing READEs on the file, using the current user ID as the search argument. Figure 11 shows a simple example of what this data may look like in the file.
Users Brad Stone (BVSTONE), Chad Wells (CHWELLS), and Rod Larson (RJLARSON) receive items. Suppose Brad Stone begins receiving items a few minutes before Chad. By the time Chad gets to work, Brad is almost done. He is entering in the amount received on his last item (LL-009). Chad begins to receive items and has just read his first record. Before Chad enters the amount received for his first item (BT-873), Brad
completes his last transaction and presses Enter. He gets an error saying that the requested record is in use.
In this case, Chad has a record lock on his first item, and Brad’s job tried to check that record to see if the key matched the search criteria (in this case, his user ID, BVSTONE). Before the program can check the value, it must obtain a temporary lock, but it couldn’t because of Chad’s record lock. So the program crashed.
An easy solution to this problem is to put an error indicator in the “Lo” resulting indicator of the READE statement, as in Figure 12. I use the same indicator that I use to detect EOF so that, if this problem arises, the program treats the error the same as EOF. Another solution is to use the No-Lock option on the READE operation. When you are ready to update, simply chain to the file using the unique key, and process the update. This technique eliminates the need for the temporary lock that the system will try to place on the next record.
—Bradley V. Stone Technical Editor Midrange Computing
Don’t Compare to Floating Point Variables
We midrange programmers are spoiled. We’ve been using decimal numbers, and everything has always come out nice and precise. Beware of floating point numbers. You can almost bet they won’t be exact. A variable you expect to have the value 5 may be something like 4.99998 or 5.001338 instead.
Convert floating point values to decimal values before comparison. An easy way to convert is with the %DECH (Convert to Packed Decimal Format with Half Adjust) builtin function, as in Figure 13. %DECH requires three arguments: the numeric value to be converted to decimal, the number of digits of the resulting decimal value, and the number of decimal positions in the decimal value.
— Ted Holt Senior Technical Editor Midrange Computing
A R SCREEN
A CSRLOC(ROW COLUMN)
A ROW 3S 0H
A COLUMN 3S 0H
A A 1 B 5 6
A B 1 B 6 6
A C 1 B 7 6
Falber1df cf e workstn infds(WSFeedback)
D WSFeedback ds
D CursorLoc 370 371b 0
C dou a = ‘*’
C exsr pos
C exfmt screen
C enddo
C move *on *inlr
C*****
C pos begsr
C*
C CursorLoc div 256 Row
C mvr Column
C*
C endsr
Figure 1: Making the cursor return to its position on the previous display is easy
C MOVEA’001’ *IN,01
Figure 2: Setting indicators 01, 02, and 03 in RPG III
D Indicators DS BASED(IndPtr)
D iAllowF1 1 1A
D iAllowF2 2 2A
D iAllowF3 3 3A
D iOptionalKeys 1 3A
D IndPtr S * INZ(%ADDR(*IN))
C EVAL iOptionalKeys = *ALL’1’
C EVAL iOptionalKeys = *ALL’0’
C EVAL iOptionalKeys = ‘110’
Figure 3: Setting indicators 01, 02, and 03 in RPG IV
D Indicators S 99 BASED(IndPtr)
D IndPtr S * INZ(%ADDR(*IN))
C EVAL Indicators = *ALL’1’
C EVAL %subst(Indicators:11:9) = ‘111000010’
Figure 4: Accessing all general indicators through EVAL in RPG IV
A DSPSIZ(24 80 *DS3)
A INVITE
A R SCRN01
A 5 5’ENTER A LETTER:’
A LETTER 1 B + 1
Figure 5: The INVITE keyword is necessary for interactive programs that check for timeout
FTIMOUTDFCF E WORKSTN
F KNUM 1
F KINFDS INFDS
IINFDS DS
I *STATUS STATUS
C LETTER DOUEQ*BLANK
C MOVE *BLANK LETTER
C WRITESCRN01
C READ TIMOUTDF 2021
C *IN20 IFEQ *ON
C STATUS IFEQ 1331
C MOVE *BLANK LETTER
C ENDIF
C ENDIF
C ENDDO
C MOVE *ON *INLR
Figure 6: This RPG program allows a display to timeout
FPRINT O E 01 PRINTER KINFDS PINFDS
Figure 7: The traditional method for testing for printer overflow
IPINFDS DS
I B 188 1890CURLIN
I B 367 368OOFLINE
Figure 8: A printer file information data structure defined in RPG III
D var1 s 4
D var2 s 4
C eval var2 = *blanks
C eval % subst(VAR2: % len(VAR2)+1-%len(% trimr(VAR1)))
C = var1
Figure 9: One way to right justify a character string
D var1 s 4
D var2 s 4
D temp s 100a varying
C eval temp = % trimr(var1)
C move (p) temp var2
Figure 10: Another way to right justify a character string
User Item Quantity Quantity ordered received BVSTONE AB-109 10 10 BVSTONE KT-449 4 2 BVSTONE LL-009 19 18 CHWELLS BT-873 5
CHWELLS BU-909 1
RJLARSON FY-310 2
RJLARSON GR-250 25
Figure 11: Example data keyed on user ID
C** HiLoEq
C READEFMT01 5151
Figure 12: An error indicator prevents record lock
D floatvar s 8f
... assume floatvar = 1.0000011
... this test will prove false
C if floatvar = 1.0
... this test will prove true
C if %dech(floatvar:6:2) = 1.0
Figure 13: Compare decimal values instead of floating point values
LATEST COMMENTS
MC Press Online