fpredir.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. {
  2. This file is part of the Free Pascal Test Suite
  3. Copyright (c) 1999-2000 by Pierre Muller
  4. Unit to redirect output and error to files
  5. Adapted from code donated to public domain by Schwartz Gabriel 20/03/1993
  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. Unit FpRedir;
  13. Interface
  14. {$R-}
  15. {$ifndef Linux}
  16. {$ifndef Unix}
  17. {$S-}
  18. {$endif}
  19. {$endif}
  20. {$ifdef Go32v2}
  21. {$define implemented}
  22. {$endif}
  23. {$ifdef OS2}
  24. {$define shell_implemented}
  25. {$endif}
  26. {$ifdef Windows}
  27. {$define implemented}
  28. {$endif}
  29. {$ifdef linux}
  30. {$define implemented}
  31. {$endif}
  32. {$ifdef BSD}
  33. {$define implemented}
  34. {$endif}
  35. {$ifdef netwlibc}
  36. {$define implemented}
  37. {$endif}
  38. {$ifdef netware_clib}
  39. {$define implemented}
  40. {$endif}
  41. {$H-}
  42. Var
  43. IOStatus : Integer;
  44. RedirErrorOut,RedirErrorIn,
  45. RedirErrorError : Integer;
  46. ExecuteResult : Word;
  47. {------------------------------------------------------------------------------}
  48. procedure InitRedir;
  49. function ExecuteRedir (Const ProgName, ComLine, RedirStdIn, RedirStdOut, RedirStdErr : String) : boolean;
  50. procedure DosExecute(ProgName, ComLine : String);
  51. function MaybeQuoted(const s:string):string;
  52. function ChangeRedirOut(Const Redir : String; AppendToFile : Boolean) : Boolean;
  53. procedure RestoreRedirOut;
  54. procedure DisableRedirOut;
  55. procedure EnableRedirOut;
  56. function ChangeRedirIn(Const Redir : String) : Boolean;
  57. procedure RestoreRedirIn;
  58. procedure DisableRedirIn;
  59. procedure EnableRedirIn;
  60. function ChangeRedirError(Const Redir : String; AppendToFile : Boolean) : Boolean;
  61. procedure RestoreRedirError;
  62. procedure DisableRedirError;
  63. procedure EnableRedirError;
  64. procedure RedirDisableAll;
  65. procedure RedirEnableAll;
  66. { unused in UNIX }
  67. const
  68. UseComSpec : boolean = true;
  69. Implementation
  70. Uses
  71. sysutils,
  72. {$ifdef go32v2}
  73. go32,
  74. {$endif go32v2}
  75. {$ifdef netwlibc}
  76. Libc,
  77. {$endif netwlibc}
  78. {$ifdef netware_clib}
  79. nwserv,
  80. {$endif netware_clib}
  81. {$ifdef Windows}
  82. windows,
  83. {$endif Windows}
  84. {$ifdef unix}
  85. baseunix,
  86. unix,
  87. {$endif unix}
  88. dos;
  89. Const
  90. {$ifdef UNIX}
  91. DirSep='/';
  92. listsep = [';',':'];
  93. exeext = '';
  94. {$else UNIX}
  95. DirSep='\';
  96. listsep = [';'];
  97. exeext = '.exe';
  98. {$endif UNIX}
  99. var
  100. FIN,FOUT,FERR : ^File;
  101. RedirChangedOut,
  102. RedirChangedIn : Boolean;
  103. RedirChangedError : Boolean;
  104. InRedirDisabled,OutRedirDisabled,ErrorRedirDisabled : Boolean;
  105. {*****************************************************************************
  106. Helpers
  107. *****************************************************************************}
  108. function FixPath(const s:string):string;
  109. var
  110. i : longint;
  111. begin
  112. { Fix separator }
  113. for i:=1 to length(s) do
  114. if s[i] in ['/','\'] then
  115. fixpath[i]:=DirSep
  116. else
  117. fixpath[i]:=s[i];
  118. fixpath[0]:=s[0];
  119. end;
  120. function maybequoted(const s:string):string;
  121. var
  122. s1 : string;
  123. i : integer;
  124. quoted : boolean;
  125. begin
  126. quoted:=false;
  127. s1:='"';
  128. for i:=1 to length(s) do
  129. begin
  130. case s[i] of
  131. '"' :
  132. begin
  133. quoted:=true;
  134. s1:=s1+'\"';
  135. end;
  136. ' ',
  137. #128..#255 :
  138. begin
  139. quoted:=true;
  140. s1:=s1+s[i];
  141. end;
  142. else
  143. s1:=s1+s[i];
  144. end;
  145. end;
  146. if quoted then
  147. maybequoted:=s1+'"'
  148. else
  149. maybequoted:=s;
  150. end;
  151. {*****************************************************************************
  152. Dos
  153. *****************************************************************************}
  154. {$ifdef implemented}
  155. var
  156. TempHOut, TempHIn,TempHError : longint;
  157. {
  158. For linux the following functions exist
  159. Function fpdup(oldfile:longint;var newfile:longint):Boolean;
  160. Function fpdup2(oldfile,newfile:longint):Boolean;
  161. Function fpClose(fd:longint):boolean;
  162. }
  163. {$ifdef go32v2}
  164. function dup(fh : longint;var nh : longint) : boolean;
  165. var
  166. Regs : Registers;
  167. begin
  168. Regs.ah:=$45;
  169. Regs.bx:=fh;
  170. MsDos (Regs);
  171. dup:=true;
  172. If (Regs.Flags and fCarry)=0 then
  173. nh:=Regs.Ax
  174. else
  175. dup:=false;
  176. end;
  177. function dup2(fh,nh : longint) : boolean;
  178. var
  179. Regs : Registers;
  180. begin
  181. dup2:=true;
  182. If fh=nh then
  183. exit;
  184. Regs.ah:=$46;
  185. Regs.bx:=fh;
  186. Regs.cx:=nh;
  187. MsDos (Regs);
  188. If (Regs.Flags and fCarry)<>0 then
  189. dup2:=false;
  190. end;
  191. function fpdup(fh:longint):longint;
  192. begin
  193. if not dup(fh,fpdup) then
  194. fpdup:=-1;
  195. end;
  196. function fpdup2(fh,nh:longint):longint;
  197. begin
  198. if dup2(fh,nh) then
  199. fpdup2:=0
  200. else
  201. fpdup2:=-1;
  202. end;
  203. function fpclose(Handle : Longint) : boolean;
  204. var Regs: registers;
  205. begin
  206. Regs.Eax := $3e00;
  207. Regs.Ebx := Handle;
  208. MsDos(Regs);
  209. fpclose:=(Regs.Flags and fCarry)=0;
  210. end;
  211. {$endif def go32v2}
  212. {$ifdef Windows}
  213. Function fpclose(Handle : Longint) : boolean;
  214. begin
  215. { Do we need this ?? }
  216. fpclose:=true;
  217. end;
  218. {$endif}
  219. {$ifdef os2}
  220. Function fpclose (Handle : Longint) : boolean;
  221. begin
  222. { Do we need this ?? }
  223. fpclose:=true;
  224. end;
  225. {$endif}
  226. {$I-}
  227. function FileExist(const FileName : PathStr) : Boolean;
  228. var
  229. f : file;
  230. Attr : word;
  231. begin
  232. Assign(f, FileName);
  233. GetFAttr(f, Attr);
  234. FileExist := DosError = 0;
  235. end;
  236. function CompleteDir(const Path: string): string;
  237. begin
  238. { keep c: untouched PM }
  239. if (Path<>'') and (Path[Length(Path)]<>DirSep) and
  240. (Path[Length(Path)]<>':') then
  241. CompleteDir:=Path+DirSep
  242. else
  243. CompleteDir:=Path;
  244. end;
  245. function LocateExeFile(var FileName:string): boolean;
  246. var
  247. S : AnsiString;
  248. dir,d,n,e : string;
  249. i : longint;
  250. begin
  251. LocateExeFile:=False;
  252. if FileExist(FileName) then
  253. begin
  254. LocateExeFile:=true;
  255. Exit;
  256. end;
  257. Fsplit(Filename,d,n,e);
  258. if (e='') and FileExist(FileName+exeext) then
  259. begin
  260. FileName:=FileName+exeext;
  261. LocateExeFile:=true;
  262. Exit;
  263. end;
  264. S:=sysutils.GetEnvironmentVariable('PATH');
  265. While Length(S)>0 do
  266. begin
  267. i:=1;
  268. While (i<=Length(S)) and not (S[i] in ListSep) do
  269. Inc(i);
  270. Dir:=CompleteDir(Copy(S,1,i-1));
  271. if i<Length(S) then
  272. Delete(S,1,i)
  273. else
  274. S:='';
  275. if FileExist(Dir+FileName) then
  276. Begin
  277. FileName:=Dir+FileName;
  278. LocateExeFile:=true;
  279. Exit;
  280. End;
  281. end;
  282. end;
  283. {............................................................................}
  284. function ChangeRedirOut(Const Redir : String; AppendToFile : Boolean) : Boolean;
  285. begin
  286. ChangeRedirOut:=False;
  287. If Redir = '' then Exit;
  288. Assign (FOUT^, Redir);
  289. If AppendToFile and FileExist(Redir) then
  290. Begin
  291. Reset(FOUT^,1);
  292. Seek(FOUT^,FileSize(FOUT^));
  293. End else Rewrite (FOUT^);
  294. RedirErrorOut:=IOResult;
  295. IOStatus:=RedirErrorOut;
  296. If IOStatus <> 0 then Exit;
  297. {$ifdef Windows}
  298. if SetStdHandle(Std_Output_Handle,FileRec(FOUT^).Handle) then
  299. {$else not Windows}
  300. TempHOut:=fpdup(StdOutputHandle);
  301. fpdup2(FileRec(FOUT^).Handle,StdOutputHandle);
  302. if (TempHOut<>UnusedHandle) and
  303. (StdOutputHandle<>UnusedHandle) then
  304. {$endif not Windows}
  305. begin
  306. ChangeRedirOut:=True;
  307. OutRedirDisabled:=False;
  308. end;
  309. RedirChangedOut:=True;
  310. end;
  311. function ChangeRedirIn(Const Redir : String) : Boolean;
  312. begin
  313. ChangeRedirIn:=False;
  314. If Redir = '' then Exit;
  315. Assign (FIN^, Redir);
  316. Reset(FIN^,1);
  317. RedirErrorIn:=IOResult;
  318. IOStatus:=RedirErrorIn;
  319. If IOStatus <> 0 then Exit;
  320. {$ifdef Windows}
  321. if SetStdHandle(Std_Input_Handle,FileRec(FIN^).Handle) then
  322. {$else not Windows}
  323. TempHIn:=fpdup(StdInputHandle);
  324. fpdup2(FileRec(FIn^).Handle,StdInputHandle);
  325. if (TempHIn<>UnusedHandle) and
  326. (StdInputHandle<>UnusedHandle) then
  327. {$endif not Windows}
  328. begin
  329. ChangeRedirIn:=True;
  330. InRedirDisabled:=False;
  331. end;
  332. RedirChangedIn:=True;
  333. end;
  334. function ChangeRedirError(Const Redir : String; AppendToFile : Boolean) : Boolean;
  335. begin
  336. ChangeRedirError:=False;
  337. If Redir = '' then Exit;
  338. Assign (FERR^, Redir);
  339. If AppendToFile and FileExist(Redir) then
  340. Begin
  341. Reset(FERR^,1);
  342. Seek(FERR^,FileSize(FERR^));
  343. End else Rewrite (FERR^);
  344. RedirErrorError:=IOResult;
  345. IOStatus:=RedirErrorError;
  346. If IOStatus <> 0 then Exit;
  347. {$ifdef Windows}
  348. if SetStdHandle(Std_Error_Handle,FileRec(FERR^).Handle) then
  349. {$else not Windows}
  350. TempHError:=fpdup(StdErrorHandle);
  351. fpdup2(FileRec(FERR^).Handle,StdErrorHandle);
  352. if (TempHError<>UnusedHandle) and
  353. (StdErrorHandle<>UnusedHandle) then
  354. {$endif not Windows}
  355. begin
  356. ChangeRedirError:=True;
  357. ErrorRedirDisabled:=False;
  358. end;
  359. RedirChangedError:=True;
  360. end;
  361. procedure RestoreRedirOut;
  362. begin
  363. If not RedirChangedOut then Exit;
  364. {$ifdef Windows}
  365. SetStdHandle(Std_Output_Handle,StdOutputHandle);
  366. {$else not Windows}
  367. fpdup2(TempHOut,StdOutputHandle);
  368. {$endif not Windows}
  369. Close (FOUT^);
  370. fpclose(TempHOut);
  371. RedirChangedOut:=false;
  372. end;
  373. {............................................................................}
  374. procedure RestoreRedirIn;
  375. begin
  376. If not RedirChangedIn then Exit;
  377. {$ifdef Windows}
  378. SetStdHandle(Std_Input_Handle,StdInputHandle);
  379. {$else not Windows}
  380. fpdup2(TempHIn,StdInputHandle);
  381. {$endif not Windows}
  382. Close (FIn^);
  383. fpclose(TempHIn);
  384. RedirChangedIn:=false;
  385. end;
  386. {............................................................................}
  387. procedure DisableRedirIn;
  388. begin
  389. If not RedirChangedIn then Exit;
  390. If InRedirDisabled then Exit;
  391. {$ifdef Windows}
  392. SetStdHandle(Std_Input_Handle,StdInputHandle);
  393. {$else not Windows}
  394. fpdup2(TempHIn,StdInputHandle);
  395. {$endif not Windows}
  396. InRedirDisabled:=True;
  397. end;
  398. {............................................................................}
  399. procedure EnableRedirIn;
  400. begin
  401. If not RedirChangedIn then Exit;
  402. If not InRedirDisabled then Exit;
  403. {$ifdef Windows}
  404. SetStdHandle(Std_Input_Handle,FileRec(FIn^).Handle);
  405. {$else not Windows}
  406. fpdup2(FileRec(FIn^).Handle,StdInputHandle);
  407. {$endif not Windows}
  408. InRedirDisabled:=False;
  409. end;
  410. {............................................................................}
  411. procedure DisableRedirOut;
  412. begin
  413. If not RedirChangedOut then Exit;
  414. If OutRedirDisabled then Exit;
  415. {$ifdef Windows}
  416. SetStdHandle(Std_Output_Handle,StdOutputHandle);
  417. {$else not Windows}
  418. fpdup2(TempHOut,StdOutputHandle);
  419. {$endif not Windows}
  420. OutRedirDisabled:=True;
  421. end;
  422. {............................................................................}
  423. procedure EnableRedirOut;
  424. begin
  425. If not RedirChangedOut then Exit;
  426. If not OutRedirDisabled then Exit;
  427. {$ifdef Windows}
  428. SetStdHandle(Std_Output_Handle,FileRec(FOut^).Handle);
  429. {$else not Windows}
  430. fpdup2(FileRec(FOut^).Handle,StdOutputHandle);
  431. {$endif not Windows}
  432. OutRedirDisabled:=False;
  433. end;
  434. {............................................................................}
  435. procedure RestoreRedirError;
  436. begin
  437. If not RedirChangedError then Exit;
  438. {$ifdef Windows}
  439. SetStdHandle(Std_Error_Handle,StdErrorHandle);
  440. {$else not Windows}
  441. fpdup2(TempHError,StdErrorHandle);
  442. {$endif not Windows}
  443. Close (FERR^);
  444. fpclose(TempHError);
  445. RedirChangedError:=false;
  446. end;
  447. {............................................................................}
  448. procedure DisableRedirError;
  449. begin
  450. If not RedirChangedError then Exit;
  451. If ErrorRedirDisabled then Exit;
  452. {$ifdef Windows}
  453. SetStdHandle(Std_Error_Handle,StdErrorHandle);
  454. {$else not Windows}
  455. fpdup2(TempHError,StdErrorHandle);
  456. {$endif not Windows}
  457. ErrorRedirDisabled:=True;
  458. end;
  459. {............................................................................}
  460. procedure EnableRedirError;
  461. begin
  462. If not RedirChangedError then Exit;
  463. If not ErrorRedirDisabled then Exit;
  464. {$ifdef Windows}
  465. SetStdHandle(Std_Error_Handle,FileRec(FErr^).Handle);
  466. {$else not Windows}
  467. fpdup2(FileRec(FERR^).Handle,StdErrorHandle);
  468. {$endif not Windows}
  469. ErrorRedirDisabled:=False;
  470. end;
  471. {............................................................................}
  472. function ExecuteRedir (Const ProgName, ComLine, RedirStdIn, RedirStdOut, RedirStdErr : String) : boolean;
  473. {$ifdef Windows}
  474. var
  475. mode,modebefore : word;
  476. {$endif Windows}
  477. Begin
  478. {$ifdef Windows}
  479. GetConsoleMode(GetStdHandle(cardinal(Std_Input_Handle)), @modebefore);
  480. {$endif Windows}
  481. RedirErrorOut:=0; RedirErrorIn:=0; RedirErrorError:=0;
  482. ExecuteResult:=0;
  483. IOStatus:=0;
  484. if RedirStdIn<>'' then
  485. ChangeRedirIn(RedirStdIn);
  486. if RedirStdOut<>'' then
  487. ChangeRedirOut(RedirStdOut,false);
  488. if RedirStdErr<>'stderr' then
  489. ChangeRedirError(RedirStdErr,false);
  490. DosExecute(ProgName,ComLine);
  491. RestoreRedirOut;
  492. RestoreRedirIn;
  493. RestoreRedirError;
  494. ExecuteRedir:=(IOStatus=0) and (RedirErrorOut=0) and
  495. (RedirErrorIn=0) and (RedirErrorError=0) and
  496. (ExecuteResult=0);
  497. {$ifdef Windows}
  498. // restore previous mode
  499. GetConsoleMode(GetStdHandle(cardinal(Std_Input_Handle)), @mode);
  500. //mode:=mode or ENABLE_MOUSE_INPUT;
  501. SetConsoleMode(GetStdHandle(cardinal(Std_Input_Handle)), modebefore);
  502. {$endif Windows}
  503. End;
  504. {............................................................................}
  505. procedure RedirDisableAll;
  506. begin
  507. If RedirChangedIn and not InRedirDisabled then
  508. DisableRedirIn;
  509. If RedirChangedOut and not OutRedirDisabled then
  510. DisableRedirOut;
  511. If RedirChangedError and not ErrorRedirDisabled then
  512. DisableRedirError;
  513. end;
  514. {............................................................................}
  515. procedure RedirEnableAll;
  516. begin
  517. If RedirChangedIn and InRedirDisabled then
  518. EnableRedirIn;
  519. If RedirChangedOut and OutRedirDisabled then
  520. EnableRedirOut;
  521. If RedirChangedError and ErrorRedirDisabled then
  522. EnableRedirError;
  523. end;
  524. procedure InitRedir;
  525. begin
  526. end;
  527. {$else not implemented}
  528. {*****************************************************************************
  529. Fake
  530. *****************************************************************************}
  531. {$IFDEF SHELL_IMPLEMENTED}
  532. {$I-}
  533. function FileExist(const FileName : PathStr) : Boolean;
  534. var
  535. f : file;
  536. Attr : word;
  537. begin
  538. Assign(f, FileName);
  539. GetFAttr(f, Attr);
  540. FileExist := DosError = 0;
  541. end;
  542. function CompleteDir(const Path: string): string;
  543. begin
  544. { keep c: untouched PM }
  545. if (Path<>'') and (Path[Length(Path)]<>DirSep) and
  546. (Path[Length(Path)]<>':') then
  547. CompleteDir:=Path+DirSep
  548. else
  549. CompleteDir:=Path;
  550. end;
  551. function LocateExeFile(var FileName:string): boolean;
  552. var
  553. S : AnsiString;
  554. dir,d,n,e : string;
  555. i : longint;
  556. begin
  557. LocateExeFile:=False;
  558. if FileExist(FileName) then
  559. begin
  560. LocateExeFile:=true;
  561. Exit;
  562. end;
  563. Fsplit(Filename,d,n,e);
  564. if (e='') and FileExist(FileName+exeext) then
  565. begin
  566. FileName:=FileName+exeext;
  567. LocateExeFile:=true;
  568. Exit;
  569. end;
  570. S:=sysutils.GetEnvironmentVariable('PATH');
  571. While Length(S)>0 do
  572. begin
  573. i:=1;
  574. While (i<=Length(S)) and not (S[i] in ListSep) do
  575. Inc(i);
  576. Dir:=CompleteDir(Copy(S,1,i-1));
  577. if i<Length(S) then
  578. Delete(S,1,i)
  579. else
  580. S:='';
  581. if FileExist(Dir+FileName) then
  582. Begin
  583. FileName:=Dir+FileName;
  584. LocateExeFile:=true;
  585. Exit;
  586. End;
  587. end;
  588. end;
  589. function ExecuteRedir (Const ProgName, ComLine, RedirStdIn, RedirStdOut, RedirStdErr: String): boolean;
  590. var
  591. CmdLine2: string;
  592. begin
  593. CmdLine2 := ComLine;
  594. if RedirStdIn <> '' then CmdLine2 := CmdLine2 + ' < ' + RedirStdIn;
  595. if RedirStdOut <> '' then CmdLine2 := CmdLine2 + ' > ' + RedirStdOut;
  596. if RedirStdErr <> '' then
  597. begin
  598. if RedirStdErr = RedirStdOut
  599. then CmdLine2 := CmdLine2 + ' 2>&1'
  600. else CmdLine2 := CmdLine2 + ' 2> ' + RedirStdErr;
  601. end;
  602. DosExecute (ProgName, CmdLine2);
  603. ExecuteRedir := true;
  604. end;
  605. {$ELSE SHELL_IMPLEMENTED}
  606. function ExecuteRedir (Const ProgName, ComLine, RedirStdIn, RedirStdOut, RedirStdErr : String) : boolean;
  607. begin
  608. ExecuteRedir:=false;
  609. end;
  610. function LocateExeFile(var FileName:string): boolean;
  611. begin
  612. LocateExeFile:=false;
  613. end;
  614. {$ENDIF SHELL_IMPLEMENTED}
  615. function ChangeRedirOut(Const Redir : String; AppendToFile : Boolean) : Boolean;
  616. begin
  617. ChangeRedirOut:=false;
  618. end;
  619. procedure RestoreRedirOut;
  620. begin
  621. end;
  622. procedure DisableRedirOut;
  623. begin
  624. end;
  625. procedure EnableRedirOut;
  626. begin
  627. end;
  628. function ChangeRedirIn(Const Redir : String) : Boolean;
  629. begin
  630. ChangeRedirIn:=false;
  631. end;
  632. procedure RestoreRedirIn;
  633. begin
  634. end;
  635. procedure DisableRedirIn;
  636. begin
  637. end;
  638. procedure EnableRedirIn;
  639. begin
  640. end;
  641. function ChangeRedirError(Const Redir : String; AppendToFile : Boolean) : Boolean;
  642. begin
  643. ChangeRedirError:=false;
  644. end;
  645. procedure RestoreRedirError;
  646. begin
  647. end;
  648. procedure DisableRedirError;
  649. begin
  650. end;
  651. procedure EnableRedirError;
  652. begin
  653. end;
  654. procedure RedirDisableAll;
  655. begin
  656. end;
  657. procedure RedirEnableAll;
  658. begin
  659. end;
  660. procedure InitRedir;
  661. begin
  662. end;
  663. {$endif not implemented}
  664. {............................................................................}
  665. procedure DosExecute(ProgName, ComLine : String);
  666. {$ifdef HASAMIGA}
  667. begin
  668. Dos.Exec(ProgName, ComLine);
  669. end;
  670. {$else}
  671. {$ifdef Windows}
  672. var
  673. StoreInherit : BOOL;
  674. {$endif Windows}
  675. {$ifdef UNIX}
  676. var
  677. s : cint;
  678. {$endif}
  679. Begin
  680. SwapVectors;
  681. {$ifdef UNIX}
  682. IOStatus:=0;
  683. {We need to use fpsystem to get wildcard expansion and avoid being
  684. interrupted by ctrl+c (SIGINT).
  685. But used wifexited and wexitstatus functions
  686. to correctly interpret fpsystem reutrn value }
  687. s:=fpsystem(MaybeQuoted(FixPath(Progname))+' '+Comline);
  688. if wifexited(s) then
  689. ExecuteResult:=wexitstatus(s)
  690. else
  691. begin
  692. ExecuteResult:=word(s);
  693. IOStatus:=(-ExecuteResult) and $7f;
  694. ExecuteResult:=((-ExecuteResult) and $ff00) shr 8;
  695. end;
  696. {$else}
  697. {$ifdef Windows}
  698. StoreInherit:=ExecInheritsHandles;
  699. ExecInheritsHandles:=true;
  700. {$endif Windows}
  701. DosError:=0;
  702. If UseComSpec then
  703. Dos.Exec (Getenv('COMSPEC'),'/C '+MaybeQuoted(FixPath(progname))+' '+Comline)
  704. else
  705. begin
  706. if LocateExeFile(progname) then
  707. Dos.Exec(ProgName,Comline)
  708. else
  709. DosError:=2;
  710. end;
  711. {$ifdef Windows}
  712. ExecInheritsHandles:=StoreInherit;
  713. {$endif Windows}
  714. IOStatus:=DosError;
  715. ExecuteResult:=DosExitCode;
  716. {$endif}
  717. SwapVectors;
  718. {$ifdef CPU86}
  719. { reset the FPU }
  720. {$asmmode att}
  721. asm
  722. fninit
  723. end;
  724. {$endif CPU86}
  725. End;
  726. {$endif HASAMIGA}
  727. {*****************************************************************************
  728. Initialize
  729. *****************************************************************************}
  730. initialization
  731. New(FIn); New(FOut); New(FErr);
  732. finalization
  733. Dispose(FIn); Dispose(FOut); Dispose(FErr);
  734. End.