Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-04

Parsing commandline parameters with wildcards on Linux

9 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart

Hi all,

I ran into a problem with resolving commandline parameters, due to wildcard expansion by bash.

Consider the following code:

  for i := 1 to paramcount do writeln(i,': ',paramstr(i));

My program expects input from the commandline in the form

scopy source dest

when you now specify wildcards on the commandline, they will be expanded by bash, so the output will be like this:

./scopy *

1: backup
2: lib
3: scopy
4: scopy.ico
5: scopy.lpi
6: scopy.lpr
7: scopy.res
8: x

However from inside my program I cannot distinguish between the followin 2 scenarios:
1: x is the last item in the list expanded by bash or 2: the commandline actually was "scopy * x"

Scenario 1 is a syntax error, and it should not be treated as if it were scenario 2.

Things get even more confusing when i use a syntax like

scopy source dest excludefilter

./scopy * y *.bak

1: 123
2: 1233
3: 12333
4: 123.bak
5: 123.tmp
6: 456.bak
7: backup
8: lib
9: scopy
10: scopy.ico
11: scopy.lpi
12: scopy.lpr
13: scopy.res
14: x
15: y
16: 123.bak
17: 456.bak

Obviously this is a common scenario on Linux, so how to treat it?

I would prefer to expand the wildcards by myself, but it seems I have no access to what the user actually typed on the commandline?

Bart

Reply by email

Luca Olivetti
to Bart

Al 10/04/11 01:34, En/na Bart ha escrit:

Obviously this is a common scenario on Linux, so how to treat it?

You don't, accept the fact and don't try to oppose resistance ;-)

I would prefer to expand the wildcards by myself, but it seems I have no access to what the user actually typed on the commandline?

The only way is to single quote parameters:

./scopy '*' y '*.bak'

(see, e.g., the manual page for find).

Bye

Luca

Reply by email

Sven Barth
to Bart: I ran into a problem with resolving commandline parameters, due to wildcard expansion by…
Bart wrote 7 lines
My program expects input from the commandline in the form scopy source dest when you now specify wildcards on the commandline, they will be expanded by bash, so the output will be like this:

[snip]

Show quoted text 10 lines
However from inside my program I cannot distinguish between the followin 2 scenarios: 1: x is the last item in the list expanded by bash or 2: the commandline actually was "scopy * x" Scenario 1 is a syntax error, and it should not be treated as if it were scenario 2.

[snip]

Obviously this is a common scenario on Linux, so how to treat it?

Just live with it... Normal "cp" works like that as well.

E.g.:

echo "Hello World 1" > test1 echo "Hello World 2" > test2 cp test* cat test*

Hello World 1
Hello World 2

Regards,
Sven

Reply by email

Michael Van Canneyt
to Bart: I ran into a problem with resolving commandline parameters, due to wildcard expansion by…
Bart wrote 5 lines
Hi all, I ran into a problem with resolving commandline parameters, due to wildcard expansion by bash.

[snip]

Show quoted text 5 lines
Obviously this is a common scenario on Linux, so how to treat it? I would prefer to expand the wildcards by myself, but it seems I have no access to what the user actually typed on the commandline?

You cannot access that. If you want the wildcards, the user must escape them himself.

Michael.

Reply by email

Marco van de Voort
to Luca Olivetti: Al 10/04/11 01:34, En/na Bart ha escrit: You don't, accept the fact and don't try to…

In our previous episode, Luca Olivetti said:

Show quoted text 13 lines
> Obviously this is a common scenario on Linux, so how to treat it? You don't, accept the fact and don't try to oppose resistance ;-) > > I would prefer to expand the wildcards by myself, but it seems I have > no access to what the user actually typed on the commandline? The only way is to single quote parameters: ./scopy '*' y '*.bak' (see, e.g., the manual page for find).

Or change the syntax in a way that the last command is not a wildcard.

mv/cp work that way, they assume the last argument is a destination, and all others are generated by wildcards.

Reply by email

David W Noon
to Michael Van Canneyt: [snip] You cannot access that. If you want the wildcards, the user must escape them…

On Sun, 10 Apr 2011 11:50:56 +0200 (CEST), Michael Van Canneyt wrote about Re: [fpc-pascal] Parsing commandline parameters with wildcards on Linux:

On Sun, 10 Apr 2011, Bart wrote:

[snip]

Show quoted text 5 lines
> I would prefer to expand the wildcards by myself, but it seems I have > no access to what the user actually typed on the commandline? You cannot access that. If you want the wildcards, the user must escape them himself.

A program can open /proc/self/cmdline as a text file, and retrieve its command line to re-parse it for itself.

Regards, Dave [RLU #314465] ======================================================================= (David W Noon) =======================================================================

Reply by email

Sven Barth
to David W Noon
David W Noon wrote 14 lines
On Sun, 10 Apr 2011 11:50:56 +0200 (CEST), Michael Van Canneyt wrote about Re: [fpc-pascal] Parsing commandline parameters with wildcards on Linux: > On Sun, 10 Apr 2011, Bart wrote: [snip] >> I would prefer to expand the wildcards by myself, but it seems I have >> no access to what the user actually typed on the commandline? > > You cannot access that. If you want the wildcards, the user must > escape them himself. A program can open /proc/self/cmdline as a text file, and retrieve its command line to re-parse it for itself.

Quick test: this does also contain the already extended command line.

Regards,
Sven

Reply by email

Jonas Maebe
to Sven Barth
Sven Barth wrote 6 lines
On 10.04.2011 13:45, David W Noon wrote: > > A program can open /proc/self/cmdline as a text file, and retrieve its > command line to re-parse it for itself. Quick test: this does also contain the already extended command line.

And the reason is that wildcard expansion is performed by the shell. The shell then execute the program using the expanded command line. /proc/self/cmdline is a virtual file exposed by the kernel containing the command line parameters it received. The kernel obviously has no clue about what the shell did or did not do.

Jonas

Reply by email

Bart
to Marco van de Voort: In our previous episode, Luca Olivetti said: Or change the syntax in a way that the last…
Marco van de Voort wrote 5 lines
In our previous episode, Luca Olivetti said: Or change the syntax in a way that the last command is not a wildcard. mv/cp work that way, they assume the last argument is a destination, and all others are generated by wildcards.

I thought of that too.
This then seems the way to go.
Yet another hurdle for making the program cross-platform.

Thanks all for the input.

Bart

Reply by email