- #
- # parse_rfc1738_url - parse RFC 1838 URLs
- #
- # Output variables are named after RFC 1838 Section 5 ("BNF for
- # specific URL schemes")
- #
- function parse_rfc1738_url
- {
- set -o nounset
- typeset url="$2"
- typeset leftover
- nameref data="$1" # output compound variable
- # ~(E) is POSIX extended regular expression matching (instead
- # of shell pattern), "x" means "multiline", "l" means "left
- # anchor", "r" means "right anchor"
- leftover="${url/~(Elrx)
- (.+?) # scheme
- :\/\/ # '://'
- ( # login
- (?:
- (.+?) # user (optional)
- (?::(.+))? # password (optional)
- @
- )?
- ( # hostport
- (.+?) # host
- (?::([[:digit:]]+))? # port (optional)
- )
- )
- (?:\/(.*?))?/X}" # path (optional)
- # All parsed data should be captured via eregex in .sh.match - if
- # there is anything left (except the 'X') then the input string did
- # not properly match the eregex
- [[ "$leftover" == 'X' ]] ||
- { print -u2 -f $"%s: Parser error, leftover=%q\n" \
- "$0" "$leftover" ; return 1 ; }
- data.url="${.sh.match[0]}"
- data.scheme="${.sh.match[1]}"
- data.login="${.sh.match[2]}"
- # FIXME: This should use [[ ! -v .sh.match[3] ]], but ksh93u has bugs
- [[ "${.sh.match[3]-}" != '' ]] && data.user="${.sh.match[3]}"
- [[ "${.sh.match[4]-}" != '' ]] && data.password="${.sh.match[4]}"
- data.hostport="${.sh.match[5]}"
- data.host="${.sh.match[6]}"
- [[ "${.sh.match[7]-}" != '' ]] && integer data.port="${.sh.match[7]}"
- [[ "${.sh.match[8]-}" != '' ]] && data.uripath="${.sh.match[8]}"
- return 0
- }
- function main
- {
- compound c
- input="foo://host/path1/path2" ; printf "## input=%q\n" "$input"
- parse_rfc1738_url c "$input"
- print -v c
- input="foo://myusr@host:14/path1/path2" ; printf "## input=%q\n" "$input"
- parse_rfc1738_url c "$input"
- print -v c
- input="foo://myusr:mypasswd@host:14/path1/path2" ; printf "## input=%q\n" "$input"
- parse_rfc1738_url c "$input"
- print -v c
- }
- main
ksh93 parse_rfc1738_url - parse RFC 1838 URLs
Posted by Anonymous on Wed 19th Jul 2023 14:59
raw | new post
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.