[4.20] Bug or Problem code ?

Linux specific forum
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

Any another ideas for getting the return of the command "which" ?
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

How do you run Purebasic : By a batch or by a GUI Shortcut ?
“Fear is a reaction. Courage is a decision.” - WC
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

Not very nice (not for unicode and no error-checking), but for the
meantime, it could be useful :)

Code: Select all

program.s = "pbcompiler"


p = popen_("/usr/bin/which " + program, "r")

buffer.s = Space(256)
out.s = ""
While Not feof_(p)
  n = fread_(@buffer, 1, 256, p)
  PokeC(@buffer + n * SizeOf(character), 0)
  out + buffer
Wend
pclose_(p)

Debug Len(out)
Debug out
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

flaith wrote:How do you run Purebasic : By a batch or by a GUI Shortcut ?
GUI Shortcut !
remi_meier wrote:Not very nice (not for unicode and no error-checking), but for the
meantime, it could be useful :)

Code: Select all

program.s = "pbcompiler"

p = popen_("/usr/bin/which " + program, "r")

buffer.s = Space(256)
out.s = ""
While Not feof_(p)
  n = fread_(@buffer, 1, 256, p)
  PokeC(@buffer + n * SizeOf(Character), 0)
  out + buffer
Wend
pclose_(p)

Debug Len(out)
Debug out

I have a simple return (0 + empty string) but if i make /usr/bin/which pbcompiler, i have the goof path of pbcompiler.

Not Unicode !
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

:?
Could you try

Code: Select all

which which
and

Code: Select all

update-alternatives --list which
?
Perhaps there is a different which interfering?
Is there a shell that implements "which" on its own?

Or does it work with different programs? Like "ls" or "man"?
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

For the "which which" :

Code: Select all

franklin@novatux-laptop:~$ which which
/usr/bin/which
And the "update-alternatives --list which" :

Code: Select all

franklin@novatux-laptop:~$ update-alternatives --list which
Pas d'alternatives pour which.
And any others which :

Code: Select all

franklin@novatux-laptop:~$ which ls
/bin/ls
franklin@novatux-laptop:~$ which man
/usr/bin/man
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Progi1984 wrote:I tried this code :

Code: Select all

  Compilateur = RunProgram("which", "pbcompiler ", "", #PB_Program_Open|#PB_Program_Read)
  Sortie$ = ""
  If Compilateur 
    While ProgramRunning(Compilateur)
      Sortie$ + ReadProgramString(Compilateur) + Chr(13)
    Wend
    Sortie$ + Chr(13) + Chr(13)
    Sortie$ + "Code de retour : " + Str(ProgramExitCode(Compilateur))
  EndIf
  MessageRequester("Sortie", Sortie$)
And Sortie$ returns ""
This code works for me. (PB 4.30 beta4, should work with earlier versions too though).

I think your problem is that the PATH variable is different for the PB program and your console tests. (maybe you add the PB location to PATH in .bashrc, in which case the GUI shortcut does not get this).

What exitcode do you get when running this ? (0 means the target was found, nonzero means that which could not find a file with this name in PATH)
If which cannot find the file, then the error line is printed on stderr, which means that you need #PB_Program_Error and ReadProgramError() to read that. It won't show up in the normal output. The simplest is just to check ProgramExitCode() and if it is not 0 then the file was simply not found.

Try this from PB and the shell and compare the result:

Code: Select all

Debug GetEnvironmentVariable("PATH")

Code: Select all

echo $PATH
Btw, if you want to use system_(), you can use shell redirection to get the result. (As system_() calls a real shell interpreter unlike RunProgram())
Something like this:

Code: Select all

If system_("which bash >/tmp/which_test.txt 2>/dev/null") = 0
  If ReadFile(0, "/tmp/which_test.txt")
    Debug ReadString(0)
    CloseFile(0)
  Else
    Debug "-- cannot read output file --"
  EndIf
Else
  Debug "-- calling which failed or target file not found --"
EndIf
DeleteFile("/tmp/which_test.txt")
quidquid Latine dictum sit altum videtur
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

Code: Select all

Compilateur = RunProgram("which", "pbcompiler ", "", #PB_Program_Open|#PB_Program_Read)
  Sortie$ = ""
  If Compilateur
    While ProgramRunning(Compilateur)
      Sortie$ + ReadProgramString(Compilateur) + Chr(13)
    Wend
    Sortie$ + Chr(13) + Chr(13)
    Sortie$ + "Code de retour : " + Str(ProgramExitCode(Compilateur))
  EndIf
  MessageRequester("Sortie", Sortie$)
The return value is 1.

Code: Select all

Debug GetEnvironmentVariable("PATH")

Code: Select all

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Code: Select all

echo $PATH

Code: Select all

franklin@novatux-laptop:~$ echo $PATH
/media/DISK/Programs/purebasic/compilers:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
At the end of the file .bashrc :

Code: Select all

#purebasic
export PUREBASIC_HOME=/media/DISK/Programs/purebasic
export PATH=$PUREBASIC_HOME/compilers:$PATH

Code: Select all

If system_("which bash >/tmp/which_test.txt 2>/dev/null") = 0
  If ReadFile(0, "/tmp/which_test.txt")
    Debug ReadString(0)
    CloseFile(0)
  Else
    Debug "-- cannot read output file --"
  EndIf
Else
  Debug "-- calling which failed or target file not found --"
EndIf
DeleteFile("/tmp/which_test.txt")
Returns : /bin/bash

Code: Select all

If system_("which pbcompiler >/tmp/which_test.txt 2>/dev/null") = 0
  If ReadFile(0, "/tmp/which_test.txt")
    Debug ReadString(0)
    CloseFile(0)
  Else
    Debug "-- cannot read output file --"
  EndIf
Else
  Debug "-- calling which failed or target file not found --"
EndIf
DeleteFile("/tmp/which_test.txt")
Returns : -- calling which failed or target file not found --

Which error do I have ?
And how can I solve it ?
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

> Which error do I have ?

Well, your code has no error. The returnvalue of 1 tells you that which simply could not find the PureBasic compiler.

The .bashrc setting only affects the console, not the GUI-started PureBasic IDE. Try to compile the code to an executable and run it from the console. This should show the result you expect.

> And how can I solve it ?

You just have to be prepared for the fact that which may not find the purebasic path. (either because your program is run from the GUI, or because the user never added purebasic to the path anywhere)

If the exitcode is not 0, just tell the user that PureBasic was not found and offer a way to enter the path manually. Thats all you can do.
quidquid Latine dictum sit altum videtur
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

freak wrote:> Which error do I have ?

Well, your code has no error. The returnvalue of 1 tells you that which simply could not find the PureBasic compiler.

The .bashrc setting only affects the console, not the GUI-started PureBasic IDE. Try to compile the code to an executable and run it from the console. This should show the result you expect.

> And how can I solve it ?

You just have to be prepared for the fact that which may not find the purebasic path. (either because your program is run from the GUI, or because the user never added purebasic to the path anywhere)

If the exitcode is not 0, just tell the user that PureBasic was not found and offer a way to enter the path manually. Thats all you can do.
Affirmative, thanks for your answer et explanations :)
Post Reply