home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / lib / gcc-lib / djgpp / 2.952 / units / printer.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  4.9 KB  |  171 lines

  1. {
  2. BP compatible printer unit with extensions
  3.  
  4. Copyright (C) 1998-2001 Free Software Foundation, Inc.
  5.  
  6. Author: Frank Heckenbach <frank@pascal.gnu.de>
  7.  
  8. This file is part of GNU Pascal.
  9.  
  10. GNU Pascal is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2, or (at your option)
  13. any later version.
  14.  
  15. GNU Pascal is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with GNU Pascal; see the file COPYING. If not, write to the
  22. Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  23. 02111-1307, USA.
  24.  
  25. As a special exception, if you link this file with files compiled
  26. with a GNU compiler to produce an executable, this does not cause
  27. the resulting executable to be covered by the GNU General Public
  28. License. This exception does not however invalidate any other
  29. reasons why the executable file might be covered by the GNU General
  30. Public License.
  31. }
  32.  
  33. {$gnu-pascal,B-,I-}
  34. {$if __GPC_RELEASE__ < 20000412}
  35. {$error This unit requires GPC release 20000412 or newer.}
  36. {$endif}
  37.  
  38. unit Printer;
  39.  
  40. interface
  41.  
  42. {$ifdef __OS_DOS__}
  43.  
  44. { Dos-like systems: writing to a printer device }
  45.  
  46. uses GPC;
  47.  
  48. var
  49.   { The file name to write printer output into }
  50.   PrinterDeviceName : ^String = @'prn';
  51.  
  52. {$else}
  53.  
  54. { Unix-like systems: printing via a printer program }
  55.  
  56. uses GPC, Pipe;
  57.  
  58. const
  59.   EPrinterRead = 610; { printer can only be opened for writing }
  60.  
  61. var
  62.   { The file name of the printer program. If it contains a '/', it
  63.     will be taken as a complete path, otherwise the file name will
  64.     be searched for in the PATH with FSearchExecutable. }
  65.   PrinterCommand : ^String = @'lpr';
  66.  
  67.   { Optional command line parameters for the printer program.
  68.     Ignored when nil. }
  69.   PrinterArguments : ^TPStrings = nil;
  70.  
  71.   { How to deal with the printer spooler after the printer pipe is
  72.     closed, cf. the Pipe unit. }
  73.   PrinterPipeSignal  : Integer = 0;
  74.   PrinterPipeSeconds : Integer = 0;
  75.   PrinterPipeWait    : Boolean = True;
  76.  
  77. {$endif}
  78.  
  79. { Text file opened to default printer }
  80. var
  81.   Lst : Text;
  82.  
  83. { Assign a file to the printer. Lst will be assigned to the default
  84.   printer at program start, but other files can be assigned to the
  85.   same or other printers (possibly after changing the variables
  86.   above). SpoolerOutput, if not null, will be redirected from the
  87.   printer spooler's standard output and error. If you use this, note
  88.   that a deadlock might arise when trying to write data to the
  89.   spooler while its output is not being read, though this seems
  90.   quite unlikely, since most printer spoolers don't write so much
  91.   output that could fill a pipe. Under Dos, where no spooler is
  92.   involved, SpoolerOutput, if not null, will be reset to an empty
  93.   file for compatibility. }
  94. procedure AssignPrinter (var f : AnyFile; var SpoolerOutput : AnyFile);
  95.  
  96. implementation
  97.  
  98. {$ifdef __OS_DOS__}
  99.  
  100. procedure AssignPrinter (var f : AnyFile; var SpoolerOutput : AnyFile);
  101. begin
  102.   Assign (f, PrinterDeviceName^);
  103.   if @SpoolerOutput <> nil then
  104.     begin
  105.       Unbind (SpoolerOutput);
  106.       Rewrite (SpoolerOutput);
  107.       Reset (SpoolerOutput)
  108.     end
  109. end;
  110.  
  111. {$else}
  112.  
  113. type
  114.   TPrinterTFDDData = record
  115.     f, SpoolerOutput : PAnyFile;
  116.   end;
  117.  
  118. procedure OpenPrinter (var PrivateData; Mode : TOpenMode);
  119. var Dummy : Pointer;
  120. begin
  121.   Dummy := @PrivateData;
  122.   if not (Mode in [foRewrite, foAppend]) then IOError (EPrinterRead)
  123. end;
  124.  
  125. { Be very lazy: don't open the pipe until data are written to it -- not
  126.   as soon as the file is opened because that happens already in the
  127.   initialization of this unit (BP compatibility) }
  128. function WritePrinter (var PrivateData; const Buffer; Size : SizeType) : SizeType;
  129. var
  130.   Data : TPrinterTFDDData absolute PrivateData;
  131.   CharBuf : array [1 .. Size] of Char absolute Buffer;
  132.   Process : PPipeProcess;
  133. begin
  134.   WritePrinter := 0;
  135.   Pipe (Data.f^, Data.SpoolerOutput^, Data.SpoolerOutput^, PrinterCommand^, PrinterArguments^, GetCEnvironment, Process, nil); { this also makes sure this function won't be called again for this file }
  136.   if InOutRes <> 0 then Exit;
  137.   Process^.Signal  := PrinterPipeSignal;
  138.   Process^.Seconds := PrinterPipeSeconds;
  139.   Process^.Wait    := PrinterPipeWait;
  140.   Write (Data.f^, CharBuf);
  141.   if InOutRes = 0 then WritePrinter := Size
  142. end;
  143.  
  144. procedure AssignPrinter (var f : AnyFile; var SpoolerOutput : AnyFile);
  145. var p : ^TPrinterTFDDData;
  146. begin
  147.   if @SpoolerOutput <> nil then
  148.     begin
  149.       Unbind (SpoolerOutput);
  150.       Rewrite (SpoolerOutput);
  151.       Reset (SpoolerOutput)
  152.     end;
  153.   New (p);
  154.   p^.f := @f;
  155.   p^.SpoolerOutput := @SpoolerOutput;
  156.   AssignTFDD (f, OpenPrinter, nil, nil, nil, WritePrinter, nil, nil, nil, p)
  157. end;
  158.  
  159. {$endif}
  160.  
  161. to begin do
  162. begin
  163.   AssignPrinter (Lst, null);
  164.   Rewrite (Lst)
  165. end;
  166.  
  167. to end do
  168.   Close (Lst);
  169.  
  170. end.
  171.