Skip to content

Commit

Permalink
Fixing Egrep Error Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
LanikSJ committed Aug 17, 2023
1 parent 9da9755 commit 991109c
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions lsusb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#
# Disclaimer: usage info and functionality from lsusb under Linux

verbose () { system_profiler SPUSBDataType; }
version () { echo "lsusb for Mac OS X 007"; }
verbose () { system_profiler SPUSBDataType 2>/dev/null; }
version () { echo "lsusb for Mac OS X 008"; }
help () {
cat >&2 <<EOM
$(usage)
Expand All @@ -25,34 +25,41 @@ EOM
}
usage () { echo "Usage: $(basename "$0") [options]..."; }

unknown_dev_num=127
parse () {
# Get the name of the device, it is the first line that ends with a ':'
# Trim the string at the end
name=`echo "$device" | head -2 | egrep ":$" | sed -e 's/^ *//g' -e 's/ *:$//g'`
name=`echo "$device" | head -2 | grep ":$" | sed -e 's/^ *//g' -e 's/ *:$//g'`

# Get the speed
speed=`echo "$device" | egrep "Speed: " | awk -F':' '{print $2}' | sed 's/^ *[^0-9]*//g; s/ *$//; s/b\/sec$//; s/ //g'`
speed=`echo "$device" | grep "Speed: " | awk -F':' '{print $2}' | sed 's/^ *[^0-9]*//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'`
PID=`echo "$device" | grep "Product ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g'`

# Get the VID string, trim at the end
VID_all=`echo "$device" | egrep "Vendor ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g'`
VID_all=`echo "$device" | grep "Vendor ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g'`
# Get the VID
VID=`echo "$VID_all" | awk -F ' ' '{print $1}' | sed 's/^ *//; s/ *$//'`
# Get the manufacturer string
manufacturer=`echo "$VID_all" | cut -d ' ' -f 2- | sed 's/^ *//; s/ *$//'`

# Get the Location Id
location=`echo "$device" | egrep "Location ID" | sed -e 's/Location ID://; s/^ *//g; s/ *$//g;'`
location=`echo "$device" | grep "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/;'`
if [[ -z $bus_num ]]; then
bus_num="00"
fi

# 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"`
if [[ -z $device_num ]]; then
device_num=$((unknown_dev_num--))
fi
device_num=`printf "%03d" "$device_num"`

# Strip off the excess from LocationID for sorting tree.
locationID=`echo "$location" | awk -F'/' '{print $1}'`
Expand All @@ -64,7 +71,10 @@ parse () {
fi

# Get the bus number, in hexadecimal. We'll convert to decimal later.
bus_num=`echo "$device" | egrep "Bus Number" | sed -e 's/Bus Number://' -e 's/0x//; s/^ *//g' -e 's/ *$//g'`
bus_num=`echo "$device" | grep "Bus Number" | sed -e 's/Bus Number://' -e 's/0x//; s/^ *//g' -e 's/ *$//g'`
if [[ -z $bus_num ]]; then
bus_num="00"
fi

# Create a fake Location ID for sorting purposes, following the model.
locationID="0x${bus_num}000000"
Expand All @@ -77,7 +87,7 @@ parse () {
manufacturer="(Apple Inc.)"

# The PID depends on the speed of the hub, which we deduce from the controller driver.
PID=`echo "$device" | egrep "Controller Driver" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g; s/.*\(....\)$/\1/'`
PID=`echo "$device" | grep "Controller Driver" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g; s/.*\(....\)$/\1/'`
case "$PID" in
OHCI) PID="8005"; name="OHCI Root Hub Simulation"; speed="12M" ;;
EHCI) PID="8006"; name="EHCI Root Hub Simulation"; speed="480M" ;;
Expand All @@ -86,8 +96,7 @@ parse () {
fi

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

# Strip the parentheses from manufacturer name unless so specified.
if [ -z "$parens" ]; then
Expand All @@ -96,7 +105,7 @@ parse () {

# Include serial number only if available.
serial_str=""
serial_number=`echo "$device" | egrep "Serial Number" | sed 's/Serial Number: //; s/^ *//g; s/ *$//g'`
serial_number=`echo "$device" | grep "Serial Number" | sed 's/Serial Number: //; s/^ *//g; s/ *$//g'`
if [ -n "$serial_number" ]; then
serial_str=" Serial: ""$serial_number"
fi
Expand All @@ -110,7 +119,7 @@ parse () {
# 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" | grep ':')" ]; 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 @@ -132,7 +141,7 @@ parse () {

# 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" | grep ':')" ]; then
# Convert input bus to lower case
arg_bus=`echo "$bus_dev" | awk -F':' '{print $1}' | sed 's/^ *//; s/ *$//'`
if [ -n "$arg_bus" ]; then
Expand All @@ -150,7 +159,7 @@ parse () {
fi
fi
# Special case: no colon after -s means device-only.
if [ -z "$(echo "$bus_dev" | egrep ':')" ]; then
if [ -z "$(echo "$bus_dev" | grep ':')" ]; then
# Strip leading and trailing spaces from argument.
arg_dev=`echo "$bus_dev" | sed 's/^ *//; s/ *$//'`
if [ -n "$arg_dev" ]; then
Expand Down Expand Up @@ -206,12 +215,20 @@ buildtreeline () {
treeline="${locationID}${spaces}Bus ""${bus_num}"".Dev ""${device_num}"": ""${name}"", ""${speed}"
}

get_devices() {
<<<"$rawlog" sed -e '/:$/i\
#'
}

get_buses() {
<<<"$rawlog" awk -v first=1 '/Bus:$/ { flag = 1; if (first == 1) { first = 0 } else { print "#" }; print; next }; /:$/ && flag == 1 { flag = 0 }; flag'
}

tree () {
setup
treeflag="yes"

devices=`echo "$rawlog" | egrep -B 2 -A 6 "Product ID" | sed 's/^--/#/'`
for device in $devices
for device in $(get_devices)
do
# Skip null device lines
if [ "${#device}" -ne 1 ]; then
Expand All @@ -221,8 +238,7 @@ tree () {
fi
done

buses=`echo "$rawlog" | egrep -A 7 "Bus:" | sed 's/^--/#/'`
for device in $buses
for device in $(get_buses)
do
parse
buildtreeline
Expand Down Expand Up @@ -282,9 +298,8 @@ setup
# with the '#' symbol in case other parameters that contain a dash
# will not be interfered

devices=`echo "$rawlog" | egrep -B 2 -A 6 "Product ID" | sed 's/^--/#/'`
# Iterate over each entry
for device in $devices
for device in $(get_devices)
do
parse
if [ $? -eq 0 ]; then
Expand All @@ -293,8 +308,8 @@ do
fi
done

buses=`echo "$rawlog" | egrep -A 7 "Bus:" | sed 's/^--/#/'`
for device in $buses
echo "Buses:"
for device in $(get_buses)
do
parse
if [ $? -eq 0 ]; then
Expand Down

0 comments on commit 991109c

Please sign in to comment.