home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / text / hyper / ADtoHT2_0.lha / AdditionalDocs.c next >
Encoding:
C/C++ Source or Header  |  1995-03-16  |  16.8 KB  |  609 lines

  1. #include <proto/dos.h>
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <ctype.h>
  7.  
  8. /************************************************************************/
  9.  
  10. #include "main.h"
  11. #include "File.h"
  12. #include "AdditionalDocs.h"
  13. #include "Includes.h"
  14. #include "Autodocs.h"
  15. #include "FormatNode.h"
  16.  
  17. /************************************************************************/
  18.  
  19. char *GlobalTOCFilename;
  20.  
  21. /************************************************************************/
  22. /*                                                                      */
  23. /* Functions to be called from FormatNode() and PrintNode() for         */
  24. /* outputting the list of autodoc modules.                              */
  25. /*                                                                      */
  26. /************************************************************************/
  27.  
  28. struct AutodocsParameters
  29.   {
  30.     struct AVLStateInfo StateInfo;
  31.     struct AutodocModuleNode *AutodocModuleNode;
  32.   };
  33.  
  34. /************************************************************************/
  35.  
  36. static char *
  37. NextLabelAutodocs (char *PrevLabel, void *Parameters)
  38.  
  39. {
  40.   struct AutodocsParameters *Params;
  41.  
  42.   Params = Parameters;
  43.   if (!PrevLabel)
  44.     {
  45.       AVL_InitTraversal (&AutodocModuleTree, &Params->StateInfo);
  46.     }
  47.  
  48.   if ((Params->AutodocModuleNode = (struct AutodocModuleNode *) AVL_InOrder (&Params->StateInfo)))
  49.     {
  50.       return xstrdup(Params->AutodocModuleNode->AnyNode.Name);
  51.     }
  52.   return NULL;
  53. }
  54.  
  55. /************************************************************************/
  56.  
  57. static void 
  58. PrintLinkAutodocs (void *Parameters)
  59.  
  60. {
  61.   WPrintf ("\x22%s/MAIN\x22",
  62.        ((struct AutodocsParameters *) Parameters)->AutodocModuleNode->AutodocFileNode->LinkName);
  63. }
  64.  
  65. /************************************************************************/
  66. /*                                                                      */
  67. /* Functions to be called from FormatNode() and PrintNode() for         */
  68. /* outputting the list of header files.                                 */
  69. /*                                                                      */
  70. /************************************************************************/
  71.  
  72. struct HeadersParameters
  73. {
  74.   struct AVLStateInfo StateInfo;
  75.   struct IncludeFileNode *IncludeFileNode;
  76. };
  77.  
  78. /************************************************************************/
  79.  
  80. static char *
  81. NextLabelHeaders (char *PrevLabel, void *Parameters)
  82.  
  83. {
  84.   struct HeadersParameters *Params;
  85.  
  86.   Params = Parameters;
  87.   if (!PrevLabel)
  88.     {
  89.       AVL_InitTraversal (&IncludeFileTree, &Params->StateInfo);
  90.     }
  91.  
  92.   if ((Params->IncludeFileNode = (struct IncludeFileNode *) AVL_InOrder (&Params->StateInfo)))
  93.     {
  94.       return xstrdup(Params->IncludeFileNode->AnyNode.Name);
  95.     }
  96.   return NULL;
  97. }
  98.  
  99. /************************************************************************/
  100.  
  101. static void 
  102. PrintLinkHeaders (void *Parameters)
  103.  
  104. {
  105.   WPrintf ("\x22%s/MAIN\x22",
  106.       ((struct HeadersParameters *) Parameters)->IncludeFileNode->LinkName);
  107. }
  108.  
  109. /************************************************************************/
  110. /*                                                                      */
  111. /* Determine first letter of a name.                                    */
  112. /* One of 'a'..'z' or '_' is returned.                    */
  113. /*                                                                      */
  114. /************************************************************************/
  115.  
  116. static int 
  117. FirstLetter (struct AnyNode *AnyNode)
  118.  
  119. {
  120.   int c;
  121.  
  122.   c = tolower (AnyNode->Name[0]);
  123.   if (c < 'a' || c > 'z')
  124.     {
  125.       c = '_';
  126.     }
  127.   return c;
  128. }
  129.  
  130. /************************************************************************/
  131. /*                                                                      */
  132. /* Count nodes in a tree.                                               */
  133. /*                                                                      */
  134. /************************************************************************/
  135.  
  136. #define LETTERCOUNT (LetterCount-'_')
  137.  
  138. /************************************************************************/
  139.  
  140. static ULONG 
  141. CountNodes (struct AVLTree *Tree, ULONG * LetterCount)
  142.  
  143. {
  144.   ULONG Count;
  145.  
  146.   Count = 0;
  147.   if (Tree)
  148.     {
  149.       struct AVLStateInfo StateInfo;
  150.       struct AnyNode *AnyNode;
  151.  
  152.       AVL_InitTraversal (Tree, &StateInfo);
  153.       while ((AnyNode = (struct AnyNode *) AVL_InOrder (&StateInfo)))
  154.     {
  155.       LETTERCOUNT[FirstLetter (AnyNode)]++;
  156.       Count++;
  157.     }
  158.     }
  159.   return Count;
  160. }
  161.  
  162. /************************************************************************/
  163. /*                                                                      */
  164. /* Functions to be called from FormatNode() and PrintNode() for         */
  165. /* outputting nodes from trees                                          */
  166. /*                                                                      */
  167. /************************************************************************/
  168.  
  169. struct NodesParameters
  170. {
  171.   struct AVLTree *Tree1, *Tree2;
  172.   int StartLetter, StopLetter;
  173.  
  174.   struct AnyNode *AnyNode1, *AnyNode2;
  175.   struct AVLStateInfo StateInfo1, StateInfo2;
  176.   struct AnyNode *CurrentNode;
  177. };
  178.  
  179. /************************************************************************/
  180.  
  181. static char *
  182. NextLabelNodes (char *PrevLabel, void *Parameters)
  183.  
  184. {
  185.   struct NodesParameters *Params;
  186.   char *Label;
  187.   int c;
  188.  
  189.   Params = Parameters;
  190.   if (!PrevLabel)
  191.     {
  192.       AVL_InitTraversal (Params->Tree1, &Params->StateInfo1);
  193.       Params->AnyNode1 = (struct AnyNode *) AVL_InOrder (&Params->StateInfo1);
  194.       if (Params->Tree2)
  195.     {
  196.       AVL_InitTraversal (Params->Tree2, &Params->StateInfo2);
  197.       Params->AnyNode2 = (struct AnyNode *) AVL_InOrder (&Params->StateInfo2);
  198.     }
  199.       else
  200.     {
  201.       Params->AnyNode2 = NULL;
  202.     }
  203.     }
  204.   else
  205.     {
  206.       free (PrevLabel);
  207.     }
  208.  
  209.   while (Params->AnyNode1 &&
  210.      ((c = FirstLetter (Params->AnyNode1)) < Params->StartLetter || c > Params->StopLetter))
  211.     {
  212.       Params->AnyNode1 = (struct AnyNode *) AVL_InOrder (&Params->StateInfo1);
  213.     }
  214.   while (Params->AnyNode2 &&
  215.      ((c = FirstLetter (Params->AnyNode2)) < Params->StartLetter || c > Params->StopLetter))
  216.     {
  217.       Params->AnyNode2 = (struct AnyNode *) AVL_InOrder (&Params->StateInfo2);
  218.     }
  219.  
  220.   if (Params->AnyNode1 || Params->AnyNode2)
  221.     {
  222.       if (!Params->AnyNode2)
  223.     {
  224.       c = -1;
  225.     }
  226.       else if (!Params->AnyNode1)
  227.     {
  228.       c = 1;
  229.     }
  230.       else
  231.     {
  232.       c = nodecmp (Params->AnyNode1, Params->AnyNode2);
  233.     }
  234.       if (c < 0)
  235.     {
  236.       if ((Params->Tree1==&AutodocNodeTree && ((struct AutodocNode *)Params->AnyNode1)->Function) ||
  237.           (Params->Tree1==&DefinesTree && ((struct IncludeItemNode *)Params->AnyNode1)->Function))
  238.         {
  239.           Label=xmalloc(strlen(Params->AnyNode1->Name)+3);
  240.           stpcpy(stpcpy(Label,Params->AnyNode1->Name),"()");
  241.         }
  242.       else
  243.         {
  244.           Label = xstrdup (Params->AnyNode1->Name);
  245.         }
  246.       Params->CurrentNode = Params->AnyNode1;
  247.       Params->AnyNode1 = (struct AnyNode *) AVL_InOrder (&Params->StateInfo1);
  248.     }
  249.       else
  250.     {
  251.       if ((Params->Tree2==&AutodocNodeTree && ((struct AutodocNode *)Params->AnyNode2)->Function) ||
  252.           (Params->Tree2==&DefinesTree && ((struct IncludeItemNode *)Params->AnyNode2)->Function))
  253.         {
  254.           Label=xmalloc(strlen(Params->AnyNode2->Name)+3);
  255.           stpcpy(stpcpy(Label,Params->AnyNode2->Name),"()");
  256.         }
  257.       else
  258.         {
  259.           Label = xstrdup (Params->AnyNode2->Name);
  260.         }
  261.       Params->CurrentNode = Params->AnyNode2;
  262.       Params->AnyNode2 = (struct AnyNode *) AVL_InOrder (&Params->StateInfo2);
  263.     }
  264.     }
  265.   else
  266.     {
  267.       Label = NULL;
  268.     }
  269.   return Label;
  270. }
  271.  
  272. /************************************************************************/
  273.  
  274. static void 
  275. PrintLinkNodes (void *Parameters)
  276.  
  277. {
  278.   struct NodesParameters *Params;
  279.  
  280.   Params = Parameters;
  281.   if (Params->Tree1 == &AutodocNodeTree)
  282.     {
  283.       WPrintf ("\x22%s/%s\x22",
  284.            ((struct AutodocNode *) Params->CurrentNode)->Entries->AutodocModuleNode->AutodocFileNode->LinkName,
  285.            ((struct AutodocNode *)Params->CurrentNode)->Entries->RealNode->AnyNode.Name);
  286.     }
  287.   else
  288.     {
  289.       WPrintf ("\x22%s/File\x22 %lu",
  290.            ((struct IncludeItemNode *) Params->CurrentNode)->Entries->IncludeFileNode->LinkName,
  291.            ((struct IncludeItemNode *) Params->CurrentNode)->Entries->Line);
  292.     }
  293. }
  294.  
  295. /************************************************************************/
  296. /*                                                                      */
  297. /* Output nodes from trees.                                             */
  298. /*                                                                      */
  299. /************************************************************************/
  300.  
  301. static void 
  302. OutputNodes (struct AVLTree *Tree1, struct AVLTree *Tree2, int StartLetter, int StopLetter)
  303.  
  304. {
  305.   struct NodesParameters Parameters;
  306.   int *ColWidth;
  307.  
  308.   WPrintf ("\n");
  309.   Parameters.Tree1 = Tree1;
  310.   Parameters.Tree2 = Tree2;
  311.   Parameters.StartLetter = StartLetter;
  312.   Parameters.StopLetter = StopLetter;
  313.   ColWidth = FormatNode (NextLabelNodes, &Parameters);
  314.   PrintNode (NextLabelNodes, PrintLinkNodes, &Parameters, ColWidth);
  315. }
  316.  
  317. /************************************************************************/
  318. /*                                                                      */
  319. /* Create document from item tree                                       */
  320. /*                                                                      */
  321. /************************************************************************/
  322.  
  323. static void 
  324. MakeDocument (struct AVLTree *Tree1, struct AVLTree *Tree2, char *Node, char *Text)
  325.  
  326. {
  327.   ULONG LetterCount['z' - '_' + 1];
  328.   ULONG Count;
  329.  
  330.   memset (LetterCount, 0, sizeof (LetterCount));
  331.   Count = CountNodes (Tree1, LetterCount) + CountNodes (Tree2, LetterCount);
  332.  
  333.   WPrintf ("@NODE %s \x22%s\x22\n\n", Node, Text);
  334.   WHeadline (Text);
  335.  
  336.   if (Count > 50)
  337.     {
  338.       struct
  339.     {
  340.       short FromChar;
  341.       short ToChar;
  342.     }
  343.       List['z' - '_' + 1];
  344.       int Index;
  345.       int i;
  346.       int Columns;
  347.  
  348.       Index = 0;
  349.       for (i = 'a'; i <= 'z'; i++)
  350.     {
  351.       if (LETTERCOUNT[i])
  352.         {
  353.           List[Index].FromChar = i;
  354.           for (Count = 0;
  355.            Count < 50 && i <= 'z';
  356.            Count += LETTERCOUNT[i++])
  357.         ;
  358.           List[Index].ToChar = --i;
  359.           while (!LETTERCOUNT[List[Index].ToChar])
  360.         {
  361.           List[Index].ToChar--;
  362.         }
  363.           Index++;
  364.         }
  365.     }
  366.       if (LETTERCOUNT['_'])
  367.     {
  368.       List[Index].FromChar = '_';
  369.       List[Index++].ToChar = '_';
  370.     }
  371.       Columns = ((*Arguments.Width) + 3) / (5 + 3 + (*Arguments.Version >= 39));
  372.       for (i = 0; i < Index; i++)
  373.     {
  374.       if (i % Columns)
  375.         {
  376.           WPrintf ("   ");
  377.         }
  378.       else
  379.         {
  380.           WPrintf ("\n");
  381.         }
  382.       if (List[i].FromChar != '_')
  383.         {
  384.           if (List[i].FromChar == List[i].ToChar)
  385.         {
  386.           WPrintf ("@{\x22  %lc  ", List[i].FromChar);
  387.         }
  388.           else
  389.         {
  390.           WPrintf ("@{\x22 %lc-%lc ", List[i].FromChar, List[i].ToChar);
  391.         }
  392.         }
  393.       else
  394.         {
  395.           WPrintf ("@{\x22other");
  396.         }
  397.       WPrintf ("\x22 LINK \x22%s%lc\x22}", Node, List[i].FromChar);
  398.     }
  399.       WPrintf ("\n@ENDNODE\n");
  400.  
  401.       for (i = 0; i < Index; i++)
  402.     {
  403.       char *NodeName, *NodeNameT;
  404.       char *NodeDesc, *NodeDescT;
  405.  
  406.       NodeDesc = xmalloc (strlen (Node) + 2);
  407.       NodeDescT = stpcpy (NodeDesc, Node);
  408.       NodeDescT[0] = List[i].FromChar;
  409.       NodeDescT[1] = '\0';
  410.  
  411.       NodeName = xmalloc (strlen (Text) + 10);
  412.       NodeNameT = stpcpy (NodeName, Text);
  413.  
  414.       if (List[i].FromChar == '_')
  415.         {
  416.           stpcpy (NodeNameT, " (other)");
  417.         }
  418.       else
  419.         {
  420.           if (List[i].FromChar == List[i].ToChar)
  421.         {
  422.           sprintf (NodeNameT, " (%lc)", (long) List[i].FromChar);
  423.         }
  424.           else
  425.         {
  426.           sprintf (NodeNameT, " (%lc-%lc)", (long) List[i].FromChar, (long) List[i].ToChar);
  427.         }
  428.         }
  429.       WPrintf ("@NODE \x22%s\x22 \x22%s\x22\n\n", NodeDesc, NodeName);
  430.       WHeadline (NodeName);
  431.       free (NodeName);
  432.       free (NodeDesc);
  433.  
  434.       OutputNodes (Tree1, Tree2, List[i].FromChar, List[i].ToChar);
  435.  
  436.       WPrintf ("@ENDNODE\n");
  437.     }
  438.     }
  439.   else
  440.     {
  441.       OutputNodes (Tree1, Tree2, '_', 'z');
  442.       WPrintf ("@ENDNODE\n");
  443.     }
  444. }
  445.  
  446. /************************************************************************/
  447. /*                                                                      */
  448. /* Create the global table of contents                                  */
  449. /*                                                                      */
  450. /************************************************************************/
  451.  
  452. void
  453. WriteGlobalTOC (void)
  454.  
  455. {
  456.   if (Arguments.Master)
  457.     {
  458.       puts("\n*** Creating global table of contents ***");
  459.       WOpen (CURRENTDIR, Arguments.Master);
  460.       GlobalTOCFilename=Arguments.FullPath ? xstrdup(WriteFilename) : Arguments.Master;
  461.       WriteHeader (WriteFilename, NULL);
  462.  
  463.       WPrintf ("\n@NODE MAIN \x22Global table of contents\x22\n\n");
  464.       WHeadline ("Table of contents");
  465.       WPrintf ("%s",
  466.            "\n"
  467.            "  @{\x22 autodoc documents \x22 LINK Autodocs}\n"
  468.            "  @{\x22   function names  \x22 LINK Functions}\n"
  469.            "\n"
  470.            "  @{\x22 header files      \x22 LINK Headers}\n"
  471.            "  @{\x22   structs/unions  \x22 LINK StructsUnions}\n"
  472.            "  @{\x22   #defines        \x22 LINK Defines}\n"
  473.            "  @{\x22   typedefs        \x22 LINK Typedefs}\n"
  474.            "@ENDNODE\n");
  475.  
  476.       {
  477.     struct AutodocsParameters Parameters;
  478.     int *ColWidth;
  479.  
  480.     WPrintf ("@NODE Autodocs \x22" "Autodoc Documents\x22\n\n");
  481.     WHeadline ("Autodoc documents");
  482.     WPrintf ("\n");
  483.     ColWidth = FormatNode (NextLabelAutodocs, &Parameters);
  484.     PrintNode (NextLabelAutodocs, PrintLinkAutodocs, &Parameters, ColWidth);
  485.     WPrintf ("@ENDNODE\n");
  486.       }
  487.  
  488.       MakeDocument (&AutodocNodeTree, NULL, "Functions", "Autodoc entries");
  489.  
  490.       {
  491.     struct HeadersParameters Parameters;
  492.     int *ColWidth;
  493.  
  494.     WPrintf ("\n@NODE Headers \x22Header files\x22\n\n");
  495.     WHeadline ("Header files");
  496.     WPrintf ("\n");
  497.     ColWidth = FormatNode (NextLabelHeaders, &Parameters);
  498.     PrintNode (NextLabelHeaders, PrintLinkHeaders, &Parameters, ColWidth);
  499.     WPrintf ("@ENDNODE\n");
  500.       }
  501.  
  502.       MakeDocument (&StructureTree, &UnionTree, "StructsUnions", "structs/unions");
  503.       MakeDocument (&DefinesTree, NULL, "Defines", "#defines");
  504.       MakeDocument (&TypedefTree, NULL, "Typedefs", "typedefs");
  505.  
  506.       WClose ();
  507.     }
  508. }
  509.  
  510. /************************************************************************/
  511. /*                                                                      */
  512. /* Output the xref for an include item tree                             */
  513. /*                                                                      */
  514. /************************************************************************/
  515.  
  516. static void 
  517. DoIncludeXRef (struct AVLTree *Tree, ULONG Type)
  518.  
  519. {
  520.   struct AVLStateInfo StateInfo;
  521.   struct IncludeItemNode *IncludeItemNode;
  522.  
  523.   AVL_InitTraversal (Tree, &StateInfo);
  524.   while ((IncludeItemNode = (struct IncludeItemNode *) AVL_InOrder (&StateInfo)))
  525.     {
  526.       struct IncludeItemNodeNode *IncludeItemNodeNode;
  527.  
  528.       for (IncludeItemNodeNode = IncludeItemNode->Entries;
  529.        IncludeItemNodeNode;
  530.        IncludeItemNodeNode = IncludeItemNodeNode->Next)
  531.     {
  532.       WPrintf ("\x22%s\x22 \x22%s\x22 %lu %lu\n",
  533.            IncludeItemNode->AnyNode.Name,
  534.            IncludeItemNodeNode->IncludeFileNode->LinkName,
  535.            IncludeItemNodeNode->Line,
  536.            ((Type == 8 && IncludeItemNode->Function) ? 4 : Type));
  537.     }
  538.     }
  539. }
  540.  
  541. /************************************************************************/
  542. /*                                                                      */
  543. /* Write the crossreference file                                        */
  544. /*                                                                      */
  545. /************************************************************************/
  546.  
  547. void 
  548. WriteXRef (void)
  549.  
  550. {
  551.   if (Arguments.XREF)
  552.     {
  553.       struct AVLStateInfo StateInfo;
  554.       struct AutodocNode *AutodocNode;
  555.  
  556.       puts("\n*** Creating crossreference file ***");
  557.  
  558.       WOpen (CURRENTDIR, Arguments.XREF);
  559.       WPrintf ("/*\n");
  560.       WriteHeader (WriteFilename, NULL);
  561.       WPrintf ("*/\n\nXREF:\n");
  562.  
  563.       AVL_InitTraversal (&AutodocNodeTree, &StateInfo);
  564.       while ((AutodocNode = (struct AutodocNode *) AVL_InOrder (&StateInfo)))
  565.     {
  566.       struct AutodocNodeNode *AutodocNodeNode;
  567.  
  568.       for (AutodocNodeNode = AutodocNode->Entries;
  569.            AutodocNodeNode;
  570.            AutodocNodeNode = AutodocNodeNode->Next)
  571.         {
  572.           ULONG Type;
  573.  
  574.           if (AutodocNode->Function)
  575.         Type = 1;
  576.           else if (strcmp (AutodocNodeNode->AutodocModuleNode->AnyNode.Name, "SAD"))
  577.         Type = 8;
  578.           else
  579.         Type = 2;
  580.  
  581.           if (AutodocNodeNode->RealNode == AutodocNodeNode)
  582.         {
  583.           WPrintf ("\x22%s\x22 \x22%s\x22 0 %lu\n",
  584.                AutodocNodeNode->AnyNode.Name,
  585.                AutodocNodeNode->AutodocModuleNode->AutodocFileNode->LinkName,
  586.                Type);
  587.         }
  588.           else
  589.         {
  590.           WPrintf ("/* $%$%ALIAS%$%$: \x22%s\x22 \x22%s\x22 \x22%s\x22 0 %lu */\n",
  591.                AutodocNodeNode->AnyNode.Name,
  592.                AutodocNodeNode->RealNode->AnyNode.Name,
  593.                AutodocNodeNode->RealNode->AutodocModuleNode->AutodocFileNode->LinkName,
  594.                Type);
  595.         }
  596.         }
  597.     }
  598.  
  599.       DoIncludeXRef (&DefinesTree, 8);
  600.       DoIncludeXRef (&StructureTree, 5);
  601.       DoIncludeXRef (&UnionTree, 5);
  602.       DoIncludeXRef (&TypedefTree, 7);
  603.  
  604.       WPrintf ("#\n");
  605.  
  606.       WClose();
  607.     }
  608. }
  609.