--- mach/scripts/mach.in~args	2003-12-03 18:33:51.000000000 +0100
+++ mach/scripts/mach.in	2003-12-03 18:34:22.000000000 +0100
@@ -107,7 +107,7 @@
 
 
 # rpm -q format, users may have customized %_query_all_fmt in ~/.rpmmacros...
-qfmt = '"%{name}-%{version}-%{release}\n"'
+qfmt = '%{name}-%{version}-%{release}\\n'
 
 def escapeShell(opt, level=1):
     while level>0:
@@ -424,6 +424,6 @@
             except OSError:
               raise self.Error, "Could not create %s" % new
    
-    def _splitarg (self, argstring, matcher):
+    def _splitarg (self, args, matcher):
         "only used by splitargspecs and splitargsrcrpms"
         result = []
@@ -430,6 +430,6 @@
         other = []
         match = re.compile (matcher, re.I)
-        for arg in argstring.split ():
+        for arg in args:
             if match.search (arg):
                 result.append (arg)
             else:
@@ -437,12 +437,12 @@
         return (other, result)
 
     # seek through the argument list for specfiles
-    def splitargspecs (self, argstring):
-        return self._splitarg (argstring, '\.spec$')
+    def splitargspecs (self, args):
+        return self._splitarg (args, '\.spec$')
 
     # seek through the argument list for srpms
-    def splitargsrpms (self, argstring):
-        return self._splitarg (argstring, '\.src\.rpm$')
+    def splitargsrpms (self, args):
+        return self._splitarg (args, '\.src\.rpm$')
 
     # run apt from outside the root on the root
     # FIXME: virtualize this
@@ -449,5 +449,5 @@
     # if message is specified, it will be printed, and newline-terminated
     # properly
     # if no message specified, then it's up to caller
