Total rewrite #15
							
								
								
									
										144
									
								
								xdg-sanity
									
									
									
									
									
								
							
							
						
						
									
										144
									
								
								xdg-sanity
									
									
									
									
									
								
							| @ -7,114 +7,108 @@ argv0="$0" | ||||
| # grabs configuration files | ||||
| CONFIG="$XDG_CONFIG_HOME"/xdg-sanity.toml | ||||
| if ! test -e "$CONFIG"; then | ||||
| 	touch "$CONFIG" | ||||
| 	CONFIG=/etc/xdg-sanity.toml | ||||
| 	if ! test -e "$CONFIG"; then | ||||
| 		exit 66 # sysexits(3) EX_NOINPUT | ||||
| 	fi | ||||
|   touch "$CONFIG" | ||||
| fi | ||||
| 
 | ||||
| 
 | ||||
| # check if usage is valid | ||||
| if ! test -n "$1"; then | ||||
| 	printf "Usage: %s [resource...]\n" "$argv0" 1>&2 | ||||
| 	exit 64 # sysexits(3) EX_USAGE | ||||
|   printf "Usage: %s [resource...]\n" "$argv0" 1>&2 | ||||
|   exit 64 # sysexits(3) EX_USAGE | ||||
| fi | ||||
| 
 | ||||
| # check if we have curl(1) | ||||
| if ! command -v curl >/dev/null 2>&1; then | ||||
| 	printf "%s: Missing dependency: curl(1)\n" "$argv0" 1>&2 | ||||
| 	exit 71 # sysexits(3) EX_OSERR | ||||
|   printf "%s: Missing dependency: curl(1)\n" "$argv0" 1>&2 | ||||
|   exit 69 # sysexits(3) EX_UNAVAILABLE | ||||
| fi | ||||
| 
 | ||||
| # check if we have tomcat(1) | ||||
| if ! command -v tomcat >/dev/null 2>&1; then | ||||
| 	printf "%s: Missing dependency: tomcat(1)\n" "$argv0" 1>&2 | ||||
| 	exit 71 # sysexits(3) EX_OSERR | ||||
|   printf "%s: Missing dependency: tomcat(1)\n" "$argv0" 1>&2 | ||||
|   exit 69 # sysexits(3) EX_UNAVAILABLE | ||||
| fi | ||||
| 
 | ||||
| # set the XDG_COMMAND | ||||
| test -n "$XDG_COMMAND" || XDG_COMMAND="$(tomcat tools.xdg "$CONFIG")" | ||||
| ! command -v handlr >/dev/null 2>&1 && ! command -v xdg-open >/dev/null 2>&1 \ | ||||
|     && XDG_COMMAND=false \ | ||||
| 		|| true | ||||
| 
 | ||||
|     || true | ||||
| 
 | ||||
| # check if we have a BROWSER | ||||
| test -n "$BROWSER" || BROWSER="$(tomcat tools.browser "$CONFIG")" | ||||
| if ! test -n "$BROWSER"; then | ||||
| 	printf "\ | ||||
|   printf "\ | ||||
| %s: \$BROWSER not filled. | ||||
| 	Please place the path to your preferred browser's executable in | ||||
| 	$XDG_CONFIG_HOME/xdg-sanity.conf or /etc/xdg-sanity.conf | ||||
|   Please place the path to your preferred browser's executable in | ||||
|   $XDG_CONFIG_HOME/xdg-sanity.toml | ||||
| " "$argv0" 1>&2 | ||||
| 	exit 71 # sysexits(3) EX_OSERR | ||||
|   exit 71 # sysexits(3) EX_OSERR | ||||
| fi | ||||
| 
 | ||||
| while test -n "$1"; do | ||||
| 	URL="$1" | ||||
|   URL="$1" | ||||
| 
 | ||||
| 	# use curl(1) to write out the request header's content_type, | ||||
| 	# strip everything after the first semicolon, | ||||
| 	# chop off any weird whitespace remnants | ||||
| 	MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' |\ | ||||
| 		xargs echo)" | ||||
|   # use curl(1) to write out the request header's content_type, | ||||
|   # strip everything after the first semicolon, | ||||
|   # chop off any weird whitespace remnants | ||||
|   MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' |\ | ||||
|     xargs echo)" | ||||
| 
 | ||||
