#!/bin/sh
#
# This file is in the public domain
# Process options - or how to do getopt(3) in shell scripts ;-)
DEBUG=0
SINGLE=0
USAGE=0
PIDFILE=
while [ -n "$1" ] # Loop as long as $1 is nonempty
do
case $1 in
-d)
DEBUG=1
shift
;;
-s)
SINGLE=1
shift
;;
-o)
# Used to redirect fd 2 (where debugging output
# normally goes) to a file - same as server itself
if [ ! "$2" = "" ]
then
exec 2>$2
shift;
else
echo ERROR: Option -o requires an argument 1>&2
USAGE=1
fi
shift
;;
-p)
# Used to write pid into a file, for log rotation
if [ ! "$2" = "" ]
then
PIDFILE=$2
shift
else
echo ERROR: Option -p requires an argument 1>&2
USAGE=1
fi
shift
;;
-h)
USAGE=1
shift
;;
*)
# If this is not the last argument, it's an
# unknown option - complain. Otherwise it's
# the positional argument, and we're done
# with the loop.
if [ -n "$2" ] # If $2 is nonempty, $1 is not last
then
echo ERROR: Unknown option $1 1>&2
USAGE=1
shift
else
break
fi
;;
esac
done
if [ $USAGE = 1 ]
then
echo "Usage: radlogger [-d][is][-o file][-p pidfile] destination" 1>&2
echo " radlogger -h" 1>&2
exit 1
fi
if [ -n "$1" ]
then
# Open output file (positional arg) for append on fd 3, reopen at HUP
DESTFILE=$1
exec 3>>$DESTFILE
trap "exec 3>>$DESTFILE" HUP
else
# Otherwise send output to stderr
exec 3>&2
fi
[ -n "$PIDFILE" ] && echo $$ >$PIDFILE
trap "echo $0: got SIGTERM 1>&2 ; [ -n "\""$PIDFILE"\"" ] && rm -f $PIDFILE ; exit 2" TERM
trap "echo $0: got SIGPIPE 1>&2 ; exit 3" PIPE
# Loop, answering requests
while :
do
# Read message
OUTPUT=
IFS='='
while read ATR VAL && [ -n "$VAL" ]
do
IFS=' '
if [ "str" = "$ATR" ]
then
# Remove quotes
eval VAL=$VAL
# Add header from 'str' attribute in front
OUTPUT="$VAL
$OUTPUT"
else
# Add pair. Note the real tab before $ATR
OUTPUT="$OUTPUT $ATR = $VAL
"
fi
IFS='='
done
IFS=' '
# Show debugging output if requested
[ $DEBUG = 1 ] && echo "radlogger[$$]: $OUTPUT" 1>&2
# Log output to file
if [ $SINGLE = 1 ]; then
echo -n "$OUTPUT" 1>&3
else
echo "$OUTPUT" 1>&3
fi
# Reply
# sleep 1 # testing only - don't worry, we're race-free ;-)
echo "int=1
"
done
syntax highlighted by Code2HTML, v. 0.9.1