/src/glib/gio/gapplicationcommandline.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright © 2010 Codethink Limited  | 
3  |  |  *  | 
4  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later  | 
5  |  |  *  | 
6  |  |  * This library is free software; you can redistribute it and/or  | 
7  |  |  * modify it under the terms of the GNU Lesser General Public  | 
8  |  |  * License as published by the Free Software Foundation; either  | 
9  |  |  * version 2.1 of the License, or (at your option) any later version.  | 
10  |  |  *  | 
11  |  |  * This library is distributed in the hope that it will be useful, but  | 
12  |  |  * WITHOUT ANY WARRANTY; without even the implied warranty of  | 
13  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
14  |  |  * Lesser General Public License for more details.  | 
15  |  |  *  | 
16  |  |  * You should have received a copy of the GNU Lesser General Public  | 
17  |  |  * License along with this library; if not, see <http://www.gnu.org/licenses/>.  | 
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  |  | #include "gfile.h"  | 
28  |  |  | 
29  |  | #include <string.h>  | 
30  |  | #include <stdio.h>  | 
31  |  |  | 
32  |  | #ifdef G_OS_UNIX  | 
33  |  | #include "gunixinputstream.h"  | 
34  |  | #endif  | 
35  |  |  | 
36  |  | #ifdef G_OS_WIN32  | 
37  |  | #include <windows.h>  | 
38  |  | #undef environ  | 
39  |  | #include "gwin32inputstream.h"  | 
40  |  | #endif  | 
41  |  |  | 
42  |  | /**  | 
43  |  |  * SECTION:gapplicationcommandline  | 
44  |  |  * @title: GApplicationCommandLine  | 
45  |  |  * @short_description: A command-line invocation of an application  | 
46  |  |  * @include: gio/gio.h  | 
47  |  |  * @see_also: #GApplication  | 
48  |  |  *  | 
49  |  |  * #GApplicationCommandLine represents a command-line invocation of  | 
50  |  |  * an application.  It is created by #GApplication and emitted  | 
51  |  |  * in the #GApplication::command-line signal and virtual function.  | 
52  |  |  *  | 
53  |  |  * The class contains the list of arguments that the program was invoked  | 
54  |  |  * with.  It is also possible to query if the commandline invocation was  | 
55  |  |  * local (ie: the current process is running in direct response to the  | 
56  |  |  * invocation) or remote (ie: some other process forwarded the  | 
57  |  |  * commandline to this process).  | 
58  |  |  *  | 
59  |  |  * The GApplicationCommandLine object can provide the @argc and @argv  | 
60  |  |  * parameters for use with the #GOptionContext command-line parsing API,  | 
61  |  |  * with the g_application_command_line_get_arguments() function. See  | 
62  |  |  * [gapplication-example-cmdline3.c][gapplication-example-cmdline3]  | 
63  |  |  * for an example.  | 
64  |  |  *  | 
65  |  |  * The exit status of the originally-invoked process may be set and  | 
66  |  |  * messages can be printed to stdout or stderr of that process.  The  | 
67  |  |  * lifecycle of the originally-invoked process is tied to the lifecycle  | 
68  |  |  * of this object (ie: the process exits when the last reference is  | 
69  |  |  * dropped).  | 
70  |  |  *  | 
71  |  |  * The main use for #GApplicationCommandLine (and the  | 
72  |  |  * #GApplication::command-line signal) is 'Emacs server' like use cases:  | 
73  |  |  * You can set the `EDITOR` environment variable to have e.g. git use  | 
74  |  |  * your favourite editor to edit commit messages, and if you already  | 
75  |  |  * have an instance of the editor running, the editing will happen  | 
76  |  |  * in the running instance, instead of opening a new one. An important  | 
77  |  |  * aspect of this use case is that the process that gets started by git  | 
78  |  |  * does not return until the editing is done.  | 
79  |  |  *  | 
80  |  |  * Normally, the commandline is completely handled in the  | 
81  |  |  * #GApplication::command-line handler. The launching instance exits  | 
82  |  |  * once the signal handler in the primary instance has returned, and  | 
83  |  |  * the return value of the signal handler becomes the exit status  | 
84  |  |  * of the launching instance.  | 
85  |  |  * |[<!-- language="C" -->  | 
86  |  |  * static int  | 
87  |  |  * command_line (GApplication            *application,  | 
88  |  |  *               GApplicationCommandLine *cmdline)  | 
89  |  |  * { | 
90  |  |  *   gchar **argv;  | 
91  |  |  *   gint argc;  | 
92  |  |  *   gint i;  | 
93  |  |  *  | 
94  |  |  *   argv = g_application_command_line_get_arguments (cmdline, &argc);  | 
95  |  |  *  | 
96  |  |  *   g_application_command_line_print (cmdline,  | 
97  |  |  *                                     "This text is written back\n"  | 
98  |  |  *                                     "to stdout of the caller\n");  | 
99  |  |  *  | 
100  |  |  *   for (i = 0; i < argc; i++)  | 
101  |  |  *     g_print ("argument %d: %s\n", i, argv[i]); | 
102  |  |  *  | 
103  |  |  *   g_strfreev (argv);  | 
104  |  |  *  | 
105  |  |  *   return 0;  | 
106  |  |  * }  | 
107  |  |  * ]|  | 
108  |  |  * The complete example can be found here:   | 
109  |  |  * [gapplication-example-cmdline.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gapplication-example-cmdline.c)  | 
110  |  |  *  | 
111  |  |  * In more complicated cases, the handling of the commandline can be  | 
112  |  |  * split between the launcher and the primary instance.  | 
113  |  |  * |[<!-- language="C" -->  | 
114  |  |  * static gboolean  | 
115  |  |  *  test_local_cmdline (GApplication   *application,  | 
116  |  |  *                      gchar        ***arguments,  | 
117  |  |  *                      gint           *exit_status)  | 
118  |  |  * { | 
119  |  |  *   gint i, j;  | 
120  |  |  *   gchar **argv;  | 
121  |  |  *  | 
122  |  |  *   argv = *arguments;  | 
123  |  |  *  | 
124  |  |  *   if (argv[0] == NULL)  | 
125  |  |  *     { | 
126  |  |  *       *exit_status = 0;  | 
127  |  |  *       return FALSE;  | 
128  |  |  *     }  | 
129  |  |  *  | 
130  |  |  *   i = 1;  | 
131  |  |  *   while (argv[i])  | 
132  |  |  *     { | 
133  |  |  *       if (g_str_has_prefix (argv[i], "--local-"))  | 
134  |  |  *         { | 
135  |  |  *           g_print ("handling argument %s locally\n", argv[i]); | 
136  |  |  *           g_free (argv[i]);  | 
137  |  |  *           for (j = i; argv[j]; j++)  | 
138  |  |  *             argv[j] = argv[j + 1];  | 
139  |  |  *         }  | 
140  |  |  *       else  | 
141  |  |  *         { | 
142  |  |  *           g_print ("not handling argument %s locally\n", argv[i]); | 
143  |  |  *           i++;  | 
144  |  |  *         }  | 
145  |  |  *     }  | 
146  |  |  *  | 
147  |  |  *   *exit_status = 0;  | 
148  |  |  *  | 
149  |  |  *   return FALSE;  | 
150  |  |  * }  | 
151  |  |  *  | 
152  |  |  * static void  | 
153  |  |  * test_application_class_init (TestApplicationClass *class)  | 
154  |  |  * { | 
155  |  |  *   G_APPLICATION_CLASS (class)->local_command_line = test_local_cmdline;  | 
156  |  |  *  | 
157  |  |  *   ...  | 
158  |  |  * }  | 
159  |  |  * ]|  | 
160  |  |  * In this example of split commandline handling, options that start  | 
161  |  |  * with `--local-` are handled locally, all other options are passed  | 
162  |  |  * to the #GApplication::command-line handler which runs in the primary  | 
163  |  |  * instance.  | 
164  |  |  *  | 
165  |  |  * The complete example can be found here:  | 
166  |  |  * [gapplication-example-cmdline2.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gapplication-example-cmdline2.c)  | 
167  |  |  *  | 
168  |  |  * If handling the commandline requires a lot of work, it may  | 
169  |  |  * be better to defer it.  | 
170  |  |  * |[<!-- language="C" -->  | 
171  |  |  * static gboolean  | 
172  |  |  * my_cmdline_handler (gpointer data)  | 
173  |  |  * { | 
174  |  |  *   GApplicationCommandLine *cmdline = data;  | 
175  |  |  *  | 
176  |  |  *   // do the heavy lifting in an idle  | 
177  |  |  *  | 
178  |  |  *   g_application_command_line_set_exit_status (cmdline, 0);  | 
179  |  |  *   g_object_unref (cmdline); // this releases the application  | 
180  |  |  *  | 
181  |  |  *   return G_SOURCE_REMOVE;  | 
182  |  |  * }  | 
183  |  |  *  | 
184  |  |  * static int  | 
185  |  |  * command_line (GApplication            *application,  | 
186  |  |  *               GApplicationCommandLine *cmdline)  | 
187  |  |  * { | 
188  |  |  *   // keep the application running until we are done with this commandline  | 
189  |  |  *   g_application_hold (application);  | 
190  |  |  *  | 
191  |  |  *   g_object_set_data_full (G_OBJECT (cmdline),  | 
192  |  |  *                           "application", application,  | 
193  |  |  *                           (GDestroyNotify)g_application_release);  | 
194  |  |  *  | 
195  |  |  *   g_object_ref (cmdline);  | 
196  |  |  *   g_idle_add (my_cmdline_handler, cmdline);  | 
197  |  |  *  | 
198  |  |  *   return 0;  | 
199  |  |  * }  | 
200  |  |  * ]|  | 
201  |  |  * In this example the commandline is not completely handled before  | 
202  |  |  * the #GApplication::command-line handler returns. Instead, we keep  | 
203  |  |  * a reference to the #GApplicationCommandLine object and handle it  | 
204  |  |  * later (in this example, in an idle). Note that it is necessary to  | 
205  |  |  * hold the application until you are done with the commandline.  | 
206  |  |  *  | 
207  |  |  * The complete example can be found here:  | 
208  |  |  * [gapplication-example-cmdline3.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gapplication-example-cmdline3.c)  | 
209  |  |  */  | 
210  |  |  | 
211  |  | /**  | 
212  |  |  * GApplicationCommandLine:  | 
213  |  |  *  | 
214  |  |  * #GApplicationCommandLine is an opaque data structure and can only be accessed  | 
215  |  |  * using the following functions.  | 
216  |  |  */  | 
217  |  |  | 
218  |  | /**  | 
219  |  |  * GApplicationCommandLineClass:  | 
220  |  |  *  | 
221  |  |  * The #GApplicationCommandLineClass-struct   | 
222  |  |  * contains private data only.  | 
223  |  |  *  | 
224  |  |  * Since: 2.28  | 
225  |  |  **/  | 
226  |  | enum  | 
227  |  | { | 
228  |  |   PROP_NONE,  | 
229  |  |   PROP_ARGUMENTS,  | 
230  |  |   PROP_OPTIONS,  | 
231  |  |   PROP_PLATFORM_DATA,  | 
232  |  |   PROP_IS_REMOTE  | 
233  |  | };  | 
234  |  |  | 
235  |  | struct _GApplicationCommandLinePrivate  | 
236  |  | { | 
237  |  |   GVariant *platform_data;  | 
238  |  |   GVariant *arguments;  | 
239  |  |   GVariant *options;  | 
240  |  |   GVariantDict *options_dict;  | 
241  |  |   gchar *cwd;  /* in GLib filename encoding, not UTF-8 */  | 
242  |  |  | 
243  |  |   gchar **environ;  | 
244  |  |   gint exit_status;  | 
245  |  | };  | 
246  |  |  | 
247  |  | G_DEFINE_TYPE_WITH_PRIVATE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJECT)  | 
248  |  |  | 
249  |  | /* All subclasses represent remote invocations of some kind. */  | 
250  | 0  | #define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \  | 
251  | 0  |                             G_TYPE_APPLICATION_COMMAND_LINE)  | 
252  |  |  | 
253  |  | static void  | 
254  |  | grok_platform_data (GApplicationCommandLine *cmdline)  | 
255  | 0  | { | 
256  | 0  |   GVariantIter iter;  | 
257  | 0  |   const gchar *key;  | 
258  | 0  |   GVariant *value;  | 
259  |  | 
  | 
260  | 0  |   g_variant_iter_init (&iter, cmdline->priv->platform_data);  | 
261  |  | 
  | 
262  | 0  |   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value)) | 
263  | 0  |     if (strcmp (key, "cwd") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_BYTESTRING))  | 
264  | 0  |       { | 
265  | 0  |         if (!cmdline->priv->cwd)  | 
266  | 0  |           cmdline->priv->cwd = g_variant_dup_bytestring (value, NULL);  | 
267  | 0  |       }  | 
268  |  |  | 
269  | 0  |     else if (strcmp (key, "environ") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_BYTESTRING_ARRAY))  | 
270  | 0  |       { | 
271  | 0  |         if (!cmdline->priv->environ)  | 
272  | 0  |           cmdline->priv->environ =  | 
273  | 0  |             g_variant_dup_bytestring_array (value, NULL);  | 
274  | 0  |       }  | 
275  |  |  | 
276  | 0  |     else if (strcmp (key, "options") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_VARDICT))  | 
277  | 0  |       { | 
278  | 0  |         if (!cmdline->priv->options)  | 
279  | 0  |           cmdline->priv->options = g_variant_ref (value);  | 
280  | 0  |       }  | 
281  | 0  | }  | 
282  |  |  | 
283  |  | static void  | 
284  |  | g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,  | 
285  |  |                                                const gchar             *message)  | 
286  | 0  | { | 
287  | 0  |   g_print ("%s", message); | 
288  | 0  | }  | 
289  |  |  | 
290  |  | static void  | 
291  |  | g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,  | 
292  |  |                                                   const gchar             *message)  | 
293  | 0  | { | 
294  | 0  |   g_printerr ("%s", message); | 
295  | 0  | }  | 
296  |  |  | 
297  |  | static GInputStream *  | 
298  |  | g_application_command_line_real_get_stdin (GApplicationCommandLine *cmdline)  | 
299  | 0  | { | 
300  | 0  | #ifdef G_OS_UNIX  | 
301  | 0  |   return g_unix_input_stream_new (0, FALSE);  | 
302  |  | #else  | 
303  |  |   return g_win32_input_stream_new (GetStdHandle (STD_INPUT_HANDLE), FALSE);  | 
304  |  | #endif  | 
305  | 0  | }  | 
306  |  |  | 
307  |  | static void  | 
308  |  | g_application_command_line_get_property (GObject    *object,  | 
309  |  |                                          guint       prop_id,  | 
310  |  |                                          GValue     *value,  | 
311  |  |                                          GParamSpec *pspec)  | 
312  | 0  | { | 
313  | 0  |   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);  | 
314  |  | 
  | 
