PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/gio/gapplicationcommandline.c

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