Unix: How to access your printer through a terminal such as telnet or SSH, e.g. with Mutt

Here's an old message I posted once that explains how to print through a terminal, by using ASCII escape sequences.


Date: Thu, 22 Jul 1999 01:41:51 +0200 
From: Christian Stigen Larsen <chrisl@xxxx.xxxx.no> 
To: mutt-users@mutt.org 
Subject: Printing through telnet to Windows desktop printer 
Mail-Followup-To: mutt-users@mutt.org 
X-Mailer: Mutt 0.95.6i 
Precedence: bulk 


This is probably old hat to most of you, but I didn't know 
about this nifty trick myself and discovered it worked very 
well with mutt. 


Problem: You ssh/telnet from a Windows machine and use Mutt 
         for reading mail. You want to easily print your 
         mail on your local Windows desktop printer. 


Solution: ANSI supports printing, if you use e.g. SecureCRT 
          for ssh connection, you can have SecureCRT trap 
          print-sequences and send them to your local 
          printer. And if you use a2ps for formatting, it 
          will look good too (and you can add "page 1 of 4" 
          etc.) 


This is how: 


          We found a small code-snippet in the (ugh) Pine 
          contribution directory. We compiled it simply 
          with "cc ansiprt.c -o lpransi". 


          In the .muttrc file you must add something like 


             set print_cmd="a2ps <options> | lpransi" 


         or if you don't want postscript, simply: 


             set print_cmd="cat - | lpransi" 


         (do we need the "cat -", or is print_cmd="lpransi" enough?) 
         This worked perfectly with our local printers. Note 
         that you can use this (probably) on any system, but 
         we only tested on Windows (probably depends on your 
         telnet/ssh client, printer settings, and so on). 


This is the file ansiprt.c that we found is attached. 


Apologies to those who are grimacing right because this was completely 
mandatory to all people that use Mutt. :-) 


Cheers! 



-- 
Christian Stigen Larsen -- http://www.sublevel3.org
    chrisl@xxxx.xxxx.no    http://www.stud.ntnu.no/~chrisl/

/*
 * ansiprt.c
 *
 * Simple filter to wrap ANSI media copy escape sequences around 
 * text on stdin.  Writes /dev/tty to get around things that might be
 * trapping stdout.  This is actually a feature because it was written
 * to be used with pine's personal print option set up to take "enscript"
 * output and send it displayward to be captured/printed to a postscript 
 * device.  Pine, of course, uses popen() to invoke the personal print
 * command, and interprets stdout as diagnostic messages from the command.
 *
 * Michael Seibel, mikes@cac.washington.edu
 *
 * 21 Apr 92
 *
 */
#include <stdio.h>
#include <sys/file.h>
#define	BUFSIZ	8192
main(argc, argv)
int  argc;
char **argv;
{
    char c[BUFSIZ];
    int  n, d;
    int  ctrld = 0;
    if(argc > 1){
        n = 0;
	while(argc > ++n){
	    if(argv[n][0] == '-'){
		switch(argv[n][1]){
		  case 'd':
		    ctrld++;
		    break;
		  default :
		    fprintf(stderr,"unknown option: %c\n", argv[n][1]);
		    break;
		}
	    }
        }
    }
    if((d=open("/dev/tty",O_WRONLY)) < 0){
        perror("/dev/tty");
	exit(1);
    }
    write(d,"\033[5i", 4);
    while((n=read(0, c, BUFSIZ)) > 0)
	write(d, c, n);
    if(ctrld)
	write(d, "\004", 1);
    write(d,"\033[4i", 4);
    close(d);
}