Revision: 2408
http://linuxconsole.svn.sourceforge.net/linuxconsole/?rev=2408&view=rev
Author: skitt
Date: 2011-03-30 20:18:23 +0000 (Wed, 30 Mar 2011)
Log Message:
-----------
Avoid using temporary files, and exit when errors occur (thanks to
Roberto Neri <rn...@li...> for the patch!).
Modified Paths:
--------------
trunk/utils/jscal-restore.in
Modified: trunk/utils/jscal-restore.in
===================================================================
--- trunk/utils/jscal-restore.in 2011-03-28 13:54:29 UTC (rev 2407)
+++ trunk/utils/jscal-restore.in 2011-03-30 20:18:23 UTC (rev 2408)
@@ -1,33 +1,59 @@
-#!/bin/sh
+#!/bin/bash
+if [ -z "$1" ]; then
+ echo "Usage: $0 {device}"
+ echo "Restores the device's calibration."
+ exit 1
+fi
+
if [ ! -x /sbin/udevadm ]; then
echo Restoring joystick configuration requires udev! >&2
+ exit 1
fi
-ident=$(mktemp)
-/sbin/udevadm info -a -n $1 | @@PREFIX@@/share/joystick/ident > $ident
-. $ident
-rm -f $ident
-
STORE=/var/lib/joystick/joystick.state
-# Retrieve the applicable commands
-commands=$(mktemp)
-if [ -f $STORE ]; then
- if [ -z "$NAME" ] && [ -z "$VENDOR" ]; then
- # Use the device name only
- @@PREFIX@@/share/joystick/extract kernel="$DEVICE" < $STORE > $commands
- else
- @@PREFIX@@/share/joystick/extract name="$NAME" serial="$SERIAL" vendor="$VENDOR" product="$PRODUCT" < $STORE > $commands
- fi
+if [ ! -f $STORE ]; then
+ echo No saved joystick configuration\(s\) to restore! >&2
+ exit 1
fi
-# Run the commands if any
-if [ -f $commands ] && [ ! -z "$(cat $commands)" ]; then
- while read command; do
- $command $1
- done < $commands
+DEVICE=""
+NAME=""
+SERIAL=""
+VENDOR=""
+PRODUCT=""
+
+# Backup original $IFS (Internal Field Separator) variable
+OIFS=$IFS
+# Set $IFS to newline only as output from @@PREFIX@@/share/joystick/ident might contain spaces
+# in the NAME value
+IFS=$'\x0A'
+
+for ATTRIBUTE in $( /sbin/udevadm info -a -n $1 | @@PREFIX@@/share/joystick/ident ); do
+ ID=$( echo "$ATTRIBUTE" | cut -f 1 -d = )
+ VALUE=$( echo "$ATTRIBUTE" | cut -f 2 -d \" )
+ case $ID in
+ "DEVICE" ) DEVICE="$VALUE" ;;
+ "NAME" ) NAME="$VALUE" ;;
+ "SERIAL" ) SERIAL="$VALUE" ;;
+ "VENDOR" ) VENDOR="$VALUE" ;;
+ "PRODUCT" ) PRODUCT="$VALUE" ;;
+ esac
+done
+
+# Restore original $IFS
+IFS=$OIFS
+
+# Retrieve the applicable commands
+if [ -z "$NAME" ] && [ -z "$VENDOR" ]; then
+ # Use the device name only
+ cat $STORE | @@PREFIX@@/share/joystick/extract kernel="$DEVICE" | while read COMMAND; do
+ $COMMAND $1
+ done
+else
+ cat $STORE | @@PREFIX@@/share/joystick/extract name="$NAME" serial="$SERIAL" vendor="$VENDOR" product="$PRODUCT" | while read COMMAND; do
+ $COMMAND $1
+ done
fi
-# Delete the generated file
-rm -f $commands
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|