🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.postscript
Article #606

txt2ps source

#606
From: Anthony Foiani
Date: Fri, 23 Jun 1989 18:41
327 lines
8457 bytes
well... since I am having troubles posting the source to alt.sources,
I'll just put it in this group...

------------ cut here -------------
/* printer.c version 1.2 */
/* last modified 2/4/86 23:07:05 */
/* this file created 2/4/86 23:07:19 */
/* text file to PostScript converter
 * 66 lines/page, elite typeface
 * suggest this be used in conjunction with pr(1)
 *
 * Copyright (c) 1986 by Pipeline Associates, Inc.
/* text2ps.c */
/*
Modifications:
 
   7/22/88
   by Marc Prewitt
      NMSU Computer Center
      Small Systems

       Added switches:
	 -l (landscape)
	 -b (BitNet)
	 -o Output
	 -p n (n = page length, default = 66 for landscape, 57 for portrait)
	 -f fontname 
	 -z fontsize
	 -s n (n = line spacing, default = 1)
	 -m n (n = left margin in inches)
	 -t n (n = left margin in inches)

    10/15/88
    MP
    Included support for environment variables.  The program checks for an 
    environment variable with the same name as argv[0], and retrieves options
    from that variable.  In this way the different names can be linked to
    the same code and environment variables created for different options.
*/

 /* Future Features:
      legal size paper option
      parse out mail files and print specified messages
      multiple copies
 */

#include <stdio.h>
#include <time.h>
#include <fcntl.h>
  
#define TRUE  1
#define FALSE  0
  
  /* #define TURBOC */

#ifdef TURBOC
#define	_rename(A, B)	rename(A, B)
#else
#define	_rename(A, B)	link(A, B)
#endif

int TOP, PAGELENGTH = 66;
double atof(), page_size = 792, left_margin = 72, top_margin = 36, SPACING = 1;
int land = FALSE, bitnet = FALSE, use_stdin;
char font[20]  = {"Courier"}, *getenv ();
int fontsize = 10;
int get_opts();
char temp_outfile[L_tmpnam] = {""}, outfile[20];

main (argc, argv)
     int argc;
     char *argv[];
{
  register int col;
  register int line;
  int y_pgptr, c, i, j, page;
  
  long time(), now;
  
  /* Get and set arguments from environment variable txt2ps */
  get_env_args(argv[0]);
  
  /* Identify and set all switches and set j to the last 
     non-option in argv 	   */
  j = get_args(argc, argv);
  
  /* if there are no more args then input is from stdin else from files */
  use_stdin = (j >= argc); 
  
  for (i = j; (i < argc) || use_stdin; i++){
    if (use_stdin == FALSE)
      {
	if (freopen(argv[i], "r", stdin) == (FILE *) NULL)
	  {
	    perror (argv[i]);
	    continue;
	  }
      }
    else 
      use_stdin = FALSE; /* reset so we only do this loop once. */
    
    
    page = 1;
    line = 1;
    col = 1;
    TOP = page_size - top_margin; 
    y_pgptr = TOP;
    
    printf ("%%%!PS-Adobe-1.0\n");
    printf ("%%%%For: %s\n", getenv("USER"));
    printf ("%%%%Creator: %s\n", argv[0]);
    printf ("%%%%Title: %s\n", argv[i]); 
    time (&now);
    printf ("%%%%CreationDate: %s", ctime (&now));
    printf ("%%%%Pages: (atend)\n");
    printf ("%%%%DocumentFonts: %s\n", font);
    if (land == TRUE) {
      printf ("90 rotate\n");
      printf ("0 -11 72 mul translate\n");
      printf ("/s{show}bind def\n");
      printf ("/S{copypage erasepage}bind def\n");
    }
    else{
      printf ("/s{show}bind def\n");
      printf ("/S{showpage}bind def\n");
    }
    printf ("/m{%.0f exch moveto}bind def\n", left_margin);
    printf ("/%s findfont %d scalefont setfont\n", font, fontsize);
    printf ("%%%%EndProlog\n");
    printf ("%%%%Page: %d %d\n", page, page);
    printf ("%d m\n(", (y_pgptr - fontsize));
    while ((c = getc(stdin)) != EOF) {
      if ((col++ == 1) && (bitnet == TRUE)) 
	switch (c){
	case '1':  /* page eject */
	  c = '\014'; 
	  break;
	case ' ':  /* single spacing */
	  SPACING = 1;
	  continue;
	case '0':  /* double spacing */
	  SPACING = 2;
	  continue;
	case '-':  /* triple spacing */
	  SPACING = 3;
	  continue;
	case '+':  /* overstrike */
	  SPACING = 0;
	  continue;
	default:
	  continue;
	}
      switch (c) {
      case '\014':
	col = 1;
	printf (")s ");
	printf ("S\n");
	page++;
	/* reset y_pgptr */
	y_pgptr = TOP;
	printf ("%%%%Page: %d %d\n", page, page);
	printf ("%d m\n", (y_pgptr - fontsize));
	putchar('(');
	line = 1;
	continue;
      case '\\':
	putchar ('\\');
	putchar ('\\');
	continue;
      case '(':
	putchar ('\\');
	putchar ('(');
	continue;
      case ')':
	putchar ('\\');
	putchar (')');
	continue;
      case '\t':
	for (; col % 8; col++)
	  putchar(' ');
	putchar(' ');
	continue;
      case '\n':
	col = 1;
	printf (")s ");
	line = line + SPACING;
	if (line > PAGELENGTH){
	  printf ("S\n");
	  page++;
	  /* reset y_pgptr */
	  y_pgptr = TOP;
	  printf ("%%%%Page: %d %d\n", page, page);
	  printf ("%d m\n", (y_pgptr - fontsize));
	  line = 1;
	}
	else {
	  y_pgptr= TOP - (line * fontsize);
	  printf ("%d m\n", y_pgptr);
	}
	putchar('(');
	continue;
      default:
	putchar (c);
      }
    }
    if (col)
      printf (")s ");
    else
      printf (") ");
    if (!col && line == 1)
      page--;
    else
      printf ("S");
    printf ("\n%%%%Trailer\n");
    printf ("%%%%Pages: %d\n", page);
  }
  /* if output was to a specified file, then rename the 
     temp file to the ouput file */
  if (temp_outfile[0] != NULL){
    unlink(outfile);
    _rename(temp_outfile, outfile);
    unlink(temp_outfile);
  }
}

