Re: [Mingw-w64-public] [PATCH mingw-w64] Add include/iscygtty.c

2016-11-10 Thread Mihail Konev
There are files that were catenated.

Running curses in conemu should be possible with
https://conemu.github.io/en/CygwinMsysConnector.html

But even then, the iscygtty() is supposed to check
whether it is present with a mintty-like stdin, and nothing else.

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.00C4.0061009F
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

--
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
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] [PATCH mingw-w64] Add include/iscygtty.c

2016-11-10 Thread Vincent Torri
two cases :

1) pipe redirection : mintty and therefore cygwin (>= 1.8) terminal
and MSYS2 terminal (as the 2 latters are based on mintty)
2) MSYS1 and CMD console

ConEmu is a specific case as, if i'm not mistaken, it can switch from
one redirection to the other one

Vincent

PS: i've also replied to the mingw-w64 ML, Mihail, maybe you should
send again the files to the ML

On Thu, Nov 10, 2016 at 7:13 PM, Mihail Konev  wrote:
> 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.

--
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
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] [PATCH mingw-w64] Add include/iscygtty.c

2016-11-09 Thread Vincent Torri
hello

just call GetConsoleMode() and check its result to know if the
redirection is with a pipe or a console handle

Vincent


On Thu, Nov 10, 2016 at 1:46 AM, Mihail Konev  wrote:
> Applications now could call iscygtty(STDIN_FILENO)
> in order to detect whether they are running from
> Cygwin/MSys terminal.
>
> Without that, they have no choice but to think that
> stdin is redirected from a named pipe.
>
> Signed-off-by: Mihail Konev 
> Moved-from: https://github.com/Alexpux/mingw-w64/pull/3
> ---
>  mingw-w64-headers/include/iscygtty.c | 69 
> 
>  1 file changed, 69 insertions(+)
>  create mode 100644 mingw-w64-headers/include/iscygtty.c
>
> diff --git a/mingw-w64-headers/include/iscygtty.c 
> b/mingw-w64-headers/include/iscygtty.c
> new file mode 100644
> index ..f7344b8b20c4
> --- /dev/null
> +++ b/mingw-w64-headers/include/iscygtty.c
> @@ -0,0 +1,69 @@
> +#ifndef __ISCYGTTY_C__
> +#define __ISCYGTTY_C__
> +
> +#include 
> +#include 
> +
> +static int iscygtty(int fd) {
> +intptr_t h_fd = _get_osfhandle(fd);
> +
> +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);
> +
> +USHORT i, l;
> +wchar_t c, *s0;
> +
> +memset(ntfn, 0, ntfn_size);
> +status = NtQueryObject((HANDLE)h_fd, ObjectNameInformation,
> +ntfn, ntfn_size, _size);
> +
> +if (!NT_SUCCESS(status))
> +return 0;
> +
> +l = ntfn->Name.Length;
> +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;
> +c = *s;
> +if ((c == 'p' || c == 't') && s[1] == 't' && s[2] == 'y' &&
> +(c = s[3]) && (c >= '0') && (c <= '9'))
> +{
> +s += 4;
> +while ((c = *s) && (c >= '0') && (c <= '9'))
> +s++;
> +if (c == '-' || c == 0)
> +return 1;
> +}
> +}
> +}
> +
> +return 0;
> +}
> +
> +#endif /* __ISCYGTTY_C__ */
> --
> 2.9.2
>
>
> --
> 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
> Mingw-w64-public@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

--
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
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


[Mingw-w64-public] [PATCH mingw-w64] Add include/iscygtty.c

2016-11-09 Thread Mihail Konev
Applications now could call iscygtty(STDIN_FILENO)
in order to detect whether they are running from
Cygwin/MSys terminal.

Without that, they have no choice but to think that
stdin is redirected from a named pipe.

Signed-off-by: Mihail Konev 
Moved-from: https://github.com/Alexpux/mingw-w64/pull/3
---
 mingw-w64-headers/include/iscygtty.c | 69 
 1 file changed, 69 insertions(+)
 create mode 100644 mingw-w64-headers/include/iscygtty.c

diff --git a/mingw-w64-headers/include/iscygtty.c 
b/mingw-w64-headers/include/iscygtty.c
new file mode 100644
index ..f7344b8b20c4
--- /dev/null
+++ b/mingw-w64-headers/include/iscygtty.c
@@ -0,0 +1,69 @@
+#ifndef __ISCYGTTY_C__
+#define __ISCYGTTY_C__
+
+#include 
+#include 
+
+static int iscygtty(int fd) {
+intptr_t h_fd = _get_osfhandle(fd);
+
+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);
+
+USHORT i, l;
+wchar_t c, *s0;
+
+memset(ntfn, 0, ntfn_size);
+status = NtQueryObject((HANDLE)h_fd, ObjectNameInformation,
+ntfn, ntfn_size, _size);
+
+if (!NT_SUCCESS(status))
+return 0;
+
+l = ntfn->Name.Length;
+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;
+c = *s;
+if ((c == 'p' || c == 't') && s[1] == 't' && s[2] == 'y' &&
+(c = s[3]) && (c >= '0') && (c <= '9'))
+{
+s += 4;
+while ((c = *s) && (c >= '0') && (c <= '9'))
+s++;
+if (c == '-' || c == 0)
+return 1;
+}
+}
+}
+
+return 0;
+}
+
+#endif /* __ISCYGTTY_C__ */
-- 
2.9.2


--
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
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public