-    def aptget (self, arg, message = None,
+    def aptget (self, args, message = None,
                 progress = False, interactive = False):
@@ -454,6 +454,7 @@
         "run apt-get (arg) from outside the root on the root"
         conf = os.path.join (self.statedir, 'apt.conf')
-        command = "%s -c %s %s" % (self.config['apt-get'], conf, arg)
+        args_esc = map(lambda x: escapeShell(x), args)
+        command  = "%s -c %s %s" % (self.config['apt-get'], conf, string.join(args_esc))
         if interactive:
             return os.system (command)
         if message: self.stdout (message)
@@ -461,13 +462,13 @@
         (status, output) = do_with_output (command, progress)
         if message and not progress: self.stdout ('\n')
         if status != 0:
-            raise self.Error, "Could not apt-get %s" % arg
+            raise self.Error, "Could not apt-get %s" % args
 
-    def intaptget (self, arg):
+    def intaptget (self, args):
         if not self.get_state ('base'):
-            self.setup ('base')
+            self.setup (('base',))
         self.mount ()
-        self.aptget (arg, interactive = True)
+        self.aptget (args, interactive = True)
         self.umount ()
 
-    def aptcache (self, arg, progress = False):
+    def aptcache (self, args, progress = False):
@@ -474,5 +475,6 @@
         "run apt-cache (arg) from outside the root on the root"
         conf = os.path.join (self.statedir, 'apt.conf')
-        command = "%s -c %s %s" % (self.config['apt-cache'], conf, arg)
+        args_esc = map(lambda x: escapeShell(x), args)
+        command  = "%s -c %s %s" % (self.config['apt-cache'], conf, string.join(args_esc))
         (status, output) = do_with_output (command, progress)
         if status != 0:
@@ -479,1 +481,1 @@
-            raise self.Error, "Could not apt-cache %s" % arg
+            raise self.Error, "Could not apt-cache %s" % args
@@ -480,11 +482,12 @@
         print output
 
-    def rpm (self, arg, progress = False):
+    def rpm (self, args, progress = False):
         "run rpm (arg) from outside the root on the root, returns output"
-        command = "%s --root %s %s" % (self.config['rpm'], self.rootdir, arg)
+        args_esc = map(lambda x: escapeShell(x), args)
+        command  = "%s --root %s %s" % (self.config['rpm'], self.rootdir, string.join(args_esc))
         (status, output) = do_with_output (command, progress)
         if status != 0:
-            raise self.Error, "Could not rpm %s" % arg
+            raise self.Error, "Could not rpm %s" % (args,)
         return output
 
     # main command executer:
@@ -538,13 +541,13 @@
             print "Not removing packages from root."
             return
         root = self.rootdir
-        cmd = 'rpm --root %s -qa --qf %s \
+        cmd = 'rpm --root %s -qa --qf "%s" \
                | diff - %s > /dev/null 2>&1' % (root, qfmt, listfile)
         debug ("running " + cmd)
         if os.system (cmd) == 0:
             # no differences
             return True
-        cmd = "rpm --root %s -qa --qf %s \
+        cmd = "rpm --root %s -qa --qf '%s' \
                | grep -v -f %s - \
                | tr '\n' ' '" % (root, qfmt, listfile)
         packages = commands.getoutput (cmd)
@@ -552,10 +555,10 @@
         if not packages:
            return True
         sys.stdout.write ("Removing packages ...")
-        self.rpm ("-ev %s" % packages, self.progress ())
+        self.rpm (("-ev",) + tuple(string.split(packages)), self.progress ())
         return True
     
 
     # implementation of actual externalized commands
     # actual externalized commands
 
@@ -562,5 +565,5 @@
-    def build (self, arg):
+    def build (self, args):
         "build from a set of spec files by packaging them up as src.rpm and "
         "passing them to rebuild"
 
-        if not arg:
+        if not args:
@@ -567,10 +570,10 @@
             raise self.Error, 'Please supply .spec files to build'
         
         # check if we can build here yet
         if not self.get_state ('build'):
-            self.setup ('build')
+            self.setup (('build',))
 
         self.lock ()
         
         srpms = [] # resulting set of srpms
         # separate options to rpmbuild from specfiles
@@ -577,4 +580,4 @@
-        (options, specs) = self.splitargspecs (arg)
+        (options, specs) = self.splitargspecs (args)
         debug ("options to rpmbuild: %s" % options)
         # check if the spec files exist and if we can parse the files necessary
         for specfile in specs:
@@ -656,7 +659,7 @@
         # ready to build them all
         print "Rebuilding generated .src.rpm's: \n- %s" % string.join (srpms, "\n- ")
         self.unlock ()
-        self.rebuild (string.join (options) + " " + string.join (srpms))
+        self.rebuild (options + srpms)
              
     def _bruteclean (self):
         "brutishly clean out the root by using mach-helper rm -rfv"
@@ -667,7 +670,7 @@
         if status != 0:
     	    raise self.Error, "Could not remove %s" % self.rootdir
     
-    def clean (self, args = ""):
+    def clean (self, args = []):
         "clean out the root"
         # does the root still exist ?
         if not os.path.exists (self.rootdir):
@@ -691,18 +694,19 @@
             self._bruteclean ()
 
     # chroot into root
-    def chroot (self, arg):
+    def chroot (self, args):
         self.mount ()
         # a minimal system might not have su, yikes
+        args_esc = string.join(map(lambda x: escapeShell(x), args))
         if not os.path.exists (os.path.join (self.rootdir, 'bin', 'su')):
-            if arg:
-                print "No su in root, can't execute %s" %arg
+            if args:
+                print "No su in root, can't execute %s" % args
                 return
             else:
-                cmd = "%s %s %s" % (self.config['chroot'], self.rootdir, arg)
+                cmd = "%s %s %s" % (self.config['chroot'], self.rootdir, args_esc)
                 print "Entering %s, type exit to leave." % self.rootdir
-        elif arg:
-             cmd = "%s %s su - root -c '%s'" % (self.config['chroot'], self.rootdir, arg)
+        elif args:
+             cmd = "%s %s su - root -c '%s'" % (self.config['chroot'], self.rootdir, args_esc)
         else:
              cmd = "%s %s su - root" % (self.config['chroot'], self.rootdir)
              print "Entering %s, type exit to leave." % self.rootdir
@@ -710,11 +714,11 @@
         os.system (cmd)
         self.umount ()
      
-    def setup (self, arg):
+    def setup (self, args):
         "Set up the root to a given target"
         target = ''
-        if arg:
-            target = arg
+        if args:
+            target = args[0]
         else:
             target = 'base' # default to build
         targets = ('prep', 'minimal', 'base', 'build')
@@ -735,12 +739,12 @@
                 break
         self.unlock ()
      
-    def rebuild (self, arg):
-        if not arg:
+    def rebuild (self, args):
+        if not args:
             raise self.Error, 'Please supply .src.rpm files to build'
 
-        (options, paths) = self.splitargsrpms (arg)
-        self.setup ('build')
+        (options, paths) = self.splitargsrpms (args)
+        self.setup (('build',))
         self.lock ()
         # FIXME: we don't need this anymore, do we ?
         #self.config_recreate ('/usr/bin/apt-sigchecker')
@@ -821,8 +825,8 @@
                 print "Building %s" % srpmname
                 debug ("BuildRequires: %s" % buildreqs)
                 try:
-        	    self.aptget ("update")
-                    self.aptget ('install -my %s' % string.join(buildreqs_esc),
+        	    self.aptget (("update",))
+                    self.aptget (('install', '-my') + tuple(buildreqs_esc),
                                  "Installing BuildRequires", self.progress ())
                     # FIXME: build-dep would be nice but only works on a pkg
                     # in the rpm-src tree apparently
@@ -914,7 +918,7 @@
             (srpm, rpms) = get_rpms_from_log (resultdir + '/rpm.log')
             # FIXME: install these RPMS based on a boolean ?
             # FIXME: error checks
-            output = self.rpm ('-qp --qf "%%{name}=%%{epoch}:%%{version}-%%{release} " %s' % string.join (['', ] + rpms, " " + self.rootdir))
+            output = self.rpm (("-qp", "--qf", "%%{name}=%%{epoch}:%%{version}-%%{release}", string.join (['', ] + rpms, " " + self.rootdir)))
             #(status, output) = do_chroot_with_output (config, '%s -qp --qf "%%{name}=%%{epoch}:%%{version}-%%{release} " %s' % (chroot_rpm, string.join (rpms, " ")))
             # FIXME: don't install built packages automatically, they can
             # cause conflicts and break sequence builds
@@ -933,7 +937,7 @@
                 # self.do_chroot ('rm ' + file)
 
             # update internal apt repo
-            self.aptget ("update")
+            self.aptget (("update",))
             print "Build of %s succeeded, results in\n%s" % (fullname, resultdir)
             resultdirs.append (resultdir)
 
@@ -1123,8 +1127,8 @@
         # create sources.list, run apt-get update
         srcs = get_sources_list (config)
         create_sources_list (config, srcs)
-        self.rpm("--initdb");
-        self.aptget ("update", "Updating apt sources",
+        self.rpm(("--initdb",));
+        self.aptget (("update",), "Updating apt sources",
                      self.progress ())
     
         self._call_setup_hook ("prep")
@@ -1190,12 +1194,12 @@
         create_sources_list (self.config, srcs) 
         for dir in ('RPMS', 'SRPMS'):
             self.do_chroot ('ln -sf %s /usr/src/rpm/%s.mach-local' % (dir, dir))
-        self.aptget ("update")
+        self.aptget (("update",))
         self._call_setup_hook ("build")
 
         self.stdout ("Making snapshot ...\n")
         # FIXME: use _with_output for this
-        output = self.rpm ("-qa --qf %s" % qfmt)
+        output = self.rpm (("-qa", "--qf", qfmt))
         self.set_state ('build', output)
         self.umount ()
     
@@ -1214,10 +1218,10 @@
             # do a two change install if necessary and gloss over errors
             # from first try
             try:
-                self.aptget ('install -y %s' % rpms,
+                self.aptget (('install', '-y') + tuple(string.split(rpms)),
                              "Installing package set '%s'" % set, True)
             except self.Error:
-                self.aptget ('install -y %s' % rpms,
+                self.aptget (('install', '-y') + tuple(string.split(rpms)),
                              "Retrying installing package set '%s'" % set, True)
 
         else:
@@ -1701,7 +1705,6 @@
         print help
         sys.exit (1)
     command = args[0]

-    args = string.join (args[1:])
     debug ("main: running %s" % command)
     
     root = Root (config)
@@ -1721,7 +1724,7 @@
             sys.exit (1)
    
         try:
-            output = Root.__dict__[command] (root, args)
+            output = Root.__dict__[command] (root, args[1:])
         except Root.Locked:
             sys.stderr.write ("Root is locked.  Use -f to override lock.\n")
         except Root.Error, message:
