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"
32 G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJECT)
35 * SECTION:gapplicationcommandline
36 * @title: GApplicationCommandLine
37 * @short_description: A command-line invocation of an application
38 * @see_also: #GApplication
40 * #GApplicationCommandLine represents a command-line invocation of
41 * an application. It is created by #GApplication and emitted
42 * in the #GApplication::command-line signal and virtual function.
44 * The class contains the list of arguments that the program was invoked
45 * with. It is also possible to query if the commandline invocation was
46 * local (ie: the current process is running in direct response to the
47 * invocation) or remote (ie: some other process forwarded the
48 * commandline to this process).
50 * The GApplicationCommandLine object can provide the @argc and @argv
51 * parameters for use with the #GOptionContext command-line parsing API,
52 * with the g_application_command_line_get_arguments() function. See
53 * <xref linkend="gapplication-example-cmdline3"/> for an example.
55 * The exit status of the originally-invoked process may be set and
56 * messages can be printed to stdout or stderr of that process. The
57 * lifecycle of the originally-invoked process is tied to the lifecycle
58 * of this object (ie: the process exits when the last reference is
61 * The main use for #GApplicationCommandLine (and the
62 * #GApplication::command-line signal) is 'Emacs server' like use cases:
63 * You can set the <envar>EDITOR</envar> environment variable to have
64 * e.g. git use your favourite editor to edit commit messages, and if you
65 * already have an instance of the editor running, the editing will happen
66 * in the running instance, instead of opening a new one. An important
67 * aspect of this use case is that the process that gets started by git
68 * does not return until the editing is done.
70 * <example id="gapplication-example-cmdline"><title>Handling commandline arguments with GApplication</title>
72 * A simple example where the commandline is completely handled
73 * in the #GApplication::command-line handler. The launching instance exits
74 * once the signal handler in the primary instance has returned, and the
75 * return value of the signal handler becomes the exit status of the launching
79 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline.c">
80 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
85 * <example id="gapplication-example-cmdline2"><title>Split commandline handling</title>
87 * An example of split commandline handling. Options that start with
88 * <literal>--local-</literal> are handled locally, all other options are
89 * passed to the #GApplication::command-line handler which runs in the primary
93 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline2.c">
94 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
99 * <example id="gapplication-example-cmdline3"><title>Deferred commandline handling</title>
101 * An example of deferred commandline handling. Here, the commandline is
102 * not completely handled before the #GApplication::command-line handler
103 * returns. Instead, we keep a reference to the GApplicationCommandLine
104 * object and handle it later(in this example, in an idle). Note that it
105 * is necessary to hold the application until you are done with the
109 * This example also shows how to use #GOptionContext for parsing the
110 * commandline arguments. Note that it is necessary to disable the
111 * built-in help-handling of #GOptionContext, since it calls exit()
112 * after printing help, which is not what you want to happen in
113 * the primary instance.
116 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline3.c">
117 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
124 * GApplicationCommandLineClass:
126 * The <structname>GApplicationCommandLineClass</structname> structure
127 * contains private data only
139 struct _GApplicationCommandLinePrivate
141 GVariant *platform_data;
149 /* All subclasses represent remote invocations of some kind. */
150 #define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \
151 G_TYPE_APPLICATION_COMMAND_LINE)
154 grok_platform_data (GApplicationCommandLine *cmdline)
160 g_variant_iter_init (&iter, cmdline->priv->platform_data);
162 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
163 if (strcmp (key, "cwd") == 0)
165 if (!cmdline->priv->cwd)
166 cmdline->priv->cwd = g_variant_dup_bytestring (value, NULL);
169 else if (strcmp (key, "environ") == 0)
171 if (!cmdline->priv->environ)
172 cmdline->priv->environ =
173 g_variant_dup_bytestring_array (value, NULL);
178 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
179 const gchar *message)
181 g_print ("%s", message);
185 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
186 const gchar *message)
188 g_printerr ("%s", message);
192 g_application_command_line_get_property (GObject *object,
197 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
202 g_value_set_variant (value, cmdline->priv->arguments);
205 case PROP_PLATFORM_DATA:
206 g_value_set_variant (value, cmdline->priv->platform_data);
210 g_value_set_boolean (value, IS_REMOTE (cmdline));
214 g_assert_not_reached ();
219 g_application_command_line_set_property (GObject *object,
224 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
229 g_assert (cmdline->priv->arguments == NULL);
230 cmdline->priv->arguments = g_value_dup_variant (value);
233 case PROP_PLATFORM_DATA:
234 g_assert (cmdline->priv->platform_data == NULL);
235 cmdline->priv->platform_data = g_value_dup_variant (value);
236 if (cmdline->priv->platform_data != NULL)
237 grok_platform_data (cmdline);
241 g_assert_not_reached ();
246 g_application_command_line_finalize (GObject *object)
248 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
250 if (cmdline->priv->platform_data)
251 g_variant_unref (cmdline->priv->platform_data);
252 if (cmdline->priv->arguments)
253 g_variant_unref (cmdline->priv->arguments);
255 g_free (cmdline->priv->cwd);
256 g_strfreev (cmdline->priv->environ);
258 G_OBJECT_CLASS (g_application_command_line_parent_class)
263 g_application_command_line_init (GApplicationCommandLine *cmdline)
266 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
267 G_TYPE_APPLICATION_COMMAND_LINE,
268 GApplicationCommandLinePrivate);
272 g_application_command_line_constructed (GObject *object)
274 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
276 if (IS_REMOTE (cmdline))
279 /* In the local case, set cmd and environ */
280 if (!cmdline->priv->cwd)
281 cmdline->priv->cwd = g_get_current_dir ();
283 if (!cmdline->priv->environ)
284 cmdline->priv->environ = g_get_environ ();
288 g_application_command_line_class_init (GApplicationCommandLineClass *class)
290 GObjectClass *object_class = G_OBJECT_CLASS (class);
292 object_class->get_property = g_application_command_line_get_property;
293 object_class->set_property = g_application_command_line_set_property;
294 object_class->finalize = g_application_command_line_finalize;
295 object_class->constructed = g_application_command_line_constructed;
297 class->printerr_literal = g_application_command_line_real_printerr_literal;
298 class->print_literal = g_application_command_line_real_print_literal;
300 g_object_class_install_property (object_class, PROP_ARGUMENTS,
301 g_param_spec_variant ("arguments",
302 P_("Commandline arguments"),
303 P_("The commandline that caused this ::command-line signal emission"),
304 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
305 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
306 G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
309 g_param_spec_variant ("platform-data",
311 P_("Platform-specific data for the commandline"),
312 G_VARIANT_TYPE ("a{sv}"), NULL,
313 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
314 G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (object_class, PROP_IS_REMOTE,
317 g_param_spec_boolean ("is-remote",
319 P_("TRUE if this is a remote commandline"),
321 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
323 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
328 * g_application_command_line_get_arguments:
329 * @cmdline: a #GApplicationCommandLine
330 * @argc: (out) (allow-none): the length of the arguments array, or %NULL
332 * Gets the list of arguments that was passed on the command line.
334 * The strings in the array may contain non-utf8 data.
336 * The return value is %NULL-terminated and should be freed using
339 * Returns: (array length=argc) (transfer full): the string array
340 * containing the arguments (the argv)
345 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
351 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
353 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
362 * g_application_command_line_get_cwd:
363 * @cmdline: a #GApplicationCommandLine
365 * Gets the working directory of the command line invocation.
366 * The string may contain non-utf8 data.
368 * It is possible that the remote application did not send a working
369 * directory, so this may be %NULL.
371 * The return value should not be modified or freed and is valid for as
372 * long as @cmdline exists.
374 * Returns: the current directory, or %NULL
379 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
381 return cmdline->priv->cwd;
385 * g_application_command_line_get_environ:
386 * @cmdline: a #GApplicationCommandLine
388 * Gets the contents of the 'environ' variable of the command line
389 * invocation, as would be returned by g_get_environ(), ie as a
390 * %NULL-terminated list of strings in the form 'NAME=VALUE'.
391 * The strings may contain non-utf8 data.
393 * The remote application usually does not send an environment. Use
394 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
395 * set it is possible that the environment is still not available (due
396 * to invocation messages from other applications).
398 * The return value should not be modified or freed and is valid for as
399 * long as @cmdline exists.
401 * See g_application_command_line_getenv() if you are only interested
402 * in the value of a single environment variable.
404 * Returns: (array zero-terminated=1) (transfer none): the environment
405 * strings, or %NULL if they were not sent
409 const gchar * const *
410 g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
412 return (const gchar **)cmdline->priv->environ;
416 * g_application_command_line_getenv:
417 * @cmdline: a #GApplicationCommandLine
418 * @name: the environment variable to get
420 * Gets the value of a particular environment variable of the command
421 * line invocation, as would be returned by g_getenv(). The strings may
422 * contain non-utf8 data.
424 * The remote application usually does not send an environment. Use
425 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
426 * set it is possible that the environment is still not available (due
427 * to invocation messages from other applications).
429 * The return value should not be modified or freed and is valid for as
430 * long as @cmdline exists.
432 * Returns: the value of the variable, or %NULL if unset or unsent
437 g_application_command_line_getenv (GApplicationCommandLine *cmdline,
440 gint length = strlen (name);
443 /* TODO: expand on windows */
444 if (cmdline->priv->environ)
445 for (i = 0; cmdline->priv->environ[i]; i++)
446 if (strncmp (cmdline->priv->environ[i], name, length) == 0 &&
447 cmdline->priv->environ[i][length] == '=')
448 return cmdline->priv->environ[i] + length + 1;
454 * g_application_command_line_get_is_remote:
455 * @cmdline: a #GApplicationCommandLine
457 * Determines if @cmdline represents a remote invocation.
459 * Returns: %TRUE if the invocation was remote
464 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
466 return IS_REMOTE (cmdline);
470 * g_application_command_line_print:
471 * @cmdline: a #GApplicationCommandLine
472 * @format: a printf-style format string
473 * @...: arguments, as per @format
475 * Formats a message and prints it using the stdout print handler in the
478 * If @cmdline is a local invocation then this is exactly equivalent to
479 * g_print(). If @cmdline is remote then this is equivalent to calling
480 * g_print() in the invoking process.
485 g_application_command_line_print (GApplicationCommandLine *cmdline,
492 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
493 g_return_if_fail (format != NULL);
495 va_start (ap, format);
496 message = g_strdup_vprintf (format, ap);
499 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
500 ->print_literal (cmdline, message);
505 * g_application_command_line_printerr:
506 * @cmdline: a #GApplicationCommandLine
507 * @format: a printf-style format string
508 * @...: arguments, as per @format
510 * Formats a message and prints it using the stderr print handler in the
513 * If @cmdline is a local invocation then this is exactly equivalent to
514 * g_printerr(). If @cmdline is remote then this is equivalent to
515 * calling g_printerr() in the invoking process.
520 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
527 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
528 g_return_if_fail (format != NULL);
530 va_start (ap, format);
531 message = g_strdup_vprintf (format, ap);
534 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
535 ->printerr_literal (cmdline, message);
540 * g_application_command_line_set_exit_status:
541 * @cmdline: a #GApplicationCommandLine
542 * @exit_status: the exit status
544 * Sets the exit status that will be used when the invoking process
547 * The return value of the #GApplication::command-line signal is
548 * passed to this function when the handler returns. This is the usual
549 * way of setting the exit status.
551 * In the event that you want the remote invocation to continue running
552 * and want to decide on the exit status in the future, you can use this
553 * call. For the case of a remote invocation, the remote process will
554 * typically exit when the last reference is dropped on @cmdline. The
555 * exit status of the remote process will be equal to the last value
556 * that was set with this function.
558 * In the case that the commandline invocation is local, the situation
559 * is slightly more complicated. If the commandline invocation results
560 * in the mainloop running (ie: because the use-count of the application
561 * increased to a non-zero value) then the application is considered to
562 * have been 'successful' in a certain sense, and the exit status is
563 * always zero. If the application use count is zero, though, the exit
564 * status of the local #GApplicationCommandLine is used.
569 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
572 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
574 cmdline->priv->exit_status = exit_status;
578 * g_application_command_line_get_exit_status:
579 * @cmdline: a #GApplicationCommandLine
581 * Gets the exit status of @cmdline. See
582 * g_application_command_line_set_exit_status() for more information.
584 * Returns: the exit status
589 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
591 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
593 return cmdline->priv->exit_status;
597 * g_application_command_line_get_platform_data:
598 * @cmdline: #GApplicationCommandLine
600 * Gets the platform data associated with the invocation of @cmdline.
602 * This is a #GVariant dictionary containing information about the
603 * context in which the invocation occurred. It typically contains
604 * information like the current working directory and the startup
607 * For local invocation, it will be %NULL.
609 * Returns: (allow-none): the platform data, or %NULL
614 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
616 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
618 if (cmdline->priv->platform_data)
619 return g_variant_ref (cmdline->priv->platform_data);
625 * g_application_command_line_create_file_for_arg:
626 * @cmdline: a #GApplicationCommandLine
627 * @arg: an argument from @cmdline
629 * Creates a #GFile corresponding to a filename that was given as part
630 * of the invocation of @cmdline.
632 * This differs from g_file_new_for_commandline_arg() in that it
633 * resolves relative pathnames using the current working directory of
634 * the invoking process rather than the local process.
636 * Returns: (transfer full): a new #GFile
641 g_application_command_line_create_file_for_arg (GApplicationCommandLine *cmdline,
644 g_return_val_if_fail (arg != NULL, NULL);
646 if (cmdline->priv->cwd)
647 return g_file_new_for_commandline_arg_and_cwd (arg, cmdline->priv->cwd);
649 g_warning ("Requested creation of GFile for commandline invocation that did not send cwd. "
650 "Using cwd of local process to resolve relative path names.");
652 return g_file_new_for_commandline_arg (arg);