xref: /titanic_52/usr/src/cmd/ast/libshell/common/scripts/shcalc.sh (revision 906afcb89d0412cc073b95c2d701a804a8cdb62c)
1*906afcb8SAndy Fiddaman#!/usr/bin/ksh93
2*906afcb8SAndy Fiddaman
3*906afcb8SAndy Fiddaman#
4*906afcb8SAndy Fiddaman# CDDL HEADER START
5*906afcb8SAndy Fiddaman#
6*906afcb8SAndy Fiddaman# The contents of this file are subject to the terms of the
7*906afcb8SAndy Fiddaman# Common Development and Distribution License (the "License").
8*906afcb8SAndy Fiddaman# You may not use this file except in compliance with the License.
9*906afcb8SAndy Fiddaman#
10*906afcb8SAndy Fiddaman# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*906afcb8SAndy Fiddaman# or http://www.opensolaris.org/os/licensing.
12*906afcb8SAndy Fiddaman# See the License for the specific language governing permissions
13*906afcb8SAndy Fiddaman# and limitations under the License.
14*906afcb8SAndy Fiddaman#
15*906afcb8SAndy Fiddaman# When distributing Covered Code, include this CDDL HEADER in each
16*906afcb8SAndy Fiddaman# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*906afcb8SAndy Fiddaman# If applicable, add the following below this CDDL HEADER, with the
18*906afcb8SAndy Fiddaman# fields enclosed by brackets "[]" replaced with your own identifying
19*906afcb8SAndy Fiddaman# information: Portions Copyright [yyyy] [name of copyright owner]
20*906afcb8SAndy Fiddaman#
21*906afcb8SAndy Fiddaman# CDDL HEADER END
22*906afcb8SAndy Fiddaman#
23*906afcb8SAndy Fiddaman
24*906afcb8SAndy Fiddaman#
25*906afcb8SAndy Fiddaman# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
26*906afcb8SAndy Fiddaman#
27*906afcb8SAndy Fiddaman
28*906afcb8SAndy Fiddaman#
29*906afcb8SAndy Fiddaman# shcalc - small shell-based calculator
30*906afcb8SAndy Fiddaman#
31*906afcb8SAndy Fiddaman
32*906afcb8SAndy Fiddaman# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
33*906afcb8SAndy Fiddamanexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
34*906afcb8SAndy Fiddaman
35*906afcb8SAndy Fiddaman# Make sure all math stuff runs in the "C" locale to avoid problems
36*906afcb8SAndy Fiddaman# with alternative # radix point representations (e.g. ',' instead of
37*906afcb8SAndy Fiddaman# '.' in de_DE.*-locales). This needs to be set _before_ any
38*906afcb8SAndy Fiddaman# floating-point constants are defined in this script).
39*906afcb8SAndy Fiddamanif [[ "${LC_ALL}" != "" ]] ; then
40*906afcb8SAndy Fiddaman    export \
41*906afcb8SAndy Fiddaman        LC_MONETARY="${LC_ALL}" \
42*906afcb8SAndy Fiddaman        LC_MESSAGES="${LC_ALL}" \
43*906afcb8SAndy Fiddaman        LC_COLLATE="${LC_ALL}" \
44*906afcb8SAndy Fiddaman        LC_CTYPE="${LC_ALL}"
45*906afcb8SAndy Fiddaman        unset LC_ALL
46*906afcb8SAndy Fiddamanfi
47*906afcb8SAndy Fiddamanexport LC_NUMERIC=C
48*906afcb8SAndy Fiddaman
49*906afcb8SAndy Fiddamanfunction fatal_error
50*906afcb8SAndy Fiddaman{
51*906afcb8SAndy Fiddaman	print -u2 "${progname}: $*"
52*906afcb8SAndy Fiddaman	exit 1
53*906afcb8SAndy Fiddaman}
54*906afcb8SAndy Fiddaman
55*906afcb8SAndy Fiddamanfunction do_calculate
56*906afcb8SAndy Fiddaman{
57*906afcb8SAndy Fiddaman	typeset calcline="$1"
58*906afcb8SAndy Fiddaman	float x=0.0
59*906afcb8SAndy Fiddaman
60*906afcb8SAndy Fiddaman	printf "(( x=( %s ) ))\n" "${calcline}" | source /dev/stdin
61*906afcb8SAndy Fiddaman	if (( $? != 0 )) ; then
62*906afcb8SAndy Fiddaman		print -f $"%s: Syntax error in %s\n" "${progname}" "${calcline}"
63*906afcb8SAndy Fiddaman		return 1
64*906afcb8SAndy Fiddaman	fi
65*906afcb8SAndy Fiddaman
66*906afcb8SAndy Fiddaman	printf "%s == %.40g\n" "${calcline}" x
67*906afcb8SAndy Fiddaman
68*906afcb8SAndy Fiddaman	return 0
69*906afcb8SAndy Fiddaman}
70*906afcb8SAndy Fiddaman
71*906afcb8SAndy Fiddamanfunction usage
72*906afcb8SAndy Fiddaman{
73*906afcb8SAndy Fiddaman	OPTIND=0
74*906afcb8SAndy Fiddaman	getopts -a "${progname}" "${shcalc_usage}" OPT '-?'
75*906afcb8SAndy Fiddaman	exit 2
76*906afcb8SAndy Fiddaman}
77*906afcb8SAndy Fiddaman
78*906afcb8SAndy Fiddaman# program start
79*906afcb8SAndy Fiddaman# (be carefull with builtins here - they are unconditionally available
80*906afcb8SAndy Fiddaman# in the shell's "restricted" mode)
81*906afcb8SAndy Fiddamanbuiltin basename
82*906afcb8SAndy Fiddamanbuiltin sum
83*906afcb8SAndy Fiddaman
84*906afcb8SAndy Fiddamantypeset progname="${ basename "${0}" ; }"
85*906afcb8SAndy Fiddaman
86*906afcb8SAndy Fiddamantypeset -r shcalc_usage=$'+
87*906afcb8SAndy Fiddaman[-?\n@(#)\$Id: shcalc (Roland Mainz) 2008-11-03 \$\n]
88*906afcb8SAndy Fiddaman[-author?Roland Mainz <roland.mainz@nrubsig.org>]
89*906afcb8SAndy Fiddaman[+NAME?shcalc - simple shell calculator]
90*906afcb8SAndy Fiddaman[+DESCRIPTION?\bsshcalc\b is a small calculator application which
91*906afcb8SAndy Fiddaman	prints the results of ISO C99 math expressions read from either
92*906afcb8SAndy Fiddaman	arguments or stdin if no arguments are given.]
93*906afcb8SAndy Fiddaman[+SEE ALSO?\bksh93\b(1),\bceil\b(3M), \bcopysign\b(3M), \bcos\b(3M),
94*906afcb8SAndy Fiddaman	\bcosh\b(3M), \berf\b(3M), \berfc\b(3M), \bexp\b(3M),
95*906afcb8SAndy Fiddaman	\bexp2\b(3M), \bexpm1\b(3M), \bfabs abs\b(3M), \bfdim\b(3M),
96*906afcb8SAndy Fiddaman	\bfinite\b(3M), \bfloor int\b(3M), \bfma\b(3M), \bfmax\b(3M), \bfmin\b(3M),
97*906afcb8SAndy Fiddaman	\bfmod\b(3M), \bfpclassify\b(3M), \bhypot\b(3M), \bilogb\b(3M),
98*906afcb8SAndy Fiddaman	\bisfinite\b(3M), \bisgreater\b(3M), \bisgreaterequal\b(3M), \bisinf\b(3M),
99*906afcb8SAndy Fiddaman	\bisless\b(3M), \bislessequal\b(3M), \bislessgreater\b(3M), \bisnan\b(3M),
100*906afcb8SAndy Fiddaman	\bisnormal\b(3M), \bissubnormal\b(3M), \bisunordered\b(3M), \biszero\b(3M),
101*906afcb8SAndy Fiddaman	\blgamma\b(3M), \blog\b(3M), \blog1p\b(3M), \blog2\b(3M),
102*906afcb8SAndy Fiddaman	\blogb\b(3M), \bnearbyint\b(3M), \bnextafter\b(3M), \bnexttoward\b(3M),
103*906afcb8SAndy Fiddaman	\bpow\b(3M), \bremainder\b(3M), \brint\b(3M), \bround\b(3M),
104*906afcb8SAndy Fiddaman	\bscalb\b(3M), \bscalbn\b(3M), \bsignbit\b(3M), \bsin\b(3M),
105*906afcb8SAndy Fiddaman	\bsinh\b(3M), \bsqrt\b(3M), \btan\b(3M), \btanh\b(3M),
106*906afcb8SAndy Fiddaman	\btgamma\b(3M), \btrunc\b(3M)]
107*906afcb8SAndy Fiddaman'
108*906afcb8SAndy Fiddamanwhile getopts -a "${progname}" "${shcalc_usage}" OPT ; do
109*906afcb8SAndy Fiddaman#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
110*906afcb8SAndy Fiddaman	case ${OPT} in
111*906afcb8SAndy Fiddaman		*)	usage ;;
112*906afcb8SAndy Fiddaman	esac
113*906afcb8SAndy Fiddamandone
114*906afcb8SAndy Fiddamanshift $((OPTIND-1))
115*906afcb8SAndy Fiddaman
116*906afcb8SAndy Fiddamaninteger res
117*906afcb8SAndy Fiddaman
118*906afcb8SAndy Fiddamanif (( $# == 0 )) ; then
119*906afcb8SAndy Fiddaman	# No arguments ? Switch to interactive mode...
120*906afcb8SAndy Fiddaman
121*906afcb8SAndy Fiddaman	# make sure "read" below uses "gmacs"-like editor keys and "multiline" mode
122*906afcb8SAndy Fiddaman
123*906afcb8SAndy Fiddaman	set -o gmacs
124*906afcb8SAndy Fiddaman	set -o multiline
125*906afcb8SAndy Fiddaman
126*906afcb8SAndy Fiddaman	while read "calcline?calc> " ; do
127*906afcb8SAndy Fiddaman		# quit ?
128*906afcb8SAndy Fiddaman		[[ "${calcline}" == ~(Elri)(exit|quit|eof) ]] && break
129*906afcb8SAndy Fiddaman
130*906afcb8SAndy Fiddaman		# empty line ?
131*906afcb8SAndy Fiddaman		[[ "${calcline}" == ~(Elri)([[:space:]]*) ]] && continue
132*906afcb8SAndy Fiddaman
133*906afcb8SAndy Fiddaman		do_calculate "$calcline"
134*906afcb8SAndy Fiddaman		(( res=$? ))
135*906afcb8SAndy Fiddaman	done
136*906afcb8SAndy Fiddaman
137*906afcb8SAndy Fiddaman	exit ${res}
138*906afcb8SAndy Fiddamanelse
139*906afcb8SAndy Fiddaman	while (( $# > 0 )) ; do
140*906afcb8SAndy Fiddaman		do_calculate "$1"
141*906afcb8SAndy Fiddaman		(( res=$? ))
142*906afcb8SAndy Fiddaman		shift
143*906afcb8SAndy Fiddaman	done
144*906afcb8SAndy Fiddaman
145*906afcb8SAndy Fiddaman	exit ${res}
146*906afcb8SAndy Fiddamanfi
147*906afcb8SAndy Fiddaman
148*906afcb8SAndy Fiddaman# not reached
149*906afcb8SAndy Fiddaman
150*906afcb8SAndy Fiddaman# EOF.
151