home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
CMDS
/
hpset.lzh
/
hpset.c
next >
Wrap
Text File
|
1995-03-21
|
10KB
|
570 lines
/* hpset.c */
#define MAIN
#include <stdio.h>
#include <termcap.h>
#include <sgstat.h>
/* These function declarations are needed to initialize
the cmds[] structure.
*/
char *show_reset(), *show_orient(), *show_fsize(),
*show_fstyle(), *show_value(), *show_wrap(),
*show_eol(), *show_lmargin(), *show_weight();
void out_reset(), out_fsize(), out_fstyle(), out_value(), out_weight();
/* This structure contains everything we need to know about each
printer function. Note that we have included '%d's in the
.codes...these are replaced by actual values when they are
sent to the printer. Note that the order may be important...for example,
you most probably should make sure that RESET is the first function!
Other functions may depend on order (eg. margins may depend on font).
*/
struct
{
char *name; /* option name */
int default, /* default value */
min, /* minimum value */
max, /* maximum value */
new; /* new value set by user */
char *codes; /* printer codes for option */
char *(*dfunc)(); /* function returning option setting name */
void (*outfunc)(); /* function to send codes to printer */
}cmds[]=
{
"RESET", 1, 0, 1, 0,
"\x1bE", show_reset, out_reset,
"Orientation", 0, 0, 1, 0,
"\x1b&l%dO", show_orient, out_value,
"End of Line Wrap", 1, 0, 1, 0,
"\x1b&s%dC", show_wrap, out_value,
"Line Termination", 0, 0, 3, 0,
"\x1b&k%dG", show_eol, out_value,
"Font Size", 0, 0, 2, 0,
"\x1b(s%dH", show_fsize, out_fsize,
"Font Style", 0, 0, 1, 0,
"\x1b(s%dS", show_fstyle, out_value,
"Font weight", 1, 0, 2, 0,
"\x1b(s%dB", show_weight, out_weight,
"Copies", 1, 1, 99, 0,
"\x1b&l%dx", show_value, out_value,
"Lines per Inch", 6, 1, 48, 0,
"\x1b&l%dD", show_value, out_value,
"Page Length", 60, 1, 200, 0,
"\x1b&l%dP", show_value, out_value,
"Left Margin", 0, 0, 40, 0,
"\x1b&a%dL", show_lmargin, out_value
};
FILE *outpath;
char *printer="/p";
int nrows, /* number of rows on screen */
ncolumns, /* number of columns on screen */
numcmds,
termcap_ok;
extern int errno;
#define TCAPSLEN 1024
/* Termcap variables */
char PC_, /* pad character */
*BC, /* cursor backspace */
*UP; /* cursor up */
short ospeed; /* terminal speed */
static char *CM, /* cursor movement */
*CL, /* clear screen */
*SO, /* standout start */
*SE; /* standout end */
/* ======================================================================
Inititialize the termcap stuff
*/
init_termcap()
{
register char *term_type;
register int t;
static char tcapbuf[TCAPSLEN];
static char tcbuf[1024];
char *ptr;
char *temp;
extern char *getenv();
static struct
{
char *tcname;
char **tcptr;
}tc[]=
{
"cm", &CM,
"cl", &CL,
"so", &SO,
"se", &SE
};
if((term_type=getenv("TERM"))==0)
terminate("Environment variable TERM not defined!");
if(tgetent(tcbuf, term_type)<=0)
terminate("Unknown terminal type '%s'!",term_type);
ptr=tcapbuf;
if(temp=tgetstr("pc", &ptr)) PC_=*temp;
for(t=0; t<sizeof(tc)/sizeof(tc[0]); t++)
{
if((*tc[t].tcptr=tgetstr(tc[t].tcname, &ptr))==NULL)
terminate("Termcap needs '%s' entry\n", tc[t].tcname);
if(ptr>=&tcbuf[TCAPSLEN])
terminate("Termcap description too big");
}
nrows=tgetnum("li");
ncolumns=tgetnum("co");
if(!nrows || !ncolumns)
terminate("Unable to determine screen size");
if(ncolumns<80 || nrows<24)
terminate("Screen must be at least 80x24");
termcap_ok=1;
}
putpad(s)
char *s;
{
extern int write1(); /* function to print 1 character */
if(s) tputs(s,1,write1);
}
write1(c)
char c;
{
putc(c,stdout);
}
gotoxy(x,y) /* position cursor at specified x/y */
int x,y;
{
putpad(tgoto(CM,x,y));
}
clrscrn() /* Clear video screen */
{
putpad(CL);
}
revon() /* Turn on reverse video */
{
putpad(SO);
}
revoff() /* Turn off reverse video */
{
putpad(SE);
}
/* ==================================================
Set up the terminal
*/
static struct sgbuf pathbf1, pathbf2;
static char pathbfv=0; /* flag, 1==path ok to reset */
setterml()
{
setbuf(stdin, NULL); /* single char input */
if(!pathbfv)
{
_gs_opt(1,&pathbf1);
_gs_opt(1,&pathbf2);
pathbfv++; /* signal okay to restore */
pathbf1.sg_echo=
pathbf1.sg_pause=
pathbf1.sg_eofch=
pathbf1.sg_psch=
pathbf1.sg_kbich=
pathbf1.sg_kbach=0;
}
_ss_opt(1,&pathbf1);
ospeed=pathbf2.sg_baud; /* set terminal speed for termcap */
}
resterml()
{
if(pathbfv) _ss_opt(1, &pathbf2);
fflush(stdout);
}
/* ======================================================================
Display usage message
*/
help()
{
register int t;
static char *msg[]=
{
"Syntax: HPSet <opts>",
"Options:",
"\t-p=path specify printer",
"\t-? this message"
};
for(t=0; t<sizeof(msg)/sizeof(msg[0]); puts(msg[t++]));
}
/* ============================================================
Disaster exit
*/
terminate(s, p1, p2, p3, p4, p5)
char *s, *p1, *p2, *p3, *p4, *p5;
{
if(termcap_ok)
{
gotoxy(ncolumns-1, nrows-1);
putc('\n', stdout);
}
if(s)
{
printf(s, p1, p2, p3, p4, p5);
putc('\n', stdout);
}
resterml();
exit(0);
}
/* ======================================================================
This set of functions are used by showline() to determine the
'value' to print for a options.
*/
char *
show_reset(value) /* reset printer yes/no */
int value;
{
static char *msg[]=
{
"No",
"Yes"
};
return msg[value];
}
char *
show_orient(value) /* page orientation */
int value;
{
static char *msg[]=
{
"Portrait",
"Landscape"
};
return msg[value];
}
char *
show_fsize(value) /* font pitch (cpi) */
int value;
{
static char *msg[]=
{
"Pica (10cpi)",
"Elite (12cpi)",
"Compressed (16.6cpi)"
};
return msg[value];
}
char *
show_fstyle(value) /* typestyle (roman/italic) */
int value;
{
static char *msg[]=
{
"Roman (upright)",
"Italic"
};
return msg[value];
}
char *
show_wrap(value) /* EOF wrap on/off */
int value;
{
static char *msg[]=
{
"Enabled",
"Disabled"
};
return msg[value];
}
char *
show_eol(value) /* EOL handling */
int value;
{
static char *msg[]=
{
"CR=CR, LF=LF, FF=FF",
"CR=CR+LF, LF=LF, FF=FF",
"CR=CR, LF=CR+LF, FF=CR+FF",
"CR=CR+LF, LF=CR+LF, FF=CR+FF"
};
return msg[value];
}
char *
show_lmargin(value) /* Left margin handling */
int value;
{
static char buff[20];
sprintf(buff, "%d characters", value);
return buff;
}
char *
show_weight(value) /* Typeface weight */
int value;
{
static char *msg[]=
{
"Light",
"Medium",
"Bold"
};
return msg[value];
}
char *
show_value(value) /* numeric values */
int value;
{
static char buff[20];
sprintf(buff, "%d", value);
return buff;
}
/* ======================================================================
These functions send the option value to the printer. They are needed
since some cmds[] values need to be converted to printer values.
*/
void
out_reset(n) /* reset...don't send if value==0 */
register int n;
{
if(cmds[n].new) fprintf(outpath, cmds[n].codes);
}
void
out_fsize(n) /* font size (pitch) */
register int n;
{
register int t;
switch(cmds[n].new)
{
default:
case 0: t=10; break;
case 1: t=12; break;
case 2: t=17; break;
}
fprintf(outpath, cmds[n].codes, t);
}
void
out_weight(n)
{
register int t;
switch(cmds[n].new)
{
default:
case 0: t=-3; break;
case 1: t=0; break;
case 2: t=3; break;
}
fprintf(outpath, cmds[n].codes, t);
}
void
out_value(n) /* send a numeric value */
register int n;
{
fprintf(outpath, cmds[n].codes, cmds[n].new);
}
/* ======================================================================
Display a option line on the screen
*/
showline(n, mode)
int n, mode;
{
register char *p1, *p2;
register int l1, l2;
gotoxy(5, n+2);
if(mode) revon();
p1=cmds[n].name;
p2=(*cmds[n].dfunc)(cmds[n].new);
l1=strlen(p1);
l2=strlen(p2);
fputs(p1, stdout);
while(l1++<20) putc(' ', stdout);
fputs(p2, stdout);
while(l2++<30) putc(' ', stdout);
if(mode) revoff();
}
/* ======================================================================
M A I N
*/
main(argc, argv)
int argc;
char *argv[];
{
register int t, key, n;
register char *p;
for(t=1; t<argc; t++)
{
p=argv[t];
if(*p++!='-') usage();
switch(tolower(*p++))
{
case 'p':
if(*p=='=') p++;
printer=p;
break;
default:
usage();
}
}
init_termcap();
setterml();
for(t=0; t<sizeof(cmds)/sizeof(cmds[0]); t++)
cmds[t].new=cmds[t].default;
clrscrn();
gotoxy(0,0);
printf(" HPSET - HP Printer Setup -- (c) 1995, Bob van der Poel Software");
gotoxy(0,nrows-1);
printf("KEYS: j/k move cursor, h/l change setting, q to quit, <ENTER> sets printer");
for(t=0; t<sizeof(cmds)/sizeof(cmds[0]); t++) showline(t, 0);
for(t=0; ; )
{
showline(t, 1);
key=getc(stdin);
showline(t, 0);
switch(tolower(key))
{
case 'j':
if(t< sizeof(cmds)/sizeof(cmds[0])-1) t++;
else t=0;
break;
case 'k':
if(t) t--;
else t=sizeof(cmds)/sizeof(cmds[0])-1;
break;
case 'l':
if(cmds[t].new<cmds[t].max) cmds[t].new++;
else cmds[t].new=cmds[t].min;
break;
case 'h':
if(cmds[t].new>cmds[t].min) cmds[t].new--;
else cmds[t].new=cmds[t].max;
break;
case '\n':
if((outpath=fopen(printer, "w"))==NULL)
terminate("Can't open path to '%s', error %d",
printer, errno);
for(n=0; n<sizeof(cmds)/sizeof(cmds[0]); n++)
(*cmds[n].outfunc)(n);
fclose(outpath);
terminate("Printer codes sent");
case 'q':
terminate("\x07*** PRINTER NOT SET ***");
}
}
}
usage()
{
static char *msg[]=
{
"HPSET (c) Bob van der Poel Software, 1995",
"Syntax: Hpset [opts]",
"Usage: Sets options on HP Laser Printer",
"Options:",
" -p[=]path set printer port (default=/p)"
};
register int t;
for(t=0; t<sizeof(msg)/sizeof(msg[0]); puts(msg[t++]) );
terminate(NULL);
}