fpredir.pas 19 KB

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