315  | 0  |   switch (prop_id)  | 
316  | 0  |     { | 
317  | 0  |     case PROP_ARGUMENTS:  | 
318  | 0  |       g_value_set_variant (value, cmdline->priv->arguments);  | 
319  | 0  |       break;  | 
320  |  |  | 
321  | 0  |     case PROP_PLATFORM_DATA:  | 
322  | 0  |       g_value_set_variant (value, cmdline->priv->platform_data);  | 
323  | 0  |       break;  | 
324  |  |  | 
325  | 0  |     case PROP_IS_REMOTE:  | 
326  | 0  |       g_value_set_boolean (value, IS_REMOTE (cmdline));  | 
327  | 0  |       break;  | 
328  |  |  | 
329  | 0  |     default:  | 
330  | 0  |       g_assert_not_reached ();  | 
331  | 0  |     }  | 
332  | 0  | }  | 
333  |  |  | 
334  |  | static void  | 
335  |  | g_application_command_line_set_property (GObject      *object,  | 
336  |  |                                          guint         prop_id,  | 
337  |  |                                          const GValue *value,  | 
338  |  |                                          GParamSpec   *pspec)  | 
339  | 0  | { | 
340  | 0  |   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);  | 
341  |  | 
  | 
342  | 0  |   switch (prop_id)  | 
343  | 0  |     { | 
344  | 0  |     case PROP_ARGUMENTS:  | 
345  | 0  |       g_assert (cmdline->priv->arguments == NULL);  | 
346  | 0  |       cmdline->priv->arguments = g_value_dup_variant (value);  | 
347  | 0  |       break;  | 
348  |  |  | 
349  | 0  |     case PROP_OPTIONS:  | 
350  | 0  |       g_assert (cmdline->priv->options == NULL);  | 
351  | 0  |       cmdline->priv->options = g_value_dup_variant (value);  | 
352  | 0  |       break;  | 
353  |  |  | 
354  | 0  |     case PROP_PLATFORM_DATA:  | 
355  | 0  |       g_assert (cmdline->priv->platform_data == NULL);  | 
356  | 0  |       cmdline->priv->platform_data = g_value_dup_variant (value);  | 
357  | 0  |       if (cmdline->priv->platform_data != NULL)  | 
358  | 0  |         grok_platform_data (cmdline);  | 
359  | 0  |       break;  | 
360  |  |  | 
361  | 0  |     default:  | 
362  | 0  |       g_assert_not_reached ();  | 
363  | 0  |     }  | 
364  | 0  | }  | 
365  |  |  | 
366  |  | static void  | 
367  |  | g_application_command_line_finalize (GObject *object)  | 
368  | 0  | { | 
369  | 0  |   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);  | 
370  |  | 
  | 
