|
Post by barryk on Nov 14, 2010 10:54:57 GMT 1
I am new to BaCon. These are some thoughts on extra features that would be nice...
BREAK [number] Just like in Bash, an optional integer number to break up any level when in nested loops.
CONTINUE [number] Some BASICs have this, Bash has it, well, lots of languages have it. Goes to the start of the current loop. Again, I suggest an optional integer argument.
Note, I just looked at freeBASIC, they have gone their own way on the above, for example, this is the equivalent of 'BREAK 2':
For i = 1 To 10 For j = 1 To 10 Exit For, For Next j Print "I will never be shown" Next i
...not as simple.
COMMAND To get the commandline parameters passed to the application.
EXEC on steroids It would be very useful if you could extend one of these with extra keywords to support stdin and stdout and the return-value. For example, here is some C code that executes a shell to find out how many instances of application 'pupmem' are running:
fpipe=(FILE*)popen("pidof pupmem | tr ' ' '\n' | wc -l","r"); fgets(pidcnt,sizeof pidcnt,fpipe); pclose(fpipe);
...and similarly a pipe can be coded for stdin.
stdout is what I guess is what EXEC already does, for example:
files$=EXEC$("ls -l")
But what about stdin? Return value?
Um, would this be do-able, or even is it a sensible syntax?:
files$=EXEC$("ls -l") STDIN=param1$ RETVAL=num1
Regards, Barry Kauler
|
|
|
Post by barryk on Nov 14, 2010 11:30:12 GMT 1
Oops, I should have read the docs a bit more, BaCon already has ARGUMENT$, so ignore my post above about COMMAND.
|
|
|
Post by Pjot on Nov 14, 2010 12:27:25 GMT 1
Hi Barry,
The BREAK and CONTINUE requests will be quite hard to implement but I'll look at it. Currently there is no CONTINUE at all so that one can be added anyway.
For EXEC, what do you mean with 'returnvalue'? Probably the returnvalue of the actual command, right? (In BASH shell scripting this is denoted with $? - I guess you refer to this?)
If you mean this returnvalue, what would be the returnvalue of a string like: EXEC("ls -l; cd /tmp; rm *.*") ? Would we see the returnvalue of the 'rm' as this one is executed last?
EXEC it is a function so the syntax will be something like this: EXEC("command", [stdin [, returnvalue]]). So the last two will be optional.
Regards Peter
|
|
|
Post by barryk on Nov 15, 2010 0:32:34 GMT 1
Peter, Yes, this would be great:
EXEC("command", [stdin [, returnvalue]])
Yes, by return value I mean the integer numeric return status when an application exits, which in Bash you get with $?.
But you are mostly likely to need returnvalue, so this would perhaps be better:
EXEC("command" [,returnvalue [,stdin]])
Adding both of these to EXEC would make it so powerful.
The stdin gives us a mechanism to feed some value into the shell from the BaCon application.
Another thing that would be nice, but this is probably asking too much, is if the "command" string could be made to recognise BaCon variables. For example:
EXEC("demoapp ${variable1}")
where variable1 is an actual variable in BaCon.
...maybe that is too hard though, as it might require run-time evaluation of the command string. Well, I suppose BaCon could be made to recognise the special syntax "${variable1}" and perform a runtime substitution.
The reason that I request all of these enhancements to EXEC is that I am so familiar with Bash/Ash and when coding in a high-level language I can often see how to solve a particular task very quickly in one line of Bash, whereas it is not so easy for me in that high-level language.
So, all the enhancements requested above would make the insertion of shell code into the BaCon program very powerful.
Regards, Barry Kauler
|
|
|
Post by barryk on Nov 15, 2010 0:38:00 GMT 1
If you mean this returnvalue, what would be the returnvalue of a string like: EXEC("ls -l; cd /tmp; rm *.*") ? Would we see the returnvalue of the 'rm' as this one is executed last?
Yes, exactly as if you did it in Bash:
ls -l; cd /tmp; rm *.* echo $?
...whatever return-value the command string finishes with.
|
|
|
Post by Pjot on Nov 15, 2010 1:38:58 GMT 1
Thanks Barry, understood it all now. Currently I have a prototype running for the BREAK [x] request, but I want to do more testing before uploading it to the BETA directory. If this works then CONTINUE [x] will be easy. (BTW I only know these commands with argument from Shell scripting, none of the BASIC's I have worked with in the past supports them, as far as I remember. But they are handy for sure!) The EXEC$ argument order can of course change to first STDIN and then returnvalue. I'll post here when I have your requests ready. Regards Peter
|
|
|
Post by vovchik on Nov 15, 2010 10:27:38 GMT 1
Dear Barry, To use a BaCon var with the the EXEC$ command, you can always do it the "long" way: mydir$ = "/tmp" mycmd$ = CONCAT$("ls -l", $mydir) myresult$ = EXEC$(mycmd$) Not as convenient as you are suggesting, but I use it all the time and it works nicely. The BREAK/CONTINUE and stdin/returncode business for EXEC$ do appear to be useful. Peter has knack for being able to implement the seemingly impossible! With kind regards, vovchik
|
|
|
Post by barryk on Nov 16, 2010 8:47:22 GMT 1
(BTW I only know these commands with argument from Shell scripting, none of the BASIC's I have worked with in the past supports them, as far as I remember. But they are handy for sure!)...I mentioned freeBASIC, but they do it with their own unusual syntax. There is one other I know of that does it exactly as in Bash: PureBasic: www.purebasic.com/documentation/reference/break_continue.html...but notice, they only do it for BREAK, not for CONTINUE.
|
|
|
Post by barryk on Nov 16, 2010 9:21:45 GMT 1
|
|
|
Post by Pjot on Nov 16, 2010 11:56:08 GMT 1
Thanks Barry, now I see why I never encountered these commands before, as indeed I never used PureBasic. Nor Gambas or RealBasic Anyway, I have BREAK [x] and CONTINUE [x] ready now. If you have time, can you check the latest BETA and let me know if this is what you have in mind? I'll look into EXEC now. Regards Peter
|
|
|
Post by Pjot on Nov 18, 2010 7:58:47 GMT 1
So EXEC$ is on its way - the STDIN part is ready, it took a little bit more time than I expected. The returnvalue of a child process is still not there because one simply cannot pass a variablename as argument to a function, as C does not do runtime conversion of memory buffers. Therefore, I was thinking of assigning a reserved variable for the returnvalue of the last command which was executed (both by SYSTEM or EXEC$), similarly to BASH. For example, simply the variable RV, or something like that. Anyway, now EXEC$ has an optional second argument which can be the feed to STDIN of the command executed in the shell. So for now the syntax is: a$ = EXEC$("command" [, "stdin"])I have tested this with the 'bc' calculator language, as follows: result$ = EXEC$("bc", "12+4\nquit") PRINT result$
' Or as follows:
result$ = EXEC$("bc", CONCAT$("12+4", NL$, "quit")) PRINT result$
This should calculate 12 added with 4 and will show 16. As usual the latest can be found in the BETA directory. Regards Peter
|
|