home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / cdity / EasyTM_src.lha / EasyTM-src / launch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  6.0 KB  |  245 lines

  1. //************************************
  2. //
  3. // Name : Launch.c
  4. //
  5. //************************************
  6.  
  7. //**** Header files
  8.  
  9. //** OS Include files
  10.  
  11. #include <exec/memory.h>
  12. #include <workbench/startup.h>
  13. #include <libraries/wbstart.h>
  14. #include <dos/dos.h>
  15. #include <dos/dostags.h>
  16.  
  17. //** OS function prototypes
  18.  
  19. #include <clib/exec_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include <clib/wbstart_protos.h>
  22. #include <clib/macros.h>
  23.  
  24. //** OS function inline calls
  25.  
  26. #include <pragmas/exec_pragmas.h>
  27. #include <pragmas/dos_pragmas.h>
  28. #include <pragmas/wbstart_pragmas.h>
  29.  
  30. //** ANSI C includes
  31.  
  32. //#include <stdio.h>
  33. #include <string.h>
  34.  
  35. //** Application include
  36.  
  37. #include "Prefs.h"
  38. #include "launch.h"
  39. #include "Librarian.h"
  40.  
  41.  
  42. //**** Local Storage
  43.  
  44. static ULONG ConIOTags[] = { SYS_Output, 0, SYS_Input, 0, SYS_Asynch, 1,
  45.                              SYS_UserShell, 1, TAG_DONE };
  46. static ULONG NoIOTags[]  = { SYS_Asynch, 1, SYS_UserShell, 1, TAG_DONE, };
  47.  
  48.  
  49. //**** Aux functions
  50.  
  51. void Launch_WB(struct ProgNode *pn, long argc, struct WBArg *argv);
  52. void Launch_CLI(struct ProgNode *pn, long argc, struct WBArg *argv);
  53. void Launch_Rexx(struct ProgNode *pn, long argc, struct WBArg *argv);
  54. void Launch_Batch(struct ProgNode *pn, long argc, struct WBArg *argv);
  55.  
  56.  
  57. void Launch(struct ProgNode *pn, long argc, struct WBArg *argv) {
  58.   if (NULL==pn) {
  59.     Launch_WB(pn,argc, argv);
  60.   } else
  61.   switch ( pn->pn_LaunchCode & LC_ENV_MASK ) {
  62.     case LC_WB :
  63.       Launch_WB(pn,argc, argv);
  64.       break;
  65.     case LC_CLI:
  66.       Launch_CLI(pn,argc, argv);
  67.       break;
  68.     case LC_REXX:
  69.       Launch_Rexx(pn,argc, argv);
  70.       break;
  71.     case LC_BATCH:
  72.       Launch_Batch(pn,argc, argv);
  73.       break;
  74.     default:
  75.       //intf("unknown LC_ENV\n");
  76.       break;
  77.   } // switch
  78. } // Launch
  79.  
  80. void Launch_WB(struct ProgNode *pn, long argc, struct WBArg *argv) {
  81.   static ULONG mytags[]={
  82.     WBStart_Name,        0L,
  83.     WBStart_DirectoryName,    0L,
  84.     WBStart_Stack,        0L,
  85.     WBStart_Priority,        0L,
  86.     WBStart_ArgumentCount,    0L,
  87.     WBStart_ArgumentList,    0L
  88.   };
  89.  
  90.   if (NULL==pn) {
  91.     mytags[1]=(ULONG)PrefsProgName;
  92.     mytags[3]=(ULONG)PrefsProgDir;
  93.     mytags[5]=4096;
  94.     mytags[7]=0;
  95.     mytags[9]=0L;
  96.     mytags[11]=0L;
  97.     WBStartTagList( (struct TagItem *)mytags);
  98.   } else {
  99.     mytags[1]=(ULONG)pn->pn_Filename;
  100.     mytags[3]=(ULONG)pn->pn_Directory;
  101.     mytags[5]=MAX(4096,pn->pn_Stack);
  102.     mytags[7]=pn->pn_Priority;
  103.     switch ( pn->pn_LaunchCode & LC_ARG_MASK ) {
  104.       case LC_EACH :
  105.         if (NULL!=argc) {
  106.           mytags[9]=1L;
  107.           for ( ; argc-- ; ) {
  108.             mytags[11]=(ULONG)argv++;
  109.             WBStartTagList( (struct TagItem *)mytags);
  110.           } // for
  111.           break;
  112.         }; // fall through to NONE
  113.       case LC_NONE :
  114.         mytags[9]=0L;
  115.         mytags[11]=0L;
  116.         WBStartTagList( (struct TagItem *)mytags);
  117.         break;
  118.       case LC_ALL  :
  119.         mytags[9]=argc;
  120.         mytags[11]=(ULONG)argv;
  121.         WBStartTagList( (struct TagItem *)mytags);
  122.         break;
  123.       default :
  124.         //intf("Bad ARG (WB)\n");
  125.         break;
  126.     } // switch
  127.   }
  128. } // Launch_WB
  129.  
  130. void MyRun(struct ProgNode *pn, STRPTR cline) {
  131.  
  132.   STRPTR ConsoleName="CON:0/0/640/128/Output/AUTO/WAIT/CLOSE";
  133.   APTR ConsoleTask=0;
  134.  
  135.   if (ConIOTags[1]=Open(ConsoleName, MODE_NEWFILE)) {
  136.     struct Process *Proc=((struct Process *)FindTask(0L));
  137.     struct FileHandle *FH;
  138.  
  139.     ConsoleTask=Proc->pr_ConsoleTask;
  140.     FH=BADDR(ConIOTags[1]);
  141.     SetConsoleTask(FH->fh_Type);
  142.     ConIOTags[3]=Open("*", MODE_OLDFILE);
  143.   }; // if
  144.  
  145.   if (ConIOTags[1])
  146.     System(cline, (struct TagItem *)&ConIOTags);
  147.   else
  148.     System(cline, (struct TagItem *)&NoIOTags);
  149.  
  150.   if (ConsoleTask) SetConsoleTask(ConsoleTask);
  151. } // MyRun
  152.  
  153. void Launch_CLI(struct ProgNode *pn, long argc, struct WBArg *argv) {
  154.   long templsize=0;
  155.   STRPTR templ=NULL;
  156.   long clinesize=0;
  157.   STRPTR cline=NULL;
  158.   BPTR newl,oldl;
  159.   long i,j;
  160.   struct WBArg *ta;
  161.   char *p;
  162.   char *q;
  163.  
  164.  
  165.   // Create Template
  166.   templsize=strlen(pn->pn_Directory)+2+strlen(pn->pn_Filename);
  167.   templ=AllocVec(templsize,MEMF_CLEAR);
  168.   strcpy(templ,pn->pn_Directory);
  169.   AddPart( templ, pn->pn_Filename, templsize );
  170.   //intf("Template = '-%s'\n",templ);
  171.  
  172.   switch ( (strstr(templ,"%s"))?pn->pn_LaunchCode & LC_ARG_MASK:LC_NONE ) {
  173.     case LC_NONE:
  174.       MyRun(pn,templ);
  175.       break;
  176.     case LC_EACH:
  177.       // Count longest wbarg name
  178.       for ( i=argc, ta=argv; i--; ta++) {
  179.         j=strlen(ta->wa_Name);
  180.         if (j>clinesize) clinesize=j;
  181.       } // for
  182.       // create space for command line
  183.       cline=AllocVec(clinesize+templsize,NULL);
  184.       // copy command part
  185.       p=strstr(templ,"%s");
  186.       *p=0;
  187.       strcpy(cline,templ);
  188.       q=cline+(p-templ);
  189.       p+=2;
  190.       // Launch programs
  191.       for( ; argc-- ; argv++) {
  192.         newl = DupLock(argv->wa_Lock);
  193.         oldl = CurrentDir(newl);
  194.         strcpy(q,argv->wa_Name);
  195.         strcat(q,p);
  196.         MyRun(pn,cline);
  197.         CurrentDir(oldl);
  198.         UnLock(newl);
  199.       } // for
  200.       FreeVec(cline);
  201.       break;
  202.     case LC_ALL:
  203.       {
  204.         char buffer[512];
  205.         // eval size
  206.         for ( i=argc, ta=argv; i--; ta++) {
  207.           NameFromLock(ta->wa_Lock, buffer, 512);
  208.           clinesize +=strlen(ta->wa_Name)+strlen(buffer)+2;
  209.         } // for
  210.         // create space for command line
  211.         cline=AllocVec(clinesize+templsize,NULL);
  212.         // copy command part
  213.         p=strstr(templ,"%s");
  214.         *p=0;
  215.         strcpy(cline,templ);
  216.         // Add each argument
  217.         for ( i=argc, ta=argv; i--; ta++) {
  218.           NameFromLock(ta->wa_Lock, buffer, 512);
  219.           AddPart(buffer,ta->wa_Name,512);
  220.           strcat(cline,buffer);
  221.           strcat(cline," ");
  222.         } // for
  223.         // copy tail of command line
  224.         strcat(cline,p+=2);
  225.         //intf("%s\n",cline);
  226.         MyRun(pn,cline);
  227.         // CleanUp
  228.         FreeVec(cline);
  229.       } // nada
  230.       break;
  231.     default :
  232.       //intf("Unknown ARG code\n");
  233.       break;
  234.   } // switch
  235.   FreeVec(templ);
  236. } // Launch_CLI
  237.  
  238. void Launch_Rexx(struct ProgNode *pn, long argc, struct WBArg *argv) {
  239. } // Launch_Rexx
  240.  
  241. void Launch_Batch(struct ProgNode *pn, long argc, struct WBArg *argv) {
  242. } // Launch_Batch
  243.  
  244. //**** End of file
  245.