home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 447.lha / ToolManager_v1.0 / ToolManager.c < prev    next >
C/C++ Source or Header  |  1990-12-08  |  13KB  |  362 lines

  1. #include "ToolManager.h"
  2.  
  3. /* Version string */
  4. MyIdent="$VER: ToolManager 1.0 (4.11.1990)";
  5.  
  6. /* Library pointers */
  7. extern struct Library *SysBase,*IntuitionBase; /* Autoinit Libraries */
  8. struct Library *WorkbenchBase,*IconBase;
  9.  
  10. /* Structures for my icon */
  11. __chip UWORD ImageData[]={ /* Graphic data (48x48 Pixels) */
  12.  0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  13.  0x0000,0x7ffE,0x0000,0x0000,0x7ffE,0x0000,0x0000,0x7ffE,0x0000,
  14.  0x0000,0x7ffE,0x0000,0x0000,0x7ffE,0x0000,0x0000,0x7ffE,0x0000,
  15.  0x3fff,0xffff,0xfffC,0x3fff,0xffff,0xfffC,0x3fff,0xffff,0xfffC,
  16.  0x3fff,0xffff,0xfffC,0x3fff,0xffff,0xfffC,0x3fff,0xffff,0xfffC,
  17.  0x0000,0x7ffE,0x0000,0x0000,0x7ffE,0x0000,0x0000,0x7ffE,0x0000,
  18.  0x0000,0x7ffE,0x0000,0x0000,0x7ffE,0x0000,0x0000,0x7ffE,0x0000,
  19.  0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
  20. struct Image MyIconImage={NULL,NULL,48,24,1,ImageData,1,0,NULL};
  21. struct DiskObject MyIcon={NULL,NULL,
  22.   /* Gadget definition */ {NULL,NULL,NULL,48,25,NULL,NULL,NULL,&MyIconImage,
  23.                            NULL,NULL,NULL,NULL,NULL,NULL},
  24.                           NULL,NULL,NULL,NO_ICON_POSITION,
  25.                           NO_ICON_POSITION,NULL,NULL,NULL};
  26. struct AppIcon *MyAppIcon;
  27.  
  28. /* Structures for my window */
  29. struct IntuiText MyText={1,0,JAM2,0,0,NULL,(UBYTE *) "Active Tools: 00",NULL};
  30. struct NewWindow MyNewWindow={20,20,250,50,0,1,CLOSEWINDOW,WINDOWDRAG|
  31.                               WINDOWDEPTH|WINDOWCLOSE|SMART_REFRESH|ACTIVATE,
  32.                               NULL,NULL,
  33.                               (UBYTE *) "ToolManager ©1990 by S. Becker",NULL,
  34.                               NULL,0,0,0,0,WBENCHSCREEN};
  35. struct Window *MyWindow;
  36.  
  37. /* Structures for my message port */
  38. struct MsgPort *MyMP;
  39. char *MyMPName="ToolManager";
  40.  
  41. /* Tags for System() */
  42. struct TagItem MyTags[]={SYS_Input,NULL,  /* Input file handle */
  43.                          SYS_Output,NULL, /* Output file handle */
  44.                          SYS_Asynch,TRUE, /* Run tools asynchronously */
  45.                          TAG_DONE,NULL};
  46.  
  47. /* miscellaneous */
  48. char *MyName;
  49. struct AppMenuItem *MyAppMenuItem;
  50. struct List ToolList; /* This list contains ToolNodes */
  51. UWORD ActTools=0;     /* number of tools in ToolList */
  52. #define BUFLEN 100
  53. char ConfigLine[BUFLEN];
  54.  
  55. /* Workbench main entry point */
  56. void wbmain(struct WBStartup *wbarg)
  57. {
  58.  BPTR fl;
  59.  
  60.  MyName=wbarg->sm_ArgList->wa_Name;         /* What is my name? */
  61.  fl=CurrentDir(wbarg->sm_ArgList->wa_Lock); /* Goto a valid directory */
  62.  startup();                                 /* common startup code */
  63.  CurrentDir(fl);                            /* Go back to old directory */
  64.                                             /* Process WB startup parameters */
  65.  WBAddToolNode(&wbarg->sm_ArgList[1],wbarg->sm_NumArgs-1);
  66.  mainloop();                                /* Go into main loop */
  67. }
  68.  
  69. /* CLI main entry point */
  70. void main(int argc, char **argv)
  71. {
  72.  int i;
  73.  BPTR fl;
  74.  
  75.  MyName=argv[0];      /* What is my name? */
  76.  startup();           /* common startup code */
  77.  fl=CurrentDir(NULL); /* Get current directory lock */
  78.                       /* Process CLI startup parameters */
  79.  for (i=1; i<argc; i++) AddToolNode(fl,argv[i],NULL);
  80.  CurrentDir(fl);      /* Go back to old directory */
  81.  mainloop();          /* Go into main loop */
  82. }
  83.  
  84. /* Handle common startup operations */
  85. void startup(void)
  86. {
  87.  BPTR fl;
  88.  FILE *fh; /* Filehandle for config file */
  89.  
  90.  if (SysBase->lib_Version<36)  /* Sanity check */
  91.   {
  92.    puts("You need a Kickstart 2.0 or better to run ToolManager!");
  93.    exit(20);
  94.   }
  95.  
  96.  Forbid();                     /* Is "ToolManager" already running? */
  97.  MyMP=FindPort(MyMPName);
  98.  Permit();
  99.  if (MyMP)
  100.   {                            /* Yes, exit gracefully */
  101.    puts("ToolManager already running!");
  102.    exit(5);
  103.   }
  104.  
  105.  if (!(WorkbenchBase=OpenLibrary(WORKBENCH_NAME,36))) /* Open libraries */
  106.   cleanup(1);
  107.  
  108.  if (!(IconBase=OpenLibrary(ICONNAME,0)))
  109.   cleanup(2);
  110.  
  111.  if (!(MyMP=CreateMsgPort()))  /* Create a message port */
  112.   cleanup(3);
  113.  MyMP->mp_Node.ln_Pri=0;
  114.  MyMP->mp_Node.ln_Name=MyMPName; /* Our name */
  115.  AddPort(MyMP);                  /* Announce our presence */
  116.  
  117.  /* Notify Workbench about icon */
  118.  if (!(MyAppIcon=AddAppIcon(NULL,NULL,MyName,MyMP,NULL,&MyIcon,NULL)))
  119.   cleanup(4);
  120.  
  121.  /* Notify Workbench about special menu item. If this item is selected,
  122.     the program will quit */
  123.  if (!(MyAppMenuItem=AddAppMenuItem(NULL,NULL,"Quit ToolManager",MyMP,NULL)))
  124.   cleanup(5);
  125.  
  126.  NewList(&ToolList);  /* Initialize tool list */
  127.  fl=CurrentDir(NULL); /* Get the current directory */
  128.  
  129.  if (fh=fopen("S:ToolManager.Config","r")) /* Scan config file */
  130.   {
  131.    while (!feof(fh)) /* if not end of file, read one line into buffer */
  132.     if (fgets(ConfigLine,BUFLEN,fh) && (strlen(ConfigLine)>1))
  133.      {
  134.       char *tp;
  135.  
  136.       ConfigLine[strlen(ConfigLine)-1]='\0'; /* Strip newline */
  137.       if (tp=strchr(ConfigLine,';'))         /* scan config line */
  138.        {
  139.         *tp='\0';                        /* Menu entry ; real program name */
  140.         AddToolNode(fl,ConfigLine,++tp);
  141.        }
  142.       else
  143.        AddToolNode(fl,ConfigLine,NULL);  /* Menu entry == real program name */
  144.      }
  145.    fclose(fh);  /* close config file */
  146.   }
  147.  CurrentDir(fl); /* go back to old directory */
  148. }
  149.  
  150. /* The main processing loop */
  151. void mainloop(void)
  152. {
  153.  BOOL end=FALSE;               /* Flag for main loop */
  154.  BOOL windowopen=FALSE;
  155.  ULONG sigs,bsigs,psigs,wsigs; /* Buffers for signals */
  156.  struct ToolNode *tn;
  157.  BPTR fl,dfl;                  /* AmigaDOS Filehandles */
  158.  
  159.                             /* break signals */
  160.  bsigs=SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F;
  161.  psigs=1L<<MyMP->mp_SigBit; /* port signal */
  162.  sigs=bsigs|psigs;          /* merge signals */
  163.  
  164.  while (!end) /* main loop */
  165.   {
  166.    register struct AppMessage *msg; /* pointer to received message */
  167.    register ULONG gotsigs;          /* received signals */
  168.  
  169.    gotsigs=Wait(sigs); /* Wait for specified signals */
  170.  
  171.    if (gotsigs&bsigs)  /* Got break signals? */
  172.     end=TRUE;          /* Yes, quit program */
  173.  
  174.    if (gotsigs&psigs)  /* Message arrived at message port? */
  175.     while (msg=GetMsg(MyMP)) /* Empty message queue */
  176.      {
  177.       switch(msg->am_Type)   /* Switch between message types */
  178.        {
  179.         case MTYPE_APPICON:      /* Icon action */
  180.          if (msg->am_NumArgs==0) /* Did the user double click my icon? */
  181.           {                      /* Yes! If window not open, open it */
  182.            if ((!windowopen) && (MyWindow=OpenWindow(&MyNewWindow)))
  183.             {
  184.              PrintActTools();   /* Print status line */
  185.              wsigs=1L<<MyWindow->UserPort->mp_SigBit; /* Window signals */
  186.              sigs|=wsigs;       /* Merge with other signals */
  187.              windowopen=TRUE;   /* Window is open */
  188.             }
  189.           }
  190.          else                    /* User dropped an icon on my icon */
  191.           {                      /* Add Workbench parameters to tool list */
  192.            if (!WBAddToolNode(msg->am_ArgList,msg->am_NumArgs))
  193.             DisplayBeep(NULL);
  194.            if (windowopen)       /* If window is open, actualize status line */
  195.             PrintActTools();
  196.           }
  197.          break;
  198.  
  199.         case MTYPE_APPMENUITEM: /* Menu action */
  200.          if (msg->am_ID==NULL)  /* Special menu item selected? */
  201.           end=TRUE;             /* Yes, quit program */
  202.          else                   /* No, scan list for selected tool */
  203.           for (tn=(struct ToolNode *) ToolList.lh_Head; tn->tn_Node.ln_Succ;
  204.                tn=(struct ToolNode *) tn->tn_Node.ln_Succ)
  205.            if (tn->tn_ID==msg->am_ID)
  206.             {                               /* Corresponding tool found */
  207.              register char *cp;
  208.  
  209.              fl=CurrentDir(tn->tn_DirLock); /* Change to tool's directory */
  210.              dfl=Open("NIL:",MODE_NEWFILE); /* Open dummy files for */
  211.              MyTags[0].ti_Data=dfl;         /* program I/O */
  212.              dfl=Open("NIL:",MODE_NEWFILE);
  213.              MyTags[1].ti_Data=dfl;
  214.              if (!(cp=tn->tn_RealName))     /* Get tool name */
  215.               cp=tn->tn_Name;
  216.              if (System(cp,MyTags)==-1)     /* Start tool */
  217.                DisplayBeep(NULL);
  218.              CurrentDir(fl);                /* Change to old directory */
  219.              break;                         /* quit loop */
  220.             }
  221.          break;
  222.        } /* end of switch(msg->am_Type) */
  223.  
  224.       ReplyMsg(msg); /* Reply message to sender */
  225.      } /* end of while(msg=GetMsg(MyMP)) */
  226.  
  227.    if (windowopen && (gotsigs&wsigs)) /* If window open, got window signal? */
  228.     {
  229.      CloseWindow(MyWindow); /* Yes, close window */
  230.      sigs&=~wsigs;          /* Remove window signals */
  231.      windowopen=FALSE;      /* Window is closed */
  232.     }
  233.   } /* end of main loop */
  234.  
  235.  /* If window is open, close it */
  236.  if (windowopen) CloseWindow(MyWindow);
  237.  
  238.  while (ActTools--) /* delete all entries on tool list */
  239.   {
  240.    tn=RemHead(&ToolList);              /* Remove node from list */
  241.    UnLock(tn->tn_DirLock);             /* Free directory */
  242.    RemoveAppMenuItem(tn->tn_MenuItem); /* Remove menu entry */
  243.    if (tn->tn_RealName) free(tn->tn_RealName); /* free memory */
  244.    free(tn->tn_Name);
  245.    free(tn);
  246.   }
  247.  
  248.  cleanup(99); /* exit program */
  249. }
  250.  
  251. /* Scan Workbench parameters and add them to tool list */
  252. BOOL WBAddToolNode(struct WBArg *wbarg, int numargs)
  253. {
  254.  int i;
  255.  BOOL rc=TRUE;
  256.  BPTR fl;
  257.  struct DiskObject *dobj;
  258.  
  259.  for (i=0; i<numargs; i++, wbarg++) /* Scan all parameters */
  260.   if ((wbarg->wa_Lock) && (strlen(wbarg->wa_Name)!=0)) /* Sanity check */
  261.    {
  262.     fl=CurrentDir(wbarg->wa_Lock);          /* Change to icon's directory */
  263.     if (dobj=GetDiskObject(wbarg->wa_Name)) /* Get icon data */
  264.      {
  265.       switch(dobj->do_Type) /* Perform action depending on icon type */
  266.        {
  267.         case WBTOOL: /* Icon is a Tool. Menu entry == real program name */
  268.          rc&=AddToolNode((struct FileLock *) wbarg->wa_Lock,wbarg->wa_Name,
  269.                          NULL);
  270.          break;
  271.         case WBPROJECT: /* Icon is a Project. Menu entry = icon name,
  272.                            real program name = default tool name */
  273.          rc&=AddToolNode((struct FileLock *) wbarg->wa_Lock,wbarg->wa_Name,
  274.                          dobj->do_DefaultTool);
  275.          break;
  276.         default:rc&=FALSE; /* Every other icon type is erroneous */
  277.                 break;
  278.        }
  279.       FreeDiskObject(dobj); /* Release icon data */
  280.      }
  281.     else
  282.      rc&=FALSE; /* Error: Can't get icon data */
  283.  
  284.     CurrentDir(fl); /* Change to old directory */
  285.    }
  286.   else
  287.    rc&=FALSE; /* Error: Bad Workbench parameter */
  288.  
  289.  return(rc); /* Return TRUE if no error */
  290. }
  291.  
  292. /* Add one program to tool list */
  293. BOOL AddToolNode(BPTR dir, char *name, char *realname)
  294. {
  295.  struct ToolNode *tn;
  296.  
  297.  if (ActTools>99) goto e1; /* Not more than 99 Tools :-) possible */
  298.  
  299.  if (!(tn=malloc(sizeof(struct ToolNode)))) /* Get memory for ToolNode */
  300.   goto e1;
  301.  
  302.  if (!(tn->tn_Name=malloc(strlen(name)+1))) /* Get memory for program name */
  303.   goto e2;
  304.  strcpy(tn->tn_Name,name); /* Copy name to allocated memory */
  305.  
  306.  if (realname) /* Does the program have a real name? */
  307.   {
  308.    if (!(tn->tn_RealName=malloc(strlen(realname)+1))) /* Allocate memory */
  309.     goto e3;
  310.    strcpy(tn->tn_RealName,realname); /* Copy real program name */
  311.   }
  312.  else
  313.   tn->tn_RealName=NULL; /* otherwise: Menu item = real program name */
  314.  
  315.  /* Add a new menu entry */
  316.  if (!(tn->tn_MenuItem=AddAppMenuItem(ActTools+1,NULL,tn->tn_Name,MyMP,NULL)))
  317.   goto e4;
  318.  
  319.  if (!(tn->tn_DirLock=DupLock(dir))) /* Duplicate directory lock */
  320.   goto e5;
  321.  
  322.  AddTail(&ToolList,tn); /* Add node to list */
  323.  tn->tn_ID=++ActTools;  /* Increment tool count */
  324.  return(TRUE);
  325.  
  326. e5: RemoveAppMenuItem(tn->tn_MenuItem);
  327. e4: if (tn->tn_RealName) free(tn->tn_RealName);
  328. e3: free(tn->tn_Name);
  329. e2: free(tn);
  330. e1: return(FALSE);
  331. }
  332.  
  333. /* Print status line into window */
  334. void PrintActTools(void)
  335. {
  336.  MyText.IText[14]=ActTools/10+'0'; /* Hack */
  337.  MyText.IText[15]=ActTools%10+'0';
  338.  PrintIText(MyWindow->RPort,&MyText,15,25);
  339. }
  340.  
  341. /* Final cleanup routine */
  342. void cleanup(int i)
  343. {
  344.  register struct Message *msg;
  345.  
  346.  switch(i)
  347.   {
  348.    case 99:
  349.            RemoveAppMenuItem(MyAppMenuItem);
  350.    case  5:RemoveAppIcon(MyAppIcon);
  351.    case  4:RemPort(MyMP);                           /* Remove message port */
  352.            while (msg=GetMsg(MyMP)) ReplyMsg(MyMP); /* Reply all messages */
  353.            DeleteMsgPort(MyMP);
  354.    case  3:CloseLibrary(IconBase);
  355.    case  2:CloseLibrary(WorkbenchBase);
  356.    case  1:break;
  357.   }
  358.  
  359.  if (i!=99) exit(i); /* error if i!=99 */
  360.  exit(RETURN_OK);    /* all o.k. */
  361. }
  362.