From the creator of BaCon - gtk-server

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#16 Post by mcewanw »

jamesbond wrote:If you can get this to work, it would be interesting.
Unfortunately, Puppy Wary uses bash 3.

coproc functionality comes with bash 4...

But it seems to me that using awk is fine anyway, or a mixture of awk and bash version 3.

Awk is a powerful programming language. Puppy users may be more familiar with bash, but awk is there on the system and not a memory hog when running, so perfectly good for programming gtk-server GUIs. So maybe this is a good time to practice/learn awk...

Quote from http://www.ibm.com/developerworks/linux ... index.html
Sure, awk doesn't have a great name. But it is a great language. Awk is geared toward text processing and report generation, yet features many well-designed features that allow for serious programming. And, unlike some languages, awk's syntax is familiar, and borrows some of the best parts of languages like C, python, and bash (although, technically, awk was created before both python and bash). Awk is one of those languages that, once learned, will become a key part of your strategic coding arsenal.
http://www.grymoire.com/Unix/Awk.html:
Why is AWK so important? It is an excellent filter and report writer. Many UNIX utilities generates rows and columns of information. AWK is an excellent tool for processing these rows and columns, and is easier to use AWK than most conventional programming languages. It can be considered to be a pseudo-C interpretor, as it understands the same arithmatic operators as C. AWK also has string manipulation functions, so it can search for particular strings and modify the output. AWK also has associative arrays, which are incredible useful, and is a feature most computing languages lack. Associative arrays can make a complex problem a trivial exercise.

http://www.vectorsite.net/tsawk_1.html#m1:
Awk statements comprise a programming language. In fact, Awk is useful for simple, quick-and-dirty computational programming. Anybody who can write a BASIC program can use Awk, although Awk's syntax is different from that of BASIC. Anybody who can write a C program can use Awk with little difficulty, and those who would like to learn C may find Awk a useful stepping stone -- with the caution that Awk and C have significant differences beyond their many similarities.

There are, however, things that Awk is not. It is not really well suited for extremely large, complicated tasks. It is also an "interpreted" language -- that is, an Awk program cannot run on its own, it must be executed by the Awk utility itself. That means that it is relatively slow, though it is efficient as interpretive languages go, and that the program can only be used on systems that have Awk.
As an example of memory usage, here is an extract from "top" showing the earlier gtk-server awk program running:

Code: Select all

PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND

 8624  8623 root     S    21544   8%   0   0% gtk-server -stdin
...
 8623 30060 root     S     2320   1%   0   0% /bin/gawk -f gtkprog.awk
Note that an instance of "bash" is heavier in terms of resource usage than gawk (from earlier running bash pipe example):

Code: Select all

PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND
13739     1 root     S    21432   8%   0   0% gtk-server -fifo /tmp/bash.gtk -detach
...
13737 30060 root     S     2948   1%   0   0% gtkprog.bash
So it seems to me like a good time to start writing new Puppy gui utilities using awk (gawk) plus gtk-server, as an alternative to bash/gtkdialogs. It will be interesting to compare awk as an interpretive programming language compared to bash in terms of speed. I think awk will do fine.
github mcewanw

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#17 Post by sunburnt »

Puppy524 uses Bash 4 or 4.2 ( I think...), a few other niceities too.

I dnlded the awk manual, but I wanted to see what Bash can do.
I didn`t think of using awk for just communicating. A simple fix...
Using stdin directly would be nice also ( no extra awk to run ).
In the end I`m speed testing all of this to see which is the winner.
As far as ram usage goes, the difference is so small it`s irrelivant.

Q... What GUIs / projects does anyone have planned for gtk-server?

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#18 Post by mcewanw »

sunburnt wrote: I didn`t think of using awk for just communicating. A simple fix...
Unfortunately, I don't think it is so simple to use gawk just to communicate with gtk-server afterall, in which case its as well just programming in gawk and calling commandline utils with the gawk "system" command as and when required (or accept using named pipes with bash versions less than 4).

I'll see if the older version of Lucid I have also has bash 4 - I'm pretty sure it is quite easy to get coproc to work with gtk-server. Unfortunately, that wouldn't help bash 3 users, of which I am currently one (using Puppy Wary).
github mcewanw

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

