BaCon Bits

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#106 Post by GatorDog »

If hug_imports.bac hasn't been edited, it has a path set to /usr/lib/hug.so

thats not correct the official file is here (maybe you have an already edited file )
http://www.basic-converter.org/hug_imports.bac.html
I stand corrected :oops:
-----------------
Just had a power outage :evil:
So I booted a fresh pup 528 into memory (pfix=ram).
- /usr/lib/hug.so is there.
- /usr/share/BaCon/ dir is there with two lang definition files. (For nicoedit and vim I think)

I ran a couple of bacon programs that were compiled with hug_imports.bac pointing to /usr/lib/hug.so
One file ran fine. The other failed. It expected a feature available in a later hug verson. (see below)

This points to a need for version tracking for hug. Bacon has a reserved variable $VERSION, but
this is for bacon itself and does not apply to hug.

The only way I know to find the hug version number is to open hug.bac. Peter maintains a commented
section there that includes the hug version number.

I've been backing up older files by naming them (for example) hug.59.so, hug.60.so, etc.

Simply having /usr/lib/hug.so (or libhug.so) gives no clue to what your working with.
In earlier post in this thread, I made tar files for the latest (beta) versions of hug as they became available.

Another point I'll mention; the hug_imports.bac version is loosely tied to the hug.bac/hug.so version.
To wit-
- hug_imports.bac is the mechanism to load HUG features from the shared library, hug.so
- When Peter adds a new HUG feature, hug_imports.bac is updated to include loading those new features.
- If a program is compiled with the new hug_imports.bac, it expects all the features are available in /usr/lib/hug.so
- If hug.bac is only updated for a bug fix, then hug_imports.bac version doesn't change, as there is no new features added.
. (Probably an oversimplification but works for this discussion)
- This is why the second program I tried didn't run. The program was compiled with a newer hug_imports, which was looking for
. /usr/lib/hug.so, which it found. However, that hug.so is outdated (pup 528), so the program failed because it couldn't
. import a feature from hug.so

So version control is another issue to consider when offering bacon programs for Puppy.
I really don't want to be limited to the "offical" hug version in Puppy. The current beta versions of Bacon, hug and hug_imports
have several bug fixes and some new features.

I don't follow gtkDialog closely, but it seems that it too is under development. Therefore new scripts require the latest gtkDialog version.
So, wonder how that is being addressed?

In conclusion, to shoot for success, this is what I've done with the programs that I've recently posted.
1.) I include the source code. Anyone that has bacon set up can compile it on their machine.
. They can use hug_imports.bac for the smallest possible program. And I comment in the
. source what versions of bacon, hug etc. were used.
2.) I include a "standalone" version of the binary/executable program. This is compiled using INCLUDE ".../hug.bac".
. It makes for a significantly larger program, but, it should only have common linux dependencies, (gtk etc.) and
. should run independent of any bacon related settings or programs.

Your Mi!eage May Vary,
GatorDog

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

#107 Post by sunburnt »

Hey guys, an item I looked into that GatorDog spoke of.

I ran "dd" on hug.so to see if I could find it`s version.
I found "1.0 build 24 beta r3" about half way down the output in rxvt.

Code: Select all

dd if=hug.so [of=hug.dd]
Providing the "of=" to make an output file just makes another copy of hug.
It seems to throw rxvt for a loop, I had to shut it down after doing this.

I can`t get sed to do anything with the output as it`s a binary file.
dd can convert, but I don`t know what it`s capabilities are.
If the output could be convert to text then sed might work.
It`s just one big long string with no LF or CR so grep isn`t much good.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#108 Post by seaside »

sunburnt wrote:Hey guys, an item I looked into that GatorDog spoke of.

I ran "dd" on hug.so to see if I could find it`s version.
I found "1.0 build 24 beta r3" about half way down the output in rxvt.

Code: Select all

dd if=hug.so [of=hug.dd]
Providing the "of=" to make an output file just makes another copy of hug.
It seems to throw rxvt for a loop, I had to shut it down after doing this.

