BRP(1) | General Commands Manual | BRP(1) |
brp - line oriented universal preprocesser using posix-shell
brp [-hHVQsS]
~$ brp -s | tee src.sh.c #>> sample ~$ sh src.sh.c -a #>> output 'hw' ~$ sh src.sh.c -a -b -a #>> hw, exec ls, hw
brp is portable preprocesser. Code is separated with 2 parts,
main code(#inc...#END) and option code (#SH_OP ...):
---src.xx.yy (needs 2 or more ext) #inc... C='^[/][/*]SH_' ; ... ...#END <<< brp main code (your text/src/others) //SH_OP a echo 'hello' <<< brp option code //SH_OP b buf=$(ls|wc -l); echo "$buf" --- ~$ sh src.xx.yy -a #>> run: echo 'hello' ~$ sh src.xx.yy -ba #>> run: ls, echo 'hello'
if you exec 'src.xx.yy' as shell code, main/option code run as:
1: exec main code (top 10 lines) and ignore the left 2: main code search/grep/regist the option code oneliner 3: exec oneliner if args exist: eg) '-ab' >> run 'a' then 'b'
brp exec the head 10 line code as shell script and exit.
linetop '#include...' is assert() for c/cpp (~$ cc src.sh.c #>>stop) so
you can delete if you want.
main code holds regex expression var C to get option code line. this
var is used as:
~$ cat src.sh.c | sed -ne "/$C/p" #BRE-regex
and gather //SH_OP or //SH_??? etc.
main code holds predefined option code, -w, -m. see below for details.
you can expand the brp working with option code.
//SH_OP b: z=100;echo "good-bye $2 $Ob $1 $z $R0" 1 2 3 4 5
Cb=$(cat<<'E' z=100;echo "good-bye $2 $Ob $1 $z $R0" E ) eval "$Cb" #if exec: ~$ src.sh.c -b
//SH_OP _ echo "preset AA";AA=1 //SH_OP a echo "$AA" #>>~$ sh src.sh.c -a ... disp "1"
//SH_OP a echo "hw" #>> ignored. sh src.sh.c -a -> disp "gw" //SH_OP a echo "gw"
1-2char var names R?, C?, O? are reserved (R,C,O,Ra,R7,Cg,O5 ...).
optarg is set to $O? ( a: >> $Oa , v: >> $Ov etc)
eg) //SH_OP a: echo "$Oa" #>> ~$ sh src.sh.c -a1 -a 5
#>> disp 1, 5
//SH_OP a echo "$Rm" # ~$ sh brp.sh.c -a >> disp brp.tmp.c //SH_OP b printf "$Ca" # ~$ sh brp.sh.c -b >> disp echo "$Rm" //SH_OP c eval "$Ca" #.. -c works as equals to -a
~$ brp -S > src.sh.c #sample ~$ sh src.sh.c -w 1. write SH_LS - SH_ED(LS_block) to both src.h($Rh)/src.c($Rs) 2. add HD_block to src.h, SC_block to src.c 3. disp filename to stdout. (src.h src.c) -. suffixes(LS,HD,SC,ED) are fixed
shell : C='^#ANYSTR_'; >> #ANYSTR_LS, #ANYSTR_ED etc python: C='^["]["]["]MARKER_'; >> """MARKER_OP etc basic : C="^[']SH_"; >> 'SH_OP etc
$C is used as follows. escape slash '/' plz.
sed -e "/${C}ED/" .. (bad)C='^/[/*]SH_' (good)C='^\/[/*]SH_'
--- copy & paste main script (~$ brp -Q) --- #include <iostream> int main(void){ std::cout << "hw" << std::endl; } //SH_OP b eval "$Cm";g++ "$Rm"; ./a.out
...save as src.brp.cxx and run ~$ sh src.brp.cxx -b >>> hw.
this app frequency uses one liner. introduce some tips.
//SH_OP m sed -ne "/[E]ND/{n;b l};d;:l;p;n;b l"<"$R0">"$Rm";echo "$Rm" >> sed -ne '*see below*' < "$R0" > "$Rm" echo "$Rm" >> cat 'foo.sh.c' | sed -ne '...' > foo.tmp.c echo "foo.tmp.c"
sed command pseudocode is the follows:
------ sed -ne '/[E]ND/{n;b l};d;: l;p;n;b l' >> sed -n(o print. print only when requested) -e(xpression as script) if (line==/END/){ .../[E]ND/ n(ext read) ...n (if not -n opt, print nowline & readnext) goto label l ...b l (b=goto. b abc -> goto abc, needs space@posix) } del line (& read nextline & *goto top*) ...d (d cmd is hard worker) label l: ... : l (label. ':' + '1sp' + 'name') p(rint line) ... p n(extline read) ... n goto label l ... b l ...del lines until 1st hit 'END'. print all lines until EOF. -------
sed cmd is difficult but very powerful. Most requests can be solved by referring to the above.
sed cant use shortest match, but posix-sh is
possible(shotest+longest).
str="aa_bb_cc" echo "${str#*_}" #>> bb_cc (match aa_ and del) echo "${str##*_}" #>> cc (longet) echo "${str%_*}" #>> aa_bb (from tail) echo "${str%%_*}" #>> aa
shell pattern(glob pattern) is very similar to sed-regex:
aa_bb_cc -> a_bb_cc reg: s/^.//g sh : ${str#?} ... any one char. reg:'.' sh:'?' aa_bb_cc -> (del) reg: ^[.]* sh : ${str#*} ... all. sh can uses wild card. aa_bb_cc -> aa_ reg: [^a_]* sh : ${str%%[!a_]*} ... not. reg:'^' sh:'!' ...[], bracket works as same, 'one char' escape sh: ${str%123"*"*} ... "*" uses as literal. 0123*567 -> 0
see https://en.wikipedia.org/wiki/Glob_%28programming%29 ..or..
~$ man sh + input / + input pattern + enter + n +
shift_n
-
--- concept I wondered why to write dependencies or compile options to makefiles. The source code should contain all the necessary information. Because the programmer's will is written in it. I dont like writing in separate files and increasing the workflow. - avoid info fragmentation (script/src/header/gcc opt/ini/config etc) - small. avoid disturbing the main code. - (consider readability) - portable. avoid vender lockin, bashism etc. - low learning cost. good usage help, dont need installation etc - others ... see unix philosophy.
posix-shell
Copyright (C) 2020 Momi-g, AGPLv3+
2024-01-08 v1.0.5 (2020-01-04 v1.0.0)
https://en.wikipedia.org/wiki/Glob_%28programming%29
http://catb.org/%7Eesr/writings/taoup/html/ch10s05.html