home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume27 / genmake / part01 / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-17  |  5.3 KB  |  276 lines

  1. /* parse.c */
  2.  
  3. #include <stdio.h>
  4. #include "types.h"
  5.  
  6. int    sp;
  7.  
  8. parse_file(fp,fname,count,buf,curp,cpp_stack,symtab)
  9. FILE        *fp;
  10. char        *fname;
  11. int        *count;
  12. char        *buf;
  13. int        *curp;
  14. CPPN        **cpp_stack;
  15. SYMENT        **symtab;
  16. {
  17.     int    not_empty;
  18.     int    lb_count = *count;
  19.     char    tok[BUFSIZE];
  20.     int    cp = *curp;
  21.  
  22.     while ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) >= 0) {
  23.         if (tok[0] == '{') {
  24.             ++lb_count;
  25.         } else if (tok[0] == '}') {
  26.             --lb_count;
  27.         } else {
  28.             if (lb_count > 0)
  29.                 continue;
  30.             /* possible function definition, 1st ed K&R */
  31.             strcpy(fname,tok);
  32.             if ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) < 0)
  33.                 return(0);
  34.             if (tok[0] != '(') {
  35.                 cp = sp;
  36.                 continue;
  37.             }
  38.             not_empty = 0;
  39.             while ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) >= 0 && tok[0] != ')') {
  40.                 not_empty = 1;
  41.                 if ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) < 0)
  42.                     return(0);
  43.                 if (tok[0] == ')')
  44.                     break;
  45.             }
  46.             if (cp < 0)
  47.                 return(0);
  48.             if (!not_empty) {
  49.                 if ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) < 0)
  50.                     return(0);
  51.                 if (tok[0] != '{')
  52.                     continue;
  53.                 ++lb_count;
  54.             }
  55.             *curp = cp;
  56.             *count = lb_count;
  57.             return(1);
  58.         }
  59.     }
  60.     return(0);
  61. }
  62.  
  63.  
  64. depends(filename,funcname)
  65. char        *filename;
  66. char        *funcname;
  67. {
  68.     FILE        *fp;
  69.     char        tok[BUFSIZE];
  70.     char        buf[BUFSIZE];
  71.     char        cur_func[BUFSIZE];
  72.     int        cp = 0;
  73.     CPPN        **cpp_stack;
  74.     SYMENT        **symtab;
  75.  
  76.     cpp_stack = (CPPN **)malloc(sizeof(CPPN *));
  77.     *cpp_stack = NULL;
  78.     symtab = (SYMENT **)malloc(sizeof(SYMENT *));
  79.     *symtab = NULL;
  80.     if ((fp = fopen(filename,"r")) == NULL) {
  81.         fprintf(stderr,"depends():  can't open file %s\n",filename);
  82.         cpp_rel(cpp_stack,symtab);
  83.         return(0);
  84.     }
  85.     cur_func[BUFSIZE] = NULL;
  86.     buf[0] = NULL;
  87.     while ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) >= 0) {
  88.         if (!strcmp(tok,funcname)) {
  89.             if ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) < 0) {
  90.                 fclose(fp);
  91.                 cpp_rel(cpp_stack,symtab);
  92.                 return(0);
  93.             }
  94.             if (tok[0] == '(') {
  95.                 fclose(fp);
  96.                 cpp_rel(cpp_stack,symtab);
  97.                 return(1);
  98.             }
  99.             cp = sp;
  100.         }
  101.     }
  102.     fclose(fp);
  103.     cpp_rel(cpp_stack,symtab);
  104.     return(0);
  105. }
  106.  
  107. hdepends(filename,sname)
  108. char        *filename;
  109. char        *sname;
  110. {
  111.     FILE        *fp;
  112.     char        tok[BUFSIZE];
  113.     char        buf[BUFSIZE];
  114.     char        name[BUFSIZE];
  115.     int        cp = 0;
  116.     CPPN        **cpp_stack;
  117.     SYMENT        **symtab;
  118.  
  119.     if ((fp = fopen(filename,"r")) == NULL) {
  120.         fprintf(stderr,"depends():  can't open file %s\n",filename);
  121.         return(0);
  122.     }
  123.     cpp_stack = (CPPN **)malloc(sizeof(CPPN *));
  124.     *cpp_stack = NULL;
  125.     symtab = (SYMENT **)malloc(sizeof(SYMENT *));
  126.     *symtab = NULL;
  127.     strcpy(name,"\"");
  128.     strcat(name,sname);
  129.     strcat(name,"\"");
  130.     buf[0] = NULL;
  131.     while ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) >= 0) {
  132.         if (tok[0] == '#' && sp == 0) {
  133.             if ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) < 0) {
  134.                 fclose(fp);
  135.                 cpp_rel(cpp_stack,symtab);
  136.                 return(0);
  137.             }
  138.             if (!strcmp("include",tok)) {
  139.                 if ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) < 0) {
  140.                     fclose(fp);
  141.                     cpp_rel(cpp_stack,symtab);
  142.                     return(0);
  143.                 }
  144.                 if (!strcmp(name,tok)) {
  145.                     fclose(fp);
  146.                     cpp_rel(cpp_stack,symtab);
  147.                     return(1);
  148.                 }
  149.             }
  150.         }
  151.     }
  152.     fclose(fp);
  153.     cpp_rel(cpp_stack,symtab);
  154.     return(0);
  155. }
  156.  
  157. gettok(fp,buf,curtok,cp,cpp_stack,symtab)
  158. FILE        *fp;
  159. char        *buf;
  160. char        *curtok;
  161. int        cp;
  162. CPPN        **cpp_stack;
  163. SYMENT        **symtab;
  164. {
  165.     int        quote = 0;
  166.     int        tokind = 0;
  167.     int        in_comment = 0;
  168.     char        quotechar;
  169.  
  170.     do {
  171. #ifdef DEBUG
  172.         printf("eating whitespace\n");
  173. #endif
  174.         while (is_white(buf[cp]))
  175.             ++cp;
  176. #ifdef DEBUG
  177.         printf("cp = %4d  buf[cp] = '%c'\n",cp,buf[cp]);
  178. #endif
  179.         if (buf[cp] == NULL && fp != NULL) {
  180.             if (fgets(buf,BUFSIZE-1,fp) == NULL)
  181.                 return(-1);
  182.             if (!in_comment) {
  183.                 if (cpp_parse(buf,cpp_stack,symtab) < 0)
  184.                     return(-1);
  185.                 if (cpp_stack != NULL) {
  186.                     if (!cpp_useline(cpp_stack,symtab)) {
  187.                         buf[0] = ' ';
  188.                         buf[1] = NULL;
  189.                     }    
  190.                 }
  191.             }
  192.             cp = 0;
  193.         }
  194.         else if (buf[cp] == NULL) {
  195.             curtok[0] = NULL;
  196.             return(-1);
  197.         }    
  198.         if (!in_comment) {
  199.             if (buf[cp] == '/' && buf[cp+1] == '*') {
  200.                 in_comment = 1;
  201.                 cp += 2;
  202.             }
  203.         }
  204.         else {
  205.             if (buf[cp] == '*' && buf[cp+1] == '/') {
  206.                 in_comment = 0;
  207.                 cp += 2;
  208.             }
  209.             else 
  210.                 cp++;
  211.         }
  212. #ifdef DEBUG
  213.         printf("in_comment = %d\n",in_comment);
  214. #endif
  215.     } while (is_white(buf[cp]) || in_comment);
  216.     sp = cp;
  217.     if (is_quote(buf[cp])) {
  218.         quotechar = buf[cp++];
  219.         curtok[tokind++] = quotechar;
  220.         quote = 1;
  221.     }
  222.     if ((is_tok_sep(buf[cp]) || buf[cp] == NULL) && quote == 0) {
  223.         curtok[tokind++] = buf[cp++];
  224.         if (curtok[tokind-1] == '<' && (buf[cp] == '>' ||
  225.             buf[cp] == '=')) {
  226.             curtok[tokind++] = buf[cp++];
  227.         } else if (curtok[tokind-1] == '>' && buf[cp] == '=') {
  228.             curtok[tokind++] = buf[cp++];
  229.         }
  230.     }
  231.     else {
  232.         while ((!is_tok_sep(buf[cp]) && !is_white(buf[cp]) &&
  233.              buf[cp] != NULL && quote == 0) ||
  234.             (buf[cp] != NULL && quote == 1)) {
  235.             if (quote == 1 && buf[cp] == '\\')
  236.                 ++cp;
  237.             else if (quote == 1 && buf[cp] == quotechar)
  238.                 break;
  239.             curtok[tokind++] = buf[cp++];
  240.         }
  241.     }
  242.     if (quote == 1) {
  243.         curtok[tokind++] = buf[cp++];
  244.     }
  245.     curtok[tokind] = NULL;
  246.     return(cp);
  247. }
  248.  
  249. is_white(c)
  250. char        c;
  251. {
  252.     return((c == '\t' || c == '\n' || c == ' '));
  253. }
  254.  
  255. is_quote(c)
  256. char        c;
  257. {
  258.     return((c == '\'' || c == '\"'));
  259. }
  260.  
  261. is_tok_sep(c)
  262. char        c;
  263. {
  264.     return((c == ',' || c == '(' ||
  265.         c == '#' || c == '!' ||
  266.         c == '-' || c == '+' ||
  267.         c == '*' || c == '/' ||
  268.         c == '>' || c == '<' ||
  269.         c == '=' || c == ';' ||
  270.         c == ';' || c == ')' ||
  271.         c == '[' || c == ']' ||
  272.         c == ':' || c == '\\' ||
  273.         c == '[' || c == ']'));
  274. }
  275.  
  276.