#!/usr/bin/perl # # clbc This script is a command-line front-end to the bc calculator. # # usage: # clbc [-rc file | -norc | -help | -Version] # # Revision History # 1.0 Initial revision. 141031 # 1.1 Added license info. 180616 # # Copyright 2014 Wayne Morrison. All rights reserved. # Written by Wayne Morrison, 141031. # use strict; use Getopt::Long qw(:config no_ignore_case_always no_getopt_compat); # # Version information. # my $NAME = "clbc"; my $VERS = "$NAME version: 1.1"; ############################################################################ # # Options fields. # my %opts = (); # Options. # # Command line arguments. # my @opts = ( 'rc=s', # Start-up file. 'norc', # Don't use a start-up file. 'help', # Give a help message. 'Version', # Display the program version. ); my $rcfile = ''; # User's start-up file. ############################################################################ my $BCCMD = "bc -q"; # bc command to execute. my $BCRC = ".bcrc"; # Default start-up file. ############################################################################ main(); exit(0); #----------------------------------------------------------------------------- # Routine: main() # # Purpose: Do everything. # sub main { $| = 1; # # Munch on the options and arguments. # optsandargs(); # # Send command-line arguments to bc. # calcer(); } #---------------------------------------------------------------------- # Routine: optsandargs() # # Purpose: Parse the command line for options. # sub optsandargs { # # Parse the options. # GetOptions(\%opts,@opts) || usage(); # # Check for some immediate-action options. # usage() if(defined($opts{'help'})); version() if(defined($opts{'Version'})); # # Figure out what to use for bc's .rc file. # if(defined($opts{'rc'})) { # # If the user-specified rc file doesn't exist, # we'll give an error and exit. # $rcfile = $opts{'rc'}; if(!-e $rcfile) { print STDERR "rc file \"$rcfile\" does not exist\n"; exit(1); } } else { # # Build the default rc file. If it doesn't exist, # we'll reset and not use an rc file at all. # $rcfile = glob("~") . "/$BCRC"; $rcfile = '' if(! -e $rcfile); } # # Reset things if the -norc option was given. # $rcfile = '' if(defined($opts{'norc'})); } #---------------------------------------------------------------------- # Routine: calcer() # # Purpose: Blob all the command-line arguments together and # send them to an executing bc. # sub calcer { my $bccmd; # bc command to execute. my $cmdline; # Joined-up arguments. # # Build one long line from the command arguments. # $cmdline = join(' ', @ARGV); # # Build the bc command line to execute. # $bccmd = "$BCCMD $rcfile"; # # Pipe the command line to bc. # open(BC, "| $bccmd"); print BC "$cmdline\n"; close(BC); } #---------------------------------------------------------------------- # Routine: version() # # Purpose: Print the version number(s) and exit. # sub version { print STDERR "$VERS\n"; exit(0); } #---------------------------------------------------------------------- # Routine: usage() # # Purpose: Give usage message and exit. # sub usage { print STDERR "usage: clbc [options] \n"; print STDERR "\n"; print STDERR "\twhere [options] are:\n"; print STDERR "\t\t-help\n"; print STDERR "\t\t-Version\n"; exit(0); } 1; ############################################################################## =pod =head1 NAME B - command-line front-end to B =head1 SYNOPSIS clbc [options] =head1 DESCRIPTION B is a command-line front-end to the B calculator. B commands given on the command line will be passed to B for execution. This does very little extra work, but make command-line usage of B a little easier. The following two commands are equivalent: $ echo 5+25 \; '200 * 32' | bc -q 30 6400 $ clbc 5+25 \; '200 * 32' 30 6400 B will read and execute command files named on the B command line. B supports this in two ways. First, if a B<.bcrc> file is in the user's home directory, it will be executed by B prior to running the B commands on the B command line. Second, the I<-rc> option allows a specific file to be function as the B<.bcrc> file. =head1 OPTIONS B takes the following options: =over 4 =item I<-norc> This option prevents an I file from being included on the command line. It takes precedence over the I<-rc> option. =item I<-rc rcfile> This option specifies an I file, whose contents will be executed prior to the B commands given on the command line. =item I<-Version> Display the version information for B. =item I<-help> Display a help message. =back =head1 LICENSE Copyright 2014 Wayne Morrison Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =head1 AUTHOR Wayne Morrison, wayne@waynemorrison.com =head1 SEE ALSO B =cut