pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


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

  1. #!/bin/ksh93
  2.  
  3. #
  4. # nfsurl.ksh - convert host/port/path from/to a nfs://-URL
  5. #
  6. # Written by Roland Mainz <roland.mainz@nrubsig.org>
  7. #
  8.  
  9. function urlencodestr
  10. {
  11.         set -o nounset
  12.  
  13.         nameref out_encodedstr=$1
  14.         typeset in_str="$2"
  15.         typeset ch hexval dummy
  16.         integer ch_num
  17.         typeset url=''
  18.  
  19.         #
  20.         # URLs are encoded in UTF-8 on byte-level, while shells
  21.         # operate on characters (which might be multibyte, and
  22.         # even in a different encoding like en_US.ISO8859-1 or
  23.         # GB18030).
  24.         # The code below solves that by using /usr/bin/iconv to
  25.         # convert everything into UTF-8 encoding, then into
  26.         # hexadecimal values, and then turn these to ASCII
  27.         # (which assumes that the current LC_CTYPE is
  28.         # ASCII-compatible)
  29.         #
  30.         printf '%s' "$in_str" | \
  31.                 iconv -t 'UTF-8' | \
  32.                 od -t x1 -w1 -v | \
  33.                 while read dummy ch_hexval ; do
  34.                 [[ "$ch_hexval" != '' ]] || break
  35.  
  36.                 ch_num="${ printf "%d" "0x$ch_hexval" ; }"
  37.                 if (( ch_num <= 127 )) ; then
  38.                         ch="${ printf "\x$ch_hexval" ; }"
  39.                 else
  40.                         # character is outside ASCII
  41.                         ch=''
  42.                 fi
  43.  
  44.                 #
  45.                 # From RFC 1738 ("Uniform Resource Locators (URL)"):
  46.                 # unsafe characters in URLS:
  47.                 # "{", "}", "|", "\", "^", "~", "[", "]", and "`"
  48.                 # characters which must always be encoded:
  49.                 # "#", "%"
  50.                 # characters which must be encoded because they have a special meaning:
  51.                 # ";", "/", "?", ":", "@", "=" and "&"
  52.                 # Only alphanumerics, "$-_.+!*'()," and reserved characters
  53.                 # ("/" for nfs://-URLS) are allowed
  54.                 #
  55.                 if (( ch_num > 127 )) || [[ "$ch" != ~(Elr)[/$-_.+!*\'(),[:alnum:]] ]] ; then
  56.                         url+="%$ch_hexval"
  57.                 else
  58.                         url+="$ch"
  59.                 fi
  60.         done
  61.  
  62.         #printf 'str=%q\n' "$url"
  63.         out_encodedstr="$url"
  64.         return 0
  65. }
  66.  
  67. function hostname_port_path_to_nfsurl
  68. {
  69.         set -o nounset
  70.  
  71.         typeset hostname="$1"
  72.         integer port="$2"
  73.         typeset path="$3"
  74.  
  75.         typeset enc_path
  76.         typeset enc_hostname
  77.  
  78.         urlencodestr enc_hostname "$hostname"
  79.         urlencodestr enc_path "$path"
  80.         if (( port == 2049 )) ; then
  81.                 printf 'url=nfs://%s/%s\n' "$enc_hostname" "$enc_path"
  82.         else
  83.                 printf 'url=nfs://%s:%d/%s\n' "$enc_hostname" port "$enc_path"
  84.         fi
  85.         return 0
  86. }
  87.  
  88. hostname_port_path_to_nfsurl "$@"
  89.  
  90. # EOF.

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at