/*
 *	 for Computer Innovations C-86 compiler and the IBM-PC,
 *	 intended as an example of C programming and released to
 *	 the public domain by:
 *
 *		   Robert B. Wooster, jr.
 *		   126 Valley Forge Road
 *		   Weston, CT 06883
 *		   Compuserve: 72415,1602   Source: CL1746
 *
 *	 to use subsitute your name, etc. where appropriate
 *	 compile and link to the CI library. ( A portable version
 *	 is in the works.)
 */

/*
 *	      where: s	source file format, i.e. 132 cols, line
 *			numbers, full heading
 *		     l	letterhead format
 *		     m	memo format
 *		     e	editing format, ie double spaced
 *	       default	80 columns, header, no line numbers
 *
 *	      if there is no file name a help message is printed
 */


#include "stdio.h"

#define  FALSE	   0
#define  TRUE	   1

char	 *afile[];



main( argc, argv )
int	 argc;
char	 *argv[];
{
	 char	   mode;
	 int	   i,	     numfiles = 0;

	 mode = 'd';
	 afile[0] = "_____";

	 /*   check arguments					*/
	 if( argc == 1 )     /*   no arguments			*/
	      help();
	 else if( argv[1][0] == '-')   /* option argument       */
	 {
	      switch( tolower(argv[1][1]) )
	      {
		   case 's':
		   case 'l':
		   case 'm':
		   case 'e': mode = tolower( argv[1][1]);
	     }
	     if( argc == 3 )
		   afile[0] = argv[2], numfiles = 1;
	     else  help();
	 }
	 else afile[0] = argv[1], numfiles = 1; /* filename argument*/

	 /*   print each file					*/
	 for( i = 0; i < numfiles; i++ )
	      printfile(afile[i],mode);

	 exit(0);
}	 /*	   main 			      */

help()
{
	 printf("\tusage: fpr [-|s|l|m|e|] filename\n");
	 printf("\t\tfilename must be present\n");
	 printf("\t\t-s source listing, 132 col w. line #s\n");
	 printf("\t\t-l letterhead\n");
	 printf("\t\t-m memorandum heading\n");
	 printf("\t\t-e edit mode, i.e. double space\n");
	 printf("\t\tdefaults to 80 cols with heading\n");
	 printf("\n\n\n");
	 abort("\t\tPlease re-enter command.\n");
}

#define  LF	   10
#define  FF	   12
#define  ENLARGE   14
#define  COMPRESS  15
#define  XCOMP	   18
#define  ESC	   27
#define  INIT	   64
#define  HT	   9

int	 pagenum,  linenum,  numlines, cpl = 80;
char	 *fname,   fdate[10] = "   ";
FILE	 *pr, *filein;

printfile(tfile, opt)
char	 *tfile,   opt;
{
	 int	   i,	slnum = 0;
	 char	   *ctemp;

	 pagenum = linenum = 1;
	 numlines = 60;
	 fname = tfile;
	 getsdate();

	 /*   get file to be printed				*/
	 if( (filein = open(fname ,AREAD)) < 0)
	 {
	      printf("Can't open %s\n", fname);
	      return(0);
	 }

	 /*   initialize printer				*/
	 if( (pr = fopen("PRN:","w")) == 0)
	      abort("printer cannot be opened");
	 fputc(ESC, pr),     fputc(INIT, pr);

	 /*   setup for individual option			*/
	 switch ( opt )
	 {
	      case 'l': letthd();      break;
	      case 'm': memohd();      break;
	      case 's': fputc(COMPRESS, pr);
			cpl = 132;
	       default: pghd(opt);
	 }

	 /*   read/write loop					*/
	 /*   from the standpoint of style this should probably */
	 /*   be placed in a separate function			*/
	 while( (i = read( filein, ctemp, 156)))
	 {
	      ctemp[i] = '\0';
	      if( opt == 's' )
	      {
		   slnum++;
		   fprintf( pr, "\t%5d\t%s", slnum, ctemp);
	      }
	      else if( opt == 'e' ) fputc(LF, pr), linenum++;
	      else fprintf( pr, "%s",ctemp);
	      if( (++linenum%numlines) == 0)
	      {
		   fputc(FF, pr);
		   pagenum++;
		   pghd(opt);
	      }
	 }

	 /*   finish up printer 				*/
	 fputc(FF, pr), fputc(ESC, pr), fputc(INIT, pr);
	 fclose( pr );
	 return(0);
}

pghd(opt)
char opt;
{
	 int i;

	 fputc(LF, pr), fputc(LF, pr), fputc(LF, pr);
	 switch ( opt )
	 {
	      case 'm':
	      case 'l': fprintf(pr,"YOUR NAME\n"); break;
	       default: fputc(ENLARGE, pr);
			fputs(fname, pr),   fputc('\n', pr);
	 }
	 fputs(fdate, pr);
	 for( i = 15; i < cpl-10; i++ ) fputc('_', pr);
	 fprintf( pr, "Page %2d\n\n", pagenum);
	 linenum += 6;
}

letthd()
{
	 fputc(LF, pr), fputc(LF, pr);
	 fputc(COMPRESS, pr),	  fputc(ENLARGE, pr);
	 pcenter(66,"YOUR NAME");
	 pcenter(132,"YOUR ADDRESS");
	 pcenter(132,"MORE OF YOUR ADDRESS");
	 pright(132,"voice: (nnn) nnn-nnnn");
	 pright(132,"modem: (nnn) nnn-nnnn");
	 pright(132,"Source: XXnnnn");
	 pright(132,"Compuserve: nnnnn,nnnn");
	 fputc(XCOMP, pr),   fputc(LF, pr);
	 pright(80, fdate),  fputc(LF, pr);
	 linenum += 12;
}

memohd()
{
	 fputc(LF, pr), fputc(LF, pr), fputc(ENLARGE, pr);
	 pcenter(40,"MEMORANDUM");
	 fputc(LF, pr), fputc(LF, pr);
	 pright(80, fdate),  fputc(LF, pr);
	 linenum += 7;
}

pcenter(llen, strg)
int llen;
char *strg;
{
	 int  i;

	 for( i = 1; i <= (llen/2-strlen(strg)/2); i++)
	      fputc(' ', pr);
	 fprintf( pr, "%s\n", strg);
}

pright(llen, strg)
int llen;
char *strg;
{
	 int  i;

	 for( i = 1; i < (llen - strlen(strg)); i++)
	      fputc(' ', pr);
	 fprintf( pr, "%s\n", strg);
}

char mos[12][4] = {"Jan","Feb","Mar","Apr","May","Jun",
		   "Jul","Aug","Sep","Oct","Nov","Dec"};
getsdate()
{
	 struct {int ax, bx, cx, dx, si, di, ds, es;} t;
	 int day, month;

	 t.bx=t.cx=t.dx=t.si=t.di=t.ds=t.es=0;
	 t.ax = 0x2A00;
	 sysint(0x21,&t,&t);
	 day = t.dx%0x100;	  /* DL */
	 month = t.dx/0x100;	  /* DH */
	 if( day > 9 ) fdate[0] = (char)(day/10 + '0');
	 fdate[1] = (char)(day%10 + '0');
	 strcat(fdate,mos[month-1]);
	 strcat(fdate,"-83");     /* kluge-sysint doesn't return */
				  /* CS, which holds year	 */
}
