home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / sb_prolog / a067s / sbpsrc / sim / builtin / c / saverest < prev    next >
Encoding:
Text File  |  1992-03-01  |  9.2 KB  |  343 lines

  1. /************************************************************************
  2. *                                                                       *
  3. * The SB-Prolog System                                                  *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987      *
  5. *                                                                       *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* saverest.c */
  25.   
  26. #if ! RISC_OS
  27. #include <sys/file.h>
  28. #endif
  29. #include "builtin.h"
  30.  
  31. b_SAVE()    /* reg1 points to PSC entry for constant giving file name */
  32. {
  33.    register LONG     op1;
  34.    register LONG_PTR top;
  35.    PSC_REC_PTR       psc_ptr;
  36.    CHAR              fname[256];
  37.  
  38.    op1 = reg[1];  DEREF(op1);
  39.    psc_ptr = GET_STR_PSC(op1);
  40.    namestring(psc_ptr, fname);
  41.    if (save(fname) < 0) {
  42.       printf("Cannot save to file '%s'\n",fname);
  43.       FAIL0;
  44.    }
  45. }
  46.  
  47.  
  48. save(filename)
  49. CHAR_PTR filename;
  50. {
  51.  
  52. #if RISC_OS
  53.   FILE *fd;
  54.   int i;
  55.   long space;
  56.  
  57. #define write( fd, b, n ) fwrite( b, sizeof(char), (size_t)n, fd )
  58.  
  59.    fd = fopen(filename, "wb");
  60.    if (fd == NULL)
  61.       return -1;
  62.    else {
  63.       i = 19;                  /* "magic no. for saved states */
  64.       write(fd, &i, 4);
  65.       write(fd, &maxpspace, 4);
  66.       write(fd, &maxmem, 4);
  67.       write(fd, &maxtrail, 4);
  68.       write(fd, &pspace, 4);
  69.       write(fd, &memory, 4);
  70.       write(fd, &tstack, 4);
  71.       write(fd, &curr_fence, 4);
  72.       write(fd, &breg, 4);
  73.       write(fd, &ereg, 4);
  74.       write(fd, &hreg, 4);
  75.       write(fd, &trreg, 4);
  76.       write(fd, &hbreg, 4);
  77.       write(fd, &cpreg, 4);
  78.       write(fd, &pcreg, 4);
  79.       write(fd, &nil_sym, 4);
  80.       write(fd, &list_str, 4);
  81.       write(fd, &list_psc, 4);
  82.       write(fd, &comma_psc, 4);
  83.       write(fd, &interrupt_psc, 4);
  84.       write(fd, &trap_vector[0], 4);
  85.       write(fd, &trap_vector[1], 4);
  86.  
  87.       for (i = 0; i < 10; i++)
  88.          write(fd, &flags[i], 4);
  89.  
  90.       space = (LONG)curr_fence - (LONG)pspace;
  91.       i = write(fd, pspace, space);
  92.       if (i < space) {
  93.          fclose(fd);
  94.          return -1;
  95.       }
  96.       space = (LONG)hreg - (LONG)heap_bottom;
  97.       i = write(fd, heap_bottom, space);
  98.       if (i < space) {
  99.          fclose(fd);
  100.          return -1;
  101.       }
  102.       space = (LONG)local_bottom - ((LONG)ereg - 1040);
  103.       i = write(fd, ((LONG)ereg-1040), space);
  104.       if (i < space) {
  105.          fclose(fd);
  106.          return -1;
  107.       }
  108.       space = (LONG)trail_bottom - (LONG)trreg;
  109.       i = write(fd, trreg, space);
  110.       if (i < space) {
  111.          fclose(fd);
  112.          return -1;
  113.       }
  114.       fclose(fd);
  115.       return 1;
  116.     }
  117. #else
  118.    int  fd, i;
  119.    LONG space;
  120.  
  121.    fd = open(filename, (O_WRONLY | O_CREAT), 0644);
  122.    if (fd < 0)
  123.       return fd;
  124.    else {
  125.       i = 19;                  /* "magic no. for saved states */
  126.       write(fd, &i, 4);
  127.       write(fd, &maxpspace, 4);
  128.       write(fd, &maxmem, 4);
  129.       write(fd, &maxtrail, 4);
  130.       write(fd, &pspace, 4);
  131.       write(fd, &memory, 4);
  132.       write(fd, &tstack, 4);
  133.       write(fd, &curr_fence, 4);
  134.       write(fd, &breg, 4);
  135.       write(fd, &ereg, 4);
  136.       write(fd, &hreg, 4);
  137.       write(fd, &trreg, 4);
  138.       write(fd, &hbreg, 4);
  139.       write(fd, &cpreg, 4);
  140.       write(fd, &pcreg, 4);
  141.       write(fd, &nil_sym, 4);
  142.       write(fd, &list_str, 4);
  143.       write(fd, &list_psc, 4);
  144.       write(fd, &comma_psc, 4);
  145.       write(fd, &interrupt_psc, 4);
  146.       write(fd, &trap_vector[0], 4);
  147.       write(fd, &trap_vector[1], 4);
  148.  
  149.       for (i = 0; i < 10; i++)
  150.          write(fd, &flags[i], 4);
  151.  
  152.       space = (LONG)curr_fence - (LONG)pspace;
  153.       i = write(fd, pspace, space);
  154.       if (i < space) {
  155.          close(fd);
  156.          return -1;
  157.       }
  158.       space = (LONG)hreg - (LONG)heap_bottom;
  159.       i = write(fd, heap_bottom, space);
  160.       if (i < space) {
  161.          close(fd);
  162.          return -1;
  163.       }
  164.       space = (LONG)local_bottom - ((LONG)ereg - 1040);
  165.       i = write(fd, ((LONG)ereg-1040), space);
  166.       if (i < space) {
  167.          close(fd);
  168.          return -1;
  169.       }
  170.       space = (LONG)trail_bottom - (LONG)trreg;
  171.       i = write(fd, trreg, space);
  172.       if (i < space) {
  173.          close(fd);
  174.          return -1;
  175.       }
  176.       close(fd);
  177.       return 1;
  178.     }  
  179. #endif
  180. }
  181.  
  182.  
  183. b_RESTORE()     /* reg1 points to PSC entry for constant giving file name */
  184. {
  185.     register LONG     op1;
  186.     register LONG_PTR top;
  187.     int               n;
  188.     PSC_REC_PTR       psc_ptr;
  189.     CHAR              fname[256];
  190.  
  191.     op1 = reg[1];  DEREF(op1);
  192.     psc_ptr = GET_STR_PSC(op1);
  193.     namestring(psc_ptr, fname);
  194.     n = restore(fname);
  195.     if (n == -1) {
  196.        char errormsg[128];
  197.        sprintf(errormsg, "Cannot restore file '%s'\n",fname);
  198.        quit(errormsg);
  199.     }
  200.     else if (n == -2)
  201.        quit(
  202.          "Saved state parameters do not match current values\n");
  203. }
  204.  
  205.  
  206. restore(fname)
  207. CHAR_PTR fname;
  208. {
  209. #if RISC_OS
  210.    FILE     *fd;
  211.    int       i;
  212.    LONG     space;
  213.    LONG_PTR npspace, nmemory, ntrail;
  214.  
  215. #define read(f,b,n) fread(b, sizeof(char), (size_t)n, f )
  216.  
  217.    fd = fopen(fname, "rb");
  218.    if (fd == NULL)
  219.       return -1;
  220.    else {
  221.       read(fd, &i, 4);
  222.       if (i != 19) {
  223.          printf("restore: file '%s' is not an SB-Prolog saved state\n",fname);
  224.          return -1;
  225.       }
  226.       read(fd, &maxpspace, 4);
  227.       read(fd, &maxmem, 4);
  228.       read(fd, &maxtrail, 4);
  229.       read(fd, &npspace, 4);
  230.       read(fd, &nmemory, 4);
  231.       read(fd, &ntrail, 4);
  232.       read(fd, &curr_fence, 4);
  233.       read(fd, &breg, 4);
  234.       read(fd, &ereg, 4);
  235.       read(fd, &hreg, 4);
  236.       read(fd, &trreg, 4);
  237.       read(fd, &hbreg, 4);
  238.       read(fd, &cpreg, 4);
  239.       read(fd, &pcreg, 4);
  240.       read(fd, &nil_sym, 4);
  241.       read(fd, &list_str, 4);
  242.       read(fd, &list_psc, 4);
  243.       read(fd, &comma_psc, 4);
  244.       read(fd, &interrupt_psc, 4);
  245.       read(fd, &trap_vector[0], 4);
  246.       read(fd, &trap_vector[1], 4);
  247.  
  248.       for (i = 0; i < 10; i++)
  249.          read(fd, &flags[i], 4);
  250.  
  251.       if (pspace != npspace)
  252.          return -2;
  253.       if (memory != nmemory)
  254.          return -2;
  255.       if (tstack != ntrail)
  256.          return -2;
  257.       space = (LONG)curr_fence - (LONG)pspace;
  258.       i = read(fd, pspace, space);
  259.       if (i != space)
  260.          return -1;
  261.       space = (LONG)hreg - (LONG)heap_bottom;
  262.       i = read(fd, heap_bottom, space);
  263.       if (i != space)
  264.          return -1;
  265.       space = (LONG)local_bottom - ((LONG)ereg-1040); /* AR has at most 256
  266.                                                          pvars, + misc info */
  267.       i = read(fd, ((LONG)ereg-1040), space);
  268.       if (i != space)
  269.          return -1;
  270.       space = (LONG)trail_bottom - (LONG)trreg;
  271.       i = read(fd, trreg, space);
  272.       if (i != space)
  273.          return -1;
  274.       return 1;
  275.     }
  276. #else
  277.  
  278.    int      fd, i;
  279.    LONG     space;
  280.    LONG_PTR npspace, nmemory, ntrail;
  281.  
  282.    fd = open(fname, O_RDONLY, 0644);
  283.    if (fd < 0)
  284.       return fd;
  285.    else {
  286.       read(fd, &i, 4);
  287.       if (i != 19) {
  288.          printf("restore: file '%s' is not an SB-Prolog saved state\n",fname);
  289.          return -1;
  290.       }
  291.       read(fd, &maxpspace, 4);
  292.       read(fd, &maxmem, 4);
  293.       read(fd, &maxtrail, 4);
  294.       read(fd, &npspace, 4);
  295.       read(fd, &nmemory, 4);
  296.       read(fd, &ntrail, 4);
  297.       read(fd, &curr_fence, 4);
  298.       read(fd, &breg, 4);
  299.       read(fd, &ereg, 4);
  300.       read(fd, &hreg, 4);
  301.       read(fd, &trreg, 4);
  302.       read(fd, &hbreg, 4);
  303.       read(fd, &cpreg, 4);
  304.       read(fd, &pcreg, 4);
  305.       read(fd, &nil_sym, 4);
  306.       read(fd, &list_str, 4);
  307.       read(fd, &list_psc, 4);
  308.       read(fd, &comma_psc, 4);
  309.       read(fd, &interrupt_psc, 4);
  310.       read(fd, &trap_vector[0], 4);
  311.       read(fd, &trap_vector[1], 4);
  312.  
  313.       for (i = 0; i < 10; i++)
  314.          read(fd, &flags[i], 4);
  315.  
  316.       if (pspace != npspace)
  317.          return -2;
  318.       if (memory != nmemory)
  319.          return -2;
  320.       if (tstack != ntrail)
  321.          return -2;
  322.       space = (LONG)curr_fence - (LONG)pspace;
  323.       i = read(fd, pspace, space);
  324.       if (i != space)
  325.          return -1;
  326.       space = (LONG)hreg - (LONG)heap_bottom;
  327.       i = read(fd, heap_bottom, space);
  328.       if (i != space)
  329.          return -1;
  330.       space = (LONG)local_bottom - ((LONG)ereg-1040); /* AR has at most 256
  331.                                                          pvars, + misc info */
  332.       i = read(fd, ((LONG)ereg-1040), space);
  333.       if (i != space)
  334.          return -1;
  335.       space = (LONG)trail_bottom - (LONG)trreg;
  336.       i = read(fd, trreg, space);
  337.       if (i != space)
  338.          return -1;
  339.       return 1;
  340.     }
  341. #endif
  342. }
  343.