using wiakmv with gtk-server -ipc

#19 Post by mcewanw »

I commented earlier that I preferred using gtk-server -ipc which utilises an inter-process message queue rather than messy named pipes format. However, as I said, the problem is that the published example for doing that requires gtk-server to run two instances of itself, which is heavy on resource usage.

It turns out that the small wiakmv message queue utility can communicate directly with gtk-server message queue, and wiakmv has very low resource usage as the ps output at the bottom of this post illustrates.

I therefore wrote a demo of gtk-server -ipc using wiakmv, which is below. I've copied a pet of the small wiakmv utility here also (the dotpet puts the tiny wiakmv into /usr/local/bin). The source code for wiakmv is GPL and available at http://wiak.org downloads.

Code: Select all

#!/bin/sh
# Bourne shell gtk-server IPC demo using wiakmv (GPL)
#   for the message queue communications with gtk-server
#
# Creation date (YMD): 2011/10/12 (original version 1.0.0)                                        
# Revision date (YMD): 2011/10/12
# (C) Copyright 2011 William McEwan (http://wiak.org); GPL
#
# Using code from Bourne shell named pipe demo with the GTK-server
# by Peter van Eerten; Revision January 15, 2007 
#
# Current PID ($$) is used for the unique message queue comms channel
#------------------------------------------------

# Communicate with GTK-server
gtk()
{
  # send the gtk string to gtk-server 
  wiakmv --zid=$$ --type=1 --send "$@"
  # receive the gtk-server return string into RESULT
  RESULT=$(wiakmv --zid=$$ --type=1 --receive stdout)
}

# Main starts here

# Start gtk-server in InterProcessComms mode
gtk-server -ipc=$$ -detach

# Setup GUI
gtk "gtk_init NULL NULL"
gtk "gtk_window_new 0"
WIN=$RESULT
gtk "gtk_window_set_title $WIN 'BASH GTK-server demo'"
gtk "gtk_window_set_default_size $WIN 400 200"
gtk "gtk_window_set_position $WIN 1"
gtk "gtk_table_new 10 10 1"
TBL=$RESULT
gtk "gtk_container_add $WIN $TBL"
gtk "gtk_button_new_with_label 'Click to Quit'"
BUT=$RESULT
gtk "gtk_table_attach_defaults $TBL $BUT 5 9 5 9"
gtk "gtk_widget_show_all $WIN"

EVENT=0

# Mainloop
while [[ $EVENT -ne $BUT && $EVENT -ne $WIN ]]
do
    gtk "gtk_server_callback WAIT"
    EVENT=$RESULT
done

# Exit GTK-server
wiakmv --zid=$$ --type=1 --send "gtk_server_exit"
# exit wiakmv
wiakmv --zid=$$ --type=1 --quit
exit 0
Showing the low wiakmv resource usage with ps:

The running gtk-server here uses roughly 21 MByte RAM and wiakmv just 1.5 MB (half what awk would use) and being in memory, message queues are fast comms channel. This is on an old Pentium III 256 MByte system with Wary 5.1.4.1 os.

Code: Select all

# ps ao vsize,pmem,pcpu,cmd | egrep '(gtk-server|wiakmv)'
 21436  3.2  0.0 gtk-server -ipc 9438 -detach
  1508  0.1  0.0 wiakmv --zid=9438 --type=1 --receive stdout
Attachments
wiakmv-2.3.0.pet
wiakmv provides message queue facility to bash
(4.04 KiB) Downloaded 394 times
github mcewanw

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#20 Post by mcewanw »

An average gtkdialog3 gui seems to consume about the same amount of RAM as gtk-server.

The gtk-server binary occupies around 250 kB on disk, whereas gtkdialog3 occupies around 172kB. However, gtk-server appears to offer much more functionality (Full GTK API available, and more...) and doesn't see too difficult to program (and good for learning gtk more generally). gtk-server seems to be well-supported and under active development in a sizable community. Available for Linux and MS Windows it seems.

I expect a gui created with gtk-server to be faster than a gtkdialog3 one.

