#ifdef	ournix
#include "ournix.h"
#endif
char sccsID[] = "@(#) lpr.c V2.0 Copyright Julian H. Stacey\n" ;
/* Puts a header on top of files, then queues them for print spooler
	written because msdos spooler is so primitive 
	it is not possible to request the file name be printed */

#define	HEADER			/* define if want header line with file name */

#define	RIGHT_MOST	79	/* right most char allowed before margin */
#define	INDENT		15	/* indent position for continuation lines */

#include <stdio.h> 
#include <ctype.h> 

#define FF	0xC	/* form feed = new page */
#define CR	0xD
#define NL	0xA
#define BS	8	/* backspace */
#define TABLN	8	/* no of space in a tab */
#define CWD_LN		100

char	header =
#ifdef	HEADER
		1 ;
#else
		0 ;
#endif
char	dir_hdr[] =	"Directory: ";
char	name_hdr[] =	"File: ";
#ifdef MSDOS
char	spool_dir[] =	"\\spool\\lpr\\";
#else
char	spool_dir[] =	"/spool/lpr\\";
#endif
FILE	*in_file;
FILE	*out_file;
char	**ARGV;
char	src_dir[CWD_LN]; 

FILE	*fopen();
extern char	*ctime(); 

int	column = 0;	/* left most char on page is column no. 0,
			tab positions are normally 0,8,16 etc */

main(argc, argv)
	int argc; char **argv;
	{

	ARGV = argv ;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */
	getcwd(src_dir,CWD_LN+2); 
	if (argc == 1) /* no args; copy standard input */ 
		{
		in_file = stdin ;
		doit("stdin");
		}
	else while (--argc > 0) 
	if ((in_file = fopen(*++argv, "r")) == NULL) 
		fprintf(stderr, "%s: can't open %s\n",*ARGV, *argv);
	else	{
		doit(*argv);
		(void) fclose(in_file);
		}
	exit(0);
	}

/* wraps line if would otherwise hit right margin */
wrap()
	{
	if ( column > RIGHT_MOST + 1)
		{
		column = 0 ;
		(void) fputc('\n',out_file);
		while (column++ < INDENT) (void) fputc(' ',out_file);
		}
	}

doit(file_name) /* copy file in_file to spooler directory & queue it */
	char *file_name;
	{
	register int	c;
	char	int_name[100], out_name[100], command[120];
	char	*ptr ;
	char	*base_name;	/* filename without directory	*/
	char	file_ext[13];	/* 8 ch filename + '.' + 3 char ext. + '\0' */
	long	req_time;

	/* get base_name (filename without directory) */
	for (ptr = file_name; *ptr != '\0' ; ptr++ ); 
	for (ptr-- ; (ptr >= file_name ) && (*ptr != '/') 
#ifdef MSDOS
		&& (*ptr != '\\') 
#endif
		; ptr-- ); 
	base_name = ++ptr ; 
/*	ptr & base_name now point at 1st char of name of file 
		(main part, not msdos 3 char extension, & not directory 
*/
	(void) strcpy(file_ext,base_name);	/*	Save for header */
/*	now avoid fred/file.new  & jim/file.new overlaying each other,
	delete the .new & insert a modulo time number (ie hopefully random) 
*/
	while ((*ptr != '\0') && (*ptr != '.')) ptr++ ;
	*ptr = '\0' ;
	(void) strcpy(int_name,spool_dir); (void)strcat(int_name,base_name);
	(void) time(&req_time) ;
	(void) sprintf(out_name,"%s.%d",int_name,(int)(req_time % 1000)) ;
#ifdef	DEBUG 
	printf("out_name is %s\n",out_name);
#endif
	if ((out_file = fopen(out_name, "w")) == NULL)
		{
		fprintf(stderr, "%s: can't open %s\n",*ARGV, out_name);
		return ;
		}
	if (header)	{	/* print header name */
		/* give rooted path name, (not just name in directory) */
		if ((*file_name == '\\') || (*file_name == '/'))
			fprintf(out_file,"%s%s",name_hdr,file_name); 
		else	{ 
			fprintf(out_file,"%s%s",name_hdr,file_ext); 
			fprintf(out_file,"   %s%s",dir_hdr,src_dir);
			if (file_name < base_name)
				{
				ptr = file_name ; 
				while ((ptr < base_name ) && ( *ptr != '\0' ))
					(void) fputc(*ptr++,out_file);
				}
			}
		fprintf(out_file,"   %s\n",ctime(&req_time)) ;
		}
	while (( c = fgetc(in_file)) != EOF) 
		{
		c &= 0x7f ;
		/* if isprint(c) column++ ; */
		if (( c >= ' ') && ( c <= '~')) column++ ;
		/* assume FF puts cursor to left margin */
		else if ((c == NL) || (c == CR) || ( c == FF)) column = 0 ;
		else if (c == '\t') 
			{
			do	{
				wrap();
				(void) fputc(' ',out_file) ;
				}
			while (++column % TABLN);
			}
		else if ((c == BS) && (column > 0)) column-- ;
			/* if preceeding char was a tab this will not delete
			the tab, but only the last space that the tab was 
			mapped to */
		if (c != '\t') { wrap() ; (void) fputc((char)c,out_file); }
		}
	(void) fclose(out_file);
	(void) sprintf(command,"print %s\r",out_name);
	if (system(command))
		{
		printf("Failed to run print spooler.\n");
		perror(*ARGV);
		}
	}