I can`t get sed to do anything with the output as it`s a binary file.
dd can convert, but I don`t know what it`s capabilities are.
If the output could be convert to text then sed might work.
It`s just one big long string with no LF or CR so grep isn`t much good.
Hey sunburnt,

How about the command "strings".

Regards,
s

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

#109 Post by sunburnt »

SeaSide; That did it !!! I`m always amazed at the number of exec. there are.
Unix has been around sooo long that there`s a file for almost everything.

This gets the "build version" from /usr/lib/hug.so

Code: Select all

strings /usr/lib/hug.so |grep ' build '

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#110 Post by GatorDog »

seaside,
Wow!
strings
Who knew?

Thank you for taking time to share that. That's (another) one I hadn't run into before.

sunburnt,
The "build" that is reported is the version of "Bacon" that was used to generate the hug.so file.
So the search goes on.
-------------------

hug_imports.bac looks for hug.so
Q. If a new version of hug.so includes the version number (hug.62.so), can a pet package not only
put it in /usr/lib/ but also take care of linking hug.so/libhug.so to the new hug.62.so?

GatorDog

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#111 Post by big_bass »

Hey guys

great ideas

probably the fastest most secure way of knowing for sure its the right one is just

Code: Select all

md5sum /usr/lib/libhug.so

Code: Select all

md5sum /usr/lib/hug.so
then place that in the source code

in the header maybe the third line down of hug_imports.bac
then quickly use sed or the head command to get the value
or place it in an array if its always in the same place

how about this for keeping up to date with Bacon
to avoid old files in the first place this is what I use
*if you want to use hug.so instead just edit the code
the default is libhug.so (this way I dont clobber your hug.so )



used on the new *.so

Code: Select all

md5sum libhug.so >md5sum_libhug.txt 
MD=$(<md5sum_libhug.txt)
sed -i "2a \'$MD " hug_imports.bac
to keep your thread cleaner to read I'll point to the full source code
http://www.murga-linux.com/puppy/viewto ... 36&start=3

Joe

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

#112 Post by sunburnt »

Looks like with all this version problem, compiling with hug.bac is the way to go.
Develop with hug_imports.bac for a fast compile, and use hug.bac for the final app.
Seems to only add about 100 KB or so to the size. For GUIs that`s okay.
And any cli or code only app. ( no GUI ), I would write in Bash anyway.


# A thought... Each compile the same hug.bac file is compiled.
Why not compile it once into hug.so and then include it in the app.?

I assume Peter would have already done this if he could have.
But what`s the problem with adding already compiled code?
As I understand it, the include code gets pasted in at the line of the include.
And the include is the first line in the file, do a "join" files?

