home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / utilities / hypertext / adtoht / source / incfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-22  |  13.5 KB  |  425 lines

  1. /* Process include files */
  2.  
  3. #include "global.h"
  4. #include "misc.h"
  5. #include "readdir.h"
  6. #include "incfile.h"
  7.  
  8. /***********************************************/
  9.  
  10. struct FileNode **IncludeFileArray;
  11. struct FileNode *IncludeFileList;
  12.  
  13. struct StructNode **StructArray;
  14. struct StructNode *StructList;
  15.  
  16. struct StructNode **TypedefArray;
  17. struct StructNode *TypedefList;
  18.  
  19. struct TypedefTempNode *TypedefTempList;
  20.  
  21. static int ExpandedTypedefs;
  22.  
  23. /***********************************************/
  24.  
  25. BOOL PrintIncludeFileList(void)
  26.  
  27. {
  28.    int Index;
  29.  
  30.    if (FPrintf(XRefFile,"\n/* Include files */\n")==-1)
  31.       {
  32.          PrintFault(IoErr(),Arguments.XRefFile);
  33.          return(FALSE);
  34.       }
  35.    for (Index=1; Index<=(int)IncludeFileArray[0]; Index++)
  36.       {
  37.          if (FPrintf(XRefFile,"\x22<%s>\x22 \x22%s\x22 0 3\n",IncludeFileArray[Index]->Node.Name,IncludeFileArray[Index]->Node.Name)==-1)
  38.             {
  39.                PrintFault(IoErr(),Arguments.XRefFile);
  40.                return(FALSE);
  41.             }
  42.       }
  43.    return(TRUE);
  44. }
  45.  
  46. /***********************************************/
  47.  
  48. BOOL PrintStructList(void)
  49.  
  50. {
  51.    int Index;
  52.  
  53.    printf("      %ld structs found\n",(int)StructArray[0]);
  54.    if (FPrintf(XRefFile,"\n/* Structures */\n")==-1)
  55.       {
  56.          PrintFault(IoErr(),Arguments.XRefFile);
  57.          return(FALSE);
  58.       }
  59.    for (Index=1; Index<=(int)StructArray[0]; Index++)
  60.       {
  61.          if (FPrintf(XRefFile,"\x22%s\x22 \x22%s\x22 %ld 5\n",StructArray[Index]->Node.Name,StructArray[Index]->File,StructArray[Index]->Line)==-1)
  62.             {
  63.                PrintFault(IoErr(),Arguments.XRefFile);
  64.                return(FALSE);
  65.             }
  66.       }
  67.    return(TRUE);
  68. }
  69.  
  70. /***********************************************/
  71.  
  72. BOOL PrintTypedefs(void)
  73.  
  74. {
  75.    int Index;
  76.  
  77.    printf("      %ld typedefs found\n",(int)TypedefArray[0]);
  78.    if (FPrintf(XRefFile,"\n/* Typedefs */\n")==-1)
  79.       {
  80.          PrintFault(IoErr(),Arguments.XRefFile);
  81.          return(FALSE);
  82.       }
  83.    for (Index=1; Index<=(int)TypedefArray[0]; Index++)
  84.       {
  85.          if (FPrintf(XRefFile,"\x22%s\x22 \x22%s\x22 %ld 5\n",TypedefArray[Index]->Node.Name,TypedefArray[Index]->File,TypedefArray[Index]->Line)==-1)
  86.             {
  87.                PrintFault(IoErr(),Arguments.XRefFile);
  88.                return(FALSE);
  89.             }
  90.       }
  91.    return(TRUE);
  92. }
  93.  
  94. /***********************************************/
  95.  
  96. BOOL AddIdent(char *Name, char *File, long Line, BOOL IsTypedef)
  97.  
  98. {
  99.    struct StructNode *NewNode;
  100.  
  101.    if (IsTypedef)
  102.       {
  103.          if (!Arguments.Types && (!strcmp(Name,"BSTR") || !strcmp(Name,"BPTR")))
  104.             {
  105.                return(TRUE);
  106.             }
  107.          NewNode=TypedefList;
  108.       }
  109.    else
  110.       {
  111.          NewNode=StructList;
  112.       }
  113.    while (NewNode && strcmp(NewNode->Node.Name,Name))
  114.       {
  115.          NewNode=(struct StructNode *)(NewNode->Node.Next);
  116.       }
  117.    if (NewNode)
  118.       {
  119.          WriteError("Warning: %s %s defined twice.\n"
  120.                     "         First definition in %s, line %ld\n"
  121.                     "         Second definition in %s, line %ld\n"
  122.                     "         Ignoring 2nd definition\n",(IsTypedef ? "typedef" : "struct"),Name,NewNode->File,NewNode->Line,File,Line);
  123.          return(TRUE);
  124.       }
  125.    if (NewNode=AllocVec(sizeof(struct StructNode)+strlen(Name)+strlen(File)+2,0))
  126.       {
  127.          NewNode->Node.Name=(char *)(NewNode+1);
  128.          NewNode->File=stpcpy(NewNode->Node.Name,Name)+1;
  129.          stpcpy(NewNode->File,File);
  130.          NewNode->Line=Line;
  131.          if (IsTypedef)
  132.             {
  133.                NewNode->Node.Next=TypedefList;
  134.                TypedefList=NewNode;
  135.             }
  136.          else
  137.             {
  138.                NewNode->Node.Next=StructList;
  139.                StructList=NewNode;
  140.             }
  141.          return(TRUE);
  142.       }
  143.    else
  144.       {
  145.          PrintFault(ERROR_NO_FREE_STORE,NULL);
  146.       }
  147.    return(FALSE);
  148. }
  149.  
  150. /***********************************************/
  151.  
  152. static BOOL AddTypedefTemp(char *Identifier, char *Struct, char *File, long Line)
  153.  
  154. {
  155.    struct TypedefTempNode *NewNode;
  156.  
  157.    if (NewNode=AllocVec(sizeof(struct TypedefTempNode)+strlen(Identifier)+strlen(Struct)+strlen(File)+3,0))
  158.       {
  159.          NewNode->Node.Name=(char *)(NewNode+1);
  160.          NewNode->Struct=stpcpy(NewNode->Node.Name,Identifier)+1;
  161.          NewNode->File=stpcpy(NewNode->Struct,Struct)+1;
  162.          stpcpy(NewNode->File,File);
  163.          NewNode->Line=Line;
  164.          NewNode->Node.Next=TypedefTempList;
  165.          TypedefTempList=NewNode;
  166.          return(TRUE);
  167.       }
  168.    else
  169.       {
  170.          PrintFault(ERROR_NO_FREE_STORE,NULL);
  171.          return(FALSE);
  172.       }
  173. }
  174.  
  175. /***********************************************/
  176.  
  177. static long MyFGetC(void)
  178.  
  179. {
  180.    long Character;
  181.  
  182.    Character=FGetC(CurrentFile);
  183.    switch(Character)
  184.       {
  185.          case '\n': CurrentLine++;
  186.          case '\t': Character=' ';
  187.       }
  188.    return(Character);
  189. }
  190.  
  191. /***********************************************/
  192.  
  193. #define ERROR_TOKEN   -1
  194. #define EOF_TOKEN      0
  195. #define IDENT_TOKEN    1
  196. #define ANY_TOKEN      2
  197.  
  198. static WORD ReadToken(void)
  199.  
  200. {
  201.    long Character;
  202.    int Index;
  203.  
  204. DoRead:
  205.    do
  206.       {
  207.          if (Break())
  208.             {
  209.                return(ERROR_TOKEN);
  210.             }
  211.          Character=MyFGetC();
  212.       }
  213.    while (Character==' ');
  214.    switch(Character)
  215.       {
  216.          case -1:   return(IoErr() ? ERROR_TOKEN : EOF_TOKEN);
  217.  
  218.          case '/':  if ((Character=MyFGetC())=='*')
  219.                        {
  220.                           do
  221.                              {
  222.                                 while ((Character=MyFGetC())!='*')
  223.                                    {
  224.                                       if (Character==-1)
  225.                                          {
  226.                                             return(IoErr() ? ERROR_TOKEN : EOF_TOKEN);
  227.                                          }
  228.                                       if (Break())
  229.                                          {
  230.                                             return(ERROR_TOKEN);
  231.                                          }
  232.                                    }
  233.                                 Character=MyFGetC();
  234.                                 if (Character!='/')
  235.                                    {
  236.                                       UnGetC(CurrentFile,Character);
  237.                                    }
  238.                              }
  239.                           while (Character!='/');
  240.                           goto DoRead;
  241.                        }
  242.                     else
  243.                        {
  244.                           UnGetC(CurrentFile,Character);
  245.                        }
  246.                     return(ANY_TOKEN);
  247.  
  248.          case ';':
  249.          case '[':
  250.          case ']':
  251.          case '{':
  252.          case '}':
  253.          case '(':
  254.          case ')':  return((WORD)Character);
  255.  
  256.          default:   Index=0;
  257.                     while ((Character>='a' && Character<='z') ||
  258.                            (Character>='A' && Character<='Z') ||
  259.                            (Character>='0' && Character<='9') ||
  260.                            (Character=='_' || Character=='#'))
  261.                        {
  262.                           Identifier[Index++]=Character;
  263.                           if (Index==MAX_LEN-1)
  264.                              {
  265.                                 WriteError("Error: Identifier too long.\n"
  266.                                            "       Operation aborted.\n");
  267.                                 return(ERROR_TOKEN);
  268.                              }
  269.                           Character=MyFGetC();
  270.                        }
  271.                     if (Index)
  272.                        {
  273.                           Identifier[Index]='\0';
  274.                           UnGetC(CurrentFile,Character);
  275.                           return(IDENT_TOKEN);
  276.                        }
  277.                     return(ANY_TOKEN);
  278.       }
  279. }
  280.  
  281. /***********************************************/
  282.  
  283. static BOOL ProcessIncludeFile(char *Name)
  284.  
  285. {
  286.    long BlockLevel;
  287.    long StructLine;
  288.    long TypedefLine;
  289.    BOOL Success;
  290.    WORD Typedef;
  291.    WORD Token;
  292.  
  293.    Success=TRUE;
  294.    if (Stricmp(FileList->Node.Name,"exec/types.h"))
  295.       {
  296.          printf("      Reading %s\n",FileList->Node.Name);
  297.          if (CurrentFile=Open(Name,MODE_OLDFILE))
  298.             {
  299.                CurrentLine=0;
  300.                BlockLevel=0;
  301.                Typedef=0;
  302.                while ((Token=ReadToken())>0 && Success)
  303.                   {
  304. CheckToken:          switch(Token)
  305.                         {
  306.                            case '(':
  307.                            case '{':
  308.                            case '[':         BlockLevel++;
  309.                                              break;
  310.                            case ')':
  311.                            case '}':
  312.                            case ']':         BlockLevel--;
  313.                                              break;
  314.             
  315.                            case IDENT_TOKEN: if (!BlockLevel)
  316.                                                 {
  317.                                                    if (!__builtin_strcmp(Identifier,"typedef"))
  318.                                                       {
  319.                                                          Typedef=1;
  320.                                                          TypedefLine=CurrentLine;
  321.                                                       }
  322.                                                    else if (!__builtin_strcmp(Identifier,"struct"))
  323.                                                       {
  324.                                                          StructLine=CurrentLine;
  325.                                                          Token=ReadToken();
  326.                                                          if (Token==IDENT_TOKEN)
  327.                                                             {
  328.                                                                if (Typedef)
  329.                                                                   {
  330.                                                                      stpcpy(Buffer,Identifier);
  331.                                                                      Typedef=2;
  332.                                                                   }
  333.                                                                Token=ReadToken();
  334.                                                                if (Token=='{')
  335.                                                                   {
  336.                                                                      Success=AddIdent(Identifier,FileList->Node.Name,StructLine,FALSE);
  337.                                                                   }
  338.                                                                goto CheckToken;
  339.                                                             }
  340.                                                          else
  341.                                                             {
  342.                                                                goto CheckToken;
  343.                                                             }
  344.                                                       }
  345.                                                 }
  346.                                              break;
  347.  
  348.                            case ';':         if (!BlockLevel)
  349.                                                 {
  350.                                                    if (Arguments.NoExpand && Typedef==2) Typedef=1;
  351.                                                    switch(Typedef)
  352.                                                       {
  353.                                                          case 1: Success=AddIdent(Identifier,FileList->Node.Name,TypedefLine,TRUE);
  354.                                                                  break;
  355.                                                          case 2: Success=AddTypedefTemp(Identifier,Buffer,FileList->Node.Name,TypedefLine);
  356.                                                                  break;
  357.                                                       }
  358.                                                    Typedef=0;
  359.                                                 }
  360.                                              break;
  361.                         }
  362.                   }
  363.                if (Token)
  364.                   {
  365.                      Success=FALSE;
  366.                   }
  367.                Close(CurrentFile);
  368.             }
  369.          else
  370.             {
  371.                PrintFault(IoErr(),FileList->Node.Name);
  372.                Success=FALSE;
  373.             }
  374.       }
  375.    return(Success);
  376. }
  377.  
  378. /***********************************************/
  379.  
  380. static BOOL ExpandTypedefs(void)
  381.  
  382. {
  383.    struct AnyNode *AnyNode;
  384.  
  385.    while (TypedefTempList)
  386.       {
  387.          AnyNode=StructList;
  388.          while (AnyNode && strcmp(AnyNode->Name,TypedefTempList->Struct))
  389.             {
  390.                AnyNode=AnyNode->Next;
  391.             }
  392.          if (AnyNode)
  393.             {
  394.                if (!AddIdent(TypedefTempList->Node.Name,((struct StructNode *)AnyNode)->File,((struct StructNode *)AnyNode)->Line,TRUE))
  395.                   {
  396.                      return(FALSE);
  397.                   }
  398.                AnyNode=TypedefTempList->Node.Next;
  399.                FreeVec(TypedefTempList);
  400.                TypedefTempList=(struct TypedefTempList *)AnyNode;
  401.                ExpandedTypedefs++;
  402.             }
  403.          else
  404.             {
  405.                AnyNode=TypedefTempList->Node.Next;
  406.                TypedefTempList->Node.Next=TypedefList;
  407.                TypedefList=(struct TypedefList *)TypedefTempList;
  408.                TypedefTempList=(struct TypedefTempNode *)AnyNode;
  409.             }
  410.       }
  411.    printf("      %ld typedef struct statements expanded\n",ExpandedTypedefs);
  412. }
  413.  
  414. /***********************************************/
  415.  
  416. BOOL ProcessIncludes(BPTR Directory, char *Name)
  417.  
  418. {
  419.    if (ReadDir(Directory,Name,".h",ProcessIncludeFile))
  420.       {
  421.          return (ExpandTypedefs());
  422.       }
  423.    return(FALSE);
  424. }
  425.