1. Redirection of output

Hello all,

how can a windows program detect, whether it's output has been
redirected to a file?

Normally, when I want to see the output of puts(1, ...), I must
add wait_key() or something similar to the code. Then I have the
following code, for instance:

----------------------------------------------------------
include get.e
integer junk

puts(1, "Hello World!\n\n")
puts(2, "Press any key...")
junk = wait_key()
----------------------------------------------------------

When the output is redirected, the statements
   puts(2, "Press any key...")
   junk = wait_key()
are not useful.

It would be very helpful, to have a function for this, so that
the code could be written as:

----------------------------------------------------------
include get.e
integer junk

puts(1, "Hello World!\n\n")
if not output_redirected() then
   puts(2, "Press any key...")
   junk = wait_key()
end if
----------------------------------------------------------

Can anyone help?

Thanks in advance,
   Juergen

new topic     » topic index » view message » categorize

2. Re: Redirection of output

On Mon, 1 Jul 2002 07:26:20 +0200, Juergen Luethje <jluethje at gmx.de> 
wrote: 
 
>how can a windows program detect, whether it's output has been 
>redirected to a file? 
 
I had a quick search for you and found this: 
############ begin ############## 
Subject: Re: Detecting redirected output in DOS?  
   Date: Thu, 26 Aug 1999 19:20:32 +0200  
   From: Andreas Killer <Andreas.Killer at t-online.de>  
Newsgroups: borland.public.turbopascal  
Curtis Cameron schrieb:  
 
> I'd like it to detect whether the output is being redirected or is going  
> to the display, and turn off the paging feature if it's redirected. Is  
> this possible?  
 
