home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Education Master 1994 (4th Edition)
/
EDUCATIONS_MASTER_4TH_EDITION.bin
/
files
/
progmisc
/
ovl312
/
ovl312.arc
/
SCULPT.C
< prev
next >
Wrap
Text File
|
1989-05-06
|
15KB
|
627 lines
/* SCULPT.C
programmed in the small memory model of Turbo C version 2.0
Create $$ODATA.OBJ file for use with OVL/PROVL system, version 3.12
and above.
*/
#include "stdio.h"
#include "dos.h"
#include "conio.h"
#include "ctype.h"
#include "mem.h"
#define PUBDEF 0x90
#define LNAMES 0x96
#define SEGDEF 0x98
#define MODEND 0x8a
#define THEADR 0x80
#define LEDATA 0xa0
#define BACKSPACE 8
#define RETURN 13
#define INSERT 210
#define DELETE 211
#define CURUP 200
#define CURL 203
#define CURR 205
#define CURDN 208
#define F9 195
#define F10 196
#define ESC 27
#define BLANKS(A) (&blanks[80-A])
FILE *fpbanner,*foutptr;
int input_len,xcur,ycur,textpos;
int funkey=0,btoggle=0; /* btoggle==0 for text, ==1 for file */
int upperflag=0;
int command_len,path_len,banner_len;
char chksum; /* checksum for object module records */
char textbuff[81];
char recbuff[1032]; /* buffer to hold bytes of record to be written */
char banbuff[4096]; /* buffer to hold characters from banner sign-on file */
char *banner_message[2]={
"Press <F9> to specify an ASCII file containing the sign-on banner.",
"Press <F9> to specify one line of text for the sign-on banner. "
};
char *banner_prompt[2]={
"Sign-on Banner Text: ",
"Sign-on Banner ASCII File Specification:"
};
char *blanks=" ";
char pathstring[82]={
"PATH="
};
char bannerstring[81]={
"Loading...."
};
char bannerfile[81]={
"SIGN-ON.TXT"
};
char commandstring[83]={
"/ZDOVERLAY.LOG"
};
void show_screen();
void put_up_text(unsigned char comm),insert_text(int x,int y),edit_text(unsigned char command);
void back_char(int x,int y),del_char (int x,int y),beep(),wipe();
void get_input(),save_info(),get_path(),get_banner(),get_command();
void write_modend(),write_lnames(),write_theadr(),write_segdef();
void write_pubdef(),write_ledata(),write_objmod();
void compute_chksum(char *chkbuff,unsigned int numchars);
int ginput(int x,int y,char *s,int length),inkey();
void main()
{
show_screen();
get_input();
if(funkey==ESC){ /* leave without saving */
clrscr();
exit(0);
}
else if(funkey==F10){
wipe(); /* remove prompts */
save_info(); /* save the information shown */
}
clrscr();
exit(0);
}
void show_screen()
{
clrscr();
gotoxy(1,2);
cprintf("Overlay File Path Environment String:\r\n");
textattr(0x70);
cprintf(BLANKS(80));
gotoxy(1,3);
cprintf("%s",pathstring);
textattr(7);
gotoxy(1,6);
cprintf("%s",banner_prompt[btoggle]);
textattr(0x70);
gotoxy(1,7);
cprintf(BLANKS(80));
gotoxy(1,7);
cprintf("%s",bannerstring);
textattr(7);
gotoxy(1,10);
cprintf("Command Line Switch(es):\r\n");
textattr(0x70);
cprintf(BLANKS(80));
gotoxy(1,11);
cprintf("%s",commandstring);
textattr(7);
gotoxy(1,23);
cprintf("Press <F10> to create Overlay Data Object File and exit.");
gotoxy(1,24);
cprintf("Press <ESC> to exit without creating Overlay Data Object File.");
}
void wipe() /* erase function key prompts */
{
gotoxy(1,21);
clreol();
gotoxy(1,23);
clreol();
gotoxy(1,24);
clreol();
}
void save_info()
{
int len;
char temp[82];
foutptr=fopen("$$ODATA.OBJ","wb"); /* open data file */
if(foutptr==NULL){ /* problem opening file */
clrscr();
gotoxy(1,2);
cprintf("*** Error opening file $$ODATA.OBJ");
gotoxy(1,3);
cprintf("*** $$ODATA.OBJ file NOT created.");
exit(1);
}
len=strlen(pathstring);
if(len && pathstring[len-1]!='='){ /* make sure path has '=' at end */
strcat(pathstring,"=");
}
strcat(commandstring,"\r"); /* append CR to end of commandstring */
len=strlen(commandstring);
strcpy(&temp[1],commandstring);
temp[0]=len; /* temp has command string with length prepended */
strcpy(commandstring,temp);
write_objmod();
fclose(foutptr); /* close data file */
}
void get_input()
{
static int pos=0;
while(1){
switch(pos){
case 0:
get_path();
break;
case 1:
get_banner();
break;
case 2:
get_command();
break;
default: /* hey, how'd you get here? */
pos=0; /* force valid position */
break;
}
if((funkey==F10 || funkey==RETURN) && btoggle && bannerfile[0]){ /* check that file exists */
if(!(access(bannerfile,0)==0)){ /* file does not exist */
beep();
gotoxy(1,8);
cprintf("File not found.");
continue;
}
}
gotoxy(1,8);
cprintf(BLANKS(16));
if(funkey==ESC || funkey==F10){
break;
}
else if (funkey==RETURN || funkey==CURDN){
pos=(pos<2?pos+1:0);
}
else if(funkey==CURUP){
pos=(pos>0?pos-1:2);
}
}
}
void get_path()
{
gotoxy(1,21);
cprintf(BLANKS(80));
textattr(0x70);
while(1){
upperflag=1; /* force caps */
funkey=ginput(1,3,pathstring,80);
upperflag=0;
if(funkey!=F9)
break;
else
beep();
}
textattr(7);
}
void get_banner()
{
gotoxy(1,21);
cprintf("%s",banner_message[btoggle]);
textattr(0x70);
while(1){
if(!btoggle){
textattr(7);
gotoxy(1,8); /* blank file not found message field */
cprintf(BLANKS(16));
textattr(0x70);
funkey=ginput(1,7,bannerstring,80);
}
else{
funkey=ginput(1,7,bannerfile,80);
}
if(funkey!=F9){
break;
}
else{ /* toggle message */
btoggle^=1;
textattr(7);
gotoxy(1,6);
cprintf("%s",banner_prompt[btoggle]);
gotoxy(1,21);
cprintf("%s",banner_message[btoggle]);
textattr(0x70);
}
}
textattr(7);
}
void get_command()
{
gotoxy(1,21);
cprintf(BLANKS(80));
textattr(0x70);
while(1){
funkey=ginput(1,11,commandstring,80);
if(funkey!=F9)
break;
else
beep();
}
textattr(7);
}
/* generic input -- pass it x,y location, string to print in s, and length.
returns new string in s */
int ginput(int x,int y,char *s,int length)
{
unsigned char command;
int i;
input_len=length;
xcur=x;
ycur=y;
textpos=0;
for (i=0;i<input_len;i++)
textbuff[i]=' ';
i=0;
while(s[i]){
textbuff[i]=s[i];
i++;
}
textbuff[input_len]=0;
gotoxy(x,y);
cprintf("%s",textbuff);
i=strlen(s);
if(i==input_len)
i--;
textpos=i;
xcur=x+i;
gotoxy(x+i,y);
command=0;
while (1){
command=inkey ();
if (command==RETURN || command==F9 || command==F10 || command==ESC || command==CURUP || command==CURDN)
break;
else if (command==CURL || command==CURR)
edit_text (command);
else if (command==DELETE)
del_char (x,y);
else if (command==INSERT)
insert_text (x,y);
else if (command==BACKSPACE)
back_char(x,y);
else if(command<' ' || command>'~'){
beep();
continue;
}
else{
put_up_text (command);
}
gotoxy(xcur,ycur);
}
i=strlen(textbuff);
while(i>=0 && (textbuff[i]<=' ' || textbuff[i]>'~'))
textbuff[i--]=0; /* strip trailing whitespace */
strcpy(s,textbuff);
return((int)(command));
}
void put_up_text(unsigned char comm) /* print a character, used by input routines */
{
gotoxy(xcur,ycur);
cprintf("%c",comm);
textbuff[textpos]=comm;
if(textpos<input_len-1){
textpos++;
xcur++;
}
}
void insert_text(int x,int y) /* insert a character, used by input routines */
{
char temp[82];
if (textbuff[input_len-1] == ' '){
strcpy(temp,textbuff);
strcpy(&temp[textpos+1],&textbuff[textpos]);
temp[input_len]=0;
temp[textpos]=' ';
gotoxy(x,y);
cprintf("%s",temp);
strcpy(textbuff,temp);
}
else
beep();
}
void edit_text(unsigned char command) /* move cursor left or right, used by input routines */
{
if (command==CURR && textpos<input_len-1){
textpos++;
xcur++;
}
else if (command==CURL && textpos>0){
textpos--;
xcur--;
}
else if (textpos==0 || textpos==input_len-1)
beep();
}
void back_char(int x,int y) /* backspace a character, used by input routines */
{
if(textpos==0)
beep();
else if(textpos==input_len-1 && textbuff[textpos]!=' ')
textbuff[textpos]=' ';
else{
textpos--;
textbuff[textpos]=' ';
xcur--;
}
gotoxy(x,y);
cprintf("%s",textbuff);
}
void del_char(int x,int y) /* delete a character, used by input routines */
{
strcpy(&textbuff[textpos],&textbuff[textpos+1]);
textbuff[input_len-1]=' ';
textbuff[input_len]=0;
gotoxy(x,y);
cprintf("%s",textbuff);
}
void beep()
{
sound(4000);
delay(200);
nosound();
}
int inkey()
{
int c;
c=getch();
if(upperflag){
c=toupper(c);
}
if(!c){
c=getch();
c+=128; /* set high bit to indicate special key */
}
return(c);
}
void write_objmod()
{
command_len=strlen(commandstring);
path_len=strlen(pathstring);
if(!btoggle){ /* pull sign-on banner from entered text */
banner_len=strlen(bannerstring);
strcpy(banbuff,bannerstring); /* copy string into banner buffer */
}
else{ /* pull sign-on banner from file */
if(bannerfile[0]){
fpbanner=fopen(bannerfile,"rb");
banner_len=fread(banbuff,1,4096,fpbanner);
fclose(fpbanner);
}
else{ /* no file name given */
banner_len=0;
}
}
write_theadr();
write_lnames();
write_segdef();
write_pubdef();
write_ledata();
write_modend();
}
void write_theadr()
{
recbuff[0]=THEADR; /* record type */
recbuff[1]=10; /* two byte record length (remaining bytes including checksum byte) */
recbuff[2]=0;
recbuff[3]=8; /* T-module name field */
recbuff[4]='$';
recbuff[5]='$';
recbuff[6]='M';
recbuff[7]='D';
recbuff[8]='.';
recbuff[9]='A';
recbuff[10]='S';
recbuff[11]='M';
compute_chksum(recbuff,12);
recbuff[12]=chksum;
fwrite((void *)recbuff,13,1,foutptr);
}
void write_lnames()
{
recbuff[0]=LNAMES; /* record type */
recbuff[1]=17; /* two byte record length */
recbuff[2]=0;
recbuff[3]=0; /* name list */
recbuff[4]=9; /* name list */
recbuff[5]='$';
recbuff[6]='$';
recbuff[7]='M';
recbuff[8]='D';
recbuff[9]='_';
recbuff[10]='D';
recbuff[11]='A';
recbuff[12]='T';
recbuff[13]='A';
recbuff[14]=4; /* name list */
recbuff[15]='C';
recbuff[16]='O';
recbuff[17]='D';
recbuff[18]='E';
compute_chksum(recbuff,19);
recbuff[19]=chksum;
fwrite((void *)recbuff,20,1,foutptr);
}
void write_segdef()
{
unsigned int seglen;
recbuff[0]=SEGDEF; /* record type */
recbuff[1]=7; /* two byte record length */
recbuff[2]=0;
recbuff[3]=0x60; /* ACBP byte, relocatable, paragraph aligned, private */
/* get length of data bytes plus banner length bytes */
seglen=path_len+banner_len+2+command_len;
movmem(&seglen,&recbuff[4],2); /* two byte segment length */
recbuff[6]=2; /* segment name index */
recbuff[7]=3; /* class name index */
recbuff[8]=1; /* overlay name index */
compute_chksum(recbuff,9);
recbuff[9]=chksum;
fwrite((void *)recbuff,10,1,foutptr);
}
void write_pubdef()
{
int offset;
recbuff[0]=PUBDEF; /* record type */
recbuff[1]=59; /* two byte record length */
recbuff[2]=0;
recbuff[3]=0; /* group index */
recbuff[4]=1; /* segment index */
recbuff[5]=10; /* name length */
movmem("$$ovl_path",&recbuff[6],10); /* name */
recbuff[16]=0; /* public offset */
recbuff[17]=0;
recbuff[18]=0; /* type index */
recbuff[19]=14; /* name length */
movmem("$$ovl_cmd_line",&recbuff[20],14); /* name */
movmem(&path_len,&recbuff[34],2); /* public offset */
recbuff[36]=0; /* type index */
recbuff[37]=20; /* name length */
movmem("$$ovl_signon_message",&recbuff[38],20); /* name */
offset=command_len+path_len;
movmem(&offset,&recbuff[58],2); /* public offset */
recbuff[60]=0; /* type index */
compute_chksum(recbuff,61);
recbuff[61]=chksum;
fwrite((void *)recbuff,62,1,foutptr);
}
void write_ledata()
{
int reclen,offset,loop,chunk;
if(path_len){ /* write path string */
recbuff[0]=LEDATA; /* record type */
reclen=path_len+4; /* length of string plus seg index, data offset, checksum bytes */
movmem(&reclen,&recbuff[1],2); /* two byte record length */
recbuff[3]=1; /* segment index */
recbuff[4]=0; /* enumerated data offset */
recbuff[5]=0;
movmem(pathstring,&recbuff[6],path_len); /* bytes of data */
compute_chksum(recbuff,path_len+6);
recbuff[path_len+6]=chksum;
fwrite((void *)recbuff,path_len+7,1,foutptr);
}
if(command_len){ /* write command line */
recbuff[0]=LEDATA; /* record type */
reclen=command_len+4; /* length of string plus seg index, data offset, checksum bytes */
movmem(&reclen,&recbuff[1],2); /* two byte record length */
recbuff[3]=1; /* segment index */
movmem(&path_len,&recbuff[4],2); /* enumerated data offset (comes after pathstring) */
movmem(commandstring,&recbuff[6],command_len); /* bytes of data */
compute_chksum(recbuff,command_len+6);
recbuff[command_len+6]=chksum;
fwrite((void *)recbuff,command_len+7,1,foutptr);
}
if(banner_len){ /* write sign-on banner message */
offset=command_len+path_len; /* data starts after path and command data strings */
for(loop=0;loop<banner_len;loop+=512){ /* write data in 512 byte chunks */
if(loop+512>=banner_len){ /* odd sized chunk */
chunk=banner_len-loop;
}
else{
chunk=512;
}
recbuff[0]=LEDATA; /* record type */
if(!loop){ /* first time thru */
chunk+=2; /* bump chunk to account for banner data length */
reclen=chunk+4;
movmem(&reclen,&recbuff[1],2); /* two byte record length */
recbuff[3]=1; /* segment index */
movmem(&offset,&recbuff[4],2); /* enumerated data offset */
movmem(&banner_len,&recbuff[6],2); /* banner length data */
movmem(&banbuff[loop],&recbuff[8],chunk-2); /* banner data */
compute_chksum(recbuff,chunk+6);
recbuff[chunk+6]=chksum;
fwrite((void *)recbuff,chunk+7,1,foutptr);
offset+=514;
}
else{
reclen=chunk+4;
movmem(&reclen,&recbuff[1],2); /* two byte record length */
recbuff[3]=1; /* segment index */
movmem(&offset,&recbuff[4],2); /* enumerated data offset */
movmem(&banbuff[loop],&recbuff[6],chunk); /* banner data */
compute_chksum(recbuff,chunk+6);
recbuff[chunk+6]=chksum;
fwrite((void *)recbuff,chunk+7,1,foutptr);
offset+=512;
}
}
}
}
void write_modend()
{
recbuff[0]=MODEND; /* record type */
recbuff[1]=2; /* two byte record length */
recbuff[2]=0;
recbuff[3]=0; /* module type */
compute_chksum(recbuff,4);
recbuff[4]=chksum;
fwrite((void *)recbuff,5,1,foutptr);
}
void compute_chksum(char *chkbuff,unsigned int numchars)
{
unsigned int i;
unsigned char total=0;
for(i=0;i<numchars;i++){
total+=chkbuff[i];
}
chksum=~total+1;
}