1. command_line() with *

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" 
} 
 

new topic     » topic index » view message » categorize

2. Re: command_line() with *

That's how del * works, it's part of the dos or unix command processor.

new topic     » goto parent     » topic index » view message » categorize

3. Re: command_line() with *

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.

new topic     » goto parent     » topic index » view message » categorize

4. Re: command_line() with *

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!

new topic     » goto parent     » topic index » view message » categorize

5. Re: command_line() with *

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

new topic     » goto parent     » topic index » view message » categorize

6. Re: command_line() with *

Bet you could pass 5 x 10 on the command line, and let Eu translate that.

new topic     » goto parent     » topic index » view message » categorize

7. Re: command_line() with *

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!

new topic     » goto parent     » topic index » view message » categorize

8. Re: command_line() with *

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.

new topic     » goto parent     » topic index » view message » categorize

9. Re: command_line() with *

georgeorr said...

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 ...

  • *
  • ?
  • $
  • \
  • '
  • "
  • `
new topic     » goto parent     » topic index » view message » categorize

10. Re: command_line() with *

Thanks!

DerekParnell said...
georgeorr said...

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 ...

  • *
  • ?
  • $
  • \
  • '
  • "
  • `
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu