- #!/bin/ksh93
 - #
 - # nfsurl.ksh - convert host/port/path from/to a nfs://-URL
 - #
 - # Written by Roland Mainz <roland.mainz@nrubsig.org>
 - #
 - function urlencodestr
 - {
 - set -o nounset
 - nameref out_encodedstr=$1
 - typeset in_str="$2"
 - typeset ch hexval dummy
 - integer ch_num
 - typeset url=''
 - #
 - # URLs are encoded in UTF-8 on byte-level, while shells
 - # operate on characters (which might be multibyte, and
 - # even in a different encoding like en_US.ISO8859-1 or
 - # GB18030).
 - # The code below solves that by using /usr/bin/iconv to
 - # convert everything into UTF-8 encoding, then into
 - # hexadecimal values, and then turn these to ASCII
 - # (which assumes that the current LC_CTYPE is
 - # ASCII-compatible)
 - #
 - printf '%s' "$in_str" | \
 - iconv -t 'UTF-8' | \
 - od -t x1 -w1 -v | \
 - while read dummy ch_hexval ; do
 - [[ "$ch_hexval" != '' ]] || break
 - ch_num="${ printf "%d" "0x$ch_hexval" ; }"
 - if (( ch_num <= 127 )) ; then
 - ch="${ printf "\x$ch_hexval" ; }"
 - else
 - # character is outside ASCII
 - ch=''
 - fi
 - #
 - # From RFC 1738 ("Uniform Resource Locators (URL)"):
 - # unsafe characters in URLS:
 - # "{", "}", "|", "\", "^", "~", "[", "]", and "`"
 - # characters which must always be encoded:
 - # "#", "%"
 - # characters which must be encoded because they have a special meaning:
 - # ";", "/", "?", ":", "@", "=" and "&"
 - # Only alphanumerics, "$-_.+!*'()," and reserved characters
 - # ("/" for nfs://-URLS) are allowed
 - #
 - if (( ch_num > 127 )) || [[ "$ch" != ~(Elr)[/$-_.+!*\'(),[:alnum:]] ]] ; then
 - url+="%$ch_hexval"
 - else
 - url+="$ch"
 - fi
 - done
 - #printf 'str=%q\n' "$url"
 - out_encodedstr="$url"
 - return 0
 - }
 - function hostname_port_path_to_nfsurl
 - {
 - set -o nounset
 - typeset hostname="$1"
 - integer port="$2"
 - typeset path="$3"
 - typeset enc_path
 - typeset enc_hostname
 - urlencodestr enc_hostname "$hostname"
 - urlencodestr enc_path "$path"
 - if (( port == 2049 )) ; then
 - printf 'url=nfs://%s/%s\n' "$enc_hostname" "$enc_path"
 - else
 - printf 'url=nfs://%s:%d/%s\n' "$enc_hostname" port "$enc_path"
 - fi
 - return 0
 - }
 - hostname_port_path_to_nfsurl "$@"
 - # EOF.
 
nfsurl.ksh prototype
Posted by Anonymous on Wed 31st Jan 2024 03:05
raw | new post
view followups (newest first): nfsurl.ksh prototype by Anonymous
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