371  | 0  |   if (cmdline->priv->options_dict)  | 
372  | 0  |     g_variant_dict_unref (cmdline->priv->options_dict);  | 
373  |  | 
  | 
374  | 0  |   if (cmdline->priv->options)  | 
375  | 0  |       g_variant_unref (cmdline->priv->options);  | 
376  |  | 
  | 
377  | 0  |   if (cmdline->priv->platform_data)  | 
378  | 0  |     g_variant_unref (cmdline->priv->platform_data);  | 
379  | 0  |   if (cmdline->priv->arguments)  | 
380  | 0  |     g_variant_unref (cmdline->priv->arguments);  | 
381  |  | 
  | 
382  | 0  |   g_free (cmdline->priv->cwd);  | 
383  | 0  |   g_strfreev (cmdline->priv->environ);  | 
384  |  | 
  | 
385  | 0  |   G_OBJECT_CLASS (g_application_command_line_parent_class)  | 
386  | 0  |     ->finalize (object);  | 
387  | 0  | }  | 
388  |  |  | 
389  |  | static void  | 
390  |  | g_application_command_line_init (GApplicationCommandLine *cmdline)  | 
391  | 0  | { | 
392  | 0  |   cmdline->priv = g_application_command_line_get_instance_private (cmdline);  | 
393  | 0  | }  | 
394  |  |  | 
395  |  | static void  | 
396  |  | g_application_command_line_constructed (GObject *object)  | 
397  | 0  | { | 
398  | 0  |   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);  | 
399  |  | 
  | 
