home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / WBStartup+ / Source / WBStartup+OS2.x / WBStartup+.c < prev    next >
C/C++ Source or Header  |  1998-10-26  |  14KB  |  459 lines

  1. #define VERSION   "2.8"
  2. #define DATENUM   __AMIGADATE__
  3. #define DATETEXT  "December 12, 1996"
  4. #define COPYRIGHT "Copyright © 1996 John Hughes"
  5.  
  6. //#define DEBUG 1
  7.  
  8. /*********************************************************************************/
  9.  
  10. #include <exec/types.h>
  11. #include <clib/dos_protos.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <utility/tagitem.h>    /* TAG_DONE */
  15. #include <dos/exall.h>
  16. #include <clib/alib_protos.h>   /* List */
  17. #include <clib/exec_protos.h>   /* AllocVec() */
  18. #include <string.h>
  19. #include <exec/memory.h>        /* MEMF_PUBLIC */
  20. #include <workbench/startup.h>
  21. #include <dos/dostags.h>
  22. #include <prefs/wbpattern.h>
  23.  
  24. #include "WBStartup+.h"
  25.  
  26. #include <exec/execbase.h>
  27.  
  28. #include <proto/dos.h>
  29. #include <proto/icon.h>
  30. #include <proto/exec.h>
  31. #include <proto/intuition.h>
  32. #include <proto/commodities.h>
  33. #include <proto/utility.h>
  34. #include <proto/input.h>
  35. #include <proto/gadtools.h>
  36. #include <proto/graphics.h>
  37. #include <proto/layers.h>
  38.  
  39. #include "ReadKeyboard.h"
  40. #include "ProgressWindow.h"
  41.  
  42. #include <proto/wbstart.h>
  43. struct Library *WBStartBase;
  44.  
  45.  
  46. char WBSTARTUPPLUS[] = "WBStartup+";
  47.  
  48. char const version[]="\0$VER: WBStartup+ "VERSION" "DATENUM"\r\n"COPYRIGHT"\r\njohughes@heartland.bradley.edu\r\nThis is th OS2.x version.";
  49.  
  50.  
  51. void GetFilenames(struct List *filenamequeue, char *directory, BOOL ShowWindow);
  52. void FreeFilenames(struct List *filenamequeue);
  53. void RunPrograms(struct List *filenamequeue, struct WBStartupPrefs *prefs);
  54. void GetArguments(int argc, char **argv, struct WBStartupPrefs *prefs);
  55. void ShowRequester(STRPTR RequesterText);
  56. void DisplayVars(void);
  57. void FreeIcons(struct List *list);
  58. void RunPrefs(struct WBStartupPrefs *prefs);
  59. struct Node *FindNameNoCase(struct List *list, char *name);
  60. BOOL CheckSemaphore(void);
  61.  
  62. extern struct ExecBase *SysBase;
  63.  
  64. struct IntuitionBase *IntuitionBase;
  65. struct Library *CxBase, *IconBase, *UtilityBase, *GadToolsBase, *LayersBase;
  66. struct GfxBase *GfxBase;
  67.  
  68.  
  69. main (int argc, char **argv)
  70. {
  71.   struct List filenamequeue;
  72.   struct WBStartupPrefs prefs={NULL,NULL,0,0,0,0,0,0,0,0,0,0,0};
  73.  
  74.   if (SysBase->LibNode.lib_Version>=37)
  75.   {
  76.     if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37))
  77.     {
  78.       if (CxBase = OpenLibrary("commodities.library", 37L))
  79.       {
  80.         if (IconBase = OpenLibrary("icon.library", 36L))
  81.         {
  82.           if (UtilityBase = OpenLibrary("utility.library", 37L))
  83.           {
  84.             if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37L))
  85.             {
  86.               if (GadToolsBase = OpenLibrary("gadtools.library", 37L))
  87.               {
  88.                 if (LayersBase=OpenLibrary("layers.library",37L))
  89.                 {
  90.                   if (WBStartBase=OpenLibrary("wbstart.library",0L))
  91.                   {
  92.  
  93.                     GetArguments(argc, argv, &prefs);
  94.  
  95.                     if (!IsQualifierDepressed(&prefs))
  96.                     {
  97. //DisplayVars();
  98.                       if (CheckSemaphore())
  99.                       {
  100.                         NewList(&filenamequeue);
  101.  
  102.                         GetFilenames(&filenamequeue, prefs.ExecutePath, prefs.ShowWindow);
  103.  
  104.                         if (!IsListEmpty(&filenamequeue))
  105.                           RunPrograms(&filenamequeue, &prefs);
  106.  
  107.                         if (prefs.ShowWindow)
  108.                           FreeIcons(&filenamequeue);
  109.                       }
  110.                     }
  111.                     CloseLibrary(WBStartBase);
  112.                   }
  113.                   CloseLibrary(LayersBase);
  114.                 }
  115.                 CloseLibrary((struct Library *)GfxBase);
  116.               }
  117.               CloseLibrary(GadToolsBase);
  118.             }
  119.             CloseLibrary(UtilityBase);
  120.           }
  121.           CloseLibrary(IconBase);
  122.         }
  123.         CloseLibrary(CxBase);
  124.       }
  125.       CloseLibrary((struct Library *)IntuitionBase);
  126.     }
  127.   }
  128.   else
  129.     ShowRequester("WBStartup+ requires AmigaDOS 2.04 or greater.");
  130.  
  131.   exit(0);
  132. }
  133.  
  134. void GetFilenames(struct List *filenamequeue, char *directory, BOOL ShowWindow)
  135. {
  136.   struct ExAllControl *myexallctrl;  /* The ExAllControl structure */
  137.   struct ExAllData    *eadptr;       /* temp pointer to data structure */
  138.   BPTR mylock;                       /* Lock on the directory */
  139.   int more;                          /* Are there more entries ? */
  140.   struct ExAllData ExAllBuf[20];     /* hold the filenames and types */
  141.   struct Node *node;                 /* hold the filenames and proiority for the List */  /* Is actually WBStartupNode */
  142.   struct DiskObject *diskobj;        /* Pointer to the icon info */
  143.   char *toolstring;                  /* Pointer to a tooltype string */
  144.   char pathname[500];                /* path and filename of the icon */
  145.   char filename[32];                 /* parsed match string 2*length+2=22 */
  146.  
  147.  
  148.   if (mylock=Lock(directory,SHARED_LOCK))
  149.   {
  150.     if (myexallctrl=(struct ExAllControl *)AllocDosObjectTags(DOS_EXALLCONTROL,TAG_END))
  151.     {
  152.       myexallctrl->eac_LastKey = 0;
  153.       myexallctrl->eac_Entries = 0;
  154.       myexallctrl->eac_MatchString = NULL;
  155.       myexallctrl->eac_MatchFunc = NULL;
  156.  
  157.       do   /* go until more==0 */
  158.       {
  159.         more = ExAll(mylock, ExAllBuf, sizeof(ExAllBuf), ED_TYPE, myexallctrl);
  160.         if ((!more) && (IoErr() != ERROR_NO_MORE_ENTRIES))
  161.         {
  162.           ShowRequester("ExAll failed abnormally");//: %ld.", IoErr());
  163.           break;
  164.         }
  165.  
  166.         if (myexallctrl->eac_Entries)
  167.         {
  168.           eadptr=ExAllBuf;
  169.  
  170.           while (eadptr)
  171.           {
  172.             if (eadptr->ed_Type < 0)  /* make sure it is a file, not a directory, see end of dos/dosextens.h */
  173.             {
  174.               strcpy(filename, eadptr->ed_Name);
  175.  
  176.               /* truncate the filename if it is an icon, ie. remove the ".info" */
  177.               if (!Stricmp(filename+strlen(filename)-5,".info"))
  178.                 filename[strlen(filename) - 5] = NULL;  /* take the .info off of the string */
  179.               else
  180.                 filename[strlen(filename)] = NULL;  /* take the .info off of the string */
  181.  
  182.               /* See if the file is already in the list */
  183.               if (!FindNameNoCase(filenamequeue,filename))
  184.               {
  185.                 if (Stricmp(filename,WBSTARTUPPLUS))  /* Make sure we don't recursively run ourself */
  186.                 {
  187.                   strcpy(pathname,directory);
  188.                   AddPart(pathname, filename, 500);
  189.                   if (diskobj=GetDiskObjectNew(pathname))
  190.                   {
  191.                     if ((diskobj->do_Type==WBPROJECT) || (diskobj->do_Type==WBTOOL))  /* Make sure we got an icon that isn't a drawer icon */
  192.                     {
  193.                       if (node=(struct Node *)AllocVec(sizeof(struct WBStartupNode),MEMF_PUBLIC))
  194.                       {
  195.                         if (node->ln_Name=(char *)AllocVec(strlen(pathname)+1,MEMF_PUBLIC))
  196.                         {
  197.                           strcpy(node->ln_Name, filename);
  198.                           if (toolstring=FindToolType(diskobj->do_ToolTypes,"STARTPRI"))
  199.                             node->ln_Pri=atoi((char *)toolstring);
  200.                           else
  201.                             node->ln_Pri=0;
  202.                           ((struct WBStartupNode *)node)->StackSize = diskobj->do_StackSize;
  203.                           if (toolstring=FindToolType(diskobj->do_ToolTypes,"TOOLPRI"))
  204.                             ((struct WBStartupNode *)node)->toolpri = strtol(toolstring,NULL,10);
  205.                           else
  206.                             ((struct WBStartupNode *)node)->toolpri = 0;
  207.                           if (toolstring=FindToolType(diskobj->do_ToolTypes,"WAIT"))
  208.                             ((struct WBStartupNode *)node)->wait = strtoul(toolstring,NULL,10);
  209.                           else
  210.                             ((struct WBStartupNode *)node)->wait = 0;
  211.                           if (ShowWindow)
  212.                             ((struct WBStartupNode *)node)->diskobj = diskobj;
  213.                           else
  214.                             FreeDiskObject(diskobj);
  215.                           Enqueue(filenamequeue,node);
  216.                         }
  217.                       }
  218.                     }
  219.                     else
  220.                       FreeDiskObject(diskobj);
  221.                   }
  222.                 }
  223.               }
  224.             }
  225.             eadptr=eadptr->ed_Next;
  226.           }
  227.         }
  228.  
  229.       } while (more);
  230.  
  231.       FreeDosObject(DOS_EXALLCONTROL,myexallctrl);
  232.     }
  233.     else
  234.       ShowRequester("Not enough memory for ExAllControl.");
  235.     UnLock(mylock);
  236.   }
  237.   else
  238.     ShowRequester("Couldn't lock directory.");
  239. }
  240.  
  241. void FreeFilenames(struct List *filenamequeue)
  242. {
  243.   struct Node *worknode;
  244.   struct Node *nextnode;
  245.  
  246.   worknode=filenamequeue->lh_Head;
  247.   while (nextnode = worknode->ln_Succ)
  248.   {
  249.     FreeVec(worknode->ln_Name);
  250.     FreeVec(worknode);
  251.     worknode=nextnode;
  252.   }
  253. }
  254.  
  255. void RunPrograms(struct List *filenamequeue, struct WBStartupPrefs *prefs)
  256. {
  257.   struct Node *node;           /* used to go through the filenames queue */
  258.   BPTR fl;
  259.   BPTR olddirlock;
  260.   LONG numprograms=0,currentprogram=0;      // used for the progress bar
  261.   struct ProgressWindowData *windata=NULL;  // this must be initialized to NULL!
  262.  
  263.  
  264.   if (prefs->ShowWindow)
  265.   {
  266.     /* Count the # of programs to run */
  267.     for (node=filenamequeue->lh_Head; node->ln_Succ; node = node->ln_Succ)
  268.       numprograms++;
  269.     /* Open and set up the Progress Window */
  270.     windata=CreateProgressWindow(prefs);
  271.   }
  272.  
  273.   fl=Lock(prefs->ExecutePath,SHARED_LOCK);
  274.   olddirlock=CurrentDir(fl);
  275.  
  276.   for (node=filenamequeue->lh_Head; node->ln_Succ; node=node->ln_Succ)
  277.   {
  278.     currentprogram++;
  279.  
  280.     /* Update Progress Bar in Window */
  281.     if (windata)
  282.     {
  283.       UpdateProgressBar(windata->win,currentprogram,numprograms);
  284.       ShowIconImage(windata,((struct WBStartupNode *)node)->diskobj->do_Gadget.GadgetRender);
  285.       DisplayProgramName(windata,node->ln_Name, (prefs->Interactive) ? TRUE : FALSE);
  286.       /* Interactive Mode */
  287.       if (prefs->Interactive)
  288.         if (!InteractiveRunProgram(windata->win))
  289.           continue;  //Skip this icon
  290.     }
  291.  
  292.     WBStartTags(WBStart_Name, node->ln_Name, WBStart_DirectoryLock, fl, TAG_DONE);
  293.  
  294.     /* Wait the amount of time specified in the WAIT= tooltype */
  295.     if (((struct WBStartupNode *)node)->wait)
  296.       Delay(((struct WBStartupNode *)node)->wait*50);
  297.  
  298.   }
  299.   /* Free resources */
  300.   CurrentDir(olddirlock);
  301.   UnLock(fl);
  302.  
  303.   if (windata)
  304.     CloseProgressWindow(windata);
  305.  
  306.   /* All OK! */
  307.   return;
  308. }
  309.  
  310.  
  311. void GetArguments(int argc, char **argv, struct WBStartupPrefs *prefs)
  312. {
  313.   UBYTE **ttypes;
  314.  
  315.   ttypes = ArgArrayInit(argc,argv);
  316.   strcpy(prefs->ExecutePath , ArgString(ttypes,"ENABLEDPATH","SYS:WBStartup/WBStartup (Enabled)"));
  317.   strcpy(prefs->StoragePath , ArgString(ttypes,"DISABLEDPATH","SYS:WBStartup/WBStartup (Disabled)"));
  318.   prefs->ShowWindow = ((FindToolType(ttypes,"PROGRESSWINDOW")) ? TRUE : FALSE);
  319.   strcpy(prefs->PrefsPath , ArgString(ttypes,"PREFSPATH","SYS:Prefs/WBStartup+Prefs"));
  320.   strcpy(prefs->PubScreenName , ArgString(ttypes,"PUBSCREEN","Workbench"));
  321.   ArgArrayDone();
  322. }
  323.  
  324. void ShowRequester(STRPTR RequesterText)
  325. {
  326.   struct EasyStruct myRequestStruct={
  327.     sizeof (struct EasyStruct ),
  328.     0,
  329.     WBSTARTUPPLUS,
  330.     NULL,
  331.     "Ok"};
  332.  
  333.     myRequestStruct.es_TextFormat=RequesterText;
  334.     EasyRequestArgs(NULL,&myRequestStruct,NULL,NULL);
  335. }
  336.  
  337. /*
  338. #include <dos/dosextens.h>
  339. #include <dos/var.h>
  340. void DisplayVars(void)
  341. {
  342.   struct Process *pr;
  343.   struct LocalVar *node;
  344.   char   buffer[1000];
  345.   BPTR file;
  346.  
  347.   if (file=Open("CON:////",MODE_NEWFILE))
  348.   {
  349.     Write(file,"These are vars in WBStartup+:\n",30);
  350.     if (pr = (struct Process *)FindTask(NULL))
  351.     {
  352.       for (node = (struct LocalVar *)pr->pr_LocalVars.mlh_Head; node->lv_Node.ln_Succ; node = (struct LocalVar *)node->lv_Node.ln_Succ)
  353.       {
  354.         sprintf(buffer,"%s\n",node->lv_Node.ln_Name);
  355.         Write(file,buffer,strlen(buffer));
  356.       }
  357.       Delay(50*5);
  358.     }
  359.     Close(file);
  360.   }
  361. }
  362.  
  363. */
  364.  
  365.  
  366. void FreeIcons(struct List *list)
  367. {
  368.   struct Node *node;
  369.  
  370.   for (node=list->lh_Head; node->ln_Succ; node=node->ln_Succ)
  371.     FreeDiskObject(((struct WBStartupNode *)node)->diskobj);
  372. }
  373.  
  374. void RunPrefs(struct WBStartupPrefs *prefs)
  375. {
  376.   struct WBStartupNode node;
  377.   struct List list;
  378.   struct MsgPort *mp;
  379.   struct WBStartupPrefs newprefs;
  380.   char filename[35];
  381.  
  382.   /* Set up filename list, which only consists of the WBStartup+Prefs program */
  383.   NewList(&list);
  384.   node.diskobj=NULL;
  385.   node.StackSize=4096;
  386.   node.wait=0;
  387.   node.toolpri=0;
  388.   node.node.ln_Name=filename;
  389.   strcpy(filename,FilePart(prefs->PrefsPath));
  390.   AddHead(&list,&node);
  391.  
  392.   /* Set up New Prefs structure */
  393.   strncpy(newprefs.ExecutePath,prefs->PrefsPath,PathPart(prefs->PrefsPath)-prefs->PrefsPath);
  394.   newprefs.StoragePath[0]=NULL;
  395.   newprefs.ShowWindow=FALSE;
  396.   newprefs.Interactive=FALSE;
  397.   newprefs.PrefsPath[0]=NULL;
  398.  
  399.   RunPrograms(&list,&newprefs);
  400.  
  401.   if (mp=CreatePort(WBSTARTUPPLUS,0))
  402.   {
  403.     Wait( (1 << mp->mp_SigBit) | SIGBREAKF_CTRL_C);   /* Wait for WBStartup+Prefs to complete OR a CTRL-C */
  404.     DeletePort(mp);
  405.   }
  406. }
  407.  
  408. struct Node *FindNameNoCase(struct List *list, char *name)
  409. {
  410.   struct Node *node;
  411.   struct Node *result=NULL;
  412.  
  413.   for (node=list->lh_Head; node->ln_Succ; node = node->ln_Succ)
  414.     if (!Stricmp(name,node->ln_Name))
  415.     {
  416.       result = node;
  417.       break;
  418.     }
  419.   return(result);
  420. }
  421.  
  422. BOOL CheckSemaphore(void)
  423. {
  424.   /* RETURN TRUE if this is the 1st time WBStartup+ has been run, OR if the user says it is ok to run it again */
  425.   struct SignalSemaphore *sema;
  426.   char   *name;
  427.   BOOL ContinueRunning = TRUE;
  428.  
  429.   Forbid();
  430.   if (!FindSemaphore(WBSTARTUPPLUS))
  431.   {
  432.     if (name=(char *)AllocMem(11,MEMF_PUBLIC))
  433.     {
  434.       strcpy(name,WBSTARTUPPLUS);
  435.       if (sema=(struct SignalSemaphore *)AllocMem(sizeof(struct SignalSemaphore),MEMF_PUBLIC|MEMF_CLEAR))
  436.       {
  437.         sema->ss_Link.ln_Name=name;
  438.         AddSemaphore(sema);
  439.       }
  440.     }
  441.     Permit();
  442.   }
  443.   else  /* Semaphore was found, meaning WBStartup+ has already been run */
  444.   {
  445.     struct EasyStruct myRequestStruct={
  446.       sizeof (struct EasyStruct ),
  447.       0,
  448.       WBSTARTUPPLUS,
  449.       "Run WBStartup+ again?",
  450.       "Ok|Cancel"};
  451.  
  452.     ContinueRunning = (BOOL)EasyRequestArgs(NULL,&myRequestStruct,NULL,NULL);
  453.  
  454.     Permit();
  455.   }
  456.  
  457.   return(ContinueRunning);
  458. }
  459.