home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 581b.lha / genmake_v1.03 / parse.c < prev    next >
C/C++ Source or Header  |  1991-11-10  |  5KB  |  272 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] = '\0';
  86.     buf[0] = '\0';
  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.     cpp_stack = (CPPN **)malloc(sizeof(CPPN *));
  120.     *cpp_stack = NULL;
  121.     symtab = (SYMENT **)malloc(sizeof(SYMENT *));
  122.     *symtab = NULL;
  123.     if ((fp = fopen(filename,"r")) == NULL) {
  124.         fprintf(stderr,"depends():  can't open file %s\n",filename);
  125.         return(0);
  126.     }
  127.     strcpy(name,"\"");
  128.     strcat(name,sname);
  129.     strcat(name,"\"");
  130.     buf[0] = '\0';
  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.                 return(0);
  136.             }
  137.             if (!strcmp("include",tok)) {
  138.                 if ((cp = gettok(fp,buf,tok,cp,cpp_stack,symtab)) < 0) {
  139.                     fclose(fp);
  140.                     return(0);
  141.                 }
  142.                 if (!strcmp(name,tok)) {
  143.                     fclose(fp);
  144.                     return(1);
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     fclose(fp);
  150.     return(0);
  151. }
  152.  
  153. gettok(fp,buf,curtok,cp,cpp_stack,symtab)
  154. FILE        *fp;
  155. char        *buf;
  156. char        *curtok;
  157. int        cp;
  158. CPPN        **cpp_stack;
  159. SYMENT        **symtab;
  160. {
  161.     int        quote = 0;
  162.     int        tokind = 0;
  163.     int        in_comment = 0;
  164.     char        quotechar;
  165.  
  166.     do {
  167. #ifdef DEBUG
  168.         printf("eating whitespace\n");
  169. #endif
  170.         while (is_white(buf[cp]))
  171.             ++cp;
  172. #ifdef DEBUG
  173.         printf("cp = %4d  buf[cp] = '%c'\n",cp,buf[cp]);
  174. #endif
  175.         if (buf[cp] == '\0' && fp != NULL) {
  176.             if (fgets(buf,BUFSIZE-1,fp) == NULL)
  177.                 return(-1);
  178.             if (!in_comment) {
  179.                 if (cpp_parse(buf,cpp_stack,symtab) < 0)
  180.                     return(-1);
  181.                 if (cpp_stack != NULL) {
  182.                     if (!cpp_useline(cpp_stack,symtab)) {
  183.                         buf[0] = ' ';
  184.                         buf[1] = '\0';
  185.                     }    
  186.                 }
  187.             }
  188.             cp = 0;
  189.         }
  190.         else if (buf[cp] == NULL) {
  191.             curtok[0] = '\0';
  192.             return(-1);
  193.         }    
  194.         if (!in_comment) {
  195.             if (buf[cp] == '/' && buf[cp+1] == '*') {
  196.                 in_comment = 1;
  197.                 cp += 2;
  198.             }
  199.         }
  200.         else {
  201.             if (buf[cp] == '*' && buf[cp+1] == '/') {
  202.                 in_comment = 0;
  203.                 cp += 2;
  204.             }
  205.             else 
  206.                 cp++;
  207.         }
  208. #ifdef DEBUG
  209.         printf("in_comment = %d\n",in_comment);
  210. #endif
  211.     } while (is_white(buf[cp]) || in_comment);
  212.     sp = cp;
  213.     if (is_quote(buf[cp])) {
  214.         quotechar = buf[cp++];
  215.         curtok[tokind++] = quotechar;
  216.         quote = 1;
  217.     }
  218.     if ((is_tok_sep(buf[cp]) || buf[cp] == '\0') && quote == 0) {
  219.         curtok[tokind++] = buf[cp++];
  220.         if (curtok[tokind-1] == '<' && (buf[cp] == '>' ||
  221.             buf[cp] == '=')) {
  222.             curtok[tokind++] = buf[cp++];
  223.         } else if (curtok[tokind-1] == '>' && buf[cp] == '=') {
  224.             curtok[tokind++] = buf[cp++];
  225.         }
  226.     }
  227.     else {
  228.         while ((!is_tok_sep(buf[cp]) && !is_white(buf[cp]) &&
  229.              buf[cp] != '\0' && quote == 0) ||
  230.             (buf[cp] != '\0' && quote == 1)) {
  231.             if (quote == 1 && buf[cp] == '\\')
  232.                 ++cp;
  233.             else if (quote == 1 && buf[cp] == quotechar)
  234.                 break;
  235.             curtok[tokind++] = buf[cp++];
  236.         }
  237.     }
  238.     if (quote == 1) {
  239.         curtok[tokind++] = buf[cp++];
  240.     }
  241.     curtok[tokind] = '\0';
  242.     return(cp);
  243. }
  244.  
  245. is_white(c)
  246. char        c;
  247. {
  248.     return((c == '\t' || c == '\n' || c == ' '));
  249. }
  250.  
  251. is_quote(c)
  252. char        c;
  253. {
  254.     return((c == '\'' || c == '\"'));
  255. }
  256.  
  257. is_tok_sep(c)
  258. char        c;
  259. {
  260.     return((c == ',' || c == '(' ||
  261.         c == '#' || c == '!' ||
  262.         c == '-' || c == '+' ||
  263.         c == '*' || c == '/' ||
  264.         c == '>' || c == '<' ||
  265.         c == '=' || c == ';' ||
  266.         c == ';' || c == ')' ||
  267.         c == '[' || c == ']' ||
  268.         c == ':' || c == '\\' ||
  269.         c == '[' || c == ']'));
  270. }
  271.  
  272.