[comp.lang.pascal] Redirection in Pascal

fmbasas@athena.mit.edu (Fred M Basas) (11/08/90)

Hi!  I'm relatively new to this group, so forgive me if this topic has
been touched upon before.

Is there a way in TP 5.5 to redirect all printer output to a file on disk.
I mean this for any program, not one that I'm writing.  I'm looking for a
simple solution (i.e. no TSRs if at all possible) for this if there is one.
Either posts or e-mail is fine, and thanks in advance.

					Fred M Basas
					fmbasas@athena.mit.edu

ts@uwasa.fi (Timo Salmi) (11/10/90)

In article <1990Nov8.001233.16525@athena.mit.edu> fmbasas@athena.mit.edu (Fred M Basas) writes:
>Hi!  I'm relatively new to this group, so forgive me if this topic has
>been touched upon before.
>
>Is there a way in TP 5.5 to redirect all printer output to a file on disk.
>I mean this for any program, not one that I'm writing.  I'm looking for a
>simple solution (i.e. no TSRs if at all possible) for this if there is one.

To redirect (not only the printer, but any output) one simply uses
assign within a Turbo Pascal program.  That is what it is for. 
(Consider the meaning of the word assign.  It figures, right). 
Example:
  uses printer;
  begin
    assign (lst, 'testfile');
    rewrite (lst);
    writeln (lst, 'Hello world');
    close (lst);
  end.

But if you want to redirect printing to a file (as you obviously do)
of any program, your question has nothing to do with Turbo Pascal in
particular.  Then you need a TSR.  Such programs (e.g. 
/pc/pd2/lptx.zip) can be found on uwasa.fi archives by anonymous ftp
or mail server. 

...................................................................
Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

phys169@canterbury.ac.nz (11/14/90)

In article <1990Nov8.001233.16525@athena.mit.edu> fmbasas@athena.mit.edu (Fred M Basas) writes:
>Is there a way in TP 5.5 to redirect all printer output to a file on disk.
>I mean this for any program, not one that I'm writing.  I'm looking for a
>simple solution (i.e. no TSRs if at all possible) for this if there is one.
 
In article <1990Nov9.174737.16317@uwasa.fi>, ts@uwasa.fi (Timo Salmi) writes:
> But if you want to redirect printing to a file (as you obviously do)
> of any program, your question has nothing to do with Turbo Pascal in
> particular.  Then you need a TSR.

There are several possibilities, some not involving TSR's (but the most general
do). The problem is that there are many ways a program can print.  If you want
to redirect printing of *your own* programs, I suggest you use something like:
  assign(Lst,GetEnv('PRINTER'));
  rewrite(Lst);

But this doesn't help you with the multitude of programs out there which use,
in descending order of "niceness" the following methods...

(1) Write to the standard printer, handle 4. Like the standard input, output,
    and error handles, these are "pre-openned" for you, and like them, output
    this way is a bit slower, and few programs/compilers use it.  You can have
    a program (Turbo Pascal, if you like, that closes handle 4 and re-opens it
    for any file you like; the change will remain after your program ends.

(2) Open and output to the device 'PRN' via standard DOS. The device driver
    supplied by DOS always sends output to LPT1. The way they wrote the code,
    you can't simply poke a different number into the device driver in RAM; you
    must supply a new driver in CONFIG.SYS - not too difficult, but it doesn't
    help if the programs use any of the remaining printing methods, and making
    it go to a file (instead of another printer) is not that easy.

(3) Open and output to the device 'LPT1' via standard DOS. This uses the int 17h
    (BIOS) call to output to the first printer; you can capture the int 17h
    vector, but making it write to a disk file is difficult because you could
    be inside a DOS system call at the time int 17h is called, making another
    call messy (this is one of the nasty concepts that put people off TSR's).

(4) Call int 17h directly.  This saves a lot of time, especially when printing
    image graphics (lots of dots = lots of system calls otherwise). As I said,
    you could capture int 17h yourself and do something with it, but for the
    weedy problem of system calls within system calls.

(5) Output directly to the printer controller card directly. The program would
    get the port address from a location in memory (a table of 4 2-byte entries
    starting at location 40:8 (hex) contains the port addresses for LPT1, LPT2,
    etc).  You can re-direct printing to a different parallel port *very*
    easily for any printing method they choose to use, by swapping entries in
    this table, but that doesn't help you get it to a disk file.

As T.S. has pointed out, there are ready-made utilities that redirect the
printer output for you. If you want to write your own, redirecting printer
output for programs using method (1) is easy; (2) to (4) is difficult, and
(5) is downright impossible - unless you are content to redirect output to a
different port rather than any filename.

If you are happy enough to tackle (1) to (4), with bufferring in RAM until it
is "safe" to print, I could help you with a program. If there is enough demand,
I'll post the source.

Mark Aitchison, Physics, University of Canterbury, New Zealand.