home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 5 / CD_Magazyn_EXEC_nr_5.iso / Programy / Programowanie / FetchRefs2.1.lha / FetchRefs2.1 / Source / GenerateIndex / GenerateIndex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-28  |  7.5 KB  |  318 lines

  1. #include "GenerateIndex.h"
  2.  
  3. extern struct WBStartup *_WBenchMsg;
  4.  
  5. const UBYTE VersTag[] = "$VER: GenerateIndex " VERSION " " DATE;
  6.  
  7. UBYTE Template[] =  "FROM/M,TO,SETTINGS,"
  8.             "RECURSIVELY/S,KEEPEMPTY/S,UNRECOGAREDOCS/S,"
  9.             "AUTODOC/S,"
  10.             "C/S,C_DEFINE/S,C_STRUCT/S,C_TYPEDEF/S,"
  11.             "E/S,E_CONST/S,E_OBJECT/S,E_PROC/S,"
  12.             "ASM/S,ASM_EQU/S,ASM_STRUCTURE/S,ASM_MACRO/S";
  13.  
  14. UBYTE CLI_Help[] = "\n"
  15.            "GenerateIndex [[FROM] {wildcard}] [TO <file>] [SETTINGS <file>]\n"
  16.            "\t[RECURSIVELY] [KEEPEMPTY] [UNRECOGAREDOCS]\n"
  17.            "\t[AUTODOC]\n"
  18.            "\t[C] [C_DEFINE] [C_STRUCT] [C_TYPEDEF]\n"
  19.            "\t[E] [E_CONST] [E_OBJECT] [E_PROC]\n"
  20.            "\t[ASM] [ASM_EQU] [ASM_STRUCTURE] [ASM_MACRO]\n"
  21.            "\n"
  22.            "- Not specifying FROM will open the GUI (ReqTools and Triton required)\n"
  23.            "- A script is handy for Shell usage\n"
  24.            "- Study the guide for more information\n"
  25.            "\n";
  26.  
  27. /* Structure for ReadArgs() to return parsed arguments into */
  28. struct {
  29.     STRPTR (*From)[];
  30.     STRPTR To;
  31.     STRPTR Settings;
  32.     LONG Recursively, KeepEmpty, UnrecogAreDocs;
  33.     LONG AutoDoc;
  34.     LONG C_Active, C_Define, C_Struct, C_Typedef;
  35.     LONG E_Active, E_Const, E_Object, E_Proc;
  36.     LONG Asm_Active, Asm_Equ, Asm_Structure, Asm_Macro;
  37. } StartupArgs;
  38.  
  39. APTR catalog;
  40. struct List FileList, EmptyList;
  41. struct RDArgs *Args;
  42. struct ReqToolsBase * ReqToolsBase;
  43. struct Library * GTLayoutBase, * LocaleBase;
  44.  
  45. UBYTE DataName[256];
  46.  
  47. /* __main() -- handle parsing of WB/Shell arguments */
  48. long main (long argc, char ** argv)
  49. {
  50.     ReqToolsBase = (struct ReqToolsBase *) OpenLibrary ("reqtools.library", 38);
  51.     if (! ReqToolsBase)
  52.     CloseAll (ERROR_CUSTOM, "You need ReqTools version 38+ for the GUI!");
  53.  
  54.     GTLayoutBase = OpenLibrary ("gtlayout.library", 39);
  55.     if (! GTLayoutBase)
  56.     CloseAll (ERROR_CUSTOM, "You need gtlayout.library version 39+ for the GUI!");
  57.  
  58.     /* This will initialize the list (FileList) and create a pool (FilePool) */
  59.     InitializeFileList();       /* voir lists.c */
  60.     NewList (&EmptyList);
  61.  
  62.     if (LocaleBase = OpenLibrary ("locale.library",38))
  63.     catalog = OpenCatalog (NULL, "GenerateIndex.catalog", OC_Version, 1, TAG_DONE);
  64.  
  65.     if (_WBenchMsg)
  66.     {
  67.     struct DiskObject *diskobj;
  68.  
  69.     CurrentDir (_WBenchMsg->sm_ArgList->wa_Lock);
  70.     if (diskobj = GetDiskObject (_WBenchMsg->sm_ArgList->wa_Name))
  71.     {
  72.         STRPTR arg;
  73.  
  74.         if (arg = FindToolType (diskobj->do_ToolTypes, "TO"))
  75.         strncpy (DataName, arg, 255);
  76.  
  77.         /* Load specified or default settings file */
  78.         arg = FindToolType (diskobj->do_ToolTypes, "SETTINGS");
  79.         LoadSettings (arg ? arg : (STRPTR) "ENV:FetchRefs_GI.prefs");
  80.  
  81.         /* Release icon again */
  82.         FreeDiskObject (diskobj);
  83.  
  84.         /* Open GUI */
  85.         GoGUI ();
  86.     } else
  87.         CloseAll (IoErr());
  88.     } else
  89.     {
  90.     if (Args = AllocDosObject (DOS_RDARGS, NULL))
  91.     {
  92.         /* Activate extended help */
  93.         Args->RDA_ExtHelp = CLI_Help;
  94.  
  95.         /* Parse arguments */
  96.         StartupArgs.From = (APTR)1;
  97.         if (ReadArgs(Template, (LONG *)&StartupArgs, Args))
  98.         {
  99.         /* Minor hack to help PostMessage() */
  100.         if (StartupArgs.From == (APTR) 1)
  101.             StartupArgs.From = NULL;
  102.  
  103.         /* Do not bother to start if the user pressed CTRL-C during
  104.          * load time or during a 'GenerateIndex ?' help session.
  105.          */
  106.         if (CheckSignal (SIGBREAKF_CTRL_C))
  107.             CloseAll (0);
  108.  
  109.         /* Set options */
  110.         if (StartupArgs.Settings)
  111.             LoadSettings (StartupArgs.Settings);
  112.         else
  113.         {
  114.             if (StartupArgs.AutoDoc)
  115.             Settings.AutoDocPrf.Active = TRUE;
  116.  
  117.             if (StartupArgs.C_Active)
  118.             Settings.CPrf.Active = TRUE;
  119.             if (StartupArgs.C_Define)
  120.             Settings.CPrf.Define = TRUE;
  121.             if (StartupArgs.C_Struct)
  122.             Settings.CPrf.Struct = TRUE;
  123.             if (StartupArgs.C_Typedef)
  124.             Settings.CPrf.Typedef = TRUE;
  125.  
  126.             if (StartupArgs.E_Active)
  127.             Settings.EPrf.Active = TRUE;
  128.             if (StartupArgs.E_Const)
  129.             Settings.EPrf.Const = TRUE;
  130.             if (StartupArgs.E_Object)
  131.             Settings.EPrf.Object = TRUE;
  132.             if (StartupArgs.E_Proc)
  133.             Settings.EPrf.Proc = TRUE;
  134.  
  135.             if (StartupArgs.Asm_Active)
  136.             Settings.AsmPrf.Active = TRUE;
  137.             if (StartupArgs.Asm_Equ)
  138.             Settings.AsmPrf.Equ = TRUE;
  139.             if (StartupArgs.Asm_Structure)
  140.             Settings.AsmPrf.Structure = TRUE;
  141.             if (StartupArgs.Asm_Macro)
  142.             Settings.AsmPrf.Macro = TRUE;
  143.  
  144.             if (StartupArgs.Recursively)
  145.             Settings.Recursively = TRUE;
  146.             if (StartupArgs.KeepEmpty)
  147.             Settings.KeepEmpty = TRUE;
  148.             if (StartupArgs.UnrecogAreDocs)
  149.             Settings.UnknownAsAutoDoc = TRUE;
  150.         }
  151.  
  152.         if (StartupArgs.To)
  153.         {
  154.             if    (! StartupArgs.From)
  155.             strncpy (DataName, StartupArgs.To, 255);
  156.             else
  157.             LoadData (StartupArgs.To);
  158.         }
  159.  
  160.         if  (! StartupArgs.From)
  161.             GoGUI ();               /* voir GUI.c */
  162.         else if (StartupArgs.To)
  163.         {
  164.             /* Expand each wildcard (which can also be just a file)
  165.              * and generate index
  166.              */
  167.             ULONG count;
  168.             STRPTR path;
  169.             struct AnchorPath * fanchor;
  170.  
  171.             if (fanchor = AllocVec (sizeof(struct AnchorPath) + 256, MEMF_CLEAR))
  172.             {
  173.             fanchor->ap_Strlen = 256;
  174.             for (count = 0; path = (*StartupArgs.From)[count]; count++)
  175.             {
  176.                 if (! MatchFirst (path, fanchor))
  177.                 {
  178.                 do
  179.                 {
  180.                     STRPTR name;
  181.  
  182.                     if (name = FullName (fanchor->ap_Buf))
  183.                     {
  184.                     StartScanning();
  185.                     if (fanchor->ap_Info.fib_DirEntryType < 0)
  186.                         IndexFile (name, "");
  187.                     else
  188.                         IndexRecursive (fanchor->ap_Buf, "");
  189.                     StopScanning (TRUE);
  190.                     FreeVec (name);
  191.                     } else
  192.                     CloseAll (IoErr());
  193.                 } while (! MatchNext (fanchor));
  194.                 MatchEnd (fanchor);
  195.                 }
  196.             }
  197.             FreeVec (fanchor);
  198.  
  199.             /* Sort the scanned list and save it */
  200.             SortExecList (&FileList, SortCompareFunc, NULL);
  201.             SaveData (StartupArgs.To);
  202.             }
  203.             else
  204.             CloseAll (ERROR_NO_FREE_STORE);
  205.          }
  206.          else
  207.             CloseAll (ERROR_REQUIRED_ARG_MISSING);
  208.         }
  209.         else
  210.         CloseAll (IoErr());
  211.     }
  212.     else
  213.         CloseAll (ERROR_NO_FREE_STORE);
  214.     }
  215.  
  216.     CloseAll (0);
  217. }
  218.  
  219. /* CloseAll (LONG error [, STRPTR errtxt]) */
  220. void CloseAll (LONG error, ...)
  221. {
  222.     STRPTR errtxt;
  223.     va_list args;
  224.  
  225.     va_start (args, error);
  226.     errtxt = *(STRPTR *) args;
  227.     va_end (args);
  228.  
  229.     FreeFileList ();        /* voir lists.c */
  230.  
  231.     if (error)
  232.     {
  233.     if (error == ERROR_CUSTOM)
  234.         PostMessage (errtxt);
  235.     else
  236.     {
  237.         UBYTE errortxt[80];
  238.  
  239.         Fault (error, "GenerateIndex", errortxt, 80);
  240.         PostMessage (errortxt);
  241.     }
  242.     }
  243.  
  244.     if (Args)
  245.     {
  246.     FreeArgs (Args);
  247.     FreeDosObject (DOS_RDARGS, Args);
  248.     }
  249.  
  250.     CloseLibrary (&ReqToolsBase->LibNode);
  251.     CloseLibrary (GTLayoutBase);
  252.  
  253.     if (LocaleBase)
  254.     {    CloseCatalog (catalog);
  255.     CloseLibrary (LocaleBase);
  256.     }
  257.     exit (error ? 5 : 0);
  258. }
  259.  
  260. /* LoadSettings() */
  261. void LoadSettings (STRPTR file)
  262. {
  263.     BPTR f;
  264.  
  265.     if (f = Open (file, MODE_OLDFILE))
  266.     {
  267.     Read (f, &Settings, sizeof(Settings));
  268.     Close (f);
  269.     }
  270. }
  271.  
  272. /* SaveSettings() */
  273. void SaveSettings (STRPTR file)
  274. {
  275.     BPTR f;
  276.  
  277.     if (f = Open (file, MODE_NEWFILE))
  278.     {
  279.     Write (f, &Settings, sizeof(Settings));
  280.     Close (f);
  281.     }
  282. }
  283.  
  284. /* PostMessage() - does its best to get a message through to the user */
  285. void PostMessage (STRPTR fmt, ...)
  286. {
  287.     static struct EasyStruct msgreq =
  288.     {
  289.     sizeof(struct EasyStruct),
  290.     0,
  291.     "GenerateIndex " VERSION " by Anders Melchiorsen",
  292.     NULL,
  293.     "Okay"
  294.     };
  295.  
  296.     va_list args;
  297.  
  298.     msgreq.es_TextFormat = fmt;
  299.     va_start(args, fmt);
  300.  
  301.     /* Use a requester if the GUI is active */
  302.     if ((_WBenchMsg) || (! StartupArgs.From))
  303.     {
  304.     if (ReqToolsBase)
  305.         rtEZRequestA (fmt, "Okay", NULL, args, NULL);
  306.     else
  307.         EasyRequestArgs (NULL, &msgreq, NULL, args);
  308.     }
  309.     else
  310.     {
  311.     /* Print in Shell window */
  312.     VPrintf (fmt, args);
  313.     PutStr ("\n");
  314.     }
  315.  
  316.     va_end (args);
  317. }
  318.