home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 125 / af125a.adf / Altabber.lzx / Altabber / src / NewReadArgs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-24  |  11.1 KB  |  324 lines

  1. /*
  2. **
  3. **  NewReadArgs() - 
  4. **  a shell/workbench transparent ReadArgs() interface
  5. **
  6. **  © 1997-98 by Stephan Rupprecht
  7. **  All rights reserved.
  8. **
  9. **  email: stephan.rupprecht@primus-online.de
  10. **
  11. **  FREEWARE - I am not responsible for any damage that
  12. **  is caused by the (mis)use of this program.
  13. **
  14. **  MaxonC++, OS2.04+
  15. **
  16. */
  17.  
  18. /*
  19. ** Slightly modified by Matteo Cortese
  20. ** to compile with Dice
  21. */
  22.  
  23. /*- INCLUDES & DEFINES -*/
  24. #include <dos/dos.h>
  25. #include <exec/memory.h>
  26. #include <workbench/startup.h>
  27. #include <workbench/workbench.h>
  28. #include <string.h>
  29. #include <clib/dos_protos.h>
  30. #include <clib/exec_protos.h>
  31. #include <clib/icon_protos.h>
  32. #include <clib/utility_protos.h>
  33.  
  34. #ifdef DEBUG
  35. #define bug     kprintf
  36. #define d(x)        (x)
  37. #else
  38. #define bug
  39. #define d(x)        ;
  40. #endif
  41.  
  42.  
  43. /****************************************************************************/
  44.  
  45. /*- NEWRDARGS STRUCTURE -*/
  46. struct NewRDArgs {
  47. // initialize these fields before calling NewReadArgs() !!!
  48.     STRPTR        Template;  // ReadArgs() template
  49.     STRPTR        ExtHelp;   // ExtHelp string
  50.     STRPTR        Window;    // workbench window -> eg. "CON:////Test"
  51.     LONG         *Parameters;    // where to store the data
  52.     LONG          FileParameter; // -1 = none, 0 = all
  53.     LONG          PrgToolTypesOnly;
  54.  
  55. // private data section
  56.     struct RDArgs    *RDArgs;   // RDArgs we give to ReadArgs()
  57.         struct RDArgs    *FreeArgs; // RDArgs we get from ReadArgs()
  58.  
  59.     APTR            Pool;
  60.  
  61.     BPTR          WinFH;        // i/o window stream
  62.     BPTR          OldInput; // old i/o streams
  63.     BPTR          OldOutput;
  64. };
  65.  
  66. /****************************************************************************/
  67.  
  68. const STRPTR NRDArgsID = "NewReadArgs 39.3 © 1997-1998 by Stephan Rupprecht";
  69.  
  70. /****************************************************************************/
  71.  
  72. void NewFreeArgs(struct NewRDArgs *);
  73. LONG NewReadArgs(struct WBStartup *, struct NewRDArgs *);
  74.  
  75. /****************************************************************************/
  76.  
  77. void NewFreeArgs(struct NewRDArgs *rdargs)
  78. {
  79.     d(bug("--- NewFreeArgs ---\n"));
  80.     FreeArgs(rdargs->FreeArgs);
  81.     d(bug("FreeArgs( rdargs->FreeArgs )\n"));
  82.     if(rdargs->RDArgs) 
  83.         FreeDosObject(DOS_RDARGS, rdargs->RDArgs);
  84.     d(bug("FreeDosObject( DOS_RDARGS, rdargs->RDArgs )\n"));
  85.     if(rdargs->WinFH) 
  86.     {
  87.         SelectOutput(rdargs->OldOutput);
  88.         Close(SelectInput(rdargs->OldInput));
  89.         d(bug("SelectOutput( .. )\nClose( ... )\n"));
  90.     }
  91.     
  92.     if(rdargs->Pool)
  93.         DeletePool(rdargs->Pool);
  94.  
  95.     d(bug("memory freed\n"));
  96.     d(bug("--- EXIT ---\n"));
  97. }
  98.  
  99. /****************************************************************************/
  100.  
  101. LONG IsArg(STRPTR template, STRPTR keyword)
  102. {
  103.     UBYTE           buffer[128], c;
  104.     register STRPTR     ptr = buffer;
  105.     
  106.     while((c = *keyword++) && (c != '=')) *ptr++ = c;
  107.     
  108.     *ptr = 0;
  109.     
  110.     /*- checks if keyword is specified in template -*/
  111.     return(FindArg(template, buffer));
  112. }
  113.  
  114. /****************************************************************************/
  115.  
  116. LONG NewReadArgs(struct WBStartup *WBStartup, struct NewRDArgs *nrdargs)
  117. {   
  118.     d(bug("--- NewReadArgs ---\n"));
  119.     
  120.     nrdargs->RDArgs     = 
  121.     nrdargs->FreeArgs   = NULL;
  122.     nrdargs->WinFH      = NULL;
  123.     nrdargs->Pool       = NULL;
  124.     
  125.     if(nrdargs->RDArgs = (struct RDArgs *)AllocDosObject(DOS_RDARGS, NULL))
  126.     {
  127.         STRPTR  ToolWindow = nrdargs->Window;
  128.         
  129.         if(WBStartup) 
  130.         {
  131.             APTR            pool;
  132.             struct WBArg    *wbarg;
  133.             STRPTR          *Args, ptr;
  134.             LONG            MaxArgs = 1, *ArgLen, num = WBStartup->sm_NumArgs,
  135.                             FileArgs = nrdargs->FileParameter, FArgNum = -1L;
  136.  
  137.             if(!(ptr = nrdargs->Template))
  138.                 return(ERROR_BAD_TEMPLATE);
  139.             
  140.             /*- count max number of args -*/
  141.             while(*ptr)
  142.             {
  143.                 if(*ptr++ == ',') 
  144.                     MaxArgs++;
  145.             }
  146.             
  147.             ptr = nrdargs->Template;
  148.             
  149.             /*- how many file args? -*/
  150.             FileArgs = (FileArgs > num) ? num : ((FileArgs == -1) ? 0L : num);          
  151.             MaxArgs += FileArgs;
  152.             
  153.             if(!(pool = nrdargs->Pool = CreatePool(MEMF_ANY, 1024, 1024)) || !(Args = AllocPooled(pool, MaxArgs*sizeof(STRPTR)*2)))
  154.                 return(ERROR_NO_FREE_STORE);
  155.                 
  156.             for(num = 0L; num < (MaxArgs*2); num++)
  157.                 Args[num] = 0L;
  158.                 
  159.             ArgLen = (LONG *)&Args[MaxArgs];
  160.     
  161.             for(    wbarg = WBStartup->sm_ArgList, num = 0L;
  162.                     num < WBStartup->sm_NumArgs; 
  163.                     num++, wbarg++  )
  164.             {
  165.                 struct DiskObject   *dobj;
  166.                 BPTR            olddir;
  167.                 
  168.                 /*- get file-names if requested -*/
  169.                 if(FileArgs)
  170.                 {
  171.                     TEXT    buf[300];
  172.                     
  173.                     if(FArgNum < FileArgs && FArgNum >= 0L)
  174.                     {
  175.                         d(bug("ICON: %s\n", wbarg->wa_Name));
  176.                         
  177.                         if( NameFromLock(wbarg->wa_Lock, buf, sizeof(buf)) &&
  178.                             AddPart(buf, wbarg->wa_Name, sizeof(buf))   )
  179.                         {
  180.                             STRPTR  dst;
  181.                             LONG    len = strlen(buf) + 2L;
  182.                             if(Args[FArgNum] = dst = AllocPooled(pool, len))
  183.                             {
  184.                                 CopyMem(buf, (dst+1), len-2L);
  185.                                 *dst = dst[len-1] = '"';
  186.                                 
  187.                                 ArgLen[FArgNum] = len;
  188.                             }
  189.                             else return(ERROR_NO_FREE_STORE);
  190.                         }
  191.                         else return(ERROR_LINE_TOO_LONG);
  192.                     }
  193.                     
  194.                     FArgNum++;
  195.                 }
  196.                                     
  197.                 if(nrdargs->PrgToolTypesOnly && num)
  198.                     continue;
  199.                 
  200.                 olddir = CurrentDir(wbarg->wa_Lock);
  201.                 
  202.                 /*- get tooltypes from .info file -*/
  203.                 if(dobj = GetDiskObject(wbarg->wa_Name))
  204.                 {
  205.                     if(dobj->do_Type == WBTOOL || dobj->do_Type == WBPROJECT)
  206.                     {
  207.                         STRPTR  *tarray = (STRPTR *)dobj->do_ToolTypes;
  208.                         
  209.                         while(*tarray)
  210.                         {
  211.                             if(**tarray != '(')
  212.                             {
  213.                                 STRPTR  src = *tarray;
  214.                                 LONG    i;
  215.                                 
  216.                                 d(bug("tt: %s\n", *tarray));
  217.                                                                 
  218.                                 /*- valid arg ? -*/
  219.                                 if((i = IsArg(ptr, src)) > -1)
  220.                                 {           
  221.                                     STRPTR  dst;
  222.                                     LONG    len;
  223.                                     
  224.                                     i += FileArgs;
  225.                                     if(Args[i] = dst = AllocPooled(pool, (len = strlen(src))+2L))
  226.                                     {
  227.                                         /*- copy arg -*/
  228.                                         while(*src)
  229.                                         {
  230.                                             if(((*dst++ = *src++) == '=') && (*src != '"'))
  231.                                             {
  232.                                                 *dst++ = Args[i][len+1] = '"';
  233.                                                 len+=2;
  234.                                             }
  235.                                         }
  236.                                                                             
  237.                                         ArgLen[i] = len;
  238.                                     }
  239.                                     else return(ERROR_NO_FREE_STORE);
  240.                                 }
  241.                                 /*- arg not specified in template, check for WINDOW tooltype -*/
  242.                                 else if(!IsArg("WINDOW", src))
  243.                                 {                                                                   
  244.                                     if((i = strlen(src)-6L) > 1L)
  245.                                     {
  246.                                         if(ToolWindow = AllocPooled(pool, i))
  247.                                             CopyMem((src+7L), ToolWindow, i);
  248.                                     }
  249.                                     else ToolWindow = "CON:";
  250.                                 }
  251.                             }
  252.  
  253.                             tarray++;
  254.                         }
  255.                     }
  256.                     
  257.                     FreeDiskObject(dobj);
  258.                 }
  259.                 
  260.                 CurrentDir(olddir);
  261.             }
  262.             
  263.             /*- now copy all given args to a single line -*/
  264.             for(num = FileArgs = 0; FileArgs < MaxArgs; FileArgs++)
  265.                 num += ArgLen[FileArgs];
  266.                                 
  267.             if(num)
  268.             {
  269.                 nrdargs->RDArgs->RDA_Source.CS_Length = (num+=MaxArgs);
  270.  
  271.                 if(nrdargs->RDArgs->RDA_Source.CS_Buffer = ptr = AllocPooled(pool, num+1))
  272.                 {    
  273.                     for(FileArgs = 0; FileArgs < MaxArgs; FileArgs++)
  274.                     {
  275.                         if(num = ArgLen[FileArgs])
  276.                         {
  277.                             CopyMem(Args[FileArgs], ptr, num);
  278.                             ptr += num;
  279.                             *ptr++ = ' ';
  280.                         }
  281.                     }
  282.                 }
  283.                 else return(ERROR_NO_FREE_STORE);
  284.                 
  285.                 *(ptr-1) = '\n';
  286.                 *ptr = '\0'; // not really needed
  287.                 
  288.                 d(bug("CS_Buffer: %s", nrdargs->RDArgs->RDA_Source.CS_Buffer));
  289.             }           
  290.         }
  291.         
  292.         /*- call ReadArgs() -*/
  293.         nrdargs->RDArgs->RDA_ExtHelp = nrdargs->ExtHelp;
  294.         if(!(nrdargs->FreeArgs = ReadArgs(nrdargs->Template, nrdargs->Parameters, nrdargs->RDArgs)))
  295.         {
  296.             d(bug("ReadArgs() error\n"));
  297.             return(IoErr());
  298.         }
  299.         
  300.         d(bug("ReadArgs() okay\n"));
  301.         
  302.         /*- when started from wb, open window if requested -*/
  303.         if(ToolWindow && WBStartup)
  304.         {
  305.             d(bug("WINDOW has been defined\n"));
  306.             if(nrdargs->WinFH = Open(ToolWindow, MODE_READWRITE))
  307.             {
  308.                 d(bug("Opened WINDOW=%s\n", ToolWindow));
  309.                 nrdargs->OldInput = SelectInput(nrdargs->WinFH);
  310.                 nrdargs->OldOutput = SelectOutput(nrdargs->WinFH);
  311.             }
  312.             else return(IoErr());
  313.         }
  314.     }
  315.     else return(ERROR_NO_FREE_STORE);
  316.     
  317.     d(bug("--- EXIT ---\n"));
  318.                 
  319.     return(RETURN_OK);
  320. }
  321.  
  322. /****************************************************************************/
  323.  
  324.