home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / TIMEXSRC.ZIP / BA_MAIN.C < prev    next >
C/C++ Source or Header  |  1990-03-29  |  5KB  |  192 lines

  1. /* ba_main.c -- Main module of the TIMEX background agent
  2.  
  3.     February 1990    Mark E. Mallett, Personal Workstation Magazine
  4.  
  5. This is the main module of TIMEXBA, the TIMEX background agent
  6.  
  7. Included are the following routines:
  8.  
  9.     main        Main routine
  10.  
  11. */
  12.  
  13. #define INCL_BASE
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <os2.h>
  19.  
  20. #include "errhand.h"
  21. #include "timex.h"
  22. #include "ba_timex.h"
  23.  
  24.  
  25. /* Local definitions */
  26.  
  27.  
  28. /* External data referenced */
  29.  
  30. extern  char    *Dbfname;       /* Database filename */
  31. extern  HSEM    Eventsem;       /* Event list lock semaphore */
  32. extern  TID     Evtid;          /* Event thread ID */
  33. extern  char    *Logfname;      /* Logfile name */
  34. extern  int     Priclass;       /* My priority class */
  35. extern  int     Prival;         /* My priority value */
  36. extern  char    *Rqpipe;        /* Request-pipe name */
  37. extern  HPIPE   RqpipeH;        /* Handle for request pipe */
  38. extern  ERRLIST *Werdisp;       /* Wild-all error intercept list */
  39. extern  ERRLIST *Werlist;       /* Wildcard error list */
  40.  
  41. /* External routines used */
  42.  
  43. extern  void    db_read( void ); /* Read the events database */
  44. extern  void    err_report( int code, char *msgP );
  45. extern  void    log_begin( void );
  46. extern  void    log_end( void );
  47.  
  48. /* Local data publicly available */
  49.  
  50.  
  51. /* Local routines and forward declarations */
  52.  
  53.         void    init(void);
  54. static  int     main1( void );          /* First continuation of main() */
  55.  
  56. /* Private data */
  57.  
  58. static  int     ArgC;                   /* Copy of argc */
  59. static  char    **ArgV;                 /* Copy of argv */
  60.  
  61.         char    Evstack[EVSTACKSIZE];   /* Stack for the event thread (this
  62.                                            must be a global, non-static
  63.                                            array or an allocated segment). */
  64.  
  65. /*
  66.  
  67. *//* main()
  68.  
  69.         The main routine
  70.  
  71. */
  72.  
  73.  
  74. main(
  75.     int        argc,        /* Argument count */
  76.     char        **argv        /* Argument vector */
  77. ) {
  78.     /* Save the command line arg vector parms */
  79.     ArgC = argc;
  80.     ArgV = argv;
  81.  
  82.     /* Create a universal warning interception list */
  83.     Werdisp = condition_list( 0, CTINTCEPT );
  84.     any_condition( Werdisp, TRUE, err_report );
  85.  
  86.     /* Activate the list and continue to main1() */
  87.     intercept_error( main1, (void *)NULL, Werdisp );
  88.  
  89.     return( 0 );
  90. }
  91.  
  92. static int
  93. main1( void ) {
  94.  
  95.         int             status;
  96.         int             argX;
  97.         char            *stackP;        /* Stack for threads */
  98.  
  99.     /* Setup the wildcard error handling list */
  100.     Werlist = condition_list( 0, CTHANDLER );
  101.     any_condition( Werlist, TRUE, (int (*)())NULL );
  102.  
  103.     /* Scan command line arguments in a rather crude way */
  104.     for( argX = 1; argX < ArgC; ++argX ) {
  105.  
  106.         if ( strnicmp( ArgV[argX], "/DB=", 4 ) == 0 )
  107.             Dbfname = &ArgV[argX][4];
  108.  
  109.         else if ( strnicmp( ArgV[argX], "/LOG=", 5 ) == 0 )
  110.             Logfname = &ArgV[argX][5];
  111.  
  112.         else {
  113.             fprintf( stderr, "Usage: timexba [/DB=name] [/LOG=name]\n" );
  114.             return( 0 );
  115.         }
  116.     }
  117.  
  118.     /* Perform initialization of global data and resources */
  119.     init();
  120.  
  121.     /* Log startup */
  122.     log_begin();
  123.     log( "" );
  124.     log( "-----------------");
  125.     log( "TIMEXBA starts." );
  126.     log( "" );
  127.     log_end();
  128.  
  129.     /* Startup the background (event scheduler) thread */
  130.     stackP = &Evstack[EVSTACKSIZE];
  131.  
  132.     DosCreateThread( schedule_events, &Evtid, stackP );
  133.  
  134.     /* Poll for and service requests. */
  135.     while( 1 ) {
  136.         status = DosConnectNmPipe( RqpipeH );
  137.         if ( status != 0 )
  138.             error( EC_NOTOK, "DosConnectNmPipe failed (%d)", status );
  139.  
  140.         /* Process the request */
  141.         process_request();
  142.  
  143.         /* Disconnect. */
  144.         DosDisConnectNmPipe( RqpipeH );
  145.     }
  146.  
  147.     return( 0 );
  148. }
  149. /*
  150.  
  151. *//* init()
  152.  
  153.         Initialize global program elements
  154.  
  155. Accepts :
  156.  
  157.  
  158. Returns :
  159.  
  160.  
  161. */
  162.  
  163. void
  164. init() {
  165.         int             status;         /* Status result */
  166.  
  167.     /* Open the log file */
  168.  
  169.  
  170.     /* Read the event database.  Note that because we haven't started
  171.        the other threads yet, we don't have to lock the event list. */
  172.     db_read();
  173.  
  174.     /* Set to the defined running priority */
  175.     set_priority( 0, Priclass, Prival );
  176.  
  177.  
  178.     /* Setup semaphores used */
  179.     status = DosCreateSem( CSEM_PRIVATE, &Eventsem, EVSEMNAME );
  180.     if ( status != 0 )
  181.         error( EC_NOTOK, "Can't create event semaphore(%d)", status );
  182.  
  183.     /* Create the pipe for communications */
  184.     status = DosMakeNmPipe( Rqpipe, &RqpipeH,
  185.                 PIPE_ACCESS_DUPLEX | PIPE_NOWRITEBEHIND,
  186.                 PIPE_WAIT | PIPE_TYPE_MESSAGE |
  187.                         PIPE_UNLIMITED_INSTANCES,
  188.                 512, 512, 0L );
  189.     if ( status != 0 )
  190.         error( EC_NOTOK, "Can't create request pipe (%d)", status );
  191. }
  192.