One of the great things about
coding RPG programs in free-format is the pure fact that the rigid spacing
requirements of RPG are gone. This fact, however, can make reading a free-format
RPG program difficult, especially when your program contains multiple levels of
nested IF and/or DO conditions.
Wouldn't it be great to have a utility
that would take "dirty" free-format RPG code and clean it up? This TechTip will
add the Indent Free-Form ILE RPG (INDFREERPG) tool to your programming toolbox.
Before we look at the INDFREERPG utility, let's look at the problem it
will help you resolve. Figure 1 is a code snippet from a free-format RPG program
gone amuck.
/Free IF Parm1 = 'Y'; SELECT; WHEN Parm2 = 1; Exsr SubrA; WHEN Parm2 = 2; Exsr SubrA; WHEN Parm2 = 3; Exsr SubrB; WHEN Parm2 = 9; For X = 1 to 3; Parm2 = X; Exsr SubrB; EndFor; EndSl; EndIf; Begsr SubrA; Rtn = Parm2 * 3.1415927; EndSr; Begsr SubrB; Rtn = Parm2 / 3.1415927; EndSr; /END-FREE
Figure 1: This code snippet illustrates a "dirty" free-format
program.
A program coded like this, while completely acceptable to
the compiler, will be difficult to follow when modification or analysis is
required later. This program desperately needs a more structured layout. To
accomplish this, the INDFREERPG utility takes free-format RPG statements and
automatically indents the source statements.
This utility is made up of
the following three objects:
FDN001RG--ILE RPG program that
indents free-format RPG source
FDN001CL--CL driver program for FDN001RG
INDFREERPG--OS/400 command source for the INDFREERPG
command
FDN001RG (Figure 2) examines the specified source
physical file member and looks for certain opcodes to determine whether any line
should be indented further than the current source indentation.
*-------------------------------------------------------------- * Program :FDN001RG * * Description: Automatic indentation of Free-Format RPG source * * Create Command: CRTBNDRPG PGM(FDN001RG) SRCFILE(QRPGLESRC) * *---------------------------------------------------- H DFTACTGRP(*NO) FQRPGLESRC IF E K DISK RENAME(QRPGLESRC:SOURCE) FQRPGLEOUT O A F 112 DISK DFDN001RG PR D Ind 15 5 DFDN001RG PI D Indnt 15 5 D Ucase C 'ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ' D Lcase C 'abcdefghijklmnopqrstuvwxyzÆØÅ' D Indent S 5 0 D FreeOn S LIKE(*IN01) D Quot S LIKE(*IN01) D X S 3 0 D Y S 3 0 D Z S 3 0 D Seqn S 6 0 D SRCDTAAR S 110 DIM(32767) D SRCDATAR S 6 0 DIM(32767) D Line S 120 D Line2 S 120 D BlankSrch S 5 0 D SemiSrch S 5 0 D OpCode S 8 D TrimSrc S 120 D BlankLine S 120 INZ(*Blanks) D CheckFree S 50 D End1 S 5 0 D End2 S 5 0 D RPGDS E DS EXTNAME(QRPGLESRC) PREFIX('X':1) /FREE Indent = 8; Read QRPGLESRC; Dou %EOF; CheckFree = %XLATE(Lcase:Ucase:%Subst(SRCDTA:7:9)); If FreeOn = *Off; Seqn = Seqn + 1; SRCDATAR(Seqn) = SRCDAT; SRCDTAAR(Seqn) = SRCDTA; If CheckFree = '/FREE'; FreeOn = *On; EndIf; Read QRPGLESRC; Iter; Else; If CheckFree = '/END-FREE'; FreeOn = *Off; Seqn = Seqn + 1; SRCDATAR(Seqn) = SRCDAT; SRCDTAAR(Seqn) = SRCDTA; Else; TrimSrc = %Trim(SRCDTA); BlankSrch = %Scan(' ':TrimSrc:1); SemiSrch = %Scan(';':TrimSrc:1); If SemiSrch < BlankSrch AND SemiSrch <> 0; BlankSrch = SemiSrch; EndIf; OpCode = %XLATE(LCase:UCase:%Subst(TrimSrc:1:BlankSrch-1)); If OpCode = 'ENDIF' OR OpCode = 'ENDFOR' OR OpCode = 'ENDDO' OR OpCode = 'ENDSL' OR OpCode = 'ENDMON' OR OpCode = 'ENDSR' OR %SUBST(OpCode:1:4) = 'ELSE' OR %SUBST(OpCode:1:4) = 'WHEN' OR %SUBST(OpCode:1:5) = 'OTHER' OR OpCode = 'ON-ERROR'; Indent = Indent - Indnt; EndIf; Line = %SUBST(BlankLine: 1: Indent) + TrimSrc; IF %Scan('//': Line)=0 Or %Scan('//': Line)>80; DoW %Len(%Trim(Line))+Indent > 80; For X=1 To 79; Y = 80-X; If %SUBST(Line:Y:1) = ' '; End2 = (%Len(%trim(LINE)) + Indent) - (Y+1); Line2 = %SUBST(Line:Y:%Len(Line)-Y); For Z = 1 To Y; If %Subst(Line:Z:1) = ''''; Quot = (Quot = *Off); EndIf; EndFor; If Quot = *On; Line = %SUBST(BlankLine:1:Indent) + %SUBST(%Trim(Line):1:Y-Indent) + '+'; Else; Line = %SUBST(BlankLine:1:Indent) + %SUBST(%Trim(Line):1:Y-Indent); EndIf; Seqn = Seqn + 1; SRCDATAR(Seqn) = SRCDAT; SRCDTAAR(Seqn) = Line; Line = %SUBST(BlankLine:1:Indent) + %Trim(Line2); Leave; EndIf; EndFor; EndDo; EndIf; Seqn = Seqn + 1; SRCDATAR(Seqn) = SRCDAT; SRCDTAAR(Seqn) = Line; If OpCode = 'IF' OR OpCode = 'DOU' OR OpCode = 'DOW' Or OpCode='FOR' OR OpCode = 'DO' OR OpCode = 'SELECT' OR %SUBST(OpCode:1:4) = 'WHEN' OR %SUBST(OpCode:1:4) = 'ELSE' OR OpCode = 'OTHER' OR OpCode = 'ON-ERROR' OR OpCode = 'MONITOR' OR OpCode = 'BEGSR'; Indent = Indent + Indnt; EndIf; EndIf; EndIf; Read QRPGLESRC; EndDo; For X = 1 To Seqn; XRCSEQ = X; XRCDTA = SRCDTAAR(X); XRCDAT = SRCDATAR(X); Except WriteOut; EndFor; *INLR = *ON; Return; /END-FREE OQRPGLEOUT EADD WriteOut O XRCSEQ O XRCDAT O XRCDTA
Figure 2: This program indents a free-format ILE RPG
program.
This program reads through the specified source member until
it hits a source record containing the /FREE compiler directive. A flag is used
to identify that the source after the compile directive is free-format. At that
point, the program continues reading through the source member, identifying
grouped operations, and indenting the code inside of those operations.
The table below shows a list of these group operations.
Operations Used for Free-Format
Indentation
Operation
Indentation Action
BEGSR, DO, DOU, DOW, FOR, IF, MONITOR, SELECT
Indent the code following this statement until an ENDxx is reached.
*ELSE
Return this statement to the location of the IF and continue indenting
statements following this one.
ENDxx
Return this statement and any that follow it to the indentation of the
beginning statement for the group.
ON-ERROR
Return this statement to the location of the MONITOR statements and
continue indenting statements following this one.
OTHER
Return this statement to the location of the SELECT/WHEN statements and
continue indenting statements following this one.
*WHEN
Return this statement to the location of the SELECT statements and continue
indenting statements following this one.
If the line becomes too long (greater than 80 characters) as a
result of a line of code being indented, the program automatically breaks the
code into multiple lines of code that honor the new indentation. The resulting
statements are initially loaded into an array and the "new" program is output at
the end of the program. This array is currently limited to 32,767 statements.
Create this program using the CRTBNDRPG command:
CRTBNDRPG PGM(FDN001RG) SRCFILE(QRPGLESRC)
To control the execution of this program, the CL program FDN001CL
(Figure 3) is used.
CHKOBJ OBJ(QTEMP/QRPGLEOUT) OBJTYPE(*FILE) MONMSG MSGID(CPF9801) EXEC(DO) CRTSRCPF FILE(QTEMP/QRPGLEOUT) RCDLEN(112) ENDDO IF COND(&SRCFILE *EQ &OUTFILE *AND &SRCMBR *EQ + &OUTMBR *AND &SRCLIB *EQ &OUTLIB) THEN(DO) IF COND(&REPLACE *EQ 'N') THEN(DO) SNDPGMMSG MSG('Output member is the same as input + member and Replace option is "NO".') GOTO CMDLBL(PGMEND) ENDDO ENDDO
CHKOBJ OBJ(&SRCLIB/&SRCFILE) OBJTYPE(*FILE) + MBR(&SRCMBR) MONMSG MSGID(CPF9815 CPF9801 CPF9810) EXEC(DO) SNDPGMMSG MSG('Input Library/File or Member not found.') GOTO CMDLBL(PGMEND) ENDDO
IF COND(&OUTFILE = '*INFILE') THEN(CHGVAR + VAR(&OUTFILE) VALUE(&SRCFILE)) IF COND(&OUTLIB = '*INLIB') THEN(CHGVAR + VAR(&OUTLIB) VALUE(&SRCLIB)) IF COND(&OUTMBR = '*INMBR') THEN(CHGVAR + VAR(&OUTMBR) VALUE(&SRCMBR))
CHKOBJ OBJ(&OUTLIB/&OUTFILE) OBJTYPE(*FILE) MONMSG MSGID(CPF9801 CPF9810) EXEC(DO) SNDPGMMSG MSG('Output Library/File does not exist') GOTO CMDLBL(PGMEND) ENDDO
CHGVAR VAR(&EXISTS) VALUE('Y') IF COND(&REPLACE *NE 'Y') THEN(DO) SNDPGMMSG MSG('Output Member Exists but Replace Option + is "NO"') GOTO CMDLBL(PGMEND) ENDDO
OK: RTVMBRD FILE(&SRCLIB/&SRCFILE) MBR(&SRCMBR) + SRCTYPE(&SRCTYPE) TEXT(&SRCTEXT) IF COND(&SRCTYPE *NE 'RPGLE' *AND &SRCTYPE *NE + 'SQLRPGLE') THEN(DO) SNDPGMMSG MSG('Member type for input source member + must be RPGLE or SQLRPGLE.') GOTO CMDLBL(PGMEND) ENDDO
Figure 3: This CL controls the execution of FDN001RG.
Create this program using the CRTCLPGM command:
CRTCLPGM PGM(FDN001CL SRCFILE(QCLSRC)
The primary function of this program is to perform error checking
on the parameters supplied by the INDFREERPG command, to prepare the work files
used by the application, and finally to create the new source member from these
work files.
The source for the INDFREERPG command is shown in Figure
4.
Once this statement has been executed, the INDFREERPG command is
ready to go. This command accepts parameters that define the input source file
and member, the output source file and member, the number of characters to
indent at each group, and whether or not to replace an existing source member.
Figure 5 shows the prompt screen from this command.
Figure
5: This is the prompt screen for INDFREERPG. (Click image to enlarge.)
This screen illustrates the parameters for the INDFREERPG command.
The first set of parameters defines the input source member to be indented. The
next parameter defines the number of characters to indent each grouping. A value
between 1 and 4 is acceptable for this command. The next set of parameters
defines the output file. Optionally, a value of *INFILE, *INLIB, or *INMBR can
be used to identify that the output file/library and member are the same as the
input file/library and member. The final parameter defines whether or not an
existing member should be replaced when the command is executed. If *NO is
specified and the output member exists, an error is returned.
Assuming
that the free-format RPG shown in Figure 1 was contained in an RPG source member
named FRE001RG, the following command would be used to indent the source:
When this statement is executed, the source member is read in and
processed by the INDFREERPG command and saved into its original source file
member.
Figure 6 shows the source from Figure 1 after is has been
processed through INDFREERPG.
/Free IF Parm1 = 'Y'; SELECT; WHEN Parm2 = 1; Exsr SubrA; WHEN Parm2 = 2; Exsr SubrA; WHEN Parm2 = 3; Exsr SubrB; WHEN Parm2 = 9; For X = 1 to 3; Parm2 = X; Exsr SubrB; EndFor; EndSl; EndIf; Begsr SubrA; Rtn = Parm2 * 3.1415927; EndSr; Begsr SubrB; Rtn = Parm2 / 3.1415927; EndSr; /END-FREE
Figure 6: The free-format code has been "cleaned up."
As you
can see, this version of the code is easier to read and follow. The flow of the
program is easy to recognize at a glance. This command works best with
applications written using all free-format code.
INDFREERPG can really
help to keep your code "free" but under control.
Active Server Pages Primer Learn how to make the most of ASP while creating a fully functional ASP "shopping cart" application. List Price $79.00
Now On Sale
JavaScript for the Business Developer Learn how JavaScript can help you create dynamic business applications with Web browser interfaces. List Price $44.95
This book provides an amazingly comprehensive introduction to the concepts while at the same time delivering enough technical detail to make you productive very quickly.
Today, it's all about input and output. Getting data into the IBM i from non-traditional sources and then displaying it back out again in varied formats. But where can you go to learn all that you need to know about this critical skill?
Too valuable to be classified as merely excellent certification material, this book should also rightly take its place on DB2 DBA bookshelves as a solid day-to-day DB2 reference.
Whether you want to obtain an IBM certified DB2 professional certification or simply become well-rounded in the fundamental concepts of DB2 and general database theory, this is your book.
Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:
More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.
IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:
TRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now! Request Now.
Forms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.
IT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.
Can you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:
Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:
Managing messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:
The thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:
For over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:
Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.
When it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:
Supply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:
The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from.
>> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit
Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.
IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn:
LATEST COMMENTS
MC Press Online