home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / ui_utils / toolman.lzh / ToolManager / Source / toolmanager.c < prev    next >
C/C++ Source or Header  |  1991-12-31  |  12KB  |  401 lines

  1. /*
  2.  * toolmanager.c   V1.5
  3.  *
  4.  * startup routines
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Library pointers */
  12. extern struct Library *SysBase,*IntuitionBase,*GfxBase; /* Autoinit Libs */
  13. extern struct Library *LayersBase,*GadToolsBase;        /* Autoinit Libs */
  14. struct Library *WorkbenchBase,*IconBase,*CxBase;
  15.  
  16. /* Tooltypes "YES" string */
  17. static char YesString[]="YES";
  18.  
  19. /* ReadArgs() stuff */
  20. static char Template[]="NOICON/S,ICONNAME/K,ICONFILE/K,CONFIG/K,OUTPUT/K,"\
  21.                        "PATH/K,CX_POPUP/S,CX_POPKEY/K,CX_PRIORITY/K/N,"\
  22.                        "NOREQ/S,DELAY/K/N,Tools/M";
  23. static long defcxprio=0;
  24. static struct { /* All entries in this array MUST be longwords! */
  25.                long noicon;
  26.                char *iconname;
  27.                char *iconfile;
  28.                char *config;
  29.                char *output;
  30.                char *path;
  31.                long popup;
  32.                char *popkey;
  33.                long *cxprio;
  34.                long noreq;
  35.                long *delay;
  36.                char **tools;
  37.               } def={FALSE,NULL,NULL,NULL,NULL,NULL,FALSE,NULL,&defcxprio,
  38.                      FALSE,NULL,NULL};
  39.  
  40. /* miscellaneous */
  41. extern char InternalConfigName[];
  42. extern struct NewBroker nb;
  43. static struct DiskObject *dobj=NULL;
  44. static struct AppMenuItem *QTMAppMenuItem;
  45. static APTR OldConsoleTask=NULL;
  46.  
  47. /* Open libraries and other stuff */
  48. static BOOL openstuff(void)
  49. {
  50.  long i;
  51.  
  52.  if (SysBase->lib_Version<36)  /* Sanity check */
  53.   {
  54.    puts("You need a Kickstart 2.0 or better to run " DEFPROGNAME "!");
  55.    exit(20);
  56.   }
  57.  
  58.  /* Open libraries */
  59.  for (i=0; i<20; i++) /* Try it 20 times. Workbench can be sloooowwww.... */
  60.   {
  61.    if (WorkbenchBase=OpenLibrary(WORKBENCH_NAME,36)) break;
  62.    Delay(50);
  63.   }
  64.  if (!WorkbenchBase) cleanup(1);
  65.  
  66.  if (!(IconBase=OpenLibrary(ICONNAME,0)))
  67.   cleanup(2);
  68.  
  69.  if (!(CxBase=OpenLibrary("commodities.library",36)))
  70.   cleanup(3);
  71.  
  72.  Forbid();                     /* Is "ToolManager" already running? */
  73.  MyMP=FindPort(MyName);
  74.  Permit();
  75.  if (MyMP) return(TRUE);       /* Yes, don't start again! */
  76.  
  77.  if (!(MyMP=CreateMsgPort()))  /* No, create message port */
  78.   cleanup(4);
  79.  MyMP->mp_Node.ln_Pri=0;
  80.  MyMP->mp_Node.ln_Name=MyName; /* Our name */
  81.  AddPort(MyMP);                /* Announce our presence */
  82.  
  83.  /* Create Commodities Broker port */
  84.  if (!(MyBrokerPort=CreateMsgPort()))
  85.   cleanup(5);
  86.  MyBrokerPort->mp_Node.ln_Pri=0;
  87.  MyBrokerPort->mp_Node.ln_Name="TM Broker Port";
  88.  nb.nb_Port=MyBrokerPort;
  89.  
  90.  /* Create Broker */
  91.  if (!(MyBroker=CxBroker(&nb,NULL)))
  92.   cleanup(6);
  93.  
  94.  SetConfigFileName(InternalConfigName); /* Copy config file name */
  95.  return(FALSE);                /* No, start ToolManager */
  96. }
  97.  
  98. /* Init tool stuff */
  99. static void inittool(void)
  100. {
  101.  NewList(&ToolList);                    /* Initialize tool list */
  102.  
  103.  /* Notify Workbench about special menu items. */
  104.  /* 1. If this item is selected, the program will quit */
  105.  if (!(QTMAppMenuItem=AddAppMenuItemA(0,NULL,"Quit ToolManager",MyMP,NULL)))
  106.   cleanup(7);
  107.  
  108.  /* 2. If this item is selected, the status window will open */
  109.  if (!(OTWAppMenuItem=AddAppMenuItemA(1,NULL,"Open TM Window",MyMP,NULL)))
  110.   cleanup(8);
  111. }
  112.  
  113. /* Send an AppMessage with our parameters to a running ToolManager process */
  114. static void SendParameters(LONG nargs, struct WBArg *arg)
  115. {
  116.  struct MsgPort *sp,*rp;
  117.  struct AppMessage *msg;
  118.  
  119.  /* Allocate memory for AppMessage */
  120.  if (!(msg=malloc(sizeof(struct AppMessage)))) goto e1;
  121.  
  122.  /* Create a reply port */
  123.  if (!(rp=CreateMsgPort())) goto e2;
  124.  
  125.  /* Build AppMessage */
  126.  msg->am_Message.mn_Node.ln_Type=NT_MESSAGE;
  127.  msg->am_Message.mn_Node.ln_Pri=0;
  128.  msg->am_Message.mn_ReplyPort=rp;
  129.  msg->am_Type=MTYPE_APPICON;
  130.  msg->am_NumArgs=nargs;
  131.  msg->am_ArgList=arg;
  132.  
  133.  Forbid();                     /* Find "ToolManager" message port */
  134.  sp=FindPort(MyName);
  135.  if (sp) PutMsg(sp,(struct Message *) msg); /* Send AppMessage */
  136.  Permit();
  137.  
  138.  if (sp)
  139.   {                            /* We have send the message */
  140.    WaitPort(rp);               /* Wait on the reply */
  141.    GetMsg(rp);                 /* Remove reply from port */
  142.   }
  143.  
  144.     DeleteMsgPort(rp);
  145. e2: free(msg);
  146. e1: return;
  147. }
  148.  
  149. /* Workbench main entry point */
  150. void wbmain(struct WBStartup *wbarg)
  151. {
  152.  BPTR fl;
  153.  ULONG delay=0;
  154.  
  155.  if (openstuff())         /* common startup code */
  156.   {                       /* ToolManager already running, send parameters */
  157.    if (wbarg->sm_NumArgs>1)
  158.     SendParameters(wbarg->sm_NumArgs-1,wbarg->sm_ArgList+1);
  159.    cleanup(4);            /* All done */
  160.   }
  161.  
  162.  /* Set global startup current directory */
  163.  StartupCD=wbarg->sm_ArgList->wa_Lock;
  164.  
  165.  /* Get the program icon */
  166.  fl=CurrentDir(StartupCD);
  167.  if (dobj=GetDiskObject(wbarg->sm_ArgList->wa_Name))
  168.   {
  169.    char *cp1,*cp2,**ttp=dobj->do_ToolTypes;
  170.  
  171.    /* Retreive ToolTypes from icon */
  172.    /*  1. CONFIG=<name> Set configuration file name */
  173.    if (cp1=FindToolType(ttp,"CONFIG")) SetConfigFileName(cp1);
  174.  
  175.    /*  2. CX_POPKEY=<key> Set commodities HotKey */
  176.    if (cp1=FindToolType(ttp,"CX_POPKEY")) PopUpHotKey=strdup(cp1);
  177.  
  178.    /*  3. CX_POPUP=YES|NO Show status window on startup */
  179.    if (cp1=FindToolType(ttp,"CX_POPUP"))
  180.     ShowStatusWindow=MatchToolValue(cp1,YesString);
  181.  
  182.    /*  4. CX_PRIORITY=<num> Set broker priority */
  183.    if (cp1=FindToolType(ttp,"CX_PRIORITY")) nb.nb_Pri=strtol(cp1,&cp2,10);
  184.  
  185.    /*  5. NOREQ Don't show requester */
  186.    ShowQuitReq=!FindToolType(ttp,"NOREQ");
  187.  
  188.    /*  6. DELAY=<num> Wait "num" seconds before adding the tools */
  189.    if (cp1=FindToolType(ttp,"DELAY")) delay=50*strtol(cp1,&cp2,10);
  190.  
  191.    /*  7. OUTPUT=<name> CLI Tool Output file */
  192.    if (cp1=FindToolType(ttp,"OUTPUT")) CLIOutputFile=strdup(cp1);
  193.  
  194.    /*  8. PATH=<name> Global CLI Tool path string */
  195.    if (cp1=FindToolType(ttp,"PATH")) GlobalPath=strdup(cp1);
  196.  
  197.    /*  9. ICONNAME=<name> Name of the AppIcon */
  198.    if (cp1=FindToolType(ttp,"ICONNAME")) IconName=strdup(cp1);
  199.  
  200.    /* 10. NOICON Show AppIcon */
  201.    ShowIcon=!FindToolType(ttp,"NOICON");
  202.  
  203.    /* 11. INTERNALICON Use the internal icon as AppIcon */
  204.    if (ShowIcon && !FindToolType(ttp,"INTERNALICON"))
  205.     MyIcon=dobj; /* Disk icon will be used as AppIcon */
  206.    else
  207.     { /* No icon or the internal icon will be used */
  208.      FreeDiskObject(dobj);
  209.      dobj=NULL;
  210.     }
  211.   }
  212.  CurrentDir(fl);
  213.  
  214.  /* Set defaults if needed */
  215.  if (!PopUpHotKey) PopUpHotKey=DefaultPopUpHotKey;
  216.  if (!CLIOutputFile) CLIOutputFile=DefaultCLIOutputFile;
  217.  if (!IconName) IconName=MyName;
  218.  
  219.  /* Init tool stuff */
  220.  if (delay) Delay(delay); /* I hope this fixes ALL mysterious crashes... */
  221.  inittool();
  222.  
  223.  /* Process WB startup parameters */
  224.  WBAddToolNode(wbarg->sm_ArgList+1,wbarg->sm_NumArgs-1);
  225.  
  226.  /* Read configuration file */
  227.  fl=CurrentDir(StartupCD);
  228.  ReadConfigFile(ConfigName,TRUE);
  229.  CurrentDir(fl);
  230.  
  231.  mainloop(); /* Go into main loop */
  232. }
  233.  
  234. /* CLI main entry point */
  235. void main(int argc, char **argv)
  236. {
  237.  struct RDArgs *rda;
  238.  struct Process *pr=FindTask(NULL);
  239.  
  240.  /* Help requested? */
  241.  if ((argc>1) && (*argv[1]=='?'))
  242.   { /* Print out usage, but DON'T start ToolManager */
  243.    puts("");
  244.    puts(CopyrightNote);
  245.    fputs("\nUsage: " DEFPROGNAME " ",stdout);
  246.    puts(Template);
  247.    puts("\n Parameter          Description                         Default");
  248.    puts(" NOICON             Don't show the program icon         Show icon");
  249.    puts(" ICONNAME <string>  Name of the program icon            " DEFPROGNAME);
  250.    puts(" ICONFILE <file>    File name for the program icon      Internal icon");
  251.    puts(" CONFIG <file>      Name of configuration file          " DEFCONFIGNAME);
  252.    puts(" OUTPUT <file>      Name of the CLI output file         (see docs)");
  253.    puts(" PATH <pathlist>    List of directories for CLI path    No path");
  254.    puts(" CX_POPUP           Open status window after startup    Don't open");
  255.    puts(" CX_POPKEY <key>    Commodities HotKey definition       \"" DEFPOPUPHOTKEY "\"");
  256.    puts(" CX_PRIORITY <num>  Commodities priority                0");
  257.    puts(" NOREQ              Don't show quit requester           Show requester");
  258.    puts(" DELAY <num>        Wait \"num\" seconds before startup   Don´t wait");
  259.    puts(" Tools              Name of programs to add to the menu");
  260.    puts("\nRequires Kickstart 2.0 or better!");
  261.    exit(0);
  262.   }
  263.  
  264.  freopen("*","w",stderr);     /* Reopen the stderr for banner line */
  265.  fputs(CopyrightNote,stderr); /* Put out banner line (only in CLI) */
  266.  fputc('\n',stderr);
  267.  fclose(stderr);
  268.  
  269.  /* Set global startup current directory */
  270.  StartupCD=CurrentDir(NULL);
  271.  CurrentDir(StartupCD);
  272.  
  273.  if (openstuff())             /* common startup code */
  274.   {                           /* ToolManager already running, send parameters */
  275.    struct WBArg *wa;
  276.  
  277.    if (argc<2) cleanup(4);    /* No parameters to send */
  278.  
  279.    /* Allocate memory for WB parameters */
  280.    if (!(wa=malloc(sizeof(struct WBArg)*(argc-1)))) cleanup(4);
  281.  
  282.    /* Process CLI startup parameters */
  283.    if (rda=ReadArgs(Template,(LONG *) &def,NULL))
  284.     {
  285.      /* Build WBArgs */
  286.      if (argv=def.tools)
  287.       {
  288.        register struct WBArg *wat=wa;
  289.  
  290.        argc=0;
  291.        while (*argv)              /* Scan list of tools */
  292.         {
  293.          wat->wa_Lock=StartupCD;  /* Copy parameters */
  294.          wat->wa_Name=*argv++;
  295.          wat++;
  296.          argc++;
  297.         }
  298.  
  299.        if (argc>0) SendParameters(argc,wa); /* Send parameters */
  300.       }
  301.      FreeArgs(rda);           /* Free RDArgs */
  302.     }
  303.  
  304.    free(wa);                  /* Free WB parameters */
  305.    cleanup(4);                /* All done */
  306.   }
  307.  
  308.  /* Process CLI startup parameters */
  309.  rda=ReadArgs(Template,(LONG *) &def,NULL);
  310.  
  311.  /* Set internal values according to command line parameters */
  312.  ShowIcon=!def.noicon;
  313.  if (def.iconname) IconName=strdup(def.iconname);
  314.  if (!IconName) IconName=MyName;
  315.  if (ShowIcon && def.iconfile && (dobj=GetDiskObject(def.iconfile)))
  316.   MyIcon=dobj;
  317.  if (def.config) SetConfigFileName(def.config);
  318.  if (def.output) CLIOutputFile=strdup(def.output);
  319.  if (!CLIOutputFile) CLIOutputFile=DefaultCLIOutputFile;
  320.  if (def.path) GlobalPath=strdup(def.path);
  321.  ShowStatusWindow=def.popup;
  322.  if (def.popkey) PopUpHotKey=strdup(def.popkey);
  323.  if (!PopUpHotKey) PopUpHotKey=DefaultPopUpHotKey;
  324.  nb.nb_Pri=*def.cxprio;
  325.  ShowQuitReq=!def.noreq;
  326.  
  327.  /* I hope this fixes ALL mysterious crashes... */
  328.  if (def.delay) Delay(*def.delay*50);
  329.  
  330.  /* Init tool stuff */
  331.  inittool();
  332.  
  333.  /* Add every other parameter as CLI tool */
  334.  if (argv=def.tools)
  335.   {
  336.    struct ConfigBlock *cb;
  337.  
  338.    if (cb=malloc(sizeof(struct ConfigBlock))) /* Get memory */
  339.     {
  340.      /* Init config block */
  341.      InitConfigBlock(cb);
  342.      cb->cb_Type=TNTYPE_CLI;
  343.  
  344.      while (*argv)
  345.       {
  346.        strncpy(cb->cb_Alias,*argv++,BUFLEN-1);
  347.        AddToolNode(cb,StartupCD);
  348.       }
  349.  
  350.      free(cb);
  351.     }
  352.   }
  353.  
  354.  /* Free RDArgs */
  355.  if (rda) FreeArgs(rda);
  356.  
  357.  ReadConfigFile(ConfigName,TRUE); /* Read configuration file */
  358.  
  359.  fclose(stdin);               /* Detach from console window */
  360.  fclose(stdout);
  361.  OldConsoleTask=pr->pr_ConsoleTask;
  362.  pr->pr_ConsoleTask=NULL;
  363.  mainloop();                  /* Go into main loop */
  364. }
  365.  
  366. /* Final cleanup routine */
  367. void cleanup(int i)
  368. {
  369.  register struct Message *msg;
  370.  
  371.  switch(i)
  372.   {
  373.    case 99:
  374.    case 10:if (MyAppIcon) RemoveAppIcon(MyAppIcon);
  375.            if (dobj) FreeDiskObject(dobj);
  376.    case  9:RemoveTools();
  377.            if (OTWAppMenuItem) RemoveAppMenuItem(OTWAppMenuItem);
  378.    case  8:RemoveAppMenuItem(QTMAppMenuItem);
  379.    case  7:DeleteCxObjAll(MyBroker);                /* Remove CX Objects */
  380.    case  6:while (msg=GetMsg(MyBrokerPort)) ReplyMsg(msg);
  381.            DeleteMsgPort(MyBrokerPort);
  382.    case  5:RemPort(MyMP);                           /* Remove message port */
  383.            while (msg=GetMsg(MyMP)) ReplyMsg(msg);  /* Reply all messages */
  384.            DeleteMsgPort(MyMP);
  385.    case  4:CloseLibrary(CxBase);
  386.    case  3:CloseLibrary(IconBase);
  387.    case  2:CloseLibrary(WorkbenchBase);
  388.    case  1:break;
  389.   }
  390.  
  391.  /* Did we have a console window? */
  392.  if (OldConsoleTask)
  393.   {
  394.    struct Process *pr=FindTask(NULL);
  395.  
  396.    pr->pr_ConsoleTask=OldConsoleTask; /* Yes, restore old value */
  397.   }
  398.  
  399.  exit(RETURN_OK);    /* all o.k. */
  400. }
  401.