home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / aaa / aux / base next >
Encoding:
Text File  |  1986-11-30  |  2.3 KB  |  114 lines

  1. BEGIN {
  2.     # Hex/octal conversion table.
  3.     hex["0"] = 0
  4.     hex["1"] = 1
  5.     hex["2"] = 2
  6.     hex["3"] = 3
  7.     hex["4"] = 4
  8.     hex["5"] = 5
  9.     hex["6"] = 6
  10.     hex["7"] = 7
  11.     hex["8"] = 8
  12.     hex["9"] = 9
  13.     hex["a"] = 10
  14.     hex["b"] = 11
  15.     hex["c"] = 12
  16.     hex["d"] = 13
  17.     hex["e"] = 14
  18.     hex["f"] = 15
  19.     hex["A"] = 10
  20.     hex["B"] = 11
  21.     hex["C"] = 12
  22.     hex["D"] = 13
  23.     hex["E"] = 14
  24.     hex["F"] = 15
  25.     # ASCII conversion tables.
  26.     ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  27.     ascii = ascii "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  28.     conv["n"] = 10
  29.     conv["b"] = 8
  30.     conv["t"] = 9
  31.     conv["r"] = 13
  32.     conv["v"] = 11
  33.     conv["f"] = 12
  34.     conv["a"] = 7
  35. }
  36. /0[xX]?[0-9a-fA-F]/ || /'/ {
  37.     out = $0
  38.     place = 1
  39.     len = length(out)
  40.     while (place <= len) {
  41.         if (substr(out, place, 1) ~ /[a-zA-Z_]/) {
  42.             # Identifier, may contain digits, skip past it.
  43.             i = place+1
  44.             while (i <= len) {
  45.                 char = substr(out, i, 1)
  46.                 if (char !~ /[a-zA-Z0-9_]/)
  47.                     break;
  48.                 i++
  49.             }
  50.             place = i
  51.         } else if (substr(out, place, 1) == "'") {
  52.             # Character constant.
  53.             ch = substr(out, place+1, 1)
  54.             i = place + 2
  55.             if (ch == "\\")    {
  56.                 number = conv[substr(out, place+2, 1)]
  57.                 i++
  58.             } else {
  59.                 ind = index(ascii, ch)
  60.                 if (ind > 0)
  61.                     number = ind + 31
  62.                 else
  63.                     number = ""
  64.             }
  65.             if (number == "")
  66.                 number = 32
  67.             first = substr(out, 1, place-1)
  68.             last = substr(out, i)
  69.             out = first number last
  70.             len = length(out)
  71.             place = len - length(last) + 1
  72.         } else if (substr(out, place) ~ /^0[xX][0-9a-fA-F]+/) {
  73.             # Hex.
  74.             total = 0
  75.             i = place+2
  76.             while (i <= len) {
  77.                 digit = substr(out, i, 1)
  78.                 if (digit !~ /[0-9a-fA-F]/)
  79.                     break;
  80.                 total = total*16 + hex[digit]
  81.                 i++
  82.             }
  83.             first = substr(out, 1, place-1)
  84.             last = substr(out, i)
  85.             # out = first total last
  86.             out = sprintf("%s%.0f%s", first, total, last)
  87.             len = length(out)
  88.             place = len - length(last) + 1
  89.         } else if (substr(out, place) ~ /^0[0-7]+/) {
  90.             # Octal.
  91.             total = 0
  92.             i = place+1
  93.             while (i <= len) {
  94.                 digit = substr(out, i, 1)
  95.                 if (digit !~ /[0-7]/)
  96.                     break;
  97.                 total = total*8 + hex[digit]
  98.                 i++
  99.             }
  100.             first = substr(out, 1, place-1)
  101.             last = substr(out, i)
  102.             # out = first total last
  103.             out = sprintf("%s%.0f%s", first, total, last)
  104.             len = length(out)
  105.             place = len - length(last) + 1
  106.         } else
  107.             # Something else, ignore.
  108.             place++
  109.     }
  110.     print out
  111.     next
  112. }
  113. /./ { print }
  114.