home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d500 / signal.lha / Signal / Source / WaitFor.c < prev    next >
C/C++ Source or Header  |  1991-06-06  |  3KB  |  99 lines

  1. /*
  2.  *  WAITFOR.C   A program to allow processes that use RUN EXECUTE to
  3.  *              wait for their sub-processes to signal that they have
  4.  *              completed their tasks (or are ready to start, etc).
  5.  *
  6.  *              Used in conjunction with SIGNAL.C
  7.  *
  8.  *              Copyright (c) 1989 by Davide P. Cervone, all rights reserved.
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/ports.h>
  13. #include <libraries/dos.h>
  14. #include <proto/exec.h>
  15.  
  16. #define ERROR_NONE           0      /* Normal exit status */
  17. #define ERROR_CANCELLED     10      /* User pressed CTRL-C to cancel */
  18. #define ERROR_BADPORTNAME   11      /* No port name on command line */
  19. #define ERROR_PORTEXISTS    12      /* Port name already in use */
  20. #define ERROR_CANTMAKEPORT  13      /* Error while trying to create the port */
  21.  
  22. #define SIGBREAKF_ANY\
  23.    (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F)
  24.  
  25.  
  26. extern struct MsgPort *FindPort();
  27. extern struct MsgPort *CreatePort();
  28. extern long Wait();
  29.  
  30.  
  31. static struct MsgPort *thePort;         /* pointer to the named port */
  32.  
  33.  
  34. /*
  35.  *  DoExit()
  36.  *
  37.  *  General purpose exit routine.  Deletes the named port, if one was
  38.  *  created, then exits with the specified return code (EXECUTE will
  39.  *  report the return code, but interactive users will get no message).
  40.  *
  41.  */
  42.  
  43. static void DoExit(status)
  44. LONG status;
  45. {
  46.    if (thePort) DeletePort(thePort);
  47.    _exit(status);
  48. }
  49.  
  50.  
  51. /*
  52.  *  _main()
  53.  *
  54.  *  Replaces the standard Lattice _main routine (since no IO is performed
  55.  *  we don't nee the usual overhead).  _main expects the command line 
  56.  *  (as entered by the user, including with the program) to be on the
  57.  *  stack as its PortName argument.
  58.  *
  59.  *
  60.  *  First clear the DOS signals, in case any are set from before.
  61.  *  Check that the port name exists, and skip over the command name
  62.  *  in the command line, and any leading blanks.  The port name will
  63.  *  be whatever remains on the command line, including blanks and
  64.  *  special characters.
  65.  *
  66.  *  If the port name was specified, check to make sure the port does
  67.  *  not already exist.  If it does, error exit, otherwise, create the
  68.  *  named port, if possible.
  69.  *
  70.  *  Now, just wait for a signal fro mthat port (or for any DOS break
  71.  *  signal).  If the user sent a BREAK (or pressed CTRL-C, D, E, or F)
  72.  *  then error exit, otherwise, clean up the port and exit normally.
  73.  */
  74.  
  75. void _main(PortName)
  76. char *PortName;
  77. {
  78.    LONG theSignal;
  79.  
  80.    SetSignal(0L,SIGBREAKF_ANY);
  81.  
  82.    if (PortName == NULL) DoExit(ERROR_BADPORTNAME);
  83.    
  84.    while (*PortName != '\0' && *PortName != ' ') PortName++;
  85.    if (*PortName == '\0' || *PortName == '\n') DoExit(ERROR_BADPORTNAME);
  86.    while (*(++PortName) == ' ');
  87.  
  88.    if (FindPort(PortName)) DoExit(ERROR_PORTEXISTS);
  89.  
  90.    thePort = CreatePort(PortName,0);
  91.    if (thePort == 0) DoExit(ERROR_CANTMAKEPORT);
  92.  
  93.    theSignal = Wait(SIGBREAKF_ANY | (1<<thePort->mp_SigBit));
  94.    if (theSignal & SIGBREAKF_ANY)
  95.       DoExit(ERROR_CANCELLED);
  96.      else
  97.       DoExit(ERROR_NONE);
  98. }
  99.