22
Sun, Dec
3 New Articles

Scan and Replace Source Code

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

OS/400 includes several tools that can find a string of text but only one that can replace one string of text with another. Moreover, that tool, SEU’s Change command, works only interactively and not in batch. This article presents a utility that can not only scan a member or group of members for a search string but can also replace it with another string. Although the shop that produced this tool did not design it for Y2K conversion, the tool has nonetheless proven useful to that end.

My firm awoke to the Y2K problem in 1996 when we received a call from a client with an accounts receivable due date after 1999. That single call swelled to a slow trickle of calls by midyear, and although it would have been simple enough to swap the one six-digit date in favor of a Y2K-compliant field, we figured it was time to face the music for all of our applications.

After a brief evaluation process, we decided to replace all our six-digit dates with eight-digit dates and all two-digit years with four digits. The date data type looked attractive, and had we more time to explore its capabilities, we might have employed it. At the time, however, we had a mile-long backlog of change requests that were more interesting and more important to our clients than Y2K conversion. Face it: Building industry intelligence into applications wins clients; Y2K compliance just prevents them from going to the competition.

Identifying Dates

With the format decision made, we needed a change strategy that would make our code ready for distribution before the trickle of date complaints swelled to a stream. Working in our favor was the fact that shop standards required all date fields in files to contain either the suffix “DAT” or the suffix “DT,” save a small minority of date-of-birth fields ending in “DOB.” We also knew that year fields—abundant in our application—would almost uniformly have the suffix “YER,” with only a few deviants ending in “YR,” so the fields in question would at least be easily identifiable. In addition, date validations were all performed through a single called program, so accommodating Y2K date validation would be painless.

We also had going for us the fact that we require date conversions to be made through that old multiplication trick (MMDDYY * 10000.01 = YYMMDD; YYMMDD *
100.0001 = MMDDYY). This conversion mechanism has a lot of well-respected detractors but cannot be faulted for succinctness. Finally, DDS for display formats (except subfiles) are required to place field prompts at positions 2 and 40 with data entry fields at 21 and 59, which meant that tacking two extra digits onto a date would rarely interfere with other fields. Let’s hear it for shop standards.

Still, identifying dates wasn’t even half the problem. Even with a limited number of patterns to locate in date field names, changing command, DDS, RPG, and CL source code was still going to be quite a chore. PDM could help us locate fields, edit codes, and conversions that needed to be modified, but it could not make the changes for us. The application we supported topped out at over 250,000 lines of code, which is modest by some standards. Nevertheless, for a small shop with only two programmers to do the coding and testing, it was looking like a three-month job. Tack on testing time, and, suddenly, two warm bodies would be out of action for a good part of the year.

This was not good news, especially since we viewed this as a maintenance item, meaning we had already been paid to do it, on top of all our other routine maintenance work. To make matters worse, it was not even interesting programming. Y2K conversions are a matter of going into program after program, making the same changes (and some new mistakes), getting out, compiling, testing, fixing, and moving on to the next program. Yawn. Come to think of it, Y2K changes are exactly the sort of dull, repetitive work from which computers are supposed to save us. There are utilities out there that do it, but they take a substantial investment of money to buy and time to learn to use, and their value evaporates once the modified code is put into production.

Another Use for a Handy Tool

Fortunately, we had in our shop a homemade utility we had been using for several years, which allowed us not only to locate strings in source code but also to replace them. Of all the programming aids we had developed or downloaded over the years, this was the only one that saw day-to-day use. Scan and Replace, or SCNRPL as we called it, could find and replace strings in command, DDS, CL, or RPG source members and had a parameter that allowed us to automatically recompile any changed source member.

But could it be used to simplify a Y2K project? Since file, display, and printer DDS source was kept in separate source files in our shop, I quickly scanned through the data file DDS in all application libraries for six-digit, zero-decimal numeric fields. They were all dates. I scanned the DDS again, this time replacing all occurrences of “6S 0” with “8S 0” and recompiling, and then recompiled dependent physical file DDS (those that used the REFFLD keyword with fields in the changed files) and logical files.

Display and printer files would be just as easy: There were only a few six-digit fields because the majority of dates had been defined in reference to database fields. Output dates would use an edit word of “ / / ”, never an edit code. This 6-byte date edit could easily be replaced with an 8-byte edit word, but, to make sure the newly changed field references took, I recompiled all source with PDM rather than just the source that had been changed through SCNRPL. Whew. This Y2K thing was rough.

