通过命令行运行时如何确定过程是否将文件传递到标准输入

问题描述 投票:0回答:3

通过命令行运行程序,将其指向输入文件,如下所示

_progres -b -p test.p < test.txt

我可以用一个简单的方法将test.txt的内容读取到test.p中

def var cline as c no-undo.
_tt:
do while true on endkey undo _tt,leave _tt:
import unformatted cline.
end.

但是,如果我不将文件传递给 test.p,则在没有显式打开输入的情况下,错误将是 ** 尝试在没有当前输入源的情况下读取。 (513)。如何确定过程已将文件作为输入传递。

command-line stdin openedge progress-4gl
3个回答
3
投票

这应该可以满足你的要求:

/* isatty.i
 */
 
function isatty returns logical () in super.

和:

/* isatty.p
 * 
 * to use this:
 *
 *    run isatty.p persistent
 *
 *    {isatty.i}
 *    message isatty().
 *
 */

&IF "{&PROCESS-ARCHITECTURE}" = "64" &THEN
  &global-define XINT           int64
  &global-define LONGINT        int64
  &global-define PUTLONGINT     PUT-INT64
  &global-define GETLONGINT     GET-INT64
 &ELSE
  &global-define XINT           integer
  &global-define LONGINT        long
  &global-define PUTLONGINT     PUT-LONG
  &global-define GETLONGINT     GET-LONG
&ENDIF

define stream inStrm.

session:add-super-procedure( this-procedure ).

return.


procedure GetFileType external "kernel32.dll":
  define input  parameter fileHandle as {&LONGINT}.
  define return parameter result     as {&LONGINT}.
end.

procedure GetStdHandle external "kernel32.dll":
  define input  parameter fileHandle as {&LONGINT}.
  define return parameter result     as {&LONGINT}.
end.


/* determine if we are running with user input or redirected input
 *
 */

function isatty returns logical ():

  define variable result     as logical   no-undo.
  define variable tty        as character no-undo.
  define variable fileHandle as int64     no-undo.
  define variable fileType   as int64     no-undo.

  result = false.

  if opsys = "unix" then
    do:

      input stream inStrm through value( "tty" ).
      import stream inStrm unformatted tty.
      input stream inStrm close.
      if tty begins "/dev/" then
        result = true.

    end.
   else
    do:

      /* Windows stdin = -10    */

      run getStdHandle( -10, output fileHandle ).

      run getFileType( fileHandle, output fileType ).

      /* 0x0000 = unknown
       * 0x0001 = disk
       * 0x0002 = character (CON or LPT etc)
       * 0x0003 = pipe
       * 0x8000 = remote (unused?)
       */

      if fileType = 2 then
        result = true.

    end.

  return result.

end.

测试它:

/* testtty.p
 */

run isatty.p persistent.
   
{isatty.i}
message isatty().
 
quit.

像这样:

$ pro -p testtty.p
yes

和:

$ cat /dev/null | pro -b -p testtty.p > tty.out
$ cat tty.out
no

这是合法的(但有点令人困惑):

$ pro -b -p testtty.p > tty.out
$ cat tty.out
yes

如果您故意这样做,请注意在这种情况下 LASTKEY 将为 -1。


0
投票

对于 Windows,我编写了一个简单的 C++ 程序

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int i,l;
    string str;
    fseek(stdin, 0, SEEK_END);
    i = ftell(stdin);
    if (i == 0)
      return 0;
    fseek(stdin, 0, SEEK_SET);
    for (std::string line; std::getline(std::cin, line);) {
        std::cout << line << std::endl;
    }
    return 0;
}

对于 UNIX,我使用了以下代码

DEFINE VARIABLE c AS C. 
define stream sin.
input stream sin through VALUE('[ ! -t 0 ] && echo $(</dev/stdin) || echo ""').
DO WHILE TRUE ON ENDKEY UNDO,LEAVE:
   import stream sin unformatted c.
   MESSAGE c. PAUSE 0.
 END.
input stream sin close.

0
投票

比起依赖 API 调用,这样做可能更简单:

_progres -b -p test.p -param test.txt

然后在会话中,您可以检查 SESSION:PARAMETER 的值,该值保存您在 -param 中指定的任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.