Page 1 of 1

[Workaround] Error handling with Shell() and soffice crash

PostPosted: Wed Jun 27, 2012 2:32 pm
by EdH
I'm using LibreOffice 3.5.4 on Arch Linux x86-64 and have been doing some BASIC programming to run some system commands. As part of this I want to be able to catch errors in case a program (in this case bash) is not installed.

In Pitonyak's book it lists Shell as returning a runtime error if the program being run is not found. Unfortunately this does not seem to be working. I can successfully catch other errors but if the program Shell() is trying to run is not found it either locks soffice at 100% of CPU or causes it to crash completely!

Here's an example:
Code: Select all   Expand viewCollapse view
Sub BrokenShell

On Error Goto ErrorHandler

Dim iNumber As Integer
Dim iCount As Integer
Dim sLine As String
Dim aFile As String

' An example of an error that gets caught
aFile = "/some/broken/url"
iNumber = Freefile
Open aFile For Output As #iNumber
Print #iNumber, "This is a line of text"
Close #iNumber
iNumber = Freefile
Open aFile For Input As iNumber

For iCount = 1 to 5
   Line Input #iNumber, sLine
Next iCount

Close #iNumber

Proceed:
msgbox("But can I catch this error?")

'Shell( "bash -c " + cQuote + "bash --version", "",6,true) 'This is a working shell example

' A few examples of broken shell commands that cause crashes
Shell("brokenname", "", 6,true) 'Crash
'Shell("brokenname", "", 6,false) 'Crash
'Shell("brokenname", "", 6) 'Crash
'Shell("brokenname") 'Locks up the program using 100% CPU time, nothing happens

msgbox("No error")

Exit Sub

ErrorHandler:
msgbox("That was an error and I handled it!")
Reset
Resume Proceed

End Sub


Run it, first it successfully catches the example error and then it crashes when given the bad shell command. I've listed a few examples of ones that don't work and an example of one that does work. I don't know if this is just my setup or if it effects others or if there is any proper way of getting this to work.

Interested to know if anyone else has any ideas. I'd be tempted to file a bug on this if it can't be worked around.

Re: Error handling with Shell() calls and soffice crash

PostPosted: Wed Jun 27, 2012 3:33 pm
by RoryOF
Perhaps you should do things differently. If you test for the existence of the file/url before opening it, it always returns true or false. If false you errorhandle, if true, you open it. Admittedly, you then have to do a test to see if the open was good - it might not be because of (say) insufficient rights for that file.

Re: Error handling with Shell() calls and soffice crash

PostPosted: Wed Jun 27, 2012 5:20 pm
by EdH
RoryOF wrote:If you test for the existence of the file/url before opening it, it always returns true or false.

That will work very well for an instance of a file, true. Where it fails for this situation is we are looking for a program that's in the systems path. In this case I'm hoping to use it with bash. Now I could look for the existence of /bin/sh but not every system has bash as it's command line or keeps it in the same place. I'm looking for a way of handling such a situation rather than having OOo crash.

Can OOo get access to the systems path environment variable in anyway? Then we could find a file within it.

Re: Error handling with Shell() calls and soffice crash

PostPosted: Wed Jun 27, 2012 5:53 pm
by Charlie Young
EdH wrote:
RoryOF wrote:If you test for the existence of the file/url before opening it, it always returns true or false.

That will work very well for an instance of a file, true. Where it fails for this situation is we are looking for a program that's in the systems path. In this case I'm hoping to use it with bash. Now I could look for the existence of /bin/sh but not every system has bash as it's command line or keeps it in the same place. I'm looking for a way of handling such a situation rather than having OOo crash.

Can OOo get access to the systems path environment variable in anyway? Then we could find a file within it.


From Basic

Code: Select all   Expand viewCollapse view
Environ("Path")

Re: Error handling with Shell() calls and soffice crash

PostPosted: Wed Jun 27, 2012 7:34 pm
by EdH
Thanks Charlie for that hint. So here's some code that reads the system PATH environment variable on any OS, converts the Windows ; delimited format into the : that every other system uses, puts them in an array and then one by one looks through and tells you where bash (for this example) is installed:

Code: Select all   Expand viewCollapse view
Sub lookinpath

' Set some stuff up
dim path as string
dim aPaths
dim BashPath as string

bash=0
BashPath=""

' Load the system 'PATH' environment variable
path=Environ("PATH")

'In case system is running Windows where paths are delimeted by semicolons
path=join(split(path, ";"), ":")

' Split the string into an array or paths about the colon delimeters
aPaths =Split(path,":")

' For each element of the path array look to see if the bash file exists in it
For i = Lbound(aPaths) to Ubound(aPaths)
   If FileExists(aPaths(i)&"/bash") then
      bash=1
      BashPath=aPaths(i)
   End If
Next

' Report to user
If bash=1 then
msgbox("Bash found in " & BashPath)
else
msgbox("Bash not found")
end if
end sub


Not bad for a little code snippet?

Think this can then be used to accomplish what I need but doesn't really leave this [solved] so I will mark it as [workaround].