From:	CSBVAX::MRGATE!RELAY-INFO-VAX@CRVAX.SRI.COM@SMTP 29-SEP-1988 12:44
To:	ARISIA::EVERHART
Subj:	Re: Mail from a detached process


Received: From KL.SRI.COM by CRVAX.SRI.COM with TCP; Thu, 29 SEP 88 07:28:45 PDT
Received: from CUNYVM.CUNY.EDU by KL.SRI.COM with TCP; Thu, 29 Sep 88 07:04:54 PDT
Received: from ISUMVS.BITNET by CUNYVM.CUNY.EDU (IBM VM SMTP R1.1) with BSMTP id 8712; Thu, 29 Sep 88 09:49:30 EDT
Date:      Thu, 29 Sep 88 08:51:43 CDT
To:        <INFO-VAX@KL.SRI.COM>
From:      "John Hascall" <GG.SPY%ISUMVS.BITNET@CUNYVM.CUNY.EDU>
Subject:   Re: Mail from a detached process

> Date:         Wed, 28 Sep 88 12:01:24 CDT
> Sender:       INFO-VAX Discussion <INFO-VAX@UBVM>
> From:         Lee Cox - AVS System Operations <SYSVAX@VENUS.TAMU.EDU>
> Subject:      Mail from a detached process
> To:           John Hascall <GA.JPH@ISUMVS>
>
> I have a FORTRAN program which I run as a detached process.  If it
> executes a certain set of instructions, I want it to mail me a message
> to let me know that something is going on.  Normally, I could use
> LIB$SPAWN, but LIB$SPAWN doesn't work from a detached process.  What
> would be the easiest way to have it send me mail?  Any ideas?
>
>                     Lee Cox
>                     Academic VAX Systems
>                     Texas A&M University
>                     SYSVAX@TAMVENUS.BITNET
>                     SYSVAX@VENUS.TAMU.EDU
>

   The following is an excerpt from a program (an editor) which mails
to a process, this should give you the gist of the method...

----------------------------------------------------------------------------
/* This file contains the code to mail a buffer to another user.
 * this code works only on VAX/VMS.  - John Hascall 03/02/87
 *
 * Method:  1) Open a link to the network mail task  'node'::"27="
 *          2) Send the header info:  From-username
 *                                    to-username
 *                                        .
 *                                    null-byte
 *                                    to-which-shows-up
 *                                    subject-line
 *          3) Send the buffer:       text-line
 *                                        .
 *                                        :
 *                                    null-byte
 *          4) Close the link
 */
#include        <ssdef.h>
#include        <stdio.h>
#include        <ctype.h>
#include        <syidef.h>
#include        <jpidef.h>
#include        <lnmdef.h>
#include        <descrip.h>
#include        <file.h>
#include        "estruct.h"
#include        "edef.h"
#define RETRY   5                       /* number of times to try network */

vmail(f,n)
{
        register LINE   *lp;
        register int    i;
        register int    fd;
        int     s;
        int     u0;
        int     n0;
        int     nlines;
        int     ncode;
        char    nullbyte[4];
        char    to[32];
        char    link[24];
        char    subject[81];
        char    mailstat[81];
        $DESCRIPTOR(node,"0123456789ABCDE");
        $DESCRIPTOR(from,"0123456789ABCDE");
        $DESCRIPTOR(lgtbl,"LNM$SYSTEM");
        $DESCRIPTOR(lgnam,"SYS$CLUSTER_NODE");
        struct {
                short   buflen;
                short   itmcode;
                int     *bufadr;
                int     *retlen;
                int     endlst;
        } lglst;

        if ((s=mlreply("Mail to: ", to, 32)) != TRUE)
                return(s);
        if ((s=mlreply("Subject: ", subject, 81)) != TRUE)
                return(s);
        n0 = strlen(to);
        u0 = 0;
        for ( i = 0; i < n0; i++ )
                if (islower(to[i]))
                        to[i] = toupper(to[i]);
        for ( i = 0; i < n0; i++ )
                if ((to[i] == ':') && (i != 0) && (to[i-1] == ':'))
                        if ((i+1) == n0) {
                                mlwrite("A username is required");
                                return(FALSE);
                        } else if (i == 1) {
                               mlwrite("A node/cluster name must precede '::'");
                                return(FALSE);
                        } else {
                                to[i-1] = '\0'; /* null terminate node  */
                                u0 = i + 1;     /* offset to username   */
                                n0 = 0;         /* offset to nodename   */
                                break;
                        }

        if (n0 == 0) {
                strcpy(link, to);
        } else {
                lglst.buflen = 23;              /* leave room for \0 */
                lglst.itmcode = LNM$_STRING;
                lglst.bufadr = &link[0];
                lglst.retlen = &i;              /* steal this var for a bit */
                lglst.endlst = 0;
                i = 0;
                if (SYS$TRNLNM(0, &lgtbl, &lgnam, 0, &lglst) == SS$_NORMAL) {
                        link[ (link[i-1] == ':') ? (i-2) : i ] = '\0';
                } else {
                        ncode = SYI$_NODENAME;
                        lib$getsyi(&ncode, 0, &node, &n0, 0, 0 );
                        node.dsc$a_pointer[n0] = '\0';
                        strcpy(link, node.dsc$a_pointer);
                }
        }
        ncode = JPI$_USERNAME;
        lib$getjpi(&ncode, 0, 0, 0, &from, &n0 );
        for (i = n0 - 1; i >= 0; --i)
                if (from.dsc$a_pointer[i] != ' ') {
                        from.dsc$a_pointer[i+1] = '\0';
                        break;
                }
        mlwrite("Establishing link to MAIL task at %s, please wait...", link);
        strcat(link, "::\"27=\"");
        for (i = 0; i <= RETRY; i++ ) {
                if ((fd=open(link, O_RDWR, 0666)) >= 0)
                        break;
                if (i == RETRY) {
                        link[strlen(link)-7] = '\0';
                        mlwrite("Unable to connect to MAIL task at %s",
                                 link);
                        return(FALSE);
                }
        }
        nullbyte[0] = '\0';
        write(fd,from.dsc$a_pointer,strlen(from.dsc$a_pointer));
        write(fd,&to[u0],strlen(&to[u0]));
        read(fd,mailstat,80);
        if (strlen(mailstat) > 1) {
                read(fd, mailstat, 80);
                mlwrite("%s",mailstat);
                close(fd);
                return(FALSE);
        }
        write(fd,nullbyte,1);
        write(fd,&to[u0],strlen(&to[u0]));
        write(fd,subject,strlen(subject));
        nlines = 0;
        for (lp = lforw(curbp->b_linep); lp != curbp->b_linep; lp = lforw(lp)) {
                if (llength(lp) > 0)
                        write(fd, &lp->l_text[0], llength(lp));
                else
                        write(fd, " ", 1);
                nlines++;
        }
        write(fd,nullbyte,1);
        read(fd,mailstat,80);
        while (strlen(mailstat) > 1) {
                read(fd, mailstat, 80);
                if (strlen(mailstat) > 1)
                        mlwrite("%s",mailstat);
        }
        close(fd);
        mlwrite("[%d Line%s mailed]", nlines, ((nlines==1)?"":"s"));
}

----------------------------------------
John Hascall
Iowa State Univ Computation Center



