[yocto] [matchbox-terminal] main.c: Add support to run user command inside the terminal

Devarsh Thakkar devarsh.thakkar at xilinx.com
Wed Mar 14 07:47:39 PDT 2018


Add support for commandline options (--command or -e)
using which one can provide commands to run inside the terminal
as shown in below command :

 $matchbox-terminal -e <command>
 $matchbox-terminal --command <command>

Add standard command line options for gtk programs.

NOTE : The command line options -e and --command
are chosen to mimic gnome-terminal and xterm which
provide same options to run commands inside the terminals.

Signed-off-by: Devarsh Thakkar <devarsht at xilinx.com>
---
 main.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)

diff --git a/main.c b/main.c
index 0d4597c..c0672df 100644
--- a/main.c
+++ b/main.c
@@ -42,9 +42,32 @@ main (int argc, char **argv)
 {
   GtkWidget *window, *terminal, *scrolled_win;
   GError *err = NULL;
-  char *cmd;
+  char *cmd = NULL;
   char **cmd_argv = NULL;
   int cmd_argc;
+  GOptionContext *context = NULL;
+
+  const GOptionEntry options[] = {
+    {
+          "command", 'e', 0,
+          G_OPTION_ARG_STRING, &cmd,
+        "Execute a command in the terminal", NULL},
+    {NULL}
+  };
+
+  context =
+      g_option_context_new
+      ("Open matchbox terminal and parser the command line args");
+  g_option_context_add_main_entries (context, options, NULL);
+  g_option_context_add_group (context, gtk_get_option_group (TRUE));
+  g_option_context_parse (context, &argc, &argv, &err);
+  g_option_context_free (context);
+
+  if (err != NULL) {
+    g_printerr ("Failed to parse command line arguments: %s\n", err->message);
+    g_error_free (err);
+    return -1;
+  }

   gtk_init (&argc, &argv);

@@ -67,11 +90,13 @@ main (int argc, char **argv)
      NULL);
   gtk_container_add (GTK_CONTAINER (scrolled_win), terminal);

-  cmd = vte_get_user_shell ();
-  if (!cmd)
-    cmd = g_strdup (g_getenv ("SHELL"));
-  if (!cmd)
-    cmd = g_strdup ("/bin/sh");
+  if (cmd == NULL) {
+    cmd = vte_get_user_shell ();
+    if (!cmd)
+      cmd = g_strdup (g_getenv ("SHELL"));
+    if (!cmd)
+      cmd = g_strdup ("/bin/sh");
+  }

   if (!g_shell_parse_argv(cmd, &cmd_argc, &cmd_argv, &err)) {
     g_printerr ("Failed to parse shell command line '%s'\n", cmd);
--
2.7.4

This email and any attachments are intended for the sole use of the named recipient(s) and contain(s) confidential information that may be proprietary, privileged or copyrighted under applicable law. If you are not the intended recipient, do not read, copy, or forward this email message or any attachments. Delete this email message and any attachments immediately.



More information about the yocto mailing list