Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 12 Nov 2019, 00:38
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
GtkDialog - tips
Post new topic   Reply to topic View previous topic :: View next topic
Page 87 of 99 [1476 Posts]   Goto page: Previous 1, 2, 3, ..., 85, 86, 87, 88, 89, ..., 97, 98, 99 Next
Author Message
step

Joined: 04 May 2012
Posts: 1220

PostPosted: Fri 25 May 2018, 08:18    Post subject:  

Here's another example
Code:

#!/bin/dash
# or busybox sh/ash even bash

func_test() {
set -x
  yad --center --button=gtk-ok --buttons-layout=center "--text=
func_test arguments: $1, $2, $3
.  using exported variable: $VARIABLE
.  click OK to exit"
}

VARIABLE="hi there!"; export VARIABLE

MAIN_DIALOG='
<window>
   <vbox>
   <frame Test>
         <hbox>
            <button>
               <label>Click me</label>
               <action>. "'"$0"'" @test 1 2 3</action>
               <action function="exit">Exit by button</action>
            </button>
         </hbox>
      <hbox>
         <button ok></button>
         <button cancel></button>
      </hbox>
   </frame>
   </vbox>
</window>
'
export MAIN_DIALOG

case $1 in
   -d | --dump) echo "$MAIN_DIALOG" ;;
   @*) sub=func_${1#@}; shift; $sub "$@"; exit $? ;; # relay func'tions
   *) gtkdialog --program=MAIN_DIALOG ;;
esac
echo done

_________________
Fatdog64-802|+Packages|Kodi|Findnrun|+forum|gtkmenuplus
Back to top
View user's profile Send private message 
wiak

Joined: 11 Dec 2007
Posts: 1828
Location: not Bulgaria

PostPosted: Fri 25 May 2018, 09:02    Post subject:  

@step: Something wrong with your code line:

Code:
               <action>. "'"$0"'" @test 1 2 3</action>


EDIT: @step: Sorry, FALSE ALARM... your code is working on my system now. My computer must have had an one two electron too many and gone on the blink...! But okay again now. Whew, what a relief... It was really weird though. I didn't change the code since; I just rebooted...
EDIT2: @step: Em no... your code really isn't working for me




It is not calling up yad via the func test as intended. Works the way I did it with program="$0"; export program, but I'd like to see what you intended above. i.e. works with:

Code:
program="$0"; export program


Code:
               <action>$program @test 1 2 3</action>


I have /bin/sh as dash currently.

gtkdialog doesn't seem to see $0 as the script name; rather it sees it as sh.

as you can find via:

Code:
<action>echo $0 >/dev/tty</action>


Beats me currently why too... gtkdialog is pretty weird sometimes. If you ran
(inside the parent script) either:

Code:
( echo $0 )

or
Code:
sh -c "echo $0"


you would indeed get the name of the parent script...

I can't help thinking that gtkdialog 'weirdness' is also what caused my recent C programming fix attempt to fail. gtkdialog appears to somehow not be getting the parent environment inherited. weird, or what is it? Or maybe there is something wrong with my gtkdialog (I think I may have left in my recompiled one... oops... EDIT: no I didn't)

wiak

Last edited by wiak on Fri 25 May 2018, 10:50; edited 7 times in total
Back to top
View user's profile Send private message 
misko_2083


Joined: 08 Nov 2016
Posts: 88

PostPosted: Fri 25 May 2018, 09:26    Post subject:  

@wiak variable "$0" needs to be exported then
it will launch the script with exec:
Code:
#!/bin/dash
# or busybox sh/ash/dash, unless this script really needs bash in which case
# stop now

func_test() {
set -x
  yad --center --button="gtk-ok":0 --buttons-layout=center --text="
func_test arguments: $1, $2, $3
.  using exported variable: $VARIABLE
.  click OK to exit"
}

VARIABLE="$0"
export VARIABLE
echo $VARIABLE

MAIN_DIALOG='
<window>
   <vbox>
   <frame Test>
         <hbox>
            <button>
               <label>Click me</label>
               <action>exec "$VARIABLE" @test 1 2 3 </action>
                <action function="exit">Exit by button</action>
            </button>
         </hbox>
      <hbox>
         <button ok></button>
         <button cancel></button>
      </hbox>
   </frame>
   </vbox>
</window>
'
export MAIN_DIALOG

case $1 in
   -d | --dump) echo "$MAIN_DIALOG" ;;
   @*) sub=func_${1#@}; shift; $sub "$@"; exit $? ;; # relay func'tions
   *) gtkdialog --program=MAIN_DIALOG ;;
esac

echo done
Back to top
View user's profile Send private message 
wiak

Joined: 11 Dec 2007
Posts: 1828
Location: not Bulgaria

PostPosted: Fri 25 May 2018, 09:30    Post subject:  

misko_2083 wrote:
@wiak variable "$0" needs to be exported then
it will launch the script with exec:


Well, that's basically what I already said above misko. Variable needed exported, which is the way I did it in my program example (in post just before step's one), but why I'm wondering does step's own code not work?

