Blame gio/gapplicationcommandline.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2010 Codethink Limited
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful, but
Packit ae235b
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gapplicationcommandline.h"
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
#include "gfile.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include "gunixinputstream.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#undef environ
Packit ae235b
#include "gwin32inputstream.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gapplicationcommandline
Packit ae235b
 * @title: GApplicationCommandLine
Packit ae235b
 * @short_description: A command-line invocation of an application
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GApplication
Packit ae235b
 *
Packit ae235b
 * #GApplicationCommandLine represents a command-line invocation of
Packit ae235b
 * an application.  It is created by #GApplication and emitted
Packit ae235b
 * in the #GApplication::command-line signal and virtual function.
Packit ae235b
 *
Packit ae235b
 * The class contains the list of arguments that the program was invoked
Packit ae235b
 * with.  It is also possible to query if the commandline invocation was
Packit ae235b
 * local (ie: the current process is running in direct response to the
Packit ae235b
 * invocation) or remote (ie: some other process forwarded the
Packit ae235b
 * commandline to this process).
Packit ae235b
 *
Packit ae235b
 * The GApplicationCommandLine object can provide the @argc and @argv
Packit ae235b
 * parameters for use with the #GOptionContext command-line parsing API,
Packit ae235b
 * with the g_application_command_line_get_arguments() function. See
Packit ae235b
 * [gapplication-example-cmdline3.c][gapplication-example-cmdline3]
Packit ae235b
 * for an example.
Packit ae235b
 *
Packit ae235b
 * The exit status of the originally-invoked process may be set and
Packit ae235b
 * messages can be printed to stdout or stderr of that process.  The
Packit ae235b
 * lifecycle of the originally-invoked process is tied to the lifecycle
Packit ae235b
 * of this object (ie: the process exits when the last reference is
Packit ae235b
 * dropped).
Packit ae235b
 *
Packit ae235b
 * The main use for #GApplicationCommandLine (and the
Packit ae235b
 * #GApplication::command-line signal) is 'Emacs server' like use cases:
Packit ae235b
 * You can set the `EDITOR` environment variable to have e.g. git use
Packit ae235b
 * your favourite editor to edit commit messages, and if you already
Packit ae235b
 * have an instance of the editor running, the editing will happen
Packit ae235b
 * in the running instance, instead of opening a new one. An important
Packit ae235b
 * aspect of this use case is that the process that gets started by git
Packit ae235b
 * does not return until the editing is done.
Packit ae235b
 *
Packit ae235b
 * Normally, the commandline is completely handled in the
Packit ae235b
 * #GApplication::command-line handler. The launching instance exits
Packit ae235b
 * once the signal handler in the primary instance has returned, and
Packit ae235b
 * the return value of the signal handler becomes the exit status
Packit ae235b
 * of the launching instance.
