Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* Re: [RFC 1/9] Unify windows specifics into common/windows-hdep files
       [not found] <00a201cbeed2$a924dfa0$fb6e9ee0$%muller@ics-cnrs.unistra.fr>
@ 2011-03-30 19:47 ` Eli Zaretskii
  2011-03-31 11:23   ` Pierre Muller
       [not found]   ` <003f01cbef22$038bef20$0aa3cd60$%muller@ics-cnrs.unistra.fr>
  0 siblings, 2 replies; 6+ messages in thread
From: Eli Zaretskii @ 2011-03-30 19:47 UTC (permalink / raw)
  To: Pierre Muller; +Cc: gdb-patches

> From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
> Date: Wed, 30 Mar 2011 14:04:45 +0200
> 
>   This unifies windows path handling routines and
> macros related to use of ASCII or WIDE version of Windows API.
                           ^^^^^
You mean "ANSI", I presume.

> +/* The primary purpose of this file is to implement conversion between
> +   POSIX style and Windows NATIVE style path and path lists.

GNU coding standards frown on using "path" for anything but PATH-style
lists of directories.  You should use "file name" instead.

Same comment on the "path" part in the name of the functions you
coded.

> +int
> +windows_conv_path (conv_type oper, const void *from, void *to, int size)
> +{
> +#ifdef USE_MINGW_CONV
> +  if (size == 0)
> +    {
> +#ifdef USE_WIDE_WINAPI
> +      if (oper == WINDOWS_POSIX_TO_NATIVE_W)
> +	return MultiByteToWideChar (CP_ACP, 0, from, -1, to, 0);
> +      if (oper == WINDOWS_NATIVE_W_TO_POSIX)
> +	return WideCharToMultiByte (CP_ACP, 0, from, -1, to, 0, 0, 0);
> +      else
> +#endif /* USE_WIDE_WINAPI */

Hmm... I'm not sure I understand the intent.  Conversion from UTF-16
to the ANSI codepage is by definition lossy; what do you expect to
come out of this conversion in those cases?  And you don't even use
the WC_NO_BEST_FIT_CHARS flag (as MSDN says you should when converting
file names), nor check for errors with GetLastError.  It looks like
this can never work reliably, or am I missing something?

> +      int len = strlen((const char *) from) + 1;
> +#ifdef USE_WIDE_WINAPI
> +      if (oper == WINDOWS_POSIX_TO_NATIVE_W)
> +	return MultiByteToWideChar (CP_ACP, 0, from, len, to, size);
> +      if (oper == WINDOWS_NATIVE_W_TO_POSIX)
> +	return WideCharToMultiByte (CP_ACP, 0, from, len, to, size, 0, 0);
> +      else
> +#endif /* USE_WIDE_WINAPI */
> +        {
> +	  if (size < len)
> +	    len = size;
> +	  strncpy ((char *) to, (const char *) from, len);
> +	  if (oper == WINDOWS_NATIVE_TO_MSYS)
> +	    {
> +	      char * p;
> +	      p = strchr (to, '\\');
> +	      while (p)
> +		{
> +		  *p = '/';
> +		  p = strchr (to, '\\');
> +		}

Here, I don't understand why you flip the slashes only when `oper' is
something other that WINDOWS_POSIX_TO_NATIVE_W or
WINDOWS_NATIVE_W_TO_POSIX.  Why not flip the slashes together with
converting from multibyte to wide characters and back?

> +	      if (((char *) to)[1] == ':' && ((char *) to)[2] == '/')

Is it guaranteed that `to' is at least 2 characters long?  What if
it's only 1 character long?

> +      return cygwin_conv_to_full_posix_path (from, to);
> +      break;

Why do we need a `break' after a `return'?

> +/* Analogeous function for PATH style lists.  */
      ^^^^^^^^^^
A typo.

> +   GDB with wide char use even on systems for which the default is to
> +   use ASCII chars.  */
          ^^^^^
"ANSI".

> +#undef _G
> +/* Macro _G_SUFFIX(text) expands either to 'text "A"' or to 'text "W"'
> +   depending on the use of wide chars or not.  */
> +#undef _G_SUFFIX

I think the C Standard says that macros whose name begins with an
underscore and a capital letter are reserved.  Applications should not
use such macros.

> +   Otherwise mingw compiler is assumed, which defaults to use of ascii
> +   WINAPI for now (this may change later).                       ^^^^^

"ANSI".

> +   Default can be overridden either by defining USE_WIDE_WINAPI
> +   to force use of wide API, or by defining USE_ASCII_WINAPI to prevent
> +   use of wide API.  */

But do we actually support that?  AFAIK, to get the wide APIs, you
need to compile with _UNICODE defined.  But we don't have such
definitions anywhere, do we?

> +# define CreateProcess CreateProcessW
> +# define GetSystemDirectory GetSystemDirectoryW
> +# define windows_strlen wcslen

Ouch!  So any API that needs one of the two varieties will need to be
added to this list of #define's?  Is that wise?