Bacon appears to occupy around 392 kB on disk.

The challenge would be to port some bash/gtkdialogs to bash(or awk)/gtk-server and compare the results.
github mcewanw

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#21 Post by technosaurus »

I posted a pet and a tarball with a bunch of fixed up examples about the time 2.3.1 came out here:
http://murga-linux.com/puppy/viewtopic. ... 947#388947

I do recommend installing findutils though for the examples.

I'm interested in trying to use it for SDL, has anyone seen an sdl config file?
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#22 Post by jamesbond »

technosaurus wrote:I posted a pet and a tarball with a bunch of fixed up examples about the time 2.3.1 came out here:
http://murga-linux.com/puppy/viewtopic. ... 947#388947

I do recommend installing findutils though for the examples.

I'm interested in trying to use it for SDL, has anyone seen an sdl config file?
Google failed me, it didn't find your original post when I searched for it. Otherwise I wouldn't have published this as a new thread :oops:
What's the findutils for?
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#23 Post by sunburnt »

Unfortunately, that wouldn't help bash 3 users, of which I am currently one (using Puppy Wary).
Do I smell an SFS file in the making?
I think most Linuxes will be into Bash 4 shortly, so not really much of a problem.

###> BASH update:
Lucid 5.25 is the most leading-edge Lucid ever and a Five Digit Update from Lucid 5.2.
It has Bash 4.1.0, an upgrade from Bash 3
Last edited by sunburnt on Wed 12 Oct 2011, 09:12, edited 3 times in total.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#24 Post by technosaurus »

jamesbond wrote:
technosaurus wrote:I posted a pet and a tarball with a bunch of fixed up examples about the time 2.3.1 came out here:
http://murga-linux.com/puppy/viewtopic. ... 947#388947

I do recommend installing findutils though for the examples.

I'm interested in trying to use it for SDL, has anyone seen an sdl config file?
Google failed me, it didn't find your original post when I searched for it. Otherwise I wouldn't have published this as a new thread :oops:
What's the findutils for?
I'm glad someone started a separate thread.

findutils is for loading images and libraries (updatedb and locate are the specific binaries iirc)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#25 Post by mcewanw »

jamesbond wrote:If you can get this to work, it would be interesting. I don't like to trap SIGINT while the script is waiting to read the pipe - I'll end up having to kill both the scripts and the gtk-server manually :?
The one required to use gtk-server most efficiently from bash...

Working gtk-server -stdin example, using coproc. You need bash version 4+ for this so won't work in current Puppy Wary I think.

Took a while, but finally got it going (using Lucid 5.2.5). Not heavily tested though, but runs very fast on this old Pentium III 256 MB machine. Probably a couple of extra lines required to tidy up after the coproc complete (maybe need to free up used file descriptors, but maybe done automatically):

Code: Select all

#!/bin/bash
# Bourne shell gtk-server stdin demo using bash ver4 coproc cmd
#   for the message communications with gtk-server
#
# Creation date (YMD): 2011/10/12 (original version 1.0.0)                                       
# Revision date (YMD): 2011/10/12
# (C) Copyright 2011 William McEwan (http://wiak.org); GPL
#
# Using code from Bourne shell named pipe demo with the GTK-server
# by Peter van Eerten; Revision January 15, 2007
#------------------------------------------------

# Communicate with GTK-server
gtk()
{
  # send the gtk string to gtk-server
  echo "$@" >&${COPROC[1]}
  # receive the gtk-server return string into RESULT
  read -u ${COPROC[0]} RESULT
}

# Main starts here

# Start gtk-server in stdin mode
coproc gtk-server -stdin

# Setup GUI
gtk "gtk_init NULL NULL"
gtk "gtk_window_new 0"
WIN=$RESULT
gtk "gtk_window_set_title $WIN 'BASH GTK-server demo'"
gtk "gtk_window_set_default_size $WIN 400 200"
gtk "gtk_window_set_position $WIN 1"
gtk "gtk_table_new 10 10 1"
TBL=$RESULT
gtk "gtk_container_add $WIN $TBL"
gtk "gtk_button_new_with_label 'Click to Quit'"
BUT=$RESULT
gtk "gtk_table_attach_defaults $TBL $BUT 5 9 5 9"
gtk "gtk_widget_show_all $WIN"

