/********************************************************************/
/*                                                                  */
/*  Program name . . . :   UT0010C                                  */
/*  Text . . . . . . . :   Message handling program for all         */
/*                         interactive jobs.                        */
/*                                                                  */
/*  Author . . . . . . :   Alexander Nubla                          */
/*  Description. . . . :   This program prepares and send all the   */
/*                         message back to the message queue passed */
/*                         in.  The message can be an impromptu msg */
/*                         or messages predefined in a file.        */
/*                         The substitution data is specified in the*/
/*                         message data (&MSGDTA) parameter. &MSGDTA*/
/*                         is a character string (200 bytes long    */
/*                         for this program) containing one or more */
/*                         concatenated message data fields.        */
/*                         The SNDPGMMSG command allows a message   */
/*                         of up to 512 characters to be sent.      */
/*                                                                  */
/*  Parameter Usage. . :                                            */
/*                                                                  */
/*       &CONTROL          Controls to determine whether message Id */
/*                         is used or an impromptu message.         */
/*                         I:  Message Id                           */
/*                         M:  Impromptu Message                    */
/*                         C:  Clear all messages                   */
/*                         S:  Status Message                       */
/*                                                                  */
/*       &AMSGID           Message id to be used                    */
/*                                                                  */
/*       &AMSGDTA          A group of concatenated fields put       */
/*                         together by the HLL program.             */
/*                                                                  */
/*       &MESSAGE          Impromptu message.  This message does    */
/*                         not contain second level message.        */
/*                                                                  */
/*       &MSGFILE          Message file used by the message id.     */
/*                                                                  */
/*       &PGMQ             This is the program message queue where  */
/*                         the messages are sent. It is usually the */
/*                         name of the calling program.             */
/*                                                                  */
/********************************************************************/
             pgm        parm(&Control  /* How to control msg    */    +
                             &Amsgid   /* Message Id to use     */    +
                             &Amsgdta  /* Message data to use   */    +
                             &Message  /* Impromptu message     */    +
                             &msgfile  /* Message file to use   */    +
                             &Pgmq)    /* Program message queue */
/*--------------------------------------------------------*/
/*  DECLARATION                                           */
/*--------------------------------------------------------*/
             dcl        &Control   *char    1
             dcl        &Amsgid    *char    7
             dcl        &Amsgdta   *char  200
             dcl        &Message   *char   80
             dcl        &Msgfile   *char   10
             dcl        &Pgmq      *char   10

             dcl        &x21       *char    1    value(x'21')

             dcl        &error     *lgl                   /* std err */
             dcl        &msgid     *char    7             /* std err */
             dcl        &msgkey    *char    4             /* std err */
             dcl        &msgdta    *char  100             /* std err */
             dcl        &msgf      *char   10             /* std err */
             dcl        &msgflib   *char   10             /* std err */
             dcl        &msgtyp    *char   10  '*DIAG'    /* std err */
             dcl        &msgtypctr *char    4 X'00000001' /* std err */
             dcl        &pgmmsgq   *char   10  '*'        /* std err */
             dcl        &stkctr    *char    4 X'00000001' /* std err */
             dcl        &errbytes  *char    4 X'00000000' /* std err */

             monmsg     msgid(cpf0000) exec(goto error)
/*--------------------------------------------------------*/
/*  Send message using Id                                 */
/*--------------------------------------------------------*/
             If         (&Control *ne ' '   *And +
                         &Control *eq 'I')  then(do)
               sndpgmmsg  msgid(&amsgid)      +
                          msgf(&msgfile)      +
                          msgdta(&amsgdta)    +
                          topgmq(*same &pgmq)
               goto       theend
             Enddo

/*--------------------------------------------------------*/
/*  Send message using Impromptu message                  */
/*--------------------------------------------------------*/
             If         (&Control *ne ' '   *And +
                         &Control *eq 'M')  then(do)
               sndpgmmsg  msg(&message)       +
                          topgmq(*same &pgmq)
               goto       theend
             Enddo

/*--------------------------------------------------------*/
/*  Send status message                                   */
/*--------------------------------------------------------*/
             If         (&Control *ne ' '   *And +
                         &Control *eq 'S')  then(do)
               If         (&Amsgid  *ne ' ')  then(do)
                 rtvmsg     msgid(&amsgid)      +
                            msgf(&msgfile)      +
                            msg(&message)
               Enddo
               chgvar     &message   (&x21 *bcat &message)
               sndpgmmsg  msgid(cpf9898)      +
                          msgf(qcpfmsg)       +
                          msgdta(&message)    +
                          msgtype(*status)    +
                          topgmq(*same &pgmq)
               goto       theend
             Enddo

/*--------------------------------------------------------*/
/*  Clear all messages                                    */
/*--------------------------------------------------------*/
             If         (&Control *ne ' '   *And +
                         &Control *eq 'C')  then(do)
               rmvmsg     pgmq(*same &pgmq)   +
                          clear(*all)
             Enddo


 theend:     return

/*--------------------------------------------------------*/
/*  error routine:                                        */
/*--------------------------------------------------------*/
 error:
             if         &error     (goto errordone)
               else      chgvar       &error  '1'
          /*----------------------------------------------*/
          /*  move all *DIAG message to *PRV program queue*/
          /*----------------------------------------------*/
             call       QMHMOVPM   (&msgkey      +
                                    &msgtyp      +
                                    &msgtypctr   +
                                    &pgmmsgq     +
                                    &stkctr      +
                                    &errbytes)
          /*----------------------------------------------*/
          /*  resend the last *ESCAPE message             */
          /*----------------------------------------------*/
 errordone:
             call       QMHRSNEM   (&msgkey      +
                                    &errbytes)
             monmsg     cpf0000    exec(do)
               sndpgmmsg  msgid(cpf3cf2) msgf(QCFPMSG) +
                            msgdta('QMHRSNEM') msgtype(*escape)
               monmsg     cpf0000
             enddo
 end:        endpgm