400  | 0  |   if (IS_REMOTE (cmdline))  | 
401  | 0  |     return;  | 
402  |  |  | 
403  |  |   /* In the local case, set cmd and environ */  | 
404  | 0  |   if (!cmdline->priv->cwd)  | 
405  | 0  |     cmdline->priv->cwd = g_get_current_dir ();  | 
406  |  | 
  | 
407  | 0  |   if (!cmdline->priv->environ)  | 
408  | 0  |     cmdline->priv->environ = g_get_environ ();  | 
409  | 0  | }  | 
410  |  |  | 
411  |  | static void  | 
412  |  | g_application_command_line_class_init (GApplicationCommandLineClass *class)  | 
413  | 0  | { | 
414  | 0  |   GObjectClass *object_class = G_OBJECT_CLASS (class);  | 
415  |  | 
  | 
416  | 0  |   object_class->get_property = g_application_command_line_get_property;  | 
417  | 0  |   object_class->set_property = g_application_command_line_set_property;  | 
418  | 0  |   object_class->finalize = g_application_command_line_finalize;  | 
419  | 0  |   object_class->constructed = g_application_command_line_constructed;  | 
420  |  | 
  | 
421  | 0  |   class->printerr_literal = g_application_command_line_real_printerr_literal;  | 
422  | 0  |   class->print_literal = g_application_command_line_real_print_literal;  | 
423  | 0  |   class->get_stdin = g_application_command_line_real_get_stdin;  | 
424  |  | 
  | 
425  | 0  |   g_object_class_install_property (object_class, PROP_ARGUMENTS,  | 
426  | 0  |     g_param_spec_variant ("arguments", | 
427  | 0  |                           P_("Commandline arguments"), | 
428  | 0  |                           P_("The commandline that caused this ::command-line signal emission"), | 
429  | 0  |                           G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,  | 
430  | 0  |                           G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |  | 
431  | 0  |                           G_PARAM_STATIC_STRINGS));  | 
432  |  | 
  | 
433  | 0  |   g_object_class_install_property (object_class, PROP_OPTIONS,  | 
434  | 0  |     g_param_spec_variant ("options", | 
435  | 0  |                           P_("Options"), | 
436  | 0  |                           P_("The options sent along with the commandline"), | 
437  | 0  |                           G_VARIANT_TYPE_VARDICT, NULL, G_PARAM_WRITABLE |  | 
438  | 0  |                           G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));  | 
439  |  | 
  | 
440  | 0  |   g_object_class_install_property (object_class, PROP_PLATFORM_DATA,  | 
441  | 0  |     g_param_spec_variant ("platform-data", | 
442  | 0  |                           P_("Platform data"), | 
443  | 0  |                           P_("Platform-specific data for the commandline"), | 
444  | 0  |                           G_VARIANT_TYPE ("a{sv}"), NULL, | 
445  | 0  |                           G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |  | 
446  | 0  |                           G_PARAM_STATIC_STRINGS));  | 
447  |  | 
  | 
448  | 0  |   g_object_class_install_property (object_class, PROP_IS_REMOTE,  | 
449  | 0  |     g_param_spec_boolean ("is-remote", | 
450  | 0  |                           P_("Is remote"), | 
451  | 0  |                           P_("TRUE if this is a remote commandline"), | 
452  | 0  |                           FALSE,  | 
453  | 0  |                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));  | 
454  | 0  | }  | 
455  |  |  | 
456  |  |  | 
457  |  | /**  | 
458  |  |  * g_application_command_line_get_arguments:  | 
459  |  |  * @cmdline: a #GApplicationCommandLine  | 
460  |  |  * @argc: (out) (optional): the length of the arguments array, or %NULL  | 
461  |  |  *  | 
462  |  |  * Gets the list of arguments that was passed on the command line.  | 
463  |  |  *  | 
464  |  |  * The strings in the array may contain non-UTF-8 data on UNIX (such as  | 
465  |  |  * filenames or arguments given in the system locale) but are always in  | 
466  |  |  * UTF-8 on Windows.  | 
467  |  |  *  | 
468  |  |  * If you wish to use the return value with #GOptionContext, you must  | 
469  |  |  * use g_option_context_parse_strv().  | 
470  |  |  *  | 
471  |  |  * The return value is %NULL-terminated and should be freed using  | 
472  |  |  * g_strfreev().  | 
473  |  |  *  | 
474  |  |  * Returns: (array length=argc) (element-type filename) (transfer full)  | 
475  |  |  *      the string array containing the arguments (the argv)  | 
476  |  |  *  | 
477  |  |  * Since: 2.28  | 
478  |  |  **/  | 
479  |  | gchar **  | 
480  |  | g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,  | 
481  |  |                                           int                     *argc)  | 
482  | 0  | { | 
483  | 0  |   gchar **argv;  | 
484  | 0  |   gsize len;  | 
485  |  | 
  | 
486  | 0  |   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);  | 
487  |  |  | 
488  | 0  |   argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);  | 
489  |  | 
  | 
