Category : C Source Code
Archive   : ANSIINT.ZIP
Filename : ANSII.C
Output of file : ANSII.C contained in archive : ANSIINT.ZIP
#include
/* ---- miscellaneous defines ---- */
#define TRUE 1
#define FALSE 0
#define ftc(f) textattr((ctextattr() & 248) | f); /* change foreground color */
#define btc(b) textattr((ctextattr() & 143) | (b << 4)); /* background color */
#pragma inline
typedef unsigned short byte;
int ansion=TRUE;
/* ---- function prototypes ---- */
byte winds(char *s);
byte ctextattr(void);
void getcursortype(byte *top,byte *bottom);
void setcursortype(byte top,byte bottom);
void getxy(byte *x,byte *y);
void setxy(byte x,byte y);
void scroll(void);
void fastputc(char c);
void reverse(void);
void conceal(void);
int getnumber(char *s);
void ansimove(char c,char *s);
void ansito(char *s);
void ansixy(int save);
void ansicl(void);
void ansics(char *s);
void normvideo(void);
void ansico(char *s);
void bell(void);
void backspace(void);
void linefeed(void);
void carriagereturn(void);
void character(char c);
void ansioutput(char c);
/*--> returns one of the window dimensions */
byte winds(char *s)
{
struct text_info tinfo;
gettextinfo(&tinfo);
switch (s[0]) {
case 'x':
switch (s[1]) {
case '1':return(tinfo.winleft-1);
case '2':return(tinfo.winright-1);
}
break;
case 'y':
switch (s[1]) {
case '1':return(tinfo.wintop-1);
case '2':return(tinfo.winbottom-1);
}
break;
}
}
/*--> returns the current text attribute */
byte ctextattr(void)
{
struct text_info tinfo;
gettextinfo(&tinfo);
return(tinfo.attribute);
}
/*--> get cursor size */
void getcursortype(byte *top,byte *bottom)
{
asm mov ax,0x0300 /* Read Cursor Position */
asm xor bx,bx
asm int 0x10
*top=_CH;
*bottom=_CL;
}
/*--> set cursor size */
void setcursortype(byte top,byte bottom)
{
asm mov ax,0x0100 /* Set Cursor Size */
asm xor bx,bx
asm mov ch,top
asm mov cl,bottom
asm int 0x10
}
/*--> get cursor position */
void getxy(byte *x,byte *y)
{
asm mov ax,0x0300 /* Read Cursor Position */
asm xor bx,bx
asm int 0x10
*x=_DL;
*y=_DH;
}
/*--> set cursor position, with respect to current window size */
void setxy(byte x,byte y)
{
struct text_info tinfo;
int c,d;
gettextinfo(&tinfo);
c=((tinfo.wintop-1)*256)+(tinfo.winleft-1);
d=((tinfo.winbottom-1)*256)+(tinfo.winright-1);
asm mov bx,x
asm mov ax,y
asm mov cx,c
asm mov dx,d
asm mov bh,al
asm cmp bl,cl
asm jae C0
asm mov bl,cl
asm jmp short C1
C0:
asm cmp bl,dl
asm jbe C1
asm mov bl,dl
C1:
asm cmp bh,ch
asm jae C2
asm mov bh,ch
asm jmp short C3
C2:
asm cmp bh,dh
asm jbe C3
asm mov bh,dh
C3:
asm mov dx,bx
asm xor bx,bx
asm mov ax,0x0200 /* Set Cursor Position */
asm int 0x10
}
/*--> scroll up one line */
void scroll(void)
{
struct text_info tinfo;
int c,d;
gettextinfo(&tinfo);
c=((tinfo.wintop-1)*256)+(tinfo.winleft-1);
d=((tinfo.winbottom-1)*256)+(tinfo.winright-1);
asm mov ax,0x0601 /* Scroll Window Up */
asm mov bh,tinfo.attribute
asm mov cx,c
asm mov dx,d
asm int 0x10
}
/*--> direct single character output to color/mono screen */
void fastputc(char c)
{
byte attrib;
attrib=ctextattr();
asm mov ax,0x0040
asm mov es,ax
asm mov bx,word ptr[es:0x0050]
asm mov ax,word ptr[es:0x004a]
asm xor cx,cx
asm mov cl,bh
asm mul cx
asm xor bh,bh
asm add ax,bx
asm shl ax,1
asm mov di,ax
asm es:mov al,word ptr[es:0x00049]
asm cmp al,7
asm jne COLO
asm mov ax,0x0b000
asm jmp MONO
COLO:
asm mov ax,0x0b800
MONO:
asm mov es,ax
asm mov al,c
asm mov ah,attrib
asm stosw
}
/*--> reverse the current text attribute */
void reverse(void)
{
byte attrib;
attrib=ctextattr();
asm mov al,attrib
asm mov bl,al
asm and bl,7
asm mov cx,4
asm shl bl,cl
asm mov bh,al
asm mov cx,4
asm shr bh,cl
asm and bh,7
asm and al,136
asm or bl,bh
asm or al,bl
textattr(_AL);
}
/*--> conceal the current text attribute (hidden text) */
void conceal(void)
{
byte attrib;
attrib=ctextattr();
asm mov al,attrib
asm and al,112
asm mov bl,al
asm mov cx,4
asm shr bl,cl
asm or al,bl
textattr(_AL);
}
/*--> gets the first number from a pair of numbers, format "x;x;x;x" */
int getnumber(char *s)
{
char t[514];
int i,n;
if (strlen(s)==0)
return(0);
else {
n=0;
while (s[n]!=';')
if (s[n]!=NULL) n++; else { n=0; break; }
if ((n==0) && (s[n]!=';')) {
strcpy(t,s);
s[0]=NULL;
} else {
for (i=0; i
for (i=0; i<=strlen(s)-n; i++) s[i]=s[i+n+1];
}
return(atoi(t)); /* returns a 0 if no conversion possible (non-numeric) */
}
}
/*--> ANSI direction movement of cursor */
void ansimove(char c,char *s)
{
byte x,y,z;
getxy(&x,&y);
if ((z=getnumber(s))<1) z=1;
switch (c) {
case 'A':
y-=z;
case 'B':
if (c=='B') y+=z;
if (y
break;
case 'C':
x+=z;
case 'D':
if (c=='D') x-=z;
if (x
break;
default:
break;
}
setxy(x,y);
}
/*--> ANSI direct movement of cursor */
void ansito(char *s)
{
int x,y;
y=getnumber(&*s); if (y<1) y=1;
x=getnumber(&*s); if (x<1) x=1;
gotoxy(x,y);
}
/*--> save/restore the cursor position */
void ansixy(int save)
{
static int x=0;
static int y=0;
if (save) {
x=wherex();
y=wherey();
} else
if ((x>0) && (y>0)) gotoxy(x,y);
}
/*--> clear to the end of the line */
void ansicl(void)
{
clreol();
}
/*--> clear the screen */
void ansics(char *s)
{
if (getnumber(&*s)==2) clrscr();
}
/*--> change to NORMAL video (color 7) */
void normvideo(void)
{
textattr(7);
}
/*--> change to a new ANSI color */
void ansico(char *s)
{
int n;
if (s[0]==NULL) normvideo();
while (strlen(s)>0) {
if ((n=getnumber(&*s))<0) n=0;
/* : B : b b b : f f f f : */
switch (n) {
case 0:normvideo(); break; /* normal video */
case 1:textattr(ctextattr() | 8); break; /* turn on f1 */
case 2:textattr(ctextattr() & 247); break; /* knock off f1 */
case 5:textattr(ctextattr() | 128); break; /* turn on B */
case 6:textattr(ctextattr() | 128); break; /* turn on B */
case 7:reverse(); break;
case 8:conceal(); break;
case 30:ftc(BLACK); break;
case 31:ftc(RED); break;
case 32:ftc(GREEN); break;
case 33:ftc(BROWN); break;
case 34:ftc(BLUE); break;
case 35:ftc(MAGENTA); break;
case 36:ftc(CYAN); break;
case 37:ftc(LIGHTGRAY); break;
case 40:btc(BLACK); break;
case 41:btc(RED); break;
case 42:btc(GREEN); break;
case 43:btc(BROWN); break;
case 44:btc(BLUE); break;
case 45:btc(MAGENTA); break;
case 46:btc(CYAN); break;
case 47:btc(LIGHTGRAY); break;
default:break;
}
}
}
/*--> handler for ASCII 7 - bell */
void bell(void)
{
sound(440);
delay(100);
nosound();
}
/*--> handler for ASCII 8 - backspace */
void backspace(void)
{
ansimove('D',"");
}
/*--> handler for ASCII 10 - line feeds */
void linefeed(void)
{
byte x,y;
getxy(&x,&y);
if (y
/*--> handler for ASCII 13 - carriage returns */
void carriagereturn(void)
{
byte x,y,z;
getxy(&x,&y); z=winds("x1");
if (x>z) setxy(z,y);
}
/*--> handler for regular chrs (anything else, except ANSI chrs) */
void character(char c)
{
struct text_info tinfo;
byte x,y;
fastputc(c);
getxy(&x,&y);
gettextinfo(&tinfo);
if (++x==tinfo.winright) {
x=tinfo.winleft-1;
if (++y==tinfo.winbottom) { --y; scroll(); }
}
setxy(x,y);
}
/*--> MAIN handler for all screen output */
void ansioutput(char c)
{
static int isansi=FALSE;
static char ansis[514]="";
static char lastc=0;
if ((isansi) && (ansion)) {
isansi=FALSE; /* if chr is a case of below, the current ANSI code will
be finished after switch statement is executed */
switch (c) {
case 'H':
case 'F':ansito(ansis); break;
case 'A':
case 'B':
case 'C':
case 'D':ansimove(c,ansis); break;
case 's':ansixy(TRUE); break;
case 'u':ansixy(FALSE); break;
case 'J':ansics(ansis); break;
case 'K':ansicl(); break;
case 'm':ansico(ansis); break;
case '0': case '1':
case '2': case '3':
case '4': case '5':
case '6': case '7':
case '8': case '9':
case ';':
isansi=TRUE;
ansis[strlen(ansis)+1]=NULL;
ansis[strlen(ansis)]=c;
break;
default:
isansi=FALSE;
break;
}
} else
if (lastc==27)
if ((c=='[') && (ansion)) {
isansi=TRUE;
ansis[0]=NULL;
} else {
character(27);
character(c);
}
else
switch (c) {
case 7:bell(); break;
case 8:backspace(); break;
case 10:linefeed(); break;
case 13:carriagereturn(); break;
case 27:if (lastc==c) character(c); break;
default:character(c); break;
}
lastc=c;
}
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/