home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume2 / window / part3 / save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.3 KB  |  179 lines

  1. /*
  2.  *************
  3.  * DISTRIBUTION NOTICE  July 30 1985
  4.  * A Revised Edition of WM, by Matt Lennon and Tom Truscott,
  5.  *        Research Triangle Institute, (919) 541-7005.
  6.  * Based on the original by Robert Jacob (decvax!nrl-css!jacob),
  7.  *        Naval Research Laboratory, (202) 767-3365.
  8.  * No claims or warranties of any sort are made for this distribution.
  9.  * General permission is granted to copy, but not for profit,
  10.  * any of this distribution, provided that this notice
  11.  * is always included in the copies.
  12.  *************
  13.  */
  14. /*
  15.  * save.c  M. Lennon  2/07/85
  16.  * Save and restore windows between WM sessions.
  17.  */
  18.  
  19. #include "wm.h"
  20.  
  21.  
  22. /* Save windows from this session in HOME/.wmrc.
  23.  *
  24.  * Produces an ascii file containing info
  25.  * necessary for restoring windows in next WM session.
  26.  */
  27. Save(file)
  28.  
  29. char *file;    /* name of save file */
  30. {
  31.     register FILE *fp;            /* save file pointer */
  32.     register WINDOW *w;
  33.     register int i;
  34.  
  35.  
  36.     if (*file == '\0')
  37.     return(FALSE);
  38.     if ((fp=fopen(file,"w")) == NULL)
  39.     return(FALSE);
  40.  
  41.     fprintf(fp, "%s\n", mkprint(prefix));
  42.  
  43.     for (i=botw; i>=0; i=win[i].next)
  44.     {
  45.     w = win[i].wptr;
  46.     fprintf(fp, "%d %d %d %d %d\n",
  47.       i,
  48.       (win[i].flags&YFLEX)? 0: wlines(w),
  49.       (win[i].flags&XFLEX)? 0: wcols(w),
  50.       wbegy(w), wbegx(w));
  51.     }
  52.  
  53.     (void)fclose(fp);
  54.     return(TRUE);
  55. }
  56.  
  57. /*
  58.  * Restore windows from previous session
  59.  * as stored in ~/.wmrc.
  60.  * Returns number of windows restored (0 on error).
  61.  */
  62. Restore(file)
  63.  
  64. char *file;    /* name of restore file */
  65. {
  66.     register FILE *fp;            /* restore file pointer */
  67.     int w;                /* window index */
  68.     int lines, cols, begline, begcol;    /* window parameters */
  69.     int count;        /* number of windows restored */
  70.     int lshrink, cshrink; /* amount that windows must be shrunk to fit */
  71.     int rc;        /* temporary to hold return code from fscanf */
  72.     char ccbuf[10], tbuf[100];    /* temporary buffers */
  73.     register char *p;    /* temp pointer into tbuf */
  74.  
  75.  
  76.      /* Open save/restore file.
  77.       */
  78.     if (*file == '\0')
  79.     return(0);
  80.     if ((fp=fopen(file,"r")) == NULL) {
  81.     showmsg("\007Cannot read '%s'.", file);
  82.     sleep(2);
  83.     return(0);
  84.     }
  85.     
  86.      /* Read first line of ~/.wmrc to get the WM prefix character.
  87.       */
  88.     if (fscanf(fp,"%2s%1[\n]", ccbuf, tbuf) != 2) {
  89.     (void)fclose(fp);
  90.     showmsg("\007Bad .wmrc file '%s'.", file);
  91.     sleep(2);
  92.     return(0);
  93.     }
  94.     if (ccbuf[0]=='^' && ccbuf[1])
  95.     prefix = (ccbuf[1]=='?' ? '\177' : ccbuf[1]-0100);
  96.     else prefix = ccbuf[0];
  97.  
  98.     
  99.      /* Restore the windows.
  100.       * Stop trying if input error encountered.
  101.       */
  102.     count = 0;
  103.     for (;;)
  104.     {
  105.      /* Read window parameters.
  106.       * Check for end of file, format error,
  107.       * bad window name, and bad window dimensions.
  108.       */
  109.     rc = fscanf(fp,"%d%d%d%d%d%1[\n]",
  110.         &w,&lines,&cols,&begline,&begcol,tbuf);
  111.     if (rc == EOF)
  112.         break;
  113.     if (rc != 6 || lines < 0 || cols < 0 || begline < 0 || begcol < 0
  114.      || w < MINWINDOW || w >= MAXWINDOWS || (win[w].flags&INUSE)) {
  115.         showmsg("\007Bad window entry, file '%s'.", file);
  116.         sleep(2);
  117.         break;
  118.     }
  119.  
  120.     /*
  121.      * check for "flex" windows
  122.      */
  123.     if (lines == 0 && begline == 0) {
  124.         lines = LINES-1;
  125.         win[w].flags |= YFLEX;
  126.     }
  127.     if (cols == 0 && begcol == 0) {
  128.         cols = COLS;
  129.         win[w].flags |= XFLEX;
  130.     }
  131.  
  132.     /* Check for windows which are beyond this screen */
  133.     /* Bug: if .wmrc is updated these windows are omitted! */
  134.     if (begline >= LINES-1 || begcol >= COLS) {
  135.         showmsg("\007Window #%d is off this screen.", w);
  136.         sleep(2);
  137.         continue;
  138.     }
  139.  
  140.     /* Check for windows which must be shrunk to fit */
  141.     /* Bug(?): if .wmrc is updated the new sizes are saved */
  142.     lshrink = cshrink = 0;
  143.     if (begline+lines > LINES-1) {
  144.         lshrink = begline+lines-(LINES-1);
  145.         lines -= lshrink;
  146.     }
  147.     if (begcol+cols > COLS) {
  148.         cshrink = begcol+cols-COLS;
  149.         cols -= cshrink;
  150.     }
  151.     if (lshrink+cshrink) {
  152.         p = tbuf;
  153.         (void)sprintf(p, "\007Window #%d shrunk by", w); p += strlen(p);
  154.         if (lshrink) {
  155.         (void)sprintf(p, " %d line%s%s", lshrink, plural(lshrink),
  156.             cshrink? " and": "");
  157.         p += strlen(p);
  158.         }
  159.         if (cshrink) {
  160.         (void)sprintf(p, " %d column%s", cshrink, plural(cshrink));
  161.         p += strlen(p);
  162.         }
  163.         (void)sprintf(p, ".");
  164.         showmsg("%s", tbuf);
  165.         sleep(2);
  166.     }
  167.  
  168.      /* Construct new window.
  169.       */
  170.     if (NewWindow(w,lines,cols,begline,begcol))
  171.         continue;    /* cannot happen? */
  172.     WListAdd(w);
  173.     count++;
  174.     }
  175.  
  176.     (void)fclose(fp);
  177.     return(count);
  178. }
  179.