home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / dmail / cond.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-13  |  1.5 KB  |  108 lines

  1. /*
  2.  * COND.C
  3.  *
  4.  *  (C) Copyright 1985-1990 by Matthew Dillon,    All Rights Reserved.
  5.  *
  6.  * Conditional routines.
  7.  *
  8.  * if [!]variable
  9.  * else
  10.  * endif
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. #include "dmail.h"
  18.  
  19. #define MAXIF    16
  20.  
  21. static int Disable_if, Disable_case;
  22.  
  23. static int If_level;
  24. static char If_state[MAXIF];
  25.  
  26. Prototype int  do_if    (char *garbage, int com);
  27. Prototype int  do_else    (char *garbage, int com);
  28. Prototype int  do_endif (char *garbage, int com);
  29.  
  30. int
  31. do_if (char *garbage, int com)
  32. {
  33.     char
  34.         *str = av [1];
  35.     int
  36.         result = 0;
  37.  
  38.     if (ac != 2) {
  39.         puts ("if: bad args");
  40.         return -1;
  41.     }
  42.  
  43.     if (Disable_if) {
  44.         ++Disable_if;
  45.         return 1;
  46.     }
  47.  
  48.     if (If_level == MAXIF) {
  49.         puts ("Too many level's of IF's");
  50.         return -1;
  51.     }
  52.  
  53.     if (*str == '!') {
  54.         ++str;
  55.         result = 1;
  56.     }
  57.  
  58.     if (get_var (LEVEL_SET, str))
  59.         result = 1 - result;
  60.  
  61.     if (!result)
  62.         ++Disable_if;
  63.  
  64.     If_state [If_level++] = result;
  65.     XDisable = Disable_if + Disable_case;
  66.     return 1;
  67. }
  68.  
  69. int
  70. do_else (char *garbage, int com)
  71. {
  72.     if (Disable_if > 1)
  73.         return 1;
  74.  
  75.     if (If_level < 1) {
  76.         puts ("else without if");
  77.         return -1;
  78.     }
  79.  
  80.     Disable_if = !(If_state [If_level - 1] = 1 - If_state [If_level - 1]);
  81.     XDisable = Disable_if + Disable_case;
  82.     return 1;
  83. }
  84.  
  85. int
  86. do_endif (char *garbage, int com)
  87. {
  88.     if (Disable_if == 1) {
  89.         --If_level;
  90.         Disable_if = 0;
  91.     }
  92.     else
  93.         if (Disable_if > 1) {
  94.             --Disable_if;
  95.         }
  96.         else {
  97.             if (If_level == 0) {
  98.                 puts ("endif without if");
  99.                 return -1;
  100.             }
  101.             --If_level;
  102.             Disable_if = 0;
  103.         }
  104.  
  105.     XDisable = Disable_if + Disable_case;
  106.     return 1;
  107. }
  108.