printer.pp 5.8 KB

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