[comp.lang.pascal] Stderr from Turbo Pascal

mcastle@mcs213c.cs.umr.edu (Mike Castle {Nexus}) (05/08/91)

Sorry for this repost, but News was down for 5 days here, and I didn't see 
any responses to my original post.  If anyone has any answers, I'd appreciate
it.  E-mail would be nice as I'm not sure I'll get a chance to read news again
before I leave school for the summer.  Don't bother responding after may 10th,
I'll be gone for a while  :->

In article <2712@umriscc.isc.umr.edu> mcastle@mcs213k.cs.umr.edu (Mike Castle {Nexus}) writes:
>
>I know how to access Stdin and Stdout from TP, but does anyone know how to
>access Stderr??  I've got a project I'd like to work on in Pascal, but it
>looks like I might have to use C to get the Stderr capabilities.  :-/
>
>Tanx,
>Mike
-- 
Mike Castle (Nexus)  s087891@umrvma.umr.edu  or  mcastle@mcs213k.cs.umr.edu

Feel lonely?  Want someone to send you e-mail?  Just post to *.test with a 
Reply-To: field, and watch your mailbox explode!!

dave@tygra.Michigan.COM (David Conrad) (05/11/91)

   In response to the recent question in comp.lang.pascal as to how to
write to stderr (standard error) from Turbo Pascal, here is a solution.
This unit is a text file device driver.  You can assign your own text
variable to stderr and open it for writing with either Rewrite or Append,
but there is a variable already assigned and opened by the unit's initial-
ization section called 'stderr'.  Simply Write or Writeln to it.

   (Note that it is not necessary to open or close the actual file handle
since DOS provides the stdin, stdout, stderr, stdprn and stdaux file
handles to the program already opened.  Thus Rewrite, Append and Close
just set the TP-specific fields of the textrec.  Also note however that if
stderr is closed then it is marked 'fmClosed' and any further attempts to
write to it produce a 'File not opened for writing' error.)

   If you aren't familiar with the standard error device, a little back-
ground is in order.  When you Write or Writeln with either no text file or
with the 'output' variable built into Pascal, your output goes to a device
known as CON or standard output.  This device can be redirected to a file,
and all output which would normally be seen on the screen is sent to that
file instead.  (Remember, when you use the CRT unit TP writes directly to
the screen memory or to the BIOS instead of to DOS, so DOS doesn't get a
chance to redirect the output to a file.)  Here is an example:

   program contest;
   begin
     writeln ('Standard output.');
   end.

Either compile this with the command line compiler (TPC or TPCX), or
compile to disk in the IDE and return to DOS.  Then try running it by
typing "contest" and "contest >contest.txt".  In the second example the
string 'Standard output.' is placed in the file contest.txt.  Now try
the following program:

   program errtest;
   uses stderror;
   begin
     writeln ('Standard output.');
     writeln (stderr,'Standard error.');
   end.

Compile as before and run with "errtest" and "errtest >errtest.txt".  In
the former both lines are printed on the screen, but in the latter the
'Standard output.' line is sent to the file errtest.txt but the 'Standard
error.' line is still shown on the screen!  The purpose behind this, in
case the name standard error didn't give it away, is to have the error
messages still go to the screen where the user can see them even if the
normal output is being logged to a file.

   (By the way, when using the CRT unit it is possible to have the normal
output still display via DOS (so it can be redirected) by placing:

   assign (output,'');
   rewrite (output);

at the beginning of your program.)

   Unit StdError Copyright 1991 by David Conrad.  It may be distributed
freely as long as it is not modified, and derivative works may be made as
long as it is indicated that they are derived from this work.  No repre-
sentation is made as to the merchantability or fitness for a particular
use of this software.  It is provided "as is" without express or implied
warranty of any kind.

BEGIN--cut here--cut here
{$D-,E-,F+,L-,N-,O-,R-,S+}
Unit StdError;

(* Copyright 1991 David R. Conrad, dave@michigan.com *)

Interface

var
  stderr : text;

Procedure AssignErr (var f : text);

Implementation

Uses Dos;

Function ErrOut (var f : textrec) : integer;
var
  r : registers;
