All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arkadiusz Miskiewicz <arekm@pld-linux.org>
To: linux-hotplug@vger.kernel.org
Subject: bashizm in hotplug scripts (including 20040401)
Date: Fri, 02 Apr 2004 00:11:45 +0000	[thread overview]
Message-ID: <200404020211.45303.arekm@pld-linux.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 1317 bytes --]

Hi,

hotplug package is seriously broken and won't work on POSIX/SUSv3 
compilant /bin/sh shells.

$ echo $((0xff))
ksh: 0xff: bad number `0xff'

There is a lot of hex to dec conversions using bash only function in hotplug, 
example:
echo $((0xff))

Under ksh (pdksh)
$ echo $((0xff))
ksh: 0xff: bad number `0xff'

There is more portable version
hex=0xff
echo "$((16#`echo "$hex" | sed -e 's#^0x##g'`))"

it works well on zsh, pdksh, bash in their enhaced mode but for example on 
pdksh run in sh mode it overflows - example:
[arekm@mobarm arekm]$ echo $((16#ff))
255
[arekm@mobarm arekm]$ echo $((16#ffffffff))
-1

pdksh run as pdksh works though.


There are two solutions known to me right now:
- first, very easy - #!/bin/bash instead of #!/bin/sh in these scripts
- second working, POSIX/SUSv3 compilant but much slower; use awk for 
conversions (see attached patch).

Any other known fast and portable hex to dec conversions?

ps. using hex2dec() function regardles of choosen conversion method would be 
nice - it would allow easy way to change the way of doing conversion + it 
explicitly says where hex number should occur
-- 
Arkadiusz Miśkiewicz     CS at FoE, Wroclaw University of Technology
arekm.pld-linux.org, 1024/3DB19BBD, JID: arekm.jabber.org, PLD/Linux

[-- Attachment #2: hotplug-PLD.patch --]
[-- Type: text/x-diff, Size: 9588 bytes --]

diff -urN hotplug-2004_04_01.org/etc/hotplug/hotplug.functions hotplug-2004_04_01/etc/hotplug/hotplug.functions
--- hotplug-2004_04_01.org/etc/hotplug/hotplug.functions	2004-04-02 00:38:53.301487496 +0200
+++ hotplug-2004_04_01/etc/hotplug/hotplug.functions	2004-04-02 01:45:37.628737224 +0200
@@ -190,4 +190,13 @@
     # empty line terminates events
 }
 
+####################################################################
+
+# usage: hex2dec hexnumber
+hex2dec()
+{
+    hex="$1"
+    echo | awk "{ printf(\"%d\n\", \"$hex\"); }"
+}
+
 # vim:syntax=sh
diff -urN hotplug-2004_04_01.org/etc/hotplug/ieee1394.agent hotplug-2004_04_01/etc/hotplug/ieee1394.agent
--- hotplug-2004_04_01.org/etc/hotplug/ieee1394.agent	2004-04-02 00:38:53.302487344 +0200
+++ hotplug-2004_04_01/etc/hotplug/ieee1394.agent	2004-04-02 00:54:07.717475136 +0200
@@ -39,9 +39,9 @@
 fi
 
 
-device_vendor_id=$((0x$VENDOR_ID))
-device_specifier_id=$((0x$SPECIFIER_ID))
-device_version=$((0x$VERSION))
+device_vendor_id=$(hex2dec 0x$VENDOR_ID)
+device_specifier_id=$(hex2dec 0x$SPECIFIER_ID)
+device_version=$(hex2dec 0x$VERSION)
 
 MATCH_VENDOR_ID=0x0001
 MATCH_SPECIFIER_ID=0x0004
@@ -63,9 +63,11 @@
 	: check match for $module
 
 	# convert from hex to dec
-	match_flags=$(($match_flags))
-	vendor_id=$(($vendor_id)); model_id=$(($model_id))
-	specifier_id=$(($specifier_id)); version=$(($version))
+	match_flags=$(hex2dec $match_flags)
+	vendor_id=$(hex2dec $vendor_id);
+	model_id=$(hex2dec $model_id)
+	specifier_id=$(hex2dec $specifier_id);
+	version=$(hex2dec $version)
 
 	: vendor_id $vendor_id $device_vendor_id
 	if [ $(($match_flags & $MATCH_VENDOR_ID)) -ne 0 -a $vendor_id -ne $device_vendor_id ]; then
diff -urN hotplug-2004_04_01.org/etc/hotplug/input.agent hotplug-2004_04_01/etc/hotplug/input.agent
--- hotplug-2004_04_01.org/etc/hotplug/input.agent	2004-04-02 00:38:53.323484152 +0200
+++ hotplug-2004_04_01/etc/hotplug/input.agent	2004-04-02 00:55:54.221284096 +0200
@@ -78,14 +78,14 @@
 	IFS=/
 	set $PRODUCT ''
 	IFS="$DEFAULT_IFS"
-	i_bustype=$((0x$1))
-	i_vendor=$((0x$2))
-	i_product=$((0x$3))
-	i_version=$((0x$4))
+	i_bustype=$(hex2dec 0x$1)
+	i_vendor=$(hex2dec 0x$2)
+	i_product=$(hex2dec 0x$3)
+	i_version=$(hex2dec 0x$4)
     fi
 
     if [ "$EV" != "" ]; then
-	i_evBits=$((0x$EV))
+	i_evBits=$(hex2dec 0x$EV)
     fi
 
     input_join_words i_keyBits "$KEY"
@@ -101,14 +101,14 @@
 INPUT_DEVICE_ID_MATCH_VENDOR=2
 INPUT_DEVICE_ID_MATCH_PRODUCT=4
 INPUT_DEVICE_ID_MATCH_VERSION=8
-INPUT_DEVICE_ID_MATCH_EVBIT=$((0x010))
-INPUT_DEVICE_ID_MATCH_KEYBIT=$((0x020))
-INPUT_DEVICE_ID_MATCH_RELBIT=$((0x040))
-INPUT_DEVICE_ID_MATCH_ABSBIT=$((0x080))
-INPUT_DEVICE_ID_MATCH_MSCBIT=$((0x100))
-INPUT_DEVICE_ID_MATCH_LEDBIT=$((0x200))
-INPUT_DEVICE_ID_MATCH_SNDBIT=$((0x400))
-INPUT_DEVICE_ID_MATCH_FFBIT=$((0x800))
+INPUT_DEVICE_ID_MATCH_EVBIT=$(hex2dec 0x010)
+INPUT_DEVICE_ID_MATCH_KEYBIT=$(hex2dec 0x020)
+INPUT_DEVICE_ID_MATCH_RELBIT=$(hex2dec 0x040)
+INPUT_DEVICE_ID_MATCH_ABSBIT=$(hex2dec 0x080)
+INPUT_DEVICE_ID_MATCH_MSCBIT=$(hex2dec 0x100)
+INPUT_DEVICE_ID_MATCH_LEDBIT=$(hex2dec 0x200)
+INPUT_DEVICE_ID_MATCH_SNDBIT=$(hex2dec 0x400)
+INPUT_DEVICE_ID_MATCH_FFBIT=$(hex2dec 0x800)
 
 
 input_match_bits ()
@@ -118,8 +118,8 @@
     if [ "$dev_bits" = "" ]; then
 	return 0
     fi
-    mword=$((0x${mod_bits##*:}))
-    dword=$((0x${dev_bits##*:}))
+    mword=$(hex2dec 0x${mod_bits##*:})
+    dword=$(hex2dec 0x${dev_bits##*:})
 
     while true; do
 	if [ $(( $mword & $dword != $mword )) -eq 1 ]; then
@@ -163,12 +163,12 @@
 	set $line
 
 	module="$1"
-	matchBits=$(($2))
+	matchBits=$(hex2dec $2)
 
-	bustype=$(($3))
-	vendor=$(($4))
-	product=$(($5))
-	version=$(($6))
+	bustype=$(hex2dec $3)
+	vendor=$(hex2dec $4)
+	product=$(hex2dec $5)
+	version=$(hex2dec $6)
 
 	evBits="$7"
 	keyBits="$8"
@@ -180,7 +180,7 @@
 	ledBits="$3"
 	sndBits="$4"
 	ffBits="$5"
-	driverInfo=$(($6))
+	driverInfo=$(hex2dec $6)
 
 	: checkmatch $module
 
diff -urN hotplug-2004_04_01.org/etc/hotplug/pci.agent hotplug-2004_04_01/etc/hotplug/pci.agent
--- hotplug-2004_04_01.org/etc/hotplug/pci.agent	2004-04-02 00:38:53.303487192 +0200
+++ hotplug-2004_04_01/etc/hotplug/pci.agent	2004-04-02 00:57:14.024152216 +0200
@@ -59,18 +59,18 @@
 
 pci_convert_vars ()
 {
-    pci_class=$((0x$PCI_CLASS))
+    pci_class=$(hex2dec 0x$PCI_CLASS)
 
     set $(echo $PCI_ID | sed -e 's/\([^:]*\):\(.*\)/\1 \2/')
-    pci_id_vendor=$((0x$1))
-    pci_id_device=$((0x$2))
+    pci_id_vendor=$(hex2dec 0x$1)
+    pci_id_device=$(hex2dec 0x$2)
 
     set $(echo $PCI_SUBSYS_ID | sed -e 's/\([^:]*\):\(.*\)/\1 \2/')
-    pci_subid_vendor=$((0x$1))
-    pci_subid_device=$((0x$2))
+    pci_subid_vendor=$(hex2dec 0x$1)
+    pci_subid_device=$(hex2dec 0x$2)
 }
 
-PCI_ANY=$((0xffffffff))
+PCI_ANY=$(hex2dec 0xffffffff)
 
 
 #
@@ -95,9 +95,12 @@
 	esac
 
 	# convert the fields from hex to dec
-	vendor=$(($vendor)); device=$(($device))
-	subvendor=$(($subvendor)); subdevice=$(($subdevice))
-	class=$(($class)); class_mask=$(($class_mask))
+	vendor=$(hex2dec $vendor)
+	device=$(hex2dec $device)
+	subvendor=$(hex2dec $subvendor)
+	subdevice=$(hex2dec $subdevice)
+	class=$(hex2dec $class)
+	class_mask=$(hex2dec $class_mask)
 
 	: checkmatch $module
 
diff -urN hotplug-2004_04_01.org/etc/hotplug/pci.rc hotplug-2004_04_01/etc/hotplug/pci.rc
--- hotplug-2004_04_01.org/etc/hotplug/pci.rc	2004-04-02 00:38:53.303487192 +0200
+++ hotplug-2004_04_01/etc/hotplug/pci.rc	2004-04-02 01:00:37.344242864 +0200
@@ -65,6 +65,7 @@
 case "$1" in
   start)
 	pci_boot_events
+	exit $?
         ;;
   stop)
 	# echo $"pci stop -- ignored"
diff -urN hotplug-2004_04_01.org/etc/hotplug/usb.agent hotplug-2004_04_01/etc/hotplug/usb.agent
--- hotplug-2004_04_01.org/etc/hotplug/usb.agent	2004-04-02 00:38:53.318484912 +0200
+++ hotplug-2004_04_01/etc/hotplug/usb.agent	2004-04-02 00:59:47.428831160 +0200
@@ -181,9 +181,9 @@
                                   -e "s+/\([0-9]\)\.\([0-9][0-9]\)+/0\1\2+" \
 			  -e "s+/\([0-9][0-9]\)\.\([0-9][0-9]\)+/\1\2+"`
     set $(echo $PRODUCT | sed -e 's+\([^/]*\)/\([^/]*\)/\(.*\)+\1 \2 \3+')
-    usb_idVendor=$((0x$1))
-    usb_idProduct=$((0x$2))
-    usb_bcdDevice=$((0x$3))
+    usb_idVendor=$(hex2dec 0x$1)
+    usb_idProduct=$(hex2dec 0x$2)
+    usb_bcdDevice=$(hex2dec 0x$3)
 
     if [ "$TYPE" != "" ]; then
     	IFS=/
@@ -193,9 +193,9 @@
         usb_bDeviceProtocol=$3
 	IFS="$DEFAULT_IFS"
     elif [ -r $SYSFS/$DEVPATH/bDeviceClass ]; then
-	usb_bDeviceClass=$((0x$(cat $SYSFS/$DEVPATH/bDeviceClass)))
-	usb_bDeviceSubClass=$((0x$(cat $SYSFS/$DEVPATH/bDeviceSubClass)))
-	usb_bDeviceProtocol=$((0x$(cat $SYSFS/$DEVPATH/bDeviceProtocol)))
+	usb_bDeviceClass=$(hex2dec 0x$(cat $SYSFS/$DEVPATH/bDeviceClass))
+	usb_bDeviceSubClass=$(hex2dec 0x$(cat $SYSFS/$DEVPATH/bDeviceSubClass))
+	usb_bDeviceProtocol=$(hex2dec 0x$(cat $SYSFS/$DEVPATH/bDeviceProtocol))
     else
 	# out-of-range values
 	usb_bDeviceClass=1000
@@ -211,9 +211,9 @@
     	usb_bInterfaceProtocol=$3
 	IFS="$DEFAULT_IFS"
     elif [ -r $SYSFS/$DEVPATH/bInterfaceClass ]; then
-	usb_bInterfaceClass=$((0x$(cat $SYSFS/$DEVPATH/bInterfaceClass)))
-	usb_bInterfaceSubClass=$((0x$(cat $SYSFS/$DEVPATH/bInterfaceSubClass)))
-	usb_bInterfaceProtocol=$((0x$(cat $SYSFS/$DEVPATH/bInterfaceProtocol)))
+	usb_bInterfaceClass=$(hex2dec 0x$(cat $SYSFS/$DEVPATH/bInterfaceClass))
+	usb_bInterfaceSubClass=$(hex2dec 0x$(cat $SYSFS/$DEVPATH/bInterfaceSubClass))
+	usb_bInterfaceProtocol=$(hex2dec 0x$(cat $SYSFS/$DEVPATH/bInterfaceProtocol))
     else
 	# out-of-range values
 	usb_bInterfaceClass=1000
@@ -222,16 +222,16 @@
     fi
 }
 
-USB_MATCH_VENDOR=$((0x0001))
-USB_MATCH_PRODUCT=$((0x0002))
-USB_MATCH_DEV_LO=$((0x0004))
-USB_MATCH_DEV_HI=$((0x0008))
-USB_MATCH_DEV_CLASS=$((0x0010))
-USB_MATCH_DEV_SUBCLASS=$((0x0020))
-USB_MATCH_DEV_PROTOCOL=$((0x0040))
-USB_MATCH_INT_CLASS=$((0x0080))
-USB_MATCH_INT_SUBCLASS=$((0x0100))
-USB_MATCH_INT_PROTOCOL=$((0x0200))
+USB_MATCH_VENDOR=$(hex2dec 0x0001)
+USB_MATCH_PRODUCT=$(hex2dec 0x0002)
+USB_MATCH_DEV_LO=$(hex2dec 0x0004)
+USB_MATCH_DEV_HI=$(hex2dec 0x0008)
+USB_MATCH_DEV_CLASS=$(hex2dec 0x0010)
+USB_MATCH_DEV_SUBCLASS=$(hex2dec 0x0020)
+USB_MATCH_DEV_PROTOCOL=$(hex2dec 0x0040)
+USB_MATCH_INT_CLASS=$(hex2dec 0x0080)
+USB_MATCH_INT_SUBCLASS=$(hex2dec 0x0100)
+USB_MATCH_INT_PROTOCOL=$(hex2dec 0x0200)
 
 #
 # stdin is "modules.usbmap" syntax
@@ -255,21 +255,21 @@
 	set $line
 
 	module=$1
-	match_flags=$(($2))
+	match_flags=$(hex2dec $2)
 
-	idVendor=$(($3))
-	idProduct=$(($4))
-	bcdDevice_lo=$(($5))
-	bcdDevice_hi=$(($6))
-
-	bDeviceClass=$(($7))
-	bDeviceSubClass=$(($8))
-	bDeviceProtocol=$(($9))
+	idVendor=$(hex2dec $3)
+	idProduct=$(hex2dec $4)
+	bcdDevice_lo=$(hex2dec $5)
+	bcdDevice_hi=$(hex2dec $6)
+
+	bDeviceClass=$(hex2dec $7)
+	bDeviceSubClass=$(hex2dec $8)
+	bDeviceProtocol=$(hex2dec $9)
 
 	shift 9
-	bInterfaceClass=$(($1))
-	bInterfaceSubClass=$(($2))
-	bInterfaceProtocol=$(($3))
+	bInterfaceClass=$(hex2dec $1)
+	bInterfaceSubClass=$(hex2dec $2)
+	bInterfaceProtocol=$(hex2dec $3)
 
 	: checkmatch $module
 
diff -urN hotplug-2004_04_01.org/etc/hotplug.d/default/default.hotplug hotplug-2004_04_01/etc/hotplug.d/default/default.hotplug
--- hotplug-2004_04_01.org/etc/hotplug.d/default/default.hotplug	2004-04-02 00:38:53.324484000 +0200
+++ hotplug-2004_04_01/etc/hotplug.d/default/default.hotplug	2004-04-02 01:01:00.153775288 +0200
@@ -32,7 +32,7 @@
 
 # DEBUG=yes export DEBUG
 
-debug_mesg "arguments ($*) env (`env`)"
+[ -n "$DEBUG" ] && debug_mesg "arguments ($*) env (`env`)"
 
 #
 # Only one required argument:  event type type being dispatched.

             reply	other threads:[~2004-04-02  0:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-02  0:11 Arkadiusz Miskiewicz [this message]
2004-04-02  1:09 ` bashizm in hotplug scripts (including 20040401) Martin Schwenke
2004-04-02 13:05 ` Arkadiusz Miskiewicz
2004-04-05  6:06 ` Martin Schwenke
2004-04-05 17:11 ` Greg KH
2004-04-05 17:46 ` Arkadiusz Miskiewicz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200404020211.45303.arekm@pld-linux.org \
    --to=arekm@pld-linux.org \
    --cc=linux-hotplug@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.