Like I said if the script is made to contain line: sh -c "echo $0" the output of that is the parent script name (which is what I would expect), whereas echo $0 within gtkdialog is giving sh as the name (which I don't understand).

EDIT: @step: Sorry, FALSE ALARM... your code is working on my system now. My computer must have had an one two electron too many and gone on the blink...! But okay again now. Whew, what a relief... It was really weird though. I didn't change the code since; I just rebooted...
EDIT2: @step: Em no... your code really isn't working for me

wiak

Last edited by wiak on Fri 25 May 2018, 10:51; edited 1 time in total
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1925
Location: Japan

PostPosted: Fri 25 May 2018, 10:11    Post subject:  

wiak wrote:
I had /bin/sh -> dash when I ran this, but doesn't matter can be bash or ash etc.
If it matters or not depends very much on the function code. With this approach it is not possible to run a bash specific function from gtkdialog when the parent script is not called by bash. Following example works only with bash shebang. Even with #!/bin/sh is would choke on the cat statement with a syntax error and the whole script would crash. Commenting out this statement would at least start gtkdialog and would make all buttons (except first ) work.

The third button calls bash and instructs it to process external code. This should always work, no matter what shell the parent script or gtkdialog are using. IMO a strong point for using external code files when your goal is portability.

The 4th button calls external POSIX compliant code. No need for bash here. Gtkdialog's sh shell can handle this. Much more effient. Would be weird to call heavy bash from a light dash only to handle script that dash could process itself.

Code:
#!/bin/bash
sub_bash_only (){
T="Bash function worked!"
cat <(echo "$T") | gxmessage -file - # <= This will crash script unless script is run in bash
}
sub_posix (){
T="POSIX compliant function worked!"
gxmessage "$T"
}
export program="$0"
export MAIN_DIALOG='
<vbox>
<button label="Execute Bash function">
 <action>$program @bash_only</action>
</button>
<button label="Execute POSIX compliant function">
 <action>$program @posix</action>
</button>

<button label="Execute external bash code">
 <action>bash /tmp/bashfunction</action>
</button>
<button label="Execute external POSIX code">
 <action>/tmp/posixfunction</action>
</button>
</vbox>'

case $1 in
@*) sub=sub_${1#@}; shift; $sub "$@"; exit $? ;;
esac

# Write file with bash code. No shebang
[ ! -e /tmp/bashfunction ]  && echo 'T="Bash file worked"; cat <(echo "$T") | gxmessage -file -' > /tmp/bashfunction 
[ ! -e /tmp/posixfunction ] && echo 'gxmessage "Posix file worked"' > /tmp/posixfunction && chmod +x /tmp/posixfunction
gtkdialog
Back to top
View user's profile Send private message 
wiak

Joined: 11 Dec 2007
Posts: 1828
Location: not Bulgaria

PostPosted: Fri 25 May 2018, 10:23    Post subject:  

MochiMoppel wrote:
With this approach it is not possible to run a bash specific function from gtkdialog when the parent script is not called by bash....Even with #!/bin/sh is would choke on the cat statement with a syntax error and the whole script would crash....

The third button calls bash and instructs it to process external code. This should always work, no matter what shell the parent script or gtkdialog are using. IMO a strong point for using external code files when your goal is portability.


Well the point of step's method here is that the original script is being called again later as if it is an external code file, so the methodology is independent of the underlying system shell. I'm not sure what you mean when you say "not possible to run a bash specific function from gtkdialog when the parent script is not called by bash". Certainly the main script needs #!/bin/bash first line if using bashisms but I presume that's not what you are meaning.

wiak
Back to top
View user's profile Send private message 
wiak

Joined: 11 Dec 2007
Posts: 1828
Location: not Bulgaria

PostPosted: Fri 25 May 2018, 10:26    Post subject:  

edit... things going wrong here... erased post cos was accidentally testing with /bin/sh -> bash when I thought I was using /bin/sh -> dash. Pity.

I think I need to sleep.

@step: Turns out your code example with
. "'"$0"'"
in the gtkdialog still isn't working for me, but my method is:

program="$0";export program

and then in the gtkdialog using

$program
instead of
. "'"$0"'"

but still I don't see why $0 not working in step's gtkdialog example. Note, "'"$0"'" works if /bin/sh -> /bin/bash in my tests but not if it is a link instead to dash. Please correct me if I am wrong...

wiak
Back to top
View user's profile Send private message 
misko_2083


Joined: 08 Nov 2016
Posts: 88

PostPosted: Fri 25 May 2018, 10:59    Post subject:  

wiak wrote:
misko_2083 wrote:
@wiak variable "$0" needs to be exported then
it will launch the script with exec:


Well, that's basically what I already said above misko.

oops Smile

wiak wrote:
misko_2083 wrote:
@wiak variable "$0" needs to be exported then
it will launch the script with exec:


Variable needed exported, which is the way I did it in my program example (in post just before step's one), but why I'm wondering does step's own code not work?

wiak

Probaly because gtkdialog runs sh -c $0 ...
if the variable MAIN_DIALOG is modified then there is no need to export $0

Code:
#!/bin/dash
# or busybox sh/ash/dash, unless this script really needs bash in which case
# stop now

func_test() {
  yad --center --button="gtk-ok":0 --buttons-layout=center --text="
func_test arguments: $1, $2, $3
.  using exported variable: $VARIABLE
.  click OK to exit"
}

VARIABLE="hi there!"
export VARIABLE

VAR="\'$0\' @test 1 2 3"

MAIN_DIALOG='
<window>
   <vbox>
   <frame Test>
         <hbox>
            <button>
               <label>Click me</label>
               <action>VAR</action>
                <action function="exit">Exit by button</action>
            </button>
         </hbox>
      <hbox>
         <button ok></button>
         <button cancel></button>
      </hbox>
   </frame>
   </vbox>
</window>
'

case $1 in
   -d | --dump) echo "$MAIN_DIALOG" ;;
   @*) sub=func_${1#@}; shift; $sub "$@"; exit $? ;; # relay func'tions
   *) echo "$MAIN_DIALOG" | sed 's!VAR!'"$VAR"'!g' | gtkdialog -s ;;