490  | 0  |   if (argc)  | 
491  | 0  |     *argc = len;  | 
492  |  | 
  | 
493  | 0  |   return argv;  | 
494  | 0  | }  | 
495  |  |  | 
496  |  | /**  | 
497  |  |  * g_application_command_line_get_options_dict:  | 
498  |  |  * @cmdline: a #GApplicationCommandLine  | 
499  |  |  *  | 
500  |  |  * Gets the options that were passed to g_application_command_line().  | 
501  |  |  *  | 
502  |  |  * If you did not override local_command_line() then these are the same  | 
503  |  |  * options that were parsed according to the #GOptionEntrys added to the  | 
504  |  |  * application with g_application_add_main_option_entries() and possibly  | 
505  |  |  * modified from your GApplication::handle-local-options handler.  | 
506  |  |  *  | 
507  |  |  * If no options were sent then an empty dictionary is returned so that  | 
508  |  |  * you don't need to check for %NULL.  | 
509  |  |  *  | 
510  |  |  * The data has been passed via an untrusted external process, so the types of  | 
511  |  |  * all values must be checked before being used.  | 
512  |  |  *  | 
513  |  |  * Returns: (transfer none): a #GVariantDict with the options  | 
514  |  |  *  | 
515  |  |  * Since: 2.40  | 
516  |  |  **/  | 
517  |  | GVariantDict *  | 
518  |  | g_application_command_line_get_options_dict (GApplicationCommandLine *cmdline)  | 
519  | 0  | { | 
520  | 0  |   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);  | 
521  |  |  | 
522  | 0  |   if (!cmdline->priv->options_dict)  | 
523  | 0  |     cmdline->priv->options_dict = g_variant_dict_new (cmdline->priv->options);  | 
524  |  | 
  | 
