coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

env: implement -S/--split-string

Open sylvestre opened this issue 1 year ago • 5 comments

The GNU doc says:

       -S, --split-string=S
              process and split S into separate arguments; used to pass multiple arguments on shebang lines

The code: https://github.com/uutils/coreutils/blob/main/src/uu/env/src/env.rs

Tested in GNU with tests/env/env-S.pl

sylvestre avatar Dec 24 '23 10:12 sylvestre

I can reproduce and run the gnu test. I'll start with working on a fix now.

cre4ture avatar Dec 28 '23 14:12 cre4ture

I ran into a challenge and I could make use of some ideas. I need to split the string provided the same way as a shell would do it. This is trivial with strings like this part1 part2 part3, but it gets more tricky with having quotes and escaped quotes: part1 "part2 with spaces" "part3 with a \"name\" that is escaped"

Is there something like this already available in uutils or a external crate?

cre4ture avatar Dec 28 '23 15:12 cre4ture

I'll try to use crate quoted-string = "0.6.1"

cre4ture avatar Dec 28 '23 16:12 cre4ture

I tried multiple different crates that claim parsing shell like syntax, but none of them supports all the corner cases from the gnu test suite. This is a really challenging task. I briefly checked the gnu implementation. They are really implementing their own custom parser for this. I would really like to avoid this. But maybe we don't get around this if we want the test suite pass 100%. What do you think? Is it worth to implement some own parser? Or should we start to improve an existing, but external crate?

cre4ture avatar Dec 29 '23 16:12 cre4ture

The format seems very specific based on the docs here: https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html#g_t_002dS_002f_002d_002dsplit_002dstring-syntax

I think it should unfortunately be a custom parser, because other crates probably aren't interested in this functionality and we need 100% compatibility. Maybe we could use nom for this. I'd suggest to implement the simple cases first. So start with having just quotes, for example, and add other functionality in later PRs.

Also, I have to ask you not to look at the GNU source code for licensing reasons. We need to implement this based on the test suite and the documentation alone.

tertsdiepraam avatar Jan 08 '24 11:01 tertsdiepraam

I think this was implemented by #5801.

Fun fact: If you write in a PR on a separate line "Fixes #5710", then GitHub automatically closes the related issue. That really comes in handy :) Example: https://github.com/uutils/coreutils/pull/6020

BenWiederhake avatar Mar 31 '24 13:03 BenWiederhake