EVENT=0

# Mainloop
while [[ $EVENT -ne $BUT && $EVENT -ne $WIN ]]
do
    gtk "gtk_server_callback WAIT"
    EVENT=$RESULT
done

# Exit GTK-server
gtk "gtk_server_exit"
exit 0
github mcewanw

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#26 Post by jamesbond »

Thanks mcewannw. This works well. (Fatdog comes with bash4 so bash4 isn't a problem).

cheers!
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#27 Post by technosaurus »

the musl libc wrapper has a coproc-like replacement that should work with even busybox ash
http://git.etalabs.net/cgi-bin/gitweb.c ... sh;hb=HEAD

I was pretty sure I was using busybox ash before, but not sure what method I used, I haven't used it in a while (determined it was easier to just use C with tcc in compile and run mode - yes C is a scriptable language now)

also MU made a glade project builder for gtk-server
http://www.murga-linux.com/puppy/viewtopic.php?t=60705
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#28 Post by mcewanw »

technosaurus wrote:the musl libc wrapper has a coproc-like replacement that should work with even busybox ash
http://git.etalabs.net/cgi-bin/gitweb.c ... sh;hb=HEAD
This sounds interesting if it can replace coproc. Can you enlighten me on this code? I don't see what it does or how to use it instead of coproc.
github mcewanw

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#29 Post by technosaurus »

This is the section I was referring to:

Code: Select all

[ "$nolink" ] || {
tmp_specs=$HOME/.specs.tmp.$$
printf '*link_libgcc:\n\n\n' > "$tmp_specs" || exit 1
exec 3<"$tmp_specs"
rm -f "$tmp_specs"
set -- -specs=/proc/self/fd/3 "$libc_start" "$libc_crt" "$@" "$libc_end" \
  -Wl,--start-group -lc -lgcc -lgcc_eh -Wl,--end-group \
  -Wl,-dynamic-linker,"$ldso_pathname" -Wl,-nostdlib
}
I don't fully understand it myself, but when I read the coproc manpage, this was what immediately came to mind... maybe my gut is lying to me, but this is what I _think_ is going on in the wrapper:

sets up a file in /root/.specs.tmp.<Random#>
with the contents:
"*link_libgcc:


"
then:
exec 3 /root/.specs.tmp.<Random#>
establishes a new process in /proc/self/fd/3 and the original file can be removed and the process can be used
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#30 Post by mcewanw »

technosaurus wrote: then:
exec 3 /root/.specs.tmp.<Random#>
establishes a new process in /proc/self/fd/3 and the original file can be removed and the process can be used
Not sure about this. I have written a C prog before that started two processes with a pipe between then (redirecting stdins and stdouts appropriately). Its trickyish to do, and I might have trouble understanding my own code so many years later... I'm not entirely sure if I could communicate with gtkserver via that or not. Lots of interesting things going on just now... alas that means "so many things to do..."

I've also just installed gtk-server on my Windows box at work too, though still to use it for anything there. I want to try it for something I need to do at work, where they don't use Linux. But I'd love to introduce some programmer work colleagues to gtk interface anyway one way or the other.
github mcewanw

User avatar
DocSalvage
Posts: 11
Joined: Sat 30 Jun 2012, 18:59
Location: Tallahassee, FL, USA
Contact:

#31 Post by DocSalvage »

Though this is a very old thread, let me just add that it bash 4 confuses things even more as it adds support for '|&' but it doesn't mean the same thing. It is shorthand for '2>&1 |'.

The '<>' operator creates a bidirectional pipe.

See...
http://wiki.bash-hackers.org/syntax/redirection
http://wiki.bash-hackers.org/howto/redirection_tutorial
http://www.tldp.org/LDP/abs/html/io-redirection.html
[i][color=blue]DocSalvager (a.k.a. DocSalvage)[/color][/i]
[url]http://www.docsalvage.info[/url], [url]http://www.softwarerevisions.net[/url]

Post Reply