esac

echo done
Back to top
View user's profile Send private message 
wiak

Joined: 11 Dec 2007
Posts: 1828
Location: not Bulgaria

PostPosted: Fri 25 May 2018, 11:02    Post subject:  

MochiMoppel wrote:


The third button calls bash and instructs it to process external code. This should always work, no matter what shell the parent script or gtkdialog are using. IMO a strong point for using external code files when your goal is portability.


Oh I see what you are getting at. Yes, external code files with or without bash in the command to access them works of course in terms of portability. However, was mainly just explaining how step's methodology worked in terms of using it with gtkdialog. It is up to the programmer to use correct #!/bin/dash or bash or sh at the top of the actual script. Certainly shouldn't be using #!/bin/sh if the code inside is actually using bashisms (even though that would still work on Puppy systems since /bin/sh is linked to bash there - that can't of course be relied on. So step's pattern is generally entirely portable in my opinion. Though I still don't see why his example isn't working with $0 used in his gtkdialog. I do of course understand why my example works with $program, but that is another matter.

wiak
Back to top
View user's profile Send private message 
wiak

Joined: 11 Dec 2007
Posts: 1828
Location: not Bulgaria

PostPosted: Fri 25 May 2018, 11:06    Post subject:  

.
Last edited by wiak on Thu 07 Jun 2018, 08:41; edited 3 times in total
Back to top
View user's profile Send private message 
misko_2083


Joined: 08 Nov 2016
Posts: 88

PostPosted: Fri 25 May 2018, 12:47    Post subject:  

check this line in actions.c
This is how the actions are executed
https://github.com/01micko/gtkdialog/blob/master/src/actions.c#L664
Back to top
View user's profile Send private message 
wiak

Joined: 11 Dec 2007
Posts: 1828
Location: not Bulgaria

PostPosted: Fri 25 May 2018, 12:56    Post subject:  

.
Last edited by wiak on Thu 07 Jun 2018, 08:42; edited 1 time in total
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1925
Location: Japan

PostPosted: Fri 25 May 2018, 21:29    Post subject:  

wiak wrote:
Certainly shouldn't be using #!/bin/sh if the code inside is actually using bashisms (even though that would still work on Puppy systems since /bin/sh is linked to bash there
Depends. My example would not work
Quote:
- that can't of course be relied on.
Not sure what "that" refers to but it is true in any case: It can't be relied on that sh is linked to bash and it can't be relied on that sh linked to bash can process bash specific code.
Back to top
View user's profile Send private message 
wiak

Joined: 11 Dec 2007
Posts: 1828
Location: not Bulgaria

PostPosted: Fri 25 May 2018, 21:46    Post subject:  

MochiMoppel wrote:
wiak wrote:
Certainly shouldn't be using #!/bin/sh if the code inside is actually using bashisms (even though that would still work on Puppy systems since /bin/sh is linked to bash there
Depends. My example would not work


No, as you said, your example would need #!/bin/bash at top.

wiak
Back to top
View user's profile Send private message 
wiak

Joined: 11 Dec 2007
Posts: 1828
Location: not Bulgaria

PostPosted: Fri 25 May 2018, 22:24    Post subject: wiak_modded gtkdialog alpha result success  

.
screenshot_using_wiakmodded_gtkdialog.jpg
 Description   Screenshot using wiak modded gtkdialog
 Filesize   29.61 KB
 Viewed   235 Time(s)

screenshot_using_wiakmodded_gtkdialog.jpg


Last edited by wiak on Thu 07 Jun 2018, 08:42; edited 2 times in total
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 87 of 99 [1476 Posts]   Goto page: Previous 1, 2, 3, ..., 85, 86, 87, 88, 89, ..., 97, 98, 99 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1509s ][ Queries: 13 (0.0895s) ][ GZIP on ]