home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
misc
/
volume32
/
shlm
/
part01
/
condump.c
next >
Wrap
C/C++ Source or Header
|
1992-09-20
|
8KB
|
304 lines
/*
condump / conrestore
*/
/* tabs = 4 */
/*------------------------------------------------------------------------
**
** includes
**
**-----------------------------------------------------------------------*/
#include "config.h"
#if defined(MAIN) || defined(USE_CONDUMP)
#include <fcntl.h>
#include <unistd.h>
#include <termio.h>
#include <stdio.h>
#include <string.h>
void condump(), conrestore();
int put_mark();
extern char *optarg;
extern int optind;
static int mem_fd = -1, put_mark_fd;
#ifdef MAIN
/*-----------------------------------------------------------------------
**
** main()
**
**----------------------------------------------------------------------*/
int main(argc, argv)
int argc;
char **argv;
{
return con_run(argc, argv);
}
#endif /* MAIN */
/*-----------------------------------------------------------------------
**
** con_run()
**
**----------------------------------------------------------------------*/
int con_run(argc, argv)
int argc;
char **argv;
{
int ch, iosize, cursor_fd;
char screen[SCREEN_SIZE + SCREEN_LINES + 2];
int verbose = 0,
newline = 0,
cursor = 0;
char *ttyname(),
*tty_ptr = NULL;
while ((ch = getopt(argc, argv, "cnv")) != -1)
switch (ch)
{
case 'c' : cursor = 1; break;
case 'n' : newline = 1; break;
case 'v' : verbose = 1; break;
case '?' :
halt("USAGE: %s [ -vnc ]\n-v verbose, -n newlines, -c cursor\n",
argv[0]);
}
if (optind < argc)
tty_ptr = argv[optind];
open_mem_fd();
iosize = SCREEN_LINES * SCREEN_COLS;
if (verbose) iosize *= SCREEN_CHAR_SPACING;
if (newline) iosize += SCREEN_LINES;
if (cursor) iosize += 2;
if (strcmp(argv[0], "conrestore") != 0 &&
strcmp(strrchr(argv[0],'/'), "/conrestore" ) != 0)
{
if (cursor)
{
if ( tty_ptr == NULL &&
(tty_ptr = ttyname(0)) == NULL)
halt("Cannot find tty name.\n");
if ( (cursor_fd=open(tty_ptr, O_WRONLY)) == -1)
halt("PERROR : Can not open console for writing.\n");
if ( (cursor_fd=open(tty_ptr, O_WRONLY)) == -1)
halt("PERROR : Can not open console for writing.\n");
}
condump(screen, cursor, newline, verbose, cursor_fd);
if (fwrite(screen, 1, iosize, stdout) != iosize)
halt("PERROR : Could not write required number of bytes.\n");
}
else
{
if (cursor)
{
if (tty_ptr == NULL)
cursor_fd = 1;
else if ( (cursor_fd=open(tty_ptr, O_WRONLY)) == -1)
halt("PERROR : Can not open console for writing.\n");
}
if (fread(screen, 1, iosize, stdin) != iosize)
halt("PERROR : Could not read required number of bytes.\n");
conrestore(screen, cursor, newline, verbose, cursor_fd);
}
return 0;
}
/*-----------------------------------------------------------------------
**
** condump()
**
**----------------------------------------------------------------------*/
void condump(screen, cursor, newline, verbose, cursor_fd)
char *screen;
int cursor, newline, verbose, cursor_fd;
{
static char *left_ptr = NULL;
char screen2[SCREEN_SIZE];
int i, errret;
if (lseek(mem_fd, SCREEN_START, SEEK_SET) == -1)
halt( "PERROR : Can not seek in memory device.\n");
if (read(mem_fd, screen2, SCREEN_SIZE) == -1)
halt("PERROR : Can not read from memory device.\n");
if (verbose && !newline)
{
memcpy(screen, screen2, SCREEN_SIZE);
screen += SCREEN_SIZE;
}
else
for (i=0 ; i < SCREEN_SIZE; i++)
{
if (newline &&
i % (SCREEN_COLS * SCREEN_CHAR_SPACING) == 0 &&
i != 0)
*(screen++) = '\n';
*(screen++) = screen2[i];
if (!verbose)
i += SCREEN_CHAR_SPACING - 1;
}
if (cursor)
{
if (lseek(mem_fd, SCREEN_START, SEEK_SET) == -1)
halt( "PERROR :Can not seek in memory device.\n");
put_mark_fd = cursor_fd;
put_mark(CURSOR_MARK);
if (read(mem_fd, screen2, SCREEN_SIZE) == -1)
halt("PERROR : Can not read from memory device.\n");
for (i=0; i < SCREEN_SIZE; i += SCREEN_CHAR_SPACING)
if (screen2[i] == CURSOR_MARK)
break;
if (i < SCREEN_SIZE)
{
*(screen++) = (i/SCREEN_CHAR_SPACING) / SCREEN_COLS;
*(screen++) = (i/SCREEN_CHAR_SPACING) % SCREEN_COLS;
#ifdef TERMINFO
if (left_ptr == NULL &&
(setupterm(CONSOLE_TERM_TYPE, 2, &errret) != 0 || /* 0==OK in curses.h */
(left_ptr = (char *)tigetstr("cub1")) == NULL ||
left_ptr == (char *) -1 ) )
#else
if (left_ptr == NULL &&
(tgetent(left_ptr, CONSOLE_TERM_TYPE) == -1 ||
(left_ptr = (char *)tgetstr("le", NULL)) == NULL) )
#endif
halt("Can not get termcap/info for left cursor movement\n");
tputs(left_ptr, 1, put_mark);
put_mark(screen2[i]);
tputs(left_ptr, 1, put_mark);
}
else
{
fprintf(stderr,"Can not get cursor position.\n");
sleep(2);
}
}
}
/*-----------------------------------------------------------------------
**
** conrestore()
**
**----------------------------------------------------------------------*/
void conrestore(screen, cursor, newline, verbose, cursor_fd)
char *screen;
int cursor, newline, verbose;
int cursor_fd;
{
int i, j, errret;
static char *curs_ptr = NULL,
*curs_ptr2 = NULL;
char *tparm();
char screen2[SCREEN_SIZE];
if (verbose && !newline)
{
memcpy(screen2, screen, SCREEN_SIZE);
screen += SCREEN_SIZE;
}
else
{
if (lseek(mem_fd, SCREEN_START, SEEK_SET) == -1)
halt( "PERROR : Can not seek in memory device.\n");
if (read(mem_fd, screen2, SCREEN_SIZE) == -1)
halt("PERROR : Can not read from memory device.\n");
for (i=0 ; i < SCREEN_SIZE; i++)
{
if (!newline ||
i % (SCREEN_COLS * SCREEN_CHAR_SPACING) != 0 ||
i == 0)
screen2[i] = *(screen++);
if (!verbose)
i += SCREEN_CHAR_SPACING - 1;
}
}
if (lseek(mem_fd, SCREEN_START, SEEK_SET) == -1)
halt( "PERROR :Can not seek in memory device.\n");
if (write(mem_fd, screen2, SCREEN_SIZE) == -1)
halt("PERROR : Can not write to memory device.\n");
if (cursor)
{
i = *(screen++);
j = *(screen++);
#ifdef TERMINFO
if (curs_ptr == NULL && /*0==OK in curses.h */
(setupterm(CONSOLE_TERM_TYPE, 2, &errret) != 0 ||
(curs_ptr = (char *)tigetstr("cup")) == NULL ||
curs_ptr == (char *) -1 ) )
halt("Can not get termcap/info for address cursor.\n");
curs_ptr2 = tparm(curs_ptr, i, j);
#else
if (curs_ptr == NULL &&
(tgetent(curs_ptr, CONSOLE_TERM_TYPE) == -1 ||
(curs_ptr = (char *)tgetstr("cm", NULL)) == NULL) )
halt("Can not get termcap/info for address cursor.\n");
curs_ptr2 = tgoto(curs_ptr, i, j);
#endif
put_mark_fd = cursor_fd;
tputs(curs_ptr2, 1, put_mark);
}
}
/*-----------------------------------------------------------------------
**
** open_mem_fd()
**
**----------------------------------------------------------------------*/
int open_mem_fd()
{
if (mem_fd == -1)
{
if ( access(CONSOLE_DEVICE, R_OK) == -1)
halt("PERROR : Can not read from console. Security check failed.\n");
if ( (mem_fd=open(MEMORY_DEVICE, O_RDWR)) == -1)
halt("PERROR : Can not open memory device.\n");
}
return mem_fd;
}
/*-----------------------------------------------------------------------
**
** close_mem_fd()
**
**----------------------------------------------------------------------*/
int close_mem_fd()
{
close(mem_fd);
return mem_fd;
}
/*-----------------------------------------------------------------------
**
** put_mark()
**
**----------------------------------------------------------------------*/
static int put_mark(ch)
char ch;
{
if (write(put_mark_fd, &ch, 1) == -1)
halt("PERROR : Can not write to terminal.\n");
return 0;
}
#endif /* MAIN || USE_CONDUMP */