Retrieve cwd and environ in local GApplicationCommandLine
[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 /**
123  * GApplicationCommandLineClass:
124  *
125  * The <structname>GApplicationCommandLineClass</structname> structure
126  * contains private data only
127  *
128  * Since: 2.28
129  **/
130 enum
131 {
132   PROP_NONE,
133   PROP_ARGUMENTS,
134   PROP_PLATFORM_DATA,
135   PROP_IS_REMOTE
136 };
137
138 struct _GApplicationCommandLinePrivate
139 {
140   GVariant *platform_data;
141   GVariant *arguments;
142   gchar *cwd;
143
144   gchar **environ;
145   gint exit_status;
146 };
147
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)
151
152 static void
153 grok_platform_data (GApplicationCommandLine *cmdline)
154 {
155   GVariantIter iter;
156   const gchar *key;
157   GVariant *value;
158
159   g_variant_iter_init (&iter, cmdline->priv->platform_data);
160
161   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
162     if (strcmp (key, "cwd") == 0)
163       {
164         if (!cmdline->priv->cwd)
165           cmdline->priv->cwd = g_variant_dup_bytestring (value, NULL);
166       }
167
168     else if (strcmp (key, "environ") == 0)
169       {
170         if (!cmdline->priv->environ)
171           cmdline->priv->environ =
172             g_variant_dup_bytestring_array (value, NULL);
173       }
174 }
175
176 static void
177 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
178                                                const gchar             *message)
179 {
180   g_print ("%s\n", message);
181 }
182
183 static void
184 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
185                                                   const gchar             *message)
186 {
187   g_printerr ("%s\n", message);
188 }
189
190 static void
191 g_application_command_line_get_property (GObject    *object,
192                                          guint       prop_id,
193                                          GValue     *value,
194                                          GParamSpec *pspec)
195 {
196   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
197
198   switch (prop_id)
199     {
200     case PROP_ARGUMENTS:
201       g_value_set_variant (value, cmdline->priv->arguments);
202       break;
203
204     case PROP_PLATFORM_DATA:
205       g_value_set_variant (value, cmdline->priv->platform_data);
206       break;
207
208     case PROP_IS_REMOTE:
209       g_value_set_boolean (value, IS_REMOTE (cmdline));
210       break;
211
212     default:
213       g_assert_not_reached ();
214     }
215 }
216
217 static void
218 g_application_command_line_set_property (GObject      *object,
219                                          guint         prop_id,
220                                          const GValue *value,
221                                          GParamSpec   *pspec)
222 {
223   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
224
225   switch (prop_id)
226     {
227     case PROP_ARGUMENTS:
228       g_assert (cmdline->priv->arguments == NULL);
229       cmdline->priv->arguments = g_value_dup_variant (value);
230       break;
231
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);
237       break;
238
239     default:
240       g_assert_not_reached ();
241     }
242 }
243
244 static void
245 g_application_command_line_finalize (GObject *object)
246 {
247   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
248
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
254   g_free (cmdline->priv->cwd);
255   g_strfreev (cmdline->priv->environ);
256
257   G_OBJECT_CLASS (g_application_command_line_parent_class)
258     ->finalize (object);
259 }
260
261 static void
262 g_application_command_line_init (GApplicationCommandLine *cmdline)
263 {
264   cmdline->priv =
265     G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
266                                  G_TYPE_APPLICATION_COMMAND_LINE,
267                                  GApplicationCommandLinePrivate);
268 }
269
270 static void
271 g_application_command_line_constructed (GObject *object)
272 {
273   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
274
275   if (IS_REMOTE (cmdline))
276     return;
277
278   /* In the local case, set cmd and environ */
279   if (!cmdline->priv->cwd)
280     cmdline->priv->cwd = g_get_current_dir ();
281
282   if (!cmdline->priv->environ)
283     cmdline->priv->environ = g_get_environ ();
284 }
285
286 static void
287 g_application_command_line_class_init (GApplicationCommandLineClass *class)
288 {
289   GObjectClass *object_class = G_OBJECT_CLASS (class);
290
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;
295
296   class->printerr_literal = g_application_command_line_real_printerr_literal;
297   class->print_literal = g_application_command_line_real_print_literal;
298
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));
306
307   g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
308     g_param_spec_variant ("platform-data",
309                           P_("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));
314
315   g_object_class_install_property (object_class, PROP_IS_REMOTE,
316     g_param_spec_boolean ("is-remote",
317                           P_("Is remote"),
318                           P_("TRUE if this is a remote commandline"),
319                           FALSE,
320                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
321
322   g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
323 }
324
325
326 /**
327  * g_application_command_line_get_arguments:
328  * @cmdline: a #GApplicationCommandLine
329  * @argc: (out): the length of the arguments array, or %NULL
330  *
331  * Gets the list of arguments that was passed on the command line.
332  *
333  * The strings in the array may contain non-utf8 data.
334  *
335  * The return value is %NULL-terminated and should be freed using
336  * g_strfreev().
337  *
338  * Returns: (array length=argc) (transfer full): the string array
339  * containing the arguments (the argv)
340  *
341  * Since: 2.28
342  **/
343 gchar **
344 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
345                                           int                     *argc)
346 {
347   gchar **argv;
348   gsize len;
349
350   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
351
352   argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
353
354   if (argc)
355     *argc = len;
356
357   return argv;
358 }
359
360 /**
361  * g_application_command_line_get_cwd:
362  * @cmdline: a #GApplicationCommandLine
363  *
364  * Gets the working directory of the command line invocation.
365  * The string may contain non-utf8 data.
366  *
367  * It is possible that the remote application did not send a working
368  * directory, so this may be %NULL.
369  *
370  * The return value should not be modified or freed and is valid for as
371  * long as @cmdline exists.
372  *
373  * Returns: the current directory, or %NULL
374  *
375  * Since: 2.28
376  **/
377 const gchar *
378 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
379 {
380   return cmdline->priv->cwd;
381 }
382
383 /**
384  * g_application_command_line_get_environ:
385  * @cmdline: a #GApplicationCommandLine
386  *
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.
391  *
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).
396  *
397  * The return value should not be modified or freed and is valid for as
398  * long as @cmdline exists.
399  *
400  * See g_application_command_line_getenv() if you are only interested
401  * in the value of a single environment variable.
402  *
403  * Returns: (array zero-terminated=1) (transfer none): the environment
404  * strings, or %NULL if they were not sent
405  *
406  * Since: 2.28
407  **/
408 const gchar * const *
409 g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
410 {
411   return (const gchar **)cmdline->priv->environ;
412 }
413
414 /**
415  * g_application_command_line_getenv:
416  * @cmdline: a #GApplicationCommandLine
417  * @name: the environment variable to get
418  *
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.
422  *
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).
427  *
428  * The return value should not be modified or freed and is valid for as
429  * long as @cmdline exists.
430  *
431  * Returns: the value of the variable, or %NULL if unset or unsent
432  *
433  * Since: 2.28
434  **/
435 const gchar *
436 g_application_command_line_getenv (GApplicationCommandLine *cmdline,
437                                    const gchar             *name)
438 {
439   gint length = strlen (name);
440   gint i;
441
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;
448
449   return NULL;
450 }
451
452 /**
453  * g_application_command_line_get_is_remote:
454  * @cmdline: a #GApplicationCommandLine
455  *
456  * Determines if @cmdline represents a remote invocation.
457  *
458  * Returns: %TRUE if the invocation was remote
459  *
460  * Since: 2.28
461  **/
462 gboolean
463 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
464 {
465   return IS_REMOTE (cmdline);
466 }
467
468 /**
469  * g_application_command_line_print:
470  * @cmdline: a #GApplicationCommandLine
471  * @format: a printf-style format string
472  * @...: arguments, as per @format
473  *
474  * Formats a message and prints it using the stdout print handler in the
475  * invoking process.
476  *
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.
480  *
481  * Since: 2.28
482  **/
483 void
484 g_application_command_line_print (GApplicationCommandLine *cmdline,
485                                   const gchar             *format,
486                                   ...)
487 {
488   gchar *message;
489   va_list ap;
490
491   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
492   g_return_if_fail (format != NULL);
493
494   va_start (ap, format);
495   message = g_strdup_vprintf (format, ap);
496   va_end (ap);
497
498   G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
499     ->print_literal (cmdline, message);
500   g_free (message);
501 }
502
503 /**
504  * g_application_command_line_printerr:
505  * @cmdline: a #GApplicationCommandLine
506  * @format: a printf-style format string
507  * @...: arguments, as per @format
508  *
509  * Formats a message and prints it using the stderr print handler in the
510  * invoking process.
511  *
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.
515  *
516  * Since: 2.28
517  **/
518 void
519 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
520                                      const gchar             *format,
521                                      ...)
522 {
523   gchar *message;
524   va_list ap;
525
526   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
527   g_return_if_fail (format != NULL);
528
529   va_start (ap, format);
530   message = g_strdup_vprintf (format, ap);
531   va_end (ap);
532
533   G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
534     ->printerr_literal (cmdline, message);
535   g_free (message);
536 }
537
538 /**
539  * g_application_command_line_set_exit_status:
540  * @cmdline: a #GApplicationCommandLine
541  * @exit_status: the exit status
542  *
543  * Sets the exit status that will be used when the invoking process
544  * exits.
545  *
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.
549  *
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.
556  *
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.
564  *
565  * Since: 2.28
566  **/
567 void
568 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
569                                             int                      exit_status)
570 {
571   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
572
573   cmdline->priv->exit_status = exit_status;
574 }
575
576 /**
577  * g_application_command_line_get_exit_status:
578  * @cmdline: a #GApplicationCommandLine
579  *
580  * Gets the exit status of @cmdline.  See
581  * g_application_command_line_set_exit_status() for more information.
582  *
583  * Returns: the exit status
584  *
585  * Since: 2.28
586  **/
587 int
588 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
589 {
590   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
591
592   return cmdline->priv->exit_status;
593 }
594
595 /**
596  * g_application_command_line_get_platform_data:
597  * @cmdline: #GApplicationCommandLine
598  *
599  * Gets the platform data associated with the invocation of @cmdline.
600  *
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
604  * notification ID.
605  *
606  * For local invocation, it will be %NULL.
607  *
608  * Returns: (allow-none): the platform data, or %NULL
609  *
610  * Since: 2.28
611  **/
612 GVariant *
613 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
614 {
615   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
616
617   if (cmdline->priv->platform_data)
618     return g_variant_ref (cmdline->priv->platform_data);
619   else
620       return NULL;
621 }