home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
TIMEXSRC.ZIP
/
UA_BAIF.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-03-29
|
8KB
|
335 lines
/* ua_baif.c -- Generalized interface to background agent, for TIMEXUA.
February 1990 Mark E. Mallett, Personal Workstation Magazine
This file contains routines that map to the TIMEXUA requests. These
routines are:
ba_add Add an event
ba_del Delete an event
ba_get Get Event
ba_getnext Get Next Event
ba_modify Modify an event
*/
#define INCL_BASE
#include <stdio.h>
#include <string.h>
#include <os2.h>
#include "timex.h"
#include "ua_timex.h"
#include "evpack.h"
#include "ua_baif.h"
/* Local definitions */
/* External data referenced */
/* External routines used */
/* Local data publicly available */
/* Local routines and forward declarations */
static EVENT *ba_evgetname( int fcode, char *evnameP );
static int ba_req(void);
/* Private data */
static USHORT Rsplen; /* Response length */
static char Reqbuf[512]; /* Request buffer */
static char Rspbuf[512]; /* Response buffer */
/*
*//* ba_add( eventP )
Add a new event to the database
Accepts :
eventP Ptr to new event
Returns :
<value> TRUE if OK
FALSE if problem
*/
int
ba_add(
EVENT *eventP /* Ptr to the event */
) {
TMXMSG *reqP; /* Ptr to request block */
reqP = (TMXMSG *)&Reqbuf[0];
/* Format the request */
reqP->m_code = REQ_ADD;
reqP->m_len = sizeof( TMXMSG );
reqP->m_len += event_pack( eventP, (char *)&reqP[1],
512 - sizeof(TMXMSG) );
/* Send off the request... */
return( ba_req() );
}
/*
*//* ba_del( evnameP )
Delete an event in the database
Accepts :
evnameP Ptr to name of event to delete
Returns :
<value> TRUE if OK
FALSE if problem
*/
int
ba_del(
char *evnameP /* Ptr to name */
) {
TMXREQ_DEL *reqP; /* Ptr to request block */
reqP = (TMXREQ_DEL *)&Reqbuf[0];
/* Format the request */
reqP->d_msghdr.m_code = REQ_DEL;
strcpy( (char *)&reqP->d_evname[0], evnameP );
reqP->d_msghdr.m_len = sizeof( TMXREQ_DEL ) + strlen( evnameP );
/* Send off the request... */
return( ba_req() );
}
/*
*//* ba_get( evnameP )
Get an event by name
Accepts :
evnameP The name of the event
Returns :
< value > The EVENT retrieved,
NULL if error or not found
*/
EVENT *
ba_get(
char *evnameP /* Name of the event */
) {
/* Hand off to general event-getter */
return( ba_evgetname( REQ_GET, evnameP ) );
}
/*
*//* ba_getnext( evnameP )
Get next event by name
Accepts :
evnameP The name of the prior event
Returns :
< value > The EVENT retrieved,
NULL if error or not found
*/
EVENT *
ba_getnext(
char *evnameP /* Name of the event */
) {
/* Hand off to general event-getter */
return( ba_evgetname( REQ_GETNEXT, evnameP ) );
}
/*
*//* ba_modify( evnameP, eventP )
Modify an event in the database
Accepts :
evnameP Ptr to name of event to modify
eventP Ptr to new info for the event
Returns :
<value> TRUE if OK
FALSE if problem
*/
int
ba_modify(
char *evnameP, /* Ptr to name */
EVENT *eventP /* Ptr to the event */
) {
int len; /* Length */
char *bufP; /* Ptr to buffer */
TMXMSG *reqP; /* Ptr to request block */
reqP = (TMXMSG *)&Reqbuf[0];
/* Format the request */
reqP->m_code = REQ_MODIFY;
reqP->m_len = sizeof( TMXMSG );
bufP = (char *)&reqP[1];
strcpy( bufP, evnameP );
len = strlen( bufP ) +1;
reqP->m_len += len;
bufP += len;
len = event_pack( eventP, bufP, 512 - reqP->m_len );
reqP->m_len += len;
/* Send off the request... */
return( ba_req() );
}
/*
*//* ba_evgetname( fcode, evnameP )
Issue a name-based event-getting request
Accepts :
fcode Function code
evnameP Name of event
Returns :
< value > Ptr to EVENT struct if OK,
NULL if not.
*/
EVENT *
ba_evgetname(
int fcode, /* Function code */
char *evnameP /* Name for function */
) {
TMXREQ_GETNEXT *reqP; /* Request ptr */
TMXMSG *rspP; /* Response ptr */
/* Fill in the request block */
reqP = (TMXREQ_GETNEXT *)&Reqbuf[0];
reqP->g_msghdr.m_code = fcode;
reqP->g_msghdr.m_len = sizeof(TMXREQ_GETNEXT) + strlen( evnameP );
strcpy( &reqP->g_evname[0], evnameP );
/* Make the request */
if ( !ba_req() )
return( NULL );
/* If no event returned, return NULL */
rspP = (TMXMSG *)&Rspbuf[0];
if ( rspP->m_code != RSP_EVENT )
return( NULL );
/* Return the event */
return( event_unpack( (char *)&rspP[1], rspP->m_len - sizeof(TMXMSG) ) );
}
/*
*//* ba_req()
Make a request of the background agent.
Accepts :
Reqbuf Request data in proper format
Returns :
< value > TRUE if request made OK
FALSE if problem making request
Rspbuf Response data
*/
static int
ba_req( void ) {
int status; /* Status code */
USHORT action; /* Response from DosOpen */
HPIPE pipeH; /* Pipe handle */
TMXMSG *reqP; /* Request ptr */
TMXMSG *rspP; /* Response ptr */
TMXRSP_ERR *errP; /* Error block */
/* Open a connection to the background agent */
/* For some reason, OS/2 doesn't mediate pipe-busy's. Have to
do it by hand!!! */
for( ; ; ) {
status = DosOpen( REQPIPE, &pipeH, &action, 0L, 0,
FILE_OPEN,
OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
0L );
if ( status == 0 ) /* Opened? */
break;
if ( status != ERROR_PIPE_BUSY ) {
warning( EC_NOTOK, "Can't contact background agent." );
return( FALSE );
}
DosWaitNmPipe( REQPIPE, (ULONG)60000 );
}
/* Make the request */
reqP = (TMXMSG *)&Reqbuf[0];
status = DosTransactNmPipe( pipeH, &Reqbuf[0], reqP->m_len,
&Rspbuf[0], 512, &Rsplen );
DosClose( pipeH );
if ( status != 0 ) {
warning( EC_NOTOK, "Can't send to background agent." );
return( FALSE );
}
rspP = (TMXMSG *)&Rspbuf[0];
if ( rspP->m_code == RSP_ERROR ) {
errP = (TMXRSP_ERR *)rspP;
warning( EC_NOTOK, "Error %d: %s", errP->e_code, &errP->e_msg[0] );
return( FALSE );
}
return( TRUE );
}