home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / flower.zip / mniplist.c < prev    next >
Text File  |  1997-06-24  |  15KB  |  543 lines

  1.  
  2. /* Copyright (c) 1996, 1997 Craig Schneiderwent */
  3. /*
  4. Program: flower
  5. File:    mniplist.c
  6. Author:  Craig Schneiderwent
  7.          74631.165@compuserve.com
  8. Date:    14-Apr-1996
  9.  
  10. These are helper functions for flower.  They
  11. manipulate the linked lists which hold the
  12. function information for the files flower
  13. has been asked to analyze.
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21. #include <limits.h>
  22. #include "mydebug.h"
  23. #include "structs.h"
  24. #include "mniplist.h"
  25. #include "memfunc.h"
  26.  
  27. int addToFileList( struct fileInfo *psFileInfo, char *fileNm )
  28. {
  29.     #ifdef MY_DEBUG
  30.     char            *thisFuncNm = "addToFuncList\0";
  31.     char            debug_msg[ MY_DEBUG_MSG_SZ ];
  32.     #endif
  33.     struct fileInfo *tail = NULL;
  34.     struct fileInfo *new = NULL;
  35.  
  36.     #ifdef MY_DEBUG
  37.     TRACE_ENTRY( thisFuncNm );
  38.     sprintf( debug_msg, " fileNm = %s", fileNm );
  39.     TRACE_MSG( thisFuncNm, debug_msg );
  40.     #endif
  41.  
  42.     if ( strlen( psFileInfo->fileNm ) == 0 ) {
  43.         /* first file */
  44.         strncpy( psFileInfo->fileNm, fileNm, MAXFILENAMESIZE );
  45.         return( 0 );
  46.     } /* endif */
  47.  
  48.     tail = endOfFileList( psFileInfo );
  49.  
  50.     new = fileInfoMalloc( );
  51.     if ( new == NULL ) {
  52.         return( 3 );
  53.     } /* endif */
  54.  
  55.     strncpy( new->fileNm, fileNm, MAXFILENAMESIZE );
  56.     new->prev = tail;
  57.     tail->next = new;
  58.  
  59.     #ifdef MY_DEBUG
  60.     TRACE_EXIT( thisFuncNm );
  61.     #endif
  62.  
  63.     return( 0 );
  64. }
  65.  
  66. int addToFuncList( struct fileInfo *psFileInfo, char *funcNm )
  67. {
  68.     #ifdef MY_DEBUG
  69.     char            *thisFuncNm = "addToFuncList\0";
  70.     #endif
  71.     struct funcInfo *new = NULL;
  72.  
  73.     #ifdef MY_DEBUG
  74.     TRACE_ENTRY( thisFuncNm );
  75.     TRACE_MSG( thisFuncNm, funcNm );
  76.     #endif
  77.  
  78.     new = funcInfoMalloc(  );
  79.     if ( new == NULL ) {
  80.         return( 3 );
  81.     } /* endif */
  82.  
  83.     strncpy( new->funcNm, funcNm, MAXFUNCNAMESIZE );
  84.     strncpy( new->fileNm, psFileInfo->fileNm, MAXFILENAMESIZE );
  85.     new->prev = psFileInfo->funcListEnd;
  86.     psFileInfo->funcListEnd = new;
  87.     if ( psFileInfo->funcListStart == NULL ) {
  88.         /* first one */
  89.         psFileInfo->funcListStart = new;
  90.     } else {
  91.         new->prev->next = new;
  92.     } /* endif */
  93.  
  94.     #ifdef MY_DEBUG
  95.     TRACE_EXIT( thisFuncNm );
  96.     #endif
  97.  
  98.     return( 0 );
  99. }
  100.  
  101. int addToCalledFuncList( struct funcInfo *psFuncInfo, char *funcNm )
  102. {
  103.     struct funcInfo *new = NULL;
  104.     short  isANewOne = 0;
  105.  
  106.     new = funcInCalledList( funcNm, psFuncInfo );
  107.     if ( new == NULL ) {
  108.         new = funcInfoMalloc(  );
  109.         if ( new == NULL ) {
  110.             return( 3 );
  111.         } /* endif */
  112.         strncpy( new->funcNm, funcNm, MAXFUNCNAMESIZE );
  113.         new->callsToMe = 1;
  114.         new->prev = psFuncInfo->calledFuncListEnd;
  115.         psFuncInfo->calledFuncListEnd = new;
  116.         isANewOne = 1;
  117.     } else {
  118.         new->callsToMe++;
  119.     } /* endif */
  120.  
  121.     if ( psFuncInfo->calledFuncListStart == NULL ) {
  122.         /* first one */
  123.         psFuncInfo->calledFuncListStart = new;
  124.         psFuncInfo->callsFromMe = 1;
  125.     } else {
  126.         if ( isANewOne ) {
  127.             new->prev->next = new;
  128.         } /* endif */
  129.         psFuncInfo->callsFromMe++;
  130.     } /* endif */
  131.  
  132.     return( 0 );
  133. }
  134.  
  135. int createCalledByFuncList( struct fileInfo *psFileInfo
  136.                           , struct isCalledByFuncInfo *psCalledByList )
  137. {
  138.     #ifdef MY_DEBUG
  139.     char            *thisFuncNm = "createCalledByFuncList\0";
  140.     #endif
  141.     struct fileInfo *nextFile = NULL;
  142.     struct funcInfo *nextFunc = NULL;
  143.     struct funcInfo *nextCalledFunc = NULL;
  144.     int             rc = 0;
  145.  
  146.     #ifdef MY_DEBUG
  147.     TRACE_ENTRY( thisFuncNm );
  148.     #endif
  149.  
  150.     nextFile = startOfFileList( psFileInfo );
  151.     nextFunc = nextFile->funcListStart;
  152.     nextCalledFunc = nextFunc->calledFuncListStart;
  153.  
  154.     while ( nextFile != NULL ) {
  155.         while ( nextFunc != NULL ) {
  156.             while ( nextCalledFunc != NULL ) {
  157.                 rc = addToCalledByFuncList( psCalledByList
  158.                                           , nextFunc
  159.                                           , nextCalledFunc );
  160.                 if ( rc != 0 ) {
  161.                     return( rc );
  162.                 } /* endif */
  163.                 nextCalledFunc = nextCalledFunc->next;
  164.             } /* endwhile */
  165.             nextFunc = nextFunc->next;
  166.             if ( nextFunc != NULL ) {
  167.                 nextCalledFunc = nextFunc->calledFuncListStart;
  168.             } /* endif */
  169.         } /* endwhile */
  170.         nextFile = nextFile->next;
  171.         if ( nextFile != NULL ) {
  172.             nextFunc = nextFile->funcListStart;
  173.             if ( nextFunc != NULL ) {
  174.                 nextCalledFunc = nextFunc->calledFuncListStart;
  175.             } /* endif */
  176.         } /* endif */
  177.     } /* endwhile */
  178.  
  179.     #ifdef MY_DEBUG
  180.     TRACE_EXIT( thisFuncNm );
  181.     #endif
  182.  
  183.     return( 0 );
  184. }
  185.  
  186. int addToCalledByFuncList( struct isCalledByFuncInfo *psCalledByList
  187.                          , struct funcInfo *psCallingFuncInfo
  188.                          , struct funcInfo *psCalledFuncInfo )
  189. {
  190.     #ifdef MY_DEBUG
  191.     char            *thisFuncNm = "addToCalledByFuncList\0";
  192.     char            debug_msg[ MY_DEBUG_MSG_SZ ];
  193.     #endif
  194.     struct isCalledByFuncInfo *tail = NULL;
  195.     struct isCalledByFuncInfo *new = NULL;
  196.     struct isCalledByFuncInfo *newCaller = NULL;
  197.  
  198.     #ifdef MY_DEBUG
  199.     TRACE_ENTRY( thisFuncNm );
  200.     sprintf( debug_msg
  201.            , "\n  psCallingFuncInfo->funcNm = %s\n  psCalledFuncInfo->funcNm = %s"
  202.            , psCallingFuncInfo->funcNm
  203.            , psCalledFuncInfo->funcNm );
  204.     TRACE_MSG( thisFuncNm, debug_msg );
  205.     #endif
  206.  
  207.     if ( strlen( psCalledByList->funcNm ) == 0 ) {
  208.         /* first one */
  209.         new = psCalledByList;
  210.         strncpy( new->fileNm
  211.                , psCalledFuncInfo->fileNm
  212.                , MAXFILENAMESIZE );
  213.         strncpy( new->funcNm
  214.                , psCalledFuncInfo->funcNm
  215.                , MAXFUNCNAMESIZE );
  216.     } else {
  217.         new = funcInCalledByFuncList( psCalledFuncInfo->funcNm
  218.                                     , psCalledByList );
  219.     } /* endif */
  220.  
  221.     if ( new == NULL ) {
  222.         new = isCalledByMalloc( );
  223.         if ( new == NULL ) {
  224.             return( 3 );
  225.         } /* endif */
  226.         tail = endOfCalledByFuncList( psCalledByList );
  227.         if ( tail == NULL ) {
  228.             /* first one */
  229.             tail = psCalledByList;
  230.             initIsCalledByFuncInfo( tail );
  231.         } /* endif */
  232.         new->prev = tail;
  233.         new->next = NULL;
  234.         tail->next = new;
  235.         strncpy( new->fileNm
  236.                , psCalledFuncInfo->fileNm
  237.                , MAXFILENAMESIZE );
  238.         strncpy( new->funcNm
  239.                , psCalledFuncInfo->funcNm
  240.                , MAXFUNCNAMESIZE );
  241.     } /* endif */
  242.  
  243.     newCaller = callerInList( psCallingFuncInfo->funcNm, new );
  244.     if ( newCaller == NULL ) {
  245.         newCaller = isCalledByMalloc( );
  246.         if ( newCaller == NULL ) {
  247.             return( 3 );
  248.         } /* endif */
  249.         strncpy( newCaller->fileNm
  250.                , psCallingFuncInfo->fileNm
  251.                , MAXFILENAMESIZE );
  252.         strncpy( newCaller->funcNm
  253.                , psCallingFuncInfo->funcNm
  254.                , MAXFUNCNAMESIZE );
  255.         newCaller->prev = new->callingFuncListEnd;
  256.         new->callingFuncListEnd = newCaller;
  257.         if ( new->callingFuncListStart == NULL ) {
  258.             /* first one */
  259.             new->callingFuncListStart = newCaller;
  260.         } else {
  261.             newCaller->prev->next = newCaller;
  262.         } /* endif */
  263.     } /* endif */
  264.  
  265.     newCaller->count++;
  266.     new->count++;
  267.  
  268.     #ifdef MY_DEBUG
  269.     TRACE_EXIT( thisFuncNm );
  270.     #endif
  271.  
  272.     return( 0 );
  273. }
  274.  
  275. void freeLists( struct fileInfo *psFileInfo )
  276. {
  277.     struct fileInfo *nextFile = NULL;
  278.     struct funcInfo *nextFunc = NULL;
  279.     struct funcInfo *nextCalledFunc = NULL;
  280.     struct fileInfo *thisFile = NULL;
  281.     struct funcInfo *thisFunc = NULL;
  282.     struct funcInfo *thisCalledFunc = NULL;
  283.  
  284.     thisFile = psFileInfo;
  285.  
  286.     while ( thisFile != NULL ) {
  287.         thisFunc = thisFile->funcListStart;
  288.         while ( thisFunc != NULL ) {
  289.             thisCalledFunc = thisFunc->calledFuncListStart;
  290.             while ( thisCalledFunc != NULL ) {
  291.                 nextCalledFunc = thisCalledFunc->next;
  292.                 free( thisCalledFunc );
  293.                 thisCalledFunc = nextCalledFunc;
  294.             } /* endwhile */
  295.             nextFunc = thisFunc->next;
  296.             free( thisFunc );
  297.             thisFunc = nextFunc;
  298.         } /* endwhile */
  299.         nextFile = thisFile->next;
  300.         if ( thisFile != psFileInfo ) {
  301.             free( thisFile );
  302.         } /* endif */
  303.         thisFile = nextFile;
  304.     } /* endwhile */
  305.  
  306.     return;
  307. }
  308.  
  309. void freeIsCalledByList( struct isCalledByFuncInfo *psFuncInfo )
  310. {
  311.     struct isCalledByFuncInfo *thisFunc = NULL;
  312.     struct isCalledByFuncInfo *thisCallingFunc = NULL;
  313.     struct isCalledByFuncInfo *nextFunc = NULL;
  314.     struct isCalledByFuncInfo *nextCallingFunc = NULL;
  315.  
  316.     thisFunc = psFuncInfo;
  317.  
  318.     while ( thisFunc != NULL ) {
  319.         thisCallingFunc = thisFunc->callingFuncListStart;
  320.         while ( thisCallingFunc != NULL ) {
  321.             nextCallingFunc = thisCallingFunc->next;
  322.             free( thisCallingFunc );
  323.             thisCallingFunc = nextCallingFunc;
  324.         } /* endwhile */
  325.         nextFunc = thisFunc->next;
  326.         if ( thisFunc != psFuncInfo ) {
  327.             free( thisFunc );
  328.         } /* endif */
  329.         thisFunc = nextFunc;
  330.     } /* endwhile */
  331.  
  332.     return;
  333. }
  334.  
  335. struct fileInfo *endOfFileList( struct fileInfo *psFileInfo )
  336. {
  337.     struct fileInfo *next = NULL;
  338.     struct fileInfo *tail = NULL;
  339.  
  340.     tail = psFileInfo;
  341.     next = tail->next;
  342.  
  343.     while ( next != NULL ) {
  344.         tail = next;
  345.         next = tail->next;
  346.     } /* endwhile */
  347.  
  348.     return( tail );
  349. }
  350.  
  351. struct fileInfo *startOfFileList( struct fileInfo *psFileInfo )
  352. {
  353.     #ifdef MY_DEBUG
  354.     char            *thisFuncNm = "startOfFileList\0";
  355.     #endif
  356.     struct fileInfo *prev = NULL;
  357.     struct fileInfo *head = NULL;
  358.  
  359.     #ifdef MY_DEBUG
  360.     TRACE_ENTRY( thisFuncNm );
  361.     #endif
  362.  
  363.     head = psFileInfo;
  364.     prev = head->prev;
  365.  
  366.     while ( prev != NULL ) {
  367.         head = prev;
  368.         prev = head->prev;
  369.     } /* endwhile */
  370.  
  371.     #ifdef MY_DEBUG
  372.     TRACE_EXIT( thisFuncNm );
  373.     #endif
  374.  
  375.     return( head );
  376. }
  377.  
  378. struct isCalledByFuncInfo *endOfCalledByFuncList( 
  379.           struct isCalledByFuncInfo *psFuncInfo )
  380. {
  381.     struct isCalledByFuncInfo *next = NULL;
  382.     struct isCalledByFuncInfo *tail = NULL;
  383.  
  384.     tail = psFuncInfo;
  385.     next = tail->next;
  386.  
  387.     while ( next != NULL ) {
  388.         tail = next;
  389.         next = tail->next;
  390.     } /* endwhile */
  391.  
  392.     return( tail );
  393. }
  394.  
  395. struct isCalledByFuncInfo *startOfCalledByFuncList( 
  396.             struct isCalledByFuncInfo *psFuncInfo )
  397. {
  398.     struct isCalledByFuncInfo *prev = NULL;
  399.     struct isCalledByFuncInfo *head = NULL;
  400.  
  401.     head = psFuncInfo;
  402.     prev = head->prev;
  403.  
  404.     while ( prev != NULL ) {
  405.         head = prev;
  406.         prev = head->prev;
  407.     } /* endwhile */
  408.  
  409.     return( head );
  410. }
  411.  
  412. struct isCalledByFuncInfo *funcInCalledByFuncList( char *funcNm
  413.                              , struct isCalledByFuncInfo *psFuncInfo )
  414. {
  415.     struct isCalledByFuncInfo *next = NULL;
  416.     struct isCalledByFuncInfo *head = NULL;
  417.  
  418.     head = startOfCalledByFuncList( psFuncInfo );
  419.     if ( head == NULL ) {
  420.         return( head );
  421.     } else {
  422.         next = head->next;
  423.     } /* endif */
  424.  
  425.     while ( next != NULL && 
  426.             strncmp( funcNm, head->funcNm, MAXFUNCNAMESIZE ) ) {
  427.         head = next;
  428.         next = head->next;
  429.     } /* endwhile */
  430.  
  431.     if ( strncmp( funcNm, head->funcNm, MAXFUNCNAMESIZE ) ) {
  432.         head = NULL;
  433.     } /* endif */
  434.     return( head );
  435. }
  436.  
  437. struct isCalledByFuncInfo *callerInList( char *funcNm
  438.                          , struct isCalledByFuncInfo *psFuncInfo )
  439. {
  440.     struct isCalledByFuncInfo *next = NULL;
  441.     struct isCalledByFuncInfo *caller = NULL;
  442.  
  443.     caller = psFuncInfo->callingFuncListStart;
  444.     if ( caller == NULL ) {
  445.         return( caller );
  446.     } /* endif */
  447.     next = caller->next;
  448.  
  449.     while ( next != NULL && 
  450.             strncmp( funcNm, caller->funcNm, MAXFUNCNAMESIZE ) ) {
  451.         caller = next;
  452.         next = caller->next;
  453.     } /* endwhile */
  454.  
  455.     if ( strncmp( funcNm, caller->funcNm, MAXFUNCNAMESIZE ) ) {
  456.         caller = NULL;
  457.     } /* endif */
  458.     return( caller );
  459. }
  460.  
  461. struct funcInfo *nextNotCalledFunc( struct funcInfo *psFuncInfo )
  462. {
  463.     struct funcInfo *aFunc = NULL;
  464.  
  465.     aFunc = psFuncInfo;
  466.  
  467.     if ( aFunc == NULL ) {
  468.         return( aFunc );
  469.     } /* endif */
  470.  
  471.     while ( aFunc != NULL && aFunc->isCalled ) {
  472.         aFunc = aFunc->next;
  473.     } /* endwhile */
  474.  
  475.     return( aFunc );
  476. }
  477.  
  478. struct funcInfo *funcInFileList( char *funcNm
  479.                                , struct fileInfo *pFileStart )
  480. {
  481.     struct fileInfo *nextFile = NULL;
  482.     struct funcInfo *nextFunc = NULL;
  483.     int             strcmpResult = 1;
  484.  
  485.     nextFile = pFileStart;
  486.  
  487.     if ( nextFile == NULL ) {
  488.         return( NULL );
  489.     } /* endif */
  490.  
  491.     while ( nextFile != NULL && strcmpResult ) {
  492.         nextFunc = nextFile->funcListStart;
  493.         while ( nextFunc != NULL && strcmpResult ) {
  494.             strcmpResult = strcmp( funcNm, nextFunc->funcNm );
  495.             if ( !strcmpResult ) {
  496.                 continue;
  497.             } /* endif */
  498.             nextFunc = nextFunc->next;
  499.         } /* endwhile */
  500.         if ( !strcmpResult ) {
  501.             continue;
  502.         } /* endif */
  503.         nextFile = nextFile->next;
  504.     } /* endwhile */
  505.  
  506.     if ( strcmpResult ) {
  507.         nextFunc = NULL;
  508.     } /* endif */
  509.  
  510.     return( nextFunc );
  511. }
  512.  
  513. struct funcInfo *funcInCalledList( char *funcNm
  514.                                  , struct funcInfo *psFuncInfo )
  515. {
  516.     struct funcInfo *next = NULL;
  517.     struct funcInfo *head = NULL;
  518.  
  519.     if ( psFuncInfo == NULL ) {
  520.         return( NULL );
  521.     } /* endif */
  522.  
  523.     head = psFuncInfo->calledFuncListStart;
  524.     if ( head == NULL ) {
  525.         return( head );
  526.     } else {
  527.         next = head->next;
  528.     } /* endif */
  529.  
  530.     while ( next != NULL && 
  531.             strncmp( funcNm, head->funcNm, MAXFUNCNAMESIZE ) ) {
  532.         head = next;
  533.         next = head->next;
  534.     } /* endwhile */
  535.  
  536.     if ( strcmp( funcNm, head->funcNm ) ) {
  537.         head = NULL;
  538.     } /* endif */
  539.  
  540.     return( head );
  541. }
  542.  
  543.