Category : C Source Code
Archive   : SMALLC.ZIP
Filename : FORMAT1.C

 
Output of file : FORMAT1.C contained in archive : SMALLC.ZIP
/* Text formatter: main program
*
* text formatter transcribed from the book
* Software Tools by Kernighan and Plauger
*
* Source: format1.c
* Version: Dec. 31, 1980.
*/




/* text formatter main program (final version) */


main()
{
char inbuf[INSIZE];
init();
while (getlin(inbuf)!=EOF) {
if (inbuf[0]=='.') {
command(inbuf);
}
else {
text(inbuf);
}
}
if (lineno>0) {
space(HUGE);
}
closeout(); /* close output file */
}


/* initialize all variables.
* open files.
*/


init()
{
/* sign on */
puts("Welcome to format version: April 5, 1981.");
/* common /cparam/ */
fill=YES;
lsval=1;
inval=0;
rmval=PAGEWIDTH;
tival=0;
ceval=0;
ulval=0;
/* common /cpage/ */
curpag=0;
newpag=1;
lineno=0;
plval=PAGELEN;
m1val=2;
m2val=2;
m3val=2;
m4val=2;
bottom=plval-m3val-m4val; /* invariant */
header[0]=NEWLINE;
header[1]=EOS;
footer[0]=NEWLINE;
footer[1]=EOS;
/* common /cout/ */
outp=0;
outw=0;
outwds=0;
/* spread() */
dir=0;
/* I/O routines */
openout();
ineof=YES; /* force openin() */
}


/* perform formatting command */


command(buf) char *buf;
{
int argtyp;
int val;
int spval;
val=getval(buf,&argtyp);
if (lookup(buf,"bp")) {
if (lineno>0) {
space(HUGE);
}
set(&curpag,val,argtyp,curpag+1,-HUGE,HUGE);
newpag=curpag;
}
else if (lookup(buf,"br")) {
brk();
}
else if (lookup(buf,"ce")) {
brk();
set(&ceval,val,argtyp,1,0,HUGE);
}
else if (lookup(buf,"fi")) {
brk();
fill=YES;
}
else if (lookup(buf,"fo")) {
gettl(buf,footer);
}
else if (lookup(buf,"he")) {
gettl(buf,header);
}
else if (lookup(buf,"in")) {
set(&inval,val,argtyp,0,0,rmval-1);
tival=inval;
}
else if (lookup(buf,"ls")) {
set(&lsval,val,argtyp,1,1,HUGE);
}
else if (lookup(buf,"nf")) {
brk();
fill=NO;
}
else if (lookup(buf,"pl")) {
set(&plval,val,argtyp,PAGELEN,
m1val+m2val+m3val+m4val+1,HUGE);
bottom=plval-m3val-m4val;
}
else if (lookup(buf,"rm")) {
set(&rmval,val,argtyp,PAGEWIDTH,
tival+1,HUGE);
}
else if (lookup(buf,"sp")) {
set(&spval,val,argtyp,1,0,HUGE);
space(spval);
}
else if (lookup(buf,"ti")) {
brk();
set(&tival,val,argtyp,0,0,rmval);
}
else if (lookup(buf,"ul")) {
set(&ulval,val,argtyp,0,1,HUGE);
}
else {
return; /* ignore unknown commands */
}
}


/* lookup routines for commands.
* return true if buf starts with string.
*/


lookup(buf,string) char *buf, *string;
{
return( (buf[1]==string[0]) *
(buf[2]==string[1]) );
}


/* evaluate optional numeric argument */


getval(buf,argtyp) char *buf; int *argtyp;
{
int i;
/* skip command name */
i=1;
while ( (buf[i]!=' ') *
(buf[i]!=TAB) *
(buf[i]!=NEWLINE) ) {
i++;
}
/* find argument */
while ( (buf[i]==' ') +
(buf[i]==TAB) ) {
i++;
}
*argtyp=buf[i];
if ( (*argtyp=='+') +
(*argtyp=='-') ) {
i++;
}
return(ctoi(buf,i));
}


/* set parameter and check range */


set(param,val,argtyp,defval,minval,maxval)
int *param,val,argtyp,defval,minval,maxval;
{
if (argtyp==NEWLINE) {
*param=defval;
}
else if (argtyp=='+') {
*param=*param+val;
}
else if (argtyp=='-') {
*param=*param-val;
}
else {
*param=val;
}
*param=min(*param,maxval);
*param=max(*param,minval);
}




  3 Responses to “Category : C Source Code
Archive   : SMALLC.ZIP
Filename : FORMAT1.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. 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/