The next day, I tackled RPG. I searched for all internally defined date fields (e.g., six-digit, zero-decimal numeric fields with names ending in “DT” or “DAT”) and replaced them with eight-digit fields. A similar operation took care of all two-digit year fields (those with two digits and zero decimals and ending in “YER” or “YR”). Since the only reason the numbers 10000.01 and 100.0001 would occur in RPG source would be for date conversion, it was easy to scan and substitute 10000.0001 for either. Also, “UDATE” would have to be replaced where it occurred with “*DATE.” Doing this in all application libraries would take a while, so I coded the various scans into a CL program, set them running, and went home. (Come to think of it, a good chunk of our Y2K conversion took

place after hours while I was puttering in the garden.) Again, because more programs would be affected than just those to which changes had been made, I chose not to use the automatic recompile option, preferring instead to recompile everything through PDM after the fact. Fewer than 10 percent of the programs failed to compile. Since it was one of only a few in the entire application that dissected a date in a data structure, I changed the validation subprogram by hand and added a feature that would default the century to the current century if none were entered.

Commands were tougher because the date parameters there were defined as 6-byte character fields, as were a good number of non-date parameters. They still adhered to the same date naming conventions used in RPG, so it was relatively simple to replace everything in the declaration (DCL) from the suffix through the length (LEN). “MMDDYY” prompts were infrequent and replaced with “MMDDYYYY” where necessary. SCNRPL does not compile commands, but since all of our commands ran CL programs named CommandName + “CL,” batch compilation through other means was a breeze. All our CL programs used CVTDAT for conversion purposes, so it was a relatively small matter to have SCNRPL replace “*YMD” with “*YYMD” and “*MDY” with “*MDYY.”

In fairness, there were quite a few year fields in the display and print file DDS that had fields following them that did not use relative positioning and so had to be changed by hand. There were also a few unexpected “DTE” suffixes for date fields in the RPG, but these were quick fixes, and the week that we spent in testing showed so few problems that we shelved it and went into distribution. Overall, we spent two weeks converting and testing 250,000 lines of code.

Building SCNRPL

The SCNRPL utility consists of a command, printer file, and three ILE programs. These three programs are built from five modules. (The source members used to create these objects are listed in Figure 1.)

SCNRPL has six parameters, which are listed in Figure 2. You may scan a single member of a source file, all members, or only those members whose names begin with a certain group of characters. SCNRPL ignores trailing blanks in the search and replacement strings.

The Allow data shift (ALWDTASHFT) parameter tells SCNRPL how to behave when the string for which you are searching is not of the same length as the replacement string. You may specify a *YES or *NO value if you wish, but you may prefer the default, *SRCTYPE, which means that the source type determines whether or not data is to be shifted. For commands, CL members, and text, data is shifted; for RPG and DDS, it is not. Consequently, replacing a file name with a longer file name does not mess up an RPG F spec by shifting the position entries to the right but correctly adjusts the closing parenthesis of the FILE parameter of a RGZPFM command in keyword format.

The recompile option calls the SCNRPLCM program for each changed member. I included only the types of members we wanted to recompile. You may want to add others.

Source members used to create this utility are available for downloading from the Midrange Computing Web site at www. midrangecomputing.com/mc/99/04. Compile them in the order shown using the compilation instructions given.

Our Happy Ending

Because we tested our replacement runs on a single member before running them against all the source files in our libraries, source errors from replacement were nonexistent. Buoyant from our success with automated programming, we wrote another utility that would generate RPG conversion source (since CPYF wouldn’t do) and ran up conversion programs for each of our physical files. Our Y2K crisis was over, and the money we saved in the process will more than pay for the generators we’re buying to get us through next New Year’s Day.

Source member Type Program Function

SCNRPL CMD Command interface for SCNRPL SCNRPLCG RPGLE SCNRPLCL Checks for a generic name SCNRPLCL CLLE SCNRPLCL Command-processing program SCNRPLCM CLLE SCNRPLCM Compiles source members SCNRPLRG RPGLE SCNRPLRG Revises source code SCNRPLMT CLLE SCNRPLRG Determines type of a source member SCNRPLPF PRTF Prints a report listing occurences of search string

Figure 1: The SCNRPL utility consists of three programs, a printer file, and a command interface.

Parameter Description

SRCFIL A qualified source physical file name MBRNAM A member name, a generic name, or *ALL SCNOBJ The string for which to search
RPLOBJ The replacement string
ALWDTASHFT Whether or not to shift source code when the search and replacement strings are not of the same length.

RECOMP Whether or not to recompile changed source members.

Figure 2: SCNRPL requires six parameters to do its magic.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 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:

  • SB Profound WP 5539More 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.

  • SB HelpSystems ROBOT Generic 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:

  • Magic MarkTRY 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.

  • SB HelpSystems ROBOT GenericForms 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.

  • SB HelpSystems ROBOT GenericIT 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.

  • SB HelpSystems ROBOT GenericCan 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:

  • FORTRA 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:

  • FORTRAManaging 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:

  • FORTRAThe 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:

  • FORTRAFor 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:

  • LANSA 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.

  • LANSAWhen 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:

  • LANSASupply 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

  • Profound Logic 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.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra 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: