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.

 

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$