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;
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_dup_bytestring (value, NULL);
168 else if (strcmp (key, "environ") == 0)
170 if (!cmdline->priv->environ)
171 cmdline->priv->environ =
172 g_variant_dup_bytestring_array (value, NULL);
177 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
178 const gchar *message)
180 g_print ("%s", message);
184 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
185 const gchar *message)
187 g_printerr ("%s", 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);
254 g_free (cmdline->priv->cwd);
255 g_strfreev (cmdline->priv->environ);
257 G_OBJECT_CLASS (g_application_command_line_parent_class)
262 g_application_command_line_init (GApplicationCommandLine *cmdline)
265 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
266 G_TYPE_APPLICATION_COMMAND_LINE,
267 GApplicationCommandLinePrivate);
271 g_application_command_line_constructed (GObject *object)
273 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
275 if (IS_REMOTE (cmdline))
278 /* In the local case, set cmd and environ */
279 if (!cmdline->priv->cwd)
280 cmdline->priv->cwd = g_get_current_dir ();
282 if (!cmdline->priv->environ)
283 cmdline->priv->environ = g_get_environ ();
287 g_application_command_line_class_init (GApplicationCommandLineClass *class)
289 GObjectClass *object_class = G_OBJECT_CLASS (class);
291 object_class->get_property = g_application_command_line_get_property;
292 object_class->set_property = g_application_command_line_set_property;
293 object_class->finalize = g_application_command_line_finalize;
294 object_class->constructed = g_application_command_line_constructed;
296 class->printerr_literal = g_application_command_line_real_printerr_literal;
297 class->print_literal = g_application_command_line_real_print_literal;
299 g_object_class_install_property (object_class, PROP_ARGUMENTS,
300 g_param_spec_variant ("arguments",
301 P_("Commandline arguments"),
302 P_("The commandline that caused this ::command-line signal emission"),
303 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
304 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
305 G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
308 g_param_spec_variant ("platform-data",
310 P_("Platform-specific data for the commandline"),
311 G_VARIANT_TYPE ("a{sv}"), NULL,
312 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
313 G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (object_class, PROP_IS_REMOTE,
316 g_param_spec_boolean ("is-remote",
318 P_("TRUE if this is a remote commandline"),
320 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
322 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
327 * g_application_command_line_get_arguments:
328 * @cmdline: a #GApplicationCommandLine
329 * @argc: (out) (allow-none): the length of the arguments array, or %NULL
331 * Gets the list of arguments that was passed on the command line.
333 * The strings in the array may contain non-utf8 data.
335 * The return value is %NULL-terminated and should be freed using
338 * Returns: (array length=argc) (transfer full): the string array
339 * containing the arguments (the argv)
344 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
350 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
352 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
361 * g_application_command_line_get_cwd:
362 * @cmdline: a #GApplicationCommandLine
364 * Gets the working directory of the command line invocation.
365 * The string may contain non-utf8 data.
367 * It is possible that the remote application did not send a working
368 * directory, so this may be %NULL.
370 * The return value should not be modified or freed and is valid for as
371 * long as @cmdline exists.
373 * Returns: the current directory, or %NULL
378 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
380 return cmdline->priv->cwd;
384 * g_application_command_line_get_environ:
385 * @cmdline: a #GApplicationCommandLine
387 * Gets the contents of the 'environ' variable of the command line
388 * invocation, as would be returned by g_get_environ(), ie as a
389 * %NULL-terminated list of strings in the form 'NAME=VALUE'.
390 * The strings may contain non-utf8 data.
392 * The remote application usually does not send an environment. Use
393 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
394 * set it is possible that the environment is still not available (due
395 * to invocation messages from other applications).
397 * The return value should not be modified or freed and is valid for as
398 * long as @cmdline exists.
400 * See g_application_command_line_getenv() if you are only interested
401 * in the value of a single environment variable.
403 * Returns: (array zero-terminated=1) (transfer none): the environment
404 * strings, or %NULL if they were not sent
408 const gchar * const *
409 g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
411 return (const gchar **)cmdline->priv->environ;
415 * g_application_command_line_getenv:
416 * @cmdline: a #GApplicationCommandLine
417 * @name: the environment variable to get
419 * Gets the value of a particular environment variable of the command
420 * line invocation, as would be returned by g_getenv(). The strings may
421 * contain non-utf8 data.
423 * The remote application usually does not send an environment. Use
424 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
425 * set it is possible that the environment is still not available (due
426 * to invocation messages from other applications).
428 * The return value should not be modified or freed and is valid for as
429 * long as @cmdline exists.
431 * Returns: the value of the variable, or %NULL if unset or unsent
436 g_application_command_line_getenv (GApplicationCommandLine *cmdline,
439 gint length = strlen (name);
442 /* TODO: expand on windows */
443 if (cmdline->priv->environ)
444 for (i = 0; cmdline->priv->environ[i]; i++)
445 if (strncmp (cmdline->priv->environ[i], name, length) == 0 &&
446 cmdline->priv->environ[i][length] == '=')
447 return cmdline->priv->environ[i] + length + 1;
453 * g_application_command_line_get_is_remote:
454 * @cmdline: a #GApplicationCommandLine
456 * Determines if @cmdline represents a remote invocation.
458 * Returns: %TRUE if the invocation was remote
463 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
465 return IS_REMOTE (cmdline);
469 * g_application_command_line_print:
470 * @cmdline: a #GApplicationCommandLine
471 * @format: a printf-style format string
472 * @...: arguments, as per @format
474 * Formats a message and prints it using the stdout print handler in the
477 * If @cmdline is a local invocation then this is exactly equivalent to
478 * g_print(). If @cmdline is remote then this is equivalent to calling
479 * g_print() in the invoking process.
484 g_application_command_line_print (GApplicationCommandLine *cmdline,
491 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
492 g_return_if_fail (format != NULL);
494 va_start (ap, format);
495 message = g_strdup_vprintf (format, ap);
498 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
499 ->print_literal (cmdline, message);
504 * g_application_command_line_printerr:
505 * @cmdline: a #GApplicationCommandLine
506 * @format: a printf-style format string
507 * @...: arguments, as per @format
509 * Formats a message and prints it using the stderr print handler in the
512 * If @cmdline is a local invocation then this is exactly equivalent to
513 * g_printerr(). If @cmdline is remote then this is equivalent to
514 * calling g_printerr() in the invoking process.
519 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
526 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
527 g_return_if_fail (format != NULL);
529 va_start (ap, format);
530 message = g_strdup_vprintf (format, ap);
533 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
534 ->printerr_literal (cmdline, message);
539 * g_application_command_line_set_exit_status:
540 * @cmdline: a #GApplicationCommandLine
541 * @exit_status: the exit status
543 * Sets the exit status that will be used when the invoking process
546 * The return value of the #GApplication::command-line signal is
547 * passed to this function when the handler returns. This is the usual
548 * way of setting the exit status.
550 * In the event that you want the remote invocation to continue running
551 * and want to decide on the exit status in the future, you can use this
552 * call. For the case of a remote invocation, the remote process will
553 * typically exit when the last reference is dropped on @cmdline. The
554 * exit status of the remote process will be equal to the last value
555 * that was set with this function.
557 * In the case that the commandline invocation is local, the situation
558 * is slightly more complicated. If the commandline invocation results
559 * in the mainloop running (ie: because the use-count of the application
560 * increased to a non-zero value) then the application is considered to
561 * have been 'successful' in a certain sense, and the exit status is
562 * always zero. If the application use count is zero, though, the exit
563 * status of the local #GApplicationCommandLine is used.
568 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
571 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
573 cmdline->priv->exit_status = exit_status;
577 * g_application_command_line_get_exit_status:
578 * @cmdline: a #GApplicationCommandLine
580 * Gets the exit status of @cmdline. See
581 * g_application_command_line_set_exit_status() for more information.
583 * Returns: the exit status
588 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
590 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
592 return cmdline->priv->exit_status;
596 * g_application_command_line_get_platform_data:
597 * @cmdline: #GApplicationCommandLine
599 * Gets the platform data associated with the invocation of @cmdline.
601 * This is a #GVariant dictionary containing information about the
602 * context in which the invocation occurred. It typically contains
603 * information like the current working directory and the startup
606 * For local invocation, it will be %NULL.
608 * Returns: (allow-none): the platform data, or %NULL
613 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
615 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
617 if (cmdline->priv->platform_data)
618 return g_variant_ref (cmdline->priv->platform_data);