|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +if [ -z "${CANPLAYER}" ]; then |
| 4 | + CANPLAYER="canplayer" |
| 5 | +fi |
| 6 | + |
| 7 | +die() { |
| 8 | + echo "$*" > /dev/stderr |
| 9 | + exit 1 |
| 10 | +} |
| 11 | + |
| 12 | +usage() { |
| 13 | + echo "canplayer-bisect <start|stop|clean|good|yes|bad|no|again|where|undo> <logfile> <canplayer options>" |
| 14 | +} |
| 15 | + |
| 16 | +is_ready() { |
| 17 | + if [ ! -d .canplayer-bisect ]; then |
| 18 | + usage |
| 19 | + exit 1 |
| 20 | + fi |
| 21 | + |
| 22 | + return 0 |
| 23 | +} |
| 24 | + |
| 25 | +setup() { |
| 26 | + is_ready |
| 27 | + |
| 28 | + LOGFILE=$(cat .canplayer-bisect/logfile |head -n 1) |
| 29 | + |
| 30 | + SAVED_LEN="$(cat .canplayer-bisect/len|tail -n 1)" |
| 31 | + LEN="$(wc -l ${LOGFILE} | awk '{ print $1 }')" |
| 32 | + |
| 33 | + if [ "$LEN" != "$SAVED_LEN" ]; then |
| 34 | + die "logfile has changed size. restart" |
| 35 | + fi |
| 36 | + |
| 37 | + CANPLAYER_ARGS=$(cat .canplayer-bisect/args |head -n 1) |
| 38 | + |
| 39 | + HEAD="$(cat .canplayer-bisect/head |tail -n 1)" |
| 40 | + TAIL="$(cat .canplayer-bisect/tail |tail -n 1)" |
| 41 | +} |
| 42 | + |
| 43 | +back() { |
| 44 | + HEAD="$(cat .canplayer-bisect/head |tail -n 2 |head -n1)" |
| 45 | + TAIL="$(cat .canplayer-bisect/tail |tail -n 2 |head -n1)" |
| 46 | +} |
| 47 | + |
| 48 | +do_undo() { |
| 49 | + sed -i '$ d' .canplayer-bisect/head |
| 50 | + sed -i '$ d' .canplayer-bisect/tail |
| 51 | +} |
| 52 | + |
| 53 | +teardown() { |
| 54 | + mkdir -p .canplayer-bisect |
| 55 | + echo $LEN > .canplayer-bisect/len |
| 56 | + echo $LOGFILE > .canplayer-bisect/logfile |
| 57 | + echo $CANPLAYER_ARGS > .canplayer-bisect/args |
| 58 | + |
| 59 | + echo $HEAD >> .canplayer-bisect/head |
| 60 | + echo $TAIL >> .canplayer-bisect/tail |
| 61 | +} |
| 62 | + |
| 63 | +show() { |
| 64 | + cat $LOGFILE | sed -n ${HEAD},${TAIL}p |
| 65 | +} |
| 66 | + |
| 67 | +play() { |
| 68 | + #we *could* pipe directly to canplayer, but then the user can't add -l i to CANPLAYER_ARGS to hunt for packets using looped playback |
| 69 | + the_show="$(mktemp)" |
| 70 | + trap "rm -rf \"${the_show}\"" EXIT |
| 71 | + |
| 72 | + show > "${the_show}" |
| 73 | + "${CANPLAYER}" ${CANPLAYER_ARGS} -I "${the_show}" |
| 74 | +} |
| 75 | + |
| 76 | +do_show() { |
| 77 | + setup |
| 78 | + |
| 79 | + show |
| 80 | +} |
| 81 | + |
| 82 | +check_heads_n_tails() { |
| 83 | + if [ $HEAD -eq $TAIL ]; then |
| 84 | + do_stop |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +do_good() { |
| 89 | + setup |
| 90 | + |
| 91 | + check_heads_n_tails |
| 92 | + |
| 93 | + if [ $(( $HEAD + 1 )) -eq $TAIL ]; then |
| 94 | + TAIL=$HEAD |
| 95 | + else |
| 96 | + TAIL=$(( ( $TAIL - $HEAD ) / 2 + $HEAD - 1 )) |
| 97 | + fi |
| 98 | + |
| 99 | + teardown |
| 100 | + play |
| 101 | +} |
| 102 | + |
| 103 | +do_bad() { |
| 104 | + setup |
| 105 | + |
| 106 | + check_heads_n_tails |
| 107 | + |
| 108 | + back |
| 109 | + if [ $(( $HEAD + 1 )) -eq $TAIL ]; then |
| 110 | + HEAD=$TAIL |
| 111 | + else |
| 112 | + HEAD=$(( ( $TAIL - $HEAD ) / 2 + $HEAD )) |
| 113 | + fi |
| 114 | + |
| 115 | + teardown |
| 116 | + play |
| 117 | +} |
| 118 | + |
| 119 | +do_again() { |
| 120 | + setup |
| 121 | + play |
| 122 | +} |
| 123 | + |
| 124 | +do_start() { |
| 125 | + do_clean |
| 126 | + |
| 127 | + LEN="$(wc -l ${LOGFILE} | awk '{ print $1 }')" |
| 128 | + |
| 129 | + HEAD=1 |
| 130 | + TAIL=$LEN |
| 131 | + |
| 132 | + echo "assuming logfile contains the packets you seek... bisecting to first half" |
| 133 | + |
| 134 | + teardown |
| 135 | + play |
| 136 | +} |
| 137 | + |
| 138 | +do_where() { |
| 139 | + setup |
| 140 | + echo "between $HEAD and $TAIL (+$(( $TAIL - $HEAD ))) of $LOGFILE" |
| 141 | +} |
| 142 | + |
| 143 | +do_stop() { |
| 144 | + setup |
| 145 | + |
| 146 | + if [ "$COMMAND" == "no" ]; then |
| 147 | + echo "failed to find what you were looking for" |
| 148 | + exit 1 |
| 149 | + else |
| 150 | + echo "the packets you seek are:" |
| 151 | + do_where |
| 152 | + exit 0 |
| 153 | + fi |
| 154 | +} |
| 155 | + |
| 156 | +do_clean() { |
| 157 | + rm -rf .canplayer-bisect |
| 158 | +} |
| 159 | + |
| 160 | +if [ -z "$1" ]; then |
| 161 | + usage |
| 162 | + exit 1 |
| 163 | +fi |
| 164 | +COMMAND=$1 |
| 165 | + |
| 166 | +if [ ! -d .canplayer-bisect ] && [ ! -z "$2" ] && [ ! -e "$2" ]; then |
| 167 | + usage |
| 168 | + exit 1 |
| 169 | +fi |
| 170 | +LOGFILE="$2" |
| 171 | + |
| 172 | +shift |
| 173 | +shift |
| 174 | +CANPLAYER_ARGS="$*" |
| 175 | + |
| 176 | +case "$COMMAND" in |
| 177 | + start) |
| 178 | + do_start |
| 179 | + ;; |
| 180 | + stop) |
| 181 | + do_stop |
| 182 | + ;; |
| 183 | + clean) |
| 184 | + do_clean |
| 185 | + ;; |
| 186 | + good|yes) |
| 187 | + do_good |
| 188 | + ;; |
| 189 | + bad|no) |
| 190 | + do_bad |
| 191 | + ;; |
| 192 | + again) |
| 193 | + do_again |
| 194 | + ;; |
| 195 | + where) |
| 196 | + do_where |
| 197 | + ;; |
| 198 | + undo) |
| 199 | + do_undo |
| 200 | + ;; |
| 201 | + show) |
| 202 | + do_show |
| 203 | + ;; |
| 204 | +esac |
| 205 | + |
0 commit comments