home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / c-util.zip / XC.C < prev    next >
Text File  |  1984-02-04  |  25KB  |  930 lines

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