begin
  if f.BufPos = 0 then
    begin
      ErrOut := 0;
      exit;
    end;
  r.ah := $40;  (* write to file or device via handle *)
  r.bx := f.Handle;  (* the handle for stderr *)
  r.cx := f.BufPos;  (* write all characters in buffer *)
  f.BufPos := 0;
  r.ds := Seg(f.BufPtr^);
  r.dx := Ofs(f.BufPtr^);  (* segment and offset of buffer *)
  msdos (r);  (* call DOS to output char *)
  ErrOut := 0;
end;

Function ErrClose (var f : textrec) : integer;
begin
  f.Mode := fmClosed;
  ErrClose := 0;
end;

Function ErrOpen (var f : textrec) : integer;
begin
  if f.mode = fmInput then
    begin
      ErrOpen := 5;  (* File Access Denied, stderr is write only *)
      exit;
    end;
  f.Handle    := 2;  (* stderr file handle *)
  f.Mode      := fmOutput;
  f.InOutFunc := @ErrOut;
  f.FlushFunc := @ErrOut;
  f.CloseFunc := @ErrClose;
  ErrOpen := 0;
end;

Procedure AssignErr (var f : text);
const
  errname : array[0..8] of char = '\dev\con'#0;
begin
  with textrec(f) do
    begin
      Mode     := fmClosed;
      BufSize  := sizeof(Buffer);
      BufPos   := 0;
      BufEnd   := 0;
      BufPtr   := @Buffer;
      OpenFunc := @ErrOpen;
      move (errname, Name, sizeof(errname));
    end;
end;

Begin
  AssignErr (stderr);
  rewrite (stderr);
End.
END--cut here--cut here

David R. Conrad   | "A dream unthreatened by the morning light, could blow this
dave@michigan.com | soul right through the roof of the night." -- Pink Floyd
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave@Michigan.COM

Kai_Henningsen@ms.maus.de (Kai Henningsen) (05/16/91)

dave @ michigan.com schrieb am 11.05.1991, 06:38

da>   In response to the recent question in comp.lang.pascal as to how to
da>write to stderr (standard error) from Turbo Pascal, here is a solution.
da>This unit is a text file device driver.  You can assign your own text

Sorry, but I can't resist. Try the following:

program Demo;
uses
  Dos;
var
  StdErr: Text;
begin
  Assign(StdErr, '');
  Rewrite(StdErr);
  TextRec(StdErr).Handle:=2;

  Writeln('Standard output');
  Writeln(StdErr, 'Standard error');

  Close(StdErr);
end.
--
Kai Henningsen  Internet: kh@ms.maus.de
Muenster         UUCP: any_backbone_that_knows_domains!ms.maus.de!kh
Germany         Fido: kh%maus ms, 2:242/2.6 or Kai Henningsen, 2:242/2.244

dave@tygra.Michigan.COM (David Conrad) (05/27/91)

In article <15667@ms.maus.de> Kai_Henningsen@ms.maus.de (Kai Henningsen) writes:
>
>dave @ michigan.com schrieb am 11.05.1991, 06:38
>
>da>   In response to the recent question in comp.lang.pascal as to how to
>da>write to stderr (standard error) from Turbo Pascal, here is a solution.
>da>This unit is a text file device driver.  You can assign your own text
>
>Sorry, but I can't resist. Try the following:
>
>program Demo;
>uses
>  Dos;
>var
>  StdErr: Text;
>begin
>  Assign(StdErr, '');
>  Rewrite(StdErr);
>  TextRec(StdErr).Handle:=2;
>
>  Writeln('Standard output');
>  Writeln(StdErr, 'Standard error');
>
>  Close(StdErr);
>end.
>--
>Kai Henningsen  Internet: kh@ms.maus.de

What can I say, other than "Duh!"  :-)
Dave Conrad
dave@michigan.com
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave@Michigan.COM

ts@uwasa.fi (Timo Salmi) (05/28/91)

In article <GUNNARTH.91May28085015@ulrik.uio.no> gunnarth@ulrik.uio.no (Gunnar Thorvaldsen) writes:

>

Yes, I think the quote sums the issue very neatly :-).  (Couldn't
resist, so flame away, if you wish to). 

...................................................................
Prof. Timo Salmi
Moderating at garbo.uwasa.fi anonymous ftp archives 128.214.12.37
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun