home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_10 / 9n10035b < prev    next >
Text File  |  1991-08-22  |  2KB  |  78 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "c_calls.h"
  6.  
  7. void build_graph(LIST *fcn_list, const char *filename)
  8.  
  9. /*
  10. Build a graph which shows the calling relationships of all fcns in
  11. a C program by parsing the program's assembly language file.
  12. */
  13.  
  14. {
  15. FILE      *input_file;
  16. LINE      line;
  17. TOKENS    toks;
  18. char      *tp;
  19. NAME      caller_name, called_name;
  20. int       i;
  21. LIST      caller_ptr = NULL, called_ptr;
  22.  
  23. if ((input_file = fopen(filename, "r")) == NULL)
  24.      error("cannot open input file");
  25.  
  26. while (!feof(input_file))
  27.      {
  28.      fgets(line, sizeof(line), input_file);
  29.      line[strlen(line) - 1] = '\0';  /* Remove \n from end of line. */
  30.  
  31.      memset(toks, 0, sizeof(toks));
  32.  
  33.      /* Extract the fields from the current line. */
  34.      tp = strtok(line, "\t ");  /* Token separator is a tab or space. */
  35.  
  36.      for (i = 0; (i < MAX_TOKENS) && (tp != NULL); i++)
  37.           {
  38.           strcpy(toks[i], tp);
  39.           tp = strtok(NULL, "\t ");  /* Get next token. */
  40.           }
  41.  
  42.      i--;  /* i now points to last token on current line. */
  43.  
  44.      /* If current line is the start of a fcn... */
  45.      if (stricmp(toks[1], "proc") == 0)
  46.           {
  47.           strcpy(caller_name, &toks[0][1]);
  48.  
  49.           /* Insert fcn into fcn list. */
  50.           insert_cell(fcn_list, caller_name);
  51.  
  52.           /* Make the new fcn the calling fcn. */
  53.           caller_ptr = find_cell(*fcn_list, caller_name);
  54.  
  55.           }
  56.  
  57.      /* If current line is a user-defined fcn call... */
  58.      if ((stricmp(toks[0], "call") == 0) &&
  59.          (toks[i][strlen(toks[i]) - 1] != '@') && (toks[i][1] != '_'))
  60.           {
  61.           strcpy(called_name, &toks[i][1]);
  62.  
  63.           /* Insert called fcn into the fcn list. */
  64.           insert_cell(fcn_list, called_name);
  65.  
  66.           /* Record that the calling fcn is calling this fcn. */
  67.           insert_cell(&caller_ptr->calls, called_name);
  68.  
  69.           /* Record that this fcn is called by the calling fcn. */
  70.           called_ptr = find_cell(*fcn_list, called_name);
  71.           insert_cell(&called_ptr->called_from, caller_name);
  72.           }
  73.      }
  74.  
  75. if (fclose(input_file) != 0)
  76.      error("cannot close input file");
  77. }
  78.