525  | 0  |   return cmdline->priv->options_dict;  | 
526  | 0  | }  | 
527  |  |  | 
528  |  | /**  | 
529  |  |  * g_application_command_line_get_stdin:  | 
530  |  |  * @cmdline: a #GApplicationCommandLine  | 
531  |  |  *  | 
532  |  |  * Gets the stdin of the invoking process.  | 
533  |  |  *  | 
534  |  |  * The #GInputStream can be used to read data passed to the standard  | 
535  |  |  * input of the invoking process.  | 
536  |  |  * This doesn't work on all platforms.  Presently, it is only available  | 
537  |  |  * on UNIX when using a D-Bus daemon capable of passing file descriptors.  | 
538  |  |  * If stdin is not available then %NULL will be returned.  In the  | 
539  |  |  * future, support may be expanded to other platforms.  | 
540  |  |  *  | 
541  |  |  * You must only call this function once per commandline invocation.  | 
542  |  |  *  | 
543  |  |  * Returns: (nullable) (transfer full): a #GInputStream for stdin  | 
544  |  |  *  | 
545  |  |  * Since: 2.34  | 
546  |  |  **/  | 
547  |  | GInputStream *  | 
548  |  | g_application_command_line_get_stdin (GApplicationCommandLine *cmdline)  | 
549  | 0  | { | 
550  | 0  |   return G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)->get_stdin (cmdline);  | 
551  | 0  | }  | 
552  |  |  | 
553  |  | /**  | 
554  |  |  * g_application_command_line_get_cwd:  | 
555  |  |  * @cmdline: a #GApplicationCommandLine  | 
556  |  |  *  | 
557  |  |  * Gets the working directory of the command line invocation.  | 
558  |  |  * The string may contain non-utf8 data.  | 
559  |  |  *  | 
560  |  |  * It is possible that the remote application did not send a working  | 
561  |  |  * directory, so this may be %NULL.  | 
562  |  |  *  | 
563  |  |  * The return value should not be modified or freed and is valid for as  | 
564  |  |  * long as @cmdline exists.  | 
565  |  |  *  | 
566  |  |  * Returns: (nullable) (type filename): the current directory, or %NULL  | 
567  |  |  *  | 
568  |  |  * Since: 2.28  | 
569  |  |  **/  | 
570  |  | const gchar *  | 
571  |  | g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)  | 
572  | 0  | { | 
573  | 0  |   return cmdline->priv->cwd;  | 
574  | 0  | }  | 
575  |  |  | 
576  |  | /**  | 
577  |  |  * g_application_command_line_get_environ:  | 
578  |  |  * @cmdline: a #GApplicationCommandLine  | 
579  |  |  *  | 
580  |  |  * Gets the contents of the 'environ' variable of the command line  | 
581  |  |  * invocation, as would be returned by g_get_environ(), ie as a  | 
582  |  |  * %NULL-terminated list of strings in the form 'NAME=VALUE'.  | 
583  |  |  * The strings may contain non-utf8 data.  | 
584  |  |  *  | 
585  |  |  * The remote application usually does not send an environment.  Use  | 
586  |  |  * %G_APPLICATION_SEND_ENVIRONMENT to affect that.  Even with this flag  | 
587  |  |  * set it is possible that the environment is still not available (due  | 
588  |  |  * to invocation messages from other applications).  | 
589  |  |  *  | 
590  |  |  * The return value should not be modified or freed and is valid for as  | 
591  |  |  * long as @cmdline exists.  | 
592  |  |  *  | 
593  |  |  * See g_application_command_line_getenv() if you are only interested  | 
594  |  |  * in the value of a single environment variable.  | 
595  |  |  *  | 
596  |  |  * Returns: (array zero-terminated=1) (element-type filename) (transfer none):  | 
597  |  |  *     the environment strings, or %NULL if they were not sent  | 
598  |  |  *  | 
599  |  |  * Since: 2.28  | 
600  |  |  **/  | 
601  |  | const gchar * const *  | 
602  |  | g_application_command_line_get_environ (GApplicationCommandLine *cmdline)  | 
603  | 0  | { | 
604  | 0  |   return (const gchar **)cmdline->priv->environ;  | 
605  | 0  | }  | 
606  |  |  | 
607  |  | /**  | 
608  |  |  * g_application_command_line_getenv:  | 
609  |  |  * @cmdline: a #GApplicationCommandLine  | 
610  |  |  * @name: (type filename): the environment variable to get  | 
611  |  |  *  | 
612  |  |  * Gets the value of a particular environment variable of the command  | 
613  |  |  * line invocation, as would be returned by g_getenv().  The strings may  | 
614  |  |  * contain non-utf8 data.  | 
615  |  |  *  | 
616  |  |  * The remote application usually does not send an environment.  Use  | 
617  |  |  * %G_APPLICATION_SEND_ENVIRONMENT to affect that.  Even with this flag  | 
618  |  |  * set it is possible that the environment is still not available (due  | 
619  |  |  * to invocation messages from other applications).  | 
620  |  |  *  | 
621  |  |  * The return value should not be modified or freed and is valid for as  | 
622  |  |  * long as @cmdline exists.  | 
623  |  |  *  | 
624  |  |  * Returns: (nullable): the value of the variable, or %NULL if unset or unsent  | 
625  |  |  *  | 
626  |  |  * Since: 2.28  | 
627  |  |  **/  | 
628  |  | const gchar *  | 
629  |  | g_application_command_line_getenv (GApplicationCommandLine *cmdline,  | 
630  |  |                                    const gchar             *name)  | 
631  | 0  | { | 
632  | 0  |   gint length = strlen (name);  | 
633  | 0  |   gint i;  | 
634  |  |  | 
635  |  |   /* TODO: expand on windows */  | 
636  | 0  |   if (cmdline->priv->environ)  | 
637  | 0  |     for (i = 0; cmdline->priv->environ[i]; i++)  | 
638  | 0  |       if (strncmp (cmdline->priv->environ[i], name, length) == 0 &&  | 
639  | 0  |           cmdline->priv->environ[i][length] == '=')  | 
640  | 0  |         return cmdline->priv->environ[i] + length + 1;  | 
641  |  |  | 
642  | 0  |   return NULL;  | 
643  | 0  | }  | 
644  |  |  | 
645  |  | /**  | 
646  |  |  * g_application_command_line_get_is_remote:  | 
647  |  |  * @cmdline: a #GApplicationCommandLine  | 
648  |  |  *  | 
649  |  |  * Determines if @cmdline represents a remote invocation.  | 
650  |  |  *  | 
651  |  |  * Returns: %TRUE if the invocation was remote  | 
652  |  |  *  | 
653  |  |  * Since: 2.28  | 
654  |  |  **/  | 
655  |  | gboolean  | 
656  |  | g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)  | 
657  | 0  | { | 
658  | 0  |   return IS_REMOTE (cmdline);  | 
659  | 0  | }  | 
660  |  |  | 
661  |  | /**  | 
662  |  |  * g_application_command_line_print:  | 
663  |  |  * @cmdline: a #GApplicationCommandLine  | 
664  |  |  * @format: a printf-style format string  | 
665  |  |  * @...: arguments, as per @format  | 
666  |  |  *  | 
667  |  |  * Formats a message and prints it using the stdout print handler in the  | 
668  |  |  * invoking process.  | 
669  |  |  *  | 
670  |  |  * If @cmdline is a local invocation then this is exactly equivalent to  | 
671  |  |  * g_print().  If @cmdline is remote then this is equivalent to calling  | 
672  |  |  * g_print() in the invoking process.  | 
673  |  |  *  | 
674  |  |  * Since: 2.28  | 
675  |  |  **/  | 
676  |  | void  | 
677  |  | g_application_command_line_print (GApplicationCommandLine *cmdline,  | 
678  |  |                                   const gchar             *format,  | 
679  |  |                                   ...)  | 
680  | 0  | { | 
681  | 0  |   gchar *message;  | 
682  | 0  |   va_list ap;  | 
683  |  | 
  | 
684  | 0  |   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));  | 
685  | 0  |   g_return_if_fail (format != NULL);  | 
686  |  |  | 
687  | 0  |   va_start (ap, format);  | 
688  | 0  |   message = g_strdup_vprintf (format, ap);  | 
689  | 0  |   va_end (ap);  | 
690  |  | 
  | 
