home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol271 / crosfunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-05-31  |  8.8 KB  |  498 lines

  1.  /* C-function cross reference generator 85-08-31 Ver 2.12*/
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <cpm.h>
  6.  
  7. #define VERSION       "Ver 2.12 85-08-31"
  8. #define ERROR      1
  9. #define READ       "r"
  10. #define WRITE      "w"
  11. #define EOF       (-1)
  12.  
  13. #define TSIZE      10000
  14. #define OSIZE      2000
  15. #define FSIZE       2000
  16. #define NSIZE      500
  17. #define CHARMAX    256
  18.  
  19. #define NEWLINE    '\n'
  20. #define CR       '\r'
  21. #define SPACE      0x20
  22. #define OTHERS     0
  23. #define TOKENSTART 1
  24. #define TOKENCONTINUE 2
  25.  
  26. #define CR_MAP1       1
  27. #define CR_FUNC       2
  28.  
  29. #define getchar fgetc
  30.  
  31. struct occ {
  32.     int         line;        /* appearance line         */
  33.     struct occ  *next;        /* next occ addr pointer     */
  34.     };
  35.  
  36. struct node {
  37.     char            *cont;        /* ptr to name table         */
  38.     int             defline;    /* function definition line    */
  39.     struct funcdef    *funcp;        /* ptr to funcdef        */
  40.     int        nextdef;    /* next definition function no    */
  41.     struct occ      *occp;        /* ptr to occ  table         */
  42.     struct node *left, *right;    /* ptr to next node        */
  43.     };
  44.  
  45. struct funcdef {
  46.     struct node    *nodep;        /* ptr to node            */
  47.     struct funcdef    *nextp;        /* ptr to next funcdef        */
  48.     };
  49.  
  50. struct occ     oheap[OSIZE];
  51. struct node     nheap[NSIZE] =        {NULL};
  52. char         chtable[CHARMAX] = {NULL};
  53. struct node     *keyword = NULL;
  54. FILE         *fp1,*fp2;
  55.  
  56. int     firstfp  = 0;            /* index to first *fheap    */
  57. struct  node     *deffunc = {NULL};    /* ptr to function name(node)    */
  58. int     brace    = 0;
  59. int tp = NULL, tpnew = NULL, op = NULL, np = NULL, printcol = NULL;
  60. int fp = NULL;
  61.  
  62. int    page     = 0;
  63. int    line     = 60;
  64. char    *headp     = {NULL};
  65. char    *secondp = {NULL};
  66. char    *ttl[10] = {
  67.      "Function Cross Reference Map (Alphabetic Order & Line-no)",
  68.      "  Funcname            def       ref",
  69.      "Function Cross Reference Map (Referance Function Map)"
  70.            };
  71.  
  72. int      hlength  = 10;
  73. int     tblp     = 0;
  74. char     chtbl[512];
  75. int     ln;
  76.  
  77. main(argc,argv)
  78.  
  79. int argc;
  80. char *argv[];
  81. {
  82.     char tsave[TSIZE];
  83.     FILE    *fopen();
  84.     struct node *insertfile();
  85.     struct node *root;
  86.     int i, c;
  87.     char *s;
  88.     int truefunc(), keycheck();
  89.  
  90.     chtbl[0]=NULL;
  91.      strcat(chtbl,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_\n");
  92.      strcat(chtbl,"0123456789\n");
  93.      strcat(chtbl,"int char struct union unsigned auto extern register ");
  94.      strcat(chtbl,"short long ");
  95.      strcat(chtbl,"typedef static goto sizeof break continue return ");
  96.      strcat(chtbl,"if else for do while switch case default entry ");
  97.      strcat(chtbl,"define ifdef ifndef undef include line ");
  98.     if (argc<2 || argc >4) {
  99.         printf("usage : A>crosfunc objectfile [x_r_file] [-s -l]\n");
  100.         exit();
  101.     }
  102.     if( argc == 3 && *argv[2]=='-') {
  103.         if (*(argv[2]+1)=='s')
  104.             hlength=10;
  105.         if (*(argv[2]+1)=='l')
  106.             hlength=20;
  107.         --argc;
  108.     }
  109.     if( argc == 4 && *argv[3]=='-') {
  110.         if (*(argv[3]+1)=='s')
  111.             hlength=10;
  112.         if (*(argv[3]+1)=='l')
  113.             hlength=20;
  114.         --argc;
  115.     }
  116.     for (i=0; i<CHARMAX; i++)
  117.         chtable[i] = OTHERS;
  118.     if (argc == 2)
  119.         fp1=(EOF);
  120.     else {
  121.         s = argv[2];
  122.         if  ((fp1=fopen(s,READ)) == ERROR) {
  123.             printf("Can 't open %s\n",s);
  124.             exit();
  125.         }
  126.     }
  127.     while ((c = getsym(fp1)) != NEWLINE && c != CR) {
  128.         if (c == EOF) {
  129.             printf("unexpected eof\n");
  130.             exit();
  131.         }
  132.          chtable[c] = TOKENSTART;
  133.     }
  134.     while ((c = getsym(fp1)) != NEWLINE && c != CR) {
  135.         if (c == EOF) {
  136.             printf("unexpected eof\n");
  137.             exit();
  138.         }
  139.         chtable[c] = TOKENCONTINUE;
  140.     }
  141.     
  142.     tp = 0;
  143.     op = 0;
  144.     np = 0;
  145.     keyword = insertfile(fp1, truefunc,tsave,FALSE);
  146.     if(fp1!=EOF) close(fp1);
  147.     if ( (fp2=fopen(argv[1],READ)) == ERROR) {
  148.         printf("Can 't open %s\n",argv[1]);
  149.         exit();
  150.     }
  151.     op = 0;ln=0;/* line pointer reset */
  152.     root = insertfile(fp2, keycheck,tsave,TRUE);
  153.     close(fp2);
  154.     headp  = ttl[0];    /* set headinf title */
  155.     secondp= ttl[1];
  156.     print(root,CR_MAP1);
  157.     headp  = ttl[2];
  158.     secondp= ttl[1];
  159.     line=60;
  160.     print(root,CR_FUNC);
  161.         printf("\n");
  162. }
  163.  
  164.  
  165.  
  166. struct node *insertfile(ibuf,cfunc,tsave,mode)
  167. FILE *ibuf;
  168. int (*cfunc)();
  169. char tsave[];
  170. int  mode;
  171. {
  172. struct node *insert();
  173. register int c;
  174. struct node *tree;
  175.  
  176.     tree = 0;
  177.     ln = 1;
  178.     c = getsym(ibuf);
  179.     while (c != EOF){
  180.         if (chtable[c] != TOKENSTART) {
  181.             if(c=='"')
  182.                 c=scanstr(ibuf);
  183.             else if(c=='\'')
  184.                 c=scanquo(ibuf);
  185.             else if(c=='/') { 
  186.                 if((c=getsym(ibuf))==EOF)
  187.                     break;
  188.                 else if(c=='*')
  189.                     c=skipcom(ibuf);
  190.             }
  191.             else if(c=='{') {
  192.                 ++brace;
  193.                 c = getsym(ibuf);
  194.             }
  195.             else if(c=='}') {
  196.                 --brace;
  197.                 c = getsym(ibuf);
  198.             }
  199.             else
  200.                 c = getsym(ibuf);
  201.         }
  202.         else {
  203.             tpnew = tp;
  204.             while (c != EOF && chtable[c] != OTHERS) {
  205.                 if (tpnew > TSIZE - 1) {
  206.                     printf("text buffer overflow\n");
  207.                     exit();
  208.                 }
  209.                 tsave[tpnew++] = c;
  210.                 c = getsym(ibuf);
  211.             }
  212.             tsave[tpnew++] = NULL;
  213.             if(mode==TRUE)
  214.                 c=skipbk(ibuf,c);
  215.             if((mode==FALSE) | (c=='(')) {
  216.                 if ((*cfunc)(&tsave[tp])) {
  217.                     if (op > OSIZE) {
  218.                         printf("occ table overflow\n");
  219.                         exit();
  220.                     }
  221.                     oheap[op].line    = ln;
  222.                     tree = insert(&tsave[tp],tree);
  223.                     op++;
  224.                 }
  225.             }
  226.         }
  227.     }
  228.     return(tree);
  229. }
  230.  
  231. skipbk(ibuf,c)
  232. register int c;
  233. FILE     *ibuf;
  234. {          
  235.     while(c==SPACE)
  236.         c=getsym(ibuf);
  237.     return(c);
  238. }
  239.  
  240. struct node *insert(w, p)
  241. char *w;
  242. struct node *p;
  243. {
  244. struct node *q;
  245.     if (p == NULL) {  /* first appearance */
  246.         if (np >= NSIZE) {
  247.             printf("node table overflow\n");
  248.             exit();
  249.         }
  250.         q = &nheap[np];
  251.         np++;
  252.         q->left  = NULL;
  253.         q->right = NULL;
  254.         q->cont  = w;
  255.         q->funcp = NULL;
  256.         if(brace>0) {/* inner function */
  257.             q->defline = NULL;
  258.             setfunc(q);
  259.         }
  260.         else {
  261.             q->defline = ln;
  262.             deffunc = q;
  263.         }
  264.         tp = tpnew ;
  265.         q->occp = &oheap[op];
  266.         oheap[op].next = NULL;
  267.         return(q);
  268.     }
  269.     else {
  270.         switch (compare(w, p->cont)) {
  271.             case -1 : p->left = insert(w, p->left);
  272.                   break;
  273.             case  0 : oheap[op].next = p->occp;
  274.                   p->occp = &oheap[op];
  275.                   if(brace==0) {
  276.                     p->defline = ln;
  277.                     deffunc    = p;
  278.                   }
  279.                       else {  /* inner function */
  280.                     setfunc(p);
  281.                   }
  282.                   break;
  283.             case  1 : p->right = insert(w, p->right);
  284.                   break;
  285.         }
  286.         return(p);
  287.     }
  288. }
  289.  
  290. setfunc(p)
  291. struct node *p;
  292. {
  293. struct funcdef *q,*malloc();
  294.     if(deffunc->funcp==NULL) { /* First allocation */
  295.         q = deffunc->funcp = malloc(sizeof(q));
  296.         q->nextp=NULL;
  297.         q->nodep=p;
  298.     }
  299.     else {
  300.         q = deffunc->funcp;
  301.         while(q->nextp!=NULL) {
  302.             q = q->nextp;
  303.         }
  304.         q->nextp  = malloc(sizeof(q));
  305.         q      = q->nextp;    /*new sell */
  306.         q->nextp  = NULL;
  307.         q->nodep  = p;
  308.     }
  309. }
  310.  
  311. truefunc()
  312. { return(1); }
  313.  
  314.  
  315. keycheck(w)
  316. char *w;
  317. { return(search(w, keyword)); }
  318.  
  319.  
  320. search(w, tree)
  321. char *w;
  322. struct node *tree;
  323. {
  324.     if (tree == NULL)
  325.         return(1);
  326.     else
  327.         switch (compare(w, tree->cont)) {
  328.             case -1 : return(search(w, tree->left));
  329.             case  0 : return (0);
  330.             case  1 : return(search(w, tree->right));
  331.         }
  332. }
  333.  
  334.  
  335. print(p,mode)
  336. struct node *p;
  337. int         mode;
  338. {
  339.     if (p != NULL) {
  340.         print(p->left,mode);
  341.         newline(1);
  342.         printf("%-20s",p->cont);
  343.         printcol = 0;
  344.         if(p->defline!=NULL)
  345.             printf("%6d%1s",p->defline," ");
  346.         else
  347.             printf("%7s"," ***** ");
  348.         switch(mode) {
  349.             case CR_MAP1 : printl(p->occp);
  350.                        break;
  351.             case CR_FUNC : prinfunc(p->funcp);
  352.                        break;
  353.         }
  354.         print(p->right,mode);
  355.     }
  356. }
  357.  
  358.  
  359. printl(o)    /* Print Crossreference list by line no */
  360. struct occ *o;
  361. {
  362.     if (o != NULL) {
  363.         printl(o->next);
  364.         if (printcol >= hlength) {
  365.             newline(1);
  366.             printf("%27s"," ");
  367.             printcol = 0;
  368.         }
  369.         printf("%5d",o->line);
  370.         printcol++;
  371.     }
  372. }
  373.  
  374. prinfunc(f)    /* Print Crossreference list by function name */
  375. struct funcdef *f;
  376. {
  377.     if (f != NULL) {
  378.         if (printcol > hlength) {
  379.             newline(1);
  380.             printf("%27s"," ");
  381.             printcol = 0;
  382.         }
  383.         printf("%-15s",f->nodep->cont);
  384.         printcol+=4;
  385.         prinfunc(f->nextp);
  386.     }
  387. }
  388. newline(count)
  389. int count;
  390. {
  391. int i;
  392.     if(line>=60) {
  393.         page++;
  394.         printf("\n\f");
  395.         printf("%50s  %17s  %5s %4d",headp,VERSION," PAGE",page);
  396.         printf("\n%-79s\n\n",secondp);
  397.         line=2;
  398.     }
  399.     else if (count>0)
  400.         for(i=1;i<=count;i++) {
  401.             line++;
  402.             printf("\n");
  403.         }
  404. }
  405.  
  406. compare(a, b)
  407. char *a, *b;
  408. {
  409.     while (*a == *b) {
  410.         if (*a == NULL)
  411.             return(0);
  412.         a++;
  413.         b++;
  414.     }
  415.     if (*a<*b)
  416.         return(-1);
  417.     else
  418.         return(1);
  419. }
  420. getsym(fp)
  421. FILE *fp;
  422.  
  423. {
  424. register int c;
  425.     if(fp==(EOF))
  426.         c=chtbl[tblp++];
  427.     else {
  428.         c=getchar(fp);
  429.         if(c == NEWLINE) {
  430.             ln++;
  431.             c=getsym(fp);
  432.         }
  433.     }
  434.     return(c);
  435. }
  436.  
  437. scanstr(fp)
  438. FILE *fp;
  439. {
  440. register int    c;
  441.     if((c=getsym(fp))==EOF)
  442.         return(EOF);
  443.         do {
  444.             while('"'!=c)
  445.             if((c=getsym(fp))==EOF)
  446.                 return(EOF);
  447.         if((c=getsym(fp))==EOF)
  448.             return(EOF);
  449.           } while(c=='"');
  450.     return c;
  451. }
  452.  
  453. scanquo(fp)
  454. FILE *fp;
  455. {
  456. register int    c;
  457.     if((c=getsym(fp))==EOF)
  458.         return(EOF);
  459.     for(;;) {
  460.         while((c!='\'')&&(c!='\\'))
  461.             if((c=getsym(fp))==EOF)
  462.                 return(EOF);
  463.         if(c=='\\') {
  464.             c=getsym(fp);
  465.             if(c==EOF)
  466.                 return(EOF);
  467.             else
  468.                 if(c=='\'')
  469.                 if((c==getsym(fp))==EOF)
  470.                     return(EOF);
  471.                 else
  472.                     break;
  473.         }
  474.         else
  475.             break;
  476.     }
  477.     /* c=='\'' */
  478.     if((c=getsym(fp))==EOF)
  479.         return(EOF);
  480.     return c;
  481. }
  482.  
  483. skipcom(fp)
  484. FILE *fp;
  485. {
  486. register int    c;
  487.     if((c=getsym(fp))==EOF)
  488.         return(EOF);
  489.         do {
  490.                 while(c!='*')
  491.             if((c=getsym(fp))==EOF)
  492.                 return(EOF);
  493.         if((c=getsym(fp))==EOF)
  494.             return(EOF);
  495.            } while(c!='/');
  496.     return(getsym(fp));
  497. }
  498.