| 	# get the pattern for the extensions to MATCH | ||||
| 	MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ | ||||
| 		sed -e 's/\/.*\+//g')" | ||||
|   # get the pattern for the extensions to MATCH | ||||
|   MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ | ||||
|     sed -e 's/\/.*\+//g')" | ||||
|    | ||||
| 	# run through MIME extensions | ||||
| 	for file in \ | ||||
| 		"$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ | ||||
| 	  /usr/share/xdg-sanity/*-mime.toml \ | ||||
| 		/usr/local/share/xdg-sanity/*-mime.toml | ||||
| 	do | ||||
| 		if test -e "$file"; then | ||||
| 			i=$(tomcat replace.urls "$file" | sed 's/	/\n/g' | wc -l) | ||||
| 			while ! [ "$i" = 0 ]; do | ||||
| 				if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then | ||||
| 					MIME="$(tomcat with.mime "$file")" | ||||
| 				fi | ||||
| 				i="$(dc -e "$i 1 - p")" | ||||
| 			done | ||||
| 		fi | ||||
| 	done | ||||
|   # run through MIME extensions | ||||
|   for file in \ | ||||
|     "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ | ||||
|     /usr/share/xdg-sanity/*-mime.toml \ | ||||
|     /usr/local/share/xdg-sanity/*-mime.toml | ||||
|   do | ||||
|     if test -e "$file"; then | ||||
|       i=$(tomcat replace.urls "$file" | sed 's/	/\n/g' | wc -l) | ||||
|       while ! [ "$i" = 0 ]; do | ||||
|         if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then | ||||
|           MIME="$(tomcat with.mime "$file")" | ||||
|         fi | ||||
|         i="$(dc -e "$i 1 - p")" | ||||
|       done | ||||
|     fi | ||||
|   done | ||||
| 
 | ||||
| 	# and the replace extensions | ||||
| 	for file in \ | ||||
| 		"$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ | ||||
| 	  /usr/share/xdg-sanity/*-replace.toml \ | ||||
| 		/usr/local/share/xdg-sanity/*-replace.toml | ||||
| 	do | ||||
| 		if test -e "$file"; then | ||||
| 			i=$(tomcat replace.urls "$file" | sed 's/	/\n/g' | wc -l) | ||||
| 			while ! [ "$i" = 0 ]; do | ||||
| 				if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then | ||||
| 						URL="$(tomcat with.url "$file")" | ||||
| 				fi | ||||
| 				i="$(dc -e "$i 1 - p")" | ||||
| 			done | ||||
| 		fi | ||||
| 	done | ||||
|   # and the replace extensions | ||||
|   for file in \ | ||||
|     "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ | ||||
|     /usr/share/xdg-sanity/*-replace.toml \ | ||||
|     /usr/local/share/xdg-sanity/*-replace.toml | ||||
|   do | ||||
|     if test -e "$file"; then | ||||
|       i="$(tomcat replace.urls "$file" | sed 's/	/\n/g' | wc -l)" | ||||
|       while ! [ "$i" = 0 ]; do | ||||
|         if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then | ||||
|             URL="$(tomcat with.url "$file")" | ||||
|         fi | ||||
|         i="$(dc -e "$i 1 - p")" | ||||
|       done | ||||
|     fi | ||||
|   done | ||||
| 
 | ||||
|   # these commands may fail; this is intentional | ||||
| 	if [ "$MIME" = "text/html" ]; then | ||||
| 		"$BROWSER" "$URL" | ||||
| 	else | ||||
| 		case "$(command -v $XDG_COMMAND)" in | ||||
| 			*/handlr ) | ||||
| 				handlr launch "$MIME" -- "$URL" | ||||
| 				;; | ||||
| 			*/xdg-open ) | ||||
| 			  "$(xdg-mime query default "$MIME")" "$URL" | ||||
| 				;; | ||||
| 			false ) | ||||
| 				exit 69 # sysexits(3) EX_UNAVAILABLE | ||||
| 				;; | ||||
| 		esac | ||||
| 	fi | ||||
| 	shift | ||||
|   if [ "$MIME" = "text/html" ]; then | ||||
|     "$BROWSER" "$URL" | ||||
|   else | ||||
|     case "$(command -v $XDG_COMMAND)" in | ||||
|       */handlr ) | ||||
|         handlr launch "$MIME" -- "$URL" | ||||
|         ;; | ||||
|       */xdg-open ) | ||||
|         "$(xdg-mime query default "$MIME")" "$URL" | ||||
|         ;; | ||||
|       false ) | ||||
|         exit 69 # sysexits(3) EX_UNAVAILABLE | ||||
|         ;; | ||||
|     esac | ||||
|   fi | ||||
|   shift | ||||
| done | ||||
| 
 | ||||
| exit 0 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user