26
Fri, Apr
1 New Articles

TechTip: User Space Tricks That You Might Have Never Thought Of

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

Learn some seldom-known yet practical techniques to utilize User Space Objects.

 

Before I read the post Re: *Usrspc question by Simon Coulter in 2001 in the Midrange-l mailing list, I had never known that the Display File (DSPF) command can be used to display or change the contents of a User Space (*USRSPC) object. I realized that as a special type of space object designed to be used by user programs, *USRSPC objects are treated slightly differently from other MI space objects by some OS-level utilities. In this article, I'll introduce several tips about utilizing *USRSPC objects that will probably ease your daily work quite a lot. For detailed discussion about how to use *USRSPC objects via CL commands, APIs, or MI instructions, please refer to the following resources:

 

Work with *USRSPC Objects via the DSPF and EDTF Commands

The Display File (DSPF) and Edit File (EDTF) commands allow users to display or change the content of either a stream file or a member of a database file. (Note that the EDTF command works only with source physical files or data physical files that are not externally described.) As mentioned, they can also be used to display or change the content of a *USRSPC object. When data stored in a *USRSPC object is in text format, it's pretty handy to be able to display or change the content of the *USRSPC via the DSPF and EDFT commands.

 

To edit the character content of a *USRSPC object, you can issue an EDTF command against the *USRSPC object and set the Stream file (STMF) parameter to the IFS path name of the *USRSPCfor example, EDTF STMF('/qsys.lib/lsbin.lib/spc0f.usrspc'). EDTF is an editor that is similar to the Source Entry Utility (SEU). It provides similar edit commands (line commands), such as I (Insert a line), MM (Block move), and An (After with repeat n times), and control commands, such as F (or FIND), C (or CHANGE), etc.

 

To browse the data content of a *USRSPC object, you can issue a DSPF command and specify the IFS path name of the *USRSPC object as the STMF parameter of the DSPF commandfor example, DSPF STMF('/qsys.lib/lsbin.lib/spc0f.usrspc'). DSPF supports control commands similar to those supported by EDTF. In addition to displaying the data content in character form, DSPF can also display the data content in hexadecimal representation. You can switch to or back from hexadecimal display by pressing function key F10.

 

Note that if the data content of a *USRSPC object contains newline characters (*CR, *LF, *CRLF, *LFCR), DSPF or EDTF separates lines according the newline characters in the data content. If the data content of a *USRSPC object does not contain any newline characters, DSPF and EDTF will adopt an arbitrary record length of 80 and 75, respectively; in other words, DSPF/EDTF displays each 80-character/75-character as one line.

Work with *USRSPC Object from Within a Shell Environment

A *USRSPC object can be used as the source of an input redirection or the target of an output redirection. For example, the following shell command issued in a QShell or PASE shell session will redirect both the standard output and the standard error generated by the list of shell commands to *USRSPC object QGPL/SPC0A.

 

> ( echo STDOUT; ls   no-such-thing ) > /qsys.lib/qgpl.lib/spc0a.usrspc 2>&1

$

 

Note that the output redirection will create the target *USRSPC object if it doesn't already exist or cause the existing *USRSPC object to be extended or truncated to proper size. Also, the appending redirection operator >> cannot be used on a *USRSPC object.

 

The sort command in the following example redirects the standard input to *USRSPC object QGPL/SPC0A and sorts the names stored in QGPL/SPC0A.

 

> cat >   /qsys.lib/qgpl.lib/spc0a.usrspc   << eof

   >

> Jack

   >

> Anny

   >

> David

   >

> eof

   $

> sort -k 1 < /qsys.lib/qgpl.lib/spc0a.usrspc

   Anny

   David

   Jack

   $

 

Note that QShell and PASE shell use different character encoding (EBCDIC and PC character encoding, respectively), so you should not use the data content stored in a *USRSPC object by QShell as the input of a PASE shell command or program, and vice versa.

 

Besides input/output redirection, a *USRSPC object can also be used as the source or destination argument of the file operation command cp. For example, the following PASE shell commands copy PASE program echo into the associated space of *USRSPC QGPL/SPC0B and then copy the content of QGPL/SPC0B to a new PASE program shout.

 

