1# $NetBSD: nanpa.awk,v 1.3 2023/01/28 13:12:16 jmcneill Exp $ 2# 3# todo: 4# parse "https://nationalnanpa.com/nanp1/npa_report.csv" 5# instead of scraping HTML. 6# 7function trim(s) 8{ 9 gsub(/^[ \t]+|[ \t]+$/, "", s); 10 return s; 11} 12function mapinit(postdb) 13{ 14 while ((getline < postdb) > 0) { 15 sub(/#.*/, ""); 16 if (length($0)==0) continue; 17 NF=split($0, f); 18 location[f[1]] = f[2]; 19 flocation[tolower(f[2])] = f[2]; 20 country[f[1]] = f[4]; 21 fcountry[tolower(f[2])] = f[4]; 22 } 23} 24function countrymap(s) 25{ 26 if (s == "CA") return "Canada"; 27 if (s == "US") return "USA"; 28 return s; 29} 30function locationmap(s, t) 31{ 32 if (s in location) { 33 t = location[s]; 34 if (s in country) { 35 t = t " (" countrymap(country[s]) ")"; 36 } 37 } else if (tolower(s) in flocation) { 38 t = flocation[tolower(s)]; 39 if (tolower(s) in fcountry) { 40 t = t " (" countrymap(fcountry[tolower(s)]) ")"; 41 } 42 } else { 43 t = s; 44 } 45 return t; 46} 47function parse(file, ispipe, isplanning, i, planinit, t) 48{ 49 planinit = 0; 50 while((ispipe?(file | getline):(getline < file)) > 0) { 51 sub(/#.*/, ""); 52 if (length($0)==0) continue; 53 if (isplanning) { 54 NF=split($0, f); 55 if (!planinit && f[2]=="New NPA") { 56 planinit=1; 57 for (i=1; i<=NF; i++) 58 fnames[f[i]]=i-1; 59 } else if (planinit && length(f[fnames["New NPA"]])>1) { 60 t = locationmap(trim(f[fnames["Location"]])) FS; 61 if (trim(f[fnames["Overlay?"]])=="Yes") 62 t = t "Overlay of " trim(f[fnames["Old NPA"]]); 63 else if (f[fnames["Old NPA"]]) 64 t = t "Split of " trim(f[fnames["Old NPA"]]); 65 if (f[fnames["Status"]]) 66 t = t " (" trim(f[fnames["Status"]]) ")"; 67 if (length(f[fnames["In Service Date"]]) > 1) 68 t = t " effective " \ 69 trim(f[fnames["In Service Date"]]); 70 data[trim(f[fnames["New NPA"]]) "*"] = t; 71 } 72 } else { 73 # digits only 74 match($0, /^[0-9]/); 75 if (RSTART==0) continue; 76 i=index($0, FS); 77 data[substr($0, 1, i-1)]=locationmap(trim(substr($0,i+1))); 78 } 79 } 80 close(file); 81} 82 83BEGIN{ 84 FS=":" 85 mapinit("na.postal"); 86 print "# $""NetBSD: $"; 87 print "# Generated from https://nationalnanpa.com/area_codes/index.html"; 88 print "# (with local exceptions)"; 89 print "# "; 90 print "# format:"; 91 print "# Area Code : Description : Detail : State/Province Abbrev."; 92 print "# (3rd and 4th fields optional)"; 93 print "# A * in the Area Code field indicates a future area code." 94 print "# "; 95 parse("ftp -o - " \ 96 "https://nationalnanpa.com/enas/geoAreaCodeNumberReport.do" \ 97 " | sed -f nanpa.sed", 1, 0); 98 parse("ftp -o - " \ 99 "https://nationalnanpa.com/enas/nonGeoNpaServiceReport.do" \ 100 " | sed -f nanpa.sed", 1, 0); 101 parse("ftp -o - " \ 102 "https://nationalnanpa.com/enas/plannedNpasNotInServiceReport.do" \ 103 " | sed -f nanpa.sed", 1, 1); 104 parse("na.phone.add", 0, 0); 105 sort="sort -n"; 106 for (i in data) 107 print i FS data[i] | sort 108 close(sort); 109} 110