I`m sure there`s start points in ram for the calls to hug, etc., etc., etc.
But wouldn`t these be the same for all apps.?

A new compiler would be needed I suppose... Just thinking...

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#113 Post by GatorDog »

Why not compile it once into hug.so and then include it in the app.?
...
And the include is the first line in the file, do a "join" files?
Why not compile it once into hug.so and then include it in the app.?
The INCLUDE file is a text file. Similar to a dot file ( . filename ) in bash.
You're right, the text gets pasted in at that position.

From BaconGUI (and probably from the command line too), there is an option to
save the intermediate c-code. There is also a Bacon command "USEC" that allows
using c-code in a Bacon program. So in theory, the c-code could be generated
and use in a bacon program. But I'll bet that'd be a can of worms. :roll:

g

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

#114 Post by sunburnt »

A can of worms it certainly sounds like, but a promising approach.
Someone like Peter would have little problem with the concept.

Like Basic itself, a next layer of code is needed to streamline the methods.
Like so many languages and other tools I see, too much code to do the job.
You look at what`s needed to describe what`s wanted and it`s not much at all.
Hug is pretty straight forward, and it`s syntax is way better than gtkDialog.

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

Bacon port of "resizepfile"

#115 Post by GatorDog »

"resizepfile.sh" is a Puppy script to resize the Puppy save file.
This program is a port of the resizepfile script to a Bacon program.
In particular it makes use of Bacon's Associative array.
An associative array is an array of which the index is determined by a string, instead of a number. Associative arrays use
round brackets '(…)' instead of the square brackets '[…]' used by normal arrays.

An associative array can use any kind of string for the index, and it can have an unlimited amount of elements.

Code: Select all

fullname$("bob") = "Robert Fuller"
fullname$("jane") = "Janet Jenkins"
name$ = "bob"
PRINT fullname$(name$)
Output: Robert Fuller
EDIT: Corrected missing "$" on variable name.

The resizepfile shell script uses an external file (/etc/rc.d/PUPSTATE) to retrieve variables and their values.
The variables are in shell syntax. ie. variable = 'text'. This syntax is decoded and assigned
values in Bacon using an associative array.

This is the contents of (my) /etc/rc.d/PUPSTATE:

Code: Select all

PUPMODE=12
PDEV1='sr1'
DEV1FS='iso9660'
PUPSFS='sda1,vfat,/l5281108.173/lupu_528.sfs'
PUPSAVE='sda1,vfat,/lupusave-528_IM-.3fs'
PMEDIA='cd'
#ATADRIVES is all internal ide/pata/sata drives, excluding optical, excluding usb...
ATADRIVES='sda '
#ATAOPTICALDRIVES is list of non-usb optical drives...
ATAOPTICALDRIVES='sr0 sr1 '
#these directories are unionfs/aufs layers in /initrd...
SAVE_LAYER='/pup_rw'
PUP_LAYER='/pup_ro2'
#The partition that has the lupusave file is mounted here...
PUP_HOME='/mnt/dev_save'
#(in /initrd) ...note, /mnt/home is a link to it.
#this file has extra kernel drivers and firmware...
ZDRV=''
#complete set of modules in the initrd (moved to main f.s.)...
ZDRVINIT='no'
#Partition no. override on boot drive to which session is (or will be) saved...
PSAVEMARK=''
The variable name (ex. PUPMODE) will be used for the associative arrays index (ex. Pupstate$("PUPMODE")
I used a SUB-routine to translate the shell variables. Here is commented code.

Code: Select all

' ------------------
SUB READ_PUPSTATE_VARIABLES
' ------------------
	' Declare the associative array
	DECLARE Pupstate$ ASSOC STRING

	' Open the file for reading
	OPEN Pupstate_file$ FOR READING AS Filehandle_

	' Check for end of file
	WHILE NOT(ENDFILE(Filehandle_)) DO

		' read in line by line
		READLN Pass$ FROM Filehandle_

		' CHOP$ - remove leading/trailing space, tab, CR, etc.
		Pass$ = CHOP$(Pass$)

		' If line begins with "#" it's a comment.
		IF LEFT$(Pass$, 1) = "#" THEN
			'Dump bash comment lines
			CONTINUE
		END IF

		' If line contains "=", there's a variable assignment going on.
		IF INSTR(Pass$, "=") THEN

			' Break line up using "=" as seperator
			' Arg$[1] will be variable name. Arg$[2] is the value.
			SPLIT Pass$ BY "=" TO Arg$ SIZE na

			' Replace the single quotes with null
			Pass$ = CHOP$(REPLACE$(Arg$[2], Single_quote$, ""))

			' Set up variable = value
			Pupstate$(Arg$[1]) = Pass$
		END IF
	WEND
	' Close file
	CLOSE FILE Filehandle_
END SUB
This is a simple use of associative arrays. There are some extended examples in the Bacon reference pages.
The rest of the program implements the actions of the shell script along with the addition of a GUI.

GatorDog
Attachments
resizepfile.png
(36.56 KiB) Downloaded 1147 times
Bacon-resizepfile-src.tar.gz
Bacon source code for resizepfile.bac
(3.64 KiB) Downloaded 509 times
Last edited by GatorDog on Mon 31 Oct 2011, 18:59, edited 1 time in total.

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#116 Post by big_bass »

Hey GatorDog

thanks for posting that http://www.murga-linux.com/puppy/viewto ... &start=114 (I had already ported the old xmessage resizepfile.sh
to Xdialog and added larger save options) so going over your BaCon version
will be easier for me to understand and follow this new associative array in action
thanks this will be fun to learn in Bacon :D

*I know this is a BaCon thread but only to ease into understanding what is happening
the equivalent bash4 example is given below using your example converted to bash4

GatorDog
# associative array Bacon example
fullname("bob") = "Robert Fuller"
fullname("jane") = "Janet Jenkins"
name = "bob"
echo $fullname($name)


big_bass

Code: Select all

# bash4 associative array equivalence to the Bacon example above

declare -A fullname
#       -A option declares associative array.

fullname[bob]="Robert Fuller"
fullname[jane]="Janet Jenkins"


echo "bob's fullname is ${fullname[bob]}."
echo "jane's fullname is ${fullname[jane]}."

echo "${!fullname[*]}"   # The array indices used
Joe

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#117 Post by GatorDog »

Hi Joe,

To further the comparison a little, LOOKUP is used to get the array indices in Bacon.
(LOOKUP works similar to the SPLIT command)
LOOKUP <assoc> TO <array> SIZE <variable>

Code: Select all

LOOKUP Pupstate$ TO Index_name$ SIZE Count
and to access -

Code: Select all

FOR x = 0 TO Count - 1
    PRINT Index_name$[x]
NEXT 
GatorDog


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

#119 Post by sunburnt »

Hi guys; Thought I`d ask before just doing this in Bash.

