Search code examples
ibm-midrangerpgle

How to scan source of a RPGLE program?


I want to scan source from a RPGLE program but there is a problem. I explain problem below. Source is:

fTcsprefix if   e             disk    rename(tcsprefix:testpr)  
f                                     prefix (@)    

I want to scan the PREFIX keyword if it exists in the source code. But when scanning it is found in the record format name. How can I avoid this?

I am using:

**C   'PREFIX'     SCAN    SRCDTA     S1**

Solution

  • Try scanning for ' PREFIX', i.e., with a leading space. Unless you can guarantee that there is always a space after the PREFIX keyword, you can't also rely on a trailing space in the search-string. (You can try it.)


    For more extensive testing:

    If coding standards don't maintain clear syntax, you need to extend the logic. This statement finds where PREFIX is found:

     C   'PREFIX'     SCAN    SRCDTA     S1
    

    It stores the location in S1. You can then use S1 to test if the position before contains a blank or a ')'. If you find any other characters that are valid, the logic can be extended to test for added valid characters. Also, you know that PREFIX is six characters long, so you can test the character at position S1+6 to see if it is a blank or a '(' or other valid character.

    When your program finds PREFIX and it also finds that both the preceding and succeeding characters are acceptable, it should know that it found a match that fits.

    When SCAN finds a match, you probably should pass the source line to a sub-procedure that tests the preceding and succeeding characters and returns an indicator value. The indicator will tell your program if it's a good match or not.

    You might also consider not using SCAN at all. Use the %scan() BIF instead and use a free-form coding style.