 /* SAVESCRN.C */
 /* Store cursor location and screen into file */

    /* Coding for TC 2.0 */

 /*
    Coding by Albert Stein, Chemical Bank, 212-310-3927
              Software Ideology
              PO Box 305, Brooklyn, NY  11204

    Any comments are welcome.  Enjoy!
 */

#include <stdio.h>
#include <stdlib.h>
#include <mem.h>
#include <conio.h>
#include <dos.h>
#include <io.h>

#define READCUR     3
#define STATE       15

#define COLOR       1
#define MONO        0

#define COLOR_SEG   0xB800
#define MONO_SEG    0xB000

void store_data (unsigned char *filename);
void read_screentype (int *type);
void read_screensize (int *rows, int *cols,int type);
void read_cursor(int *row, int *col);
void syntax(void);

int type;
int row,col;
int rows,cols;

void main(int argc, char *argv[])
{
    if (argc == 1)
    {   syntax();
        exit (0);
    }
    read_screentype (&type);
    read_screensize (&rows,&cols,0);
    read_cursor (&row,&col);
    store_data (argv[1]);
    exit (0);
}

void store_data (unsigned char *filename)
{
    FILE *out;
    unsigned int seg,off;
    unsigned int line[266];         /* store up to 133 columns w/ attribs */
    unsigned char far *pline = (void far *)line;
    unsigned int linesize;
    unsigned int i;

    if ( (out=fopen (filename,"wb")) == NULL)
    {   printf ("Fatal error on opening '%s'\n",filename);
        exit (1);
    }
    fwrite (&row,2,1,out);
    fwrite (&col,2,1,out);
    seg = (type == COLOR) ? COLOR_SEG : MONO_SEG;
    linesize = cols * 2;
    for (i=off=0; i < rows; i++,off += linesize)
    {   movedata (seg,off,FP_SEG(pline),FP_OFF(pline),linesize);
        fwrite (line,linesize,1,out);
    }
    fclose (out);
}

void read_screensize (int *rows, int *cols,int type)
{
    union REGS regs;
    unsigned int value;
    unsigned int far *pvalue = &value;

    if (type == 1)
    {   regs.h.ah = 0x0F;
        int86(0x10,&regs,&regs);
        *cols = regs.h.ah;
        switch (regs.h.al)
        {   case 0x00:
            case 0x01:
            case 0x02:
            case 0x03:
            case 0x07:
                *rows = 25;
                break;
            default:
                printf ("Unexpected mode=0x%02x\n",regs.h.al);
                getch();
        }
    }
    else
    {   movedata (0x000,0x044A,FP_SEG(pvalue),FP_OFF(pvalue),2);
        *cols = *pvalue;
            /* this will return the total memory space in use */
        movedata (0x000,0x044C,FP_SEG(pvalue),FP_OFF(pvalue),2);
        *rows = *pvalue / (*cols * 2);
    }
}

void read_cursor(int *row, int *col)
/* read cursor position */
{
    union REGS regs;

    regs.h.ah = READCUR;
    regs.h.bh = 0x00;    /* page */
    int86(0x10,&regs,&regs);
    *row = regs.h.dh;
    *col = regs.h.dl;
}

void read_screentype (int *type)
{
    union REGS regs;

    regs.h.ah = 0x0F;
    int86(0x10,&regs,&regs);
    switch (regs.h.al)
    {   case 0x00:
        case 0x01:
        case 0x02:
        case 0x03:
            *type = COLOR;
            break;
        case 0x07:
            *type = MONO;
            break;
        default:
            printf ("Unexpected mode=0x%02x\n",regs.h.al);
            getch();
    }
}

void syntax()
{
    printf ("Syntax:   SAVESCRN filename\n"
            "Purpose:  Store cursor location and screen into a file\n"
            "          Will work with 40/43/50 line screens too!\n");
}
