1. command_line() with *
- Posted by georgeorr Apr 16, 2009
- 926 views
I am working on a simple command line calculator and wanted to pull the command line into the program for immediate execution. I used the command_line() function for this and it works fine except that when it encounters an * on the command line it seems to expand it into a list of the files and directories in the current folder. I have not found anything in the documentation that would have made me expect this sort of behavior. Is this a feature or a bug? Has anyone else encountered this? Details are below
I can get around this by quoting the command line, but this is a bit awkward for a command line calculator!
My code (in a file called "cline" is
-- checking Command Line include pretty.e sequence cline cline = command_line() pretty_print(1,cline,{2}) puts(1,"\n\n")
When I execute with files "Another file", "cline", and "emake" in the current directory I get
eui cline test * test
with I get
{ "/home/george/EU40_2/bin/eui", "cline", "test", "Another file", "cline", "emake", "test" }
2. Re: command_line() with *
- Posted by irv Apr 16, 2009
- 930 views
That's how del * works, it's part of the dos or unix command processor.
3. Re: command_line() with *
- Posted by LarryMiller Apr 16, 2009
- 873 views
It appears that you are running under Linux. If I remember correctly the Linux shell itself expands the "*" character before passing the command line to the Euphoria program. I dpn't know if there is a solution.
4. Re: command_line() with *
- Posted by georgeorr Apr 16, 2009
- 883 views
- Last edited Apr 17, 2009
I was sure this was what the operating system command line processor does. I just was not sure (based on the command-line() documentation) that this was what the function was suppose to do. If it is, I would recommend making this clear in the eventual documentation.
Thanks for the responses!
5. Re: command_line() with *
- Posted by jeremy (admin) Apr 16, 2009
- 861 views
- Last edited Apr 17, 2009
Others are correct. The unix shell expands * itself, it has nothing to do with Euphoria. A simple C program has the same effect:
#include <stdio.h> int main(int argc, char **argv) { int i; for (i=0; i<argc; i++) printf("%d = %s\n", i, argv[i]); return 0; }
To get around this, the only way is to escape the * on the command line, which isn't very user friendly:
$ calc 10 \* 20
Now, one other alternative is you may want to split things inside of Euphoria itself. For instance, take an expression on the command line:
-- Run with: -- $ calc.ex "10 * 20" -- include std/sequence.e sequence args = command_line() for i = 3 to length(args) do sequence expr_args = split(args[i]) -- expr_args = { "10", "*", "20" } end for
Jeremy
6. Re: command_line() with *
- Posted by irv Apr 16, 2009
- 875 views
- Last edited Apr 17, 2009
Bet you could pass 5 x 10 on the command line, and let Eu translate that.
7. Re: command_line() with *
- Posted by georgeorr Apr 16, 2009
- 870 views
- Last edited Apr 17, 2009
I have had no problem at all with using command_line() with any arguments except *. I have built a fairly complete reverse polish calculator, and just put in an opening routine to pull arguments directly from the command line. Everything had tested OK, but I guess I didn't use * until later in testing. Then is when I started seeing "weird" arguments that turned out to be the file/directory names in the current directory, and then asked to see if anyone else knew a way around this.
Everything works fine if I quote the arguments or escape the *. The program parses the input string and does its thing with no problems. But as Jeremy points out this isn't very friendly and makes inputs at the command line different from within the program. I obviously just didn't realize that command_line() was interacting with the operating system as much as it does. I guess if I had looked at the code I would have known...
The little calculator I am playing with is really just a way to try out the stack.e and map.e routines in EU4.0. They really make this type of calculator almost trivial. I have been very impressed with the capabilities I am finding throughout EU4.0. But once I have something simple that works well, I am always tempted to add just a few bells and whistles (like pulling in the initial string to be executed from the command line) and start getting myself in trouble! I guess it's fun, though, or I wouldn't be spending my time away from work working so hard...
Thanks again for the responses. This was just a behavior I wasn't expecting, and the experience of others is invaluable in making sure I am not just being stupid again!
8. Re: command_line() with *
- Posted by georgeorr Apr 16, 2009
- 868 views
- Last edited Apr 17, 2009
True enough. I had used * for the multiplication operator just because that is what is used in other applications such as FORTH. It is easy enough to go another way and use another symbol for this operator. Are there things other than the * that I should avoid to prevent this type of command_line() problem? * is the only one I have found, but ...
Thanks.
9. Re: command_line() with *
- Posted by DerekParnell (admin) Apr 17, 2009
- 883 views
Are there things other than the * that I should avoid to prevent this type of command_line() problem?
This is only a problem on Unix-type systems. On thise systems, the shell program interprets sme symbols in a special way before your program even gets to see the command line. Watch out for ...
- *
- ?
- $
- \
- '
- "
- `
10. Re: command_line() with *
- Posted by georgeorr Apr 17, 2009
- 887 views
Thanks!
Are there things other than the * that I should avoid to prevent this type of command_line() problem?
This is only a problem on Unix-type systems. On thise systems, the shell program interprets sme symbols in a special way before your program even gets to see the command line. Watch out for ...
- *
- ?
- $
- \
- '
- "
- `