691  | 0  |   G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)  | 
692  | 0  |     ->print_literal (cmdline, message);  | 
693  | 0  |   g_free (message);  | 
694  | 0  | }  | 
695  |  |  | 
696  |  | /**  | 
697  |  |  * g_application_command_line_printerr:  | 
698  |  |  * @cmdline: a #GApplicationCommandLine  | 
699  |  |  * @format: a printf-style format string  | 
700  |  |  * @...: arguments, as per @format  | 
701  |  |  *  | 
702  |  |  * Formats a message and prints it using the stderr print handler in the  | 
703  |  |  * invoking process.  | 
704  |  |  *  | 
705  |  |  * If @cmdline is a local invocation then this is exactly equivalent to  | 
706  |  |  * g_printerr().  If @cmdline is remote then this is equivalent to  | 
707  |  |  * calling g_printerr() in the invoking process.  | 
708  |  |  *  | 
709  |  |  * Since: 2.28  | 
710  |  |  **/  | 
711  |  | void  | 
712  |  | g_application_command_line_printerr (GApplicationCommandLine *cmdline,  | 
713  |  |                                      const gchar             *format,  | 
714  |  |                                      ...)  | 
715  | 0  | { | 
716  | 0  |   gchar *message;  | 
717  | 0  |   va_list ap;  | 
718  |  | 
  | 
719  | 0  |   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));  | 
720  | 0  |   g_return_if_fail (format != NULL);  | 
721  |  |  | 
722  | 0  |   va_start (ap, format);  | 
723  | 0  |   message = g_strdup_vprintf (format, ap);  | 
724  | 0  |   va_end (ap);  | 
725  |  | 
  | 
