devinteske

Parsing FreeBSD pkgng JSON +MANIFEST files using awk

Aug 20th, 2014
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 2.35 KB | None | 0 0
  1. # NOTE: See http://pastebin.com/q7RpEp5y for an updated version
  2. BEGIN { keyword = "pkg" }
  3. function trim_match() {
  4.     match_text = substr($0, RSTART, RLENGTH)
  5.     $0 = substr($0, RSTART + RLENGTH)
  6.     return match_text
  7. }
  8. function trim_keyword() {
  9.     keyword = substr(keyword, 0, length(keyword) - length(key[i--]) - 1)
  10. }
  11. function print_value(var, value) {
  12.     print ++L, var "=[" value "]"
  13. }
  14. {
  15.     while ($0) {
  16.         #print ++L, $0
  17.         if (building_string) {
  18.             while (match($0, /^[^"]*\\"/))
  19.                 value = value trim_match()
  20.             if (!match($0, /^[^"]*"/)) {
  21.                 value = value $0
  22.                 next
  23.             }
  24.             building_string = 0
  25.             value = value substr($0, RSTART, RLENGTH - 1)
  26.             trim_match()
  27.             sub(/^[[:space:]]*,[[:space:]]*/, "")
  28.             print_value(keyword, value)
  29.             trim_keyword()
  30.         }
  31.         else if (match($0, /^[[:space:]]*{[[:space:]]*/))
  32.             trim_match()
  33.         else if (match($0, \
  34.             /^[[:space:]]*"[^"]+"[[:space:]]*:[[:space:]]*/ \
  35.         )) {
  36.             key[++i] = trim_match()
  37.             sub(/^[[:space:]]*"/, "", key[i])
  38.             sub(/"[[:space:]]*:[[:space:]]*$/, "", key[i])
  39.             keyword = keyword ( keyword ? "." : "" ) key[i]
  40.         }
  41.         else if (keyword && match($0, /^[[:space:]]*\[/)) {
  42.             array_value = ""
  43.             building_array = 1
  44.             trim_match()
  45.         }
  46.         else if (keyword && match($0, /^[[:space:]]*"/)) {
  47.             value = ""
  48.             trim_match()
  49.             while (match($0, /^[^"]*\\"/))
  50.                 value = value trim_match()
  51.             if (!match($0, /^[^"]*"/)) {
  52.                 building_string = 1
  53.                 value = $0
  54.                 next
  55.             }
  56.             value = value substr($0, RSTART, RLENGTH - 1)
  57.             trim_match()
  58.  
  59.             sub(/^[[:space:]]*,[[:space:]]*/, "")
  60.             if (building_array) array_value = array_value \
  61.                 ( array_value ? "," : "" ) value
  62.             else {
  63.                 print_value(keyword, value)
  64.                 trim_keyword()
  65.             }
  66.         }
  67.         else if (keyword && match($0, \
  68.             /^[[:space:]]*[^[:space:],}\]]+[[:space:]]*/ \
  69.         )) {
  70.             value = trim_match()
  71.             sub(/^[[:space:]]*/, "", value)
  72.             sub(/[[:space:]]*$/, "", value)
  73.             sub(/^[[:space:]]*,[[:space:]]*/, "")
  74.             print_value(keyword, value)
  75.             trim_keyword()
  76.         }
  77.         else if (match($0, /^[[:space:]]*\][[:space:]]*/)) {
  78.             print_value(keyword, array_value)
  79.             building_array = 0
  80.             trim_keyword()
  81.             trim_match()
  82.             sub(/^[[:space:]]*,[[:space:]]*/, "")
  83.         }
  84.         else if (match($0, /^[[:space:]]*}[[:space:]]*/)) {
  85.             building_array = 0
  86.             trim_keyword()
  87.             trim_match()
  88.             sub(/^[[:space:]]*,[[:space:]]*/, "")
  89.         }
  90.     }
  91. }
Add Comment
Please, Sign In to add comment