get_env_args(env_name)
     char *env_name;
     /* get and set options from the evironment variable with the 
      ** same name as the program. eg If the program is called as 
      ** txt2ps the environment variable txt2ps will be checked,
      ** however, if the txt2landps is linked to this same code
      ** and called, the env var txt2landps will be checked.
      */
{
  char *strtok();
  int argc = 1;
  char *argv[20];
  char *tokptr, *strptr;
  
  argv[0] = "getenv";       /* dummy name */
  strptr = getenv(env_name);
  
  if (strptr != (char *) 0) { 
    while ( (tokptr = strtok (strptr, " \t")) != (char *) 0) {
      argv[argc++] = tokptr;
      strptr = (char *) 0;
    }
    get_args(argc, argv);
  }
}


int get_args(argc, argv)
     /* get and set command line arguments. also open output file
      ** if neccesary. Return the index of the first argv that is 
      ** not an option.
      */
     int argc;
     char *argv[];
{
  extern char *optarg;
  extern int  optind;
  
  register c;
  
  while ( (c = getopt (argc, argv, "t:m:lbo:p:f:s:z:")) != EOF )
    {
      switch (c) {
      case 't':
	top_margin = atof(optarg) * 72;
	break;
      case 'm':
	left_margin = atof(optarg) * 72;
	break;
      case 'l':
	land = TRUE;
	TOP = 774;
	left_margin = 18;
	PAGELENGTH = 57;
	fontsize = 8;
	break;
      case 'b':
	bitnet = TRUE;
	break;
      case 'o':
	strcpy (outfile, optarg);
	tmpnam (temp_outfile);
	if (freopen(temp_outfile, "w", stdout) == (FILE *) NULL)
	  {
	    perror(optarg);
	    exit(1);
	  }
	break;
      case 'p':
	PAGELENGTH = atoi(optarg);
	break;
      case 'f':
	strcpy (font, optarg);
	break;
      case 's':
	SPACING = atof(optarg);
	break;
      case 'z':
	fontsize = atoi(optarg);
	break;
      case '?':
	fprintf(stderr, "\nusage: \ttext2ps [-lb] [-o output] [-p page_length] \n\t[-f fontname] [-z fontsize] [-s line_spacing] \n\t[-m left_margin] [-t top_margin] [filename ...] \n\n");
	exit(1);
      default:
	fprintf(stderr, "Unrecoverable error.\n");
	exit(1);
      }
    }
  return (optind);
}
---------- cut here ---------
 
I hope you will find this program useful!.

I am currently working to modify this program to print out two
tall-orientation columns of 6pt Courier (or something along those
lines) so to print out ftp archive lists or things along that line.

--
tony foiani  (afoiani@nmsu.edu)         "And remember...don't lose your
             (mcsajf@nmsuvm1.bitnet)     head..." -The Kurgan, HIGHLANDER
Technical Details
Message-ID: <AFOIANI.89Jun23124153@dante.nmsu.EDU>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!bionet!ames!hc!lanl!opus!afoiani

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!bionet!ames!hc!lanl!opus!afoiani
From: afoiani@dante.nmsu.EDU (Anthony Foiani)
Newsgroups: comp.lang.postscript
Subject: txt2ps source
Message-ID: <AFOIANI.89Jun23124153@dante.nmsu.EDU>
Date: 23 Jun 89 18:41:53 GMT
Sender: news@nmsu.edu
Distribution: comp
Organization: New Mexico State University, Las Cruces, NM
Lines: 327