Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/ash
- alias_file="$(realpath ~/cat_test_alias.in)"
- ALIAS_FNS="$(awk -v ALIASFILE="$alias_file" '
- function add_get_canonical_hdr(){
- print "function get_canonical_name(s){ "
- print " if (s in CANONICAL_ARY){ "
- print " return CANONICAL_ARY[s] "
- print " } else { "
- print " switch(s){ "
- }
- function add_get_canonical_footer(){
- print " } "
- print " return s "
- print "} "
- }
- function add_canonical_case(s,name){
- #TODO: Do this untill there is no astrixs left to replace.
- s=gensub(/^(.*[^.*])(\*)(.*)$/,"\\1.*\\3","G",s) # replace "*" with ".* "
- s=gensub(/^(.*[^\\+])(\+)(.*)$/,"\\1\\\\+\\3","G",s) # replace "+" with "\+"
- print " case /" s "/: "
- print " return \"" name "\" "
- }
- function write_CANONICAL_ARY_init_hdr(){
- print "init_CANONICAL_ARY(){ "
- }
- function write__init_CANONICAL_ARY(){
- print "init_CANONICAL_ARY(){ "
- for (p in CANONICAL_ARY){
- print " CANONICAL_ARY[\"" p "\"]=\"" CANONICAL_ARY[p] "\""
- }
- print "}"
- }
- #This writes the start of the function get_canonical_name()
- #this function will return a single name for all equivalent alias values.
- BEGIN { FS = ","; add_get_canonical_hdr()}
- #If wild cards are used in an alias get_canonical_name() uses regX case statments to do the match.
- #Otherwise get_cannonical_name() looks up the value in an associative array (AKA dictonary or hashmap)
- {
- canonical_NAME=$1
- sub(/\*/,"",canonical_NAME) #This isn'"'"'t necissary but makes the key slightly nicer looking
- for(i=NF;i>=1;i--){
- if(index($i,"*")>0){
- add_canonical_case($i,canonical_NAME) #alias has a wild card so wirite case statement to match alias
- }
- else{
- CANONICAL_ARY[$i]=canonical_NAME #alias has no wild card so add to lookup array)
- }
- }
- }
- END {add_get_canonical_footer()
- write__init_CANONICAL_ARY()
- } ' "$alias_file")"
- echo "$ALIAS_FNS"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement