<commit_before>/* client.cc - gpgex assuan client implementation
   Copyright (C) 2007, 2008, 2013, 2014 g10 Code GmbH

   This file is part of GpgEX.

   GpgEX is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   GpgEX 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 Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.  */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <vector>
#include <string>
#include <stdexcept>

using std::vector;
using std::string;

#include <winsock2.h>
#include <windows.h>

#include <assuan.h>

#include "main.h"
#include "registry.h"
#include "exechelp.h"

#include "client.h"

static inline char *
_gpgex_stpcpy (char *a, const char *b)
{
  while (*b)
    *a++ = *b++;
  *a = 0;
  return a;
}
#define stpcpy(a,b) _gpgex_stpcpy ((a), (b))




static const char *
default_socket_name (void)
{
  static string name;

  if (name.size () == 0)
    {
      char *dir = NULL;

      dir = default_homedir ();
      if (dir)
	{
	  try { name = ((string) dir) + "\\S.uiserver"; } catch (...) {}
	  free ((void *) dir);
	}
    }

  return name.c_str ();
}


/* Return the name of the default UI server.  This name is used to
   auto start an UI server if an initial connect failed.  */
static const char *
default_uiserver_cmdline (void)
{
  static char *name;

  if (!name)
    {
      const char *dir;
      char *uiserver, *p;
      int extra_arglen = 0;

      dir = gpgex_server::root_dir;
      if (!dir)
        return NULL;

      uiserver = read_w32_registry_string (NULL, REGKEY, "UI Server");
      if (!uiserver)
        {
          uiserver = strdup ("kleopatra.exe");
          if (!uiserver)
            return NULL;
          extra_arglen = 9; /* Space required for " --daemon".  */
        }

      name = (char*)malloc (strlen (dir) + strlen (uiserver) + extra_arglen +2);
      if (!name)
        {
          free (uiserver);
          return NULL;
        }
      strcpy (stpcpy (stpcpy (name, dir), "\\"), uiserver);
      for (p = name; *p; p++)
        if (*p == '/')
          *p = '\\';
      free (uiserver);
      gpgex_server::ui_server = "Kleopatra";
      if (extra_arglen && access (name, F_OK))
        {
          /* Kleopatra is not installed: Try GPA instead but if it is
             also not available return the Kleopatra filename.  */
          const char gpaserver[] = "gpa.exe";
          char *name2;

          name2 = (char*)malloc (strlen (dir) + strlen (gpaserver)
                                 + extra_arglen+2);
          if (name2)
            {
              strcpy (stpcpy (stpcpy (name2, dir), "\\"), gpaserver);
              for (p = name2; *p; p++)
                if (*p == '/')
                  *p = '\\';
              if (access (name2, F_OK ))
                free (name2);
              else
                {
                  free (name);
                  name = name2;
                  gpgex_server::ui_server = "GPA";
                }
            }
        }

      /* Append the --daemon arg unless the server name has been taken
         from the Registry.  */<commit_msg>Start launch-gpa to avoid pop up console windows.<commit_after>/* client.cc - gpgex assuan client implementation
   Copyright (C) 2007, 2008, 2013, 2014 g10 Code GmbH

   This file is part of GpgEX.

   GpgEX is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   GpgEX 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 Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.  */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <vector>
#include <string>
#include <stdexcept>

using std::vector;
using std::string;

#include <winsock2.h>
#include <windows.h>

#include <assuan.h>

#include "main.h"
#include "registry.h"
#include "exechelp.h"

#include "client.h"

static inline char *
_gpgex_stpcpy (char *a, const char *b)
{
  while (*b)
    *a++ = *b++;
  *a = 0;
  return a;
}
#define stpcpy(a,b) _gpgex_stpcpy ((a), (b))




static const char *
default_socket_name (void)
{
  static string name;

  if (name.size () == 0)
    {
      char *dir = NULL;

      dir = default_homedir ();
      if (dir)
	{
	  try { name = ((string) dir) + "\\S.uiserver"; } catch (...) {}
	  free ((void *) dir);
	}
    }

  return name.c_str ();
}


/* Return the name of the default UI server.  This name is used to
   auto start an UI server if an initial connect failed.  */
static const char *
default_uiserver_cmdline (void)
{
  static char *name;

  if (!name)
    {
      const char *dir;
      char *uiserver, *p;
      int extra_arglen = 0;

      dir = gpgex_server::root_dir;
      if (!dir)
        return NULL;

      uiserver = read_w32_registry_string (NULL, REGKEY, "UI Server");
      if (!uiserver)
        {
          uiserver = strdup ("kleopatra.exe");
          if (!uiserver)
            return NULL;
          extra_arglen = 9; /* Space required for " --daemon".  */
        }

      name = (char*)malloc (strlen (dir) + strlen (uiserver) + extra_arglen +2);
      if (!name)
        {
          free (uiserver);
          return NULL;
        }
      strcpy (stpcpy (stpcpy (name, dir), "\\"), uiserver);
      for (p = name; *p; p++)
        if (*p == '/')
          *p = '\\';
      free (uiserver);
      gpgex_server::ui_server = "Kleopatra";
      if (extra_arglen && access (name, F_OK))
        {
          /* Kleopatra is not installed: Try GPA instead but if it is
             also not available return the Kleopatra filename.  */
          const char gpaserver[] = "launch-gpa.exe";
          char *name2;

          name2 = (char*)malloc (strlen (dir) + strlen (gpaserver)
                                 + extra_arglen+2);
          if (name2)
            {
              strcpy (stpcpy (stpcpy (name2, dir), "\\"), gpaserver);
              for (p = name2; *p; p++)
                if (*p == '/')
                  *p = '\\';
              if (access (name2, F_OK ))
                free (name2);
              else
                {
                  free (name);
                  name = name2;
                  gpgex_server::ui_server = "GPA";
                }
            }
        }

      /* Append the --daemon arg unless the server name has been taken
         from the Registry.  */
{'fraction_non_alphanumeric': 0.11540041067761807, 'fraction_numerical': 0.012867898699520876, 'mean_word_length': 2.0004106776180697, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2793629', 'n_tokens_mistral': 2400, 'n_tokens_neox': 2262, 'n_words': 797}