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 Sat 18 Aug 2018, 19:47
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 86 of 95 [1418 Posts]   Goto page: Previous 1, 2, 3, ..., 84, 85, 86, 87, 88, ..., 93, 94, 95 Next
Author Message
wiak

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

PostPosted: Wed 23 May 2018, 23:51    Post subject:  

Effectively, you end up needing something like:

Code:
execl("/bin/sh", "sh", "-c", command, (char *) NULL);


changed to

Code:
execl("/bin/bash", "bash", "-c", command, (char *) NULL);


or whatever, say, SHELL says the shell should be.

But to be robust, there is more to it, cos the parent should then wait for the child process to terminate and more. Basically needs a new C function call, which is a modified copied of existing system.c (though a much simpler, few lines of code: check SHELL, fork, exec, as above would maybe do in practice...).

EDIT:

One potential problem might be that the shell executed might need to be passed an appropriate environment (variables) so other form of exec maybe needed (I don't know off the top of my head, but maybe execle or execve). One quick explanation of all these different exec calls is given on Wikipedia:

https://en.wikipedia.org/wiki/Exec_(system_call)

(I can't get that link to format properly as a url, sorry. url tags just make post invisible).

wiak

Last edited by wiak on Thu 24 May 2018, 02:23; edited 2 times in total
Back to top
View user's profile Send private message 
wiak

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

PostPosted: Thu 24 May 2018, 02:03    Post subject:  

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

Joined: 04 May 2012
Posts: 1066

PostPosted: Thu 24 May 2018, 03:31    Post subject:  

@wiak, I use the following scripting pattern to work around the lack of export -f in non-bash Bourne shells, and keep all sub-functions in a single file. As far as my testing goes, it works for dash, ash, bourne sh, and bash. It should work for ksh (untested). Save to file, chmod +x file, run: dash file, see:
hi
there
done

Code:

sub_func() { foo; echo "$@"; }
foo() { echo $VARIABLE; }
VARIABLE=hi; export VARIABLE
#Main body
case $1 in
  @*) sub=sub_${1#@}; shift; $sub "$@"; exit $? ;; # relay sub_func'ions
  # add cases for typical script option parsing, i.e., --help, -x, etc.
esac
# ...
if true; then
  # call sub-function (fork)
  "$0" @func there
  # process $? from @func if needed
fi
# ...
echo done

I think this is more portable as far as gtkdialog scripting than changing the underlying system call. Of course, your gtkdialog XML needs to call "$0" @func instead of just "func". This pattern isn't specific to gtkdialog programming, it works for any script for which you want to keep exported functions inside the main file.

_________________
Fatdog64-721|+Packages|Kodi|Findnrun|+forum|gtkmenuplus

Last edited by step on Thu 24 May 2018, 15:49; edited 1 time in total
Back to top
View user's profile Send private message 
wiak

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

PostPosted: Thu 24 May 2018, 04:17    Post subject:  

Ah yes, step, that's exactly the type of pattern I jotted down myself today as a workaround - i.e. using case just as you have done (almost... I put all my functions in a totally separate but single file). Better than tons of little external function scripts. Thanks.

The way you've organised it is much better than my rough draft though. I may well use what you suggest!

wiak

EDIT: Having said that, there is no reason I can see that modifying gtkdialog.c itself to use appropriate sh, as determined by SHELL variable contents, wouldn't likely be completely portable (though I don't know intracacies of ksh and so on, so I might be wrong). If so, these kind of workarounds would no longer be required.
Back to top
View user's profile Send private message 
misko_2083


Joined: 08 Nov 2016
Posts: 53

PostPosted: Thu 24 May 2018, 07:44    Post subject:  

step wrote:
Save to file, chmod +x file, run: dash file, see:
hi
there
done

Code:

sub_func() { foo; echo "@"; }
foo() { echo $VARIABLE; }
VARIABLE=hi; export VARIABLE
#Main body
case $1 in
  @*) sub=sub_${1#@}; shift; $sub "$@"; exit $? ;; # relay sub_func'ions
  # add cases for typical script option parsing, i.e., --help, -x, etc.
esac
# ...
if true; then
  # call sub-function (fork)
  "$0" @func there
  # process $? from @func if needed
fi
# ...
echo done


Small typo @step
sub_func() { foo; echo "$@"; }

@wiak Is it safe to run any string from enviroment variable SHELL? Very Happy
Back to top
View user's profile Send private message 
wiak

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

PostPosted: Thu 24 May 2018, 07:49    Post subject:  

@step:

Just for anyone else reading your 'solution', you missed out a dollar on the first line; minor typo I realise but should be:

Code:
sub_func() { foo; echo "$@"; }


Yes, I like this methodology; a pretty precise/neat way of re-calling the script (parent) from the child (such as gtkdialog) and using the parent script functions whilst appropriately shifting in the commandline parameters in the case statement. It was what I was looking for and wondering how to do it.

wiak

EDIT: @misko: Just noticed you sent same about $ missed out (which didn't anyway actually effect the operation of the program aside from the output text), sorry.
Back to top
View user's profile Send private message 
wiak

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

PostPosted: Thu 24 May 2018, 08:15    Post subject:  

misko_2083 wrote:

@wiak Is it safe to run any string from enviroment variable SHELL? Very Happy


Yeah, well... that's a good point. It isn't of course. Hmmm, yes, that is a problem - only option I can think of would be that gtkdialog always execs /bin/bash -c rather than current /bin/sh -c but that would be crippling the use of gtkdialog in other shells... Oh well, step's solution and similar will have to do... Really it's a pity ash or dash or similar didn't just have that export -f functionality without needing the rest of bash bloat then this issue wouldn't arise.

EDIT: Hmmm (I hate to give up...) but you could pass in the name of the shell you want it to use, to gtkdialog as a command line argument...

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

Joined: 04 May 2012
Posts: 1066

PostPosted: Thu 24 May 2018, 15:57    Post subject:  

wiak and misko_2083 thanks, typo fixed.

BTW, I find that this way of organizing code is useful also when scripting YAD buttons.

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

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

PostPosted: Fri 25 May 2018, 00:52    Post subject:  

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


Joined: 18 May 2008
Posts: 4829
Location: Kingwood, TX

PostPosted: Fri 25 May 2018, 01:58    Post subject:  

Weird, I swore it used popen instead of system.
Its been a while since I have used gtkdialog, but IIRC, you can put all the functions you would export via bashisms in a file and use that with the event file option ... I think it is -e.
(export -f is a bashism)
Jwm uses system also, we tried switch to fork/exec but it caused nuanced problems that i don't recall. Better to just put your functions in an event file.

Btw, you can do SHELL=/bin/bash && myprog

_________________
Check out my github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
wiak

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

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

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

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

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

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

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

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

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


Joined: 11 Dec 2013
Posts: 3219
Location: holland

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

step wrote:
Code:

sub_func() { foo; echo "$@"; }
foo() { echo $VARIABLE; }
VARIABLE=hi; export VARIABLE
#Main body
case $1 in
@*) sub=sub_${1#@}; shift; $sub "$@"; exit $? ;; # relay sub_func'ions
# add cases for typical script option parsing, i.e., --help, -x, etc.
esac
# ...
if true; then
# call sub-function (fork)
"$0" @func there
# process $? from @func if needed
fi
# ...
echo done


I'd like to try. How can I apply step's workaround above in an existing gtkdialog script that has export -f functons like this ?
Code:
func_test(){
....
....
}
export -f func_test


Fred

_________________
Dog Linux website
Back to top
View user's profile Send private message 
wiak

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

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

Something like this, Fred. In practice you might want to improve the quoting round $program.

I had /bin/sh -> dash when I ran this, but doesn't matter can be bash or ash etc.

Of course you can put the case statement higher up in the program so when it gets called to access functions later the variables don't actually need to be exported again...

wiak

Code:
#!/bin/sh

#Functions
sub_now (){
   date > /tmp/date
}
#you avoid using export -f since a bashism

sub_filemanager (){
 pcmanfm &
}

#Variables
program="$0"; export program

script='
<vbox>
  <entry>
    <variable>ENTRY_DATE</variable>
    <input>cat /tmp/date</input>
  </entry>
  <button>
    <label>Refresh</label>
    <action>$program @now</action>
    <action>refresh:ENTRY_DATE</action>
  </button>
  <button>
    <input file stock="gtk-home"></input>
    <action>$program @filemanager</action>
  </button>
</vbox>'
export script

#Main body - this is step's main pattern along with recalling the script
case $1 in
@*) sub=sub_${1#@}; shift; $sub "$@"; exit $? ;; # relay sub_func'ions
# add cases for typical script option parsing, i.e., --help, -x, etc.
esac

gtkdialog -p script

Last edited by wiak on Fri 25 May 2018, 08:56; edited 3 times in total
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 86 of 95 [1418 Posts]   Goto page: Previous 1, 2, 3, ..., 84, 85, 86, 87, 88, ..., 93, 94, 95 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.1236s ][ Queries: 15 (0.0667s) ][ GZIP on ]