menu threaded test: run the mainloop after export
[glib.git] / gio / gapplicationcommandline.c
blobe7a5c6c694e044a2ff1e0626bcdee978518dd3fe
1 /*
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,
17 * USA.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
22 #include "config.h"
24 #include "gapplicationcommandline.h"
26 #include "glibintl.h"
28 #include <string.h>
29 #include <stdio.h>
31 G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJECT)
33 /**
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
58 * dropped).
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>
70 * <para>
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
75 * instance.
76 * </para>
77 * <programlisting>
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>
80 * </xi:include>
81 * </programlisting>
82 * </example>
84 * <example id="gapplication-example-cmdline2"><title>Split commandline handling</title>
85 * <para>
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
89 * instance.
90 * </para>
91 * <programlisting>
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>
94 * </xi:include>
95 * </programlisting>
96 * </example>
98 * <example id="gapplication-example-cmdline3"><title>Deferred commandline handling</title>
99 * <para>
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
105 * commandline.
106 * </para>
107 * <para>
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.
113 * </para>
114 * <programlisting>
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>
117 * </xi:include>
118 * </programlisting>
119 * </example>
122 enum
124 PROP_NONE,
125 PROP_ARGUMENTS,
126 PROP_PLATFORM_DATA,
127 PROP_IS_REMOTE
130 struct _GApplicationCommandLinePrivate
132 GVariant *platform_data;
133 GVariant *arguments;
134 GVariant *cwd;
136 const gchar **environ;
137 gint exit_status;
140 /* All subclasses represent remote invocations of some kind. */
141 #define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \
142 G_TYPE_APPLICATION_COMMAND_LINE)
144 static void
145 grok_platform_data (GApplicationCommandLine *cmdline)
147 GVariantIter iter;
148 const gchar *key;
149 GVariant *value;
151 g_variant_iter_init (&iter, cmdline->priv->platform_data);
153 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
154 if (strcmp (key, "cwd") == 0)
156 if (!cmdline->priv->cwd)
157 cmdline->priv->cwd = g_variant_ref (value);
160 else if (strcmp (key, "environ") == 0)
162 if (!cmdline->priv->environ)
163 cmdline->priv->environ =
164 g_variant_get_bytestring_array (value, NULL);
168 static void
169 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
170 const gchar *message)
172 g_print ("%s\n", message);
175 static void
176 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
177 const gchar *message)
179 g_printerr ("%s\n", message);
182 static void
183 g_application_command_line_get_property (GObject *object,
184 guint prop_id,
185 GValue *value,
186 GParamSpec *pspec)
188 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
190 switch (prop_id)
192 case PROP_ARGUMENTS:
193 g_value_set_variant (value, cmdline->priv->arguments);
194 break;
196 case PROP_PLATFORM_DATA:
197 g_value_set_variant (value, cmdline->priv->platform_data);
198 break;
200 case PROP_IS_REMOTE:
201 g_value_set_boolean (value, IS_REMOTE (cmdline));
202 break;
204 default:
205 g_assert_not_reached ();
209 static void
210 g_application_command_line_set_property (GObject *object,
211 guint prop_id,
212 const GValue *value,
213 GParamSpec *pspec)
215 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
217 switch (prop_id)
219 case PROP_ARGUMENTS:
220 g_assert (cmdline->priv->arguments == NULL);
221 cmdline->priv->arguments = g_value_dup_variant (value);
222 break;
224 case PROP_PLATFORM_DATA:
225 g_assert (cmdline->priv->platform_data == NULL);
226 cmdline->priv->platform_data = g_value_dup_variant (value);
227 if (cmdline->priv->platform_data != NULL)
228 grok_platform_data (cmdline);
229 break;
231 default:
232 g_assert_not_reached ();
236 static void
237 g_application_command_line_finalize (GObject *object)
239 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
241 if (cmdline->priv->platform_data)
242 g_variant_unref (cmdline->priv->platform_data);
243 if (cmdline->priv->arguments)
244 g_variant_unref (cmdline->priv->arguments);
245 if (cmdline->priv->cwd)
246 g_variant_unref (cmdline->priv->cwd);
248 G_OBJECT_CLASS (g_application_command_line_parent_class)
249 ->finalize (object);
252 static void
253 g_application_command_line_init (GApplicationCommandLine *cmdline)
255 cmdline->priv =
256 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
257 G_TYPE_APPLICATION_COMMAND_LINE,
258 GApplicationCommandLinePrivate);
261 static void
262 g_application_command_line_class_init (GApplicationCommandLineClass *class)
264 GObjectClass *object_class = G_OBJECT_CLASS (class);
266 object_class->get_property = g_application_command_line_get_property;
267 object_class->set_property = g_application_command_line_set_property;
268 object_class->finalize = g_application_command_line_finalize;
269 class->printerr_literal = g_application_command_line_real_printerr_literal;
270 class->print_literal = g_application_command_line_real_print_literal;
272 g_object_class_install_property (object_class, PROP_ARGUMENTS,
273 g_param_spec_variant ("arguments",
274 P_("Commandline arguments"),
275 P_("The commandline that caused this ::command-line signal emission"),
276 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
277 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
278 G_PARAM_STATIC_STRINGS));
280 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
281 g_param_spec_variant ("platform-data",
282 P_("Platform data"),
283 P_("Platform-specific data for the commandline"),
284 G_VARIANT_TYPE ("a{sv}"), NULL,
285 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
286 G_PARAM_STATIC_STRINGS));
288 g_object_class_install_property (object_class, PROP_IS_REMOTE,
289 g_param_spec_boolean ("is-remote",
290 P_("Is remote"),
291 P_("TRUE if this is a remote commandline"),
292 FALSE,
293 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
295 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
300 * g_application_command_line_get_arguments:
301 * @cmdline: a #GApplicationCommandLine
302 * @argc: (out): the length of the arguments array, or %NULL
304 * Gets the list of arguments that was passed on the command line.
306 * The strings in the array may contain non-utf8 data.
308 * The return value is %NULL-terminated and should be freed using
309 * g_strfreev().
311 * Returns: (array length=argc) (transfer full): the string array
312 * containing the arguments (the argv)
314 * Since: 2.28
316 gchar **
317 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
318 int *argc)
320 gchar **argv;
321 gsize len;
323 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
325 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
327 if (argc)
328 *argc = len;
330 return argv;
334 * g_application_command_line_get_cwd:
335 * @cmdline: a #GApplicationCommandLine
337 * Gets the working directory of the command line invocation.
338 * The string may contain non-utf8 data.
340 * It is possible that the remote application did not send a working
341 * directory, so this may be %NULL.
343 * The return value should not be modified or freed and is valid for as
344 * long as @cmdline exists.
346 * Returns: the current directory, or %NULL
348 * Since: 2.28
350 const gchar *
351 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
353 if (cmdline->priv->cwd)
354 return g_variant_get_bytestring (cmdline->priv->cwd);
355 else
356 return NULL;
360 * g_application_command_line_get_environ:
361 * @cmdline: a #GApplicationCommandLine
363 * Gets the contents of the 'environ' variable of the command line
364 * invocation, as would be returned by g_get_environ(), ie as a
365 * %NULL-terminated list of strings in the form 'NAME=VALUE'.
366 * The strings may contain non-utf8 data.
368 * The remote application usually does not send an environment. Use
369 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
370 * set it is possible that the environment is still not available (due
371 * to invocation messages from other applications).
373 * The return value should not be modified or freed and is valid for as
374 * long as @cmdline exists.
376 * See g_application_command_line_getenv() if you are only interested
377 * in the value of a single environment variable.
379 * Returns: (array zero-terminated=1) (transfer none): the environment
380 * strings, or %NULL if they were not sent
382 * Since: 2.28
384 const gchar * const *
385 g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
387 return cmdline->priv->environ;
391 * g_application_command_line_getenv:
392 * @cmdline: a #GApplicationCommandLine
393 * @name: the environment variable to get
395 * Gets the value of a particular environment variable of the command
396 * line invocation, as would be returned by g_getenv(). The strings may
397 * contain non-utf8 data.
399 * The remote application usually does not send an environment. Use
400 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
401 * set it is possible that the environment is still not available (due
402 * to invocation messages from other applications).
404 * The return value should not be modified or freed and is valid for as
405 * long as @cmdline exists.
407 * Returns: the value of the variable, or %NULL if unset or unsent
409 * Since: 2.28
411 const gchar *
412 g_application_command_line_getenv (GApplicationCommandLine *cmdline,
413 const gchar *name)
415 gint length = strlen (name);
416 gint i;
418 /* TODO: expand on windows */
419 if (cmdline->priv->environ)
420 for (i = 0; cmdline->priv->environ[i]; i++)
421 if (strncmp (cmdline->priv->environ[i], name, length) == 0 &&
422 cmdline->priv->environ[i][length] == '=')
423 return cmdline->priv->environ[i] + length + 1;
425 return NULL;
429 * g_application_command_line_get_is_remote:
430 * @cmdline: a #GApplicationCommandLine
432 * Determines if @cmdline represents a remote invocation.
434 * Returns: %TRUE if the invocation was remote
436 * Since: 2.28
438 gboolean
439 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
441 return IS_REMOTE (cmdline);
445 * g_application_command_line_print:
446 * @cmdline: a #GApplicationCommandLine
447 * @format: a printf-style format string
448 * @...: arguments, as per @format
450 * Formats a message and prints it using the stdout print handler in the
451 * invoking process.
453 * If @cmdline is a local invocation then this is exactly equivalent to
454 * g_print(). If @cmdline is remote then this is equivalent to calling
455 * g_print() in the invoking process.
457 * Since: 2.28
459 void
460 g_application_command_line_print (GApplicationCommandLine *cmdline,
461 const gchar *format,
462 ...)
464 gchar *message;
465 va_list ap;
467 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
468 g_return_if_fail (format != NULL);
470 va_start (ap, format);
471 message = g_strdup_vprintf (format, ap);
472 va_end (ap);
474 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
475 ->print_literal (cmdline, message);
476 g_free (message);
480 * g_application_command_line_printerr:
481 * @cmdline: a #GApplicationCommandLine
482 * @format: a printf-style format string
483 * @...: arguments, as per @format
485 * Formats a message and prints it using the stderr print handler in the
486 * invoking process.
488 * If @cmdline is a local invocation then this is exactly equivalent to
489 * g_printerr(). If @cmdline is remote then this is equivalent to
490 * calling g_printerr() in the invoking process.
492 * Since: 2.28
494 void
495 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
496 const gchar *format,
497 ...)
499 gchar *message;
500 va_list ap;
502 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
503 g_return_if_fail (format != NULL);
505 va_start (ap, format);
506 message = g_strdup_vprintf (format, ap);
507 va_end (ap);
509 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
510 ->printerr_literal (cmdline, message);
511 g_free (message);
515 * g_application_command_line_set_exit_status:
516 * @cmdline: a #GApplicationCommandLine
517 * @exit_status: the exit status
519 * Sets the exit status that will be used when the invoking process
520 * exits.
522 * The return value of the #GApplication::command-line signal is
523 * passed to this function when the handler returns. This is the usual
524 * way of setting the exit status.
526 * In the event that you want the remote invocation to continue running
527 * and want to decide on the exit status in the future, you can use this
528 * call. For the case of a remote invocation, the remote process will
529 * typically exit when the last reference is dropped on @cmdline. The
530 * exit status of the remote process will be equal to the last value
531 * that was set with this function.
533 * In the case that the commandline invocation is local, the situation
534 * is slightly more complicated. If the commandline invocation results
535 * in the mainloop running (ie: because the use-count of the application
536 * increased to a non-zero value) then the application is considered to
537 * have been 'successful' in a certain sense, and the exit status is
538 * always zero. If the application use count is zero, though, the exit
539 * status of the local #GApplicationCommandLine is used.
541 * Since: 2.28
543 void
544 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
545 int exit_status)
547 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
549 cmdline->priv->exit_status = exit_status;
553 * g_application_command_line_get_exit_status:
554 * @cmdline: a #GApplicationCommandLine
556 * Gets the exit status of @cmdline. See
557 * g_application_command_line_set_exit_status() for more information.
559 * Returns: the exit status
561 * Since: 2.28
564 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
566 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
568 return cmdline->priv->exit_status;
572 * g_application_command_line_get_platform_data:
573 * @cmdline: #GApplicationCommandLine
575 * Gets the platform data associated with the invocation of @cmdline.
577 * This is a #GVariant dictionary containing information about the
578 * context in which the invocation occurred. It typically contains
579 * information like the current working directory and the startup
580 * notification ID.
582 * For local invocation, it will be %NULL.
584 * Returns: the platform data, or %NULL
586 * Since: 2.28
588 GVariant *
589 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
591 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
593 if (cmdline->priv->platform_data)
594 return g_variant_ref (cmdline->priv->platform_data);
595 else
596 return NULL;