On 4/11/19 5:29 PM, Rob Landley wrote:
> On 4/11/19 11:34 AM, enh wrote:
>> On Fri, Apr 5, 2019 at 8:44 PM Rob Landley <[email protected]> wrote:
>>>
>>> I just added --restrict to require all the tarball's contents to extract 
>>> under a
>>> single directory, and the obvious way to use it is the same as ls --color, 
>>> via
>>> alias.
>>>
>>>   $ tar cz linux-4.20 l*.txt > ll.tgz
>>>   $ tar xvf ../ll.tgz --restrict
>>>   ...
>>>   linux-4.20/net/x25/x25_link.c
>>>   linux-4.20/net/x25/x25_proc.c
>>>   linux-4.20/net/x25/sysctl_net_x25.c
>>>   l11.txt
>>>   tar: 'l11.txt' not under 
>>> '/home/landley/toybox/toy3/tartest/sub/linux-4.20'
>>>   l1.txt
>>>   tar: 'l1.txt' not under '/home/landley/toybox/toy3/tartest/sub/linux-4.20'
>>>   l2.txt
>>>   tar: 'l2.txt' not under '/home/landley/toybox/toy3/tartest/sub/linux-4.20'
>>>   $
>>>
>>> Except... tar xvzf not needing dash is because the first argument is treated
>>> specially, and extra arguments from alias come first. Do you suppose that 
>>> should
>>> be the first _non-longopt_ argument, maybe?
> 
> I guess I should assume that this is the case then?

I sat down and implemented it (attached if you're curious)  and then started
worrying about:

  $ tar --create vf
  tar: Refusing to write archive contents to terminal (missing -f option?)
  tar: Error is not recoverable: exiting now

And went "no, somebody's going to do that" so the rule can't be first non-short
opt it has to be first _opt_.

So I dug into the bash man page and although alias doesn't have a way to say
"put the arguments here", shell _functions" with "$@" do. So you can still go:

tar()
{
  command tar "$@" --restrict
}

Which means I don't need to modify toybox to make the new option usefully
applicable as an environment default. (Dunno if mksh can do that, but it's on
the toysh todo list.)

Rob

P.S. the attached patch wound up doing:

  if (0) {
label:
    stuff();
  }

And went "is that going to break a compiler?" (undead code elimination, that was
one of my test cases for my old tinycc for by the way) and I ran "make tests"
under both compilers and found out a lot of the utf8 tests are failing under
llvm/bionic that aren't failing under glibc and I should probably update my ndk
before trying to track them down (it's from September..). Pretty standard
programming session for me, really. I'm trying to close things down to cut a
release, so naturally I'm generating new todo items...
diff --git a/lib/args.c b/lib/args.c
index b0bede1..5259518 100644
--- a/lib/args.c
+++ b/lib/args.c
@@ -74,8 +74,9 @@
 //     ^ stop at first nonoption argument
 //     <0 die if less than # leftover arguments (default 0)
 //     >9 die if > # leftover arguments (default MAX_INT)
-//     ? Allow unknown arguments (pass them through to command).
-//     & first arg has imaginary dash (ala tar/ps/ar) which sets FLAGS_NODASH
+//     ? Allow unknown arguments (pass them through to command in toys.optargs)
+//     & first non-longopt arg has imaginary dash (ala tar/ps/ar)
+//       sets FLAGS_NODASH so "ps -ax" and "ps ax" can behave differently
 //
 //   At the end: [groups] of previously seen options
 //     - Only one in group (switch off)    [-abc] means -ab=-b, -ba=-a, -abc=-c
@@ -395,8 +396,6 @@ void get_optflags(void)
 
   parse_optflaglist(&gof);
 
-  if (toys.argv[1] && toys.argv[1][0] == '-') gof.nodash_now = 0;
-
   // Iterate through command line arguments, skipping argv[0]
   for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) {
     gof.arg = toys.argv[gof.argc];
@@ -405,11 +404,11 @@ void get_optflags(void)
     // Parse this argument
     if (gof.stopearly>1) goto notflag;
 
-    if (gof.argc>1 || *gof.arg=='-') gof.nodash_now = 0;
-
     // Various things with dashes
     if (*gof.arg == '-') {
 
+      if (gof.arg[1] != '-') gof.nodash_now = 0;
+
       // Handle -
       if (!gof.arg[1]) goto notflag;
       gof.arg++;
@@ -470,12 +469,14 @@ void get_optflags(void)
         goto notflag;
       }
     }
-    continue;
 
-    // Not a flag, save value in toys.optargs[]
+    if (0) {
 notflag:
-    if (gof.stopearly) gof.stopearly++;
-    toys.optargs[toys.optc++] = toys.argv[gof.argc];
+      // Not a flag, save value in toys.optargs[]
+      if (gof.stopearly) gof.stopearly++;
+      toys.optargs[toys.optc++] = toys.argv[gof.argc];
+    }
+    gof.nodash_now = 0;
   }
 
   // Sanity check
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to