Packit ae235b
 * |[
Packit ae235b
 * static int
Packit ae235b
 * command_line (GApplication            *application,
Packit ae235b
 *               GApplicationCommandLine *cmdline)
Packit ae235b
 * {
Packit ae235b
 *   gchar **argv;
Packit ae235b
 *   gint argc;
Packit ae235b
 *   gint i;
Packit ae235b
 *
Packit ae235b
 *   argv = g_application_command_line_get_arguments (cmdline, &argc);
Packit ae235b
 *
Packit ae235b
 *   g_application_command_line_print (cmdline,
Packit ae235b
 *                                     "This text is written back\n"
Packit ae235b
 *                                     "to stdout of the caller\n");
Packit ae235b
 *
Packit ae235b
 *   for (i = 0; i < argc; i++)
Packit ae235b
 *     g_print ("argument %d: %s\n", i, argv[i]);
Packit ae235b
 *
Packit ae235b
 *   g_strfreev (argv);
Packit ae235b
 *
Packit ae235b
 *   return 0;
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 * The complete example can be found here: 
Packit ae235b
 * [gapplication-example-cmdline.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-cmdline.c)
Packit ae235b
 *
Packit ae235b
 * In more complicated cases, the handling of the comandline can be
Packit ae235b
 * split between the launcher and the primary instance.
Packit ae235b
 * |[
Packit ae235b
 * static gboolean
Packit ae235b
 *  test_local_cmdline (GApplication   *application,
Packit ae235b
 *                      gchar        ***arguments,
Packit ae235b
 *                      gint           *exit_status)
Packit ae235b
 * {
Packit ae235b
 *   gint i, j;
Packit ae235b
 *   gchar **argv;
Packit ae235b
 *
Packit ae235b
 *   argv = *arguments;
Packit ae235b
 *
Packit ae235b
 *   i = 1;
Packit ae235b
 *   while (argv[i])
Packit ae235b
 *     {
Packit ae235b
 *       if (g_str_has_prefix (argv[i], "--local-"))
Packit ae235b
 *         {
Packit ae235b
 *           g_print ("handling argument %s locally\n", argv[i]);
Packit ae235b
 *           g_free (argv[i]);
Packit ae235b
 *           for (j = i; argv[j]; j++)
Packit ae235b
 *             argv[j] = argv[j + 1];
Packit ae235b
 *         }
Packit ae235b
 *       else
Packit ae235b
 *         {
Packit ae235b
 *           g_print ("not handling argument %s locally\n", argv[i]);
Packit ae235b
 *           i++;
Packit ae235b
 *         }
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   *exit_status = 0;
Packit ae235b
 *
Packit ae235b
 *   return FALSE;
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * static void
Packit ae235b
 * test_application_class_init (TestApplicationClass *class)
Packit ae235b
 * {
Packit ae235b
 *   G_APPLICATION_CLASS (class)->local_command_line = test_local_cmdline;
Packit ae235b
 *
Packit ae235b
 *   ...
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 * In this example of split commandline handling, options that start
Packit ae235b
 * with `--local-` are handled locally, all other options are passed
Packit ae235b
 * to the #GApplication::command-line handler which runs in the primary
Packit ae235b
 * instance.
Packit ae235b
 *
Packit ae235b
 * The complete example can be found here:
Packit ae235b
 * [gapplication-example-cmdline2.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-cmdline2.c)
Packit ae235b
 *
Packit ae235b
 * If handling the commandline requires a lot of work, it may
Packit ae235b
 * be better to defer it.
Packit ae235b
 * |[
Packit ae235b
 * static gboolean
Packit ae235b
 * my_cmdline_handler (gpointer data)
Packit ae235b
 * {
Packit ae235b
 *   GApplicationCommandLine *cmdline = data;
Packit ae235b
 *
Packit ae235b
 *   // do the heavy lifting in an idle
Packit ae235b
 *
Packit ae235b
 *   g_application_command_line_set_exit_status (cmdline, 0);
Packit ae235b
 *   g_object_unref (cmdline); // this releases the application
Packit ae235b
 *
Packit ae235b
 *   return G_SOURCE_REMOVE;
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * static int
Packit ae235b
 * command_line (GApplication            *application,
Packit ae235b
 *               GApplicationCommandLine *cmdline)
Packit ae235b
 * {
Packit ae235b
 *   // keep the application running until we are done with this commandline
Packit ae235b
 *   g_application_hold (application);
Packit ae235b
 *
Packit ae235b
 *   g_object_set_data_full (G_OBJECT (cmdline),
Packit ae235b
 *                           "application", application,
Packit ae235b
 *                           (GDestroyNotify)g_application_release);
Packit ae235b
 *
Packit ae235b
 *   g_object_ref (cmdline);
Packit ae235b
 *   g_idle_add (my_cmdline_handler, cmdline);
Packit ae235b
 *
Packit ae235b
 *   return 0;
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 * In this example the commandline is not completely handled before
Packit ae235b
 * the #GApplication::command-line handler returns. Instead, we keep
Packit ae235b
 * a reference to the #GApplicationCommandLine object and handle it
Packit ae235b
 * later (in this example, in an idle). Note that it is necessary to
Packit ae235b
 * hold the application until you are done with the commandline.
Packit ae235b
 *
Packit ae235b
 * The complete example can be found here:
Packit ae235b
 * [gapplication-example-cmdline3.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-cmdline3.c)
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GApplicationCommandLine:
Packit ae235b
 *
Packit ae235b
 * #GApplicationCommandLine is an opaque data structure and can only be accessed
Packit ae235b
 * using the following functions.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GApplicationCommandLineClass:
Packit ae235b
 *
Packit ae235b
 * The #GApplicationCommandLineClass-struct 
Packit ae235b
 * contains private data only.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_NONE,
Packit ae235b
  PROP_ARGUMENTS,
Packit ae235b
  PROP_OPTIONS,
Packit ae235b
  PROP_PLATFORM_DATA,
Packit ae235b
  PROP_IS_REMOTE
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GApplicationCommandLinePrivate
Packit ae235b
{
Packit ae235b
  GVariant *platform_data;
Packit ae235b
  GVariant *arguments;
Packit ae235b
  GVariant *options;
Packit ae235b
  GVariantDict *options_dict;
Packit ae235b
  gchar *cwd;  /* in GLib filename encoding, not UTF-8 */
Packit ae235b
Packit ae235b
  gchar **environ;
Packit ae235b
  gint exit_status;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_PRIVATE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
/* All subclasses represent remote invocations of some kind. */
Packit ae235b
#define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \
Packit ae235b
                            G_TYPE_APPLICATION_COMMAND_LINE)
Packit ae235b
Packit ae235b
static void
Packit ae235b
grok_platform_data (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
  GVariantIter iter;
Packit ae235b
  const gchar *key;
Packit ae235b
  GVariant *value;
Packit ae235b
Packit ae235b
  g_variant_iter_init (&iter, cmdline->priv->platform_data);
Packit ae235b
Packit ae235b
  while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
Packit ae235b
    if (strcmp (key, "cwd") == 0)
Packit ae235b
      {
Packit ae235b
        if (!cmdline->priv->cwd)
Packit ae235b
          cmdline->priv->cwd = g_variant_dup_bytestring (value, NULL);
Packit ae235b
      }
Packit ae235b
Packit ae235b
    else if (strcmp (key, "environ") == 0)
Packit ae235b
      {
Packit ae235b
        if (!cmdline->priv->environ)
Packit ae235b
          cmdline->priv->environ =
Packit ae235b
            g_variant_dup_bytestring_array (value, NULL);
Packit ae235b
      }
Packit ae235b
Packit ae235b
    else if (strcmp (key, "options") == 0)
Packit ae235b
      {
Packit ae235b
        if (!cmdline->priv->options)
Packit ae235b
          cmdline->priv->options = g_variant_ref (value);
Packit ae235b
      }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
Packit ae235b
                                               const gchar             *message)
Packit ae235b
{
Packit ae235b
  g_print ("%s", message);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
Packit ae235b
                                                  const gchar             *message)
Packit ae235b
{
Packit ae235b
  g_printerr ("%s", message);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GInputStream *
Packit ae235b
g_application_command_line_real_get_stdin (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  return g_unix_input_stream_new (0, FALSE);
Packit ae235b
#else
Packit ae235b
  return g_win32_input_stream_new (GetStdHandle (STD_INPUT_HANDLE), FALSE);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_command_line_get_property (GObject    *object,
Packit ae235b
                                         guint       prop_id,
Packit ae235b
                                         GValue     *value,
Packit ae235b
                                         GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_ARGUMENTS:
Packit ae235b
      g_value_set_variant (value, cmdline->priv->arguments);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_PLATFORM_DATA:
Packit ae235b
      g_value_set_variant (value, cmdline->priv->platform_data);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_IS_REMOTE:
Packit ae235b
      g_value_set_boolean (value, IS_REMOTE (cmdline));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_command_line_set_property (GObject      *object,
Packit ae235b
                                         guint         prop_id,
Packit ae235b
                                         const GValue *value,
Packit ae235b
                                         GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_ARGUMENTS:
Packit ae235b
      g_assert (cmdline->priv->arguments == NULL);
Packit ae235b
      cmdline->priv->arguments = g_value_dup_variant (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_OPTIONS:
Packit ae235b
      g_assert (cmdline->priv->options == NULL);
Packit ae235b
      cmdline->priv->options = g_value_dup_variant (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_PLATFORM_DATA:
Packit ae235b
      g_assert (cmdline->priv->platform_data == NULL);
Packit ae235b
      cmdline->priv->platform_data = g_value_dup_variant (value);
Packit ae235b
      if (cmdline->priv->platform_data != NULL)
Packit ae235b
        grok_platform_data (cmdline);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_command_line_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
Packit ae235b
Packit ae235b
  if (cmdline->priv->options_dict)
Packit ae235b
    g_variant_dict_unref (cmdline->priv->options_dict);
Packit ae235b
Packit ae235b
  if (cmdline->priv->options)
Packit ae235b
      g_variant_unref (cmdline->priv->options);
Packit ae235b
Packit ae235b
  if (cmdline->priv->platform_data)
Packit ae235b
    g_variant_unref (cmdline->priv->platform_data);
Packit ae235b
  if (cmdline->priv->arguments)
Packit ae235b
    g_variant_unref (cmdline->priv->arguments);
Packit ae235b
Packit ae235b
  g_free (cmdline->priv->cwd);
Packit ae235b
  g_strfreev (cmdline->priv->environ);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_application_command_line_parent_class)
Packit ae235b
    ->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_command_line_init (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
  cmdline->priv = g_application_command_line_get_instance_private (cmdline);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_command_line_constructed (GObject *object)
Packit ae235b
{
Packit ae235b
  GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
Packit ae235b
Packit ae235b
  if (IS_REMOTE (cmdline))
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  /* In the local case, set cmd and environ */
Packit ae235b
  if (!cmdline->priv->cwd)
Packit ae235b
    cmdline->priv->cwd = g_get_current_dir ();
Packit ae235b
Packit ae235b
  if (!cmdline->priv->environ)
Packit ae235b
    cmdline->priv->environ = g_get_environ ();
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_command_line_class_init (GApplicationCommandLineClass *class)
Packit ae235b
{
Packit ae235b
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Packit ae235b
Packit ae235b
  object_class->get_property = g_application_command_line_get_property;
Packit ae235b
  object_class->set_property = g_application_command_line_set_property;
Packit ae235b
  object_class->finalize = g_application_command_line_finalize;
Packit ae235b
  object_class->constructed = g_application_command_line_constructed;
Packit ae235b
Packit ae235b
  class->printerr_literal = g_application_command_line_real_printerr_literal;
Packit ae235b
  class->print_literal = g_application_command_line_real_print_literal;
Packit ae235b
  class->get_stdin = g_application_command_line_real_get_stdin;
Packit ae235b
Packit ae235b
  g_object_class_install_property (object_class, PROP_ARGUMENTS,
Packit ae235b
    g_param_spec_variant ("arguments",
Packit ae235b
                          P_("Commandline arguments"),
Packit ae235b
                          P_("The commandline that caused this ::command-line signal emission"),
Packit ae235b
                          G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
Packit ae235b
                          G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                          G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (object_class, PROP_OPTIONS,
Packit ae235b
    g_param_spec_variant ("options",
Packit ae235b
                          P_("Options"),
Packit ae235b
                          P_("The options sent along with the commandline"),
Packit ae235b
                          G_VARIANT_TYPE_VARDICT, NULL, G_PARAM_WRITABLE |
Packit ae235b
                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
Packit ae235b
    g_param_spec_variant ("platform-data",
Packit ae235b
                          P_("Platform data"),
Packit ae235b
                          P_("Platform-specific data for the commandline"),
Packit ae235b
                          G_VARIANT_TYPE ("a{sv}"), NULL,
Packit ae235b
                          G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                          G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (object_class, PROP_IS_REMOTE,
Packit ae235b
    g_param_spec_boolean ("is-remote",
Packit ae235b
                          P_("Is remote"),
Packit ae235b
                          P_("TRUE if this is a remote commandline"),
Packit ae235b
                          FALSE,
Packit ae235b
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_get_arguments:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 * @argc: (out) (optional): the length of the arguments array, or %NULL
Packit ae235b
 *
Packit ae235b
 * Gets the list of arguments that was passed on the command line.
Packit ae235b
 *
Packit ae235b
 * The strings in the array may contain non-UTF-8 data on UNIX (such as
Packit ae235b
 * filenames or arguments given in the system locale) but are always in
Packit ae235b
 * UTF-8 on Windows.
Packit ae235b
 *
Packit ae235b
 * If you wish to use the return value with #GOptionContext, you must
Packit ae235b
 * use g_option_context_parse_strv().
Packit ae235b
 *
Packit ae235b
 * The return value is %NULL-terminated and should be freed using
Packit ae235b
 * g_strfreev().
Packit ae235b
 *
Packit ae235b
 * Returns: (array length=argc) (element-type filename) (transfer full)
Packit ae235b
 *      the string array containing the arguments (the argv)
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
gchar **
Packit ae235b
g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
Packit ae235b
                                          int                     *argc)
Packit ae235b
{
Packit ae235b
  gchar **argv;
Packit ae235b
  gsize len;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
Packit ae235b
Packit ae235b
  argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len;;
Packit ae235b
Packit ae235b
  if (argc)
Packit ae235b
    *argc = len;
Packit ae235b
Packit ae235b
  return argv;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_get_options_dict:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 *
Packit ae235b
 * Gets the options there were passed to g_application_command_line().
Packit ae235b
 *
Packit ae235b
 * If you did not override local_command_line() then these are the same
Packit ae235b
 * options that were parsed according to the #GOptionEntrys added to the
Packit ae235b
 * application with g_application_add_main_option_entries() and possibly
Packit ae235b
 * modified from your GApplication::handle-local-options handler.
Packit ae235b
 *
Packit ae235b
 * If no options were sent then an empty dictionary is returned so that
Packit ae235b
 * you don't need to check for %NULL.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): a #GVariantDict with the options
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 **/
Packit ae235b
GVariantDict *
Packit ae235b
g_application_command_line_get_options_dict (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
Packit ae235b
Packit ae235b
  if (!cmdline->priv->options_dict)
Packit ae235b
    cmdline->priv->options_dict = g_variant_dict_new (cmdline->priv->options);
Packit ae235b
Packit ae235b
  return cmdline->priv->options_dict;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_get_stdin:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 *
Packit ae235b
 * Gets the stdin of the invoking process.
Packit ae235b
 *
Packit ae235b
 * The #GInputStream can be used to read data passed to the standard
Packit ae235b
 * input of the invoking process.
Packit ae235b
 * This doesn't work on all platforms.  Presently, it is only available
Packit ae235b
 * on UNIX when using a DBus daemon capable of passing file descriptors.
Packit ae235b
 * If stdin is not available then %NULL will be returned.  In the
Packit ae235b
 * future, support may be expanded to other platforms.
Packit ae235b
 *
Packit ae235b
 * You must only call this function once per commandline invocation.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GInputStream for stdin
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 **/
Packit ae235b
GInputStream *
Packit ae235b
g_application_command_line_get_stdin (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
  return G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)->get_stdin (cmdline);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_get_cwd:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 *
Packit ae235b
 * Gets the working directory of the command line invocation.
Packit ae235b
 * The string may contain non-utf8 data.
Packit ae235b
 *
Packit ae235b
 * It is possible that the remote application did not send a working
Packit ae235b
 * directory, so this may be %NULL.
Packit ae235b
 *
Packit ae235b
 * The return value should not be modified or freed and is valid for as
Packit ae235b
 * long as @cmdline exists.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable) (type filename): the current directory, or %NULL
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
const gchar *
Packit ae235b
g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
  return cmdline->priv->cwd;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_get_environ:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 *
Packit ae235b
 * Gets the contents of the 'environ' variable of the command line
Packit ae235b
 * invocation, as would be returned by g_get_environ(), ie as a
Packit ae235b
 * %NULL-terminated list of strings in the form 'NAME=VALUE'.
Packit ae235b
 * The strings may contain non-utf8 data.
Packit ae235b
 *
Packit ae235b
 * The remote application usually does not send an environment.  Use
Packit ae235b
 * %G_APPLICATION_SEND_ENVIRONMENT to affect that.  Even with this flag
Packit ae235b
 * set it is possible that the environment is still not available (due
Packit ae235b
 * to invocation messages from other applications).
Packit ae235b
 *
Packit ae235b
 * The return value should not be modified or freed and is valid for as
Packit ae235b
 * long as @cmdline exists.
Packit ae235b
 *
Packit ae235b
 * See g_application_command_line_getenv() if you are only interested
Packit ae235b
 * in the value of a single environment variable.
Packit ae235b
 *
Packit ae235b
 * Returns: (array zero-terminated=1) (element-type filename) (transfer none):
Packit ae235b
 *     the environment strings, or %NULL if they were not sent
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
const gchar * const *
Packit ae235b
g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
  return (const gchar **)cmdline->priv->environ;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_getenv:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 * @name: (type filename): the environment variable to get
Packit ae235b
 *
Packit ae235b
 * Gets the value of a particular environment variable of the command
Packit ae235b
 * line invocation, as would be returned by g_getenv().  The strings may
Packit ae235b
 * contain non-utf8 data.
Packit ae235b
 *
Packit ae235b
 * The remote application usually does not send an environment.  Use
Packit ae235b
 * %G_APPLICATION_SEND_ENVIRONMENT to affect that.  Even with this flag
Packit ae235b
 * set it is possible that the environment is still not available (due
Packit ae235b
 * to invocation messages from other applications).
Packit ae235b
 *
Packit ae235b
 * The return value should not be modified or freed and is valid for as
Packit ae235b
 * long as @cmdline exists.
Packit ae235b
 *
Packit ae235b
 * Returns: the value of the variable, or %NULL if unset or unsent
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
const gchar *
Packit ae235b
g_application_command_line_getenv (GApplicationCommandLine *cmdline,
Packit ae235b
                                   const gchar             *name)
Packit ae235b
{
Packit ae235b
  gint length = strlen (name);
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  /* TODO: expand on windows */
Packit ae235b
  if (cmdline->priv->environ)
Packit ae235b
    for (i = 0; cmdline->priv->environ[i]; i++)
Packit ae235b
      if (strncmp (cmdline->priv->environ[i], name, length) == 0 &&
Packit ae235b
          cmdline->priv->environ[i][length] == '=')
Packit ae235b
        return cmdline->priv->environ[i] + length + 1;
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_get_is_remote:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 *
Packit ae235b
 * Determines if @cmdline represents a remote invocation.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the invocation was remote
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
  return IS_REMOTE (cmdline);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_print:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 * @format: a printf-style format string
Packit ae235b
 * @...: arguments, as per @format
Packit ae235b
 *
Packit ae235b
 * Formats a message and prints it using the stdout print handler in the
Packit ae235b
 * invoking process.
Packit ae235b
 *
Packit ae235b
 * If @cmdline is a local invocation then this is exactly equivalent to
Packit ae235b
 * g_print().  If @cmdline is remote then this is equivalent to calling
Packit ae235b
 * g_print() in the invoking process.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_application_command_line_print (GApplicationCommandLine *cmdline,
Packit ae235b
                                  const gchar             *format,
Packit ae235b
                                  ...)
Packit ae235b
{
Packit ae235b
  gchar *message;
Packit ae235b
  va_list ap;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
Packit ae235b
  g_return_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  va_start (ap, format);
Packit ae235b
  message = g_strdup_vprintf (format, ap);
Packit ae235b
  va_end (ap);
Packit ae235b
Packit ae235b
  G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
Packit ae235b
    ->print_literal (cmdline, message);
Packit ae235b
  g_free (message);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_printerr:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 * @format: a printf-style format string
Packit ae235b
 * @...: arguments, as per @format
Packit ae235b
 *
Packit ae235b
 * Formats a message and prints it using the stderr print handler in the
Packit ae235b
 * invoking process.
Packit ae235b
 *
Packit ae235b
 * If @cmdline is a local invocation then this is exactly equivalent to
Packit ae235b
 * g_printerr().  If @cmdline is remote then this is equivalent to
Packit ae235b
 * calling g_printerr() in the invoking process.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_application_command_line_printerr (GApplicationCommandLine *cmdline,
Packit ae235b
                                     const gchar             *format,
Packit ae235b
                                     ...)
Packit ae235b
{
Packit ae235b
  gchar *message;
Packit ae235b
  va_list ap;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
Packit ae235b
  g_return_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  va_start (ap, format);
Packit ae235b
  message = g_strdup_vprintf (format, ap);
Packit ae235b
  va_end (ap);
Packit ae235b
Packit ae235b
  G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
Packit ae235b
    ->printerr_literal (cmdline, message);
Packit ae235b
  g_free (message);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_set_exit_status:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 * @exit_status: the exit status
Packit ae235b
 *
Packit ae235b
 * Sets the exit status that will be used when the invoking process
Packit ae235b
 * exits.
Packit ae235b
 *
Packit ae235b
 * The return value of the #GApplication::command-line signal is
Packit ae235b
 * passed to this function when the handler returns.  This is the usual
Packit ae235b
 * way of setting the exit status.
Packit ae235b
 *
Packit ae235b
 * In the event that you want the remote invocation to continue running
Packit ae235b
 * and want to decide on the exit status in the future, you can use this
Packit ae235b
 * call.  For the case of a remote invocation, the remote process will
Packit ae235b
 * typically exit when the last reference is dropped on @cmdline.  The
Packit ae235b
 * exit status of the remote process will be equal to the last value
Packit ae235b
 * that was set with this function.
Packit ae235b
 *
Packit ae235b
 * In the case that the commandline invocation is local, the situation
Packit ae235b
 * is slightly more complicated.  If the commandline invocation results
Packit ae235b
 * in the mainloop running (ie: because the use-count of the application
Packit ae235b
 * increased to a non-zero value) then the application is considered to
Packit ae235b
 * have been 'successful' in a certain sense, and the exit status is
Packit ae235b
 * always zero.  If the application use count is zero, though, the exit
Packit ae235b
 * status of the local #GApplicationCommandLine is used.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
Packit ae235b
                                            int                      exit_status)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
Packit ae235b
Packit ae235b
  cmdline->priv->exit_status = exit_status;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_get_exit_status:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 *
Packit ae235b
 * Gets the exit status of @cmdline.  See
Packit ae235b
 * g_application_command_line_set_exit_status() for more information.
Packit ae235b
 *
Packit ae235b
 * Returns: the exit status
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
int
Packit ae235b
g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
Packit ae235b
Packit ae235b
  return cmdline->priv->exit_status;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_get_platform_data:
Packit ae235b
 * @cmdline: #GApplicationCommandLine
Packit ae235b
 *
Packit ae235b
 * Gets the platform data associated with the invocation of @cmdline.
Packit ae235b
 *
Packit ae235b
 * This is a #GVariant dictionary containing information about the
Packit ae235b
 * context in which the invocation occurred.  It typically contains
Packit ae235b
 * information like the current working directory and the startup
Packit ae235b
 * notification ID.
Packit ae235b
 *
Packit ae235b
 * For local invocation, it will be %NULL.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): the platform data, or %NULL
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
GVariant *
Packit ae235b
g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
Packit ae235b
Packit ae235b
  if (cmdline->priv->platform_data)
Packit ae235b
    return g_variant_ref (cmdline->priv->platform_data);
Packit ae235b
  else
Packit ae235b
      return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_application_command_line_create_file_for_arg:
Packit ae235b
 * @cmdline: a #GApplicationCommandLine
Packit ae235b
 * @arg: (type filename): an argument from @cmdline
Packit ae235b
 *
Packit ae235b
 * Creates a #GFile corresponding to a filename that was given as part
Packit ae235b
 * of the invocation of @cmdline.
Packit ae235b
 *
Packit ae235b
 * This differs from g_file_new_for_commandline_arg() in that it
Packit ae235b
 * resolves relative pathnames using the current working directory of
Packit ae235b
 * the invoking process rather than the local process.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a new #GFile
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 **/
Packit ae235b
GFile *
Packit ae235b
g_application_command_line_create_file_for_arg (GApplicationCommandLine *cmdline,
Packit ae235b
                                                const gchar             *arg)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (arg != NULL, NULL);
Packit ae235b
Packit ae235b
  if (cmdline->priv->cwd)
Packit ae235b
    return g_file_new_for_commandline_arg_and_cwd (arg, cmdline->priv->cwd);
Packit ae235b
Packit ae235b
  g_warning ("Requested creation of GFile for commandline invocation that did not send cwd. "
Packit ae235b
             "Using cwd of local process to resolve relative path names.");
Packit ae235b
Packit ae235b
  return g_file_new_for_commandline_arg (arg);
Packit ae235b
}