> +typedef enum
> +{
> +  WINDOWS_NATIVE_A_TO_POSIX = 1
> +  ,WINDOWS_POSIX_TO_NATIVE_A = 2
> +#ifdef USE_WIDE_WINAPI
> +  ,WINDOWS_NATIVE_W_TO_POSIX = 3
> +  ,WINDOWS_POSIX_TO_NATIVE_W = 4
> +#endif
> +  ,WINDOWS_NATIVE_TO_MSYS = 5

I think this is ugly.  Why cannot you put the commas after the values?

> +   Returns size of converted string in characters or
> +   -1 if there was an error.  */
> +
> +extern int windows_conv_path (conv_type oper, const void *from,
> +			      void *to, int size);

The return value is not -1 when it fails, it's zero, because that's
what MultiByteToWideChar and WideCharToMultiByte return in that case.

> +/* Analogeous function for PATH style lists.  */
      ^^^^^^^^^^
A typo.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [RFC 1/9] Unify windows specifics into common/windows-hdep files
  2011-03-30 19:47 ` [RFC 1/9] Unify windows specifics into common/windows-hdep files Eli Zaretskii
@ 2011-03-31 11:23   ` Pierre Muller
       [not found]   ` <003f01cbef22$038bef20$0aa3cd60$%muller@ics-cnrs.unistra.fr>
  1 sibling, 0 replies; 6+ messages in thread
From: Pierre Muller @ 2011-03-31 11:23 UTC (permalink / raw)
  To: 'Eli Zaretskii'; +Cc: gdb-patches



> -----Message d'origine-----
> De : gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> owner@sourceware.org] De la part de Eli Zaretskii
> Envoyé : mercredi 30 mars 2011 21:39
> À : Pierre Muller
> Cc : gdb-patches@sourceware.org
> Objet : Re: [RFC 1/9] Unify windows specifics into common/windows-hdep
files
> 
> > From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
> > Date: Wed, 30 Mar 2011 14:04:45 +0200
> >
> >   This unifies windows path handling routines and
> > macros related to use of ASCII or WIDE version of Windows API.
>                            ^^^^^
> You mean "ANSI", I presume.

  Yes you are right, I should use the Microsoft API wording of
ANSI versus Unicode.
 
> > +/* The primary purpose of this file is to implement conversion between
> > +   POSIX style and Windows NATIVE style path and path lists.
> 
> GNU coding standards frown on using "path" for anything but PATH-style
> lists of directories.  You should use "file name" instead.

  I have no opinion on that subject,
I just wanted to stay close to the cygwin function names...
 
> Same comment on the "path" part in the name of the functions you
> coded.
> 
> > +int
> > +windows_conv_path (conv_type oper, const void *from, void *to, int
size)
> > +{
> > +#ifdef USE_MINGW_CONV
> > +  if (size == 0)
> > +    {
> > +#ifdef USE_WIDE_WINAPI
> > +      if (oper == WINDOWS_POSIX_TO_NATIVE_W)
> > +	return MultiByteToWideChar (CP_ACP, 0, from, -1, to, 0);
> > +      if (oper == WINDOWS_NATIVE_W_TO_POSIX)
> > +	return WideCharToMultiByte (CP_ACP, 0, from, -1, to, 0, 0, 0);
> > +      else
> > +#endif /* USE_WIDE_WINAPI */
> 
> Hmm... I'm not sure I understand the intent.  Conversion from UTF-16
> to the ANSI codepage is by definition lossy; what do you expect to
> come out of this conversion in those cases?  And you don't even use
> the WC_NO_BEST_FIT_CHARS flag (as MSDN says you should when converting
> file names), nor check for errors with GetLastError.  It looks like
> this can never work reliably, or am I missing something?

  I didn't check those options.
I will need to read their descriptions 
more precisely.
 
> > +      int len = strlen((const char *) from) + 1;
> > +#ifdef USE_WIDE_WINAPI
> > +      if (oper == WINDOWS_POSIX_TO_NATIVE_W)
> > +	return MultiByteToWideChar (CP_ACP, 0, from, len, to, size);
> > +      if (oper == WINDOWS_NATIVE_W_TO_POSIX)
> > +	return WideCharToMultiByte (CP_ACP, 0, from, len, to, size, 0, 0);
> > +      else
> > +#endif /* USE_WIDE_WINAPI */
> > +        {
> > +	  if (size < len)
> > +	    len = size;
> > +	  strncpy ((char *) to, (const char *) from, len);
> > +	  if (oper == WINDOWS_NATIVE_TO_MSYS)
> > +	    {
> > +	      char * p;
> > +	      p = strchr (to, '\\');
> > +	      while (p)
> > +		{
> > +		  *p = '/';
> > +		  p = strchr (to, '\\');
> > +		}
> 
> Here, I don't understand why you flip the slashes only when `oper' is
> something other that WINDOWS_POSIX_TO_NATIVE_W or
> WINDOWS_NATIVE_W_TO_POSIX.  Why not flip the slashes together with
> converting from multibyte to wide characters and back?

 I agree that I also should do the flips on the two above cases...

 
> > +	      if (((char *) to)[1] == ':' && ((char *) to)[2] == '/')
> 
> Is it guaranteed that `to' is at least 2 characters long?  What if
> it's only 1 character long?

  One more missing check, thanks for noticing. 
 
> > +      return cygwin_conv_to_full_posix_path (from, to);
> > +      break;
> 
> Why do we need a `break' after a `return'?
 Missing C code practice :( 
> > +/* Analogeous function for PATH style lists.  */
>       ^^^^^^^^^^
> A typo.

  Apparently, at least I was consistent ...
 
> > +   GDB with wide char use even on systems for which the default is to
> > +   use ASCII chars.  */
>           ^^^^^
> "ANSI".
> 
> > +#undef _G
> > +/* Macro _G_SUFFIX(text) expands either to 'text "A"' or to 'text "W"'
> > +   depending on the use of wide chars or not.  */
> > +#undef _G_SUFFIX
> 
> I think the C Standard says that macros whose name begins with an
> underscore and a capital letter are reserved.  Applications should not
> use such macros.

  But we are also using __USEWIDE before my patch ...
 or do you mean that two underscores are OK?

> > +   Otherwise mingw compiler is assumed, which defaults to use of ascii
> > +   WINAPI for now (this may change later).                       ^^^^^
> 
> "ANSI".
> 
> > +   Default can be overridden either by defining USE_WIDE_WINAPI
> > +   to force use of wide API, or by defining USE_ASCII_WINAPI to prevent
> > +   use of wide API.  */
> 
> But do we actually support that?  AFAIK, to get the wide APIs, you
> need to compile with _UNICODE defined.  But we don't have such
> definitions anywhere, do we?

  I completely remove __USEWIDE and tried to replace it by this new
USE_WIDE_WINAPI.
  Or is this __USEWIDE macro used anywhere else than inside GDB code?

  Should I rather call the macro USE_UNICODE_WINAPI
 (and USE_ANSI_WINAPI to force ANSI version?) 
 
> > +# define CreateProcess CreateProcessW
> > +# define GetSystemDirectory GetSystemDirectoryW
> > +# define windows_strlen wcslen
> 
> Ouch!  So any API that needs one of the two varieties will need to be
> added to this list of #define's?  Is that wise?

  Isn't it better than being forced to use
#ifdef __USEWIDE
  CreateProcessW (...
#else
  CreateProcessA (...
#endif
 
> > +typedef enum
> > +{
> > +  WINDOWS_NATIVE_A_TO_POSIX = 1
> > +  ,WINDOWS_POSIX_TO_NATIVE_A = 2
> > +#ifdef USE_WIDE_WINAPI
> > +  ,WINDOWS_NATIVE_W_TO_POSIX = 3
> > +  ,WINDOWS_POSIX_TO_NATIVE_W = 4
> > +#endif
> > +  ,WINDOWS_NATIVE_TO_MSYS = 5
> 
> I think this is ugly.  Why cannot you put the commas after the values?

  No reason, it's just that I also find it ugly to have a comma
after the last enum (but I am not even forced to do that here, so my
argument is bad...)
 
> > +   Returns size of converted string in characters or
> > +   -1 if there was an error.  */
> > +
> > +extern int windows_conv_path (conv_type oper, const void *from,
> > +			      void *to, int size);
> 
> The return value is not -1 when it fails, it's zero, because that's
> what MultiByteToWideChar and WideCharToMultiByte return in that case.

  Here you are talking about the Mingw version:
I agree that I must check failure of those calls
and return -1 in that case.
 
> > +/* Analogeous function for PATH style lists.  */
>       ^^^^^^^^^^
> A typo.
Agreed

Thanks for the useful comments.

Pierre


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [RFC 1/9] Unify windows specifics into common/windows-hdep files
       [not found]     ` <8362qzq93q.fsf@gnu.org>
@ 2011-04-01  5:13       ` Pierre Muller
       [not found]       ` <002e01cbf02b$7a9cafa0$6fd60ee0$%muller@ics-cnrs.unistra.fr>
  1 sibling, 0 replies; 6+ messages in thread
From: Pierre Muller @ 2011-04-01  5:13 UTC (permalink / raw)
  To: 'Eli Zaretskii'; +Cc: gdb-patches

> > From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
> > Cc: <gdb-patches@sourceware.org>
> > Date: Wed, 30 Mar 2011 23:32:36 +0200
> >
> > > > +#undef _G_SUFFIX
> > >
> > > I think the C Standard says that macros whose name begins with an
> > > underscore and a capital letter are reserved.  Applications should not
> > > use such macros.
> >
> >   But we are also using __USEWIDE before my patch ...
> >  or do you mean that two underscores are OK?
> 
> No, AFAIK macros that begin with two underscores are also reserved.

  But nobody protested for __USEWIDE nor for __PMAX for instance...
But checking gdb directory, it seems that are only a fee macros 
starting with an underscore.
 
> >   I completely remove __USEWIDE and tried to replace it by this new
> > USE_WIDE_WINAPI.
> >   Or is this __USEWIDE macro used anywhere else than inside GDB code?
> 
> I don't think so, but maybe someone else could tell for sure.
> 
> >   Should I rather call the macro USE_UNICODE_WINAPI
> >  (and USE_ANSI_WINAPI to force ANSI version?)
> 
> I don't think it matters what the macro is called.

 I agree with you on this point.

 
> > > > +# define CreateProcess CreateProcessW
> > > > +# define GetSystemDirectory GetSystemDirectoryW
> > > > +# define windows_strlen wcslen
> > >
> > > Ouch!  So any API that needs one of the two varieties will need to be
> > > added to this list of #define's?  Is that wise?
> >
> >   Isn't it better than being forced to use
> > #ifdef __USEWIDE
> >   CreateProcessW (...
> > #else
> >   CreateProcessA (...
> > #endif
> 
> The Windows headers already have the machinery to do all this for you:
> it works by defining _UNICODE.

  But using this macro would require that we are able to 
put of the code that decides if we want to use the Unicode or ASCII version
of the windows header before even including it.
  I am unsure if this is possible...
If you look at the code in common/windows-hdep.c I submitted,
you will see that we need at least to load sys/cygwin.h  and
cygwin/version.h headers
in order to be able to compute the Cygwin DLL version.
  Each might already also include windows.h
meaning than setting _UNICODE at that point would be too late.

  If fact, looking at ChangeLogs and using 'git blame'
it seems like __USEWIDE was only introduced in March 2010 by Christopher
Faylor.
Maybe Christopher could tell us why he decided to use this conditional
instead
of the standard _UNICODE macro.

Pierre


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC 1/9] Unify windows specifics into common/windows-hdep files
       [not found]       ` <002e01cbf02b$7a9cafa0$6fd60ee0$%muller@ics-cnrs.unistra.fr>
@ 2011-04-01  8:39         ` Eli Zaretskii
  2011-04-01  9:24           ` Pierre Muller
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2011-04-01  8:39 UTC (permalink / raw)
  To: Pierre Muller; +Cc: gdb-patches

> From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
> Cc: <gdb-patches@sourceware.org>
> Date: Fri, 1 Apr 2011 07:12:42 +0200
> 
> > > From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
> > > Cc: <gdb-patches@sourceware.org>
> > > Date: Wed, 30 Mar 2011 23:32:36 +0200
> > >
> > > > > +#undef _G_SUFFIX
> > > >
> > > > I think the C Standard says that macros whose name begins with an
> > > > underscore and a capital letter are reserved.  Applications should not
> > > > use such macros.
> > >
> > >   But we are also using __USEWIDE before my patch ...
> > >  or do you mean that two underscores are OK?
> > 
> > No, AFAIK macros that begin with two underscores are also reserved.
> 
>   But nobody protested for __USEWIDE nor for __PMAX for instance...
> But checking gdb directory, it seems that are only a fee macros 
> starting with an underscore.

Again, I think it's bad practice to use such macros, but that's me.
If no one else cares, you can disregard me on that account.

> > > > > +# define CreateProcess CreateProcessW
> > > > > +# define GetSystemDirectory GetSystemDirectoryW
> > > > > +# define windows_strlen wcslen
> > > >
> > > > Ouch!  So any API that needs one of the two varieties will need to be
> > > > added to this list of #define's?  Is that wise?
> > >
> > >   Isn't it better than being forced to use
> > > #ifdef __USEWIDE
> > >   CreateProcessW (...
> > > #else
> > >   CreateProcessA (...
> > > #endif
> > 
> > The Windows headers already have the machinery to do all this for you:
> > it works by defining _UNICODE.
> 
>   But using this macro would require that we are able to 
> put of the code that decides if we want to use the Unicode or ASCII version
> of the windows header before even including it.

Sorry, I'm not following.  What difficulties you envision.

In general, I don't think we should drag into GDB the stuff MinGW
headers do to "transparently" support compile-time selection of ANSI
vs Unicode APIs.  I think we should use the mechanism provided for
that by the headers.  That mechanism is to define _UNICODE.

> If you look at the code in common/windows-hdep.c I submitted,
> you will see that we need at least to load sys/cygwin.h  and
> cygwin/version.h headers
> in order to be able to compute the Cygwin DLL version.
>   Each might already also include windows.h
> meaning than setting _UNICODE at that point would be too late.

I don't know enough about the Cygwin part of this, so maybe you are
right.  But in that case, we should have explicit code that selects
either ..A or ..W variety of the API at run time, instead of hiding
them in a header using #define's.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [RFC 1/9] Unify windows specifics into common/windows-hdep files
  2011-04-01  8:39         ` Eli Zaretskii
@ 2011-04-01  9:24           ` Pierre Muller
  0 siblings, 0 replies; 6+ messages in thread
From: Pierre Muller @ 2011-04-01  9:24 UTC (permalink / raw)
  To: 'Eli Zaretskii'; +Cc: gdb-patches



> -----Message d'origine-----
> De : gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> owner@sourceware.org] De la part de Eli Zaretskii
> Envoyé : vendredi 1 avril 2011 10:39
> À : Pierre Muller
> Cc : gdb-patches@sourceware.org
> Objet : Re: [RFC 1/9] Unify windows specifics into common/windows-hdep
files
> 
> > From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
> > Cc: <gdb-patches@sourceware.org>
> > Date: Fri, 1 Apr 2011 07:12:42 +0200
> >
> > > > From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
> > > > Cc: <gdb-patches@sourceware.org>
> > > > Date: Wed, 30 Mar 2011 23:32:36 +0200
> > > >
> > > > > > +#undef _G_SUFFIX
> > > > >
> > > > > I think the C Standard says that macros whose name begins with an
> > > > > underscore and a capital letter are reserved.  Applications should
> not
> > > > > use such macros.
> > > >
> > > >   But we are also using __USEWIDE before my patch ...
> > > >  or do you mean that two underscores are OK?
> > >
> > > No, AFAIK macros that begin with two underscores are also reserved.
> >
> >   But nobody protested for __USEWIDE nor for __PMAX for instance...
> > But checking gdb directory, it seems that are only a fee macros
> > starting with an underscore.
> 
> Again, I think it's bad practice to use such macros, but that's me.
> If no one else cares, you can disregard me on that account.

  As I know very little about all those rules,
I am perfectly willing to adhere to them.
  It will all depend if we try to use the standard UNICODE
macro.

  By the way, I noticed that in /usr/include/w32api/winnt.h
#ifdef UNICODE
/*
 * NOTE: This tests UNICODE, which is different from the _UNICODE define
 *       used to differentiate standard C runtime calls.
 */
so it seems that we would probably need both UNICODE and _UNICODE
_UNICODE is used solely in /usr/include/mingw/tchar.h header
and defined _tprintf to be either wprintf or printf
and a long list of similar functions.
  But this can only be used in mingw specific code as
those macros do not seem to exist for Cygwin.
 
> > > > > > +# define CreateProcess CreateProcessW
> > > > > > +# define GetSystemDirectory GetSystemDirectoryW
> > > > > > +# define windows_strlen wcslen
> > > > >
> > > > > Ouch!  So any API that needs one of the two varieties will need to
> be
> > > > > added to this list of #define's?  Is that wise?
> > > >
> > > >   Isn't it better than being forced to use
> > > > #ifdef __USEWIDE
> > > >   CreateProcessW (...
> > > > #else
> > > >   CreateProcessA (...
> > > > #endif
> > >
> > > The Windows headers already have the machinery to do all this for you:
> > > it works by defining _UNICODE.

  According to the note above it is UNICODE, not _UNICODE.
> >
> >   But using this macro would require that we are able to
> > put of the code that decides if we want to use the Unicode or ASCII
> version
> > of the windows header before even including it.
> 
> Sorry, I'm not following.  What difficulties you envision.
> 
> In general, I don't think we should drag into GDB the stuff MinGW
> headers do to "transparently" support compile-time selection of ANSI
> vs Unicode APIs.  I think we should use the mechanism provided for
> that by the headers.  That mechanism is to define _UNICODE.

  Here also, it's UNICODE, not _UNICODE.
But the question of being sure that winnt.h header was not included 
before would still be a potential problem...
 
> > If you look at the code in common/windows-hdep.c I submitted,
> > you will see that we need at least to load sys/cygwin.h  and
> > cygwin/version.h headers
> > in order to be able to compute the Cygwin DLL version.
> >   Each might already also include windows.h
> > meaning than setting _UNICODE at that point would be too late. 
> I don't know enough about the Cygwin part of this, so maybe you are
> right.  But in that case, we should have explicit code that selects
> either ..A or ..W variety of the API at run time, instead of hiding
> them in a header using #define's.

  Supporting the two (Unicode and ASCII) versions at run time
would be another patch that might be not so easy to implement...

  I only tried to expand the possibility to use Unicode Windows API version
to use with mingw hosts, and kept most of the macros that
were in windows-nat.c but moved them to common/windows-hdep.h header.

Pierre


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [RFC 1/9] Unify windows specifics into common/windows-hdep files
@ 2011-03-30 12:15 Pierre Muller
  0 siblings, 0 replies; 6+ messages in thread
From: Pierre Muller @ 2011-03-30 12:15 UTC (permalink / raw)
  To: gdb-patches

  This unifies windows path handling routines and
macros related to use of ASCII or WIDE version of Windows API.
  See main email for details.
  
Pierre Muller
GDB pascal language maintainer

ChangeLog entry:

2011-03-30  Pierre Muller  <muller@ics.u-strasbg.fr>

	* common/windows-hdep.h: New file.
	* common/windows-hdep.c: New file.

diff --git a/gdb/common/windows-hdep.c b/gdb/common/windows-hdep.c
new file mode 100644
index 0000000..cbd5a8f
--- /dev/null
+++ b/gdb/common/windows-hdep.c
@@ -0,0 +1,231 @@
+/* Copyright 2011 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
+
+/* The primary purpose of this file is to implement conversion between
+   POSIX style and Windows NATIVE style path and path lists.
+   For this purpose, generic replacement of the two cygwin function
+   cygwin_conv_path and cygwin_conv_path_list are created.  */
+
+#include <winsock2.h>
+#include <windows.h>
+#include <sys/types.h>
+#include <string.h>
+
+#ifndef GDBSERVER
+# include "defs.h"
+# include "gdb_assert.h"
+#else
+# include "server.h"
+#endif
+#include "windows-hdep.h"
+
+int
+windows_conv_path (conv_type oper, const void *from, void *to, int size)
+{
+#ifdef USE_MINGW_CONV
+  if (size == 0)
+    {
+#ifdef USE_WIDE_WINAPI
+      if (oper == WINDOWS_POSIX_TO_NATIVE_W)
+	return MultiByteToWideChar (CP_ACP, 0, from, -1, to, 0);
+      if (oper == WINDOWS_NATIVE_W_TO_POSIX)
+	return WideCharToMultiByte (CP_ACP, 0, from, -1, to, 0, 0, 0);
+      else
+#endif /* USE_WIDE_WINAPI */
+        return (strlen((const char *) from) + 1);
+    }
+  else
+    {
+      int len = strlen((const char *) from) + 1;
+#ifdef USE_WIDE_WINAPI
+      if (oper == WINDOWS_POSIX_TO_NATIVE_W)
+	return MultiByteToWideChar (CP_ACP, 0, from, len, to, size);
+      if (oper == WINDOWS_NATIVE_W_TO_POSIX)
+	return WideCharToMultiByte (CP_ACP, 0, from, len, to, size, 0, 0);
+      else
+#endif /* USE_WIDE_WINAPI */
+        {
+	  if (size < len)
+	    len = size;
+	  strncpy ((char *) to, (const char *) from, len);
+	  if (oper == WINDOWS_NATIVE_TO_MSYS)
+	    {
+	      char * p;
+	      p = strchr (to, '\\');
+	      while (p)
+		{
+		  *p = '/';
+		  p = strchr (to, '\\');
+		}
+	      if (((char *) to)[1] == ':' && ((char *) to)[2] == '/')
+		{
+		  char drive = ((char *) to)[0];
+		  ((char *) to)[0] = '/';
+		  ((char *) to)[1] = drive;
+		}
+
+	    }
+          /* For null termination.  */
+	  if (len == size)
+	    ((char *) to)[size] = '\0';
+	}
+      return len;
+    }
+#endif /* USE_MING_CONV */
+#ifdef USE_NEW_CYGWIN_CONV
+  int op;
+
+  switch (oper)
+    {
+#ifdef USE_WIDE_WINAPI
+     case WINDOWS_NATIVE_W_TO_POSIX:
+      op = CCP_WIN_W_TO_POSIX;
+      break;
+
+    case WINDOWS_POSIX_TO_NATIVE_W:
+      op = CCP_POSIX_TO_WIN_W;
+      break;
+
+#endif /* USE_WIDE_WINAPI */
+ 
+    case WINDOWS_NATIVE_A_TO_POSIX:
+    case WINDOWS_NATIVE_TO_MSYS:
+      op = CCP_WIN_A_TO_POSIX;
+      break;
+
+    case WINDOWS_POSIX_TO_NATIVE_A:
+      op = CCP_POSIX_TO_WIN_A;
+      break;
+    default:
+      {
+	int len = strlen((const char *) from) + 1;
+
+	strncpy ((char *) to, (const char *) from, size);
+	return len;
+      }
+    }
+
+  return (cygwin_conv_path (op, from, to, size));
+#endif /* USE_NEW_CYGWIN_CONV */
+#ifdef USE_OLD_CYGWIN_CONV
+  switch (oper)
+  /* Those two macros:
+     WINDOWS_NATIVE_W_TO_POSIX and WINDOWS_POSIX_TO_NATIVE_W
+     are not defined for old Cygwin, and thus not handled here .  */
+    {
+    case WINDOWS_NATIVE_A_TO_POSIX:
+    case WINDOWS_NATIVE_TO_MSYS:
+      if (size == 0)
+	return __PMAX;
+      gdb_assert (size >= __PMAX);
+      return cygwin_conv_to_full_posix_path (from, to);
+      break;
+
+    case WINDOWS_POSIX_TO_NATIVE_A:
+      if (size == 0)
+	return __PMAX;
+      gdb_assert (size >= __PMAX);
+      return cygwin_conv_to_win32_path (from, to);
+      break;
+    };
+  return -1;
+#endif /* USE_OLD_CYGWIN_CONV */
+}
+
+/* Analogeous function for PATH style lists.  */
+
+int
+windows_conv_path_list (conv_type oper, const void *from, void *to, int
size)
+{
+#ifdef USE_MINGW_CONV
+#ifdef USE_WIDE_WINAPI
+  switch (oper)
+    {
+    case WINDOWS_NATIVE_W_TO_POSIX:
+    case WINDOWS_POSIX_TO_NATIVE_W:
+      return -1;
+    default:
+      ;
+    }
+#endif /* USE_WIDE_WINAPI */
+  if (size == 0)
+    return (strlen((const char *) from) + 1);
+  else
+    {
+      int len = strlen((const char *) from) + 1;
+      if (len > size)
+	len = size;
+      strncpy ((char *) to, (const char *) from, len);
+      /* For null termination.  */
+      if (len == size)
+	((char *) to)[size] = '\0';
+      return len;
+    }
+#endif /* USE_MING_CONV */
+#ifdef USE_NEW_CYGWIN_CONV
+  int op;
+
+  switch (oper)
+    {
+#ifdef USE_WIDE_WINAPI
+     case WINDOWS_NATIVE_W_TO_POSIX:
+      op = CCP_WIN_W_TO_POSIX;
+      break;
+
+    case WINDOWS_POSIX_TO_NATIVE_W:
+      op = CCP_POSIX_TO_WIN_W;
+      break;
+
+#endif /* USE_WIDE_WINAPI */
+
+    case WINDOWS_NATIVE_A_TO_POSIX:
+    case WINDOWS_NATIVE_TO_MSYS:
+      op = CCP_WIN_A_TO_POSIX;
+      break;
+
+    case WINDOWS_POSIX_TO_NATIVE_A:
+      op = CCP_POSIX_TO_WIN_A;
+      break;
+    };
+
+  return (cygwin_conv_path_list (op, from, to, size));
+#endif /* USE_NEW_CYGWIN_CONV */
+#ifdef USE_OLD_CYGWIN_CONV
+  switch (oper)
+  /* Those two macros:
+     WINDOWS_NATIVE_W_TO_POSIX and WINDOWS_POSIX_TO_NATIVE_W
+     are not defined for old Cygwin, and thus not handled here .  */
+    {
+    case WINDOWS_NATIVE_A_TO_POSIX:
+    case WINDOWS_NATIVE_TO_MSYS:
+      if (size == 0)
+	return cygwin_win32_to_posix_path_list_buf_size (from);
+      gdb_assert (size >= cygwin_win32_to_posix_path_list_buf_size (from));
+      return cygwin_win32_to_posix_path_list (from, to);
+      break;
+
+    case WINDOWS_POSIX_TO_NATIVE_A:
+      if (size == 0)
+         return cygwin_posix_to_win32_path_list_buf_size (from);
+      gdb_assert (size >= cygwin_posix_to_win32_path_list_buf_size (from));
+      return cygwin_posix_to_win32_path_list (from, to);
+      break;
+    };
+  return -1;
+#endif /* USE_OLD_CYGWIN_CONV */
+}
+
diff --git a/gdb/common/windows-hdep.h b/gdb/common/windows-hdep.h
new file mode 100644
index 0000000..0514835
--- /dev/null
+++ b/gdb/common/windows-hdep.h
@@ -0,0 +1,173 @@
+/* Copyright 2011 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
+
+/* The primary purpose of this file is to separate out
+   conversion between POSIX style and Windows NATIVE style
+   path and path lists.
+   For this purpose, generic replacement of the two cygwin function
+   cygwin_conv_path and cygwin_conv_path_list are created.
+
+   The header also declares a substitute of sleep function
+   if that function is not included in the system headers.  */
+
+#ifndef WINDOWS_HDEP_H
+#define WINDOWS_HDEP_H
+
+/* The following macros are redefined below depending on the use of wide
+   chars or not.  */
+#undef STARTUPINFO
+#undef CreateProcess
+#undef GetModuleFileNameEx
+#undef GetSystemDirectory
+/* Macro _G(text) either expands to simply text or to L##text depending
+   on use of wide chars or not.
+   Although this is similar to _T macro inside winnt.h header,
+   its value is set according to USE_WIDE_API macro, allowing to compile
+   GDB with wide char use even on systems for which the default is to
+   use ASCII chars.  */
+#undef _G
+/* Macro _G_SUFFIX(text) expands either to 'text "A"' or to 'text "W"'
+   depending on the use of wide chars or not.  */
+#undef _G_SUFFIX
+/* Macro LPGSTR holds either LCSTR or LPWSTR.  */
+#undef LPGSTR
+/* Type win_buf_t is also set to char or wchar_t. */
+
+/* The use of wide char for the above function is dependent of the
definition
+   of the macro USE_WIDE_WINAPI.
+
+   For Cygwin compiler, the default is wide API for the newer cygwin
+   version 1.7.X and above; and ascii API for older 1.5.X versions.
+
+   Otherwise mingw compiler is assumed, which defaults to use of ascii
+   WINAPI for now (this may change later).
+
+   Default can be overridden either by defining USE_WIDE_WINAPI
+   to force use of wide API, or by defining USE_ASCII_WINAPI to prevent
+   use of wide API.  */
+
+
+/* CYGWIN part */
+#ifdef __CYGWIN__
+#ifdef PATH_MAX
+# define __PMAX	PATH_MAX
+#else
+# include <sys/param.h>
+# define __PMAX MAXPATHLEN
+#endif
+/* For new Cygwin 1.7.X versions, use WIDE version of Windows API functions
+   by default, unless USE_ASCII_WINAPI is defined.
+   For older Cygwin 1.5.X versionsd, or for MingwXX systems, default to use
+   the Ansi versions.  */
+#include <sys/cygwin.h>
+#include <cygwin/version.h>
+# if
CYGWIN_VERSION_DLL_MAKE_COMBINED(CYGWIN_VERSION_API_MAJOR,CYGWIN_VERSION_API
_MINOR) >= 181
+/* Conditional set to use the newer cygwin_conv_path function.  */
+#  define USE_NEW_CYGWIN_CONV
+/* Set macro USE_WIDE_WINAPI by default, unless USE_ASCII_WINAPI is set
+   to disable its use.  */
+#   ifndef USE_ASCII_WINAPI
+#     define USE_WIDE_WINAPI
+#   endif /* not USE_ASCII_WINAPI  */
+# else
+/*  For older 1.5.X Cygwin versions, use cygwin_conv_to_XXX functions
+    to emulate newer cygwin_conv_path function. These function are only
+    implemented for ascii strings.  */
+#  define USE_OLD_CYGWIN_CONV
+#  ifdef USE_WIDE_WINAPI
+#    error "Old Cygwin 1.5.X versions do no support WWide string
conversions"
+#  endif
+#  define CW_SET_DOS_FILE_WARNING -1	/* no-op this for older Cygwin */
+# endif
+#else /* not __CYGWIN__ */
+/* We assume that no __CYGWIN__ macro means that we are using a
+   mingw compiler.  */
+# define __PMAX	(MAX_PATH + 1)
+# define USE_MINGW_CONV
+#endif /* not __CYGWIN__ */
+
+
+/* Using wide WINAPI.  */
+#ifdef USE_WIDE_WINAPI
+# define _G(text) L##text
+# define _G_SUFFIX(text) (text "W")
+# define LPGSTR LPWSTR
+# define WINDOWS_POSIX_TO_NATIVE WINDOWS_POSIX_TO_NATIVE_W
+# define WINDOWS_NATIVE_TO_POSIX WINDOWS_NATIVE_W_TO_POSIX
+  typedef wchar_t win_buf_t;
+# define STARTUPINFO STARTUPINFOW
+# define CreateProcess CreateProcessW
+# define GetSystemDirectory GetSystemDirectoryW
+# define windows_strlen wcslen
+# define windows_a_to_g_strlen(ascii_str) mbstowcs (NULL, ascii_str, 0)
+# define windows_a_to_g_strncpy(gdb_str, ascii_str, len) \
+				mbstowcs (gdb_str, ascii_str, len)
+# define windows_strcat wcscat
+# define windows_strcpy wcscpy
+/* Mingw has a special swprintf function without length argument.  */
+# ifndef __CYGWIN__
+#  define swprintf _snwprintf
+#endif
+#else /* not USE_WIDE_WINAPI */
+/* Using ascii WINAPI.  */
+# define _G(text) text
+# define _G_SUFFIX(text) (text "A")
+# define LPGSTR LPSTR
+# define WINDOWS_POSIX_TO_NATIVE WINDOWS_POSIX_TO_NATIVE_A
+# define WINDOWS_NATIVE_TO_POSIX WINDOWS_NATIVE_A_TO_POSIX
+  typedef char win_buf_t;
+# define STARTUPINFO STARTUPINFOA
+# define CreateProcess CreateProcessA
+# define GetSystemDirectory GetSystemDirectoryA
+# define windows_strlen strlen
+# define windows_a_to_g_strlen(ascii_str) strlen (ascii_str)
+# define windows_a_to_g_strncpy(gdb_str, ascii_str, len) \
+				strncpy (gdb_str, ascii_str, len)
+# define windows_strcat strcat
+# define windows_strcpy strcpy
+#endif /* not USE_WIDE_WINAPI */
+
+
+typedef enum
+{
+  WINDOWS_NATIVE_A_TO_POSIX = 1
+  ,WINDOWS_POSIX_TO_NATIVE_A = 2
+#ifdef USE_WIDE_WINAPI
+  ,WINDOWS_NATIVE_W_TO_POSIX = 3
+  ,WINDOWS_POSIX_TO_NATIVE_W = 4
+#endif
+  ,WINDOWS_NATIVE_TO_MSYS = 5
+} conv_type;
+
+/* GDB mapping to cygwin type cygwin_conv_path.
+   If SIZE is zero, return required size (in number of characters)
+   to store the converted string FROM.
+   if SIZE is non-zero, convert FROM according to OPER value,
+   respecting SIZE limit.
+   Returns size of converted string in characters or
+   -1 if there was an error.  */
+
+extern int windows_conv_path (conv_type oper, const void *from,
+			      void *to, int size);
+
+/* Analogeous function for PATH style lists.  */
+
+extern int windows_conv_path_list (conv_type oper, const void *from,
+				   void *to, int size);
+
+#endif /* WINDOWS_HDEP_H */
+
-- 
1.7.4


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-04-01  9:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <00a201cbeed2$a924dfa0$fb6e9ee0$%muller@ics-cnrs.unistra.fr>
2011-03-30 19:47 ` [RFC 1/9] Unify windows specifics into common/windows-hdep files Eli Zaretskii
2011-03-31 11:23   ` Pierre Muller
     [not found]   ` <003f01cbef22$038bef20$0aa3cd60$%muller@ics-cnrs.unistra.fr>
     [not found]     ` <8362qzq93q.fsf@gnu.org>
2011-04-01  5:13       ` Pierre Muller
     [not found]       ` <002e01cbf02b$7a9cafa0$6fd60ee0$%muller@ics-cnrs.unistra.fr>
2011-04-01  8:39         ` Eli Zaretskii
2011-04-01  9:24           ` Pierre Muller
2011-03-30 12:15 Pierre Muller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox