home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd3.lzh / SBPROLOG2.2 / SIM / BUILTIN / saverest.c < prev    next >
Text File  |  1991-08-10  |  5KB  |  185 lines

  1. /************************************************************************
  2. *                                    *
  3. *    The SB-Prolog System                        *
  4. *    Copyright SUNY at Stony Brook, 1986                *
  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.  
  25. #ifndef OS9
  26. #include <stdio.h>
  27. #include <sys/file.h>
  28. #else
  29. #include <modes.h>
  30. #endif
  31. #include "../sim.h"
  32. #include "../aux.h"
  33.  
  34. b_SAVE()    /* R1 points to PSC entry for constant giving file name */
  35. {
  36.     register word op1;
  37.     register pw top;
  38.     struct psc_rec *pscptr;
  39.     char fname[256];
  40.  
  41.     op1 = gregc(1);
  42.     deref(op1);
  43.     pscptr = get_str_psc(op1);
  44.     namestring(pscptr, fname);
  45.     if (save(fname) < 0) {
  46.     printf("*** save: cannot write file ***\n");
  47.     Fail0;
  48.     }
  49. }
  50.  
  51. save(filename)
  52. char *filename;
  53. {
  54.     int fd, space, i;
  55.  
  56. #ifndef OS9
  57.     fd = open(filename, (O_WRONLY | O_CREAT), 0644);
  58. #else
  59.     fd = creat(filename, (S_IWRITE | S_IREAD));
  60. #endif
  61.     if (fd < 0) return fd;
  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++) write(fd, &flags[i], 4);
  88.  
  89.     space = (word)curr_fence - (word)pspace;
  90.     i = write(fd, pspace, space);
  91.     if (i < space) {close(fd);return -1;}
  92.     space = (word)hreg - (word)heap_bottom;
  93.     i = write(fd, heap_bottom, space);
  94.     if (i < space) {close(fd); return -1;}
  95.     space = (word)local_bottom - ((word)ereg-1040);
  96.     i = write(fd, ((word)ereg-1040), space);
  97.     if (i < space) {close(fd); return -1;}
  98.     space = (word)trail_bottom - (word)trreg;
  99.     i = write(fd, trreg, space);
  100.     if (i < space) {close(fd); return -1;}
  101.     close(fd);
  102.     return 1;
  103.     }
  104. }
  105.  
  106. b_RESTORE()     /* R1 points to PSC entry for constant giving file name */
  107. {
  108.     register word op1;
  109.     register pw top;
  110.     int n;
  111.     struct psc_rec *pscptr;
  112.     char fname[256];
  113.  
  114.     op1 = gregc(1); deref(op1);
  115.     pscptr = get_str_psc(op1);
  116.     namestring(pscptr, fname);
  117.     n = restore(fname);
  118.     if ( n == -1 ) 
  119.     quit("*** restore: cannot access file ***\n");
  120.     else if ( n == -2 )
  121.     quit("*** restore: saved state parameters do not match current values ***\n");
  122. }
  123.  
  124. restore(fname)
  125. char *fname;
  126. {
  127.     int fd, space, i;
  128.     word *npspace, *nmemory, *ntrail;
  129.  
  130. #ifndef OS9
  131.     fd = open(fname, O_RDONLY, 0644);
  132. #else
  133.     fd = open(fname, S_IREAD);
  134. #endif
  135.     if (fd < 0) return fd;
  136.     else {
  137.     read(fd, &i, 4);
  138.     if (i != 19) {
  139.         printf("*** restore: file not an SB-Prolog saved state! ***\n");
  140.         return -1;}
  141.     read(fd, &maxpspace, 4);
  142.     read(fd, &maxmem, 4);
  143.     read(fd, &maxtrail, 4);
  144.     read(fd, &npspace, 4);
  145.     read(fd, &nmemory, 4);
  146.     read(fd, &ntrail, 4);
  147.     read(fd, &curr_fence, 4);
  148.     read(fd, &breg, 4);
  149.     read(fd, &ereg, 4);
  150.     read(fd, &hreg, 4);
  151.     read(fd, &trreg, 4);
  152.     read(fd, &hbreg, 4);
  153.     read(fd, &cpreg, 4);
  154.     read(fd, &pcreg, 4);
  155.     read(fd, &nil_sym, 4);
  156.     read(fd, &list_str, 4);
  157.     read(fd, &list_psc, 4);
  158.     read(fd, &comma_psc, 4);
  159.     read(fd, &interrupt_psc, 4);
  160.     read(fd, &trap_vector[0], 4);
  161.     read(fd, &trap_vector[1], 4);
  162.  
  163.     for (i=0;i<10;i++) read(fd, &flags[i], 4);
  164.  
  165.     if (pspace != npspace) return -2;
  166.     if (memory != nmemory) return -2;
  167.     if (tstack != ntrail)  return -2;
  168.     space = (word)curr_fence - (word)pspace;
  169.     i = read(fd, pspace, space);
  170.     if (i != space) return -1;
  171.     space = (word)hreg - (word)heap_bottom;
  172.     i = read(fd, heap_bottom, space);
  173.     if (i != space) return -1;
  174.     space = (word)local_bottom - ((word)ereg-1040); /* AR has at most 256
  175.                                pvars, + misc info */
  176.     i = read(fd, ((word)ereg-1040), space);
  177.     if (i != space) return -1;
  178.     space = (word)trail_bottom - (word)trreg;
  179.     i = read(fd, trreg, space);
  180.     if (i != space) return -1;
  181.     return 1;
  182.     }
  183. }
  184.  
  185.