home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / System / Sysmon / src / Broadcast.c < prev    next >
C/C++ Source or Header  |  2000-05-30  |  4KB  |  134 lines

  1. /*
  2. **    $RCSfile: Broadcast.c,v $
  3. **    $Filename: Broadcast.c $
  4. **    $Revision: 1.1 $
  5. **    $Date: 2000/05/01 19:47:16 $
  6. **
  7. **    sysmon.library support command Broadcast (version 1.1) 
  8. **    
  9. **    (C) Copyright 1995-2000 by Etienne Vogt
  10. */
  11.  
  12. #include <exec/alerts.h>
  13. #include <exec/memory.h>
  14. #include <dos/dos.h>
  15. #include <workbench/startup.h>
  16. #define __USE_SYSBASE
  17. #include <proto/exec.h>
  18. #include <proto/dos.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include "sysmon.h"
  22. #include "sysmon_protos.h"
  23. #include "sysmon_pragmas.h"
  24.  
  25. struct ExecBase *SysBase;
  26. struct DosLibrary *DOSBase;
  27. struct SysmonBase *SysmonBase;
  28. static struct WBStartup *wbmsg;
  29. static struct RDArgs *myrda;
  30. struct BroadcastMsg *bcmsg;
  31.  
  32. ULONG __saveds main(void);
  33. static void cleanexit(ULONG rc);
  34. static __inline BOOL SysLog(ULONG priority, APTR format, ...);
  35.  
  36. static UBYTE version[] = "$VER: Broadcast 1.1 (30.4.2000)";
  37. static UBYTE template[] = "MESSAGE/A,COUNTDOWN/K/N,TIMEOUT/K/N,CANCEL/S,DEBUG/S,NORMAL/S,URGENT/S,SHUTDOWN/S,UNMOUNT/S,HALT/S";
  38.  
  39. #define OPT_MESSAGE    0
  40. #define    OPT_COUNTDOWN    1
  41. #define OPT_TIMEOUT    2
  42. #define    OPT_CANCEL    3
  43. #define    OPT_DEBUG    4
  44. #define OPT_NORMAL    5
  45. #define    OPT_URGENT    6
  46. #define    OPT_SHUTDOWN    7
  47. #define    OPT_UNMOUNT    8
  48. #define    OPT_HALT    9
  49. #define OPTMAX        10
  50.  
  51. ULONG __saveds main(void)    /* No startup code */
  52. {
  53.   struct Process *myproc;
  54.   LONG opts[OPTMAX];
  55.  
  56.   SysBase = *(struct ExecBase **)4;
  57.   DOSBase = NULL;
  58.   SysmonBase = NULL;
  59.   wbmsg = NULL;
  60.   myrda = NULL;
  61.   bcmsg = NULL;
  62.  
  63.   myproc = (struct Process *)FindTask(NULL);
  64.   if ((DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",36)) == NULL)
  65.   { Alert(AT_Recovery|AG_OpenLib|AO_DOSLib);
  66.     return 100;
  67.   }
  68.  
  69.   if (!(myproc->pr_CLI))        /* If started from WB, exit cleanly */
  70.   { WaitPort(&(myproc->pr_MsgPort));
  71.     wbmsg = (struct WBStartup *)GetMsg(&(myproc->pr_MsgPort));
  72.     cleanexit(20);
  73.   }
  74.   else
  75.   { memset((char *)opts, 0, sizeof(opts));
  76.     if ((SysmonBase = (struct SysmonBase *)OpenLibrary("sysmon.library",1)) == NULL)
  77.     { PutStr("Broadcast : Couldn't open sysmon.library V1\n");
  78.       cleanexit(20);
  79.     }
  80.     if ((myrda = ReadArgs(template, opts, NULL)) == NULL)
  81.     { PrintFault(IoErr(),NULL);
  82.       cleanexit(20);
  83.     }
  84.  
  85.     if ((bcmsg = AllocVec(sizeof(struct BroadcastMsg), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  86.     { PutStr("Broadcast : No memory for struct BroadCastMsg\n");
  87.       cleanexit(20);
  88.     }
  89.  
  90.     bcmsg->bcm_EventTxt = (STRPTR)opts[OPT_MESSAGE];
  91.     bcmsg->bcm_TimeOut = 50;
  92.     bcmsg->bcm_Level = BCM_NORMAL;
  93.  
  94.     if (opts[OPT_COUNTDOWN]) bcmsg->bcm_CountDown = *((ULONG *)opts[OPT_COUNTDOWN]);
  95.     if (opts[OPT_TIMEOUT]) bcmsg->bcm_TimeOut = *((ULONG *)opts[OPT_TIMEOUT]);
  96.  
  97.     if (opts[OPT_CANCEL]) bcmsg->bcm_Flags |= BCMF_CANCEL;
  98.  
  99.     if (opts[OPT_DEBUG]) bcmsg->bcm_Level = BCM_DEBUG;
  100.     if (opts[OPT_NORMAL]) bcmsg->bcm_Level = BCM_NORMAL;
  101.     if (opts[OPT_URGENT]) bcmsg->bcm_Level = BCM_URGENT;
  102.     if (opts[OPT_SHUTDOWN]) bcmsg->bcm_Level = BCM_SHUTDOWN;
  103.     if (opts[OPT_UNMOUNT]) bcmsg->bcm_Level = BCM_UNMOUNT;
  104.     if (opts[OPT_HALT]) bcmsg->bcm_Level = BCM_HALT;
  105.  
  106.     if (smSendBroadcastMsg(bcmsg) >= 0)
  107.     { Printf("Broadcast Message successfully sent to %ld clients (%ld timeouts)\n",bcmsg->bcm_ReplyCount,bcmsg->bcm_TimeOutCount);
  108.       Printf("%s\n",bcmsg->bcm_EventTxt);
  109.     }
  110.     else
  111.     { PutStr("Broadcast : Error sending broadcast message\n");
  112.       cleanexit(10);
  113.     }
  114.   }
  115.   cleanexit(0);
  116. }
  117.  
  118. static void cleanexit(ULONG rc)
  119. {
  120.   if (bcmsg) FreeVec(bcmsg);
  121.   if (myrda) FreeArgs(myrda);
  122.   if (DOSBase) CloseLibrary((struct Library *)DOSBase);
  123.   if (SysmonBase) CloseLibrary((struct Library *)SysmonBase);
  124.   if (wbmsg)
  125.   { Forbid();
  126.     ReplyMsg((struct Message *)wbmsg);
  127.   }
  128.   Exit(rc);
  129. }
  130.  
  131. static __inline BOOL SysLog(ULONG priority, APTR format, ...)
  132. { return smVSysLog(priority, format, &format + 1);
  133. }
  134.