[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#528783: bogl: support 'bterm [options] -- command [arguments]'



Package: bogl
Version: 0.1.18-3
Severity: wishlist
Tags: patch
User: ubuntu-devel@lists.ubuntu.com
Usertags: origin-ubuntu ubuntu-patch karmic

bterm's command-line syntax currently only supports invocations of the
following form:

  bterm [options] [command]

... where the options and the command name may be interleaved. This is
inconvenient if you want to run a command that takes arguments inside
bterm, especially if any of those arguments start with "-"; you have to
write a wrapper script, and if you want any of those arguments to be
variable then you need to pass them through using environment variables.
It would be much better if bterm supported a sensible adverbial style of
invocation.

The attached patch changes bterm to support an additional syntax of the
following form:

  bterm [options] -- command [arguments]

I believe that it is fully backward-compatible with the previous syntax.

Thanks,

-- 
Colin Watson                                       [cjwatson@ubuntu.com]
diff -Nru bogl-0.1.18/bterm.c bogl-0.1.18/bterm.c
--- bogl-0.1.18/bterm.c	2009-04-23 16:46:07.000000000 +0100
+++ bogl-0.1.18/bterm.c	2009-05-15 15:41:57.000000000 +0100
@@ -66,6 +66,26 @@
 static struct termios ttysave;
 static int quit = 0;
 
+/* Out of memory.  Give up. */
+static void out_of_memory (void)
+{
+  fprintf (stderr, "virtual memory exhausted\n");
+  abort ();
+}
+
+/* Allocate AMT bytes of memory and make sure it succeeded. */
+static void *xmalloc (size_t size)
+{
+  void *p;
+  
+  if (size == 0)
+    return 0;
+  p = malloc (size);
+  if (!p)
+    out_of_memory ();
+  return p;
+}
+
 /* This first tries the modern Unix98 way of getting a pty, followed by the
  * old-fashioned BSD way in case that fails. */
 int get_ptytty(int *xptyfd, int *xttyfd)
@@ -145,7 +165,7 @@
 	quit = 1;
 }
 
-void spawn_shell(int ptyfd, int ttyfd, const char *command)
+void spawn_shell(int ptyfd, int ttyfd, char * const *command_args)
 {
   fflush(stdout);
   child_pid = fork();
@@ -170,7 +190,7 @@
   setgid(getgid());
   setuid(getuid());
 
-  execl(command, command, NULL);
+  execvp(command_args[0], command_args);
   exit(127);
 }
 
@@ -224,11 +244,13 @@
   int ptyfd, ttyfd;
   struct bogl_font *font;
   char *locale = "", *command = NULL;
+  char **command_args;
   int i;
   char o = ' ';
   int pending = 0;
 
-  for (i = 1 ; i < argc ; ++i)
+  for (i = 1 ; i < argc ; ++i) {
+      int done = 0;
       if (argv[i][0] == '-')
           switch (argv[i][1])
           {
@@ -237,6 +259,10 @@
                   o = argv[i][1];
                   break;
 
+              case '-':
+                  done = 1;
+                  break;
+
               default:
                   printf ("unknown option: %c\n", argv[i][1]);
           }
@@ -258,6 +284,10 @@
                     break;
             }
 
+      if (done)
+          break;
+  }
+
   setlocale(LC_CTYPE, locale);
 
   if (font_name == NULL) {
@@ -291,7 +321,22 @@
     exit(1);
   }
 
-  spawn_shell(ptyfd, ttyfd, command == NULL ? "/bin/sh" : command);
+  if (command) {
+    command_args = xmalloc(2 * sizeof *command_args);
+    command_args[0] = command;
+    command_args[1] = NULL;
+  } else if (i < argc - 1) {
+    int j;
+    command_args = xmalloc((argc - i) * sizeof *command_args);
+    for (j = i + 1; j < argc; ++j)
+      command_args[j - (i + 1)] = argv[j];
+    command_args[argc - (i + 1)] = NULL;
+  } else {
+    command_args = xmalloc(2 * sizeof *command_args);
+    command_args[0] = "/bin/sh";
+    command_args[1] = NULL;
+  }
+  spawn_shell(ptyfd, ttyfd, command_args);
 
   signal(SIGHUP, reload_font);
   signal(SIGTERM, sigterm);

Reply to: