home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / misc / Notify100.lzh / Notify.c < prev    next >
C/C++ Source or Header  |  1991-09-25  |  5KB  |  156 lines

  1. /*
  2.  *  Notify - the small notification utility
  3.  *  ⌐ 1991 Stefan Sticht
  4.  *  freely distributable
  5.  *
  6.  *  To compile with DICE: dcc -r Notify.c -o Notify
  7.  *
  8.  *  V1.00 25 Sep 1991 Stefan Sticht
  9.  */
  10.  
  11. #define VERSION "V1.00"
  12.  
  13. #include <stdlib.h>
  14. #include <dos/notify.h>
  15. #include <exec/memory.h>
  16. #include <clib/dos_protos.h>
  17. #include <clib/exec_protos.h>
  18.  
  19. /*
  20.  *  some SAS/C specularities, change for other compilers!
  21.  */
  22. #ifdef __SASC
  23. #include <pragmas/dos_pragmas.h>    /* pragmas for inline library calls         */
  24. #include <pragmas/exec_pragmas.h>   /* pragmas for inline library calls         */
  25. void chkabort(void) {}              /* disable Ctrl-C detect                    */
  26. #endif
  27.  
  28. extern struct Library *SYSBase;     /* Library base pointer from startup code   */
  29. extern struct Library *DOSBase;     /* Library base pointer from startup code   */
  30.  
  31. /*
  32.  *  a string, which will be found by Version
  33.  */
  34. char version[] = "\0$VER: Notify " VERSION;
  35.  
  36. #define TEMPLATE "File/A,Times/K/N,Quiet/S"
  37. #define OPT_FILE            0
  38. #define OPT_TIMES           1
  39. #define OPT_QUIET           2
  40. #define OPT_COUNT           3
  41. /*
  42.  *  this array will be filled by ReadArgs()
  43.  */
  44. long options[OPT_COUNT];
  45.  
  46. void _main(char *line)
  47. {
  48.     struct NotifyRequest *notifyrequest;
  49.     long rc = 20;
  50.     long times = 1;
  51.     struct RDArgs *args;
  52.     unsigned long sigreceived;
  53.     unsigned char signal;
  54.  
  55.     /*
  56.      *  first check DOS version; we require at least V37
  57.      */
  58.     if (DOSBase->lib_Version >= 37l) {
  59.         /*
  60.          *  get command line arguments
  61.          */
  62.         if (args = ReadArgs((UBYTE *)TEMPLATE, options, NULL)) {
  63.             /*
  64.              *  alloc public memory for notifyrequest
  65.              */
  66.             if (notifyrequest = (struct NotifyRequest *)AllocMem(sizeof(struct NotifyRequest), MEMF_PUBLIC)) {
  67.                 /*
  68.                  *  alloc a signal
  69.                  */
  70.                 if ((signal = AllocSignal(-1)) != -1l) {
  71.                     /*
  72.                      *  fill out notifyrequest
  73.                      */
  74.                     notifyrequest->nr_Name = (char *)options[OPT_FILE];
  75.                     notifyrequest->nr_Flags = NRF_SEND_SIGNAL;
  76.                     notifyrequest->nr_stuff.nr_Signal.nr_Task = FindTask(NULL);
  77.                     notifyrequest->nr_stuff.nr_Signal.nr_SignalNum = signal;
  78.                     /*
  79.                      *  now start a notify
  80.                      */
  81.                     if (StartNotify(notifyrequest) == DOSTRUE) {
  82.  
  83.                         rc = 0;
  84.                         if (options[OPT_TIMES]) times = *((long *)options[OPT_TIMES]);
  85.                         if (!options[OPT_QUIET]) VPrintf("Notification on %s started...\n", &options[OPT_FILE]);
  86.  
  87.                         while (times--) {
  88.                             /*
  89.                              *  Wait times times for a signal or one time for a break signal
  90.                              */
  91.                             sigreceived = Wait(SIGBREAKF_CTRL_C | (1 << signal));
  92.                             if (sigreceived & SIGBREAKF_CTRL_C) {
  93.                                 /*
  94.                                  *  user pressed or sent Ctrl-C
  95.                                  */
  96.                                 times = 0;
  97.                                 rc = 5;
  98.                                 if (!options[OPT_QUIET]) PutStr("***Break\n");
  99.                                 }
  100.  
  101.                             if ((sigreceived & (1 << signal)) && !options[OPT_QUIET])
  102.                                 PutStr("Notified.\n");
  103.  
  104.                             } /* while times */
  105.                         /*
  106.                          *  remove notify
  107.                          */
  108.                         EndNotify(notifyrequest);
  109.  
  110.                         }
  111.  
  112.                     else {
  113.                         /*
  114.                          *  StartNotify returned an error
  115.                          */
  116.                         rc = 10;
  117.                         PrintFault(IoErr(), "Error");
  118.                         }
  119.  
  120.                     /*
  121.                      *  free allocated signal
  122.                      */
  123.                     FreeSignal((long)signal);
  124.  
  125.                     } /* if signal = AllocSignal() */
  126.  
  127.                 else if (!options[OPT_QUIET]) PutStr("Couldn't allocate signal!\n");
  128.  
  129.                 /*
  130.                  *  free memory of notifyrequest
  131.                  */
  132.                 FreeMem(notifyrequest, sizeof(struct NotifyRequest));
  133.  
  134.                 } /* if notifyrequest = AllocMem() */
  135.  
  136.             else if (!options[OPT_QUIET]) PutStr("Couldn't allocate memory!\n");
  137.  
  138.             /*
  139.              *  free arguments allocated by ReadArgs()
  140.              */
  141.             FreeArgs(args);
  142.  
  143.             } /* if args = ReadArgs() */
  144.  
  145.         else {
  146.             /*
  147.              *  wrong number of or wrong arguments
  148.              */
  149.             if (!options[OPT_QUIET]) PrintFault(IoErr(), NULL);
  150.             }
  151.  
  152.         } /* if (DOSBase->dl_lib.lib_Version > 37l) */
  153.  
  154.     exit(rc);
  155. }
  156.