> cp   /QOpenSys/QIBM/ProdData/OS400/PASE/bin/echo \

   >

> /qsys.lib/lsbin.lib/spc14.usrspc

   $

> ./shout "I'm back :p"

   I'm back :p

   $

 

Exchange Information with Shell Programs via *USRSPC Objects in a Native HLL Program

The QShell utilities and the PASE shell utilities are necessary for managing Integrated File System (IFS) objects. They are also helpful for developers and operators to work with /QSYS.LIB objects from the CL environment or programs written in native High Level Languages (HLL)for example, you can use a QShell (or PASE shell) <I>find command to look up source members by name, file type, last access time, etc. However, for IBM i HLL programs, it's still inconvenient to exchange information with shell commands or programsfor example, receiving the output of a shell command. One possible solution is to store information to be shared with shell commands or programs in *USRSPC objects.

 

Imagine that you are expected to collect all source members with a specific library that have been modified within a week and write the source member names to a database file or a spooled file. To achieve your goal, you may follow these steps:

  1. Run a QShell find command to list the source members that satisfy the search criteria (being modified within a week), and redirect the output of your find command to a *USRSPC object (say, MYGPL/ONEWEEK). The QShell find command might look like this: find /qsys.lib/mysrclib.lib -type f -ctime -7 > /qsys.lib/mygpl.lib/oneweek.usrspc.
  2. Read the search result stored in MYGPL/ONEWEEK in an RPG program, parse the character lines, and write them to a database file or a spooled file.

 

The following example CL program spc01.clp and RPG program spc02.rpgle demonstrate the above shown steps.

 

SPC01

             /** @file spc01.clp                                   */

             /** Calls: SPC02                                     */

            PGM         PARM(&SRCLIB &GPL)

             DCL       VAR(&SRCLIB) TYPE(*CHAR) LEN(10)

             DCL       VAR(&GPL) TYPE(*CHAR) LEN(10)

             DCL       VAR(&FINDCMD) TYPE(*CHAR) LEN(80)

             DCL       VAR(&SPCNAM) TYPE(*CHAR) LEN(20)

             CHGVAR     VAR(&FINDCMD) VALUE('find   /qsys.lib/' *CAT +

                         &SRCLIB *TCAT   '.lib -type f -ctime -7 > +

                         /qsys.lib/' *CAT   &GPL *TCAT +

                           '.lib/oneweek.usrspc')

           STRQSH       CMD(&FINDCMD) /* Run QShell find command */

             CHGVAR     VAR(&SPCNAM) VALUE('ONEWEEK   ' *CAT &GPL)

             CALL       PGM(SPC02) PARM(&SPCNAM) /* Parse   the output +

                         of previous issued   QShell find command */

             DSPSPLF   FILE(QSYSPRT) JOB(*) SPLNBR(*LAST) /*   Show +

                         search result */

 

