2016-11-20 78 views
1

我需要知道标准输入和标准输出是否已被重定向使用最新的FPC(V3.0.0)如何检测stdin或stdout是否在Windows下的FreePascal中重定向?

一个控制台应用程序在旧Turbo Pascal的日子里,我不得不写在组件这样做的功能。如下所示:

{ ************************************************************** 
    * Routine : RedirectedStd         * 
    * Purpose : Return Yes (True) if standard handle is being * 
    *   : redirected.         * 
    * Note(s) : Even though handle can take any handle value, * 
    *   : the function will be meaningful only for the * 
    *   : standard input, standard output, and standard * 
    *   : error. It will, however, be True for any  * 
    *   : handle that does NOT point to the console.  * 
    *   : (Besides, this is what it actually checks for.)* 
    *   : Make sure handle belongs to an open file or * 
    *   : you will get wrong answer (check IOResult). * 
    ************************************************************** } 
function RedirectedStd(handle: Word): Boolean; assembler; 
const 
    DEVICE  = $0080; 
    FASTCONSOLE = $0010; 
    CONSOUT  = $0002; 
    CONSIN  = $0001; 
asm 
    mov  InOutRes,0 
    mov  ax,$4400  { IOCTL svc, get device information } 
    mov  bx,handle 
    int  $21   { result in DX } 
    mov  ax,1   { assume function is True } 
    jc  @Error   { got error with code in AX } 
    test dx,DEVICE 
    jz  @Out 
    test dx,FASTCONSOLE 
    jz  @Out 
    test dx,CONSOUT 
    jz  @Out 
    test dx,CONSIN 
    jz  @Out 
    xor  ax,ax   { function is False } 
    jmp  @Out 
@Error: 
    mov  InOutRes,ax 
@Out: 
end; { RedirectedStd } 

此语法对于FPC汇编器无效。我想我的运气有以下变体,它虽然编译OK崩溃:(不知道如果我的转换等效)

function RedirectedStd(handle: Word): Boolean; assembler; 
label Error,Done; 
const DEVICE  = $0080; 
     FASTCONSOLE = $0010; 
     CONSOUT  = $0002; 
     CONSIN  = $0001; 
asm 
      movw  $0,InOutRes 
      movw  $4400,%ax   { IOCTL svc, get device information } 
      movw  handle,%bx 
      int  $21     { result in DX } 
      movw  $1,%ax    { assume function is True } 
      jc  Error    { got error with code in AX } 
      test  DEVICE,%dx 
      jz  Done 
      test  FASTCONSOLE,%dx 
      jz  Done 
      test  CONSOUT,%dx 
      jz  Done 
      test  CONSIN,%dx 
      jz  Done 
      xor  %ax,%ax    { function is False } 
      jmp  Done 
Error: movw  %ax,InOutRes 
Done: 
end; { RedirectedStd } 

任何想法?

编辑: 基于这给了我足够的方向找出解决办法接受的答案,我想出了下面的下拉更换为我原来的子程序:

function RedirectedStd(handle: Word): Boolean; {$ifndef WINDOWS} unimplemented; {$endif} 
begin 
    RedirectedStd := False; 
    {$ifdef WINDOWS} 
    case handle of 
    0: RedirectedStd := GetFileType(GetStdHandle(STD_INPUT_HANDLE)) <> FILE_TYPE_CHAR; 
    1: RedirectedStd := GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)) <> FILE_TYPE_CHAR; 
    2: RedirectedStd := GetFileType(GetStdHandle(STD_ERROR_HANDLE)) <> FILE_TYPE_CHAR; 
    end; 
    {$endif} 
end; { RedirectedStd } 

哦,你需要请使用Windows;

+0

可能重复[如何确定Console.Out是否已被重定向到文件?](http://stackoverflow.com/questions/743885/how-can-i-determine-whether-console-out-已被重定向到一个文件) –

+0

@DavidHeffernan:不是。我正在寻找FreePascal解决方案。 – tonypdmtr

+0

在顶级答案中调用winapi函数。或者找到其中一个说无数其他的模糊的东西。这里有一个http://stackoverflow.com/questions/9021916/how-do-i-check-if-my-delphi-console-app-is-redirected-to-a-file-or-pipe还有很多很多 –

回答

1

使用

GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)); 

标准输出。对于stdin,应该很明显该怎么做。

+0

够好。我不得不做更多的工作来弄清楚返回的值是如何使用的,但是让它工作起来。我更新了我的问题以包含工作功能。 – tonypdmtr

+1

这一切都记录在案,你知道。应该从中明确。不要使用魔术常量。 –

相关问题