home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume11 / rpl / part02 / storecmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-10  |  3.1 KB  |  204 lines

  1. /****************************************************************
  2.  
  3. Module:
  4.     StoreCmd
  5.  
  6. Description:
  7.     Commands for manipulating variables
  8.  
  9.  
  10. Modification history:
  11.  
  12. 0.0    hjp    89-07-08
  13.  
  14.     initial version.
  15.  
  16. 0.1    hjp:    89-07-25
  17.  
  18.     comments added.
  19.  
  20. 0.2    hjp:    89-08-14
  21.  
  22.     PURGE and USER added.
  23.  
  24. 0.3    hjp:    89-11-08
  25.  
  26.     STO now globbers existing variables.
  27.  
  28. 0.4    hjp:    89-11-23
  29.  
  30.     PURGE now can delete files.
  31.     (This would belong into FileCmd, but I didn't want 2 purges.)
  32.  
  33. 0.5    hjp    90-03-03
  34.  
  35.     malloc replaced by mallocobj (at last!).
  36.  
  37. ****************************************************************/
  38.  
  39. #include <stddef.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42.  
  43. #include "rpl.h"
  44. #include "errors.h"
  45. #include "intcmd.h"
  46. #include "stackcmd.h"
  47. #include "storecmd.h"
  48. #include "globvar.h"
  49. #include "debug.h"
  50.  
  51. /*
  52.     STO: store object in variable
  53.  
  54.     2: obj    1: qname    ->
  55. */
  56.  
  57. void    c_sto    (void)
  58. {
  59.     listobj * a, * b;
  60.     varobj    * p;
  61.  
  62.     if ((b = stack) && (a = stack->next)) {
  63.  
  64.         if (b->obj->id != QNAME) {
  65.  
  66.             error ("STO", ERR_WRTYPE, NULL);
  67.  
  68.         } else if (p = findvar (((nameobj *) b->obj)->name)) {
  69.  
  70.             destroy (p->val, 1);    /*    destroy old contents of variable    */
  71.  
  72.             c_drop ();        /*    drop name    */
  73.  
  74.             p->val = a->obj;
  75.  
  76.             stack = a->next;    /*    drop stored object w/o destroing it !!    */
  77.             a->obj = NULL;
  78.             destroy (a, 0);
  79.  
  80.         } else if (p = mallocobj (VARIABLE)) {
  81.  
  82.             p->id = VARIABLE;
  83.             p->link = 1;
  84.             p->size = sizeof (varobj);
  85.             strcpy (p->name, ((nameobj *) b->obj)->name);
  86.  
  87.             c_drop ();        /*    drop name    */
  88.  
  89.             p->val = a->obj;
  90.  
  91.             stack = a->next;    /*    drop stored object w/o destroing it !!    */
  92.             a->obj = NULL;
  93.             destroy (a, 0);
  94.  
  95.             p->next = vars;     /*    hook it into variable list    */
  96.             vars = p;
  97.  
  98.         }
  99.     } else {
  100.  
  101.         error ("STO", ERR_2FEWARG, NULL);
  102.     }
  103. }
  104.  
  105. /*
  106.     RCL: recall variable
  107.     1: qname    ->    1: obj
  108. */
  109.  
  110. void    c_rcl (void)
  111. {
  112.     nameobj * a;
  113.     varobj    * p;
  114.  
  115.     if (! stack) {
  116.         error ("RCL", ERR_2FEWARG, NULL);
  117.         return;
  118.     }
  119.  
  120.     if ((a = stack->obj)->id != QNAME) {
  121.         error ("RCL", ERR_WRTYPE, NULL);
  122.         return;
  123.     }
  124.  
  125.     for (p = vars; p && strcmp (a->name, p->name); p = p->next);
  126.  
  127.     c_drop ();
  128.  
  129.     if (p) {
  130.         push (p->val);
  131.     } else {
  132.         error ("RCL", ERR_NXVAR, NULL);
  133.         return;
  134.     }
  135. }
  136.  
  137. /*
  138.     USER: show user variables
  139. */
  140.  
  141. void    c_user (void)
  142. {
  143.     varobj    * p;
  144.  
  145.     if (vars) {
  146.         for (p = vars; p; p = p->next) {
  147.             printf ("'%s'\n", p->name);
  148.         }
  149.     } else {
  150.         error ("USER", ERR_NOVAR, NULL);
  151.         return;
  152.     }
  153. }
  154.  
  155. /*
  156.     PURGE: purge user variable(s) or file.
  157.  
  158.     1:qname    ->
  159.     1:v    ->        (the variable with name v is purged)
  160.  
  161.     1:string    ->
  162.     1:s        ->    (the file with name s is unlinked)
  163. */
  164.  
  165. void    c_purge (void)
  166. {
  167.     nameobj * a;
  168.     varobj    * p, * pp;
  169.  
  170.     if (! stack) {
  171.         error ("PURGE", ERR_2FEWARG, NULL);
  172.         return;
  173.     }
  174.  
  175.     if ((a = stack->obj)->id == QNAME) {
  176.  
  177.         for (pp = NULL, p = vars;
  178.              p && strcmp (a->name, p->name);
  179.              pp = p, p = p->next);
  180.  
  181.         c_drop ();
  182.  
  183.         if (p) {
  184.             if (pp) {
  185.                 pp->next = p->next;
  186.             } else {
  187.                 vars = p->next;
  188.             }
  189.             destroy (p, 1);
  190.         } else {
  191.             error ("PURGE", ERR_NXVAR, NULL);
  192.             return;
  193.         }
  194.     } else if ((a = stack->obj)->id == STRING) {
  195.         if (unlink (((stringobj *) a)->val) == -1) {
  196.             error ("PURGE", ERR_DOS, strerror (errno));
  197.         }
  198.         c_drop ();
  199.     } else {
  200.         error ("PURGE", ERR_WRTYPE, NULL);
  201.         return;
  202.     }
  203. }
  204.