3 # Copyright (c) 2020 Apple Inc. All rights reserved.
7 declare -r script=${BASH_SOURCE[0]}
8 declare -r rcodesURL
='https://www.iana.org/assignments/dns-parameters/dns-parameters-6.csv'
10 #============================================================================================================================
15 echo "Usage: $( basename "${script}" ) [options]"
18 echo " -h Display script usage."
19 echo " -V Display version of this script and exit."
21 echo "This script writes C functions to convert DNS RCODE values to strings and vice versa to stdout"
22 echo "based on the latest DNS RCODE data available at"
28 #============================================================================================================================
36 #============================================================================================================================
38 StripLeadingTrailingWhitespace
()
40 sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
43 #============================================================================================================================
48 while IFS
=',' read value name others
; do
49 name
=$( StripLeadingTrailingWhitespace <<< "${name}" )
50 [[ ${name} =~ ^unassigned$
]] && continue
52 value
=$( StripLeadingTrailingWhitespace <<< "${value}" )
53 [[ ${value} =~ ^
[0-9]+$
]] || continue
54 [ "${value}" -le 15 ] || continue
56 echo "${name},${value}"
61 #============================================================================================================================
65 name
="${1//[^A-Za-z0-9_]/_}" # Only allow alphanumeric and underscore characters.
66 printf "kDNSRCode_${name}"
69 #============================================================================================================================
73 local -r inputFile
=${1}
74 printf "typedef enum\n"
77 < "${inputFile}" sort --field-separator=, --key=2,2 --numeric-sort --unique |
78 while IFS
=',' read name value
; do
80 local enum
=$( RCodeMnemonicToEnum "${name}" )
81 printf "\t%-20s= %d" "${enum}" "${value}"
86 printf "}\tDNSRCode;\n"
89 #============================================================================================================================
91 PrintValueToStringElseIf
()
95 [ "${first}" -le "${last}" ] || ErrQuit
"${first} > ${last}"
97 local stringArray
=( "$@" )
99 if [ "${last}" -ne "${first}" ]; then
100 printf "\telse if( ( inValue >= ${first} ) && ( inValue <= ${last} ) )\n"
101 local -r arrayVarName
="sNames_${first}_${last}"
103 printf "\telse if( inValue == ${first} )\n"
104 local -r arrayVarName
="sNames_${first}"
107 printf "\t\tstatic const char * const\t\t${arrayVarName}[] =\n"
110 for string
in "${stringArray[@]}"; do
111 printf "\t\t\t%-15s // %3d\n" "\"${string}\"," "${value}"
112 value
=$(( value + 1 ))
114 local -r stringCount
=$(( value - first ))
115 local -r expectedCount
=$(( last - first + 1 ))
116 [ "${stringCount}" -eq "${expectedCount}" ] || ErrQuit
"${stringCount} != ${expectedCount}"
118 printf "\t\tstring = ${arrayVarName}[ inValue - ${first} ];\n"
122 #============================================================================================================================
124 PrintValueToStringFunction
()
126 local -r inputFile
=${1}
127 printf "const char *\tDNSRCodeToString( const int inValue )\n"
129 printf "\tswitch( inValue )\n"
131 < "${inputFile}" sort --field-separator=, --key=2,2 --numeric-sort --unique |
134 while IFS
=',' read name value
; do
135 local enum
=$( RCodeMnemonicToEnum "${name}" )
136 printf "\t\t%-28s%s\n" "case ${enum}:" "return( \"${name}\" );"
139 printf "\t\t%-28sreturn( NULL );\n" "default:"
144 #============================================================================================================================
146 PrintStringToValueFunction
()
148 local -r inputFile
=${1}
149 printf "#include <stdlib.h>\n"
151 printf "typedef struct\n"
153 printf "\tconst char *\t\tname;\n"
154 printf "\tint\t\t\t\t\tvalue;\n"
156 printf "}\t_DNSRCodeTableEntry;\n"
158 printf "static int\t_DNSRCodeFromStringCmp( const void *inKey, const void *inElement );\n"
160 printf "int\tDNSRCodeFromString( const char * const inString )\n"
162 printf "\t// The name-value table is sorted by name in ascending lexicographical order to allow going from name to\n"
163 printf "\t// value in logarithmic time via a binary search.\n"
165 printf "\tstatic const _DNSRCodeTableEntry\t\tsTable[] =\n"
169 < "${inputFile}" sort --field-separator=, --key=1,1 --ignore-case --unique |
170 while IFS
=',' read name value
; do
172 local enum
=$( RCodeMnemonicToEnum "${name}" )
173 printf "\t\t%-16s%-20s}" "{ \"${name}\"," "${enum}"
178 printf "\tconst _DNSRCodeTableEntry *\t\t\tentry;\n"
180 printf "\tentry = (_DNSRCodeTableEntry *) bsearch( inString, sTable, sizeof( sTable ) / sizeof( sTable[ 0 ] ),\n"
181 printf "\t\tsizeof( sTable[ 0 ] ), _DNSRCodeFromStringCmp );\n"
182 printf "\treturn( entry ? entry->value : -1 );\n"
185 printf "static int\t_DNSRCodeFromStringCmp( const void * const inKey, const void * const inElement )\n"
187 printf "\tconst _DNSRCodeTableEntry * const\t\tentry = (const _DNSRCodeTableEntry *) inElement;\n"
188 printf "\treturn( strcasecmp( (const char *) inKey, entry->name ) );\n"
192 #============================================================================================================================
196 if [ -d "${tempDir}" ]; then
201 #============================================================================================================================
205 printf "// This code was autogenerated on $( date -u '+%Y-%m-%d' ) by $( basename ${script} ) version ${version}\n"
206 printf "// Data source URL: ${rcodesURL}\n"
210 #============================================================================================================================
214 while getopts ":hO:V" option
; do
221 echo "$( basename "${script}" ) version ${version}"
225 ErrQuit
"option '${OPTARG}' requires an argument."
228 ErrQuit
"unknown option '${OPTARG}'."
233 [ "${OPTIND}" -gt "$#" ] || ErrQuit
"unexpected argument \"${!OPTIND}\"."
235 trap ExitHandler EXIT
236 tempDir
=$( mktemp -d ) || ErrQuit
"Failed to make temporary directory."
237 declare -r originalRCodesFile
="${tempDir}/rcodesOriginal.csv"
238 curl
--output "${originalRCodesFile}" "${rcodesURL}" || ErrQuit
"Failed to download CSV file."
240 declare -r rcodesFile
="${tempDir}/rcodes.csv"
241 < "${originalRCodesFile}" GetNamesAndValues
> "${rcodesFile}"
243 declare -r tempFile
="${tempDir}/temp.csv"
244 < "${rcodesFile}" sort --field-separator=, --key=2,2 --unique --numeric-sort > "${tempFile}"
245 < "${tempFile}" sort --field-separator=, --key=1,1 --unique --ignore-case > "${rcodesFile}"
248 PrintRCodeEnums
"${rcodesFile}"
251 PrintValueToStringFunction
"${rcodesFile}"
254 PrintStringToValueFunction
"${rcodesFile}"