|
Post by vovchik on Nov 29, 2018 22:30:26 GMT 1
Dear Joe,
Very nice - and works like a charm.
Thanks.
With kind regards, vovchik
|
|
|
Post by bigbass on Dec 7, 2018 20:15:38 GMT 1
We can make real bash commands work in bacon several different ways
Joe
PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith OPTION PARSE FALSE
'---solve "the problem" by not needing quoted strings in the first place DEF FN BASHLINE(...) = fprintf(stdout, "%d\n" ,system(#__VA_ARGS__))
'--- Now we will take any arguments you may need in the BASHLINE
PRINT PRINT "tr command demo " PRINT "replace spaces with commas using the tr command and BASHLINE" PRINT "the simple way to change any delimiter" PRINT BASHLINE(echo hello how are you | tr ' ' ',' ) PRINT
PRINT "WHAT YOU SEE IS WHAT YOU GET" PRINT "a new printline function demo " PRINT
DEF FN PRINTLINE(...) = fprintf(stdout, "%s\n", #__VA_ARGS__)
PRINTLINE(<p> <A HREF="http://localhost/phpinfo.php"> php info link</A><p>) PRINT
and a few more demos
PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith OPTION PARSE FALSE
'--- I declare not to forget to DECLARE using a char ARRAY DECLARE command_ls TYPE char ARRAY 2 DECLARE command_df TYPE char ARRAY 2
'--- we use the C string copy command because we compile the macro '--- during the conversion time '--- and make new bash functions to be used in BaCon strcpy(command_ls, "ls *.bac" ) strcpy(command_df, "df -h" )
'---Some real bash stand alone functions in BaCon they take no arguments '---we define the new bash functions in C directtly '---df - report file system disk space usage DEF FN DISKSPACE() = fprintf(stdout, "%d\n" ,system(command_df))
PRINT PRINT "DISKSPACE() demo" PRINT DISKSPACE() PRINT PRINT PRINT
'--- list all *.bac files in the current dirctory DEF FN LS_BAC() = fprintf(stdout, "%d\n" ,system(command_ls))
PRINT PRINT "LS_BAC() demo" PRINT LS_BAC() PRINT PRINT
'--- print the working directory DEF FN PWD() = fprintf(stdout, "%s\n" , getenv("PWD"))
PRINT PRINT "PWD() demo " PRINT PWD() PRINT PRINT
'--- print the file path DEF FN FILEPATH() = fprintf(stdout, "%s/%s\n" , getenv("PWD"), __FILE__)
PRINT PRINT "FILEPATH() demo " PRINT FILEPATH() PRINT PRINT
'---solve "the problem" by not needing quoted strings in the first place DEF FN BASHLINE(...) = fprintf(stdout, "%d\n" ,system(#__VA_ARGS__))
'--- Now we will take any arguments you may need in the BASHLINE
PRINT PRINT "tr command demo " PRINT "replace spaces with commas using the tr command and BASHLINE" PRINT "the simple way to change any delimiter" PRINT BASHLINE(echo hello how are you | tr ' ' ',' ) PRINT
PRINT PRINT "ls command demo " PRINT "print *.bac files in this directory with BASHLINE" PRINT BASHLINE(ls *.bac ) PRINT PRINT
'--- we will make some specialized custom functions now
DECLARE command_aptinstalled TYPE char ARRAY 2
'--- we use C string copy because we compile the macro '--- during the conversion time '--- and make new functions to be used in BaCon strcpy(command_aptinstalled, "apt list --installed > myinstalled_packages.txt" ) 'strcpy(command_aptinstalled, "apt list --installed | more" )
DEF FN APT_INSTALLED() = fprintf(stdout, "%d\n" ,system(command_aptinstalled))
'--- made a terminal command function simplified and seen in BaCon '--- we can write a file myinstalled_packages.txt '--- if you want to see it in the terminal "apt list --installed | more"
PRINT PRINT "APT_INSTALLED() demo wrote file myinstalled_packages.txt" APT_INSTALLED() PRINT
|
|
|
Post by vovchik on Dec 7, 2018 22:51:51 GMT 1
Dear Joe,
Thanks. That BASHLINE function would be nice for Rik's project, I think, and is generally very useful. I have problems compiling, though:
PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <string.h> PRAGMA INCLUDE <stdlib.h> PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith OPTION PARSE FALSE
DEF FN BASHLINE(...) = fprintf(stdout, "%s\n" , system(#__VA_ARGS__))
PRINT PRINT "tr command demo " PRINT "replace spaces with commas using the tr command and BASHLINE" PRINT "the simple way to change any delimiter" PRINT BASHLINE(echo hello how are you | tr ' ' ',' ) PRINT
I am getting this:
format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
Do you know what I am doing wrong???
With kind regards, vovchik
|
|
|
Post by bigbass on Dec 8, 2018 6:10:49 GMT 1
Hello vovchik sorry so late to respond I wasn't at a computer to test with I was able to sort it out And force the error with compiler options then undo the error PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <string.h> PRAGMA INCLUDE <stdlib.h> PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wformat=0 OPTION PARSE FALSE
the key line is -Wformat=0 if you change the zero to 1 or two it will cause the error warning I fixed the cause now I needed a %d not a %s DEF FN BASHLINE(...) = fprintf(stdout, "%d\n" ,system(#__VA_ARGS__)) now it should work for you too without the -Wformat=0 but its good to know where and how to fix these errors for a future reference thanks for reporting I will edit the above post with the fix this is the link I used tigcc.ticalc.org/doc/comopts.htmlJoe
|
|
|
Post by vovchik on Dec 8, 2018 11:50:54 GMT 1
Dear Joe, Thanks. Everything works. With kind regards, vovchik
|
|
|
Post by bigbass on Dec 8, 2018 15:38:11 GMT 1
Hello vovchik
When I compiled the the first time I didn't get any errors then I added stricter compiler options and fixed the code
in the above code demos I fixed the second part after and a few more demos
they should compile cleanly now
just as an extra
I also tested the code in g++ just in case we do something in g++ later we need this
PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <string.h> PRAGMA INCLUDE <stdlib.h> PRAGMA INCLUDE <iostream> PRAGMA LDFLAGS -lbacon++ -s PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wformat=1 OPTION PARSE FALSE
thanks for the bacon lib tip!
Joe
|
|
|
Post by bigbass on Dec 8, 2018 21:24:22 GMT 1
here is a replace word function
'---C program to search and replace '---all occurrences of a word with another word.
'--- port from geeks to geeks to BaCon by bigbass
PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <string.h> PRAGMA INCLUDE <stdlib.h> PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wformat=0 OPTION PARSE FALSE
'---Function to replace a string with another string '====================================================================== FUNCTION REPLACEWORD(char* s, char* oldW, char* newW) TYPE char* '====================================================================== LOCAL result TYPE char* LOCAL i TYPE int LOCAL cnt TYPE int cnt = 0 i = 0 LOCAL newWlen TYPE int LOCAL oldWlen TYPE int newWlen = strlen(newW) oldWlen = strlen(oldW) '--- Counting the number of times old word '--- occur in the string WHILE (s[i] != '\0') IF (strstr(&s[i], oldW) == &s[i]) THEN INCR cnt '--- Jumping to index after the old word. i = i + (oldWlen - 1) END IF INCR i WEND '--- Making new string of enough length result = (char*)malloc(i + cnt * (newWlen - oldWlen) + 1) i = 0 WHILE (*s ) '--- compare the substring with the result IF (strstr(s, oldW) == s) THEN strcpy(&result[i], newW) i = i + newWlen s = s + oldWlen ELSE result[i++] = *s++ END IF WEND result[i] = '\0' RETURN result END FUNCTION '--- main Program DECLARE str[] = "xxforxx" TYPE char DECLARE c[] = "xx" TYPE char DECLARE d[] = "Geeks" TYPE char DECLARE result TYPE char*
result = '\0' '--- oldW string STR$ = (char*)str PRINT "Old string: ",STR$ result = REPLACEWORD(str, c, d) RESULT$ = (char*)result PRINT "New String: ",RESULT$ free(result)
|
|
|
Post by bigbass on Dec 11, 2018 16:21:14 GMT 1
baconized the code more (clang gives a much better error report to go by)
and found a new way to do it and get the bacon$ working better with c code
declare as a char ARRAY with a value
DECLARE some_str TYPE char ARRAY 2 some_str = "xxforxx"
SOME_STRING$ = some_str PRINT "Old string: " ,SOME_STRING$
fixed a problem when comparing strings with the ADDRESS() command
'---C program to search and replace '---all occurrences of a word with another word.
'--- port from geeks to geeks to BaCon by bigbass
'PRAGMA COMPILER clang PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith OPTION PARSE FALSE
ALIAS strcpy TO STRING_COPY ALIAS strstr TO STRING_COMPARE '---Function to replace a string with another string '====================================================================== FUNCTION REPLACEWORD(STRING s, STRING oldW, STRING newW) TYPE STRING '====================================================================== LOCAL result TYPE STRING LOCAL i TYPE int LOCAL cnt TYPE int cnt = 0 i = 0 LOCAL newWlen TYPE int LOCAL oldWlen TYPE int newWlen = LEN(newW) oldWlen = LEN(oldW) '--- Counting the number of times old word '--- occurs in the string WHILE (s[i] ISNOT '\0') IF (STRING_COMPARE((STRING)ADDRESS(s[i]), oldW) EQ (STRING)ADDRESS(s[i])) THEN INCR cnt '--- Jumping to index after the old word. i = i + (oldWlen - 1) END IF INCR i WEND '--- Making new string of enough length result = MEMORY(i + cnt * (newWlen - oldWlen) + 1) i = 0 WHILE (*s) '--- compare the substring with the result IF (STRING_COMPARE(s, oldW) EQ s) THEN STRING_COPY((STRING)ADDRESS(result[i]), newW) i = i + newWlen s = s + oldWlen ELSE result[i++] = *s++ END IF WEND result[i] = '\0' RETURN result END FUNCTION '--- main Program DECLARE some_str TYPE char ARRAY 2 DECLARE c TYPE char ARRAY 2 DECLARE d TYPE char ARRAY 2 DECLARE result TYPE STRING
some_str = "xxforxx" c = "xx" d = "Geeks"
result = '\0' '--- oldW string SOME_STRING$ = some_str PRINT "Old string: " ,SOME_STRING$ result = REPLACEWORD(some_str, c, d) RESULT$ = result PRINT "New String: ",RESULT$ FREE result
|
|
|
Post by bigbass on Dec 14, 2018 5:53:37 GMT 1
Hey guys
here is a homemade macro to compare strings that used a c function strcmp inside the bacon macro its all bacon code now
and it shows different ways to use it we can add many more uses I try to post snippets that are stand alone examples as a reference demo
Joe
'--- made a BaCon Macro called STRING_CMP that uses the real strcmp '--- and will show several different ways to use the macro
'--- needed for the strcmp function in c OPTION PARSE FALSE
'---(1) DECLARE a1 TYPE STRING DECLARE a2 TYPE STRING a1= "hot" a2= "hot"
'---(2) DECLARE a3 TYPE char ARRAY 5 DECLARE a4 TYPE char ARRAY 5
a3 = "cold" a4 = "cood"
'---if the strings are equal 0 else 1 DEF FN STRING_CMP(s1,s2) = (strcmp(s1,s2) ? 1 : 0)
RESULT = STRING_CMP("hello","helo")
'---(3) INLINE PRINT IIF$(RESULT == 0, "They are the same ", "They are different ")
'---(4) using a common string PRINT STRING_CMP("Bacon","BaCon")
'---(5) TYPE STRING PRINT STRING_CMP(a1,a2)
'---(6) TYPE char ARRAY 5 PRINT STRING_CMP(a3,a4)
'---(7) COMPLEX and we need the address of the char array '--- but we don't need a cast to char* with this syntax complex1$ = ADDRESS(a3[0]) complex2$ = &a4[0] PRINT complex1$ PRINT complex2$
'---(8) PRINT STRING_CMP(&a3[0],&a4[0])
|
|
|
Post by bigbass on Dec 14, 2018 7:43:27 GMT 1
simulating pipes and how they really work note when compiled in clang it detected that I needed a free for my_string gcc didnt warn me '---How to simulate pipes in BaCon '---one command line example will be used for the demo '---env | grep "LANG" '---(1st command output) gets piped into the (2nd command )
OPTION PARSE FALSE
DECLARE env_pipe TYPE FILE* DECLARE grep_pipe TYPE FILE*
DECLARE bytes_read TYPE int DECLARE nbytes TYPE size_t nbytes = 100 DECLARE my_string TYPE STRING PRINT PRINT "We are going to simulate ", "env | grep \"LANG\" " PRINT "=================================================="
'--- Open our two pipes r read w write env_pipe = popen ("env", "r") '--- this is the pipe without the pipe character "|" needed grep_pipe = popen ("grep LANG", "w")
'--- Check that pipes are non-null, therefore open IF NOT(env_pipe) OR NOT(grep_pipe) THEN PRINT "One or both pipes failed." END IF
'--- Read from env_pipe until two newlines my_string = (STRING) MEMORY (nbytes + 1) bytes_read = getdelim (&my_string, &nbytes, 0, env_pipe)
'---Close env_pipe, checking for errors IF (pclose (env_pipe) ISNOT 0) THEN PRINT "Could not run 'env', or other error." END IF
'---Send output of 'env' to 'grep LANG', with two newlines fprintf (grep_pipe, "%s\n\n", my_string)
'---Close grep_pipe, checking for errors IF (pclose (grep_pipe) ISNOT 0) THEN PRINT "Could not run 'grep', or other error." END IF
FREE my_string
|
|
|
Post by bigbass on Dec 16, 2018 20:22:27 GMT 1
Hello vovchik you and I are probably the only ones to use this but I will post it anyway I found a more advanced terminal example from here reference postI got it to compile and did some small edits for the RPI there is a read me its pure c++ and I want to test it out before adding it to another app where I use a simple terminal at the moment there's a read me to get it set up quickly with a link to the original source Joe fltk-adv-terminal.tar.gz (4.71 KB)
|
|
|
Post by vovchik on Dec 16, 2018 22:22:38 GMT 1
Dear Joe, Thanks. It looks nice. I used the following command line for compilation: g++ unix-bidir-dumb-terminal-fixed.cxx -o termtest -Wall -v -lfltk -lXext -lX11 -lm -lpthread The code could be massaged a bit - I am not sure whether everybody needs to install tcsh, but the terminal is not too bad. With kind regards, vovchik
|
|
|
Post by bigbass on Dec 17, 2018 4:56:42 GMT 1
hello vovchik but the terminal is not too bad most important is it ran for you we can clean up the unneeded code nice that it will do python too as a demo I got a few compiler warnings but can live with it it will eventually replace the simple terminal after more testing thanks for testing not something you want to port directly just getting it to run is fine with me Joe
|
|
|
Post by bigbass on Feb 27, 2019 19:40:47 GMT 1
Hello everyone just wanted to show two things here one how to use the new CLASS command vovchik and I both have got around this without the having new CLASS command but I will show how we can use it with the latest bacon beta because now that it is official we can use it another way still we will use an official C++ demo for fltk seriss.com/people/erco/fltk/#Fl_Menu_Bar_Single_Callbackto show all the steps involved after porting it with the new CLASS/ENDCLASS it would look like this
PRAGMA LDFLAGS -lfltk -L. PRAGMA LDFLAGS -lbacon++ PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-permissive OPTION PARSE FALSE
'--- Show the use of item_pathname() in a custom class. PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <string.h> PRAGMA INCLUDE <stdlib.h> PRAGMA INCLUDE <FL/Fl.H> PRAGMA INCLUDE <FL/Fl_Window.H> PRAGMA INCLUDE <FL/Fl_Menu_Bar.H>
CLASS MyApp Fl_Window *win; Fl_Menu_Bar *menubar;
// Static menu callback static void Menu_CB(Fl_Widget*w, void*data) { MyApp *o = (MyApp*)data; o->Menu_CB2(); }
// Callback method with class access void Menu_CB2() { char picked[80]; menubar->item_pathname(picked, sizeof(picked)-1); printf("CALLBACK: You picked '%s'\n", picked);
// How to handle callbacks.. if ( strcmp(picked, "File/Quit") == 0 ) exit(0); if ( strcmp(picked, "Help/Help") == 0 ) printf("Help goes here\n"); }
public: MyApp() { // Make the app window and menu bar w/callbacks win = new Fl_Window(720, 486); menubar = new Fl_Menu_Bar(0, 0, win->w(), 25); menubar->add("File/Open", 0, Menu_CB, (void*)this); // userdata is always 'this' menubar->add("File/Quit", 0, Menu_CB, (void*)this); menubar->add("Help/Help", 0, Menu_CB, (void*)this); win->end(); win->show(); } };ENDCLASS
'// MAIN
DECLARE app TYPE MyApp app Fl::run()
but this demo code is too complex still and it could be done a lot easier like this we don't even need the custom class in the demo to get the same results! and reduce it down even more I left the printf there so its easy to compare to the source code '--- ported and made easier by bigbass
PRAGMA LDFLAGS -lfltk -L. PRAGMA LDFLAGS -lbacon++ PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-permissive OPTION PARSE FALSE
'--- Show the use of item_pathname() without a custom class needed . PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <string.h> PRAGMA INCLUDE <stdlib.h> PRAGMA INCLUDE <FL/Fl.H> PRAGMA INCLUDE <FL/Fl_Window.H> PRAGMA INCLUDE <FL/Fl_Menu_Bar.H>
'--- simple Callback method without a class needed SUB Menu_CB(Fl_Widget* dummy, void* data) LOCAL picked[80] TYPE char menubar->item_pathname(picked, sizeof(picked)-1) printf("CALLBACK: You picked '%s'\n", picked)
'--- How to handle callbacks.. IF ( strcmp(picked, "File/Quit") == 0 ) THEN exit(0) IF ( strcmp(picked, "Help/Help") == 0 ) THEN printf("Help goes here\n") END SUB
'--- MAIN DECLARE win TYPE Fl_Window* DECLARE menubar TYPE Fl_Menu_Bar* '// Make the app window and menu bar w/callbacks win = new Fl_Window(720, 486) menubar = new Fl_Menu_Bar(0, 0, win->w(), 25) menubar->add("File/Open", 0, Menu_CB) menubar->add("File/Quit", 0, Menu_CB) menubar->add("Help/Help", 0, Menu_CB) win->end() win->show() Fl::run()
|
|
|
Post by alexfish on Feb 27, 2019 20:26:10 GMT 1
Hi Joe
not sure what you mean by the ugly
so not sure if these type of macro functions will help
#define MenuBarAdd menubar->add #define Fl_Menu_Bar new Fl_Menu_Bar #define CALLBACK (void*) #define Fl_Window new Fl_Window can test using bacon 'DEF FN'
at the foot can test this SHOW macro
USEH #define SHOW win->end() ; win->show() ; Fl::run(); ENDUSEH
'// Make the app window and menu bar w/callbacks win = new Fl_Window(720, 486) menubar = new Fl_Menu_Bar(0, 0, win->w(), 25) menubar->add("File/Open", 0, Menu_CB) menubar->add("File/Quit", 0, Menu_CB) menubar->add("Help/Help", 0, Menu_CB) SHOW
In the demo of CLASS
CLASS MyApp { I get this
Compiling 'fltk2.bac'... g++ -Wno-write-strings -Wno-pointer-arith -Wno-permissive -c fltk2.bac.c Makefile.bacon:6: recipe for target 'fltk2.bac.o' failed Compiler error:
Description: file 'fltk2.bac' line 59: undefined syntax error, hint: check for missing ENDIF, WEND, NEXT. Cause: expected unqualified-id before '{' token Alex
|
|