From: ucacmsu@ucl.ac.uk (Mr Stephen R Usher)
Newsgroups: comp.sys.sun.admin
Subject: LaserJet printer input filter for Postscript.
Date: 10 Nov 92 09:33:44 GMT
Organization: Bloomsbury Computing Consortium, London


Due to the many requests I've had for my filter I've decided to post it
here, so here it comes!

Steve

--- Cut --- Here ---
/*
 * ljetfilt.c	Version 1.0 10th November 1992.
 *
 * Copyright 1992 S.R.Usher.
 *
 * This source may be freely distributed and modified as long as this
 * copyright notice stays intact.
 *
 */

#include <stdio.h>
#include <sys/fcntl.h>
#include <sys/param.h>

#define BUFFER_LENGTH 1024
#define GS_COMMAND_STRING "/usr/local/bin/gs -q -dNOPAUSE -sDEVICE=laserjet -sOUTPUTFILE=%s -"

#ifdef DEBUGIT
FILE *debug_file;
#endif

main(argc, argv)
int argc;
char *argv[];
{
	static char buffer[BUFFER_LENGTH] = { 0 };
	char start_chars[3];
	static int isps = 0;

	int infd, outfd;

	int error;

#ifdef DEBUGIT
	char pathname[MAXPATHLEN];
	int i;

	if ((debug_file = fopen("/tmp/ljdebug", "w")) == NULL)
	{
		exit(1);
	}

	for (i = 0; i < argc; fprintf(debug_file, "%s ", argv[i++]));

	fprintf(debug_file, "\n");

	fprintf(debug_file, "Current directory is %s\n", getwd(pathname));

	fflush(debug_file);
#endif
	setpgid(0, 0);
#ifdef FILES
	switch (argc)
	{
	case 1:
		break;
	case 2:
		if ((infd = open(argv[1], O_RDONLY)) == -1)
		{
			perror(argv[1]);
			exit(1);
		}

		dup2(infd, 0);
		close(infd);
		break;
	case 3:
		if ((infd = open(argv[1], O_RDONLY)) == -1)
		{
			perror(argv[1]);
			exit(1);
		}

		if ((outfd = open(argv[2], (O_WRONLY | O_CREAT | O_TRUNC), 0644)) == -1)
		{
			perror(argv[2]);
			exit(1);
		}

		dup2(infd, fileno(stdin));
		dup2(outfd, fileno(stdout));
		close(infd);
		close(outfd);
		break;
	default:
		usage(argv[0]);
		exit(1);
	}
#endif /* FILES */
	fgets(buffer, BUFFER_LENGTH - 1, stdin);

	if (strlen(buffer) > 2)
	{
		strncpy(start_chars, buffer, 2);

		start_chars[2] = '\0';

		if (!strcmp(start_chars, "%!"))
			isps = 1;
	}

	error = 0;

	if (isps)
		error = output_to_gs(buffer);
	else
		error = output_to_file(buffer);

	printf("\033(s0P\004\n");

	fflush(stdout);

#ifdef DEBUGIT
	fprintf(debug_file, "Returning code %d.\n", error);
	fflush(debug_file);
	fclose(debug_file);
#endif

	exit(error);
}

usage(name)
char *name;
{
	fprintf(stderr, "Usage: %s [input-file [output-file]]\n", name);
}

output_to_gs(buffer)
char buffer[BUFFER_LENGTH];
{
	FILE *fp;
	int length;
	char command[1024];
	char *tmpfile;

	if ((tmpfile = tempnam("/tmp", "ljflt")) == NULL)
	{
		perror("tempnam");
		return 1;
	}

	sprintf(command, GS_COMMAND_STRING, tmpfile);

#ifdef DEBUGIT
	fprintf(debug_file, "Command string is \"%s\"\n", command);
#endif

	if ((fp = popen(command, "w")) == NULL)
	{
		perror("popen");
		return 1;
	}

	fprintf(fp, "%s", buffer);

	while (fgets(buffer, BUFFER_LENGTH - 1, stdin) != NULL)
		fprintf(fp, "%s", buffer);

	fprintf(fp, "quit\n");

	fflush(fp);

	pclose(fp);

	if ((fp = fopen(tmpfile, "r")) == NULL)
	{
		perror("fopen");
		return 1;
	}

	while ((length = read(fileno(fp), buffer, BUFFER_LENGTH)) > 0)
		write(fileno(stdout), buffer, length);

	fclose(fp);

	unlink(tmpfile);

	return 0;
}

output_to_file(buffer)
char buffer[BUFFER_LENGTH];
{
	int length;
	
	printf("%s", buffer);
	
	while(fgets(buffer, BUFFER_LENGTH -1 ,stdin) != NULL)
		printf("%s", buffer);

	printf("\f");

	fflush(stdout);

	return 0;
}
--- Cut --- Here ---
--
Addresses:-
JANET:-		ucacmsu@uk.ac.ucl or	steve@uk.ac.ox.earth (preferable)
Internet:-	ucacmsu@ucl.ac.uk or	steve@earth.ox.ac.uk (preferable)
Newsgroups: comp.sys.sun.admin