[comp.lang.postscript] ato2pps

edwards@dogie.edu ( Mark Edwards) (12/16/88)

Recently I got a shell version of this program off the net and 
really enjoyed the output. Here is a version that I hacked 
together with code from a program called text_ps.c and the shell
version of a2ps. I wanted to port the program over to my pc and 
our VAX, but we don't have sed, awk, and so forth working on our
VAX and pipes are to labor intensive on a PC.  (Next I'm going
to port it to my Macintosh. If anyone wants a copy let me know.)

Its easy to use. Just compile and link it. The program currently
drops any extensions on the file and appends ".ps" to the end.
So to use it:

ato2pps file.c

print file.ps


enjoy,
mark




================  cut here  =========================================
/*
 *  ato2pps.c      ascii to 2 page on one page postscript
 *
 *      15 Dec 1988     by edwards@dogie.macc.wisc.edu
 *			Hacked cplai@daisy version and Eric Gisin C code
 *                     	together in C. Tested under MSDOS, VMS and BSD4.3.
 * 
 *                      ato2pageps file.c    /* output goes to file.ps */
 *
 *      11/29/88	cplai@daisy	added pnumbuf to allow printing of ",
 * 
 *      Created: Wed May 25 11:22:04 1988 by evan@csli (Evan Kirshenbaum)",
 *
 *	Copyright (c) 1986, Eric Gisin, egisin@waterloo.CSNET
 *
 *	This program may be copied and redistributed, provided
 *	no fee is charged and this copyright notice is preserved.
 */

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

#define	LLen	80
#define	PLen	66

char *	Head [] = {
"%!  PostScript Source Code",
"%  File: csli:/user/evan/bin/a2ps.ps",
"%  Created: Wed May 25 11:22:04 1988 by evan@csli (Evan Kirshenbaum)",
"%  Version: 1.0(1)",
"%  Description: PostScript prolog for a2ps ascii to PostScript program.",
"% ",
"%  Edit History:",
"%  11/29/88	cplai@daisy	added pnumbuf to allow printing of ",
"%				10 or more pages",
"",
"/xdef {exch def} bind def",
"/inch {72 mul} bind def",
"/getfont {exch findfont exch scalefont} bind def",
"",
"/filenamesize 12 def",
"/filenamefont /Helvetica-Bold filenamesize getfont def",
"/linesperpage 66 def",
"",
"/datesize filenamesize 2 sub def",
"/datefont /Helvetica datesize getfont def",
"/headersize filenamesize 4 add def",
"/bodysize 6.8 def",
"/bodyfont /Courier bodysize getfont def",
"/sidemargin 4 def",
"/topmargin 4 def",
"/pagewidth ",
"   bodyfont setfont (0) stringwidth pop 80 mul sidemargin dup add add",
"   def",
"/pageheight",
"   bodysize linesperpage mul topmargin dup add add headersize add",
"   def",
"/uppery 8.5 inch pageheight add 2 div def",
"/upperx [ 11 inch pagewidth 2 mul sub 3 div",
"          dup 2 mul pagewidth add ] def",
"",
"/pnumbuf 10 string def",
"",
"/endpage",
"   { pageside 1 eq",
"       { /pageside 0 def",
"	 numberpage",
"         copypage erasepage ",
"	 /sheet sheet 1 add def }",
"       { /pageside 1 def }",
"     ifelse",
"     /pagenum pagenum 1 add def",
"    } bind def",
"/numberpage",
"    { 11 inch upperx 0 get sub sidemargin add",
"      8.5 inch uppery sub headersize sub moveto",
"      datefont setfont",
"      sheet pnumbuf cvs show",
"    } bind def",
"/newfile",
"    { /filename xdef",
"      /pagenum 1 def ",
"    } bind def",
"/cleanup",
"    { pageside 1 eq",
"      {numberpage showpage} if",
"    } bind def",
"/startdoc",
"    { 8.5 inch 0 inch translate",
"      90 rotate ",
"      /pageside 0 def",
"      /sheet 1 def",
"    } bind def",
"/startpage",
"    { printheader",
"      printborder",
"      upperx pageside get sidemargin add",
"      uppery topmargin sub bodysize sub headersize sub moveto",
"      bodyfont setfont",
"    } bind def",
"/s  { gsave",
"        show",
"      grestore",
"      0 bodysize neg rmoveto",
"    } bind def",
"/printheader",
"    { upperx pageside get uppery headersize sub 1 add moveto",
"      datefont setfont",
"      gsave",
"        sidemargin 2 rmoveto date show",
"      grestore",
"      gsave",
"	/pnum pagenum pnumbuf cvs def",
"        pagewidth sidemargin sub pnum stringwidth pop sub",
"        (Page ) stringwidth pop sub 3 rmoveto",
"        (Page ) show pnum show",
"      grestore",
"      gsave",
"        filenamefont setfont",
"        pagewidth filename stringwidth pop sub 2 div 2 rmoveto",
"        filename show",
"      grestore",
"    } bind def",
"/printborder ",
"    { upperx pageside get uppery moveto",
"      gsave",
"        pagewidth 0 rlineto",
"        0 pageheight neg rlineto",
"        pagewidth neg 0 rlineto",
"        closepath stroke",
"      grestore",
"      0 headersize neg rmoveto pagewidth 0 rlineto stroke",
"    } bind def",
	NULL};

