2 * Copyright © 2010 Codethink Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
19 * Authors: Ryan Lortie <desrt@desrt.ca>
24 #include "gapplicationcommandline.h"
31 G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJECT)
34 * SECTION:gapplicationcommandline
35 * @title: GApplicationCommandLine
36 * @short_description: A command-line invocation of an application
37 * @see_also: #GApplication
39 * #GApplicationCommandLine represents a command-line invocation of
40 * an application. It is created by #GApplication and emitted
41 * in the #GApplication::command-line signal and virtual function.
43 * The class contains the list of arguments that the program was invoked
44 * with. It is also possible to query if the commandline invocation was
45 * local (ie: the current process is running in direct response to the
46 * invocation) or remote (ie: some other process forwarded the
47 * commandline to this process).
49 * The GApplicationCommandLine object can provide the @argc and @argv
50 * parameters for use with the #GOptionContext command-line parsing API,
51 * with the g_application_command_line_get_arguments() function. See
52 * <xref linkend="gapplication-example-cmdline3"/> for an example.
54 * The exit status of the originally-invoked process may be set and
55 * messages can be printed to stdout or stderr of that process. The
56 * lifecycle of the originally-invoked process is tied to the lifecycle
57 * of this object (ie: the process exits when the last reference is
60 * The main use for #GApplicationCommandline (and the
61 * #GApplication::command-line signal) is 'Emacs server' like use cases:
62 * You can set the <envar>EDITOR</envar> environment variable to have
63 * e.g. git use your favourite editor to edit commit messages, and if you
64 * already have an instance of the editor running, the editing will happen
65 * in the running instance, instead of opening a new one. An important
66 * aspect of this use case is that the process that gets started by git
67 * does not return until the editing is done.
69 * <example id="gapplication-example-cmdline"><title>Handling commandline arguments with GApplication</title>
71 * A simple example where the commandline is completely handled
72 * in the #GApplication::command-line handler. The launching instance exits
73 * once the signal handler in the primary instance has returned, and the
74 * return value of the signal handler becomes the exit status of the launching
78 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline.c">
79 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
84 * <example id="gapplication-example-cmdline2"><title>Split commandline handling</title>
86 * An example of split commandline handling. Options that start with
87 * <literal>--local-</literal> are handled locally, all other options are
88 * passed to the #GApplication::command-line handler which runs in the primary
92 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline2.c">
93 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
98 * <example id="gapplication-example-cmdline3"><title>Deferred commandline handling</title>
100 * An example of deferred commandline handling. Here, the commandline is
101 * not completely handled before the #GApplication::command-line handler
102 * returns. Instead, we keep a reference to the GApplicationCommandline
103 * object and handle it later(in this example, in an idle). Note that it
104 * is necessary to hold the application until you are done with the
108 * This example also shows how to use #GOptionContext for parsing the
109 * commandline arguments. Note that it is necessary to disable the
110 * built-in help-handling of #GOptionContext, since it calls exit()
111 * after printing help, which is not what you want to happen in
112 * the primary instance.
115 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline3.c">
116 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
123 * GApplicationCommandLineClass:
125 * The <structname>GApplicationCommandLineClass</structname> structure
126 * contains private data only
138 struct _GApplicationCommandLinePrivate
140 GVariant *platform_data;
144 const gchar **environ;
148 /* All subclasses represent remote invocations of some kind. */
149 #define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \
150 G_TYPE_APPLICATION_COMMAND_LINE)
153 grok_platform_data (GApplicationCommandLine *cmdline)
159 g_variant_iter_init (&iter, cmdline->priv->platform_data);
161 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
162 if (strcmp (key, "cwd") == 0)
164 if (!cmdline->priv->cwd)
165 cmdline->priv->cwd = g_variant_ref (value);
168 else if (strcmp (key, "environ") == 0)
170 if (!cmdline->priv->environ)
171 cmdline->priv->environ =
172 g_variant_get_bytestring_array (value, NULL);
177 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
178 const gchar *message)
180 g_print ("%s\n", message);
184 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
185 const gchar *message)
187 g_printerr ("%s\n", message);
191 g_application_command_line_get_property (GObject *object,
196 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
201 g_value_set_variant (value, cmdline->priv->arguments);
204 case PROP_PLATFORM_DATA:
205 g_value_set_variant (value, cmdline->priv->platform_data);
209 g_value_set_boolean (value, IS_REMOTE (cmdline));
213 g_assert_not_reached ();
218 g_application_command_line_set_property (GObject *object,
223 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
228 g_assert (cmdline->priv->arguments == NULL);
229 cmdline->priv->arguments = g_value_dup_variant (value);
232 case PROP_PLATFORM_DATA:
233 g_assert (cmdline->priv->platform_data == NULL);
234 cmdline->priv->platform_data = g_value_dup_variant (value);
235 if (cmdline->priv->platform_data != NULL)
236 grok_platform_data (cmdline);
240 g_assert_not_reached ();
245 g_application_command_line_finalize (GObject *object)
247 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
249 if (cmdline->priv->platform_data)
250 g_variant_unref (cmdline->priv->platform_data);
251 if (cmdline->priv->arguments)
252 g_variant_unref (cmdline->priv->arguments);
253 if (cmdline->priv->cwd)
254 g_variant_unref (cmdline->priv->cwd);
256 G_OBJECT_CLASS (g_application_command_line_parent_class)
261 g_application_command_line_init (GApplicationCommandLine *cmdline)
264 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
265 G_TYPE_APPLICATION_COMMAND_LINE,
266 GApplicationCommandLinePrivate);
270 g_application_command_line_class_init (GApplicationCommandLineClass *class)
272 GObjectClass *object_class = G_OBJECT_CLASS (class);
274 object_class->get_property = g_application_command_line_get_property;
275 object_class->set_property = g_application_command_line_set_property;
276 object_class->finalize = g_application_command_line_finalize;
277 class->printerr_literal = g_application_command_line_real_printerr_literal;
278 class->print_literal = g_application_command_line_real_print_literal;
280 g_object_class_install_property (object_class, PROP_ARGUMENTS,
281 g_param_spec_variant ("arguments",
282 P_("Commandline arguments"),
283 P_("The commandline that caused this ::command-line signal emission"),
284 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
285 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
286 G_PARAM_STATIC_STRINGS));
288 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
289 g_param_spec_variant ("platform-data",
291 P_("Platform-specific data for the commandline"),
292 G_VARIANT_TYPE ("a{sv}"), NULL,
293 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
294 G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (object_class, PROP_IS_REMOTE,
297 g_param_spec_boolean ("is-remote",
299 P_("TRUE if this is a remote commandline"),
301 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
303 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
308 * g_application_command_line_get_arguments:
309 * @cmdline: a #GApplicationCommandLine
310 * @argc: (out): the length of the arguments array, or %NULL
312 * Gets the list of arguments that was passed on the command line.
314 * The strings in the array may contain non-utf8 data.
316 * The return value is %NULL-terminated and should be freed using
319 * Returns: (array length=argc) (transfer full): the string array
320 * containing the arguments (the argv)
325 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
331 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
333 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
342 * g_application_command_line_get_cwd:
343 * @cmdline: a #GApplicationCommandLine
345 * Gets the working directory of the command line invocation.
346 * The string may contain non-utf8 data.
348 * It is possible that the remote application did not send a working
349 * directory, so this may be %NULL.
351 * The return value should not be modified or freed and is valid for as
352 * long as @cmdline exists.
354 * Returns: the current directory, or %NULL
359 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
361 if (cmdline->priv->cwd)
362 return g_variant_get_bytestring (cmdline->priv->cwd);
368 * g_application_command_line_get_environ:
369 * @cmdline: a #GApplicationCommandLine
371 * Gets the contents of the 'environ' variable of the command line
372 * invocation, as would be returned by g_get_environ(), ie as a
373 * %NULL-terminated list of strings in the form 'NAME=VALUE'.
374 * The strings may contain non-utf8 data.
376 * The remote application usually does not send an environment. Use
377 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
378 * set it is possible that the environment is still not available (due
379 * to invocation messages from other applications).
381 * The return value should not be modified or freed and is valid for as
382 * long as @cmdline exists.
384 * See g_application_command_line_getenv() if you are only interested
385 * in the value of a single environment variable.
387 * Returns: (array zero-terminated=1) (transfer none): the environment
388 * strings, or %NULL if they were not sent
392 const gchar * const *
393 g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
395 return cmdline->priv->environ;
399 * g_application_command_line_getenv:
400 * @cmdline: a #GApplicationCommandLine
401 * @name: the environment variable to get
403 * Gets the value of a particular environment variable of the command
404 * line invocation, as would be returned by g_getenv(). The strings may
405 * contain non-utf8 data.
407 * The remote application usually does not send an environment. Use
408 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
409 * set it is possible that the environment is still not available (due
410 * to invocation messages from other applications).
412 * The return value should not be modified or freed and is valid for as
413 * long as @cmdline exists.
415 * Returns: the value of the variable, or %NULL if unset or unsent
420 g_application_command_line_getenv (GApplicationCommandLine *cmdline,
423 gint length = strlen (name);
426 /* TODO: expand on windows */
427 if (cmdline->priv->environ)
428 for (i = 0; cmdline->priv->environ[i]; i++)
429 if (strncmp (cmdline->priv->environ[i], name, length) == 0 &&
430 cmdline->priv->environ[i][length] == '=')
431 return cmdline->priv->environ[i] + length + 1;
437 * g_application_command_line_get_is_remote:
438 * @cmdline: a #GApplicationCommandLine
440 * Determines if @cmdline represents a remote invocation.
442 * Returns: %TRUE if the invocation was remote
447 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
449 return IS_REMOTE (cmdline);
453 * g_application_command_line_print:
454 * @cmdline: a #GApplicationCommandLine
455 * @format: a printf-style format string
456 * @...: arguments, as per @format
458 * Formats a message and prints it using the stdout print handler in the
461 * If @cmdline is a local invocation then this is exactly equivalent to
462 * g_print(). If @cmdline is remote then this is equivalent to calling
463 * g_print() in the invoking process.
468 g_application_command_line_print (GApplicationCommandLine *cmdline,
475 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
476 g_return_if_fail (format != NULL);
478 va_start (ap, format);
479 message = g_strdup_vprintf (format, ap);
482 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
483 ->print_literal (cmdline, message);
488 * g_application_command_line_printerr:
489 * @cmdline: a #GApplicationCommandLine
490 * @format: a printf-style format string
491 * @...: arguments, as per @format
493 * Formats a message and prints it using the stderr print handler in the
496 * If @cmdline is a local invocation then this is exactly equivalent to
497 * g_printerr(). If @cmdline is remote then this is equivalent to
498 * calling g_printerr() in the invoking process.
503 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
510 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
511 g_return_if_fail (format != NULL);
513 va_start (ap, format);
514 message = g_strdup_vprintf (format, ap);
517 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
518 ->printerr_literal (cmdline, message);
523 * g_application_command_line_set_exit_status:
524 * @cmdline: a #GApplicationCommandLine
525 * @exit_status: the exit status
527 * Sets the exit status that will be used when the invoking process
530 * The return value of the #GApplication::command-line signal is
531 * passed to this function when the handler returns. This is the usual
532 * way of setting the exit status.
534 * In the event that you want the remote invocation to continue running
535 * and want to decide on the exit status in the future, you can use this
536 * call. For the case of a remote invocation, the remote process will
537 * typically exit when the last reference is dropped on @cmdline. The
538 * exit status of the remote process will be equal to the last value
539 * that was set with this function.
541 * In the case that the commandline invocation is local, the situation
542 * is slightly more complicated. If the commandline invocation results
543 * in the mainloop running (ie: because the use-count of the application
544 * increased to a non-zero value) then the application is considered to
545 * have been 'successful' in a certain sense, and the exit status is
546 * always zero. If the application use count is zero, though, the exit
547 * status of the local #GApplicationCommandLine is used.
552 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
555 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
557 cmdline->priv->exit_status = exit_status;
561 * g_application_command_line_get_exit_status:
562 * @cmdline: a #GApplicationCommandLine
564 * Gets the exit status of @cmdline. See
565 * g_application_command_line_set_exit_status() for more information.
567 * Returns: the exit status
572 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
574 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
576 return cmdline->priv->exit_status;
580 * g_application_command_line_get_platform_data:
581 * @cmdline: #GApplicationCommandLine
583 * Gets the platform data associated with the invocation of @cmdline.
585 * This is a #GVariant dictionary containing information about the
586 * context in which the invocation occurred. It typically contains
587 * information like the current working directory and the startup
590 * For local invocation, it will be %NULL.
592 * Returns: the platform data, or %NULL
597 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
599 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
601 if (cmdline->priv->platform_data)
602 return g_variant_ref (cmdline->priv->platform_data);