[Mingw-w64-public] [PATCH v5] Add include/iscygtty.c
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 --- v5: - stricter check (follows cygwin tty.cc) - drop \\?\, as the name shouldn't be long mingw-w64-headers/include/iscygtty.c | 104 +++ 1 file changed, 104 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 ..1fbd8a6cba3d --- /dev/null +++ b/mingw-w64-headers/include/iscygtty.c @@ -0,0 +1,104 @@ +#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, &ntfn_size); + +if (!NT_SUCCESS(status)) +return 0; + +l = ntfn->Name.Length; +s0 = ntfn->Name.Buffer; + +/* Check for "\Device\NamedPipe" */ +{ +wchar_t expect[] = L"\\Device\\NamedPipe\\"; + +for (i = 0; i < l; i++) { +wchar_t e = expect[i]; +c = s0[i]; +if (!e) +break; +if (c != e) { +return 0; +} +} + +l -= i; +s0 += i; +} + +/* Look for "[a-z0-9]+-[a-z0-9]+-pty[0-9]+-from-master" */ +{ +int part = 0; + +for (i = 0; i < l; i++) { +c = s0[i]; + +if (!c) +break; + +if (c == L'-') { +part++; + +/* handle -pty%d- */ +if (part == 2) { +wchar_t *s = s0 + i + 1; + +if (s[0] == L'p' && s[1] == L't' && s[2] == L'y' && +(c = s[3]) && (c >= L'0') && (c <= L'9')) +{ +s += 4; +while ((c = *s) && (c >= L'0') && (c <= L'9')) +s++; +if (c != L'-') { +return 0; +} +part++; + +{ +wchar_t expect[] = L"-from-master"; +int j; + +j = 0; +while (1) { +wchar_t e = expect[j]; +c = s[j]; +if (!e) { +return 1; +} +if (c != e) { +return 0; +} +j++; +} +} +} else { +return 0; +} +} +} else if (!(c >= L'a' && c <= L'z') && !(c >= L'0' && c <= L'9')) { +return 0; +} +} +} + +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
Re: [Mingw-w64-public] [PATCH v5] Add include/iscygtty.c
On 11/12/2016 11:57, 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 I don't really have any big objections other than on style. > --- > v5: > - stricter check (follows cygwin tty.cc) > - drop \\?\, as the name shouldn't be long > > mingw-w64-headers/include/iscygtty.c | 104 > +++ > 1 file changed, 104 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 ..1fbd8a6cba3d > --- /dev/null > +++ b/mingw-w64-headers/include/iscygtty.c > @@ -0,0 +1,104 @@ > +#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)]; Where does this magic 256 number come from? > +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, &ntfn_size); > + > +if (!NT_SUCCESS(status)) > +return 0; > + > +l = ntfn->Name.Length; > +s0 = ntfn->Name.Buffer; > + > +/* Check for "\Device\NamedPipe" */ > +{ > +wchar_t expect[] = L"\\Device\\NamedPipe\\"; I'd prefer all the variables initialized at the beginning of the function then than in an anonymous scope. Make it const too if you are not writing to it. > + > +for (i = 0; i < l; i++) { > +wchar_t e = expect[i]; > +c = s0[i]; > +if (!e) > +break; > +if (c != e) { > +return 0; > +} > +} > + > +l -= i; > +s0 += i; > +} Use wide strncmp? > + > +/* Look for "[a-z0-9]+-[a-z0-9]+-pty[0-9]+-from-master" */ > +{ > +int part = 0; > + > +for (i = 0; i < l; i++) { > +c = s0[i]; > + > +if (!c) > +break; > + > +if (c == L'-') { > +part++; > + > +/* handle -pty%d- */ > +if (part == 2) { > +wchar_t *s = s0 + i + 1; > + > +if (s[0] == L'p' && s[1] == L't' && s[2] == L'y' && > +(c = s[3]) && (c >= L'0') && (c <= L'9')) > +{ > +s += 4; > +while ((c = *s) && (c >= L'0') && (c <= L'9')) > +s++; > +if (c != L'-') { > +return 0; > +} > +part++; > + > +{ > +wchar_t expect[] = L"-from-master"; Const it. > +int j; > + > +j = 0; > +while (1) { > +wchar_t e = expect[j]; > +c = s[j]; > +if (!e) { > +return 1; > +} > +if (c != e) { > +return 0; > +} > +j++; > +} > +} > +} else { > +return 0; > +} > +} > +} else if (!(c >= L'a' && c <= L'z') && !(c >= L'0' && c <= > L'9')) { > +return 0; > +} > +} > +} > + > +return 0; > +} > + > +#endif /* __ISCYGTTY_C__ */ > signature.asc Description: OpenPGP digital signature -- 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 v5] Add include/iscygtty.c
On Nov 12 12:27, JonY wrote: > On 11/12/2016 11:57, 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 > > I don't really have any big objections other than on style. 1. This should be *strictly* non-Cygwin/non-MSYS. Only native Windows applications will have a problem to recognize Cygwin ptys, thus this functions makes no sense at all for applications linked against Cygwin or MSYS. 2. Why include a non-standard API? Why not provide this as isatty function as a replacement for the system isatty? I'd wager your boots on this function going mostly unused, otherwise. Corinna signature.asc Description: PGP signature -- 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 v5] Add include/iscygtty.c
On Nov 12, 2016 1:30 PM, "Corinna Vinschen" wrote: > > On Nov 12 12:27, JonY wrote: > > On 11/12/2016 11:57, 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 > > > > I don't really have any big objections other than on style. > > 1. This should be *strictly* non-Cygwin/non-MSYS. Only native Windows >applications will have a problem to recognize Cygwin ptys, thus this >functions makes no sense at all for applications linked against >Cygwin or MSYS. > > 2. Why include a non-standard API? Why not provide this as isatty >function as a replacement for the system isatty? I'd wager your >boots on this function going mostly unused, otherwise. MSYS2 will use it extensively. > > > Corinna > > -- > 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
Re: [Mingw-w64-public] [PATCH v5] Add include/iscygtty.c
On Nov 12 14:52, Ray Donnelly wrote: > On Nov 12, 2016 1:30 PM, "Corinna Vinschen" wrote: > > > > On Nov 12 12:27, JonY wrote: > > > On 11/12/2016 11:57, 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 > > > > > > I don't really have any big objections other than on style. > > > > 1. This should be *strictly* non-Cygwin/non-MSYS. Only native Windows > >applications will have a problem to recognize Cygwin ptys, thus this > >functions makes no sense at all for applications linked against > >Cygwin or MSYS. > > > > 2. Why include a non-standard API? Why not provide this as isatty > >function as a replacement for the system isatty? I'd wager your > >boots on this function going mostly unused, otherwise. > > MSYS2 will use it extensively. If MSYS2 uses it there's something wrong. MSYS2 is Cygwin, so it doesn't need this function at all; It has everything already builtin. Again: Only non-Cygwin applications will need this function. Corinna signature.asc Description: PGP signature -- 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 v5] Add include/iscygtty.c
MSYS2 is a software distribution with both (extremely) Cygwin-like and also native Windows software, all (usually) launched from mintty. On Nov 12, 2016 3:41 PM, "Corinna Vinschen" wrote: > On Nov 12 14:52, Ray Donnelly wrote: > > On Nov 12, 2016 1:30 PM, "Corinna Vinschen" wrote: > > > > > > On Nov 12 12:27, JonY wrote: > > > > On 11/12/2016 11:57, 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 > > > > > > > > I don't really have any big objections other than on style. > > > > > > 1. This should be *strictly* non-Cygwin/non-MSYS. Only native Windows > > >applications will have a problem to recognize Cygwin ptys, thus this > > >functions makes no sense at all for applications linked against > > >Cygwin or MSYS. > > > > > > 2. Why include a non-standard API? Why not provide this as isatty > > >function as a replacement for the system isatty? I'd wager your > > >boots on this function going mostly unused, otherwise. > > > > MSYS2 will use it extensively. > > If MSYS2 uses it there's something wrong. MSYS2 is Cygwin, so it > doesn't need this function at all; It has everything already builtin. > > Again: Only non-Cygwin applications will need this function. > > > Corinna > > > -- > 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
Re: [Mingw-w64-public] [PATCH v5] Add include/iscygtty.c
On Nov 12 23:10, Ray Donnelly wrote: > MSYS2 is a software distribution with both (extremely) Cygwin-like and also > native Windows software, all (usually) launched from mintty. MSYS2 is a slightly tweaked Cygwin DLL in the first place. There are applications linked against it, thus using the POSIX emulation. Those don't need this function at all. And there are applications not linked against it, what we refer to as "native" applications. Those would profit from this function. But that's not the major point. The major point is: 2. Why include a non-standard API? Why not provide this as isatty function as a replacement for the system isatty? I'd wager your boots on this function going mostly unused, otherwise. This would only (correctly) affect native applications, but then *all* of them would see mintty as a tty. Corinna -- 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 v5] Add include/iscygtty.c
On Nov 13 11:01, Corinna Vinschen wrote: > On Nov 12 23:10, Ray Donnelly wrote: > > MSYS2 is a software distribution with both (extremely) Cygwin-like and also > > native Windows software, all (usually) launched from mintty. > > MSYS2 is a slightly tweaked Cygwin DLL in the first place. > > There are applications linked against it, thus using the POSIX > emulation. Those don't need this function at all. > > And there are applications not linked against it, what we refer to as > "native" applications. Those would profit from this function. > > But that's not the major point. The major point is: > > 2. Why include a non-standard API? Why not provide this as isatty > function as a replacement for the system isatty? I'd wager your > boots on this function going mostly unused, otherwise. > > This would only (correctly) affect native applications, but then *all* > of them would see mintty as a tty. s/all of them/all of them using the isatty function/ Corinna signature.asc Description: PGP signature -- 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