My years of Quick Basic are slowly coming back to me as I use BaCon.
I recalled that Basic works with arrays not lists, Bash is powerful with both.
Basic`s problem with arrays is getting data into them... It`s one line at a time.
Bash will read and write files and arrays all in one statement.
I`m going to suggest this for the next BaCon, and also passing arrays to/from Bash.

# The Q:
An easy way to get the number of lines in a file to declare the array index?
All I can think of to do is:

Code: Select all

	OPEN File$ FOR INPUT AS file
	i = -1
	WHILE NOT(ENDFILE(file)) DO
		INCR i
		READLN txt$ FROM file
	WEND
	CLOSE file

	GLOBAL Array$[i] TYPE STRING

	OPEN File$ FOR INPUT AS file
	i = 0
	WHILE NOT(ENDFILE(file)) DO
		READLN txt$ FROM file
		IF ENDFILE(file) THEN BREAK
		Array$[i] = txt$
		INCR i
	WEND
	CLOSE file
Not a very efficient or elegant way to do anything at all.!

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#120 Post by seaside »

sunburnt,

Perhaps something like this-

Code: Select all

i$=EXEC$("wc -l filename")
Whether that's faster or not remains to be seen.... :)

regards,
s

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

#121 Post by sunburnt »

seaside; I thought of that, but why bother with BaCon if always using Bash.

Long ago in Quick Basic I noticed the HD cranking with each "read line".
Visual Basic had a second "input" command that would take in the whole file.
I`d like to see BaCon do file I/O and fill arrays quickly in one command.

I thought of just making a BaCon function library to simplify the repetitive code.
But that doesn`t improve BaCon`s actual operation any. It`d still work the HD.

At the moment my thought is porting my Bash app. "sysinfo" to BaCon.
I could do as you suggested, but then why not just have it call sysinfo?
I wrote a wrapper script to redirect sysinfo`s output to a file for BaCon to read.