726  | 0  |   G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)  | 
727  | 0  |     ->printerr_literal (cmdline, message);  | 
728  | 0  |   g_free (message);  | 
729  | 0  | }  | 
730  |  |  | 
731  |  | /**  | 
732  |  |  * g_application_command_line_set_exit_status:  | 
733  |  |  * @cmdline: a #GApplicationCommandLine  | 
734  |  |  * @exit_status: the exit status  | 
735  |  |  *  | 
736  |  |  * Sets the exit status that will be used when the invoking process  | 
737  |  |  * exits.  | 
738  |  |  *  | 
739  |  |  * The return value of the #GApplication::command-line signal is  | 
740  |  |  * passed to this function when the handler returns.  This is the usual  | 
741  |  |  * way of setting the exit status.  | 
742  |  |  *  | 
743  |  |  * In the event that you want the remote invocation to continue running  | 
744  |  |  * and want to decide on the exit status in the future, you can use this  | 
745  |  |  * call.  For the case of a remote invocation, the remote process will  | 
746  |  |  * typically exit when the last reference is dropped on @cmdline.  The  | 
747  |  |  * exit status of the remote process will be equal to the last value  | 
748  |  |  * that was set with this function.  | 
749  |  |  *  | 
750  |  |  * In the case that the commandline invocation is local, the situation  | 
751  |  |  * is slightly more complicated.  If the commandline invocation results  | 
752  |  |  * in the mainloop running (ie: because the use-count of the application  | 
753  |  |  * increased to a non-zero value) then the application is considered to  | 
754  |  |  * have been 'successful' in a certain sense, and the exit status is  | 
755  |  |  * always zero.  If the application use count is zero, though, the exit  | 
756  |  |  * status of the local #GApplicationCommandLine is used.  | 
757  |  |  *  | 
758  |  |  * Since: 2.28  | 
759  |  |  **/  | 
760  |  | void  | 
761  |  | g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,  | 
762  |  |                                             int                      exit_status)  | 
763  | 0  | { | 
764  | 0  |   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));  | 
765  |  |  | 
766  | 0  |   cmdline->priv->exit_status = exit_status;  | 
767  | 0  | }  | 
768  |  |  | 
769  |  | /**  | 
770  |  |  * g_application_command_line_get_exit_status:  | 
771  |  |  * @cmdline: a #GApplicationCommandLine  | 
772  |  |  *  | 
773  |  |  * Gets the exit status of @cmdline.  See  | 
774  |  |  * g_application_command_line_set_exit_status() for more information.  | 
775  |  |  *  | 
776  |  |  * Returns: the exit status  | 
777  |  |  *  | 
778  |  |  * Since: 2.28  | 
779  |  |  **/  | 
780  |  | int  | 
781  |  | g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)  | 
782  | 0  | { | 
783  | 0  |   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);  | 
784  |  |  | 
785  | 0  |   return cmdline->priv->exit_status;  | 
786  | 0  | }  | 
787  |  |  | 
788  |  | /**  | 
789  |  |  * g_application_command_line_get_platform_data:  | 
790  |  |  * @cmdline: #GApplicationCommandLine  | 
791  |  |  *  | 
792  |  |  * Gets the platform data associated with the invocation of @cmdline.  | 
793  |  |  *  | 
794  |  |  * This is a #GVariant dictionary containing information about the  | 
795  |  |  * context in which the invocation occurred.  It typically contains  | 
796  |  |  * information like the current working directory and the startup  | 
797  |  |  * notification ID.  | 
798  |  |  *  | 
799  |  |  * It comes from an untrusted external process and hence the types of all  | 
800  |  |  * values must be validated before being used.  | 
801  |  |  *  | 
802  |  |  * For local invocation, it will be %NULL.  | 
803  |  |  *  | 
804  |  |  * Returns: (nullable) (transfer full): the platform data, or %NULL  | 
805  |  |  *  | 
806  |  |  * Since: 2.28  | 
807  |  |  **/  | 
808  |  | GVariant *  | 
809  |  | g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)  | 
810  | 0  | { | 
811  | 0  |   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);  | 
812  |  |  | 
813  | 0  |   if (cmdline->priv->platform_data)  | 
814  | 0  |     return g_variant_ref (cmdline->priv->platform_data);  | 
815  | 0  |   else  | 
816  | 0  |       return NULL;  | 
817  | 0  | }  | 
818  |  |  | 
819  |  | /**  | 
820  |  |  * g_application_command_line_create_file_for_arg:  | 
821  |  |  * @cmdline: a #GApplicationCommandLine  | 
822  |  |  * @arg: (type filename): an argument from @cmdline  | 
823  |  |  *  | 
824  |  |  * Creates a #GFile corresponding to a filename that was given as part  | 
825  |  |  * of the invocation of @cmdline.  | 
826  |  |  *  | 
827  |  |  * This differs from g_file_new_for_commandline_arg() in that it  | 
828  |  |  * resolves relative pathnames using the current working directory of  | 
829  |  |  * the invoking process rather than the local process.  | 
830  |  |  *  | 
831  |  |  * Returns: (transfer full): a new #GFile  | 
832  |  |  *  | 
833  |  |  * Since: 2.36  | 
834  |  |  **/  | 
835  |  | GFile *  | 
836  |  | g_application_command_line_create_file_for_arg (GApplicationCommandLine *cmdline,  | 
837  |  |                                                 const gchar             *arg)  | 
838  | 0  | { | 
839  | 0  |   g_return_val_if_fail (arg != NULL, NULL);  | 
840  |  |  | 
841  | 0  |   if (cmdline->priv->cwd)  | 
842  | 0  |     return g_file_new_for_commandline_arg_and_cwd (arg, cmdline->priv->cwd);  | 
843  |  |  | 
844  | 0  |   g_warning ("Requested creation of GFile for commandline invocation that did not send cwd. " | 
845  | 0  |              "Using cwd of local process to resolve relative path names.");  | 
846  |  | 
  | 
847  | 0  |   return g_file_new_for_commandline_arg (arg);  | 
848  | 0  | }  |