Yes, use this routine with Output to detect if the output is 
redirected:  
 
  function Redirect(var T : Text) : Boolean; Assembler;  
    {-True wenn InPut/OutPut per DOS in auf eine Datei umgeleitet 
wurde.}  
    { true if Input/Output was redirected via DOS to a file}  
  asm  
    les     di, T  
    mov     ax, 4400h             {-IOCTL: Attribut lesen {read 
attribute  
    mov     bx, es:[di]           {-BX = Handle  
    int     21h  
    mov     al, False  
    jc      @@Done                {-Fehler?}  {error  
{---  
  DX enthält die Attribute des Treibers. Ein gesetztes Bit bedeutet:  
     0    Treiber ist CON-Eingabe (Tastatur)  
     1    Treiber ist CON-Ausgabe (Bildschirm)  
     2    Treiber ist NUL  
     3    Treiber ist ein Clock-Device  
     5    Treiber ist im RAW-Modus, ansonsten COOKED-Modus  
     7    Treiber ist ein Zeichentreiber, ansonsten eine Datei  
    14    Treiber kann Steuerzeichen von AX=4402h/4403h INT 21 
verarbeiten  
  Alle anderen Bits sind reserviert.  
  DX contains the attributes of the driver. A set bit represents:  
     0    driver is CON-input {keyboard  
     1    driver is CON-output {CRT  
     2    driver is NUL  
     3    driver is a clock device  
     5    driver is in raw mode, else cooked mode  
     7    driver is a character driver, else a file (block device)  
    14    driver can work on control codes from AX=4402H/4403H Int 21H 
  all remaining bits are reserved  
---}  
 
    test    dl, 80h  
    jnz     @@Done  
    mov     al, True  
@@Done:  
  end;  
 
########## end ############ 
 
I dunno what it means either, but someone might!

new topic     » goto parent     » topic index » view message » categorize

3. Re: Redirection of output

Hello Derek,

> I'm not sure if this is what you are after , but I tend to do this sort of
> thing...

>   if platform() != WIN32 then
>     puts(2, "Press any key...")
>     junk = wait_key()   
>   end if

no, this is not what I'm after.

When I have a program "myprog.exw" that does some console output, then
I want it to be able to detect, whether the user had called it this way:
   exw.exe myprog.exw

or that way:
   exw.exe myprog.exw > myoutput.txt

I know that this is possible, because there are other programming
languages with built-in functions for this purpose. Please see also my
reply to Pete.

Thanks and best regards,
   Juergen








>> -----Original Message-----
>> From: Juergen Luethje [mailto:jluethje at gmx.de]
>> Sent: Monday, 1 July 2002 15:39
>> To: EUforum
>> Subject: Redirection of output
>> 
>> 
>> Hello all,
>> 
>> how can a windows program detect, whether it's output has been
>> redirected to a file?
>> 
>> Normally, when I want to see the output of puts(1, ...), I must
>> add wait_key() or something similar to the code. Then I have the
>> following code, for instance:
>> 
>> ----------------------------------------------------------
>> include get.e
>> integer junk
>> 
>> puts(1, "Hello World!\n\n")
>> puts(2, "Press any key...")
>> junk = wait_key()
>> ----------------------------------------------------------
>> 
>> When the output is redirected, the statements
>>    puts(2, "Press any key...")
>>    junk = wait_key()
>> are not useful.
>> 
>> It would be very helpful, to have a function for this, so that
>> the code could be written as:
>> 
>> ----------------------------------------------------------
>> include get.e
>> integer junk
>> 
>> puts(1, "Hello World!\n\n")
>> if not output_redirected() then
>>    puts(2, "Press any key...")
>>    junk = wait_key()
>> end if
>> ----------------------------------------------------------
>> 
>> Can anyone help?
>> 
>> Thanks in advance,
>>    Juergen

new topic     » goto parent     » topic index » view message » categorize

4. Re: Redirection of output

Hello Pete,

you wrote:

> On Mon, 1 Jul 2002 07:26:20 +0200, Juergen Luethje <jluethje at gmx.de>
> wrote:

>>how can a windows program detect, whether it's output has been
>>redirected to a file?

> I had a quick search for you and found this:
> ############ begin ##############
> Subject: Re: Detecting redirected output in DOS? 
>    Date: Thu, 26 Aug 1999 19:20:32 +0200 
>    From: Andreas Killer <Andreas.Killer at t-online.de> 
> Newsgroups: borland.public.turbopascal 
> Curtis Cameron schrieb: 

>> I'd like it to detect whether the output is being redirected or is going 
>> to the display, and turn off the paging feature if it's redirected. Is 
>> this possible? 

> Yes, use this routine with Output to detect if the output is
> redirected: 

>   function Redirect(var T : Text) : Boolean; Assembler; 
>     {-True wenn InPut/OutPut per DOS in auf eine Datei umgeleitet
> wurde.} 
>     { true if Input/Output was redirected via DOS to a file} 
>   asm 
>     les     di, T 
>     mov     ax, 4400h             {-IOCTL: Attribut lesen} {read
> attribute} 
>     mov     bx, es:[di]           {-BX = Handle} 
>     int     21h 
>     mov     al, False 
>     jc      @@Done                {-Fehler?}  {error?} 
> {--- 
>   DX enthält die Attribute des Treibers. Ein gesetztes Bit bedeutet: 
>      0    Treiber ist CON-Eingabe (Tastatur) 
>      1    Treiber ist CON-Ausgabe (Bildschirm) 
>      2    Treiber ist NUL 
>      3    Treiber ist ein Clock-Device 
>      5    Treiber ist im RAW-Modus, ansonsten COOKED-Modus 
>      7    Treiber ist ein Zeichentreiber, ansonsten eine Datei 
>     14    Treiber kann Steuerzeichen von AX=4402h/4403h INT 21
> verarbeiten 
>   Alle anderen Bits sind reserviert. 
>   DX contains the attributes of the driver. A set bit represents: 
>      0    driver is CON-input {keyboard} 
>      1    driver is CON-output {CRT} 
>      2    driver is NUL 
>      3    driver is a clock device 
>      5    driver is in raw mode, else cooked mode 
>      7    driver is a character driver, else a file (block device) 
>     14    driver can work on control codes from AX=4402H/4403H Int 21H
>   all remaining bits are reserved 
> ---} 

>     test    dl, 80h 
>     jnz     @@Done 
>     mov     al, True 
> @@Done: 
>   end; 

> ########## end ############

> I dunno what it means either, but someone might!

Thank you!!
That looks like the DOS-Version of the function I'm looking for,
but unfortunately I'm not able, to translate it to proper working
Eu code. sad

In the meantime, I had the idea for a "brute force" function
(see below).

Best regards,
   Juergen

   
--------------------------------------------------------------->8---
redir.e
--------------------------------------------------------------------
-- works on DOS32, WIN32  (and Linux ?)
include graphics.e

global function output_redirected()
   sequence s
   integer ret

   s = get_position()
   puts(1, '\n')
   ret = equal(s, get_position())
   if ret = 0 then
      position(s[1], s[2])
   end if
   return ret
end function
--------------------------------------------------------------------


--------------------------------------------------------------------
redemo.exw  or  redemo.ex  (or  redemo.exu ?)
--------------------------------------------------------------------
include get.e
include redir.e

integer junk

puts(1, "Hello World!\n\n")
if not output_redirected() then
   puts(2, "Press any key...")
   junk = wait_key()
end if
--------------------------------------------------------------->8---

new topic     » goto parent     » topic index » view message » categorize

5. Re: Redirection of output

Hi all, I wrote:

> Hello Pete,

> you wrote:

>> On Mon, 1 Jul 2002 07:26:20 +0200, Juergen Luethje <jluethje at gmx.de>
>> wrote:

>>> how can a windows program detect, whether it's output has been
>>> redirected to a file?

>> I had a quick search for you and found this:
[decisive reference to int #21, ax=#4400]

> Thank you!!
> That looks like the DOS-Version of the function I'm looking for,
> but unfortunately I'm not able, to translate it to proper working
> Eu code. sad
<snip>

After having one more look at Ralph Brown's interrupt list,
now I managed to write an appropriate Eu function.
(Maybe I could not imagine, that it is that simple. blink

--------------------------------------------------------------->8---
redir.ex
--------------------------------------------------------------------
include machine.e

constant REG_LIST_SIZE = 10, CF = 1

global function output_redirected ()
   -- returns TRUE or FALSE, or -1 on error
   -- (after Ralf Brown's Interrupt List, Release 61 (2000))
   sequence reg_list

   reg_list = repeat(0, REG_LIST_SIZE)
   reg_list[REG_AX] = #4400
   reg_list[REG_BX] = 1   -- handle (1 = StdOut)
   reg_list[REG_FLAGS] = or_bits(reg_list[REG_FLAGS], 1)
   reg_list = dos_interrupt(#21, reg_list)
   if and_bits(reg_list[REG_FLAGS], CF) = 1 then
      return -1   -- error
   else
      return (and_bits(reg_list[REG_DX], #8000)  = 0)  -- redirected to a file
          or (and_bits(reg_list[REG_DX], #0004) != 0)  -- redirected to NUL
   end if
end function


-- Demo
integer ofn

puts(1, "Hello World!\n")
ofn = open("redir.log", "w")
printf(ofn, "redirected: %d", output_redirected())
close(ofn)
--------------------------------------------------------------->8---


To see what the program does, call it the following ways:
   ex.exe redir.ex
   ex.exe redir.ex > re.txt
   ex.exe redir.ex > nul

and after each call, read the text in redir.log.

Thanks again Pete, and best regards,
   Juergen


PS: Does anyone know how this could be done on Windows?

new topic     » goto parent     » topic index » view message » categorize

6. Re: Redirection of output

{{{ On Sat, 6 Jul 2002 15:20:00 +0200, Juergen Luethje <jluethje at gmx.de> wrote:

<snip>

[decisive reference to int #21, ax=#4400] <snip>
PS: Does anyone know how this could be done on Windows?

Hmm, shows how little I know, thought that wd b same in Windz.

Pete

new topic     » goto parent     » topic index » view message » categorize

7. Re: Redirection of output

Hi Pete, you wrote:

> On Sat, 6 Jul 2002 15:20:00 +0200, Juergen Luethje <jluethje at gmx.de>
> wrote:

> <snip>
>> [decisive reference to int #21, ax=#4400]
> <snip>
>> PS: Does anyone know how this could be done on Windows?

> Hmm, shows how little I know, thought that wd b same in Windz.

> Pete

AFAIK it's not possible to call a DOS interrupt from a 32-bit Windows
application. (At least Euphoria doesn't allow it: The command
"x = dos_interrupt()" will cause an error in an .EXW program.)

Converting DOS code to Windows will mean replacing interrupt-based code
with Windows API calls, I think. But I'm not an expert in this field,
too. Maybe someone else can tell us something more?

Best regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu