SOURCE\PC.C  ·  C  ·  7.7 KB  ·  1989-09-01  ·  from PersonalComputer_Sep-1989
/*******************************************************************************

                          PC MENUSYSTEM SOURCE CODE

                               By  Robin Nixon

     To be compiled with Turbo C but should work with most other compilers
                          with little or no change.

*******************************************************************************/

#include "stdio.h"
#include "stdlib.h"
#include "process.h"
#include "dos.h"
#include "dir.h"
#include "string.h"

unsigned char *pointer;
unsigned char *progptr;

unsigned int offset=0x8000;

int     pressed=0, line=0, mainptr=0, flen;

main()
{
    unsigned char buffer[0x1000];
    unsigned char progbuf[0x3000];
    FILE *fp;

    pointer=buffer;
    progptr=progbuf;

    chdir("\\");
    fp=fopen("\\main.src","rb");
    if (fp == 0)
    {
        cls();
        printf("\\MAIN.SRC not found\n");
        exit(0);
    }
    read(fileno(fp),progptr,flen=filelength(fileno(fp)));
    fclose(fp);

    if (ismono())
        offset=0;

    while (1)
        getparms();
}

getparms()
{
    char ch;
    unsigned char parm1[100],parm2[100],parm3[100],whole[100];
    int p1=0,p2=0,p3=0,j;

    *parm1=*parm2=*parm3=0;

    while(progptr[mainptr] == 13
        || progptr[mainptr] == 10
        || progptr[mainptr] == ' ')
        ++mainptr;

    ch=progptr[mainptr++];
    if (progptr[mainptr] != 13)
    {
        ++mainptr;

        j=0;
        do
        {
            whole[j]=progptr[mainptr+j++];
        } while (progptr[mainptr+j] != 13);
        whole[j]=0;

        while (progptr[mainptr] != 13 && progptr[mainptr] != ' ')
            parm1[p1++]=progptr[mainptr++];
        parm1[p1]=0;

        if (progptr[mainptr] != 13)
        {
            ++mainptr;

            while (progptr[mainptr] != 13 && progptr[mainptr] != ' ')
                parm2[p2++]=progptr[mainptr++];
            parm2[p2]=0;

            if (progptr[mainptr] != 13)
            {
                ++mainptr;

                while (progptr[mainptr] != 13 && progptr[mainptr] != ' ')
                    parm3[p3++]=progptr[mainptr++];
                parm3[p3]=0;

                if (progptr[mainptr] != 13)
                    mainptr=nextline(mainptr);
            }
        }
    }
    doit(ch,parm1,parm2,parm3,whole);
}

doit(letter,parm1,parm2,parm3,whole)
char letter, *parm1, *parm2, *parm3, *whole;
{
    unsigned char temp[100];

    switch(toupper(letter))
    {
        case 'B':   border (atoi(parm1)); break;
        case 'C':   cls(); break;
        case 'D':   display(parm1); break;
        case 'G':   dogoto(parm1); break;
        case 'I':   if (pressed == atoi(parm1))
                        dogoto(parm2);
                    break;
        case 'L':   locate(atoi(parm1),atoi(parm2)); break;
        case 'M':   mode(atoi(parm1)); break;
        case 'N':   chdir(parm1); break;
        case 'P':   newputs(whole); crlf(); break;
        case 'R':   if (spawnl(P_WAIT,parm1,parm1,parm2,parm3,NULL) == 0)
                        wait();
                    break;
        case 'S':   select(atoi(parm1)); break;
        case 'T':   type(parm1); break;
        case 'W':   wait(); break;
        case 'X':   cls(); exit(0);
        case 'Y':   system(whole);
                    break;
        case '.':
        case ';':   break;

        default :   cls();
                    printf("Unrecognised command %c",letter);
                    crlf();
                    exit(0);
    }
}

dogoto(string)
char *string;
{
    int j=0,k;

    while (j<flen)
    {
        while(progptr[j] != '.' && j<flen)
            ++j;
        if (progptr[j-1] == 10 || j == 0)
        {
            j+=2;


            if (strnicmp(progptr+j,string,strlen(string)) == 0)
            {
                mainptr=nextline(j);
                return;
            }
        }
        else
            ++j;
    }
    if (j >= flen)
    {
        cls();
        printf("Label not found\n");
        exit(0);
    }
}