But BaCon as a wrapper for Bash is sad. What`s the point of a compiled exec.?
For BaCon to be really useful it has to do most code stuff by itself.

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

Bacon Associative Arrays

#122 Post by GatorDog »

Sunburnt,
An easy way to get the number of lines in a file to declare the array index?
One way to accomplish the array assignment is to use Associative arrays.
This snipit will give you the line count and also goes ahead and assigns the lines to Array$(....) .

Code: Select all

DECLARE Array$ ASSOC STRING
Array_index = 0
OPEN My_file$ FOR READING AS Filehandle_
WHILE NOT(ENDFILE(Filehandle_)) DO
	INCR Array_index
	READLN Txt$ FROM Filehandle_
	Array$(STR$(Array_index)) = Txt$
WEND
CLOSE FILE Filehandle_
GatorDog

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

getting text file into array

#123 Post by vovchik »

Dear sunburnt,

I already posted this in the BaCon forum, but here it is again. Just another way:

Code: Select all

' --------------------
FUNCTION CAT(STRING FILENAME$)
' --------------------
    LOCAL fileline$, txt$ TYPE STRING
    IF FILEEXISTS(FILENAME$) THEN
        OPEN FILENAME$ FOR READING AS catfile
        WHILE NOT(ENDFILE(catfile)) DO
            READLN fileline$ FROM catfile
            txt$ = CONCAT$(txt$, fileline$, NL$)
        WEND
        CLOSE FILE catfile
    END IF
    RETURN CHOP$(txt$)
END FUNCTION

x$ = CAT("myfile.txt")
SPLIT x$ BY NL$ TO myarray$ SIZE mysize
The var "mysize" will be the size of the arrray (i.e. no. of lines). You can use "OPTION COLLAPSE" at the top of your program to ignore empty lines. As for passing arrays to SUBs and FUNCTIONs, Peter explains that business in the first few pages of the BaCon manual. It is entirely possible.

With kind regards,
vovchik

PS. BaCon should not be construed as a bash replacement, but a normal compiled language, like C, but with nicer, easier syntax. If you have complex tasks, it runs circles around bash.

PPS. If it is terseness that you're after, you can always do the following:

Code: Select all

x$ = EXEC$("cat myfile.txt")
SPLIT x$ BY NL$ TO myarray$ SIZE mysize

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#124 Post by big_bass »

thanks vovchik,GatorDog
for the great code snippets

and thanks for sparking the question sunburnt
I was trying to do arrays in BaCon too
as I was following the presize.bac code GatorDog posted
since I am used to doing this in bash I want to do it in Bacon also

arrays are the easy way to pull out data

*really the whole problem with linux in general is
important files have scrambled un formatted
data that needs much filtering to get the data out into "useable"
data if much pre thought went into those important files
we wouldnt need so many different tools to filter out the data
and everything would be easier and faster
I commented about this in "speeding up bash scripts"
If it is terseness that you're after, you can always do the following:
I like terse and simple and commented :D
I like to recycle simple code snippets sometimes I forget how to
do somethings that appear easy
it takes too much time to read large programs
and take out some small usable pieces because they get too complex
as time goes while new features get added quickly

so here is simple I took vovchik's snippet and added just a little bit to it
to keep it simple and recyclable
Joe

Code: Select all

'--- cat the file into an array using  x$ ---'
x$ = EXEC$("cat /etc/rc.d/PUPSTATE")
SPLIT x$ BY NL$ TO myarray$ SIZE mysize 

'--- this prints all arrays ---'
FOR x = 0 TO mysize  - 1
    PRINT myarray$[x]
NEXT 

'--- this prints only the first array ---'
PRINT myarray$[0]

'--- this prints only the scecond array ---'
PRINT myarray$[1]

'--- this prints only the third array ---'
PRINT myarray$[2]



is there a way to covert this bash snippet to BaCon
to check for undefined arrays

Code: Select all

#str=something

str=""

if [ $str ]
then
	echo "Not empty"
else
	echo "Empty"
fi

now if we only had some regular expressions in Bacon
explained with simple code snippets

I would be so happy it would make me dance

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

Bacon regular expressions

#125 Post by GatorDog »

now if we only had some regular expressions in Bacon explained with simple code snippets
Bacon REGEX
This checks that the text does not contain digits.
Then it looks for an underline chr or a capital X.

Code: Select all

Txt$ = "big_bass"

IF REGEX(Txt$, "[^[:digit:]]") THEN
	PRINT "Yep, ", Txt$, " is an ahpha dog!"
END IF

IF REGEX(Txt$, "_|X") THEN
	PRINT "Yes, there IS an underline chr or a capital X."
END IF
I would be so happy it would make me dance
It may not rise to the level of a dance, but can I at least get a toe-tap? :wink:

rod
Attachments
sparky.gif
(498 Bytes) Downloaded 1502 times

Post Reply