printer.pp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. member of the Free Pascal development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { Change Log
  13. ----------
  14. Started by Michael Van Canneyt, 1996
  15. ([email protected])
  16. Current version is 0.9
  17. Date Version Who Comments
  18. 1999-2000 by 0.8 Michael Initial implementation
  19. 11/97 0.9 Peter Vreman <[email protected]>
  20. Unit now depends on the
  21. linux unit only.
  22. Cleaned up code.
  23. ---------------------------------------------------------------------}
  24. Unit printer;
  25. Interface
  26. {.$DEFINE PRINTERDEBUG}
  27. {$I printerh.inc}
  28. Procedure AssignLst ( Var F : text; ToFile : string);
  29. {
  30. Assigns to F a printing device. ToFile is a string with the following form:
  31. '|filename options' : This sets up a pipe with the program filename,
  32. with the given options
  33. 'filename' : Prints to file filename. Filename can contain the string 'PID'
  34. (No Quotes), which will be replaced by the PID of your program.
  35. When closing lst, the file will be sent to lpr and deleted.
  36. (lpr should be in PATH)
  37. 'filename|' Idem as previous, only the file is NOT sent to lpr, nor is it
  38. deleted.
  39. (useful for opening /dev/printer or for later printing)
  40. Lst is set up using '/tmp/PID.lst'. You can change this behaviour at
  41. compile time, setting the DefFile constant.
  42. }
  43. Implementation
  44. Uses Unix,BaseUnix,Strings;
  45. {$I printer.inc}
  46. {
  47. include definition of textrec
  48. }
  49. {$i textrec.inc}
  50. Const
  51. P_TOF = 1; { Print to file }
  52. P_TOFNP = 2; { Print to File, don't spool }
  53. P_TOP = 3; { Print to Pipe }
  54. Var
  55. Lpr : String[255]; { Contains path to lpr binary, including null char }
  56. Procedure PrintAndDelete (f:string);
  57. var
  58. i,j : longint;
  59. p,pp : ppchar;
  60. begin
  61. f:=f+#0;
  62. if lpr='' then
  63. exit;
  64. i:=fpFork;
  65. if i<0 then
  66. exit; { No printing was done. We leave the file where it is.}
  67. if i=0 then
  68. begin
  69. { We're in the child }
  70. getmem(p,12);
  71. if p=nil then
  72. halt(127);
  73. pp:=p;
  74. pp^:=@lpr[1];
  75. inc(pp);
  76. pp^:=@f[1];
  77. inc(pp);
  78. pp^:=nil;
  79. fpExecve(lpr,p,envp);
  80. { In trouble here ! }
  81. halt(128)
  82. end
  83. else
  84. begin
  85. { We're in the parent. }
  86. fpwaitpid (i,@j,0);
  87. if j<>0 then
  88. exit;
  89. { Erase the file }
  90. fpUnlink(f);
  91. end;
  92. end;
  93. Procedure OpenLstPipe ( Var F : Text);
  94. begin
  95. POpen (f,StrPas(textrec(f).name),'W');
  96. end;
  97. Procedure OpenLstFile ( Var F : Text);
  98. var
  99. i : longint;
  100. begin
  101. {$IFDEF PRINTERDEBUG}
  102. writeln ('Printer : In OpenLstFile');
  103. {$ENDIF}
  104. If textrec(f).mode <> fmoutput then
  105. exit;
  106. textrec(f).userdata[15]:=0; { set Zero length flag }
  107. i:=fpOpen(StrPas(textrec(f).name),(Open_WrOnly or Open_Creat), 438);
  108. if i<0 then
  109. textrec(f).mode:=fmclosed
  110. else
  111. textrec(f).handle:=i;
  112. end;
  113. Procedure CloseLstFile ( Var F : Text);
  114. begin
  115. {$IFDEF PRINTERDEBUG}
  116. writeln ('Printer : In CloseLstFile');
  117. {$ENDIF}
  118. fpclose (textrec(f).handle);
  119. { In case length is zero, don't print : lpr would give an error }
  120. if (textrec(f).userdata[15]=0) and (textrec(f).userdata[16]=P_TOF) then
  121. begin
  122. fpUnlink(StrPas(textrec(f).name));
  123. exit
  124. end;
  125. { Non empty : needs printing ? }
  126. if (textrec(f).userdata[16]=P_TOF) then
  127. PrintAndDelete (strpas(textrec(f).name));
  128. textrec(f).mode:=fmclosed
  129. end;
  130. Procedure InOutLstFile ( Var F : text);
  131. begin
  132. {$IFDEF PRINTERDEBUG}
  133. writeln ('Printer : In InOutLstFile');
  134. {$ENDIF}
  135. If textrec(f).mode<>fmoutput then
  136. exit;
  137. if textrec(f).bufpos<>0 then
  138. textrec(f).userdata[15]:=1; { Set it is not empty. Important when closing !!}
  139. fpwrite(textrec(f).handle,textrec(f).bufptr^,textrec(f).bufpos);
  140. textrec(f).bufpos:=0;
  141. end;
  142. function SubstPidInName (const S: string): string;
  143. var
  144. i : longint;
  145. temp : string[8];
  146. begin
  147. i:=pos('PID',s);
  148. if i=0 then
  149. SubstPidInName := S
  150. else
  151. begin
  152. Str (fpGetPid, Temp);
  153. SubstPidInName := Copy (S, 1, Pred (I)) + Temp +
  154. Copy (S, I + 3, Length (S) - I - 2);
  155. {$IFDEF PRINTERDEBUG}
  156. writeln ('Print : Filename became : ', Result);
  157. {$ENDIF}
  158. end;
  159. end;
  160. Procedure AssignLst ( Var F : text; ToFile : string);
  161. begin
  162. {$IFDEF PRINTERDEBUG}
  163. writeln ('Printer : In AssignLst');
  164. {$ENDIF}
  165. If ToFile='' then
  166. exit;
  167. textrec(f).bufptr:=@textrec(f).buffer;
  168. textrec(f).bufsize:=128;
  169. ToFile := SubstPidInName (ToFile);
  170. if ToFile[1]='|' then
  171. begin
  172. Assign(f,Copy(ToFile,2,255));
  173. textrec(f).userdata[16]:=P_TOP;
  174. textrec(f).OpenFunc:=@OpenLstPipe;
  175. end
  176. else
  177. begin
  178. if Tofile[Length(ToFile)]='|' then
  179. begin
  180. Assign(f,Copy(ToFile,1,length(Tofile)-1));
  181. textrec(f).userdata[16]:=P_TOFNP;
  182. end
  183. else
  184. begin
  185. Assign(f,ToFile);
  186. textrec(f).userdata[16]:=P_TOF;
  187. end;
  188. textrec(f).OpenFunc:=@OpenLstFile;
  189. textrec(f).CloseFunc:=@CloseLstFile;
  190. textrec(f).InoutFunc:=@InoutLstFile;
  191. textrec(f).FlushFunc:=@InoutLstFile;
  192. end;
  193. end;
  194. begin
  195. InitPrinter (SubstPidInName ('/tmp/PID.lst'));
  196. SetPrinterExit;
  197. Lpr := '/usr/bin/lpr';
  198. end.
  199. {
  200. $Log$
  201. Revision 1.7 2004-12-05 11:21:46 hajny
  202. * common implementation of unit printer - fix for bug 3421
  203. Revision 1.6 2003/09/20 12:38:29 marco
  204. * FCL now compiles for FreeBSD with new 1.1. Now Linux.
  205. Revision 1.5 2003/09/14 20:15:01 marco
  206. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  207. Revision 1.4 2002/09/07 16:01:27 peter
  208. * old logs removed and tabs fixed
  209. }