home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 492.lha / ToolManager_v1.3 / src / starttools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-06  |  5.1 KB  |  167 lines

  1. /*
  2.  * starttools.c   V1.3
  3.  *
  4.  * CLI & WB startup procedure
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Start tool as a CLI process */
  12. BOOL StartCLITool(struct AppMessage *msg, struct ToolNode *tn, char *name)
  13. {
  14.  BPTR fl;                       /* AmigaDOS file handle */
  15.  BOOL rc=TRUE;
  16.  char cmd[NAMELEN];             /* Buffer for command line */
  17.  int cmdlen;                    /* Command line length */
  18.  char dir[NAMELEN];
  19.  struct WBArg *wa=msg->am_ArgList;
  20.  int i;
  21.  
  22.  fl=CurrentDir(tn->tn_DirLock); /* Change to tool's directory */
  23.  
  24.  strcpy(cmd,name);              /* Command name first */
  25.  cmdlen=strlen(cmd);
  26.  
  27.  for (i=msg->am_NumArgs; i; i--,wa++)
  28.   {
  29.    char *cp;
  30.    int namelen;
  31.  
  32.    if (!wa->wa_Lock) continue;  /* Skip arguments which don't support locks! */
  33.  
  34.    if (cmdlen>NAMELEN-2) break; /* Append a space for each parameter */
  35.    strcat(cmd," ");
  36.    cmdlen++;
  37.  
  38.    /* Build parameter from Lock & name */
  39.    if (SameLock(tn->tn_DirLock,wa->wa_Lock)==LOCK_SAME) cp=wa->wa_Name;
  40.    else
  41.     {
  42.      if (!NameFromLock(wa->wa_Lock,dir,NAMELEN)) continue;
  43.      if (!AddPart(dir,wa->wa_Name,NAMELEN)) continue;
  44.      cp=dir;
  45.     }
  46.  
  47.    namelen=strlen(cp);
  48.  
  49.    if (strchr(cp,' '))          /* Special case: Space in a filename */
  50.     if (namelen>NAMELEN-3) break;
  51.     else
  52.      {
  53.       strins(cp,"\"");          /* Set parameter in double quotes */
  54.       strcat(cp,"\"");
  55.       namelen+=2;
  56.      }
  57.  
  58.    if (cmdlen+namelen>NAMELEN-2) break;
  59.    strcat(cmd,cp);              /* Append parameter */
  60.    cmdlen+=namelen;             /* New command line length */
  61.   }
  62.  
  63.  /* Start tool */
  64.  if (SystemTags(cmd,SYS_Input,Open("NIL:",MODE_NEWFILE),
  65.                     SYS_Output,Open("NIL:",MODE_NEWFILE),
  66.                     SYS_Asynch,TRUE, /* Run tools asynchronously */
  67.                     TAG_DONE)==-1)
  68.   rc=FALSE; /* An error occured */
  69.  
  70.  CurrentDir(fl);                /* Change to old directory */
  71.  return(rc);
  72. }
  73.  
  74. /* Start tool as a WB process */
  75. BOOL StartWBTool(struct AppMessage *msg, struct ToolNode *tn, char *name)
  76. {
  77.  BPTR fl;                       /* AmigaDOS file handle */
  78.  struct WBStartup *wbs;         /* WBStartup message for tool */
  79.  struct DiskObject *tdob;       /* Tool icon */
  80.  LONG ssize=4096;               /* StackSize, default */
  81.  struct MsgPort *proc;          /* Process descriptor for tool */
  82. /* struct Process *proc;  Process descriptor for CreateNewProc */
  83.  struct WBArg *wbad,*wbas;      /* Pointers to WB arguments */
  84.  int i;
  85.  
  86.  /* Allocate memory for WBStartup */
  87.  if (!(wbs=calloc(sizeof(struct WBStartup)+
  88.                   sizeof(struct WBArg)*(msg->am_NumArgs+1),1))) return (FALSE);
  89.  
  90.  fl=CurrentDir(tn->tn_DirLock); /* Change to tool's directory */
  91.  
  92.  if (!(wbs->sm_Segment=NewLoadSeg(name,NULL))) goto e1; /* Load tool code */
  93.  
  94.  /* Get some information about the tool */
  95.  if (tdob=GetDiskObject(name))
  96.   {
  97.    if (tdob->do_Type==WBTOOL) /* Only WBTools supply this type of info! */
  98.     {
  99.      if (tdob->do_ToolWindow) wbs->sm_ToolWindow=strdup(tdob->do_ToolWindow);
  100.      if (tdob->do_StackSize>ssize) ssize=tdob->do_StackSize;
  101.     }
  102.    FreeDiskObject(tdob);
  103.   }
  104.  
  105.  /* Build WBStartup message */
  106.  /* wbs->sm_Message.mn_Node.ln_Type=NT_MESSAGE; PutMsg() does this for us! */
  107.  wbs->sm_Message.mn_ReplyPort=MyMP;
  108.  wbs->sm_Message.mn_Length=sizeof(struct WBStartup);
  109.  wbs->sm_NumArgs=msg->am_NumArgs+1;
  110.  wbs->sm_ArgList=wbs+1;             /* WBArg array starts after WBStartup */
  111.  
  112.  /* Initialize WBArg pointers */
  113.  wbas=msg->am_ArgList;
  114.  wbad=wbs->sm_ArgList;
  115.  
  116.  /* 1. argument is the tool itself! */
  117.  if (!(wbad->wa_Lock=DupLock(tn->tn_DirLock))) goto e2;
  118.  if (!(wbad->wa_Name=strdup(name))) goto e3;
  119.  wbad++;
  120.  
  121.  /* Copy WB arguments */
  122.  for (i=msg->am_NumArgs; i; i--,wbas++,wbad++)
  123.   {
  124.    if (!(wbad->wa_Lock=DupLock(wbas->wa_Lock)))
  125.     {
  126.      wbad--;             /* Skip parameters, which don't support a lock */
  127.      wbs->sm_NumArgs--;
  128.      continue;           /* Next parameter */
  129.     }
  130.  
  131.    /* Sanity check for name string... Enforcer is watching you! */
  132.    if (!wbas->wa_Name || !(wbad->wa_Name=strdup(wbas->wa_Name))) goto e3;
  133.   }
  134.  
  135.  /* Create process */
  136.  if (!(wbs->sm_Process=CreateProc(wbs->sm_ArgList->wa_Name,0,wbs->sm_Segment,
  137.                                   ssize))) goto e3;
  138.  
  139. /* if (!(proc=CreateNewProcTags(NP_Seglist,wbs->sm_Segment,
  140.                               NP_FreeSeglist,TRUE,
  141.   Maybe someday I will know   NP_StackSize,ssize,
  142.   what Tags I need to make    NP_Name,wbs->sm_ArgList->wa_Name,
  143.   CreateNewProc() behave      NP_CloseInput,FALSE,
  144.   like CreateProc()           NP_CloseOutput,FALSE,
  145.                               TAG_DONE))) goto e3; */
  146.  
  147.  /* Send WBStartup message to tool */
  148. /* wbs->sm_Process=&proc->pr_MsgPort; for CreateNewProc() */
  149.  PutMsg(wbs->sm_Process,(struct Message *) wbs);
  150.  CurrentDir(fl);                /* Change to old directory */
  151.  wbactive++;                    /* Tool started! */
  152.  return(TRUE);
  153.  
  154.  /* An error occurred. Free all resources */
  155. e3: wbas=wbs->sm_ArgList;
  156.     for (i=wbs->sm_NumArgs; i; i--,wbas++)
  157.      {
  158.       UnLock(wbas->wa_Lock);
  159.       if (wbas->wa_Name) free(wbas->wa_Name);
  160.      }
  161. e2: UnLoadSeg(wbs->sm_Segment);
  162. e1: CurrentDir(fl);
  163.     free(wbs);
  164.     return(FALSE);
  165. }
  166.  
  167.