SPC02

     h dftactgrp(*no)

     fQSYSPRT   o     f 132       printer

     * SYSBIF memchr()

     /copy mih-comp

 

     d qual_name_t    ds                 qualified

     d     obj                         10a

     d     lib                         10a

 

     * My prototype

     d i_main         pr                 extpgm('SPC02')

     d     spc_name                           likeds(qual_name_t)

 

     * Materialize space size

     d matspsz         pr           10i 0

 

     * Prototype of the Retrieve Pointer to   User Space API

     d qusptrus       pr                 extpgm('QUSPTRUS')

     d     spc_name                           likeds(qual_name_t)

     d     @spp                         *

 

     d @spp           s               *

     d c               s         32767a     based(@spp)

     d spsz           s             10i 0

     d disp           s             10i 0

     d ln             s           132a

     * Newline character

     d NL             c                   x'25'

     d @pos           s               *

 

     d i_main         pi

     d     spc_name                           likeds(qual_name_t)

 

     /free

           // Retrieve a space pointer   addressing the *USRSPC's

           // associated space

           qusptrus(spc_name : @spp);

           spsz = matspsz();           // Retrieve *USRSPC size

           @pos = memchr(@spp : NL :   spsz);   // Search for NL

           dow @pos <> *null;

               disp = @pos - @spp;

               spsz -= disp + 1;

 

               if disp < 132;

                   ln = %subst(c:1:disp);

               else;

                   ln = c;

               endif;

               except OREC;

 

               // Offset @spp and search for next NL character

               @spp = @pos + 1;

               @pos = memchr(@spp : NL :   spsz);

           enddo;

 

           *inlr = *on;

     /end-free

 

     oQSYSPRT   e           OREC

     o                      ln

 

     p matspsz         b

     * Space attribute in format SPCA0100   returned by QUSRUSAT

     d spca0100_t     ds                 qualified

     d     bytes_returned...

     d                               10i 0

     d     bytes_available...

    d                               10i 0

     d     spc_size                   10i   0

     d     auto_extend                 1a

     d     init_value                   1a

     d     lib_name                   10a

     * Prototype of the Retrieve User Space   Attributes API

     d qusrusat       pr                 extpgm('QUSRUSAT')

     d     attr                               likeds(spca0100_t)

     d     rcv_len                     10i   0

     d     fmt_name                     8a

     d     spc_name                          likeds(qual_name_t)

     d     ec                           8a     options(*varsize)

 

     d attr           ds                 likeds(spca0100_t)

     d rcv_len         s             10i 0 inz(%size(attr))

     d fmt_name       s             8a   inz('SPCA0100')

     d ec             s             8a     inz(*allx'00')

     d matspsz         pi           10i 0

     /free

           qusrusat(attr : rcv_len : fmt_name   : spc_name : ec);

           return attr.spc_size;

     /end-free

     p                e

 

The Retrieve Pointer to User Space (QUSPTRUS) API is used to retrieve the space pointer addressing the associated space of the target *USRSPC object, and the Retrieve User Space Attributes (QUSRUSAT) API is used to materialize the space size (the size of the associated space) of the target *USRSPC object. The Find Character Constrained (MEMCHR) system built-in is prototyped in mih-comp.rpgleinc.

 

Call SPC01 and specify the proper source library and result *USRSPC library parameters. The result might look like the following:

 

                             Display Spooled   File

File   . . . . . :   QSYSPRT                         Page/Line   1/1

Control . . . . .                                     Columns     1 - 78

Find   . . . . . .

*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+...

/qsys.lib/SRC2013.lib/A.FILE/A310.MBR

/qsys.lib/SRC2013.lib/A.FILE/A311.MBR

/qsys.lib/SRC2013.lib/A.FILE/A312.MBR

/qsys.lib/SRC2013.lib/A.FILE/A313.MBR

/qsys.lib/SRC2013.lib/A.FILE/A314.MBR

/qsys.lib/SRC2013.lib/A.FILE/A316.MBR

/qsys.lib/SRC2013.lib/A.FILE/A317.MBR

/qsys.lib/SRC2013.lib/JAN.FILE/II301.MBR

 

One More Thing

For your convenience, I'd like to mention something off topic. Suppose all you care about from a submitted QShell command is the return code. You can retrieve the return code of a QShell command via the Start QSH (STRQSH) command. You can receive the completion (*COMP) message (with message ID QSH0005) sent to the call message queue via STRQSH as shown here:

 

             DCL       VAR(&RC) TYPE(*INT) LEN(4)

             STRQSH     CMD('rm ~/Oh-no-no-no-no-no')

             RCVMSG     MSGTYPE(*COMP) MSGDTA(&RC)

 

The QSH#### messages are defined in message file (*MSGF) QSHELL/QZSHMSGF.

 

Junlei Li

Junlei Li is a programmer from Tianjin, China, with 10 years of experience in software design and programming. Junlei Li began programming under i5/OS (formerly known as AS/400, iSeries) in late 2005. He is familiar with most programming languages available on i5/OS—from special-purpose languages such as OPM/ILE RPG to CL to general-purpose languages such as C, C++, Java; from strong-typed languages to script languages such as QShell and REXX. One of his favorite programming languages on i5/OS is machine interface (MI) instructions, through which one can discover some of the internal behaviors of i5/OS and some of the highlights of i5/OS in terms of operating system design.

 

Junlei Li's Web site is http://i5toolkit.sourceforge.net/, where his open-source project i5/OS Programmer's Toolkit (https://sourceforge.net/projects/i5toolkit/) is documented.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

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: