|
Post by rikky on Oct 27, 2020 8:12:27 GMT 1
So, now that the ARGUMENT$ can be split out properly, The whole project becomes easy.
Suddenly all the difficult workaround sh*t that never worked can be thrown overboard.
A teaser for AMOUNT.bac :
OPTION EXPLICIT TRUE
GLOBAL version$ version$ = "0.1"
DECLARE argument$ ASSOC STRING GLOBAL basename$ = BASENAME$(TOKEN$(ARGUMENT$,1))
SUB HELP
PRINT PRINT basename$ & " [option] string$ [delimiter$])" PRINT PRINT "string$ can be replaced with piped input." PRINT PRINT "Returns the amount of tokens in a string split by delimiter$. The delimiter$ is optional. If it is omitted, then environment variable DELIM is used." PRINT "IF DELIM is empty, then it defaults to <space>." PRINT "When specified, it may consist of multiple characters. If delimiter$ occurs between double quotes in string$ then it is ignored." PRINT "This behaviour can be changed by setting the environment variable QUOTED to FALSE." PRINT PRINT "options :" PRINT " -v --version print version." PRINT " -h --help print help." PRINT PRINT "example : (count the newlines)" PRINT PRINT "blabla=" & CHR$(34) & "This is a single quote: '" PRINT "This is a double quote: " & CHR$(34) & CHR$(39) & CHR$(34) & CHR$(39) PRINT "export DELIM=" & CHR$(34) PRINT CHR$(34) PRINT "echo " & CHR$(34) & "$blabla" & CHR$(34) & " | " & basename$ PRINT END SUB
FUNCTION GET_STDIN$() LOCAL input_string$ TYPE STRING LOCAL stndin WHILE TRUE stndin = WAIT(STDIN_FILENO, 50) IF stndin = 0 THEN BREAK input_string$ = input_string$ & CHR$(stndin) WEND RETURN input_string$ END FUNCTION SUB SELECT_ARGUMENT
LOCAL i LOCAL option$ LOCAL counter = 0 FOR i = 2 TO AMOUNT(ARGUMENT$) option$ = FLATTEN$(TOKEN$(ARGUMENT$,i)) SELECT option$ CASE "-v"; CASE "--version" PRINT basename$ & " version : " & version$ END 0 CASE "--help"; CASE "-h" HELP END 0 DEFAULT INCR counter argument$(STR$(counter)) = option$ END SELECT NEXT i END SUB
SELECT_ARGUMENT
GLOBAL input$ GLOBAL delimiter$ GLOBAL quoted$ = ""
'this one AFTER figuring out the ARGUMENT$ quoted$ = GETENVIRON$("QUOTED") IF quoted$ = "FALSE" OR quoted$ = "false" OR quoted$ = "0" THEN OPTION QUOTED FALSE
'AMOUNT string$ [delimiter$]"
input$ = GET_STDIN$()
IF LEN(input$) = 0 THEN input$ = argument$("1") delimiter$ = argument$("2") ELSE input$ = RIP$(input$,-1) delimiter$ = argument$("1") END IF
IF delimiter$ = "" THEN delimiter$ = GETENVIRON$("DELIM") IF delimiter$ = "" THEN delimiter$ = " "
PRINT AMOUNT(input$,delimiter$)
|
|
|
Post by rikky on Oct 30, 2020 19:43:42 GMT 1
next teaser: EXTRACT [option] x$ y$ [flag] OPTION EXPLICIT TRUE OPTION UTF8 TRUE
GLOBAL version$ version$ = "0.4"
DECLARE argument$ ASSOC STRING GLOBAL basename$ = BASENAME$(TOKEN$(ARGUMENT$,1)) GLOBAL verbose = 0 GLOBAL retval = 0 GLOBAL ERROR$ = "" GLOBAL input$
SUB HELP LOCAL help$ help$ = "Returns the string defined in <x$> from which the string mentioned in <y$> has been removed. The optional flag set to TRUE determines if the <y$> should be taken as a regular expression where the environment variable COMPARE set to TRUE establishes case insensitive expression matching. See also REPLACE." PRINT PRINT basename$ & " [option] x$ y$ [flag]" PRINT PRINT "x$ can be replaced with piped input." PRINT PRINT ALIGN$(help$,COLUMNS,0) PRINT PRINT "options :" PRINT " -v --version print version." PRINT " -h --help print help." PRINT PRINT "example:" PRINT PRINT "EXTRACT " & CHR$(34) & "bacon program" & CHR$(34) & " ra" PRINT "EXTRACT " & CHR$(34) & "$blabla" & CHR$(34) & " e" PRINT "EXTRACT " & CHR$(34) & "a.b.c" & CHR$(34) & " .* TRUE" PRINT "cat file | EXTRACT .* TRUE" PRINT PRINT "export COMPARE=TRUE" : ' this is set automatically PRINT "blabla=" & CHR$(34) & "a b c" & CHR$(34) PRINT "EXTRACT " & CHR$(34) & "$blabla" & CHR$(34) & " B TRUE" PRINT END SUB
FUNCTION GET_STDIN$() LOCAL input_string$ TYPE STRING LOCAL stndin WHILE TRUE stndin = WAIT(STDIN_FILENO, 50) IF stndin = 0 THEN BREAK input_string$ = input_string$ & CHR$(stndin) WEND RETURN input_string$ END FUNCTION
SUB THE_COMPUTER_SAYS_NO(lineno) IF ERROR$ <> "" THEN EPRINT ERROR$ EPRINT basename$ & ": The computer says no, in line " & STR$(lineno) IF retval THEN END retval END 1
END SUB
SUB SELECT_ARGUMENT
LOCAL i LOCAL option$ LOCAL counter = 0 FOR i = 2 TO AMOUNT(ARGUMENT$) option$ = FLATTEN$(TOKEN$(ARGUMENT$,i)) SELECT option$ CASE "-v"; CASE "--version" PRINT basename$ & " version : " & version$ END 0 CASE "--help"; CASE "-h" HELP END 0 CASE "--verbose" INCR verbose DEFAULT INCR counter argument$(STR$(counter)) = option$ END SELECT NEXT i END SUB
FUNCTION ISNUMBER(string$)
'This function returns TRUE if the string$ is a numeric value. LOCAL char$ LOCAL i string$ = CHOP$(string$) IF LEFT$(string$,1) = "+" OR LEFT$(string$,1) = "-" THEN string$ = RIP$(string$,1,1) END IF 'we might have one point in it. IF INSTR(string$,".") THEN string$ = RIP$(string$,INSTR(string$,"."),1) ENDIF IF INSTR(string$,".") THEN RETURN 0 IF LEN(string$) = 0 THEN RETURN 0 FOR i = 1 TO LEN(string$) char$ = MID$(string$,i,1) IF VAL(char$) = 0 THEN IF char$ <> "0" THEN RETURN 0 END IF END IF NEXT i
RETURN 1 : 'VAL(string$) gives wrong value for zero. END FUNCTION
SELECT_ARGUMENT
'EXTRACT [option] x$ y$ [flag]
GLOBAL y$ GLOBAL flag$ GLOBAL compare$
compare$ = GETENVIRON$("COMPARE") IF compare$ = "TRUE" OR compare$ = "true" OR compare$ = "1" THEN compare$ = "TRUE" IF verbose THEN PRINT "OPTION COMPARE TRUE" OPTION COMPARE TRUE END IF
input$ = GET_STDIN$()
IF LEN(input$) = 0 THEN input$ = argument$("1") y$ = argument$("2") flag$ = argument$("3") ELSE input$ = RIP$(input$,-1) y$ = argument$("1") flag$ = argument$("2") END IF
IF input$ = "" THEN END 0
IF y$ = "" THEN END 0
IF flag$ = "" THEN IF compare$ = "TRUE" THEN flag$ = "1" : ' OPTION COMPARE only works with regular expressions so the flag has to be set as default. END IF END IF
IF flag$ = "TRUE" OR flag$ = "true" OR flag$ = "1" THEN flag$ = "1" ELSE flag$ = "0" END IF
IF verbose THEN PRINT "EXTRACT$(" & input$ & "," & y$ & "," & flag$ & ")"
PRINT EXTRACT$(input$, y$, VAL(flag$))
Rik EDIT: removed remark in the help$.
|
|
|
Post by rikky on Nov 3, 2020 17:49:51 GMT 1
I guess that's why they call it teaser.
Next teaser is too simple, ISASCII.
But it is perfect to have laying around between your standard set of bash tools.
OPTION EXPLICIT TRUE OPTION UTF8 TRUE
GLOBAL version$ version$ = "0.5"
DECLARE argument$ ASSOC STRING GLOBAL basename$ = BASENAME$(TOKEN$(ARGUMENT$,1)) GLOBAL input$
SUB HELP LOCAL help$ help$ = "Returns TRUE (1) if <string$> only contains ASCII data. If not, FALSE (0) is returned. See also TOASCII" PRINT PRINT basename$ & " [option] string$" PRINT PRINT ALIGN$(help$,COLUMNS,0) PRINT PRINT "options :" PRINT " -v --version print version." PRINT " -h --help print help." PRINT PRINT "example:" PRINT PRINT "ISASCII " & CHR$(34) & "hello world" & CHR$(34) PRINT "echo hello world | ISASCII" PRINT END SUB
FUNCTION GET_STDIN$() LOCAL input_string$ TYPE STRING LOCAL stndin WHILE TRUE stndin = WAIT(STDIN_FILENO, 50) IF stndin = 0 THEN BREAK input_string$ = input_string$ & CHR$(stndin) WEND RETURN input_string$ END FUNCTION
SUB SELECT_ARGUMENT
LOCAL i LOCAL option$ LOCAL counter = 0 FOR i = 2 TO AMOUNT(ARGUMENT$) option$ = FLATTEN$(TOKEN$(ARGUMENT$,i)) SELECT option$ CASE "-v"; CASE "--version" PRINT basename$ & " version : " & version$ END 0 CASE "--help"; CASE "-h" HELP END 0 DEFAULT INCR counter argument$(STR$(counter)) = option$ END SELECT NEXT i END SUB
SELECT_ARGUMENT
input$ = GET_STDIN$()
IF LEN(input$) = 0 THEN input$ = argument$("1") ELSE input$ = RIP$(input$,-1) END IF
IF ISASCII(input$) THEN PRINT "1" END 1 ELSE PRINT "0" END 0 END IF
|
|
|
Post by vovchik on Nov 3, 2020 20:49:39 GMT 1
Dear Rik, Thanks for these examples. I will have to take a close look and see what I can do to incorporate those bits in bish if they are missing... With kind regards, vovchik Attachments:bish-src.tar.gz (44.89 KB)
|
|
|
Post by rikky on Nov 4, 2020 8:47:24 GMT 1
Ah ja, I kept track of bish in the beginning, but then lost it somewhere, due to having not so much time and busy with other things. I guess it is also a matter of taste. I just like the one-thing commands more. technically I could write a command like EXCHANGE.bac like: SYSTEM "bish exchange " & DEL$(ARGUMENT$,1) or something. Except that your bish has not yet taken the new ARGUMENT$ into account. My: EXCHANGE 'a b c d "e f" g h i j' 8 5 gives: a b c d i g h "e f" j
Which is the correct answer While yours: bish exchange 'a b c d "e f" g h i j' 8 5 gives: a b c d h f" g "e i j Rik. EDIT: export QUOTED=false EXCHANGE 'a b c d "e f" g h i j' 8 5 gives: a b c d h f" g "e i j like yours. ;°) Attachments:EXCHANGE.bac (4.08 KB)
|
|
|
Post by rikky on Nov 8, 2020 7:37:15 GMT 1
Hola Vovchik, Looking again at your bish program, I see that you did solve the ARGUMENT$ long before Peter solved it. DECLARE var$[10] TYPE STRING DECLARE i TYPE int
FOR i = 0 TO argc - 1 var$[i] = (char*)argv[i] PRINT STR$(i) & TAB$(1) & var$[i] NEXT i Which was also mentioned hereThing is that if I see something like (char*)argv, I tend to skip it, for being too difficult. So I think I owe you an apology. Sorry. Rik.
|
|
|
Post by rikky on Nov 10, 2020 16:08:41 GMT 1
So I decided to do the whole project again, this time by hand, without a script. Hopeful to find some new bugs in the process. But, due to time restrictions, and other things that come in between, that demand my attention, I am now at the 'R' of ROTATE. And I'm getting bored. It's stupid work. So I made this short script that Installs the link(s) to the bish command(s) in <folder>, unless something with the same name is already there. It is supposed that bish is in your $PATH INSTALL [bish_command] [TO <folder>] or INSTALL --help version$ = "0.1" DECLARE argument$ ASSOC STRING GLOBAL basename$ = BASENAME$(TOKEN$(ARGUMENT$,1)) GLOBAL verbose = 0 GLOBAL var GLOBAL destiny$ GLOBAL bish_executables$ = "" & \ "abs acos align amount append asc asctab asin atn atn2 " & \ "b64dec b64dec_file b64enc b64enc_file basename bin2dec " & \ "bitwise bmi bytelen ceil change changedir chop chr clear " & \ "clip_copy clip_paste comp_num comp_str concat copy cos " & \ "count_asc count_ucs cr crunch curdir cut datestamp day dec " & \ "dec2bin decr deg del delete delim dirname dos2unix el " & \ "eprint equal eval even excerpt exchange exec exp explode " & \ "extract fappend fileexists filelen filetime filetype fill " & \ "first flatten floor getenviron getkey hasdelim hash head " & \ "help hex host hostname html2text html_decode html_scrub " & \ "html_strip hour inbetween incr input insert instr instrrev " & \ "int isascii istoken isutf8 last lcase lcut left len lgrep " & \ "load log makedir match match_wc max_num max_str merge " & \ "meteo_feelslike meteo_humidex meteo_windchill mid midset " & \ "min_num min_str minute mod month month_str nl now obscure " & \ "odd os outbetween pad parse_url passwd pi pluckout pow " & \ "print printf proper purge rad random range realpath regex " & \ "rename replace rev reverse right rip rm_extra rnd rol ror " & \ "rot13 rotate round rounded save second setenviron sgn sin " & \ "sleep sort spc spc_rm sqr string tab tail tally tan " & \ "time_utc timestamp toascii token ucase ucs ulen unflatten " & \ "uniq units unix2dos using utf8 utf8_block walk wc weather " & \ "week weekday wget wget_url wildcard xor year"
SUB HELP LOCAL help$ help$ = "Installs link(s) to the bish_commands in <folder>, unless something with the same name is already there." PRINT PRINT basename$ & " [option] [bish_command] [TO <folder>]" PRINT PRINT ALIGN$(help$,COLUMNS,0) PRINT PRINT "options :" PRINT " -v --version print version." PRINT " -h --help print help." PRINT PRINT "example:" PRINT PRINT "Install all symlinks in current directory:" PRINT "INSTAL" PRINT END SUB
SUB SELECT_ARGUMENT
LOCAL i LOCAL option$ LOCAL counter = 0 FOR i = 2 TO AMOUNT(ARGUMENT$) option$ = FLATTEN$(TOKEN$(ARGUMENT$,i)) SELECT option$ CASE "-v"; CASE "--version" PRINT basename$ & " version : " & version$ END 0 CASE "--help"; CASE "-h" HELP END 0 CASE "--verbose" INCR verbose CASE "TO" INCR i destiny$ = TOKEN$(ARGUMENT$,i) DEFAULT INCR counter argument$(STR$(counter)) = option$ END SELECT NEXT i END SUB
SELECT_ARGUMENT
IF NRKEYS(argument$) = 0 THEN 'all possible executables will be done. FOR var = 1 TO AMOUNT(bish_executables$) argument$(STR$(var)) = TOKEN$(bish_executables$,var) NEXT var END IF
IF destiny$ <> "" THEN IF verbose THEN PRINT "destiny: " & destiny$ MAKEDIR destiny$ CHANGEDIR destiny$ END IF IF verbose THEN FOR var = 1 TO NRKEYS(argument$) PRINT "argument " & STR$(var) & ": " & argument$(STR$(var)) NEXT var END IF
'first make the executable GLOBAL executable$ = CHOP$(EXEC$("mktemp")) GLOBAL line$ = "SYSTEM " & CHR$(34) & "bish " & CHR$(34) & " & LCASE$(BASENAME$(TOKEN$(ARGUMENT$,1))) & " & CHR$(34) & " " & CHR$(34) & " & DEL$(ARGUMENT$,1)" SAVE line$ TO executable$ & ".bac" SYSTEM "bacon " & executable$ & ".bac"
FOR var = 1 TO NRKEYS(argument$) IF verbose THEN PRINT IF verbose THEN PRINT "argument " & STR$(var) & ": " & argument$(STR$(var)) IF NOT(FILEEXISTS(UCASE$(argument$(STR$(var))))) THEN IF ISTOKEN(bish_executables$,argument$(STR$(var))) THEN COPY executable$ TO UCASE$(argument$(STR$(var))) ELSE EPRINT executable$ & " is not a bish token." END IF ELSE EPRINT UCASE$(argument$(STR$(var))) & " already exists" END IF NEXT var
'aij BaCon COPY doesnt copy the permissions. SYSTEM "sudo chmod +x *"
SYSTEM "rm " & executable$ & "*"
So, hmm, lets test it. ROTATE "Rome Amsterdam Kiev Bern Paris London" 2 gives : Paris London Rome Amsterdam Kiev Bern .... Toppie. echo Rome Amsterdam Kiev Bern Paris London | ROTATE 2 gives : Rome Amsterdam Kiev Bern Paris London .... hmm. blabla='Rome "Amsterdam Kiev Bern" Paris London' ROTATE "$blabla" 2 gives: Paris London Rome "Amsterdam Kiev Bern" .... perfect. extra: ROTATE.bac : OPTION EXPLICIT TRUE OPTION UTF8 TRUE OPTION ERROR FALSE
GLOBAL version$ version$ = "0.7"
DECLARE argument$ ASSOC STRING GLOBAL basename$ = BASENAME$(TOKEN$(ARGUMENT$,1)) GLOBAL verbose = 0 GLOBAL input$ GLOBAL var GLOBAL quoted$
CATCH ERROR help
SUB help(function_name$, filename$, lineno) PRINT ERR$(ERROR), " in function ", function_name$, " in file '", filename$, "' at line ", lineno END 123 END SUB
SUB HELP LOCAL help$ help$ = "Rotates all elements in a delimited string split by delimiter$ <step (default=1)> positions forward. In case the <step> parameter is a negative number, the rotation will be backwards. The delimiter$ is optional. If it is omitted, then the definition from OPTION DELIM is assumed. When specified, it may consist of multiple characters." PRINT PRINT basename$ & " [option] string$ [step] [delimiter$]" PRINT PRINT "string$ can be replaced with piped input." PRINT PRINT ALIGN$(help$,COLUMNS,0) PRINT PRINT "options :" PRINT " -v --version print version." PRINT " -h --help print help." PRINT PRINT "example:" PRINT PRINT "echo Rome Amsterdam Kiev Bern Paris London | ROTATE 2" PRINT END SUB
FUNCTION GET_STDIN$() LOCAL input_string$ TYPE STRING LOCAL stndin WHILE TRUE stndin = WAIT(STDIN_FILENO, 50) IF stndin = 0 THEN BREAK input_string$ = input_string$ & CHR$(stndin) WEND RETURN input_string$ END FUNCTION
SUB SELECT_ARGUMENT
LOCAL i LOCAL option$ LOCAL counter = 0 FOR i = 2 TO AMOUNT(ARGUMENT$) option$ = FLATTEN$(TOKEN$(ARGUMENT$,i)) SELECT option$ CASE "-v"; CASE "--version" PRINT basename$ & " version : " & version$ END 0 CASE "--help"; CASE "-h" HELP END 0 CASE "--verbose" INCR verbose DEFAULT INCR counter argument$(STR$(counter)) = option$ END SELECT NEXT i END SUB
FUNCTION ISNUMBER(string$)
'This function returns TRUE if the string$ is a numeric value. LOCAL char$ LOCAL i string$ = CHOP$(string$) 'IF LEFT$(string$,1) = "+" OR LEFT$(string$,1) = "-" THEN ' string$ = RIP$(string$,1,1) 'END IF 'we might have one point in it. 'IF INSTR(string$,".") THEN ' string$ = RIP$(string$,INSTR(string$,"."),1) 'ENDIF 'IF INSTR(string$,".") THEN RETURN 0 IF LEN(string$) = 0 THEN RETURN 0 FOR i = 1 TO LEN(string$) char$ = MID$(string$,i,1) IF VAL(char$) = 0 THEN IF char$ <> "0" THEN RETURN 0 END IF END IF NEXT i
RETURN 1 : 'VAL(string$) gives wrong value for zero. END FUNCTION
SUB SHUFF
'shifts the argument array one up LOCAL i FOR i = 1 TO NRKEYS(argument$) argument$(STR$(i)) = argument$(STR$(i+1)) NEXT i FREE argument$(STR$(NRKEYS(argument$))) END SUB
SELECT_ARGUMENT
'this one AFTER figuring out the ARGUMENT$ quoted$ = GETENVIRON$("QUOTED") IF quoted$ = "FALSE" OR quoted$ = "false" OR quoted$ = "0" THEN OPTION QUOTED FALSE
input$ = GET_STDIN$()
IF LEN(input$) = 0 THEN input$ = argument$("1") SHUFF ELSE input$ = RIP$(input$,-1) END IF
'ROTATE [option] string$, step [,delimiter$]
GLOBAL step$ = argument$("1") GLOBAL delimiter$ = argument$("2")
IF delimiter$ = "" THEN delimiter$ = GETENVIRON$("DELIM") IF delimiter$ = "" THEN delimiter$ = " "
IF step$ = "" THEN step$ = "1"
IF NOT(ISNUMBER(step$)) THEN step$ = "1"
IF verbose THEN PRINT "ROTATE$(" & input$ & "," & step$& "," & delimiter$ & ")"
PRINT ROTATE$(input$,VAL(step$),delimiter$)
Rik.
|
|
|
Post by vovchik on Nov 10, 2020 22:00:55 GMT 1
Dear Rik,
Thanks for all your work and code. I will have a careful look soon.
With kind regards, vovchik
|
|
|
Post by rikky on Nov 14, 2020 9:36:50 GMT 1
Okee; I have them ready. Except the ones that deal with regex. For those you can use bish, or the INSTALL program from two posts ago. i.e. INSTALL walk There might be some glitches in de docu's obtainable with COMMANDNAME --help and so. But they should work. Every argument given to the commands should be quoted the (ba)sh way. So "this is one argument" is taken as 1 argument. Rik. Attachments:custom.tgz (31.45 KB)
|
|
|
Post by vovchik on Nov 14, 2020 15:58:19 GMT 1
Dear Rik, Thanks. Whether it's via my bish or your b4d it is very nice to have all those commands available for scripting. With kind regards, vovchik
|
|