home *** CD-ROM | disk | FTP | other *** search
- /*
- * snapshot.c
- */
-
- #include <stdio.h>
- #include <ctype.h>
- #include "snapshot.h"
-
- static FILE *fp; /* Output stream. */
-
- /*
- * Make a snapshot of lines start through end of the screen into file.
- * Return 0 if ok, -1 if error.
- */
- snapshot(file, start, end)
- char *file; /* Output file. */
- int start,
- end; /* First, last lines to copy. */
- {
- char buf[300]; /* Contents of one line of the screen. */
-
- if (start < minline || end > maxline || start > end) {
- fprintf(stderr, "Illegal line range\n");
- return -1;
- }
- if (!(fp = fopen(file, "w"))) {
- perror(file);
- return -1;
- }
- if (handlesigs() == -1) {
- perror("signal");
- return -1;
- }
- if (echo_off() == -1) {
- perror("ioctl");
- return -1;
- }
- savepos();
- /*
- * If we've been interrupted, die gracefully. The original call to
- * setjmp() returns 0; int_handler() makes it return -1. setjmp is used
- * so this function can return -1 on ^C.
- */
- if (setjmp(sjbuf)) {
- (void) cleanup();
- return -1;
- }
- for (; start <= end; ++start) {
- transline(start);
- /* Get null-terminated string with newline stripped. */
- if (fflush(stdout) == EOF || gets(buf) == NULL) {
- /* Something weird happened. */
- fprintf(stderr, "Transmission error\n");
- (void) cleanup();
- return -1;
- }
- editable(buf);
- fputs(buf, fp);
- }
-
- return cleanup();
- }
-
- /*
- * Remove trailing spaces or any character in the last column.
- */
- editable(buf)
- char *buf;
- {
- register char *tmp;
-
- tmp = buf + strlen(buf) - 1;
- /* Make the line editable in case it's a full screen wide. */
- *tmp = '\n';
- /* Skip past trailing spaces. */
- while (tmp != buf && isspace(*tmp))
- --tmp;
- /*
- * If we got all the way to the start of the line, it's a blank line.
- * Otherwise, we're positioned at the last non-space char.
- */
- if (tmp != buf)
- ++tmp;
- tmp[0] = '\n';
- tmp[1] = '\0';
- }
-
- /*
- * Clean up the mess we've made. Return 0 if ok, -1 if error.
- */
- cleanup()
- {
- int status = 0;
-
- if (restoresigs() == -1) {
- perror("signal");
- status = -1;
- }
- if (fclose(fp) == EOF) {
- fprintf(stderr, "Error closing file\n");
- status = -1;
- }
- if (fflush(stdout) == EOF) {
- fprintf(stderr, "Error flushing output\n");
- status = -1;
- }
- if (echo_on() == -1) {
- perror("ioctl");
- status = -1;
- }
- restorepos();
- return status;
- }
-