home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
495a.lha
/
Scan
/
console.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-04-06
|
9KB
|
338 lines
/* Console.c */
/* [Ed note: This is a somewhat chopped up and modified version of
console.c by Jim Cooper from AmigaLibDisk 69. Mostly just chopped out
some of the graphics routines and added a *VERY* basic capability of
determining what the current WB screen size and font are to use as
the default for the console. Special thanks to Jim for doing all the
dirty work here! -Dan
*/
#include <exec/types.h>
#include <exec/io.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <devices/conunit.h>
#include <libraries/diskfont.h>
/*#include <stdio.h>*/ /* for debug use */
#define EOF -1
#define GRAPHICS_REV 33
#define INTUITION_REV 33
#define INTUITION_OPEN 1
#define GRAPHICS_OPEN INTUITION_OPEN << 1
#define DISKFONT_OPEN GRAPHICS_OPEN << 1
#define WINDOW_OPEN DISKFONT_OPEN << 1
#define READPORT_OPEN WINDOW_OPEN << 1
#define CONREADER_OPEN READPORT_OPEN << 1
#define CONDEVICE_OPEN CONREADER_OPEN << 1
#define RP mywindow->RPort /* make life easy, since I use it a lot */
void myexit(), mysetup(), myprintf(), queueread();
int mygetc(), myscanf();
char *mygets();
short pagelen;
long GfxBase;
struct IntuitionBase *IntuitionBase;
ULONG DiskfontBase;
extern struct TextFont *OpenDiskFont();
struct TextFont *ConFont = NULL;
struct IOStdReq *CreateStdIO();
struct MsgPort *CreatePort();
static struct Screen *wbscreen();
struct Screen *OurS = NULL;
struct Window *OpenWindow();
struct Window *mywindow;
static struct IOStdReq *conreader, conrequest;
struct MsgPort *conreadport;
extern char WindowTitle[];
static char buff[513];
static char conchar;
static int myflags;
static struct NewWindow NewWindow = {
0,1,1,1,-1,-1, /* left, top, width, height, detail, block pen */
NULL,
ACTIVATE|WINDOWDRAG|SIMPLE_REFRESH,
NULL, NULL, (UBYTE *)WindowTitle,
NULL, NULL, 0, 0, 0, 0, /* Min & Max size don't matter with no size gadget */
WBENCHSCREEN };
/* initialize the window so we can read and write to it */
void mysetup()
{
IntuitionBase=(struct IntuitionBase *)
OpenLibrary("intuition.library",INTUITION_REV);
if (IntuitionBase==NULL)
myexit(1);
myflags |= INTUITION_OPEN;
GfxBase=OpenLibrary("graphics.library",GRAPHICS_REV);
if (GfxBase==NULL)
myexit(2);
myflags |= GRAPHICS_OPEN;
DiskfontBase = (ULONG) OpenLibrary("diskfont.library", 0L);
if (DiskfontBase == NULL)
myexit(9);
myflags |= DISKFONT_OPEN;
if ((OurS = wbscreen()) == NULL)
{
NewWindow.Width =640;
NewWindow.Height=199;
}
else
{
NewWindow.Width = OurS->Width;
NewWindow.Height= (OurS->Height)-1;
}
ConFont = OpenDiskFont(OurS->Font);
if ((mywindow = OpenWindow(&NewWindow)) == NULL)
myexit(3);
myflags |= WINDOW_OPEN;
pagelen=((mywindow->Height)/ConFont->tf_YSize)-2;
/* fprintf(stderr,"pagelen = %d, Window = %d, Font Height = %d\n",pagelen,
mywindow->Height,ConFont->tf_YSize);*/
/* create a port to receive messages about completed operations */
if ((conreadport = CreatePort("my.con.read", 0)) == NULL)
myexit(5);
myflags |= READPORT_OPEN;
/* construct a request for that port */
if ((conreader = CreateStdIO(conreadport)) == NULL)
myexit(6);
myflags |= CONREADER_OPEN;
conreader->io_Data = (APTR)mywindow;
conreader->io_Length = sizeof(*mywindow);
if (OpenDevice("console.device", 0, conreader, 0) != 0)
myexit(7);
myflags |= CONDEVICE_OPEN;
/* copy so we have a read request structure */
conrequest.io_Device = conreader->io_Device;
conrequest.io_Unit = conreader->io_Unit;
queueread();
}
static struct Screen
*wbscreen()
{
register struct Screen *s;
Forbid();
for (s = IntuitionBase->FirstScreen; s ; s = s->NextScreen)
if ((s->Flags & SCREENTYPE) == WBENCHSCREEN)
break;
Permit();
return (s);
}
void myputc(c) /* write a single character to our window */
char c;
{
if (mywindow == NULL) mysetup();
conrequest.io_Command = CMD_WRITE;
conrequest.io_Data = (APTR)&c;
conrequest.io_Length = 1;
DoIO(&conrequest);
}
void queueread()
{
conreader->io_Command = CMD_READ;
conreader->io_Data = (APTR)&conchar;
conreader->io_Length = 1;
SendIO(conreader);
}
int mygetc()
{
struct IntuiMessage *GetMsg();
char c = 0;
if (mywindow == NULL) mysetup();
/* if there hasn't been a response to the last read request, wait for it */
while((GetMsg(conreadport) == NULL)) WaitPort(conreadport);
/* get what the response character was */
c = conchar;
/* and queue up for another one */
queueread();
return((c == 0x1c) ? EOF : c);
}
char *mygets()
{
static char DELCHR[] = "\b \b"; /* String to DELete a CHaRacter */
register char inchar;
register int i,j;
register int count = 0;
register int ScrCount;
int LnCount, MaxLnCount = 0;
int InitCount = 0;
if (mywindow == NULL) mysetup();
myprintf("\x9b6n"); /* <CSI>6n - get current cursor pos */
while (mygetc() != ';'); /* returned as "<CSI>row;columnR" */
/* row & column returned as ASCII */
while ((inchar = mygetc()) != 'R')
InitCount = (InitCount * 10) + (inchar - '0');
LnCount = ScrCount = InitCount;
myprintf("\x9b q"); /* <CSI><space>q - window status request */
for (i=0;i<3;i++) /* returns window bounds in printable */
while (mygetc() != ';'); /* characters, format: */
/* <CSI>top line,first char,bottom line,last char<space>r */
while ((inchar = mygetc()) != ' ')
MaxLnCount = (MaxLnCount * 10) + (inchar - '0');
mygetc(); /* throw away the 'r' */
buff[0] = NULL;
do {
switch (inchar = mygetc()) {
case '\b' : /* Backspace handling */
if (count == 0)
break;
if (buff[--count] == '\t') {
i = buff[--count];
ScrCount -= i;
LnCount = ((LnCount - i) >= 0) ? (MaxLnCount - i) : (LnCount - i);
for(j=i;j>0;j--)
myprintf("%s",DELCHR);
} else {
ScrCount--;
LnCount = ((LnCount - 1) >= 0) ? MaxLnCount : 0;
myprintf("%s", DELCHR); /* Got to use myprintf to avoid newline */
}
break;
case '\t' : /* tab character */
if (count < 256) {
i = 9-(LnCount%8);
if ((LnCount + i) > MaxLnCount) {
i = MaxLnCount - LnCount + 1;
LnCount = 1;
} else LnCount += i;
ScrCount += i;
buff[count++] = i;
buff[count++] = '\t';
for(j=0;j<i;j++) myputc(' ');
}
break;
case '\r' :
buff[count] = '\r';
myputc('\n');
break;
case 0x18 : /* This is handling of ^X */
if (count == 0)
break;
myprintf("\x9b0 p"); /* turn cursor off */
for (; ScrCount > InitCount; ScrCount--)
myprintf("%s", DELCHR);
myprintf("\x9b p"); /* turn cursor on */
count = 0;
LnCount = InitCount;
buff[0] = NULL;
break;
default :
if ((count < 256) && (inchar > 31)) {
myputc(inchar);
ScrCount++;
LnCount = ((LnCount + 1) > MaxLnCount) ? 0 : (LnCount + 1);
buff[count++] = inchar;
}
}
} while (inchar != '\r');
buff[count] = '\0';
for(i=count-1;i>0;i--)
if(buff[i] == '\t') {
for(j=i-1;j<count;j++)
buff[j] = buff[j+1];
i--;
}
return(buff);
}
int myscanf(str,v1,v2,v3,v4,v5,v6,v7,v8,v9)
char *str;
long *v1,*v2,*v3,*v4,*v5,*v6,*v7,*v8,*v9;
{
return(sscanf(mygets(),str,v1,v2,v3,v4,v5,v6,v7,v8,v9));
}
void myputs(str) /* write a string to our window */
char *str;
{
if (mywindow == NULL) mysetup();
conrequest.io_Command = CMD_WRITE;
conrequest.io_Data = (APTR)str;
conrequest.io_Length = strlen(str);
DoIO(&conrequest);
}
/* write a formatted string to our window */
void myprintf(str,v1,v2,v3,v4,v5,v6,v7,v8,v9)
char *str;
long v1,v2,v3,v4,v5,v6,v7,v8,v9;
{
char buff[256];
if (mywindow == NULL) mysetup();
conrequest.io_Command = CMD_WRITE;
conrequest.io_Data = (APTR)buff;
conrequest.io_Length = sprintf(buff,str,v1,v2,v3,v4,v5,v6,v7,v8,v9);
DoIO(&conrequest);
}
/* close up everything and leave the program */
void myexit(code)
int code;
{
if (myflags && CONDEVICE_OPEN) CloseDevice(conreader);
if (myflags && CONREADER_OPEN) DeleteStdIO(conreader);
if (myflags && READPORT_OPEN) DeletePort(conreadport);
if (myflags && WINDOW_OPEN) CloseWindow(mywindow);
if (myflags && DISKFONT_OPEN) CloseLibrary(DiskfontBase);
if (myflags && GRAPHICS_OPEN) CloseLibrary(GfxBase);
if (myflags && INTUITION_OPEN) CloseLibrary(IntuitionBase);
_exit(code);
}