nextline(j)
int j;
{
    while (progptr[j] != 13)
        ++j;
    if (progptr[j] == 10)
        ++j;
    return(j);

}


display(screen)
char *screen;
{
    int p,q;
    struct SREGS segregs;
    FILE *fp=fopen(screen,"rb");
    unsigned int buf=0;

    if (fp == 0)
    {
        cls();
        printf("Display file not found\n");
        exit(0);
    }
    read(fileno(fp),pointer,filelength(fileno(fp)));
    fclose(fp);

    locate(0,25);
    segread(&segregs);

    for (p=1 ; p<=25 ; ++p)
    {
        movedata(segregs.ds,FP_OFF(pointer+buf+(25-p)*160),0xb000,offset,p*160);
        for (q=0 ; q<2500 ; ++q)
            ;
    }
}


select(max) 
int max;
{
    int c=0;

    while (c == 0)
    {
        c=getch();
        if (c>48 && c<=48+max)
            c-=48;
        else if (c>58 && c<=58+max)
            c-=58;
        else
            c=0;
        if (c > 0 || max == 0)
        {
            pressed=c;
            return;
        }
    }
    pressed=c;
    return;
}


locate(x,y)
int x,y;
{
    union REGS inregs, outregs;

    inregs.h.ah=2;
    inregs.h.bh=0;
    inregs.h.dl=x;
    inregs.h.dh=y;

    int86(0x10,&inregs,&outregs);
}


ismono()
{
    union REGS inregs, outregs;

    inregs.h.ah=0x0f;

    int86(0x10,&inregs,&outregs);
    return(outregs.h.al == 7);
}


cls()
{
    union REGS inregs, outregs;

    inregs.h.ah=0x0f;
    int86(0x10,&inregs,&outregs);
    inregs.h.ah=0;
    inregs.h.al=outregs.h.al;

    int86(0x10,&inregs,&outregs);
}


wait()
{
    printf("Press a key to continue...");
    getch();
}


colputch(chr,att)
int chr,att;
{
    int x,y,p;

    getcur(&x,&y);
    p=x*2 + y*160;
    pokeb(0xb000,offset+p,chr);
    pokeb(0xb000,offset+p+1,att);
    if (x++ >= 80)
    {
        putch(13);
        putch(10);
    }
    else locate(x,y);
}


getcur(x,y)
int *x,*y;
{
    union REGS inregs, outregs;

    inregs.h.ah=3;
    inregs.h.bh=0;

    int86(0x10,&inregs,&outregs);

    *x=outregs.h.dl;
    *y=outregs.h.dh;
}


type(fname)
unsigned char *fname;
{
    FILE *fp;
    unsigned char c,t=0;

    cls();
    fp=fopen(fname,"rb");
    if (fp == NULL)
    {
        printf("File not found");
        exit(0);
    }
    while (!feof(fp))
    {
        c=fgetc(fp);
        switch(c)
        {
            case(12):
                      break;
            case(27):
                      fgetc(fp);
                      break;
            case(10): break;
            case(13): putch(10);
                      putch(13);
                      ++t;
                      break;
            default : colputch(c,14);
        }
        if (t == 24)
        {
            wait();
            cls();
            t=0;
        }
    }
    fclose(fp);
    locate(0,24);
    wait();
}


mode(n)
int n;
{
    union REGS inregs, outregs;

    inregs.h.ah=0;
    inregs.h.al=n;

    int86(0x10,&inregs,&outregs);

    if (ismono())
        offset=0;
    else
        offset=0x8000;
}


border(b)
int b;
{
    outport(0x3d9,b);
}


crlf()
{
    putch(13);
    putch(10);
}

newputs(string)
char *string;
{
    union REGS inregs, outregs;
    int j,x,y;

    for (j=0 ; j<strlen(string) ; ++j)
    {
        inregs.h.ah=10;
        inregs.h.al=string[j];
        inregs.h.bh=0;
        inregs.h.ch=0;
        inregs.h.cl=1;
        int86(0x10,&inregs,&outregs);
        getcur(&x,&y);
        if (++x >= 79)
        {
            crlf();
            getcur(&x,&y);
        }
        locate(x,y);
    }
}