home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume4 / snapshot / snapshot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.4 KB  |  114 lines

  1. /*
  2.  * snapshot.c
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include "snapshot.h"
  8.  
  9. static FILE *fp;        /* Output stream. */
  10.  
  11. /*
  12.  * Make a snapshot of lines start through end of the screen into file. 
  13.  * Return 0 if ok, -1 if error.
  14.  */
  15. snapshot(file, start, end)
  16.     char   *file;        /* Output file. */
  17.     int     start,
  18.             end;        /* First, last lines to copy. */
  19. {
  20.     char    buf[300];        /* Contents of one line of the screen. */
  21.  
  22.     if (start < minline || end > maxline || start > end) {
  23.     fprintf(stderr, "Illegal line range\n");
  24.     return -1;
  25.     }
  26.     if (!(fp = fopen(file, "w"))) {
  27.     perror(file);
  28.     return -1;
  29.     }
  30.     if (handlesigs() == -1) {
  31.         perror("signal");
  32.     return -1;
  33.     }
  34.     if (echo_off() == -1) {
  35.     perror("ioctl");
  36.     return -1;
  37.     }
  38.     savepos();
  39.     /*
  40.      * If we've been interrupted, die gracefully.  The original call to
  41.      * setjmp() returns 0; int_handler() makes it return -1. setjmp is used
  42.      * so this function can return -1 on ^C. 
  43.      */
  44.     if (setjmp(sjbuf)) {
  45.     (void) cleanup();
  46.     return -1;
  47.     }
  48.     for (; start <= end; ++start) {
  49.     transline(start);
  50.     /* Get null-terminated string with newline stripped. */
  51.     if (fflush(stdout) == EOF || gets(buf) == NULL) {
  52.         /* Something weird happened. */
  53.         fprintf(stderr, "Transmission error\n");
  54.         (void) cleanup();
  55.         return -1;
  56.     }
  57.     editable(buf);
  58.     fputs(buf, fp);
  59.     }
  60.  
  61.     return cleanup();
  62. }
  63.  
  64. /*
  65.  * Remove trailing spaces or any character in the last column.
  66.  */
  67. editable(buf)
  68.     char   *buf;
  69. {
  70.     register char *tmp;
  71.  
  72.     tmp = buf + strlen(buf) - 1;
  73.     /* Make the line editable in case it's a full screen wide. */
  74.     *tmp = '\n';
  75.     /* Skip past trailing spaces. */
  76.     while (tmp != buf && isspace(*tmp))
  77.     --tmp;
  78.     /*
  79.      * If we got all the way to the start of the line, it's a blank line.
  80.      * Otherwise, we're positioned at the last non-space char. 
  81.      */
  82.     if (tmp != buf)
  83.     ++tmp;
  84.     tmp[0] = '\n';
  85.     tmp[1] = '\0';
  86. }
  87.  
  88. /*
  89.  * Clean up the mess we've made.  Return 0 if ok, -1 if error.
  90.  */
  91. cleanup()
  92. {
  93.     int     status = 0;
  94.  
  95.     if (restoresigs() == -1) {
  96.         perror("signal");
  97.     status = -1;
  98.     }
  99.     if (fclose(fp) == EOF) {
  100.     fprintf(stderr, "Error closing file\n");
  101.     status = -1;
  102.     }
  103.     if (fflush(stdout) == EOF) {
  104.     fprintf(stderr, "Error flushing output\n");
  105.     status = -1;
  106.     }
  107.     if (echo_on() == -1) {
  108.     perror("ioctl");
  109.     status = -1;
  110.     }
  111.     restorepos();
  112.     return status;
  113. }
  114.