Difference between
modified post 6486f08ef by Anonymous on Wed 31st Jan 2024 14:02
original post ed8aa by Anonymous on Wed 31st Jan 2024 13:43
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | #!/bin/ksh93 | |
| 2 | 2 | ||
| 3 | 3 | # | |
| 4 | 4 | # MIT License | |
| 5 | 5 | # | |
| 6 | 6 | # Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org> | |
| 7 | 7 | # | |
| 8 | 8 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 9 | 9 | # of this software and associated documentation files (the "Software"), to deal | |
| 10 | 10 | # in the Software without restriction, including without limitation the rights | |
| 11 | 11 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 12 | 12 | # copies of the Software, and to permit persons to whom the Software is | |
| 13 | 13 | # furnished to do so, subject to the following conditions: | |
| 14 | 14 | # | |
| 15 | 15 | # The above copyright notice and this permission notice shall be included in all | |
| 16 | 16 | # copies or substantial portions of the Software. | |
| 17 | 17 | # | |
| 18 | 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 19 | 19 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 20 | 20 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 21 | 21 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 22 | 22 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 23 | 23 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 24 | 24 | # SOFTWARE. | |
| 25 | 25 | # | |
| 26 | 26 | ||
| 27 | 27 | # | |
| 28 | 28 | # nfsurlconv.ksh - convert host/port/path from/to a nfs://-URL | |
| 29 | 29 | ||
| 30 | 30 | # | |
| 31 | 31 | # Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 32 | 32 | # | |
| 33 | 33 | ||
| 34 | 34 | function usage | |
| 35 | 35 | { | |
| 36 | 36 | (( OPTIND=0 )) | |
| 37 | 37 | getopts -a "${1}" "${2}" OPT '-?' | |
| 38 | 38 | return 2 | |
| 39 | 39 | } | |
| 40 | 40 | ||
| 41 | 41 | # | |
| 42 | 42 | # parse_rfc1738_url - parse RFC 1838 URLs | |
| 43 | 43 | # | |
| 44 | 44 | # Output variables are named after RFC 1838 Section 5 ("BNF for | |
| 45 | 45 | # specific URL schemes") | |
| 46 | 46 | # | |
| 47 | 47 | function parse_rfc1738_url | |
| 48 | 48 | { | |
| 49 | 49 | set -o nounset | |
| 50 | 50 | ||
| 51 | 51 | typeset url="$2" | |
| 52 | 52 | typeset leftover | |
| 53 | 53 | nameref data="$1" # output compound variable | |
| 54 | 54 | ||
| 55 | 55 | # ~(E) is POSIX extended regular expression matching (instead | |
| 56 | 56 | # of shell pattern), "x" means "multiline", "l" means "left | |
| 57 | 57 | # anchor", "r" means "right anchor" | |
| 58 | 58 | leftover="${url/~(Elrx) | |
| 59 | 59 | (.+?) # scheme | |
| 60 | 60 | :\/\/ # '://' | |
| 61 | 61 | ( # login | |
| 62 | 62 | (?: | |
| 63 | 63 | (.+?) # user (optional) | |
| 64 | 64 | (?::(.+))? # password (optional) | |
| 65 | 65 | @ | |
| 66 | 66 | )? | |
| 67 | 67 | ( # hostport | |
| 68 | 68 | (.+?) # host | |
| 69 | 69 | (?::([[:digit:]]+))? # port (optional) | |
| 70 | 70 | ) | |
| 71 | 71 | ) | |
| 72 | 72 | (?:\/(.*?))?/X}" # path (optional) | |
| 73 | 73 | ||
| 74 | 74 | # All parsed data should be captured via eregex in .sh.match - if | |
| 75 | 75 | # there is anything left (except the 'X') then the input string did | |
| 76 | 76 | # not properly match the eregex | |
| 77 | 77 | [[ "$leftover" == 'X' ]] || | |
| 78 | 78 | { print -u2 -f $"%s: Parser error, leftover=%q\n" \ | |
| 79 | 79 | "$0" "$leftover" ; return 1 ; } | |
| 80 | 80 | ||
| 81 | 81 | data.url="${.sh.match[0]}" | |
| 82 | 82 | data.scheme="${.sh.match[1]}" | |
| 83 | 83 | data.login="${.sh.match[2]}" | |
| 84 | 84 | # FIXME: This should use [[ ! -v .sh.match[3] ]], but ksh93u has bugs | |
| 85 | 85 | [[ "${.sh.match[3]-}" != '' ]] && data.user="${.sh.match[3]}" | |
| 86 | 86 | [[ "${.sh.match[4]-}" != '' ]] && data.password="${.sh.match[4]}" | |
| 87 | 87 | data.hostport="${.sh.match[5]}" | |
| 88 | 88 | data.host="${.sh.match[6]}" | |
| 89 | 89 | [[ "${.sh.match[7]-}" != '' ]] && integer data.port="${.sh.match[7]}" | |
| 90 | 90 | [[ "${.sh.match[8]-}" != '' ]] && data.uripath="${.sh.match[8]}" | |
| 91 | 91 | ||
| 92 | 92 | if [[ -v data.uripath ]] ; then | |
| 93 | 93 | data.path="${ printf "${data.uripath//~(E)(?:%([[:xdigit:]][[:xdigit:]]))/\\x\1}" ; }" | |
| 94 | 94 | fi | |
| 95 | 95 | ||
| 96 | 96 | return 0 | |
| 97 | 97 | } | |
| 98 | 98 | ||
| 99 | 99 | ||
| 100 | 100 | function parse_sshnfs_url | |
| 101 | 101 | { | |
| 102 | 102 | typeset url="$2" | |
| 103 | 103 | nameref data="$1" | |
| 104 | 104 | ||
| 105 | 105 | parse_rfc1738_url data "$url" || return 1 | |
| 106 | 106 | ||
| 107 | 107 | [[ "${data.scheme}" == ~(Elr)(ssh\+nfs|nfs) ]] || \ | |
| 108 | 108 | { print -u2 -f $"%s: Not a nfs:// or ssh+nfs:// url\n" "$0" ; return 1 ; } | |
| 109 | 109 | [[ "${data.host}" != '' ]] || { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; } | |
| 110 | 110 | [[ "${data.uripath}" != '' ]] || { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; } | |
| 111 | 111 | [[ "${data.uripath}" == /* ]] || { print -u2 -f $"%s: NFS path (%q) must be absolute\n" "$0" "${data.uripath}" ; return 1 ; } | |
| 112 | 112 | [[ "${data.uripath}" != //* ]] || { print -u2 -f $"%s: NFS path (%q) should not start with '//' \n" "$0" "${data.uripath}" ; return 1 ; } | |
| 113 | 113 | ||
| 114 | 114 | return 0 | |
| 115 | 115 | } | |
| 116 | 116 | ||
| 117 | 117 | ||
| 118 | 118 | function urlencodestr | |
| 119 | 119 | { | |
| 120 | 120 | set -o nounset | |
| 121 | 121 | ||
| 122 | 122 | nameref out_encodedstr=$1 | |
| 123 | 123 | typeset in_str="$2" | |
| 124 | 124 | typeset ch ch_hexval dummy | |
| 125 | 125 | integer ch_num | |
| 126 | 126 | typeset url='' | |
| 127 | 127 | ||
| 128 | 128 | # | |
| 129 | 129 | # URLs encode non-ASCII characters as UTF-8 on byte-level, | |
| 130 | 130 | # while POSIX shells (ksh93, bash, etc.) operate on | |
| 131 | 131 | # characters (which *MAY* be - like UTF-8 - encoded as | |
| 132 | 132 | # multibyte characters, but may use a different encoding | |
| 133 | 133 | # like ISO8859-1 or GB18030). | |
| 134 | 134 | # The code below solves that by using iconv(1) to | |
| 135 | 135 | # convert everything into UTF-8 bytes, then convert the | |
| 136 | 136 | # bytes via od(1) into pairs of { position, hexadecimal | |
| 137 | 137 | # character value ("hexval") }, and then turn these to | |
| 138 | 138 | # ASCII ("ch") / numeric ASCII ("ch_num") values | |
| 139 | 139 | # (this assumes that the current LC_CTYPE is | |
| 140 | 140 | # ASCII-compatible) | |
| 141 | 141 | # | |
| 142 | 142 | printf '%s' "$in_str" | \ | |
| 143 | 143 | iconv -t 'UTF-8' | \ | |
| 144 | 144 | od -t x1 -w1 -v | \ | |
| 145 | 145 | while read dummy ch_hexval ; do | |
| 146 | 146 | [[ "$ch_hexval" != '' ]] || break | |
| 147 | 147 | ||
| 148 | 148 | ch_num="${ printf "%d" "0x$ch_hexval" ; }" | |
| 149 | 149 | if (( ch_num <= 127 )) ; then | |
| 150 | 150 | typeset ch="${ printf "\x$ch_hexval" ; }" | |
| 151 | 151 | else | |
| 152 | 152 | # | |
| 153 | 153 | # character is outside ASCII, shell may | |
| 154 | 154 | # not be able to represent this | |
| 155 | 155 | # | |
| 156 | 156 | [[ -v ch ]] && unset ch | |
| 157 | 157 | fi | |
| 158 | 158 | ||
| 159 | 159 | # | |
| 160 | 160 | # From RFC 1738 ("Uniform Resource Locators (URL)"): | |
| 161 | 161 | # unsafe characters in URLS: | |
| 162 | 162 | # "{", "}", "|", "\", "^", "~", "[", "]", and "`" | |
| 163 | 163 | # characters which must always be encoded: | |
| 164 | 164 | # "#", "%" | |
| 165 | 165 | # characters which must be encoded because they have a special meaning: | |
| 166 | 166 | # ";", "/", "?", ":", "@", "=" and "&" | |
| 167 | 167 | # Only alphanumerics, "$-_.+!*'()," and reserved characters | |
| 168 | 168 | # ("/" for nfs://-URLS) are allowed | |
| 169 | 169 | # | |
| 170 | 170 | if (( ch_num > 127 )) || [[ "$ch" != ~(Elr)[/$-_.+!*\'(),[:alnum:]] ]] ; then | |
| 171 | 171 | url+="%$ch_hexval" | |
| 172 | 172 | else | |
| 173 | 173 | url+="$ch" | |
| 174 | 174 | fi | |
| 175 | 175 | done | |
| 176 | 176 | ||
| 177 | 177 | #printf 'str=%q\n' "$url" | |
| 178 | 178 | out_encodedstr="$url" | |
| 179 | 179 | return 0 | |
| 180 | 180 | } | |
| 181 | 181 | ||
| 182 | 182 | ||
| 183 | 183 | function hostname_port_path_to_nfsurl | |
| 184 | 184 | { | |
| 185 | 185 | set -o nounset | |
| 186 | 186 | ||
| 187 | 187 | typeset hostname="$1" | |
| 188 | 188 | integer port="$2" | |
| 189 | 189 | typeset path="$3" | |
| 190 | 190 | ||
| 191 | 191 | typeset enc_path | |
| 192 | 192 | typeset enc_hostname | |
| 193 | 193 | ||
| 194 | 194 | urlencodestr enc_hostname "$hostname" | |
| 195 | 195 | urlencodestr enc_path "$path" | |
| 196 | 196 | if (( port == 2049 )) ; then | |
| 197 | 197 | printf 'url=nfs://%s/%s\n' "$enc_hostname" "$enc_path" | |
| 198 | 198 | else | |
| 199 | 199 | printf 'url=nfs://%s:%d/%s\n' "$enc_hostname" port "$enc_path" | |
| 200 | 200 | fi | |
| 201 | 201 | return 0 | |
| 202 | 202 | } | |
| 203 | 203 | ||
| 204 | 204 | function main | |
| 205 | 205 | { | |
| 206 | 206 | set -o nounset | |
| 207 | 207 | ||
| 208 | 208 | # fixme: Need better text layout for $ nfsurlconv --man # | |
| 209 | 209 | typeset -r nfsurlconv_usage=$'+ | |
| 210 | 210 | [-?\n@(#)\$Id: nfsurlconv (Roland Mainz) 2024-01-31 \$\n] | |
| 211 | 211 | [-author?Roland Mainz <roland.mainz@nrubsig.org>] | |
| 212 | 212 | [+NAME?nfsurlconv - convert hostname,port,path from/to a nfs://-URL] | |
| 213 | 213 | [+DESCRIPTION?\bnfsurlconv\b convert { hostname, port, path } from/to a nfs://-URL.] | |
| 214 | 214 | [D:debug?Enable debugging.] | |
| 215 | 215 | ||
| 216 | - | hostnameportpath2nfsurl [options] | |
| 216 | + | hostnameportpath2nfsurl hostname port path | |
| 217 | - | hostnamepath2nfsurl [options] | |
| 217 | + | hostnamepath2nfsurl hostname path | |
| 218 | - | url2hostnameportpath [options] | |
| 218 | + | url2hostnameportpath url | |
| 219 | - | url2hostportpath [options] | |
| 219 | + | url2hostportpath url | |
| 220 | - | url2compound [options] | |
| 220 | + | url2compound url | |
| 221 | 221 | --man | |
| 222 | 222 | ||
| 223 | 223 | [+EXAMPLES]{ | |
| 224 | 224 | [+?Example 1:][+?Convert hostname bbb, port 12049 and path /a/b/c to a nfs://-URL]{ | |
| 225 | 225 | [+\n$ nfsurlconv hostnameportpath2nfsurl bbb 12049 "/a/b/c" | |
| 226 | 226 | url=nfs:://bbb::12049//a/b/c | |
| 227 | 227 | ] | |
| 228 | 228 | } | |
| 229 | 229 | [+?Example 2:][+?Convert URL nfs://bbb:12049//a/b/c to ( hostname=, port=, path= )]{ | |
| 230 | 230 | [+\n$ nfsurlconv.ksh url2hostnameportpath nfs:://bbb//a/b/c | |
| 231 | 231 | hostname=bbb | |
| 232 | 232 | port=2049 | |
| 233 | 233 | path=/a/b/c | |
| 234 | 234 | ] | |
| 235 | 235 | } | |
| 236 | 236 | [+?Example 3:][+?Convert URL nfs://bbb:12049//a/b/c to ( hostport=, path= )]{ | |
| 237 | 237 | [+\n$ nfsurlconv.ksh url2hostportpath nfs:://bbb//a/b/c | |
| 238 | 238 | hostport=bbb | |
| 239 | 239 | path=/a/b/c | |
| 240 | 240 | ] | |
| 241 | 241 | } | |
| 242 | 242 | } | |
| 243 | 243 | [+SEE ALSO?\bksh93\b(1),\bssh\b(1),\bmount.nfs\b(8),\bnfs\b(5)] | |
| 244 | 244 | ' | |
| 245 | 245 | ||
| 246 | 246 | compound c | |
| 247 | 247 | typeset -a c.args | |
| 248 | 248 | integer saved_optind_m1 # saved OPTIND-1 | |
| 249 | 249 | ||
| 250 | 250 | c.args=( "$@" ) | |
| 251 | 251 | ||
| 252 | 252 | # | |
| 253 | 253 | # Argument parsing | |
| 254 | 254 | # | |
| 255 | 255 | while getopts -a "${progname}" "${nfsurlconv_usage}" OPT "${c.args[@]}" ; do | |
| 256 | 256 | case "${OPT}" in | |
| 257 | 257 | 'D') | |
| 258 | 258 | # fixme: Implement debugging option | |
| 259 | 259 | ;; | |
| 260 | 260 | *) | |
| 261 | 261 | usage "${progname}" "${nfsurlconv_usage}" | |
| 262 | 262 | return $? | |
| 263 | 263 | ;; | |
| 264 | 264 | esac | |
| 265 | 265 | done | |
| 266 | 266 | ||
| 267 | 267 | (( saved_optind_m1=OPTIND-1 )) | |
| 268 | 268 | ||
| 269 | 269 | # remove options we just parsed from c.args | |
| 270 | 270 | for ((i=0 ; i < saved_optind_m1 ; i++)) ; do | |
| 271 | 271 | unset c.args[$i] | |
| 272 | 272 | done | |
| 273 | 273 | ||
| 274 | 274 | # | |
| 275 | 275 | # c.args mighth be a sparse array (e.g. "([1]=aaa [2]=bbb [4]=ccc)") | |
| 276 | 276 | # right now after we removed processed options/arguments. | |
| 277 | 277 | # For easier processing below we "reflow" the array back to a | |
| 278 | 278 | # normal linear layout (e.g. ([0]=aaa [1]=bbb [2]=ccc) | |
| 279 | 279 | # | |
| 280 | 280 | c.args=( "${c.args[@]}" ) | |
| 281 | 281 | ||
| 282 | - | typeset mode="$1" | |
| 282 | + | typeset mode="${c.args[0]-}" | |
| 283 | 283 | ||
| 284 | 284 | case "$mode" in | |
| 285 | + | # fixme: add "hostportpath2nfsurl" | |
| 285 | 287 | 'hostnameportpath2nfsurl') | |
| 286 | 288 | hostname_port_path_to_nfsurl "${@:2}" | |
| 287 | 289 | return $? | |
| 288 | 290 | ;; | |
| 289 | 291 | 'hostnamepath2nfsurl') | |
| 290 | 292 | hostname_port_path_to_nfsurl "${@:2:1}" 2049 "${@:3:1}" | |
| 291 | 293 | return $? | |
| 292 | 294 | ;; | |
| 293 | 295 | 'url2hostnameportpath') | |
| 294 | 296 | compound urldata | |
| 295 | 297 | ||
| 296 | 298 | parse_sshnfs_url urldata "${@:2:1}" || return 1 | |
| 297 | 299 | printf 'hostname=%s\n' "${urldata.host}" | |
| 298 | 300 | printf 'port=%s\n' "${urldata.port-2049}" | |
| 299 | 301 | printf 'path=%s\n' "${urldata.path-}" | |
| 300 | 302 | return 0 | |
| 301 | 303 | ;; | |
| 302 | 304 | 'url2hostportpath') | |
| 303 | 305 | compound urldata | |
| 304 | 306 | ||
| 305 | 307 | parse_sshnfs_url urldata "${@:2:1}" || return 1 | |
| 306 | 308 | printf 'hostport=%s\n' "${urldata.hostport}" | |
| 307 | 309 | printf 'path=%s\n' "${urldata.path-}" | |
| 308 | 310 | return 0 | |
| 309 | 311 | ;; | |
| 310 | 312 | 'url2compound') | |
| 311 | 313 | compound urldata | |
| 312 | 314 | ||
| 313 | 315 | parse_sshnfs_url urldata "${@:2:1}" || return 1 | |
| 314 | 316 | print -v urldata | |
| 315 | 317 | return 0 | |
| 316 | 318 | ;; | |
| 317 | 319 | *) | |
| 318 | 320 | print -u2 -f $"Unknown mode %q\n" "$mode" | |
| 319 | 321 | usage "${progname}" "${nfsurlconv_usage}" | |
| 320 | 322 | return 2 | |
| 321 | 323 | ;; | |
| 322 | 324 | esac | |
| 323 | 325 | ||
| 324 | 326 | return 2 | |
| 325 | 327 | } | |
| 326 | 328 | ||
| 327 | 329 | # | |
| 328 | 330 | # main | |
| 329 | 331 | # | |
| 330 | 332 | builtin cat | |
| 331 | 333 | builtin mkdir | |
| 332 | 334 | builtin basename | |
| 333 | 335 | ||
| 334 | 336 | typeset progname="${ basename "${0}" ; }" | |
| 335 | 337 | ||
| 336 | 338 | main "$@" | |
| 337 | 339 | exit $? | |
| 338 | 340 | ||
| 339 | 341 | # EOF. | |
| 340 | 342 |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
nrubsig.kpaste.net RSS