Wrong destination address :)

I didn't tried ConEmu.
Is it possible to run cygwin curses app in it, like a text editor, btw?

But with msys mintty and standard Console results do differ.

----- Forwarded message from Mihail Konev <[email protected]> -----

From: Mihail Konev <[email protected]>
To: Vincent Torri <[email protected]>
Date: Thu, 10 Nov 2016 23:13:15 +0500
Subject: Re: [Mingw-w64-public] [PATCH mingw-w64] Add include/iscygtty.c

On Thu, Nov 10, 2016 at 06:16:38AM +0100, Vincent Torri wrote:
> just call GetConsoleMode() and check its result to know if the
> redirection is with a pipe or a console handle
The result would then differ in two cases:
- stdin is Cygwin/MSys terminal
- stdin is Windows console

Attached are test program source, Makefile, and 'make' output when run
from MSys2-MINGW64 shell.

#include <stdio.h>

#include <io.h>
#include <winternl.h>

#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif

#ifndef NO_CONSOLE_MODE
#include <windows.h>
#endif

static int isacygtty(int fd) {
    intptr_t h_stdin = _get_osfhandle(fd);
    printf("h_stdin: %ld\n", (long)h_stdin);

    char ntfn_bytes[sizeof(OBJECT_NAME_INFORMATION) + 256 * sizeof(WCHAR)];
    OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION*) ntfn_bytes;
    NTSTATUS status;
    ULONG ntfn_size = sizeof(ntfn_bytes);

    memset(ntfn, 0, ntfn_size);
    status = NtQueryObject((HANDLE)h_stdin, ObjectNameInformation,
            ntfn, ntfn_size, &ntfn_size);

    if (!NT_SUCCESS(status)) {
        printf("failed to NtQueryObject h_stdin ObjectNameInformation (return 
code %ld)\n", (long)status);
        return 0;
    }

    printf("len %d\n", ntfn->Name.Length);
    printf("name %ls\n", ntfn->Name.Buffer);

    USHORT i, l = ntfn->Name.Length;
    wchar_t c, *s0 = ntfn->Name.Buffer;
    /* Check for "\Device\NamedPipe" */
    {
        USHORT l1 = l;
        wchar_t *s1 = s0;
        wchar_t expect[] = L"\\Device\\NamedPipe\\";

        if (s0[0] == '\\' && s0[1] == '\\' && s0[2] == '?' && s0[3] == '\\') {
            l1 -= 4;
            s1 += 4;
        }
        for (i = 0; i < l1; i++) {
            wchar_t e = expect[i];
            c = s1[i];
            if (!e)
                break;
            if (c != e)
                return 0;
        }
    }
    /* Look for "-pty%d-" */
    for (i = 0; i < l; i++) {
        c = s0[i];
        if (c == '-') {
            wchar_t *s = s0 + i + 1;
            if (s[0] == 'p' && s[1] == 't' && s[2] == 'y' &&
                    (c = s[3]) && (c >= '0') && (c <= '9'))
            {
                printf("-pty%%d [+%d]: '%ls'\n", (int)i, s);
                s += 4;
                while ((c = *s) && (c >= '0') && (c <= '9'))
                    s++;
                if (c == '-' || c == 0)
                    return 1;
            }
        }
    }

    return 0;
}

#ifndef NO_CONSOLE_MODE
int isaconsole(DWORD std_handle) {
    {
        size_t l = 256;
        WCHAR console_title[l];
        memset(console_title, 0, l);
        if (!GetConsoleTitleW(console_title, l)) {
            printf("failed to GetConsoleTitleW()\n");
            return 0;
        } else {
            printf("console title: %ls\n", console_title);
        }
    }

    {
        HANDLE h_std = GetStdHandle(std_handle);
        DWORD console_mode;
        long rc;

        if (h_std == INVALID_HANDLE_VALUE) {
            printf("failed to GetStdHandle()\n");
            return 0;
        }

        rc = GetConsoleMode(h_std, &console_mode);
        if (!rc) {
            printf("failed to GetConsoleMode() (return code %ld)\n", rc);
            return 0;
        }
    }

    return 1;
}
#endif

int main(int argc, char **argv) {
    (void) argc; (void) argv;

    printf("is stdin a cygwin tty: %s\n", isacygtty(STDIN_FILENO) ? "yes" : 
"no");
#ifndef NO_CONSOLE_MODE
    printf("is stdin a console: %s\n", isaconsole(STD_INPUT_HANDLE) ? "yes" : 
"no");
#endif
    return 0;
}

all: test

a.exe: main.c
        $(CC) -Wall -Wextra main.c -lntdll

test: a.exe
        @echo
        @echo " -- Test not a tty --"
        @echo
        ls | ./a.exe
        @echo
        @echo " -- Test a tty --"
        @echo
        ./a.exe
        @echo
        @echo " -- Test a named pipe --"
        @echo
        @rm .named_pipe 2>/dev/null || true
        mkfifo .named_pipe
        ( sleep 0.2; echo abc > .named_pipe; ) & \
                ./a.exe < .named_pipe
        @echo
        @echo " -- Test a cmd.exe --"
        @echo
        echo "a.exe" | cmd
        @echo
        @echo
        @echo " -- Test a console --"
        @echo
        start //wait //min cmd //c "a.exe > .con_out"
        @cat .con_out
        @rm .con_out
        @echo

clean:
        rm a.exe 2>/dev/null || true


 -- Test not a tty --

ls | ./a.exe
h_stdin: 508
len 106
name \Device\NamedPipe\msys-65762b9fcd7dbfea-1952-pipe-0x1
is stdin a cygwin tty: no
console title: invisible cygwin console
failed to GetConsoleMode() (return code 0)
is stdin a console: no

 -- Test a tty --

/a.exe
h_stdin: 464
len 112
name \Device\NamedPipe\msys-65762b9fcd7dbfea-pty0-from-master
-pty%d [+39]: 'pty0-from-master'
is stdin a cygwin tty: yes
console title: invisible cygwin console
failed to GetConsoleMode() (return code 0)
is stdin a console: no

 -- Test a named pipe --

mkfifo .named_pipe
( sleep 0.2; echo abc > .named_pipe; ) & \
        ./a.exe < .named_pipe
h_stdin: 292
len 140
name \Device\NamedPipe\msys-65762b9fcd7dbfea-fifo.000000C4.006100000000009F
is stdin a cygwin tty: no
console title: invisible cygwin console
failed to GetConsoleMode() (return code 0)
is stdin a console: no

 -- Test a cmd.exe --

echo "a.exe" | cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

J:\msys\home\u\mingw-isatty>a.exe
h_stdin: 508
len 104
name \Device\NamedPipe\msys-65762b9fcd7dbfea-400-pipe-0x1
is stdin a cygwin tty: no
console title: invisible cygwin console - a.exe
failed to GetConsoleMode() (return code 0)
is stdin a console: no

J:\msys\home\u\mingw-isatty>

 -- Test a console --

start //wait //min cmd //c "a.exe > .con_out"
h_stdin: 3
failed to NtQueryObject h_stdin ObjectNameInformation (return code -1073741816)
is stdin a cygwin tty: no
console title: C:\Windows\System32\cmd.exe
is stdin a console: yes



----- End forwarded message -----

------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to