SHARE
TWEET

build_cannonical

s243a Dec 11th, 2019 (edited) 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/ash
  2. alias_file="$(realpath ~/cat_test_alias.in)"
  3. ALIAS_FNS="$(awk -v  ALIASFILE="$alias_file" '
  4. function add_get_canonical_hdr(){
  5.  print "function get_canonical_name(s){                  "
  6.  print "  if (s in CANONICAL_ARY){                       "
  7.  print "   return CANONICAL_ARY[s]                       "
  8.  print "  } else {                                       "
  9.  print "    switch(s){                                   "
  10. }
  11. function add_get_canonical_footer(){
  12.  print "  }                                              "
  13.  print "  return s                                       "
  14.  print "}                                                "
  15. }
  16. function add_canonical_case(s,name){
  17.  
  18.  #TODO: Do this untill there is no astrixs left to replace.
  19.  s=gensub(/^(.*[^.*])(\*)(.*)$/,"\\1.*\\3","G",s) # replace "*" with ".* "
  20.  s=gensub(/^(.*[^\\+])(\+)(.*)$/,"\\1\\\\+\\3","G",s) # replace "+" with "\+"
  21.  print "      case /" s "/:                              "
  22.  print "        return \"" name "\"                          "
  23. }
  24. function write_CANONICAL_ARY_init_hdr(){
  25.  print "init_CANONICAL_ARY(){                            "
  26. }
  27. function write__init_CANONICAL_ARY(){
  28.  print "init_CANONICAL_ARY(){                            "
  29.  for (p in CANONICAL_ARY){
  30.    print "  CANONICAL_ARY[\"" p "\"]=\"" CANONICAL_ARY[p] "\""
  31.  }
  32.  print "}"
  33. }
  34.  
  35.  
  36. #This writes the start of the function get_canonical_name()
  37. #this function will return a single name for all equivalent alias values.
  38. BEGIN { FS = ","; add_get_canonical_hdr()}
  39.  
  40. #If wild cards are used in an alias get_canonical_name() uses regX case statments to do the match.
  41. #Otherwise get_cannonical_name() looks up the value in an associative array (AKA dictonary or hashmap)
  42. {
  43.  canonical_NAME=$1
  44.  sub(/\*/,"",canonical_NAME) #This isn'"'"'t necissary but makes the key slightly nicer looking
  45.   for(i=NF;i>=1;i--){
  46.     if(index($i,"*")>0){
  47.       add_canonical_case($i,canonical_NAME) #alias has a wild card so wirite case statement to match alias
  48.     }
  49.     else{
  50.       CANONICAL_ARY[$i]=canonical_NAME #alias has no wild card so add to lookup array)
  51.     }  
  52.   }
  53. }
  54. END {add_get_canonical_footer()
  55.      write__init_CANONICAL_ARY()
  56.     } ' "$alias_file")"
  57.    
  58. echo "$ALIAS_FNS"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top