home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume11 / rpl / part02 / filecmd.c < prev    next >
C/C++ Source or Header  |  1990-03-10  |  3KB  |  171 lines

  1. /****************************************************************
  2.  
  3. Module:
  4.     FileCmd
  5.  
  6. Description:
  7.     Commands for file I/O
  8.  
  9.  
  10. Modification history:
  11.  
  12.     0.0    hjp    89-08-14
  13.  
  14.         initial version.
  15.  
  16.     0.1    hjp    89-08-15
  17.  
  18.         SAVE    debugged
  19.         LOAD    added
  20.         SYSTEM    added.
  21.  
  22.     0.2    hjp    89-11-23
  23.  
  24.         SAVE now _appends_ to file.
  25.  
  26.     0.3    hjp    89-12-02
  27.  
  28.         PRINT added.
  29.  
  30.     0.4    hjp    89-12-11
  31.  
  32.         minor bug fixing.
  33.  
  34. ****************************************************************/
  35.  
  36. #include <stddef.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39.  
  40. #include "rpl.h"
  41. #include "errors.h"
  42. #include "filecmd.h"
  43. #include "globvar.h"
  44. #include "intcmd.h"
  45. #include "stackcmd.h"
  46.  
  47. /*
  48.     SAVE: save object at level 2 to file at level 1
  49.  
  50.     1: obj    2: string    ->
  51. */
  52.  
  53. void c_save (void)
  54. {
  55.     listobj    * a, * b;
  56.     FILE    * fp;
  57.  
  58.     if ((b = stack) && (a = stack->next)) {
  59.  
  60.         if (b->obj->id != STRING) {
  61.  
  62.             error ("SAVE", ERR_WRTYPE, id2str (b->obj->id));
  63.  
  64.         } else if (fp = fopen (((stringobj *) b->obj)->val, "a")) {
  65.  
  66.             c_drop ();        /*    drop name    */
  67.  
  68.             iop = iobuffer;
  69.             printobj (a->obj);
  70.             fprintf (fp, "%s\n", iobuffer);
  71.             if (fclose (fp)) {
  72.                 error ("SAVE", ERR_DOS, strerror (errno));
  73.             }
  74.             c_drop ();    /*    drop saved object    */
  75.  
  76.         } else {
  77.             error ("SAVE", ERR_DOS, strerror (errno));
  78.         }
  79.     } else {
  80.  
  81.         error ("SAVE", ERR_2FEWARG, NULL);
  82.     }
  83. }
  84.  
  85.  
  86. /*
  87.     LOAD: load file into command line
  88.  
  89.     1: string    ->    ??
  90. */
  91.  
  92. void c_load (void)
  93. {
  94.     listobj    * b;
  95.     FILE    * fp;
  96.     int    rdcnt;
  97.  
  98.     if ((b = stack)) {
  99.  
  100.         if (b->obj->id != STRING) {
  101.  
  102.             error ("LOAD", ERR_WRTYPE, id2str (b->obj->id));
  103.  
  104.         } else if (fp = fopen (((stringobj *) b->obj)->val, "r")) {
  105.  
  106.             c_drop ();                        /*    drop name            */
  107.  
  108.             rdcnt = fread (cmdline, 1, PROGMAXSIZE - 1, fp);    /*    try to read max. program size    */
  109.             cmdline [rdcnt] = 0;                    /*    append EOS            */
  110.             rdptr = cmdline; empty = 0;                /*    simulate edit ()        */
  111.             if (fclose (fp)) {
  112.                 error ("LOAD", ERR_DOS, strerror (errno));
  113.             }
  114.  
  115.         } else {
  116.             error ("LOAD", ERR_DOS, strerror (errno));
  117.         }
  118.     } else {
  119.  
  120.         error ("LOAD", ERR_2FEWARG, NULL);
  121.     }
  122. }
  123.  
  124. void c_system (void)
  125. {
  126.     listobj    * b;
  127.  
  128.     if ((b = stack)) {
  129.  
  130.         if (b->obj->id != STRING) {
  131.  
  132.             error ("SYSTEM", ERR_WRTYPE, id2str (b->obj->id));
  133.  
  134.         } else if (system (((stringobj *) b->obj)->val)) {
  135.  
  136.             error ("SYSTEM", ERR_DOS, strerror (errno));
  137.  
  138.         }
  139.         c_drop ();
  140.  
  141.     } else {
  142.  
  143.         error ("SYSTEM", ERR_2FEWARG, NULL);
  144.     }
  145. }
  146.  
  147.  
  148. /*
  149.     PRINT: write object at level 1 to stdout
  150.  
  151.     1: obj    ->
  152. */
  153.  
  154. void c_print (void)
  155. {
  156.     listobj    * a;
  157.  
  158.     if (a = stack) {
  159.  
  160.         iop = iobuffer;
  161.         printobj (a->obj);
  162.         printf ("%s\n", iobuffer);
  163.         c_drop ();    /*    drop printed object    */
  164.  
  165.     } else {
  166.  
  167.         error ("PRINT", ERR_2FEWARG, NULL);
  168.     }
  169. }
  170.  
  171.