Skip to content

Commit

Permalink
Changed: formatting, version output
Browse files Browse the repository at this point in the history
  • Loading branch information
jlhonora committed Feb 24, 2014
1 parent a01e7bf commit d5af057
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 69 deletions.
134 changes: 66 additions & 68 deletions lsusb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Disclaimer: usage info and functionality from lsusb under Linux

verbose () { system_profiler SPUSBDataType; }
version () { echo "lsusb (usbutils) 007"; }
version () { echo "lsusb for Mac OS X"; }
help () {
cat >&2 <<EOM
$(usage)
Expand All @@ -30,7 +30,7 @@ parse () {
# Trim the string at the end
name=`echo $device | head -2 | egrep ":$" | sed -e 's/^ *//g' -e 's/ *:$//g'`

#get the speed
# Get the speed
speed=`echo $device | egrep "Speed: " | awk -F':' '{print $2}' | sed 's/^ *Up to //g; s/ *$//; s/b\/sec$//; s/ //g'`
# Get the PID, trim at the end
PID=`echo $device | egrep "Product ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g'`
Expand All @@ -41,18 +41,18 @@ parse () {
# Get the manufacturer string
manufacturer=`echo $VID_all | cut -d ' ' -f 2- | sed 's/^ *//; s/ *$//'`

#get the Location Id
# Get the Location Id
location=`echo $device | egrep "Location ID" | sed -e 's/Location ID://; s/^ *//g; s/ *$//g;'`

# Get the bus number. It's the first two hex digits of the Location ID
# we'll convert to decimal later
bus_num=`echo $location | sed -e 's/^..\(..\).*/\1/;'`
bus_num=`echo $location | sed -e 's/^..\(..\).*/\1/;'`

# Get the device number. It's after the '/' in the Location ID, already decimal
device_num=`echo $location | awk -F'/' '{print $2}'`
device_num=`printf "%0*d" 3 $device_num`

#strip off the excess from LocationID for sorting tree
# Strip off the excess from LocationID for sorting tree
locationID=`echo $location | awk -F'/' '{print $1}'`

# Special case for "root hub" - follow the (faked) Linux standard
Expand All @@ -74,12 +74,12 @@ parse () {
esac
fi

#convert bus number from hexadecimal to decimal
# Convert bus number from hexadecimal to decimal
bus_num=`echo "$((16#$bus_num))"`
bus_num=`printf "%0*d" 3 $bus_num`

#strip the parentheses from manufacturer name unless so specified
if [ -z $parens ]; then
# Strip the parentheses from manufacturer name unless it is specified
if [ -z $parens ]; then
manufacturer=`echo $manufacturer | sed 's/(//; s/)//'`
fi

Expand All @@ -90,17 +90,16 @@ parse () {
serial_str=" Serial: $serial_number"
fi


# don't filter if we're building the tree
# Don't filter if we're building the tree
if [ "$treeflag" == "yes" ]; then
exitcode=0
return
return
fi

# Filter by VID/PID if given as input argument
if [ -n "$vid_pid" ]; then
# Convert input vid to lower case
if [ -n "$(echo $vid_pid | egrep ':')" ]; then
if [ -n "$(echo $vid_pid | egrep ':')" ]; then
arg_vid=`echo $vid_pid | awk -F':' '{print $1}' | sed 's/^0x//' | tr '[A-Z]' '[a-z]'`
if [ -n "$arg_vid" ]; then
if [ $((16#$arg_vid)) -ne $((16#$VID)) ]; then
Expand All @@ -114,23 +113,23 @@ parse () {
fi
fi
else
#missing a colon is a syntax error after -d
help
# Missing a colon is a syntax error after -d
help
exit 1
fi
fi

# Filter by BUS/DEV if given as input argument
if [ -n "$bus_dev" ]; then
if [ -n "$(echo $bus_dev | egrep ':')" ]; then
if [ -n "$(echo $bus_dev | egrep ':')" ]; then
# Convert input bus to lower case
arg_bus=`echo $bus_dev | awk -F':' '{print $1}' | sed 's/^ *//; s/ *$//'`
if [ -n "$arg_bus" ]; then
if [ $arg_bus -ne $bus_num ]; then
return 1
fi
fi
# strip leading and trailing spaces from argument
# Strip leading and trailing spaces from argument
arg_dev=`echo $bus_dev | awk -F':' '{print $2}' | sed 's/^ *//; s/ *$//'`
if [ -n "$arg_dev" ]; then
if [ $arg_dev -ne $device_num ]; then
Expand All @@ -139,8 +138,8 @@ parse () {
fi
fi
fi
#special case: no colon after -s means device-only
if [ -z "$(echo $bus_dev | egrep ':')" ]; then
# Special case: no colon after -s means device-only
if [ -z "$(echo $bus_dev | egrep ':')" ]; then
# strip leading and trailing spaces from argument
arg_dev=`echo $bus_dev | sed 's/^ *//; s/ *$//'`
if [ -n "$arg_dev" ]; then
Expand All @@ -150,93 +149,92 @@ parse () {
fi
fi

#if we got this far, we found a match, so set exit code to 0
# If we got this far, we found a match, so set exit code to 0
exitcode=0
}

setup () {
#save the data
if [ -z $rawlog ]; then
rawlog=$(verbose)
fi

# Save the data
if [ -z $rawlog ]; then
rawlog=$(verbose)
fi

# Change the IFS to #, backup the current one
OIFS=$IFS
IFS="#"
exitcode=1
treeflag="no"
# Change the IFS to #, backup the current one
OIFS=$IFS
IFS="#"
exitcode=1
treeflag="no"
}

cleanup () {
IFS=$OIFS
exit $exitcode
IFS=$OIFS
exit $exitcode
}

buildtreeline () {
# build a formatted line to be sorted for the tree
# the LocationID has the tree structure (0xbbdddddd):
# Build a formatted line to be sorted for the tree.
# The LocationID has the tree structure (0xbbdddddd):
# 0x -- always
# bb -- bus number in hexadecimal
# dddddd -- up to six levels for the tree, each digit represents its
# position on that level
#
#
# so we start each line with the LocationID for sorting purposes,
# then append the rest of the line as we want it
# Later, we'll do the sort, then strip off the LocationID for display purposes
# Later, we'll do the sort, then strip off the LocationID for display purposes.

spaces=`echo $locationID | sed 's/^0x...//; s/0//g; s/./ /g; s/.*/&&&&/'`
if [ ${#spaces} -eq 0 ]; then
spaces=" /: "
else
#weird syntax because we have one space too many
# Weird syntax because we have one space too many
spaces="${spaces}|__ "
fi
treeline="${locationID}${spaces}Bus ${bus_num}.Dev ${device_num}: ${name}, ${speed}"
}

tree () {
setup
treeflag="yes"
tree () {
setup
treeflag="yes"

devices=`echo $rawlog | egrep -B 2 -A 6 "Product ID" | sed 's/^--/#/'`
for device in $devices
do
# Skip null items
if [ "${#device}" -ne 1 ]; then
parse
buildtreeline
treedata="$treedata${treeline}\n"
fi
done

devices=`echo $rawlog | egrep -B 2 -A 6 "Product ID" | sed 's/^--/#/'`
for device in $devices
do
#skip null items
if [ "${#device}" -ne 1 ]; then
buses=`echo $rawlog | egrep -A 7 "Bus:" | sed 's/^--/#/'`
for device in $buses
do
parse
buildtreeline
treedata="$treedata${treeline}\n"
fi
done
done

buses=`echo $rawlog | egrep -A 7 "Bus:" | sed 's/^--/#/'`
for device in $buses
do
parse
buildtreeline
treedata="$treedata${treeline}\n"
done
# Strip off final \n
treedata=`echo ${treedata/%%?/}`
# Sort by Location ID
treedata=`echo ${treedata} | sort`

#strip off final \n (weird syntax but it works)
treedata=`echo ${treedata/%%?/}`
#sort by Location ID
treedata=`echo ${treedata} | sort`
# Now strip off leading Location ID and print
for line in $treedata
do
echo $line | sed 's/^...........//'
done

#Now strip off leading Location ID and print
for line in $treedata
do
echo $line | sed 's/^...........//'
done

# Restore the IFS
cleanup
# Restore the IFS
cleanup

#not really needed, as cleanup routine does the exit
exit 0
# Not really needed, as cleanup routine does the exit
exit 0
}

# Parse options
# Parse options
while getopts ":hvpd:s:Vt" opt; do
case "$opt" in
h) help ; exit 0 ;; # help
Expand Down
2 changes: 1 addition & 1 deletion man/lsusb.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH lsusb 8 "21 Feb 2014" "usbutils-007" "USB Utilities"
.TH lsusb 8 "21 Feb 2014" "lsusb for Mac OS X" "USB Utilities"
.IX lsusb
.SH NAME
lsusb \- list USB devices
Expand Down

0 comments on commit d5af057

Please sign in to comment.