From Matthew Probert · 1996-04-04 22:57 · #75219 in R25C
TSR PROGRAMMING WITH C
Matthew Probert
Servile Software
Programs which remain running and resident in memory while other
programs are running are the most exciting line of programming for
many PC developers. This type of program is known as a "Terminate and
Stay Resident" or "TSR" program and they are very difficult to program
sucessfuly.
The difficulties in programming TSRs comes from the limitations of DOS
which is not a multi-tasking operating system, and does not react well
to re-enterant code. That is it's own functions (interrupts) calling
themselves.
In theory a TSR is quite simple. It is an ordinary program which
terminates not through the usual DOS terminate function, but through
the DOS "keep" function - interrupt 27h. This function reserves an
area of memory, used by the program so that no other programs will
overwrite it. This in itself is not a very difficult task, excepting
that the program needs to tell DOS how much memory to leave it!
The problems stem mainly from not being able to use DOS function calls
within the TSR program once it has "gone resident".
There are a few basic rules which help to clarify the problems
encountered in programming TSRs:
1. Avoid DOS function calls
2. Monitor the DOS busy flag, when this flag is nonzero, DOS is
executing an interrupt 21h function and MUST NOT be disturbed!
3. Monitor interrupt 28h. This reveals when DOS is busy waiting for
console input. At this time you can disturb DOS regardless of the
DOS busy flag setting.
4. Provide some way of checking whether the TSR is already loaded to
prevent multiple copies occuring in memory.
5. Remember that other TSR programs may be chained to interrupts, and
so you must chain any interrupt vectors that your program needs.
6. Your TSR program must use its own stack, and NOT that of the
running process.
7. TSR programs must be compiled in a small memory model with stack
checking turned off.
8. When control passes to your TSR program, it must tell DOS that the
active process has changed.
The following three source code modules describe a complete TSR
program. This is a useful pop-up address book database which can be
activated while any other program is running by pressing the key
combination 'Alt' and '.'. If the address book does not respond to the
key press, it is probably because DOS cannot be disturbed, and you
should try to pop-it-up again.
I accept no credit for the TSR system included in this source code. I
got it some seven-eight years ago from a BBS....!
/*
A practical TSR program (a pop-up address book database)
Compile in small memory model with stack checking OFF
*/
#include <dos.h>
#include <stdio.h>
#include <string.h>
#include <dir.h>
static union REGS rg;
/*
Size of the program to remain resident
experimentation is required to make this as small as possible
*/
unsigned sizeprogram = 28000/16;
/* Activate with Alt . */
unsigned scancode = 52; /* . */
unsigned keymask = 8; /* ALT */
char signature[]= "POPADDR";
char fpath[40];
/*
Function prototypes
*/
void curr_cursor(int *x, int *y);
int resident(char *, void interrupt(*)());
void resinit(void);
void terminate(void);
void restart(void);
void wait(void);
void resident_psp(void);
void exec(void);
/*
Entry point from DOS
*/
main(int argc, char *argv[])
{
void interrupt ifunc();
int ivec;
/*
For simplicity, assume the data file is in the root directory
of drive C:
*/
strcpy(fpath,"C:\\ADDRESS.DAT");
if ((ivec = resident(signature,ifunc)) != 0)
{
/* TSR is resident */
if (argc > 1)
{
rg.x.ax = 0;
if (strcmp(argv[1],"quit") == 0)
rg.x.ax = 1;
else if (strcmp(argv[1],"restart") == 0)
rg.x.ax = 2;
else if (strcmp(argv[1],"wait") == 0)
rg.x.ax = 3;
if (rg.x.ax)
{
int86(ivec,&rg,&rg);
return;
}
}
printf("\nPopup Address Book is already resident");
}
else
{
/* Initial load of TSR program */
printf("Popup Address Book Resident.\nPress Alt . To Activate....\n");
resinit();
}
}
void interrupt ifunc(bp,di,si,ds,es,dx,cx,bx,ax)
{
if(ax == 1)
terminate();
else if(ax == 2)
restart();
else if(ax == 3)
wait();
}
popup()
{
int x,y;
curr_cursor(&x,&y);
/* Call the TSR C program here */
exec();
cursor(x,y);
}
/*
Second source module
*/
#include <dos.h>
#include <stdio.h>
static union REGS rg;
static struct SREGS seg;
static unsigned mcbseg;
static unsigned dosseg;
static unsigned dosbusy;
static unsigned enddos;
char far *intdta;
static unsigned intsp;
static unsigned intss;
static char far *mydta;
static unsigned myss;
static unsigned stack;
static unsigned ctrl_break;
static unsigned mypsp;
static unsigned intpsp;
static unsigned pids[2];
static int pidctr = 0;
static int pp;
static void interrupt (*oldtimer)();
static void interrupt (*old28)();
static void interrupt (*oldkb)();
static void interrupt (*olddisk)();
static void interrupt (*oldcrit)();
void interrupt newtimer();
void interrupt new28();
void interrupt newkb();
void interrupt newdisk();
void interrupt newcrit();
extern unsigned sizeprogram;
extern unsigned scancode;
extern unsigned keymask;
static int resoff = 0;
static int running = 0;
static int popflg = 0;
static int diskflag = 0;
static int kbval;
static int cflag;
void dores(void);
void pidaddr(void);
void resinit()
{
segread(&seg);
myss = seg.ss;
rg.h.ah = 0x34;
intdos(&rg,&rg);
dosseg = _ES;
dosbusy = rg.x.bx;
mydta = getdta();
pidaddr();
oldtimer = getvect(0x1c);
old28 = getvect(0x28);
oldkb = getvect(9);
olddisk = getvect(0x13);
setvect(0x1c,newtimer);
setvect(9,newkb);
setvect(0x28,new28);
setvect(0x13,newdisk);
stack = (sizeprogram - (seg.ds - seg.cs)) * 16 - 300;
rg.x.ax = 0x3100;
rg.x.dx = sizeprogram;
intdos(&rg,&rg);
}
void interrupt newdisk(bp,di,si,ds,es,dx,cx,bx,ax,ip,cs,flgs)
{
diskflag++;
(*olddisk)();
ax = _AX;
newcrit();
flgs = cflag;
--diskflag;
}
void interrupt newcrit(bp,di,si,ds,es,dx,cx,bx,ax,ip,cs,flgs)
{
ax = 0;
cflag = flgs;
}
void interrupt newkb()
{
if (inportb(0x60) == scancode)
{
kbval = peekb(0,0x417);
if (!resoff && ((kbval & keymask) ^ keymask) == 0)
{
kbval = inportb(0x61);
outportb(0x61,kbval | 0x80);
outportb(0x61,kbval);
disable();
outportb(0x20,0x20);
enable();
if (!running)
popflg = 1;
return;
}
}
(*oldkb)();
}
void interrupt newtimer()
{
(*oldtimer)();
if (popflg && peekb(dosseg,dosbusy) == 0)
if(diskflag == 0)
{
outportb(0x20,0x20);
popflg = 0;
dores();
}
}
void interrupt new28()
{
(*old28)();
if (popflg && peekb(dosseg,dosbusy) != 0)
{
popflg = 0;
dores();
}
}
resident_psp()
{
intpsp = peek(dosseg,*pids);
for(pp = 0; pp < pidctr; pp++)
poke(dosseg,pids[pp],mypsp);
}
interrupted_psp()
{
for(pp = 0; pp < pidctr; pp++)
poke(dosseg,pids[pp],intpsp);
}
void dores()
{
running = 1;
disable();
intsp = _SP;
intss = _SS;
_SP = stack;
_SS = myss;
enable();
oldcrit = getvect(0x24);
setvect(0x24,newcrit);
rg.x.ax = 0x3300;
intdos(&rg,&rg);
ctrl_break = rg.h.dl;
rg.x.ax = 0x3301;
rg.h.dl = 0;
intdos(&rg,&rg);
intdta = getdta();
setdta(mydta);
resident_psp();
popup();
interrupted_psp();
setdta(intdta);
setvect(0x24,oldcrit);
rg.x.ax = 0x3301;
rg.h.dl = ctrl_break;
intdos(&rg,&rg);
disable();
_SP = intsp;
_SS = intss;
enable();
running = 0;
}
static int avec = 0;
unsigned resident(char *signature,void interrupt(*ifunc)())
{
char *sg;
unsigned df;
int vec;
segread(&seg);
df = seg.ds-seg.cs;
for(vec = 0x60; vec < 0x68; vec++)
{
if (getvect(vec) == NULL)
{
if (!avec)
avec = vec;
continue;
}
for(sg = signature; *sg; sg++)
if (*sg != peekb(peek(0,2+vec*4)+df,(unsigned)sg))
break;
if (!*sg)
return vec;
}
if (avec)
setvect(avec,ifunc);
return 0;
}
static void pidaddr()
{
unsigned adr = 0;
rg.h.ah = 0x51;
intdos(&rg,&rg);
mypsp = rg.x.bx;
rg.h.ah = 0x52;
intdos(&rg,&rg);
enddos = _ES;
enddos = peek(enddos,rg.x.bx-2);
while(pidctr < 2 && (unsigned)((dosseg<<4) + adr) < (enddos <<4))
{
if (peek(dosseg,adr) == mypsp)
{
rg.h.ah = 0x50;
rg.x.bx = mypsp + 1;
intdos(&rg,&rg);
if (peek(dosseg,adr) == mypsp + 1)
pids[pidctr++] = adr;
rg.h.ah = 0x50;
rg.x.bx = mypsp;
intdos(&rg,&rg);
}
adr++;
}
}
static resterm()
{
setvect(0x1c,oldtimer);
setvect(9,oldkb);
setvect(0x28,old28);
setvect(0x13,olddisk);
setvect(avec,(void interrupt (*)()) 0);
rg.h.ah = 0x52;
intdos(&rg,&rg);
mcbseg = _ES;
mcbseg = peek(mcbseg,rg.x.bx-2);
segread(&seg);
while(peekb(mcbseg,0) == 0x4d)
{
if(peek(mcbseg,1) == mypsp)
{
rg.h.ah = 0x49;
seg.es = mcbseg+1;
intdosx(&rg,&rg,&seg);
}
mcbseg += peek(mcbseg,3) + 1;
}
}
terminate()
{
if (getvect(0x13) == (void interrupt (*)()) newdisk)
if (getvect(9) == newkb)
if(getvect(0x28) == new28)
if(getvect(0x1c) == newtimer)
{
resterm();
return;
}
resoff = 1;
}
restart()
{
resoff = 0;
}
wait()
{
resoff = 1;
}
void cursor(int y, int x)
{
rg.x.ax = 0x0200;
rg.x.bx = 0;
rg.x.dx = ((y << 8) & 0xff00) + x;
int86(16,&rg,&rg);
}
void curr_cursor(int *y, int *x)
{
rg.x.ax = 0x0300;
rg.x.bx = 0;
int86(16,&rg,&rg);
*x = rg.h.dl;
*y = rg.h.dh;
}
/*
Third module, the simple pop-up address book
with mouse support
*/
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <dos.h>
#include <conio.h>
#include <graphics.h>
#include <bios.h>
/* left cannot be less than 3 */
#define left 4
/* Data structure for records */
typedef struct
{
char name[31];
char company[31];
char address[31];
char area[31];
char town[31];
char county[31];
char post[13];
char telephone[16];
char fax[16];
}
data;
extern char fpath[];
static char scr[4000];
static char sbuff[2000];
char stext[30];
data rec;
int handle;
int recsize;
union REGS inreg,outreg;
/*
Function prototypes
*/
void FATAL(char *);
void OPENDATA(void);
void CONTINUE(void);
void EXPORT_MULTI(void);
void GETDATA(int);
int GETOPT(void);
void DISPDATA(void);
void ADD_REC(void);
void PRINT_MULTI(void);
void SEARCH(void);
void MENU(void);
int GET_MOUSE(int *buttons)
{
inreg.x.ax = 0;
int86(0x33,&inreg,&outreg);
*buttons = outreg.x.bx;
return outreg.x.ax;
}
void MOUSE_CURSOR(int status)
{
/* Status = 0 cursor off */
/* 1 cursor on */
inreg.x.ax = 2 - status;
int86(0x33,&inreg,&outreg);
}
int MOUSE_LOCATION(int *x, int *y)
{
inreg.x.ax = 3;
int86(0x33,&inreg,&outreg);
*x = outreg.x.cx / 8;
*y = outreg.x.dx / 8;
return outreg.x.bx;
}
int GETOPT()
{
int result;
int x;
int y;
do
{
do
{
result = MOUSE_LOCATION(&x,&y);
if (result & 1)
{
if (x >= 52 && x <= 53 && y >= 7 && y <= 15)
return y - 7;
if (x >= 4 && x <= 40 && y >= 7 && y <= 14)
return y + 10;
if (x >= 4 && x <= 40 && y == 15)
return y + 10;
}
}
while(!bioskey(1));
result = bioskey(0);
x = result & 0xff;
if (x == 0)
{
result = result >> 8;
result -= 60;
}
}
while(result < 0 || result > 8);
return result;
}
void setvideo(unsigned char mode)
{
/* Sets the video display mode and clears the screen */
inreg.h.al = mode;
inreg.h.ah = 0x00;
int86(0x10, &inreg, &outreg);
}
int activepage(void)
{
/* Returns the currently selected video display page */
union REGS inreg,outreg;
inreg.h.ah = 0x0F;
int86(0x10, &inreg, &outreg);
return(outreg.h.bh);
}
void print(char *str)
{
/*
Prints characters only directly to the current display page
starting at the current cursor position. The cursor is not
advanced.
This function assumes a colour display card. For use with a
monochrome display card change 0xB800 to read 0xB000
*/
int page;
int offset;
unsigned row;
unsigned col;
char far *ptr;
page = activepage();
curr_cursor(&row,&col);
offset = page * 4000 + row * 160 + col * 2;
ptr = MK_FP(0xB800,offset);
while(*str)
{
*ptr++= *str++;
ptr++;
}
}
void TRUESHADE(int lef, int top, int right, int bottom)
{
int n;
/* True Shading of a screen block */
gettext(lef,top,right,bottom,sbuff);
for(n = 1; n < 2000; n+= 2)
sbuff[n] = 7;
puttext(lef,top,right,bottom,sbuff);
}
void DBOX(int l, int t, int r, int b)
{
/* Draws a double line box around the described area */
int n;
cursor(t,l);
print("+");
for(n = 1; n < r - l; n++)
{
cursor(t,l + n);
print("-");
}
cursor(t,r);
print("+");
for (n = t + 1; n < b; n++)
{
cursor(n,l);
print("|");
cursor(n,r);
print("|");
}
cursor(b,l);
print("+");
for(n = 1; n < r - l; n++)
{
cursor(b,l+n);
print("-");
}
cursor(b,r);
print("+");
}
int INPUT(char *text,unsigned length)
{
/* Receive a string from the operator */
unsigned key_pos;
int key;
unsigned start_row;
unsigned start_col;
unsigned end;
char temp[80];
char *p;
curr_cursor(&start_row,&start_col);
key_pos = 0;
end = strlen(text);
for(;;)
{
key = bioskey(0);
if ((key & 0xFF) == 0)
{
key = key >> 8;
if (key == 79)
{
while(key_pos < end)
key_pos++;
cursor(start_row,start_col + key_pos);
}
else
if (key == 71)
{
key_pos = 0;
cursor(start_row,start_col);
}
else
if ((key == 75) && (key_pos > 0))
{
key_pos--;
cursor(start_row,start_col + key_pos);
}
else
if ((key == 77) && (key_pos < end))
{
key_pos++;
cursor(start_row,start_col + key_pos);
}
else
if (key == 83)
{
p = text + key_pos;
while(*(p+1))
{
*p = *(p+1);
p++;
}
*p = 32;
if (end > 0)
end--;
cursor(start_row,start_col);
cprintf(text);
cprintf(" ");
if ((key_pos > 0) && (key_pos == end))
--- WILDMAIL!/WC v4.12
* Origin: Ooh! BBS, London [MULTILINES] 0181-395-3108 (2:254/233.0)