Spelling fixes
[platform/upstream/glib.git] / gio / gapplicationcommandline.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include "gapplicationcommandline.h"
25
26 #include "glibintl.h"
27
28 #include <string.h>
29 #include <stdio.h>
30
31 G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJECT)
32
33 /**
34  * SECTION:gapplicationcommandline
35  * @title: GApplicationCommandLine
36  * @short_description: A command-line invocation of an application
37  * @see_also: #GApplication
38  *
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.
42  *
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).
48  *
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.
53  *
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).
59  *
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.
68  *
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>
83  *
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>
97  *
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>
120  **/
121
122 enum
123 {
124   PROP_NONE,
125   PROP_ARGUMENTS,
126   PROP_PLATFORM_DATA,
127   PROP_IS_REMOTE
128 };
129
130 struct _GApplicationCommandLinePrivate
131 {
132   GVariant *platform_data;
133   GVariant *arguments;
134   GVariant *cwd;
135
136   const gchar **environ;
137   gint exit_status;
138 };
139
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)
143
144 static void
145 grok_platform_data (GApplicationCommandLine *cmdline)
146 {
147   GVariantIter iter;
148   const gchar *key;
149   GVariant *value;
150
151   g_variant_iter_init (&iter, cmdline->priv->platform_data);
152
153   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
154     if (strcmp (key, "cwd") == 0)
155       {
156         if (!cmdline->priv->cwd)
157           cmdline->priv->cwd = g_variant_ref (value);
158       }
159
160     else if (strcmp (key, "environ") == 0)
161       {
162         if (!cmdline->priv->environ)
163           cmdline->priv->environ =
164             g_variant_get_bytestring_array (value, NULL);
165       }
166 }
167
168 static void
169 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
170                                                const gchar             *message)
171 {
172   g_print ("%s\n", message);
173 }
174
175 static void
176 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
177                                                   const gchar             *message)
178 {
179   g_printerr ("%s\n", message);
180 }
181
182 static void
183 g_application_command_line_get_property (GObject    *object,
184                                          guint       prop_id,
185                                          GValue     *value,
186                                          GParamSpec *pspec)
187 {
188   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
189
190   switch (prop_id)
191     {
192     case PROP_ARGUMENTS:
193       g_value_set_variant (value, cmdline->priv->arguments);
194       break;
195
196     case PROP_PLATFORM_DATA:
197       g_value_set_variant (value, cmdline->priv->platform_data);
198       break;
199
200     case PROP_IS_REMOTE:
201       g_value_set_boolean (value, IS_REMOTE (cmdline));
202       break;
203
204     default:
205       g_assert_not_reached ();
206     }
207 }
208
209 static void
210 g_application_command_line_set_property (GObject      *object,
211                                          guint         prop_id,
212                                          const GValue *value,
213                                          GParamSpec   *pspec)
214 {
215   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
216
217   switch (prop_id)
218     {
219     case PROP_ARGUMENTS:
220       g_assert (cmdline->priv->arguments == NULL);
221       cmdline->priv->arguments = g_value_dup_variant (value);
222       break;
223
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;
230
231     default:
232       g_assert_not_reached ();
233     }
234 }
235
236 static void
237 g_application_command_line_finalize (GObject *object)
238 {
239   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
240
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);
247
248   G_OBJECT_CLASS (g_application_command_line_parent_class)
249     ->finalize (object);
250 }
251
252 static void
253 g_application_command_line_init (GApplicationCommandLine *cmdline)
254 {
255   cmdline->priv =
256     G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
257                                  G_TYPE_APPLICATION_COMMAND_LINE,
258                                  GApplicationCommandLinePrivate);
259 }
260
261 static void
262 g_application_command_line_class_init (GApplicationCommandLineClass *class)
263 {
264   GObjectClass *object_class = G_OBJECT_CLASS (class);
265
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;
271
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));
279
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));
287
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));
294
295   g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
296 }
297
298
299 /**
300  * g_application_command_line_get_arguments:
301  * @cmdline: a #GApplicationCommandLine
302  * @argc: (out): the length of the arguments array, or %NULL
303  *
304  * Gets the list of arguments that was passed on the command line.
305  *
306  * The strings in the array may contain non-utf8 data.
307  *
308  * The return value is %NULL-terminated and should be freed using
309  * g_strfreev().
310  *
311  * Returns: (array length=argc) (transfer full): the string array
312  * containing the arguments (the argv)
313  *
314  * Since: 2.28
315  **/
316 gchar **
317 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
318                                           int                     *argc)
319 {
320   gchar **argv;
321   gsize len;
322
323   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
324
325   argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
326
327   if (argc)
328     *argc = len;
329
330   return argv;
331 }
332
333 /**
334  * g_application_command_line_get_cwd:
335  * @cmdline: a #GApplicationCommandLine
336  *
337  * Gets the working directory of the command line invocation.
338  * The string may contain non-utf8 data.
339  *
340  * It is possible that the remote application did not send a working
341  * directory, so this may be %NULL.
342  *
343  * The return value should not be modified or freed and is valid for as
344  * long as @cmdline exists.
345  *
346  * Returns: the current directory, or %NULL
347  *
348  * Since: 2.28
349  **/
350 const gchar *
351 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
352 {
353   if (cmdline->priv->cwd)
354     return g_variant_get_bytestring (cmdline->priv->cwd);
355   else
356     return NULL;
357 }
358
359 /**
360  * g_application_command_line_get_environ:
361  * @cmdline: a #GApplicationCommandLine
362  *
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.
367  *
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).
372  *
373  * The return value should not be modified or freed and is valid for as
374  * long as @cmdline exists.
375  *
376  * See g_application_command_line_getenv() if you are only interested
377  * in the value of a single environment variable.
378  *
379  * Returns: (array zero-terminated=1) (transfer none): the environment
380  * strings, or %NULL if they were not sent
381  *
382  * Since: 2.28
383  **/
384 const gchar * const *
385 g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
386 {
387   return cmdline->priv->environ;
388 }
389
390 /**
391  * g_application_command_line_getenv:
392  * @cmdline: a #GApplicationCommandLine
393  * @name: the environment variable to get
394  *
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.
398  *
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).
403  *
404  * The return value should not be modified or freed and is valid for as
405  * long as @cmdline exists.
406  *
407  * Returns: the value of the variable, or %NULL if unset or unsent
408  *
409  * Since: 2.28
410  **/
411 const gchar *
412 g_application_command_line_getenv (GApplicationCommandLine *cmdline,
413                                    const gchar             *name)
414 {
415   gint length = strlen (name);
416   gint i;
417
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;
424
425   return NULL;
426 }
427
428 /**
429  * g_application_command_line_get_is_remote:
430  * @cmdline: a #GApplicationCommandLine
431  *
432  * Determines if @cmdline represents a remote invocation.
433  *
434  * Returns: %TRUE if the invocation was remote
435  *
436  * Since: 2.28
437  **/
438 gboolean
439 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
440 {
441   return IS_REMOTE (cmdline);
442 }
443
444 /**
445  * g_application_command_line_print:
446  * @cmdline: a #GApplicationCommandLine
447  * @format: a printf-style format string
448  * @...: arguments, as per @format
449  *
450  * Formats a message and prints it using the stdout print handler in the
451  * invoking process.
452  *
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.
456  *
457  * Since: 2.28
458  **/
459 void
460 g_application_command_line_print (GApplicationCommandLine *cmdline,
461                                   const gchar             *format,
462                                   ...)
463 {
464   gchar *message;
465   va_list ap;
466
467   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
468   g_return_if_fail (format != NULL);
469
470   va_start (ap, format);
471   message = g_strdup_vprintf (format, ap);
472   va_end (ap);
473
474   G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
475     ->print_literal (cmdline, message);
476   g_free (message);
477 }
478
479 /**
480  * g_application_command_line_printerr:
481  * @cmdline: a #GApplicationCommandLine
482  * @format: a printf-style format string
483  * @...: arguments, as per @format
484  *
485  * Formats a message and prints it using the stderr print handler in the
486  * invoking process.
487  *
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.
491  *
492  * Since: 2.28
493  **/
494 void
495 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
496                                      const gchar             *format,
497                                      ...)
498 {
499   gchar *message;
500   va_list ap;
501
502   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
503   g_return_if_fail (format != NULL);
504
505   va_start (ap, format);
506   message = g_strdup_vprintf (format, ap);
507   va_end (ap);
508
509   G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
510     ->printerr_literal (cmdline, message);
511   g_free (message);
512 }
513
514 /**
515  * g_application_command_line_set_exit_status:
516  * @cmdline: a #GApplicationCommandLine
517  * @exit_status: the exit status
518  *
519  * Sets the exit status that will be used when the invoking process
520  * exits.
521  *
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.
525  *
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.
532  *
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.
540  *
541  * Since: 2.28
542  **/
543 void
544 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
545                                             int                      exit_status)
546 {
547   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
548
549   cmdline->priv->exit_status = exit_status;
550 }
551
552 /**
553  * g_application_command_line_get_exit_status:
554  * @cmdline: a #GApplicationCommandLine
555  *
556  * Gets the exit status of @cmdline.  See
557  * g_application_command_line_set_exit_status() for more information.
558  *
559  * Returns: the exit status
560  *
561  * Since: 2.28
562  **/
563 int
564 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
565 {
566   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
567
568   return cmdline->priv->exit_status;
569 }
570
571 /**
572  * g_application_command_line_get_platform_data:
573  * @cmdline: #GApplicationCommandLine
574  *
575  * Gets the platform data associated with the invocation of @cmdline.
576  *
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.
581  *
582  * For local invocation, it will be %NULL.
583  *
584  * Returns: the platform data, or %NULL
585  *
586  * Since: 2.28
587  **/
588 GVariant *
589 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
590 {
591   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
592
593   if (cmdline->priv->platform_data)
594     return g_variant_ref (cmdline->priv->platform_data);
595   else
596       return NULL;
597 }