printer.pp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 (const f: RawByteString);
  56. var
  57. i: pid_t;
  58. p,pp : ppchar;
  59. begin
  60. if lpr='' then
  61. exit;
  62. i:=fpFork;
  63. if i<0 then
  64. exit; { No printing was done. We leave the file where it is.}
  65. if i=0 then
  66. begin
  67. { We're in the child }
  68. getmem(p,12);
  69. if p=nil then
  70. halt(127);
  71. pp:=p;
  72. pp^:=@lpr[1];
  73. inc(pp);
  74. pp^:=@f[1];
  75. inc(pp);
  76. pp^:=nil;
  77. fpExecve(lpr,p,envp);
  78. { In trouble here ! }
  79. fpexit(127)
  80. end
  81. else
  82. begin
  83. { We're in the parent. }
  84. if waitprocess(i)<>0 then
  85. exit;
  86. { Erase the file }
  87. fpUnlink(f);
  88. end;
  89. end;
  90. Procedure OpenLstPipe ( Var F : Text);
  91. var
  92. r: rawbytestring;
  93. begin
  94. {$ifdef FPC_ANSI_TEXTFILEREC}
  95. { encoding is already correct }
  96. r:=textrec(f).name;
  97. SetCodePage(r,DefaultFileSystemCodePage,false);
  98. {$else}
  99. r:=ToSingleByteFileSystemEncodedFileName(textrec(f).name);
  100. {$endif}
  101. POpen (f,r,'W');
  102. end;
  103. Procedure OpenLstFile ( Var F : Text);
  104. var
  105. i : cint;
  106. r: rawbytestring;
  107. begin
  108. {$IFDEF PRINTERDEBUG}
  109. writeln ('Printer : In OpenLstFile');
  110. {$ENDIF}
  111. If textrec(f).mode <> fmoutput then
  112. exit;
  113. textrec(f).userdata[15]:=0; { set Zero length flag }
  114. {$ifdef FPC_ANSI_TEXTFILEREC}
  115. { encoding is already correct }
  116. r:=textrec(f).name;
  117. SetCodePage(r,DefaultFileSystemCodePage,false);
  118. {$else}
  119. r:=ToSingleByteFileSystemEncodedFileName(textrec(f).name);
  120. {$endif}
  121. repeat
  122. i:=fpOpen(pansichar(r),(Open_WrOnly or Open_Creat), 438);
  123. until (i<>-1) or (fpgeterrno<>ESysEINTR);
  124. if i<0 then
  125. textrec(f).mode:=fmclosed
  126. else
  127. textrec(f).handle:=i;
  128. end;
  129. Procedure CloseLstFile ( Var F : Text);
  130. var
  131. res: cint;
  132. begin
  133. {$IFDEF PRINTERDEBUG}
  134. writeln ('Printer : In CloseLstFile');
  135. {$ENDIF}
  136. repeat
  137. res:=fpclose (textrec(f).handle);
  138. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  139. { In case length is zero, don't print : lpr would give an error }
  140. if (textrec(f).userdata[15]=0) and (textrec(f).userdata[16]=P_TOF) then
  141. begin
  142. {$IFDEF FPC_ANSI_TEXTFILEREC}
  143. fpUnlink(pansichar(@textrec(f).name));
  144. {$ELSE}
  145. fpUnlink(ToSingleByteFileSystemEncodedFileName(textrec(f).name));
  146. {$ENDIF}
  147. exit
  148. end;
  149. { Non empty : needs printing ? }
  150. if (textrec(f).userdata[16]=P_TOF) then
  151. {$IFDEF FPC_ANSI_TEXTFILEREC}
  152. PrintAndDelete (textrec(f).name);
  153. {$ELSE}
  154. PrintAndDelete (ToSingleByteFileSystemEncodedFileName(textrec(f).name));
  155. {$ENDIF}
  156. textrec(f).mode:=fmclosed
  157. end;
  158. Procedure InOutLstFile ( Var F : text);
  159. var
  160. res: cint;
  161. begin
  162. {$IFDEF PRINTERDEBUG}
  163. writeln ('Printer : In InOutLstFile');
  164. {$ENDIF}
  165. If textrec(f).mode<>fmoutput then
  166. exit;
  167. if textrec(f).bufpos<>0 then
  168. textrec(f).userdata[15]:=1; { Set it is not empty. Important when closing !!}
  169. repeat
  170. res:=fpwrite(textrec(f).handle,textrec(f).bufptr^,textrec(f).bufpos);
  171. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  172. textrec(f).bufpos:=0;
  173. end;
  174. function SubstPidInName (const S: string): string;
  175. var
  176. i : longint;
  177. temp : string[8];
  178. begin
  179. i:=pos('PID',s);
  180. if i=0 then
  181. SubstPidInName := S
  182. else
  183. begin
  184. Str (fpGetPid, Temp);
  185. SubstPidInName := Copy (S, 1, Pred (I)) + Temp +
  186. Copy (S, I + 3, Length (S) - I - 2);
  187. {$IFDEF PRINTERDEBUG}
  188. writeln ('Print : Filename became : ', Result);
  189. {$ENDIF}
  190. end;
  191. end;
  192. Procedure AssignLst ( Var F : text; ToFile : string);
  193. begin
  194. {$IFDEF PRINTERDEBUG}
  195. writeln ('Printer : In AssignLst');
  196. {$ENDIF}
  197. If ToFile='' then
  198. exit;
  199. textrec(f).bufptr:=@textrec(f).buffer;
  200. textrec(f).bufsize:=128;
  201. ToFile := SubstPidInName (ToFile);
  202. if ToFile[1]='|' then
  203. begin
  204. Assign(f,Copy(ToFile,2,255));
  205. textrec(f).userdata[16]:=P_TOP;
  206. textrec(f).OpenFunc:=@OpenLstPipe;
  207. end
  208. else
  209. begin
  210. if Tofile[Length(ToFile)]='|' then
  211. begin
  212. Assign(f,Copy(ToFile,1,length(Tofile)-1));
  213. textrec(f).userdata[16]:=P_TOFNP;
  214. end
  215. else
  216. begin
  217. Assign(f,ToFile);
  218. textrec(f).userdata[16]:=P_TOF;
  219. end;
  220. textrec(f).OpenFunc:=@OpenLstFile;
  221. textrec(f).CloseFunc:=@CloseLstFile;
  222. textrec(f).InoutFunc:=@InoutLstFile;
  223. textrec(f).FlushFunc:=@InoutLstFile;
  224. end;
  225. end;
  226. begin
  227. InitPrinter (SubstPidInName ('/tmp/PID.lst'));
  228. SetPrinterExit;
  229. Lpr := '/usr/bin/lpr';
  230. end.