home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d896 / wbstart.lha / WBStart / WBStarter.c < prev   
C/C++ Source or Header  |  1993-06-27  |  3KB  |  122 lines

  1. /*
  2.  * WBStarter.c   V1.3
  3.  *
  4.  * Start WB programs via WBStart-Handler
  5.  *
  6.  * (c) 1991-93 Stefan Becker
  7.  *
  8.  */
  9.  
  10. /* Handler includes */
  11. #include "WBStart.h"
  12.  
  13. /* System includes */
  14. #include <dos/dostags.h>
  15. #include <workbench/startup.h>
  16.  
  17. /* Prototypes */
  18. #include <clib/dos_protos.h>
  19. #include <clib/exec_protos.h>
  20.  
  21. /* Pragmas */
  22. #include <pragmas/dos_pragmas.h>
  23. #include <pragmas/exec_pragmas.h>
  24.  
  25. /* ANSI C includes */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28.  
  29. /* Library bases */
  30. extern struct Library *SysBase, *DOSBase;
  31.  
  32. /* Version string */
  33. static const char Version[]="$VER: WBStarter 1.3 (" __COMMODORE_DATE__ ")";
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37.  BPTR fl;
  38.  struct WBStartMsg msg;
  39.  struct MsgPort *mp,*hp;
  40.  
  41.  /* Check OS version */
  42.  if (SysBase->lib_Version<37) return(20);
  43.  
  44.  if (!(mp=CreateMsgPort())) {
  45.   puts("No message port!\n");
  46.   exit(20);
  47.  }
  48.  
  49.  fl=CurrentDir(NULL);
  50.  msg.wbsm_Msg.mn_Node.ln_Pri=0;
  51.  msg.wbsm_Msg.mn_ReplyPort=mp;
  52.  msg.wbsm_DirLock=fl;
  53.  msg.wbsm_Stack=4096;
  54.  msg.wbsm_Prio=0;
  55.  msg.wbsm_NumArgs=0;
  56.  msg.wbsm_ArgList=NULL;
  57.  
  58.  while (--argc) {
  59.   msg.wbsm_Name=*++argv;
  60.  
  61.   /* Try to send a message to the WBStart-Handler */
  62.   Forbid();
  63.   hp=FindPort(WBS_PORTNAME);
  64.   if (hp) PutMsg(hp,(struct Message *) &msg);
  65.   Permit();
  66.  
  67.   /* No WBStart-Handler, try to start it! */
  68.   if (!hp) {
  69.    BPTR ifh=Open("NIL:",MODE_NEWFILE);
  70.    BPTR ofh=Open("NIL:",MODE_OLDFILE);
  71.  
  72.    /* Start handler */
  73.    if (SystemTags(WBS_LOADNAME,SYS_Input,ifh,
  74.                                SYS_Output,ofh,
  75.                                SYS_Asynch,TRUE,
  76.                                SYS_UserShell,TRUE,
  77.                                NP_ConsoleTask,NULL,
  78.                                NP_WindowPtr,NULL,
  79.                                TAG_DONE)!=-1) {
  80.     int i;
  81.  
  82.     /* Handler started, try to send message (Retry up to 5 seconds) */
  83.     for (i=0; i<10; i++) {
  84.      /* Try to send message */
  85.      Forbid();
  86.      hp=FindPort(WBS_PORTNAME);
  87.      if (hp) PutMsg(hp,(struct Message *) &msg);
  88.      Permit();
  89.  
  90.      /* Message sent? Yes, leave loop */
  91.      if (hp) break;
  92.  
  93.      /* No, wait 1/2 second */
  94.      Delay(25);
  95.     }
  96.    } else {
  97.     /* Handler not started, close file handles */
  98.     Close(ifh);
  99.     Close(ofh);
  100.    }
  101.   }
  102.  
  103.   /* Could we send the message? */
  104.   if (hp) {
  105.    /* Get reply message */
  106.    WaitPort(mp);
  107.    GetMsg(mp);
  108.   } else {
  109.    /* Oops. ERROR! */
  110.    puts("Can't find 'WBStart-Handler'!");
  111.    break;
  112.   }
  113.  }
  114.  
  115.  /* Free resources */
  116.  CurrentDir(fl);
  117.  DeleteMsgPort(mp);
  118.  
  119.  /* All OK! */
  120.  return(0);
  121. }
  122.