home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / PAMAKE18.ZIP / IFPROC.C < prev    next >
C/C++ Source or Header  |  1989-09-30  |  4KB  |  149 lines

  1. /*************************************************************************
  2. |                                                                        |
  3. |   IFPROC.C                                                    31.08.89 |
  4. |   PAMAKE Utility:  process conditionals                                |
  5. |                                                                        |
  6. *************************************************************************/
  7.  
  8. #define MAXIF 10
  9.  
  10. #ifdef VMS
  11. #include stdio
  12. #include string
  13. #include stdlib
  14. #include ctype
  15. #include "h.h"
  16. #else
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include "h.h"
  22. #endif
  23.  
  24. int iftable[MAXIF+1] = {0};
  25. int iflevel = 0;
  26. extern char ifmessage;
  27.  
  28. #define NORMAL 0    /* No conditionals in force */
  29. #define OKCOND 1    /* Processing lines, waiting for else or endif */
  30. #define SKIP   2    /* Skipping lines, waiting for else or endif */
  31. #define HADELSE 4   /* Had else, must be waiting for endif */
  32.  
  33. /*****  check clean eof */
  34.  
  35. void
  36. ifeof()
  37. {                     
  38.     if (iflevel == 1) fatal("End of file - expected #endif");
  39.     if (iflevel > 1) fatal("End of file - expected %d #endif's",iflevel);
  40. }
  41.  
  42. /*****  check clean end of commands */
  43.  
  44. void
  45. ifeoc()
  46. {                     
  47.     if (iflevel == 1) fatal("End of command set - expected %%endif");
  48.     if (iflevel > 1) fatal("End of command set - expected %d %%endif's",iflevel);
  49. }
  50.  
  51. /*****  check each line, return true if to be ignored */
  52.  
  53. int
  54. ifproc(s,n)
  55. char *          s;
  56. int             n;
  57. {
  58.     register int            i;
  59.     register int            test;
  60.  
  61.     switch (n) 
  62.     {
  63.         case 1:                                 /* IF */
  64.         case 2:                                 /* IFN */
  65.             if (iflevel >= (MAXIF))
  66.                 error("%cif and %cifn statements nested too deeply",
  67.                     ifmessage,ifmessage);
  68.             s += 4;
  69.             while (pspace(*s)) s++;
  70.             i = istrue(s);
  71.             if (n == 2) i = !i;
  72.             iftable[++iflevel] = 2 - i;
  73.             return 1;
  74.         case 3:                                 /* ELSE */
  75.             i = iftable[iflevel];
  76.             if ((i == NORMAL) || (i & HADELSE))
  77.                 error("%celse without %cif",ifmessage,ifmessage);
  78.             if (i & OKCOND) i = SKIP | HADELSE;
  79.             else i = OKCOND | HADELSE;
  80.             iftable[iflevel] = i;
  81.             return 1;
  82.         case 4:
  83.             if (iftable[iflevel])
  84.             {
  85.                 iftable[iflevel] = NORMAL;
  86.                 iflevel--;
  87.                 return 1;
  88.             }
  89.             error("%cendif without %cif",ifmessage,ifmessage);
  90.         default:
  91.             test = 0;
  92.             for (i = 0; i <= iflevel; i++) test |= (iftable[i] & SKIP);
  93.             return (test);              /* true if skipping */
  94.     }
  95. }
  96.  
  97. /*****  check whether if statement is true */
  98.  
  99. int
  100. istrue(s)
  101. char *          s;
  102. {
  103.     char *                  p;              /*  General  */
  104.     char *                  q;              /*  General  */
  105.     struct macro *          mp;
  106.     int                     r;
  107.  
  108.     p = s;
  109.  
  110.     while (((q = strchr(p, '=')) != (char *)0) &&
  111.         (p != q) && (q[-1] == '\\'))        /*  Find value */
  112.     {
  113.         register char *         a;
  114.         a = q - 1;      /*  Del \ chr; move rest back  */
  115.         p = q;
  116.         while ((*a++ = *q++) != '\0')
  117.             ;
  118.     }
  119.     if (q != (char *)0)
  120.     {
  121.         register char *         a;
  122.  
  123.         *q++ = '\0';            /*  Separate name and val  */
  124.         while (pspace(*q))
  125.             q++;
  126.         if ((p = strrchr(q, '\n')) != (char *)0)
  127.             *p = '\0';
  128.         p = s;
  129.         if ((a = gettok(&p)) == (char *)0)
  130.             error("Bad conditional on %cif statement",ifmessage);
  131.         expand(q);
  132.         if ((mp = getmp(a)) != (struct macro *)0)
  133.         {
  134.             if (mp->m_sub) 
  135.             {
  136.                 r = strcmp(q,mp->m_sub);
  137.                 free(mp->m_sub);
  138.             }
  139.             else 
  140.                 r = strcmp(q,mp->m_val);
  141.         }
  142.         else
  143.             r = strcmp(q,"");
  144.         return (r == 0);
  145.     }
  146.     error("No conditional on %cif statement",ifmessage);
  147.     return 0;                   /* not reached */
  148. }
  149.