home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 171_01 / xc.c < prev   
Text File  |  1983-10-27  |  26KB  |  943 lines

  1. /*************************************************************************/
  2. /*   9-26-83   Microsoft C 1.04  Conversion    WHR             */
  3. /*     -  \t between line numbers and text to fix indenting problem.     */
  4. /*     -  added option -e for output to Epson in condensed print.     */
  5. /*     -  toupper() and isupper() are macros, not functions.         */
  6. /*     -  eliminate side effect in toupper(*++arg) in main().         */
  7. /*     -  change alloc() to malloc().                     */
  8. /*     -  add #define NAMES that are not in stdio.h             */
  9. /*     -  MS-C requires () in statement A?(c=B):(c=C)    error or not??     */
  10. /*                                     */
  11. /*   4-30-83   Computer Innovations C-86 1.31 Conversion    WHR      */
  12. /*     -  #include filename changed to allow a disk drive prefix, D:     */
  13. /*     -  convert if(fprintf(...) == ERROR) lst_err(); to fprintf(..);     */
  14. /*     -  convert if(fopen(...) == ERROR) statements to == NULL.     */
  15. /*     -  C86 requires () in statement A?(c=B):(c=C)    error or not??     */
  16. /*     -  remove getc() == ERROR check in fil_chr().             */
  17. /*     -  convert file conventions from BDS C to C-86.             */
  18. /*     -  comment out BDS unique statements, mark revised statements.     */
  19. /*      keep all BDS statements to document conversion effort.     */
  20. /*                                     */
  21. /**  4-19-83   BDS C Version file XC.CQ copied from Laurel RCPM    WHR     */
  22. /*************************************************************************/
  23. /*                               */
  24. /*    XC  -  A 'C' Concordance Utility                     */
  25. /*                               */
  26. /*    Version 1.0   January, 1982               */
  27. /*                               */
  28. /*    Copyright (c) 1982 by Philip N. Hisley           */
  29. /*                               */
  30. /*    Released for non-commercial distribution only       */
  31. /*                               */
  32. /*    Abstract:                        */
  33. /*                               */
  34. /*    'XC' is a cross-reference utility for 'C' programs.  */
  35. /*    Its has the ability to handle nested include files   */
  36. /*    to a depth of 8 levels and properly processes nested */
  37. /*    comments as supported by BDS C. Option flags support */
  38. /*    the following features:                   */
  39. /*                               */
  40. /*    - Routing of list output to disk               */
  41. /*    - Cross-referencing of reserved words           */
  42. /*    - Processing of nested include files           */
  43. /*    - Generation of listing only               */
  44. /*                               */
  45. /*    Usage: xc <filename> <flag(s)>               */
  46. /*                               */
  47. /*    Flags: -i        = Enable file inclusion       */
  48. /*         -l        = Generate listing only       */
  49. /*         -r        = Cross-ref reserved words       */
  50. /*         -o <filename> = Write output to named file    */
  51. /*                               */
  52. /*    Please report bugs/fixes/enhancements to:        */
  53. /*                               */
  54. /*          Philip N. Hisley                   */
  55. /*          548H Jamestown Court               */
  56. /*          Edgewood, Maryland 21040               */
  57. /*          (301) 679-4606                   */
  58. /*          Net Addr: PNH@MIT-AI               */
  59. /*                               */
  60. /*                               */
  61. /***********************************************************/
  62.  
  63. /**BDS***  #include "bdscio.h"  */                          /* BDS C */
  64.  
  65. #include <stdio.h>                          /* WHR */
  66. #include <ctype.h>                          /* WHR */
  67.  
  68. #define  NULL        0
  69. #define  FALSE        0                          /* WHR */
  70. #define  TRUE        1                          /* WHR */
  71. #define  CPMEOF   0x1A              /* end of file */       /* WHR */
  72. #define  ERROR      (-1)                          /* WHR */
  73.  
  74. #define  MAX_REF    5        /* maximum refs per ref-block */
  75.  
  76.  
  77.  
  78. #define  MAX_LEN    20        /* maximum identifier length  */
  79. #define  MAX_WRD   749        /* maximum number of identifiers */
  80. #define  MAX_ALPHA  53        /* maximum alpha chain heads */
  81. #define  REFS_PER_LINE    8    /* maximum refs per line */
  82. #define  LINES_PER_PAGE 60
  83. #define  FF 0x0C         /* formfeed */
  84.  
  85. struct    id_blk {
  86.          char  id_name[MAX_LEN];
  87.          struct id_blk *alpha_lnk;
  88.          struct rf_blk *top_lnk;
  89.          struct rf_blk *lst_lnk;
  90.            } oneid;
  91.  
  92. struct    rf_blk {
  93.          int  ref_item[MAX_REF];
  94.          int  ref_cnt;
  95.            } onerf;
  96.  
  97. struct id_blk *id_vector[MAX_WRD];
  98.  
  99. struct alpha_hdr { struct id_blk *alpha_top;
  100.            struct id_blk *alpha_lst;
  101.          };
  102.  
  103. struct alpha_hdr alpha_vector[MAX_ALPHA];
  104.  
  105. int    linum;        /* line number */
  106. int    edtnum;     /* edit line number */
  107. int    fil_cnt;    /* active file index */
  108. int    wrd_cnt;    /* token count */
  109. int    pagno;        /* page number */
  110. int    id_cnt;     /* number of unique identifiers */
  111. int    rhsh_cnt;    /* number of conflict hits */
  112. int    filevl;     /* file level  */
  113. int    paglin;     /* page line counter */
  114. int    prt_ref;
  115. char    act_fil[MAX_LEN];
  116. char    lst_fil[MAX_LEN];
  117. char    gbl_fil[MAX_LEN];
  118. /*BDS**** char      l_buffer[BUFSIZ]; */                 /* BDS C */
  119. FILE   *l_buffer;                         /*   WHR */
  120. int    i_flg,o_flg,r_flg,l_flg;
  121. int    debug;
  122. char    Epson[]  = "\x1B\x40\x0F\x1BQ\x84";                  /* WHR */
  123. int    e_flg;                             /* WHR */
  124. /*-------------------------------------------*/
  125.  
  126. main(argc,argv)
  127.      int     argc;
  128.      char    **argv;
  129. {
  130.      char  *arg;
  131.      char *strcpy();
  132. /*** int  toupper();   ***/             /* Microsoft C   macro */
  133.      char cc;                     /* Microsoft C */
  134.  
  135.      if (argc < 2) use_err();
  136.      i_flg=r_flg=o_flg=l_flg=FALSE;
  137.      debug = FALSE;
  138.      strcpy(gbl_fil,*++argv);
  139.      --argc;
  140.      if(gbl_fil[0] == '-')
  141.       use_err();
  142.      while(--argc != 0)
  143.      {      if(*(arg=*++argv) == '-')
  144.     /*****{    switch( toupper(*++arg) )     *** side effect in Microsoft C */
  145.       {    switch( cc=*++arg, toupper(cc) )          /* Microsoft C */
  146.            {   case 'I':  i_flg++;
  147.                   break;
  148.            case 'R':  r_flg++;
  149.                   break;
  150.            case 'L':  l_flg++;
  151.                   break;
  152.            case 'O': {o_flg++;
  153.                   if(--argc == 0) use_err();
  154.                   strcpy(lst_fil,*++argv);
  155.                   if(lst_fil[0] == '-') use_err();
  156.                   if(debug) printf("lst_fil=>%s<",lst_fil);
  157.                   break;
  158.                  }
  159.            case 'D':  debug++;
  160.                   break;
  161.            case 'E':  e_flg++;                            /* WHR */
  162.                   o_flg++;
  163.                   strcpy(lst_fil,"LPT1:");
  164.                   break;
  165.            default:   use_err();
  166.            }
  167.       }
  168.      else use_err();
  169.      }
  170.      if(debug) printf("\ni_flg=%d, r_flg=%d, l_flg=%d", i_flg,r_flg,l_flg);
  171.      if(debug) printf("\no_flg=%d, debug=%d", o_flg,debug);
  172.      if (o_flg)
  173. /*BDS{      if ( (l_buffer = fopen(lst_fil,"w")) == ERROR) ** output file **BDS*/
  174.  
  175.      {      if ( (l_buffer = fopen(lst_fil,"w")) == NULL)   /*** output file ***/
  176.       {    printf("ERROR: Unable to create list file - %s\n",lst_fil);
  177.            exit(0);
  178.       }
  179.       printf("\nXC ....... 'C' Concordance Utility  v1.0\n");
  180.       printf("    Microsoft C 1.04,  Conversion 9-26-83\n\n");  /* WHR */
  181.       if (e_flg) fprintf(l_buffer,"%s",Epson);
  182.      }
  183.                                      /* WHR */
  184.   /**BDS***   _allocp = NULL;**/ /* initialize memory allocation pointer */
  185.      prt_ref = FALSE;
  186.      for(linum=0;linum < MAX_WRD;linum++) {
  187.       id_vector[linum] = NULL;
  188.      }
  189.      for(linum=0;linum < MAX_ALPHA;linum++)
  190.      {
  191.       alpha_vector[linum].alpha_top =
  192.            alpha_vector[linum].alpha_lst = NULL;
  193.      }
  194.      fil_cnt = wrd_cnt = linum = 0;
  195.      filevl=paglin=pagno=edtnum=0;
  196.      id_cnt=rhsh_cnt=0;
  197.      proc_file(gbl_fil);
  198.      if(!l_flg) {
  199.      prnt_tbl();
  200.      printf("\nAllowable Symbols: %d\n",MAX_WRD);
  201.      printf("Unique    Symbols: %d\n",id_cnt);
  202.      }
  203.      if(o_flg) {
  204.       nl();
  205.       fprintf(l_buffer,"\nAllowable Symbols: %d\n",MAX_WRD);   /* WHR */
  206.       fprintf(l_buffer,"Unique    Symbols: %d\n",id_cnt);      /* WHR */
  207.       fprintf(l_buffer,"%c%c",FF,CPMEOF);                      /* WHR */
  208.       /*BDSif(fprintf(l_buffer,"%c",CPMEOF) == ERROR) lst_err();  **/
  209.       fflush(l_buffer);
  210.       fclose(l_buffer);
  211.      }
  212. }/*main.
  213. ----------------------------------------*/
  214.  
  215. strcmp(s,t)
  216.      char s[], t[];
  217. {
  218.      int i;
  219.      i=0;
  220.      while(s[i] == t[i])
  221.       if(s[i++] == '\0') return (0);
  222.      return (s[i]-t[i]);
  223. }/*strcmp.
  224. ----------------------------------------*/
  225.  
  226.  
  227. char *strcpy(s1,s2)
  228.      char *s1, *s2;
  229. {
  230.      char  *temp;
  231.      temp=s1;
  232.      while (*s1++ = *s2++);
  233.      return (temp);
  234. }/*strcpy.
  235. ----------------------------------*/
  236.  
  237.  
  238. lst_err()
  239. {
  240.      printf("\nERROR: Write error on list output file - %s\n",
  241.      lst_fil);
  242.      exit(0);
  243. }/*lst_err.
  244. -----------------------------------*/
  245.  
  246.  
  247. use_err()
  248. {
  249.      printf("\nERROR: Invalid parameter specification\n\n");
  250.      printf("Usage: xc <filename> <flag(s)>\n\n");
  251.      printf("Flags: -i             = Enable file inclusion\n");
  252.      printf("       -l             = Generate listing only\n");
  253.      printf("       -r             = Cross-reference reserved words\n");
  254.      printf("       -o <filename>  = Write output to named file\n");
  255.      exit(0);
  256. }/*use_err.
  257. -------------------------------------------*/
  258.  
  259.  
  260. proc_file(filnam)
  261.      char    *filnam;
  262.  
  263. {
  264.      FILE  *buffer;       /* allocated buffer pointer */        /* WHR */
  265.   /***BDS***   char  *buffer;         * allocated buffer pointer */
  266.   /***BDS***   char  *sav_buffer;    * saved alloc buffer pointer */
  267.      char  token[MAX_LEN]; /* token buffer */
  268.      int   eof_flg;       /* end-of-file indicator */
  269.      int   tok_len;       /* token length */
  270.      int   incnum;       /* included line number */
  271.      char *strcpy();
  272.  
  273.      strcpy(act_fil,filnam);
  274.   /*   BDS C stuff                         * WHR *
  275.   **   if ((sav_buffer = buffer =  alloc(BUFSIZ)) == 0) {
  276.   **       printf("\nERROR: Unable to allocate file buffer for %s\n",filnam);
  277.   **       exit(0);
  278.   **  }
  279.   **  if (fopen(filnam,buffer)) == ERROR)   *** input file ***
  280.   */
  281.      if ((buffer = fopen(filnam,"r")) == NULL)  /*** input file ***/ /* WHR */
  282.      {      printf("\nERROR: Unable to open input file: %s\n",filnam);
  283.       exit(0);
  284.      }
  285.      if(filevl++ == 0) prt_hdr();
  286.      eof_flg = FALSE;
  287.  
  288.      do {
  289.       if(get_token(buffer,token,&tok_len,&eof_flg,0))
  290.       {    if (debug) printf("\ntoken: %s   length: %d\n",token,&tok_len);
  291.            if (chk_token(token))
  292.            {
  293.              /* #include processing changed to accept drive:   WHR */
  294.             if (strcmp(token,"#include") == 0)
  295.             {     if (get_token(buffer,token,&tok_len,&eof_flg,1))
  296.              {    if (debug) printf("\ntoken: %s   length: %d\n",
  297.                           token,&tok_len);
  298.                   if (!i_flg) continue;
  299.                   else
  300.                   {    incnum=edtnum;
  301.                    edtnum=0;
  302.                    nl();
  303.                    proc_file(token);
  304.                    edtnum=incnum;
  305.                    strcpy(act_fil,filnam);
  306.  
  307.                    continue;
  308.                   }
  309.              }
  310.             }
  311.             put_token(token,linum);
  312.            }
  313.       }
  314.      } while (!eof_flg);
  315.  
  316.      filevl -= 1;
  317.      fclose(buffer);
  318.  /***     free(sav_buffer);  */
  319. }/*proc_file.
  320. -------------------------------------------*/
  321.  
  322. get_token(g_buffer,g_token,g_toklen,g_eoflg,g_flg)
  323.      FILE    *g_buffer;
  324.      char    *g_token;
  325.      int     *g_toklen;
  326.      int     *g_eoflg;
  327.      int     g_flg;
  328.  
  329.     /*
  330.      *     'getoken' returns the next valid identifier or
  331.      *     reserved word from a given file along with the
  332.      *     character length of the token and an end-of-file
  333.      *     indicator
  334.      *
  335.      */
  336.  
  337. {
  338.      int     c;
  339.      char    *h_token;
  340.      char    tmpchr;
  341.      char    tmpchr2;              /* WHR fix for B:filename.ext */
  342.  
  343.      h_token = g_token;
  344.  
  345.  gtk:                   /* top of loop, get new token */
  346.      *g_toklen = 0;
  347.      g_token = h_token;
  348.  
  349.     /*
  350.      *    Scan and discard any characters until an alphabetic or
  351.      *    '_' (underscore) character is encountered or an end-of-file
  352.      *    condition occurs
  353.      */
  354.  
  355.      while(  (!isalpha(*g_token = rdchr(g_buffer,g_eoflg,g_flg)))
  356.          &&  !*g_eoflg    &&  *g_token != '_'
  357.          && *g_token != '0' &&  *g_token != '#' );
  358.      if(*g_eoflg) return(FALSE);
  359.  
  360.      *g_toklen += 1;
  361.  
  362.     /*
  363.      *     Scan and collect identified alpanumeric token until
  364.      *     a non-alphanumeric character is encountered or and
  365.      *     end-of-file condition occurs
  366.      */
  367.  
  368.      if(g_flg) {
  369.       tmpchr  = '.';
  370.       tmpchr2 = ':';                   /* WHR fix for B:filename.ext */
  371.      }
  372.      else {
  373.       tmpchr  = '_';
  374.       tmpchr2 = '_';                   /* WHR fix for B:filename.ext */
  375.      }
  376.      while( (isalpha(c=rdchr(g_buffer,g_eoflg,g_flg)) ||
  377.         isdigit(c) || c == '_' || c == tmpchr || c == tmpchr2)
  378.         && !*g_eoflg)           /* WHR fix for B:filename.ext */
  379.      {      if(*g_toklen < MAX_LEN)
  380.       {    *++g_token = c;
  381.            *g_toklen += 1;
  382.       }
  383.      }
  384.  
  385.  
  386.     /*
  387.      *        Check to see if a numeric hex or octal constant has
  388.      *        been encountered ... if so dump it and try again
  389.      */
  390.  
  391.  
  392.      if (*h_token == '0') goto gtk;
  393.  
  394.  
  395.     /*
  396.      *        Tack a NULL character onto the end of the token
  397.      */
  398.  
  399.      *++g_token = NULL;
  400.  
  401.     /*
  402.      *        Screen out all #token strings except #include
  403.      */
  404.  
  405.      if (*h_token == '#' && strcmp(h_token,"#include")) goto gtk;
  406.  
  407.      return (TRUE);
  408. }/*get_token.
  409. -----------------------------------------*/
  410.  
  411. fil_chr(f_buffer,f_eof)
  412.      FILE *f_buffer;
  413.      int *f_eof;
  414. {
  415.      int fc;
  416.      fc=getc(f_buffer);
  417. /*   if(fc == ERROR)       **** BDS stuff ****                 ** WHR **
  418. **   {      printf("\nERROR: Error while processing input file - %s\n",
  419. **      act_fil);
  420. **      exit(0);
  421. **   }
  422. */
  423.      if (fc == CPMEOF || fc == EOF) {
  424.       *f_eof = TRUE;
  425.       fc = NULL;
  426.      }
  427.      return(fc);
  428. }/*fil_chr.
  429. ----------------------------------------*/
  430.  
  431. rdchr(r_buffer,r_eoflg,rd_flg)
  432.      int     *r_eoflg;
  433.      FILE    *r_buffer;
  434.      int     rd_flg;
  435.  
  436. /*
  437.     'rdchr' returns the next valid character in a file
  438.     and an end-of-file indicator. A valid character is
  439.     defined as any which does not appear in either a
  440.     commented or a quoted string ... 'rdchr' will correctly
  441.     handle comment tokens which appear within a quoted
  442.     string
  443. */
  444.  
  445. {
  446.      int     c;
  447.      int     q_flg;         /* double quoted string flag */
  448.      int     q1_flg;         /* single quoted string flag */
  449.      int     cs_flg;         /* comment start flag */
  450.      int     ce_flg;         /* comment end flag */
  451.      int     c_cnt;         /* comment nesting level */
  452.      int     t_flg;         /* transparency flag */
  453.  
  454.      q_flg = FALSE;
  455.      q1_flg = FALSE;
  456.      cs_flg = FALSE;
  457.      ce_flg = FALSE;
  458.      t_flg = FALSE;
  459.      c_cnt  = 0;
  460.  
  461. rch:
  462.  
  463.  
  464.     /*
  465.      *     Fetch character from file
  466.      */
  467.  
  468.      c = fil_chr(r_buffer,r_eoflg);
  469.  
  470.      if (*r_eoflg) return(c);    /* EOF encountered */
  471.      if (c == '\n')
  472.        nl();
  473.      else
  474.       if (o_flg)
  475.            fprintf(l_buffer,"%c",c);                        /* WHR */
  476.       /* BDSC  {    if (fprintf(l_buffer,"%c",c) == ERROR)      ** WHR **
  477.        *         lst_err();
  478.        *       }
  479.        */
  480.       else
  481.            printf("%c",c);
  482.  
  483.      if (rd_flg) return(c);
  484.  
  485.      if (t_flg) {
  486.       t_flg = !t_flg;
  487.       goto rch;
  488.      }
  489.  
  490.      if (c == '\\') {
  491.       t_flg = TRUE;
  492.       goto rch;
  493.      }
  494.     /*
  495.     If the character is not part of a quoted string
  496.     check for and process commented strings...
  497.     nested comments are handled correctly but unbalanced
  498.     comments are not ... the assumption is made that
  499.     the syntax of the program being xref'd is correct
  500.     */
  501.  
  502.      if (!q_flg  &&  !q1_flg) {
  503.       if (c == '*'  &&  c_cnt  &&  !cs_flg) {
  504.            ce_flg = TRUE;
  505.            goto rch;
  506.       }
  507.       if (c == '/'  &&  ce_flg) {
  508.            c_cnt -= 1;
  509.            ce_flg = FALSE;
  510.            goto rch;
  511.       }
  512.       ce_flg = FALSE;
  513.       if (c == '/') {
  514.            cs_flg = TRUE;
  515.            goto rch;
  516.       }
  517.       if (c == '*'  &&  cs_flg) {
  518.            c_cnt += 1;
  519.            cs_flg = FALSE;
  520.            goto rch;
  521.       }
  522.       cs_flg = FALSE;
  523.  
  524.       if (c_cnt) goto rch;
  525.      }
  526.  
  527.     /*
  528.     Check for and process quoted strings
  529.     */
  530.  
  531.      if ( c == '"'  &&  !q1_flg) {      /* toggle quote flag */
  532.       q_flg =  !q_flg;
  533.       if(debug) printf("\nq_flg toggled to: %d\n" ,q_flg);
  534.       goto rch;
  535.      }
  536.      if (q_flg) goto rch;
  537.  
  538.      if (c == '\'') {        /* toggle quote flag */
  539.       q1_flg = !q1_flg;
  540.       if(debug) printf("\nq1_flg toggled to: %d\n" ,q1_flg);
  541.       goto rch;
  542.      }
  543.      if (q1_flg) goto rch;
  544.  
  545.     /*
  546.     Valid character ... return to caller
  547.     */
  548.  
  549.      return (c);
  550. }/*rdchr.
  551. -----------------------------------------------*/
  552.  
  553.  
  554. chk_token(c_token)
  555.      char    *c_token;
  556. {
  557.      char  u_token[MAX_LEN];
  558.      int   i;
  559.  
  560.      {
  561.       if (r_flg) return(TRUE);
  562.       i = 0;
  563.       do { u_token[i] = toupper(c_token[i]);
  564.       } while (c_token[i++] != NULL);
  565.  
  566.       switch(u_token[0]) {
  567.     case 'A': if (strcmp(u_token,"AUTO") == 0) return(FALSE);
  568.           break;
  569.     case 'B': if (strcmp(u_token,"BREAK") == 0) return(FALSE);
  570.           break;
  571.     case 'C': if (strcmp(u_token,"CHAR") == 0) return (FALSE);
  572.           if (strcmp(u_token,"CONTINUE") == 0) return (FALSE);
  573.           if (strcmp(u_token,"CASE") == 0) return (FALSE);
  574.           break;
  575.  
  576.     case 'D': if(strcmp(u_token,"DOUBLE") == 0) return(FALSE);
  577.           if(strcmp(u_token,"DO") == 0) return(FALSE);
  578.           if(strcmp(u_token,"DEFAULT") == 0) return(FALSE);
  579.           break;
  580.     case 'E': if(strcmp(u_token,"EXTERN") == 0) return(FALSE);
  581.           if(strcmp(u_token,"ELSE") == 0) return(FALSE);
  582.           if(strcmp(u_token,"ENTRY") == 0) return(FALSE);
  583.           break;
  584.     case 'F': if(strcmp(u_token,"FLOAT") == 0) return(FALSE);
  585.           if(strcmp(u_token,"FOR") == 0) return(FALSE);
  586.           break;
  587.     case 'G': if(strcmp(u_token,"GOTO") == 0) return(FALSE);
  588.           break;
  589.     case 'I': if(strcmp(u_token,"INT") == 0) return(FALSE);
  590.           if(strcmp(u_token,"IF") == 0) return(FALSE);
  591.           break;
  592.     case 'L': if(strcmp(u_token,"LONG") == 0) return(FALSE);
  593.           break;
  594.     case 'R': if(strcmp(u_token,"RETURN") == 0) return(FALSE);
  595.           if(strcmp(u_token,"REGISTER") == 0) return(FALSE);
  596.           break;
  597.     case 'S': if(strcmp(u_token,"STRUCT") == 0) return(FALSE);
  598.           if(strcmp(u_token,"SHORT") == 0) return(FALSE);
  599.           if(strcmp(u_token,"STATIC") == 0) return(FALSE);
  600.           if(strcmp(u_token,"SIZEOF") == 0) return(FALSE);
  601.           if(strcmp(u_token,"SWITCH") == 0) return(FALSE);
  602.           break;
  603.     case 'T': if(strcmp(u_token,"TYPEDEF") == 0) return(FALSE);
  604.           break;
  605.     case 'U': if(strcmp(u_token,"UNION") == 0) return(FALSE);
  606.           if(strcmp(u_token,"UNSIGNED") == 0) return(FALSE);
  607.           break;
  608.     case 'W': if(strcmp(u_token,"WHILE") == 0) return(FALSE);
  609.           break; }
  610.     }
  611.   return (TRUE);
  612. }/*chk_token.
  613. ---------------------------------------------*/
  614.  
  615.  
  616.   /*
  617.    *    Install parsed token and line reference in linked structure
  618.    */
  619.  
  620. put_token(p_token,p_ref)
  621.      char *p_token;
  622.      int  p_ref;
  623. {
  624.      int  hsh_index;
  625.      int  i;
  626.      int  j;
  627.      int  d;
  628.      int  found;
  629.      struct id_blk *idptr;
  630.      struct rf_blk *rfptr;
  631.      struct id_blk *alloc_id();
  632.      struct rf_blk *alloc_rf();
  633.      struct rf_blk *add_rf();
  634.  
  635.      if (l_flg) return;
  636.      j=0;
  637.      for (i=0; p_token[i] != NULL; i++)  /* Hashing algorithm is far from */
  638.      {                     /* optimal but is adequate for a */
  639.      j = j * 10 + p_token[i];     /* memory-bound index vector!      */
  640.      }
  641.      hsh_index = abs(j) % MAX_WRD;
  642.  
  643.      found = FALSE;
  644.      d = 1;
  645.      do
  646.      {      idptr = id_vector[hsh_index];
  647.       if (idptr == NULL)
  648.       {    id_cnt++;
  649.            idptr = id_vector[hsh_index] = alloc_id(p_token);
  650.            chain_alpha(idptr,p_token);
  651.            idptr->top_lnk = idptr->lst_lnk = alloc_rf(p_ref);
  652.            found = TRUE;
  653.       }
  654.       else
  655.       if (strcmp(p_token,idptr->id_name) == 0)
  656.       {    idptr->lst_lnk = add_rf(idptr->lst_lnk,p_ref);
  657.            found = TRUE;
  658.       }
  659.       else
  660.       {    hsh_index += d;
  661.            d += 2;
  662.            rhsh_cnt++;
  663.            if (hsh_index >= MAX_WRD)
  664.             hsh_index -= MAX_WRD;
  665.            if (d == MAX_WRD)
  666.            {    printf("\nERROR: Symbol table overflow\n");
  667.             exit(0);
  668.            }
  669.       }
  670.      } while (!found);
  671. }/*put_token.
  672. --------------------------------------------*/
  673.  
  674.  
  675. chain_alpha(ca_ptr,ca_token)
  676.      struct id_blk *ca_ptr;
  677.      char  *ca_token;
  678. {
  679.      char  c;
  680.      int   f;
  681.      struct id_blk *cur_ptr;
  682.      struct id_blk *lst_ptr;
  683. /*** int isupper();   ****/           /** Microsoft C     macro **/
  684.  
  685.  
  686.      c = ca_token[0];
  687.      if (c == '_')  c = 0;
  688.      else
  689.      /**  isupper(c) ? c=1+((c-'A')*2) : c=2+((c-'a')*2) ;     error or not??
  690.       **  A good one for the puzzle book! Is the () required around (c=..)?
  691.       **  C86 and Microsoft C both req the ()'s, BDS C did not.
  692.       **  Is it required because = has lower precedence than ?: ????
  693.       **/
  694.       isupper(c) ? (c=1+((c-'A')*2)) : (c=2+((c-'a')*2)) ;
  695.  
  696.      if(alpha_vector[c].alpha_top == NULL)
  697.      {      alpha_vector[c].alpha_top =
  698.           alpha_vector[c].alpha_lst = ca_ptr;
  699.       ca_ptr->alpha_lnk = NULL;
  700.       return;
  701.      }
  702.  
  703.     /*    check to see if new id_blk should be inserted between
  704.      *    the alpha_vector header block and the first id_blk in
  705.      *    the current alpha chain
  706.      */
  707.  
  708.      if(strcmp(alpha_vector[c].alpha_top->id_name,ca_token) >0)
  709.      {      ca_ptr->alpha_lnk=alpha_vector[c].alpha_top;
  710.       alpha_vector[c].alpha_top=ca_ptr;
  711.       return;
  712.      }
  713.  
  714.      if(strcmp(alpha_vector[c].alpha_lst->id_name,ca_token) < 0)
  715.      {      alpha_vector[c].alpha_lst->alpha_lnk = ca_ptr;
  716.       ca_ptr->alpha_lnk = NULL;
  717.       alpha_vector[c].alpha_lst=ca_ptr;
  718.       return;
  719.      }
  720.  
  721.      cur_ptr = alpha_vector[c].alpha_top;
  722.      while(strcmp(cur_ptr->id_name,ca_token) < 0)
  723.      {      lst_ptr = cur_ptr;
  724.       cur_ptr = lst_ptr->alpha_lnk;
  725.      }
  726.  
  727.      lst_ptr->alpha_lnk = ca_ptr;
  728.      ca_ptr->alpha_lnk = cur_ptr;
  729.      return;
  730. }/*chain_alpha.
  731. -----------------------------------------*/
  732.  
  733.  
  734.  
  735.  
  736.      struct id_blk
  737. *alloc_id(aid_token)
  738.      char  *aid_token;
  739. {
  740.      int  ai;
  741.      struct id_blk *aid_ptr;
  742.      char *malloc();                         /* Microsoft C */
  743.  
  744. /*** if((aid_ptr =  alloc(sizeof(oneid))) == 0) { ***/         /* Microsoft C */
  745.      if((aid_ptr = (struct id_blk *) malloc(sizeof(oneid))) == 0) { /* MS C */
  746.  
  747.       printf("\nERROR: Unable to allocate identifier block\n");
  748.       exit(0);
  749.      }
  750.      ai=0;
  751.      do {
  752.       aid_ptr->id_name[ai] = aid_token[ai];
  753.      } while (aid_token[ai++] != NULL);
  754.      return (aid_ptr);
  755. }/*id_blk.
  756. -----------------------------------------*/
  757.  
  758.      struct rf_blk
  759. *alloc_rf(arf_ref)
  760.      int  arf_ref;
  761. {
  762.     int ri;
  763.     struct rf_blk *arf_ptr;
  764.     char * malloc();                       /* Microsoft C */
  765.  
  766. /** if((arf_ptr = alloc(sizeof(onerf))) == 0) { **/    /* Microsoft C */
  767.     if((arf_ptr = (struct rf_blk *) malloc(sizeof(onerf))) == 0) { /* MS C */
  768.      printf("\nERROR: Unable to allocate reference block\n");
  769.      exit(0);
  770.     }
  771.     arf_ptr->ref_item[0] = arf_ref;
  772.     arf_ptr->ref_cnt = 1;
  773.     for (ri=1;ri<MAX_REF;ri++)
  774.       arf_ptr->ref_item[ri] = NULL;
  775.     return (arf_ptr);
  776. }/*alloc_rf.
  777. ------------------------------------------*/
  778.  
  779.  
  780.      struct rf_blk
  781. *add_rf(adr_ptr,adr_ref)
  782.  
  783.      struct rf_blk *adr_ptr;
  784.      int adr_ref;
  785. {
  786.      struct rf_blk *tmp_ptr;
  787.  
  788.      tmp_ptr = adr_ptr;
  789.      if(adr_ptr->ref_cnt == MAX_REF) {
  790.      /*** tmp_ptr = adr_ptr->ref_cnt = alloc_rf(adr_ref);    Microsoft C **/
  791.       adr_ptr->ref_cnt = alloc_rf(adr_ref);       /* Microsoft C **/
  792.       tmp_ptr = (struct rf_blk *) adr_ptr->ref_cnt;   /* Microsoft C **/
  793.      }
  794.      else
  795.      {      adr_ptr->ref_item[adr_ptr->ref_cnt++] = adr_ref;
  796.      }
  797.      return (tmp_ptr);
  798. }/*rf_blk.
  799. ------------------------------------------*/
  800.  
  801. prnt_tbl()
  802. {
  803.      int prf_cnt;
  804.      int pti;
  805.      int pref;
  806.      int lin_cnt;
  807.      struct id_blk *pid_ptr;
  808.      struct rf_blk *ptb_ptr;
  809.  
  810.      prt_ref = TRUE;
  811.      prt_hdr();
  812.      for (pti=0;pti<MAX_ALPHA;pti++)
  813.      {      if ((pid_ptr = alpha_vector[pti].alpha_top) != NULL)
  814.       {    do
  815.            {    if(o_flg)
  816.              fprintf(l_buffer,"%-14.13s: ",pid_ptr->id_name);/*WHR*/
  817.        /* BDSC  {     if(fprintf(l_buffer,"%-14.13s: ",
  818.         *                pid_ptr->id_name) == ERROR)
  819.         *         lst_err();
  820.         *        }
  821.         */
  822.             else
  823.              printf("%-14.13s: ",pid_ptr->id_name);
  824.             ptb_ptr=pid_ptr->top_lnk;
  825.             lin_cnt=prf_cnt=0;
  826.             do
  827.             {     if(prf_cnt == MAX_REF)
  828.              {    prf_cnt=0;
  829.              /*** ptb_ptr = ptb_ptr->ref_cnt;  Microsoft C **/
  830.                   ptb_ptr = (struct rf_blk *)ptb_ptr->ref_cnt;
  831.              }
  832.              if(ptb_ptr > MAX_REF)
  833.              {
  834.                   if((pref=ptb_ptr->ref_item[prf_cnt++]) != 0)
  835.                   {    if(o_flg)
  836.                      fprintf(l_buffer,"%-4d  ",pref); /*WHR*/
  837.               /* BDSC  { if(fprintf(l_buffer,"%-4d  ",pref)
  838.                *             == ERROR) lst_err();
  839.                *       }
  840.                */
  841.                    else
  842.                     printf("%-4d  ",pref);
  843.                    if (++lin_cnt == REFS_PER_LINE)
  844.                    {    nl();
  845.                     if(o_flg)
  846.                        fprintf(l_buffer,"\t\t");   /*WHR*/
  847.                 /* BDSC    {  if(fprintf(l_buffer,"\t\t")
  848.                  *             == ERROR) lst_err();
  849.                  *        }
  850.                  */
  851.                     else
  852.                          printf("\t\t");
  853.                     lin_cnt=0;
  854.                    }
  855.                   }
  856.              }
  857.              else pref=0;
  858.             } while (pref);
  859.             if (lin_cnt = 0)
  860.              nl();
  861.             else
  862.             {     nl();      /**  nl(); ***single space****/
  863.             }
  864.            } while ((pid_ptr=pid_ptr->alpha_lnk) != NULL);
  865.       }
  866.      }
  867. }/*prnt_tbl.
  868. ---------------------------------------*/
  869.  
  870.  
  871. prt_hdr()
  872.  
  873. {
  874.      if (pagno++ != 0)
  875.      if(o_flg)
  876.       fprintf(l_buffer,"%c",FF);                          /* WHR */
  877. /*BDS{      if(fprintf(l_buffer,"%c",FF) == ERROR) lst_err();
  878.  *   }
  879.  */
  880.      else printf("%c",FF);
  881.  
  882.      if(o_flg)
  883.       fprintf(l_buffer,
  884.             "XC ... 'C' Concordance Utility : %s\t\t\t\t Page %d",
  885.             gbl_fil,pagno);                   /* WHR */
  886. /*BDS{      if(fprintf(l_buffer,
  887.  *            "XC ... 'C' Concordance Utility : %s\t\t\t\t Page %d",
  888.  *            gbl_fil,pagno) == ERROR) lst_err();
  889.  *   }
  890.  */
  891.      else
  892.       printf("XC ... 'C' Concordance Utility : %s\t\t\t\t Page %d",
  893.           gbl_fil,pagno);
  894.      if (o_flg)
  895.       fprintf(l_buffer,"\n\n");                               /* WHR */
  896. /*BDS{      if(fprintf(l_buffer,"\n\n") == ERROR) lst_err();
  897.  *   }
  898.  */
  899.      else     printf("\n\n");
  900.  
  901.      nl();
  902.      paglin =3;
  903.      return;
  904. }/*prt_hdr.
  905. --------------------------------------*/
  906.  
  907.  
  908. nl()
  909.  
  910. {
  911.      if (o_flg)
  912.       fprintf(l_buffer,"\n");                                /* WHR */
  913. /*BDS{      if(fprintf(l_buffer,"\n") == ERROR) lst_err();
  914.  *   }
  915.  */
  916.      else
  917.       printf("\n");
  918.  
  919.      if (++paglin == LINES_PER_PAGE) prt_hdr();
  920.      else
  921.       if(!prt_ref)
  922.       {    if(o_flg)
  923.             fprintf(l_buffer,"%-4d %4d:\t",
  924.                 ++linum,++edtnum);         /* WHR */
  925. /*BDS           {    if (fprintf(l_buffer,"%-4d %4d:  ",
  926.  *                ++linum,++edtnum) == ERROR)
  927.  *             lst_err();
  928.  *           }
  929.  */
  930.            else
  931.             printf("%-4d %4d:\t",++linum,++edtnum);
  932.  
  933.            if(o_flg)
  934.             if(linum % 60 == 1)
  935.              printf("\n<%d>\t",linum);
  936.             else
  937.              printf(".");
  938.       }
  939.      return;
  940. }/*nl.
  941. -------------------------------------------*/
  942. /*============= end of file xc.c ==========================*/
  943.