home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / rh / rhcmds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-23  |  3.8 KB  |  109 lines

  1.  
  2. /* ----------------------------------------------------------------------
  3.  * FILE: rhcmds.c
  4.  * (c) 1989 Ken Stauffer
  5.  * This file contains the functions that do the evaluation of
  6.  * the stack program.
  7.  * These functions are simple, and behave like RPN operators, that is
  8.  * they use the last two values on that stack, apply an operator
  9.  * and push the result. Similarly for unary ops.
  10.  *
  11.  * ---------------------------------------------------------------------- */
  12.  
  13. #include "rh.h"
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16.  
  17. c_or(i)   long i;   { Stack[SP-2]=Stack[SP-2] || Stack[SP-1]; SP--; }
  18. c_and(i)  long i;   { Stack[SP-2]=Stack[SP-2] && Stack[SP-1]; SP--; }
  19. c_le(i)   long i;   { Stack[SP-2]=Stack[SP-2] <= Stack[SP-1]; SP--; }
  20. c_lt(i)   long i;   { Stack[SP-2]=Stack[SP-2] < Stack[SP-1]; SP--;  }
  21. c_ge(i)   long i;   { Stack[SP-2]=Stack[SP-2] >= Stack[SP-1]; SP--; }
  22. c_gt(i)   long i;   { Stack[SP-2]=Stack[SP-2] > Stack[SP-1]; SP--;  }
  23. c_ne(i)   long i;   { Stack[SP-2]=Stack[SP-2] != Stack[SP-1]; SP--; }
  24. c_eq(i)   long i;   { Stack[SP-2]=Stack[SP-2] == Stack[SP-1]; SP--; }
  25. c_bor(i)  long i;   { Stack[SP-2]=Stack[SP-2] | Stack[SP-1]; SP--;  }
  26. c_band(i) long i;   { Stack[SP-2]=Stack[SP-2] & Stack[SP-1]; SP--;  }
  27. c_bxor(i) long i;   { Stack[SP-2]=Stack[SP-2] ^ Stack[SP-1]; SP--;  }
  28. c_plus(i) long i;   { Stack[SP-2]=Stack[SP-2] + Stack[SP-1];SP--;   }
  29. c_mul(i)  long i;   { Stack[SP-2]=Stack[SP-2] * Stack[SP-1]; SP--;  }
  30. c_minus(i)long i;   { Stack[SP-2]=Stack[SP-2] - Stack[SP-1]; SP--;  }
  31. c_div(i)  long i;   { Stack[SP-2]=Stack[SP-2] / Stack[SP-1]; SP--;  }
  32. c_mod(i)  long i;   { Stack[SP-2]=Stack[SP-2] % Stack[SP-1]; SP--;  }
  33. c_lshift(i) long i; { Stack[SP-2]=Stack[SP-2] << Stack[SP-1]; SP--;  }
  34. c_rshift(i) long i; { Stack[SP-2]=Stack[SP-2] >> Stack[SP-1]; SP--;  }
  35.  
  36. /* unary instructions */
  37.  
  38. c_not(i)      long i; { Stack[SP-1]= ! Stack[SP-1]; }
  39. c_bnot(i)     long i; { Stack[SP-1]= ~ Stack[SP-1]; }
  40. c_uniminus(i) long i; { Stack[SP-1]= - Stack[SP-1]; }
  41.  
  42. /* trinary operator ?: */
  43.  
  44. c_qm(i)    long i; { PC = (Stack[SP-1]) ?  PC : i; SP--; }
  45. c_colon(i) long i; { PC = i; }
  46. c_nop(i)   long i; { }
  47.  
  48. /* operand functions */
  49.  
  50. c_number(i) long i; { Stack[SP++] = i;                }
  51. c_atime(i)  long i; { Stack[SP++] = globuf->st_atime; }
  52. c_ctime(i)  long i; { Stack[SP++] = globuf->st_ctime; }
  53. c_dev(i)    long i; { Stack[SP++] = globuf->st_dev;   }
  54. c_gid(i)    long i; { Stack[SP++] = globuf->st_gid;   }
  55. c_ino(i)    long i; { Stack[SP++] = globuf->st_ino;   }
  56. c_mode(i)   long i; { Stack[SP++] = globuf->st_mode;  }
  57. c_mtime(i)  long i; { Stack[SP++] = globuf->st_mtime; }
  58. c_nlink(i)  long i; { Stack[SP++] = globuf->st_nlink; }
  59. c_rdev(i)   long i; { Stack[SP++] = globuf->st_rdev;  }
  60. c_size(i)   long i; { Stack[SP++] = globuf->st_size;  }
  61. c_uid(i)    long i; { Stack[SP++] = globuf->st_uid;   }
  62.  
  63.  
  64. /* ----------------------------------------------------------------------
  65.  * star:
  66.  *    This implements the trivial regular expression stuff.
  67.  *    Since people may want to upgrade this, I will explain the
  68.  *    parameter. 'i' is an index into the array Startbuf[]. The
  69.  *    string contained there is the actual '\0' terminated
  70.  *    string that occured in the expression (eg "*.BAK" ), minus
  71.  *    the "'s.
  72.  *    The reasons for the simplistic regular expressions is
  73.  *    because it was easy, because lots of unix systems do
  74.  *    regexp() in lots of ways and this method is fairly fast.
  75.  *
  76.  */
  77.  
  78. c_star(i)
  79. long i;
  80. {
  81.  
  82.     register int ri,ii;
  83.     
  84.     if( Starbuf[i]=='*') {
  85.         ii=strlen(fname)-1;
  86.         ri=strlen(Starbuf+i)-1+i;
  87.         while( fname[ii]==Starbuf[ri] && ri>i ) {
  88.             ri--; ii--;
  89.         }
  90.         Stack[SP++] = (ri==i);
  91.     }
  92.     else {
  93.         int x=0;
  94.         ii=0;
  95.         while( fname[x] ) {
  96.             if(fname[x]=='/') ii=x;
  97.             x++;
  98.         }
  99.         ii++;
  100.         ri=i;
  101.         while( fname[ii]==Starbuf[ri] && Starbuf[ri]!='*' 
  102.             && fname[ii] && Starbuf[ri]) {
  103.             ri++; ii++;
  104.         }
  105.         Stack[SP++]=!(fname[ii]+Starbuf[ri]) || Starbuf[ri]=='*';
  106.     }
  107. }
  108.  
  109.