printer.pp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. {$IFDEF FPC_UNICODE_RTL}
  94. POpen (f,textrec(f).name,'W');
  95. {$ELSE}
  96. POpen (f,StrPas(textrec(f).name),'W');
  97. {$ENDIF}
  98. end;
  99. Procedure OpenLstFile ( Var F : Text);
  100. var
  101. i : cint;
  102. begin
  103. {$IFDEF PRINTERDEBUG}
  104. writeln ('Printer : In OpenLstFile');
  105. {$ENDIF}
  106. If textrec(f).mode <> fmoutput then
  107. exit;
  108. textrec(f).userdata[15]:=0; { set Zero length flag }
  109. repeat
  110. {$IFDEF FPC_UNICODE_RTL}
  111. i:=fpOpen(textrec(f).name,(Open_WrOnly or Open_Creat), 438);
  112. {$ELSE}
  113. i:=fpOpen(StrPas(textrec(f).name),(Open_WrOnly or Open_Creat), 438);
  114. {$ENDIF}
  115. until (i<>-1) or (fpgeterrno<>ESysEINTR);
  116. if i<0 then
  117. textrec(f).mode:=fmclosed
  118. else
  119. textrec(f).handle:=i;
  120. end;
  121. Procedure CloseLstFile ( Var F : Text);
  122. var
  123. res: cint;
  124. begin
  125. {$IFDEF PRINTERDEBUG}
  126. writeln ('Printer : In CloseLstFile');
  127. {$ENDIF}
  128. repeat
  129. res:=fpclose (textrec(f).handle);
  130. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  131. { In case length is zero, don't print : lpr would give an error }
  132. if (textrec(f).userdata[15]=0) and (textrec(f).userdata[16]=P_TOF) then
  133. begin
  134. {$IFDEF FPC_UNICODE_RTL}
  135. fpUnlink(textrec(f).name);
  136. {$ELSE}
  137. fpUnlink(StrPas(textrec(f).name));
  138. {$ENDIF}
  139. exit
  140. end;
  141. { Non empty : needs printing ? }
  142. if (textrec(f).userdata[16]=P_TOF) then
  143. {$IFDEF FPC_UNICODE_RTL}
  144. PrintAndDelete (textrec(f).name);
  145. {$ELSE}
  146. PrintAndDelete (strpas(textrec(f).name));
  147. {$ENDIF}
  148. textrec(f).mode:=fmclosed
  149. end;
  150. Procedure InOutLstFile ( Var F : text);
  151. var
  152. res: cint;
  153. begin
  154. {$IFDEF PRINTERDEBUG}
  155. writeln ('Printer : In InOutLstFile');
  156. {$ENDIF}
  157. If textrec(f).mode<>fmoutput then
  158. exit;
  159. if textrec(f).bufpos<>0 then
  160. textrec(f).userdata[15]:=1; { Set it is not empty. Important when closing !!}
  161. repeat
  162. res:=fpwrite(textrec(f).handle,textrec(f).bufptr^,textrec(f).bufpos);
  163. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  164. textrec(f).bufpos:=0;
  165. end;
  166. function SubstPidInName (const S: string): string;
  167. var
  168. i : longint;
  169. temp : string[8];
  170. begin
  171. i:=pos('PID',s);
  172. if i=0 then
  173. SubstPidInName := S
  174. else
  175. begin
  176. Str (fpGetPid, Temp);
  177. SubstPidInName := Copy (S, 1, Pred (I)) + Temp +
  178. Copy (S, I + 3, Length (S) - I - 2);
  179. {$IFDEF PRINTERDEBUG}
  180. writeln ('Print : Filename became : ', Result);
  181. {$ENDIF}
  182. end;
  183. end;
  184. Procedure AssignLst ( Var F : text; ToFile : string);
  185. begin
  186. {$IFDEF PRINTERDEBUG}
  187. writeln ('Printer : In AssignLst');
  188. {$ENDIF}
  189. If ToFile='' then
  190. exit;
  191. textrec(f).bufptr:=@textrec(f).buffer;
  192. textrec(f).bufsize:=128;
  193. ToFile := SubstPidInName (ToFile);
  194. if ToFile[1]='|' then
  195. begin
  196. Assign(f,Copy(ToFile,2,255));
  197. textrec(f).userdata[16]:=P_TOP;
  198. textrec(f).OpenFunc:=@OpenLstPipe;
  199. end
  200. else
  201. begin
  202. if Tofile[Length(ToFile)]='|' then
  203. begin
  204. Assign(f,Copy(ToFile,1,length(Tofile)-1));
  205. textrec(f).userdata[16]:=P_TOFNP;
  206. end
  207. else
  208. begin
  209. Assign(f,ToFile);
  210. textrec(f).userdata[16]:=P_TOF;
  211. end;
  212. textrec(f).OpenFunc:=@OpenLstFile;
  213. textrec(f).CloseFunc:=@CloseLstFile;
  214. textrec(f).InoutFunc:=@InoutLstFile;
  215. textrec(f).FlushFunc:=@InoutLstFile;
  216. end;
  217. end;
  218. begin
  219. InitPrinter (SubstPidInName ('/tmp/PID.lst'));
  220. SetPrinterExit;
  221. Lpr := '/usr/bin/lpr';
  222. end.