home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / toolmanager_442.lzh / ToolManager / ToolManager.c < prev    next >
C/C++ Source or Header  |  1991-01-20  |  8KB  |  241 lines

  1. #include "ToolManager.h"
  2.  
  3. /* Library pointers */
  4. extern struct Library *SysBase,*IntuitionBase,*GfxBase; /* Autoinit Libs */
  5. struct Library *WorkbenchBase,*IconBase,*GadToolsBase;
  6.  
  7. /* Structures for icon */
  8. extern struct DiskObject MyIcon;
  9. static struct AppIcon *MyAppIcon;
  10.  
  11. /* Name of the message port & icon */
  12. static char *MyName="ToolManager";
  13.  
  14. /* Tags for System() */
  15. extern struct TagItem MyTags[];
  16. #define SYSTAGS_IN  0
  17. #define SYSTAGS_OUT 1
  18.  
  19. /* miscellaneous */
  20. static struct AppMenuItem *MyAppMenuItem;
  21.  
  22. /* Workbench main entry point */
  23. void wbmain(struct WBStartup *wbarg)
  24. {
  25.  BPTR fl;
  26.  
  27.  fl=CurrentDir(wbarg->sm_ArgList->wa_Lock); /* Goto a valid directory */
  28.  startup();                                 /* common startup code */
  29.  CurrentDir(fl);                            /* Go back to old directory */
  30.                                             /* Process WB startup parameters */
  31.  WBAddToolNode(&wbarg->sm_ArgList[1],wbarg->sm_NumArgs-1);
  32.  mainloop();                                /* Go into main loop */
  33. }
  34.  
  35. /* CLI main entry point */
  36. void main(int argc, char **argv)
  37. {
  38.  int i;
  39.  BPTR fl;
  40.  
  41.  startup();           /* common startup code */
  42.  fl=CurrentDir(NULL); /* Get current directory lock */
  43.                       /* Process CLI startup parameters */
  44.  for (i=1; i<argc; i++) AddToolNode(fl,argv[i],NULL);
  45.  CurrentDir(fl);      /* Go back to old directory */
  46.  mainloop();          /* Go into main loop */
  47. }
  48.  
  49. /* Buffer length for one config file line */
  50. #define BUFLEN 100
  51.  
  52. /* Handle common startup operations */
  53. void startup(void)
  54. {
  55.  BPTR fl;
  56.  FILE *fh;                /* Filehandle for config file */
  57.  char ConfigLine[BUFLEN]; /* Buffer for one config file line */
  58.  
  59.  if (SysBase->lib_Version<36)  /* Sanity check */
  60.   {
  61.    puts("You need a Kickstart 2.0 or better to run ToolManager!");
  62.    exit(20);
  63.   }
  64.  
  65.  Forbid();                     /* Is "ToolManager" already running? */
  66.  MyMP=FindPort(MyName);
  67.  Permit();
  68.  if (MyMP)
  69.   {                            /* Yes, exit gracefully */
  70.    puts("ToolManager already running!");
  71.    exit(5);
  72.   }
  73.  
  74.  /* Open libraries */
  75.  if (!(WorkbenchBase=OpenLibrary(WORKBENCH_NAME,0)))
  76.   cleanup(1);
  77.  
  78.  if (!(IconBase=OpenLibrary(ICONNAME,0)))
  79.   cleanup(2);
  80.  
  81.  if (!(GadToolsBase=OpenLibrary("gadtools.library",0)))
  82.   cleanup(3);
  83.  
  84.  /* Create message port */
  85.  if (!(MyMP=CreateMsgPort()))
  86.   cleanup(4);
  87.  MyMP->mp_Node.ln_Pri=0;
  88.  MyMP->mp_Node.ln_Name=MyName;   /* Our name */
  89.  AddPort(MyMP);                  /* Announce our presence */
  90.  
  91.  /* Notify Workbench about icon */
  92.  if (!(MyAppIcon=AddAppIcon(NULL,NULL,MyName,MyMP,NULL,&MyIcon,NULL)))
  93.   cleanup(5);
  94.  
  95.  /* Notify Workbench about special menu item. If this item is selected,
  96.     the program will quit */
  97.  if (!(MyAppMenuItem=AddAppMenuItem(NULL,NULL,"Quit ToolManager",MyMP,NULL)))
  98.   cleanup(6);
  99.  
  100.  NewList(&ToolList);  /* Initialize tool list */
  101.  fl=CurrentDir(NULL); /* Get the current directory */
  102.  
  103.  if (fh=fopen(ConfigName,"r")) /* Scan config file */
  104.   {
  105.    while (!feof(fh)) /* if not end of file, read one line into buffer */
  106.     if (fgets(ConfigLine,BUFLEN,fh) && (strlen(ConfigLine)>1))
  107.      {
  108.       char *tp;
  109.  
  110.       ConfigLine[strlen(ConfigLine)-1]='\0'; /* Strip newline */
  111.       if (tp=strchr(ConfigLine,';'))         /* scan config line */
  112.        {
  113.         *tp='\0';                        /* Menu entry ; real program name */
  114.         AddToolNode(fl,ConfigLine,++tp);
  115.        }
  116.       else
  117.        AddToolNode(fl,ConfigLine,NULL);  /* Menu entry == real program name */
  118.      }
  119.    fclose(fh);  /* close config file */
  120.   }
  121.  CurrentDir(fl); /* go back to old directory */
  122. }
  123.  
  124. /* The main processing loop */
  125. void mainloop(void)
  126. {
  127.  BOOL end=TRUE;                  /* Flag for main loop */
  128.  BOOL windowopen=FALSE;          /* Flag for status window */
  129.  ULONG sigs,bsigs,psigs,wsigs;   /* Buffers for signals */
  130.  
  131.  bsigs=SIGBREAKF_CTRL_C|         /* break signals */
  132.        SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F;
  133.  psigs=1L<<MyMP->mp_SigBit;      /* port signal */
  134.  sigs=bsigs|psigs;               /* merge signals */
  135.  
  136.  while (end)                     /* main loop */
  137.   {
  138.    struct AppMessage *msg;       /* pointer to received message */
  139.    ULONG gotsigs;                /* received signals */
  140.  
  141.    gotsigs=Wait(sigs);           /* Wait for specified signals */
  142.  
  143.    if (gotsigs&bsigs)            /* Got break signals? */
  144.     end=FALSE;                   /* Yes, quit program */
  145.  
  146.    if (gotsigs&psigs)            /* Message arrived at message port? */
  147.     while (msg=GetMsg(MyMP))     /* Yes, empty message queue */
  148.      {
  149.       switch(msg->am_Type)       /* Switch between message types */
  150.        {
  151.         case MTYPE_APPWINDOW:    /* Window action */
  152.         case MTYPE_APPICON:      /* Icon action */
  153.          if (msg->am_NumArgs==0) /* Did the user double click my icon? */
  154.           {                      /* Yes! If window not open, open it */
  155.            if ((!windowopen) && (wsigs=OpenStatusWindow()))
  156.             {
  157.              sigs|=wsigs;        /* Merge with other signals */
  158.              windowopen=TRUE;    /* Window is open */
  159.             }
  160.           }
  161.          else                    /* User dropped an icon on my icon */
  162.           {                      /* Add Workbench parameters to tool list */
  163.            if (!WBAddToolNode(msg->am_ArgList,msg->am_NumArgs))
  164.             DisplayBeep(NULL);
  165.            if (windowopen)       /* Refresh status window if open */
  166.             RefreshStatusWindow();
  167.           }
  168.          break;
  169.  
  170.         case MTYPE_APPMENUITEM:  /* Menu action */
  171.          if (msg->am_ID==NULL)   /* Special menu item selected? */
  172.           end=FALSE;             /* Yes, quit program */
  173.          else                    /* No, scan list for selected tool */
  174.           {
  175.            struct ToolNode *tn;
  176.  
  177.            for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn))
  178.             if (tn->tn_ID==msg->am_ID)
  179.              {                                /* Corresponding tool found */
  180.               BPTR fl,dfh;                    /* AmigaDOS Filehandles */
  181.               char *cp;
  182.  
  183.               fl=CurrentDir(tn->tn_DirLock);  /* Change to tool's directory */
  184.               dfh=Open("NIL:",MODE_NEWFILE);  /* Open dummy files for */
  185.               MyTags[SYSTAGS_IN].ti_Data=dfh; /* program I/O */
  186.               dfh=Open("NIL:",MODE_NEWFILE);
  187.               MyTags[SYSTAGS_OUT].ti_Data=dfh;
  188.               if (!(cp=tn->tn_RealName))      /* Get tool name */
  189.                cp=tn->tn_Node.ln_Name;
  190.               if (System(cp,MyTags)==-1)      /* Start tool */
  191.                 DisplayBeep(NULL);
  192.               CurrentDir(fl);                 /* Change to old directory */
  193.               break;                          /* quit loop */
  194.              }
  195.           }
  196.          break;
  197.        } /* end of switch(msg->am_Type) */
  198.  
  199.       ReplyMsg(msg); /* Reply message to sender */
  200.      } /* end of while(msg=GetMsg(MyMP)) */
  201.  
  202.    if (windowopen && (gotsigs&wsigs)) /* If window open, got window signal? */
  203.     if (HandleWindowEvent())          /* Handle window event */
  204.     {                                 /* Window close event? */
  205.      CloseStatusWindow();             /* Yes, close window */
  206.      sigs&=~wsigs;                    /* Remove window signals */
  207.      windowopen=FALSE;                /* Window closed */
  208.     }
  209.   } /* end of main loop */
  210.  
  211.  /* If window open, close it */
  212.  if (windowopen) CloseStatusWindow();
  213.  
  214.  /* Exit program */
  215.  cleanup(99);
  216. }
  217.  
  218. /* Final cleanup routine */
  219. void cleanup(int i)
  220. {
  221.  register struct Message *msg;
  222.  
  223.  switch(i)
  224.   {
  225.    case 99:
  226.            RemoveTools();
  227.            RemoveAppMenuItem(MyAppMenuItem);
  228.    case  6:RemoveAppIcon(MyAppIcon);
  229.    case  5:RemPort(MyMP);                           /* Remove message port */
  230.            while (msg=GetMsg(MyMP)) ReplyMsg(MyMP); /* Reply all messages */
  231.            DeleteMsgPort(MyMP);
  232.    case  4:CloseLibrary(GadToolsBase);
  233.    case  3:CloseLibrary(IconBase);
  234.    case  2:CloseLibrary(WorkbenchBase);
  235.    case  1:break;
  236.   }
  237.  
  238.  if (i!=99) exit(i); /* error if i!=99 */
  239.  exit(RETURN_OK);    /* all o.k. */
  240. }
  241.