xref: /onnv-gate/usr/src/lib/libshell/common/scripts/shnote.sh (revision 12068:08a39a083754)
18462SApril.Chin@Sun.COM#!/usr/bin/ksh93
28462SApril.Chin@Sun.COM
38462SApril.Chin@Sun.COM#
48462SApril.Chin@Sun.COM# CDDL HEADER START
58462SApril.Chin@Sun.COM#
68462SApril.Chin@Sun.COM# The contents of this file are subject to the terms of the
78462SApril.Chin@Sun.COM# Common Development and Distribution License (the "License").
88462SApril.Chin@Sun.COM# You may not use this file except in compliance with the License.
98462SApril.Chin@Sun.COM#
108462SApril.Chin@Sun.COM# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
118462SApril.Chin@Sun.COM# or http://www.opensolaris.org/os/licensing.
128462SApril.Chin@Sun.COM# See the License for the specific language governing permissions
138462SApril.Chin@Sun.COM# and limitations under the License.
148462SApril.Chin@Sun.COM#
158462SApril.Chin@Sun.COM# When distributing Covered Code, include this CDDL HEADER in each
168462SApril.Chin@Sun.COM# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
178462SApril.Chin@Sun.COM# If applicable, add the following below this CDDL HEADER, with the
188462SApril.Chin@Sun.COM# fields enclosed by brackets "[]" replaced with your own identifying
198462SApril.Chin@Sun.COM# information: Portions Copyright [yyyy] [name of copyright owner]
208462SApril.Chin@Sun.COM#
218462SApril.Chin@Sun.COM# CDDL HEADER END
228462SApril.Chin@Sun.COM#
238462SApril.Chin@Sun.COM
248462SApril.Chin@Sun.COM#
25*12068SRoger.Faulkner@Oracle.COM# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
268462SApril.Chin@Sun.COM#
278462SApril.Chin@Sun.COM
288462SApril.Chin@Sun.COM# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
298462SApril.Chin@Sun.COMexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
308462SApril.Chin@Sun.COM
318462SApril.Chin@Sun.COM# Make sure all math stuff runs in the "C" locale to avoid problems
328462SApril.Chin@Sun.COM# with alternative # radix point representations (e.g. ',' instead of
338462SApril.Chin@Sun.COM# '.' in de_DE.*-locales). This needs to be set _before_ any
348462SApril.Chin@Sun.COM# floating-point constants are defined in this script).
358462SApril.Chin@Sun.COMif [[ "${LC_ALL}" != "" ]] ; then
368462SApril.Chin@Sun.COM    export \
378462SApril.Chin@Sun.COM        LC_MONETARY="${LC_ALL}" \
388462SApril.Chin@Sun.COM        LC_MESSAGES="${LC_ALL}" \
398462SApril.Chin@Sun.COM        LC_COLLATE="${LC_ALL}" \
408462SApril.Chin@Sun.COM        LC_CTYPE="${LC_ALL}"
418462SApril.Chin@Sun.COM        unset LC_ALL
428462SApril.Chin@Sun.COMfi
438462SApril.Chin@Sun.COMexport LC_NUMERIC=C
448462SApril.Chin@Sun.COM
458462SApril.Chin@Sun.COMfunction fatal_error
468462SApril.Chin@Sun.COM{
478462SApril.Chin@Sun.COM	print -u2 "${progname}: $*"
488462SApril.Chin@Sun.COM	exit 1
498462SApril.Chin@Sun.COM}
508462SApril.Chin@Sun.COM
518462SApril.Chin@Sun.COMfunction encode_multipart_form_data
528462SApril.Chin@Sun.COM{
538462SApril.Chin@Sun.COM	nameref formdata="$1"
548462SApril.Chin@Sun.COM	nameref content="formdata.content"
558462SApril.Chin@Sun.COM	integer numformelements=${#formdata.form[*]}
568462SApril.Chin@Sun.COM	integer i
578462SApril.Chin@Sun.COM	typeset tmp
588462SApril.Chin@Sun.COM
598462SApril.Chin@Sun.COM	content=""
608462SApril.Chin@Sun.COM
618462SApril.Chin@Sun.COM	# todo: add support to upload files
628462SApril.Chin@Sun.COM	for (( i=0 ; i < numformelements ; i++ )) ; do
638462SApril.Chin@Sun.COM		nameref element="formdata.form[${i}]"
648462SApril.Chin@Sun.COM
658462SApril.Chin@Sun.COM		content+="--${formdata.boundary}\n"
668462SApril.Chin@Sun.COM		content+="Content-Disposition: form-data; name=\"${element.name}\"\n"
678462SApril.Chin@Sun.COM		content+="\n"
688462SApril.Chin@Sun.COM		# make sure we quote the '\' properly since we pass these data to one instance of
698462SApril.Chin@Sun.COM		# "print" when putting the content on the wire.
708462SApril.Chin@Sun.COM		content+="${element.data//\\/\\\\}\n" # fixme: may need encoding for non-ASCII data
718462SApril.Chin@Sun.COM	done
728462SApril.Chin@Sun.COM
738462SApril.Chin@Sun.COM	# we have to de-quote the content before we can count the real numer of bytes in the payload
748462SApril.Chin@Sun.COM	tmp="$(print -- "${content}")"
758462SApril.Chin@Sun.COM	formdata.content_length=${#tmp}
768462SApril.Chin@Sun.COM
778462SApril.Chin@Sun.COM	# add content tail (which MUST not be added to the content length)
788462SApril.Chin@Sun.COM	content+="--${formdata.boundary}--\n"
798462SApril.Chin@Sun.COM
808462SApril.Chin@Sun.COM	return 0
818462SApril.Chin@Sun.COM}
828462SApril.Chin@Sun.COM
838462SApril.Chin@Sun.COM# parse HTTP return code, cookies etc.
848462SApril.Chin@Sun.COMfunction parse_http_response
858462SApril.Chin@Sun.COM{
868462SApril.Chin@Sun.COM	nameref response="$1"
878462SApril.Chin@Sun.COM	typeset h statuscode statusmsg i
888462SApril.Chin@Sun.COM
898462SApril.Chin@Sun.COM	# we use '\r' as additional IFS to filter the final '\r'
908462SApril.Chin@Sun.COM	IFS=$' \t\r' read -r h statuscode statusmsg  # read HTTP/1.[01] <code>
918462SApril.Chin@Sun.COM	[[ "$h" != ~(Eil)HTTP/.* ]]         && { print -u2 -f $"%s: HTTP/ header missing\n" "$0" ; return 1 ; }
928462SApril.Chin@Sun.COM	[[ "$statuscode" != ~(Elr)[0-9]* ]] && { print -u2 -f $"%s: invalid status code\n"  "$0" ; return 1 ; }
938462SApril.Chin@Sun.COM	response.statuscode="$statuscode"
948462SApril.Chin@Sun.COM	response.statusmsg="$statusmsg"
958462SApril.Chin@Sun.COM
968462SApril.Chin@Sun.COM	# skip remaining headers
978462SApril.Chin@Sun.COM	while IFS='' read -r i ; do
988462SApril.Chin@Sun.COM		[[ "$i" == $'\r' ]] && break
998462SApril.Chin@Sun.COM
1008462SApril.Chin@Sun.COM		# strip '\r' at the end
1018462SApril.Chin@Sun.COM		i="${i/~(Er)$'\r'/}"
1028462SApril.Chin@Sun.COM
1038462SApril.Chin@Sun.COM		case "$i" in
1048462SApril.Chin@Sun.COM			~(Eli)Content-Type:.*)
1058462SApril.Chin@Sun.COM				response.content_type="${i/~(El).*:[[:blank:]]*/}"
1068462SApril.Chin@Sun.COM				;;
1078462SApril.Chin@Sun.COM			~(Eli)Content-Length:[[:blank:]]*[0-9]*)
1088462SApril.Chin@Sun.COM				integer response.content_length="${i/~(El).*:[[:blank:]]*/}"
1098462SApril.Chin@Sun.COM				;;
1108462SApril.Chin@Sun.COM			~(Eli)Transfer-Encoding:.*)
1118462SApril.Chin@Sun.COM				response.transfer_encoding="${i/~(El).*:[[:blank:]]*/}"
1128462SApril.Chin@Sun.COM				;;
1138462SApril.Chin@Sun.COM		esac
1148462SApril.Chin@Sun.COM	done
1158462SApril.Chin@Sun.COM
1168462SApril.Chin@Sun.COM	return 0
1178462SApril.Chin@Sun.COM}
1188462SApril.Chin@Sun.COM
1198462SApril.Chin@Sun.COMfunction cat_http_body
1208462SApril.Chin@Sun.COM{
1218462SApril.Chin@Sun.COM	typeset emode="$1"
1228462SApril.Chin@Sun.COM	typeset hexchunksize="0"
1238462SApril.Chin@Sun.COM	integer chunksize=0
1248462SApril.Chin@Sun.COM
1258462SApril.Chin@Sun.COM	if [[ "${emode}" == "chunked" ]] ; then
1268462SApril.Chin@Sun.COM		while IFS=$'\r' read hexchunksize &&
127*12068SRoger.Faulkner@Oracle.COM			[[ "${hexchunksize}" == ~(Elri)[0-9abcdef]+ ]] &&
128*12068SRoger.Faulkner@Oracle.COM			(( chunksize=$( printf "16#%s\n" "${hexchunksize}" ) )) && (( chunksize > 0 )) ; do
1298462SApril.Chin@Sun.COM			dd bs=1 count="${chunksize}" 2>/dev/null
1308462SApril.Chin@Sun.COM		done
1318462SApril.Chin@Sun.COM	else
1328462SApril.Chin@Sun.COM		cat
1338462SApril.Chin@Sun.COM	fi
1348462SApril.Chin@Sun.COM
1358462SApril.Chin@Sun.COM	return 0
1368462SApril.Chin@Sun.COM}
1378462SApril.Chin@Sun.COM
1388462SApril.Chin@Sun.COMfunction history_write_record
1398462SApril.Chin@Sun.COM{
1408462SApril.Chin@Sun.COM	# rec: history record:
1418462SApril.Chin@Sun.COM	#     rec.title
1428462SApril.Chin@Sun.COM	#     rec.description
1438462SApril.Chin@Sun.COM	#     rec.provider
1448462SApril.Chin@Sun.COM	#     rec.providertoken
1458462SApril.Chin@Sun.COM	#     rec.url
1468462SApril.Chin@Sun.COM	nameref rec="$1"
1478462SApril.Chin@Sun.COM	integer histfd
1488462SApril.Chin@Sun.COM
1498462SApril.Chin@Sun.COM	mkdir -p "${HOME}/.shnote"
1508462SApril.Chin@Sun.COM
1518462SApril.Chin@Sun.COM	{
1528462SApril.Chin@Sun.COM		# write a single-line record which can be read
1538462SApril.Chin@Sun.COM		# as a compound variable back into the shell
1548462SApril.Chin@Sun.COM		printf "title=%q description=%q date=%q provider=%q providertoken=%q url=%q\n" \
1558462SApril.Chin@Sun.COM			"${rec.title}" \
1568462SApril.Chin@Sun.COM			"${rec.description}" \
1578462SApril.Chin@Sun.COM			"$(date)" \
1588462SApril.Chin@Sun.COM			"${rec.provider}" \
1598462SApril.Chin@Sun.COM			"${rec.providertoken}" \
1608462SApril.Chin@Sun.COM			"${rec.url}"
1618462SApril.Chin@Sun.COM	} >>"${history_file}"
1628462SApril.Chin@Sun.COM
1638462SApril.Chin@Sun.COM	return $?
1648462SApril.Chin@Sun.COM}
1658462SApril.Chin@Sun.COM
1668462SApril.Chin@Sun.COMfunction print_history
1678462SApril.Chin@Sun.COM{
1688462SApril.Chin@Sun.COM	integer histfd # http stream number
1698462SApril.Chin@Sun.COM	typeset line
1708462SApril.Chin@Sun.COM
1718462SApril.Chin@Sun.COM	(( $# != 0 && $# != 1 )) && { print -u2 -f $"%s: Wrong number of arguments.\n" "$0" ; return 1 ; }
1728462SApril.Chin@Sun.COM
1738462SApril.Chin@Sun.COM	# default output format is:
1748462SApril.Chin@Sun.COM	# <access url>/<title> <date> <access url>
1758462SApril.Chin@Sun.COM	[[ "$1" == "-l" ]] || printf "# %s\t\t\t\t\t%s\t%s\n" "<url>" "<title>" "<date>"
1768462SApril.Chin@Sun.COM
1778462SApril.Chin@Sun.COM	# no history file ?
1788462SApril.Chin@Sun.COM	if [[ ! -f "${history_file}" ]] ; then
1798462SApril.Chin@Sun.COM		return 0
1808462SApril.Chin@Sun.COM	fi
1818462SApril.Chin@Sun.COM
1828462SApril.Chin@Sun.COM	# open history file
18310898Sroland.mainz@nrubsig.org	redirect {histfd}<> "${history_file}"
18410898Sroland.mainz@nrubsig.org	(( $? != 0 )) && { print -u2 "Could not open history file." ;  return 1 ; }
1858462SApril.Chin@Sun.COM
1868462SApril.Chin@Sun.COM	while read -u${histfd} line ; do
18710898Sroland.mainz@nrubsig.org		compound rec
1888462SApril.Chin@Sun.COM
1898462SApril.Chin@Sun.COM		printf "( %s )\n" "${line}"  | read -C rec
1908462SApril.Chin@Sun.COM
1918462SApril.Chin@Sun.COM		if [[ "$1" == "-l" ]] ; then
1928462SApril.Chin@Sun.COM			print -- "${rec}"
1938462SApril.Chin@Sun.COM		else
1948462SApril.Chin@Sun.COM			printf "%q\t%q\t%q\n" "${rec.url}" "${rec.title}" "${rec.date}"
1958462SApril.Chin@Sun.COM		fi
1968462SApril.Chin@Sun.COM
1978462SApril.Chin@Sun.COM		unset rec
1988462SApril.Chin@Sun.COM	done
1998462SApril.Chin@Sun.COM
2008462SApril.Chin@Sun.COM	# close history file
2018462SApril.Chin@Sun.COM	redirect {histfd}<&-
2028462SApril.Chin@Sun.COM
2038462SApril.Chin@Sun.COM	return 0
2048462SApril.Chin@Sun.COM}
2058462SApril.Chin@Sun.COM
2068462SApril.Chin@Sun.COMfunction put_note_pastebin_ca
2078462SApril.Chin@Sun.COM{
2088462SApril.Chin@Sun.COM	# key to autheticate this script against pastebin.ca
2098462SApril.Chin@Sun.COM	typeset -r pastebin_ca_key="9CFXFyeNC3iga/vthok75kTBu5kSSLPD"
2108462SApril.Chin@Sun.COM	# site setup
2118462SApril.Chin@Sun.COM	typeset url_host="opensolaris.pastebin.ca"
2128462SApril.Chin@Sun.COM	typeset url_path="/quiet-paste.php?api=${pastebin_ca_key}"
2138462SApril.Chin@Sun.COM	typeset url="http://${url_host}${url_path}"
2148462SApril.Chin@Sun.COM	integer netfd # http stream number
21510898Sroland.mainz@nrubsig.org	compound httpresponse
2168462SApril.Chin@Sun.COM
2178462SApril.Chin@Sun.COM	(( $# != 1 )) && { print -u2 -f $"%s: Wrong number of arguments.\n" "$0" ; return 1 ; }
2188462SApril.Chin@Sun.COM	(( ${#1} == 0 )) && { print -u2 -f $"%s: No data.\n" "$0" ; return 1 ; }
2198462SApril.Chin@Sun.COM
2208462SApril.Chin@Sun.COM	# argument for "encode_multipart_form_data"
22110898Sroland.mainz@nrubsig.org	compound mimeform=(
2228462SApril.Chin@Sun.COM		# input
2238462SApril.Chin@Sun.COM		typeset boundary
2248462SApril.Chin@Sun.COM		typeset -a form
2258462SApril.Chin@Sun.COM		# output
2268462SApril.Chin@Sun.COM		typeset content
2278462SApril.Chin@Sun.COM		integer content_length
2288462SApril.Chin@Sun.COM	)
2298462SApril.Chin@Sun.COM
2308462SApril.Chin@Sun.COM	typeset request=""
2318462SApril.Chin@Sun.COM	typeset content=""
2328462SApril.Chin@Sun.COM
2338462SApril.Chin@Sun.COM	typeset -r boundary="--------shnote_${RANDOM}_Xfish_${RANDOM}_Yeats_${RANDOM}_Zchicken_${RANDOM}monster_--------"
2348462SApril.Chin@Sun.COM
2358462SApril.Chin@Sun.COM	mimeform.boundary="${boundary}"
2368462SApril.Chin@Sun.COM	mimeform.form=( # we use explicit index numbers since we rely on them below when filling the history
2378462SApril.Chin@Sun.COM		[0]=( name="name"		data="${LOGNAME}" )
2388462SApril.Chin@Sun.COM		[1]=( name="expiry"		data="Never" )
2398462SApril.Chin@Sun.COM		[2]=( name="type"		data="1" )
2408462SApril.Chin@Sun.COM		[3]=( name="description"	data="logname=${LOGNAME};hostname=$(hostname);date=$(date)" )
2418462SApril.Chin@Sun.COM		[4]=( name="content"		data="$1" )
2428462SApril.Chin@Sun.COM	)
2438462SApril.Chin@Sun.COM	encode_multipart_form_data mimeform
2448462SApril.Chin@Sun.COM
2458462SApril.Chin@Sun.COM	content="${mimeform.content}"
2468462SApril.Chin@Sun.COM
2478462SApril.Chin@Sun.COM	request="POST ${url_path} HTTP/1.1\r\n"
2488462SApril.Chin@Sun.COM	request+="Host: ${url_host}\r\n"
2498462SApril.Chin@Sun.COM	request+="User-Agent: ${http_user_agent}\r\n"
2508462SApril.Chin@Sun.COM	request+="Connection: close\r\n"
2518462SApril.Chin@Sun.COM	request+="Content-Type: multipart/form-data; boundary=${boundary}\r\n"
2528462SApril.Chin@Sun.COM	request+="Content-Length: $(( mimeform.content_length ))\r\n"
2538462SApril.Chin@Sun.COM
25410898Sroland.mainz@nrubsig.org	redirect {netfd}<> "/dev/tcp/${url_host}/80"
25510898Sroland.mainz@nrubsig.org	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ;  return 1 ; }
2568462SApril.Chin@Sun.COM
2578462SApril.Chin@Sun.COM	# send http post
2588462SApril.Chin@Sun.COM	{
2598462SApril.Chin@Sun.COM		print -n -- "${request}\r\n"
2608462SApril.Chin@Sun.COM		print -n -- "${content}\r\n"
2618462SApril.Chin@Sun.COM	}  >&${netfd}
2628462SApril.Chin@Sun.COM
2638462SApril.Chin@Sun.COM	# process reply
2648462SApril.Chin@Sun.COM	parse_http_response httpresponse <&${netfd}
2658462SApril.Chin@Sun.COM	response="$(cat_http_body "${httpresponse.transfer_encoding}" <&${netfd})"
2668462SApril.Chin@Sun.COM
2678462SApril.Chin@Sun.COM	# close connection
2688462SApril.Chin@Sun.COM	redirect {netfd}<&-
2698462SApril.Chin@Sun.COM
2708462SApril.Chin@Sun.COM	if [[ "${response}" == ~(E).*SUCCESS.* ]] ; then
2718462SApril.Chin@Sun.COM		typeset response_token="${response/~(E).*SUCCESS:/}"
2728462SApril.Chin@Sun.COM
2738462SApril.Chin@Sun.COM		printf "SUCCESS: http://opensolaris.pastebin.ca/%s\n" "${response_token}"
2748462SApril.Chin@Sun.COM
2758462SApril.Chin@Sun.COM		# write history entry
27610898Sroland.mainz@nrubsig.org		compound histrec=(
2778462SApril.Chin@Sun.COM			title="${mimeform.form[0].data}"
2788462SApril.Chin@Sun.COM			description="${mimeform.form[3].data}"
2798462SApril.Chin@Sun.COM			providertoken="${response_token}"
2808462SApril.Chin@Sun.COM			provider="opensolaris.pastebin.ca"
2818462SApril.Chin@Sun.COM			url="http://opensolaris.pastebin.ca/${response_token}"
2828462SApril.Chin@Sun.COM		)
2838462SApril.Chin@Sun.COM
2848462SApril.Chin@Sun.COM		history_write_record histrec
2858462SApril.Chin@Sun.COM		return 0
2868462SApril.Chin@Sun.COM	else
2878462SApril.Chin@Sun.COM		printf "ERROR: %s\n" "${response}"
2888462SApril.Chin@Sun.COM		return 1
2898462SApril.Chin@Sun.COM	fi
2908462SApril.Chin@Sun.COM
2918462SApril.Chin@Sun.COM	# not reached
2928462SApril.Chin@Sun.COM}
2938462SApril.Chin@Sun.COM
2948462SApril.Chin@Sun.COMfunction get_note_pastebin_ca
2958462SApril.Chin@Sun.COM{
2968462SApril.Chin@Sun.COM	typeset recordname="$1"
2978462SApril.Chin@Sun.COM	integer netfd # http stream number
2988462SApril.Chin@Sun.COM
2998462SApril.Chin@Sun.COM	(( $# != 1 )) && { print -u2 -f $"%s: No key or key URL.\n" "$0" ; return 1 ; }
3008462SApril.Chin@Sun.COM
3018462SApril.Chin@Sun.COM	case "${recordname}" in
3028462SApril.Chin@Sun.COM		~(Elr)[0-9][0-9]*)
3038462SApril.Chin@Sun.COM			# pass-through
3048462SApril.Chin@Sun.COM			;;
3058462SApril.Chin@Sun.COM		~(Elr)http://opensolaris.pastebin.ca/raw/[0-9]*)
3068462SApril.Chin@Sun.COM			recordname="${recordname/~(El)http:\/\/opensolaris.pastebin.ca\/raw\//}"
3078462SApril.Chin@Sun.COM			;;
3088462SApril.Chin@Sun.COM		~(Elr)http://opensolaris.pastebin.ca/[0-9]*)
3098462SApril.Chin@Sun.COM			recordname="${recordname/~(El)http:\/\/opensolaris.pastebin.ca\//}"
3108462SApril.Chin@Sun.COM			;;
3118462SApril.Chin@Sun.COM		*)
3128462SApril.Chin@Sun.COM			fatal_error $"Unsupported record name ${recordname}."
3138462SApril.Chin@Sun.COM	esac
3148462SApril.Chin@Sun.COM
3158462SApril.Chin@Sun.COM	print -u2 -f "# Record name is '%s'\n" "${recordname}"
3168462SApril.Chin@Sun.COM
3178462SApril.Chin@Sun.COM	typeset url_host="opensolaris.pastebin.ca"
3188462SApril.Chin@Sun.COM	typeset url_path="/raw/${recordname}"
3198462SApril.Chin@Sun.COM	typeset url="http://${url_host}${url_path}"
3208462SApril.Chin@Sun.COM	# I hereby curse Solaris for not having an entry for "http" in /etc/services
3218462SApril.Chin@Sun.COM
3228462SApril.Chin@Sun.COM	# open TCP channel
32310898Sroland.mainz@nrubsig.org	redirect {netfd}<> "/dev/tcp/${url_host}/80"
32410898Sroland.mainz@nrubsig.org	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ; return 1 ; }
3258462SApril.Chin@Sun.COM
3268462SApril.Chin@Sun.COM	# send HTTP request
3278462SApril.Chin@Sun.COM	request="GET ${url_path} HTTP/1.1\r\n"
3288462SApril.Chin@Sun.COM	request+="Host: ${url_host}\r\n"
3298462SApril.Chin@Sun.COM	request+="User-Agent: ${http_user_agent}\r\n"
3308462SApril.Chin@Sun.COM	request+="Connection: close\r\n"
3318462SApril.Chin@Sun.COM	print -u${netfd} -- "${request}\r\n"
3328462SApril.Chin@Sun.COM
3338462SApril.Chin@Sun.COM	# collect response and send it to stdout
3348462SApril.Chin@Sun.COM	parse_http_response httpresponse <&${netfd}
3358462SApril.Chin@Sun.COM	cat_http_body "${httpresponse.transfer_encoding}" <&${netfd}
3368462SApril.Chin@Sun.COM
3378462SApril.Chin@Sun.COM	# close connection
3388462SApril.Chin@Sun.COM	redirect {netfd}<&-
3398462SApril.Chin@Sun.COM
3408462SApril.Chin@Sun.COM	print # add newline
3418462SApril.Chin@Sun.COM
3428462SApril.Chin@Sun.COM	return 0
3438462SApril.Chin@Sun.COM}
3448462SApril.Chin@Sun.COM
3458462SApril.Chin@Sun.COMfunction usage
3468462SApril.Chin@Sun.COM{
3478462SApril.Chin@Sun.COM	OPTIND=0
3488462SApril.Chin@Sun.COM	getopts -a "${progname}" "${USAGE}" OPT '-?'
3498462SApril.Chin@Sun.COM	exit 2
3508462SApril.Chin@Sun.COM}
3518462SApril.Chin@Sun.COM
3528462SApril.Chin@Sun.COM# program start
3538462SApril.Chin@Sun.COMbuiltin basename
3548462SApril.Chin@Sun.COMbuiltin cat
3558462SApril.Chin@Sun.COMbuiltin date
3568462SApril.Chin@Sun.COMbuiltin uname
3578462SApril.Chin@Sun.COM
3588462SApril.Chin@Sun.COMtypeset progname="${ basename "${0}" ; }"
3598462SApril.Chin@Sun.COM
3608462SApril.Chin@Sun.COM# HTTP protocol client identifer
361*12068SRoger.Faulkner@Oracle.COMtypeset -r http_user_agent="shnote/ksh93 (2010-03-27; $(uname -s -r -p))"
3628462SApril.Chin@Sun.COM
3638462SApril.Chin@Sun.COM# name of history log (the number after "history" is some kind of version
3648462SApril.Chin@Sun.COM# counter to handle incompatible changes to the history file format)
3658462SApril.Chin@Sun.COMtypeset -r history_file="${HOME}/.shnote/history0.txt"
3668462SApril.Chin@Sun.COM
3678462SApril.Chin@Sun.COMtypeset -r shnote_usage=$'+
368*12068SRoger.Faulkner@Oracle.COM[-?\n@(#)\$Id: shnote (Roland Mainz) 2010-03-27 \$\n]
3698462SApril.Chin@Sun.COM[-author?Roland Mainz <roland.mainz@nrubsig.org>]
3708462SApril.Chin@Sun.COM[+NAME?shnote - read/write text data to internet clipboards]
3718462SApril.Chin@Sun.COM[+DESCRIPTION?\bshnote\b is a small utilty which can read and write text
3728462SApril.Chin@Sun.COM	data to internet "clipboards" such as opensolaris.pastebin.ca.]
3738462SApril.Chin@Sun.COM[+?The first arg \bmethod\b describes one of the methods, "put" saves a string
3748462SApril.Chin@Sun.COM	to the internet clipboard, returning an identifer and the full URL
3758462SApril.Chin@Sun.COM	where the data are stored. The method "get" retrives the raw
3768462SApril.Chin@Sun.COM	information using the identifer from the previous "put" action.
3778462SApril.Chin@Sun.COM	The method "hist" prints a history of transactions created with the
3788462SApril.Chin@Sun.COM	"put" method and the keys to retrive them again using the "get" method.]
3798462SApril.Chin@Sun.COM[+?The second arg \bstring\b contains either the string data which should be
3808462SApril.Chin@Sun.COM	stored on the clipboard using the "put" method, the "get" method uses
3818462SApril.Chin@Sun.COM	this information as identifer to retrive the raw data from the
3828462SApril.Chin@Sun.COM	clipboard.]
3838462SApril.Chin@Sun.COM
3848462SApril.Chin@Sun.COMmethod [ string ]
3858462SApril.Chin@Sun.COM
3868462SApril.Chin@Sun.COM[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtwitter\b(1), \bshtinyurl\b(1), http://opensolaris.pastebin.ca]
3878462SApril.Chin@Sun.COM'
3888462SApril.Chin@Sun.COM
3898462SApril.Chin@Sun.COMwhile getopts -a "${progname}" "${shnote_usage}" OPT ; do
3908462SApril.Chin@Sun.COM#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
3918462SApril.Chin@Sun.COM	case ${OPT} in
3928462SApril.Chin@Sun.COM		*)	usage ;;
3938462SApril.Chin@Sun.COM	esac
3948462SApril.Chin@Sun.COMdone
3958462SApril.Chin@Sun.COMshift $((OPTIND-1))
3968462SApril.Chin@Sun.COM
3978462SApril.Chin@Sun.COM# expecting at least one more argument, the single method below will do
3988462SApril.Chin@Sun.COM# the checks for more arguments if needed ("put" and "get" methods need
3998462SApril.Chin@Sun.COM# at least one extra argument, "hist" none).
4008462SApril.Chin@Sun.COM(($# >= 1)) || usage
4018462SApril.Chin@Sun.COM
4028462SApril.Chin@Sun.COMtypeset method="$1"
4038462SApril.Chin@Sun.COMshift
4048462SApril.Chin@Sun.COM
4058462SApril.Chin@Sun.COMcase "${method}" in
4068462SApril.Chin@Sun.COM	put)	put_note_pastebin_ca "$@" ; exit $? ;;
4078462SApril.Chin@Sun.COM	get)	get_note_pastebin_ca "$@" ; exit $? ;;
4088462SApril.Chin@Sun.COM	hist)	print_history "$@"        ; exit $? ;;
4098462SApril.Chin@Sun.COM	*)	usage ;;
4108462SApril.Chin@Sun.COMesac
4118462SApril.Chin@Sun.COM
4128462SApril.Chin@Sun.COMfatal_error $"not reached."
4138462SApril.Chin@Sun.COM# EOF.
414