int	wflag = 0;
int	page = 0;
FILE *o;

main(argc, argv)
	char **	argv;
{
	int	i;
	int	nofiles = 1;

	for (i = 1; i < argc; i ++)
	    if (argv[i][0]=='-')
		switch (argv[i][1]) {
		  case 'w':
			wflag = !wflag;
			break;
		  default:
			fprintf(stderr, "Usage: %s [-w] file ...\n", argv[0]);
			exit(1);
		}
	    else {
		FILE *f;
  		int j,k;
 		char outfile[30];

		nofiles = 0; j=0;
		f = fopen(argv[i], "r");
		if (f==NULL) {
			fprintf(stderr, "%s: Can't open input %s\n", argv[0], argv[i]);
			continue;
		}
 		strcpy(outfile, argv[i]);
		while(outfile[j] != 0)
		{
  			if (outfile[j] == '.')
			  break;
			j++;
		}
		outfile[j]=0;
		strcat(outfile,".ps");
		printf("File is %s \n",outfile);
		o = fopen(outfile, "w");
	        for (k = 0; Head[k]!=NULL; k ++)
		fprintf(o,"%s\n", Head[k]);
		fprintf(o,"(%s) newfile \n",argv[i]);
		text_ps(f);
		fclose(f);
		fclose(o);
	    }
	if (nofiles)
		text_ps(stdin);
}

text_ps(f)
	register FILE *	f;
{
	register c;
	int	row = 0;
	int	col = 0;
	char	line [LLen+1];
        long t, time();


        char tchar[30], tim[6], date[12];

        t = time();
        strcpy(tchar,ctime(&t));
        convtime(tchar,tim,date);
        fprintf(o,"/date (%s %s) def\n",date,tim);
	fprintf(o,"startdoc\n");
	while (1) switch((c = getc(f))) {
	  case EOF:
	  case '\r':
	  case '\n':
	  case '\f':
		if (c==EOF && row==0)
		{
			fprintf(o,"cleanup\n");
			return;
		}
		if (row==0) {
			page ++;
			fprintf(o,"startpage\n");
		}
		if (col>0) {
			line[(col<LLen) ? col : LLen] = '\0';
			putstring(line);
			fprintf(o," s\n");
		}
		if (c=='\n')
			row ++;
		if (row==PLen || c=='\f' || c==EOF) {
			fprintf(o,"endpage ");
			row = 0;
		}
		if (c==EOF)
		{
			fprintf(o,"cleanup\n");
			return;
		}
		col = 0;
		break;
	  case '\t':
		while (col<LLen) {
			line[col++] = ' ';
			if (col%8 == 0) break;
		}
		break;
	  case '\b':
		if (col>0)
			col --;
		break;
	  default:
		if (col<LLen)
			line[col++] = (isascii(c) && isprint(c)) ? c : ' ';
	}
}

putstring(s)
	register char *s;
{
	register c;

	fputc('(',o);
	while ((c = *s++) != '\0') {
		if (c=='\\' || c=='(' || c==')')
			fputc('\\',o);
		fputc(c,o);
	}
	fputc(')',o);
}

convtime(tchar,t,date)
char *tchar, *t, *date;
{
   
   t[0] = tchar[11];
   t[1] = tchar[12];
   t[2] = tchar[13];
   t[3] = tchar[14];
   t[4] = tchar[15];
   t[5] = 0;

   date[0] = tchar[8];
   date[1] = tchar[9];
   date[2] = tchar[3];
   date[3] = tchar[4];
   date[4] = tchar[5];
   date[5] = tchar[6];
   date[6] = tchar[7];
   date[7] = tchar[20];
   date[8] = tchar[21];
   date[9] = tchar[22];
   date[10] = tchar[23];
   date[11] = 0;

} /*  convtime  */
-- 
    edwards@vms.macc.wisc.edu
    UW-Madison, 1210 West Dayton St., Madison WI 53706