1#! /bin/sh
2
3# bar
4# 'cat' with ASCII progress bar
5# (c) Henrik Theiling
6BAR_VERSION=1.4
7
8# Synopsis:
9#   'bar' works just like 'cat', but shows a progress bar in ASCII art on stderr.
10#   The script's main function is meant to be usable in any Bourne shell to be
11#   suitable for install scripts without the need for any additional tool.
12#
13# Shell Script Usage: bar [options] [files]
14# Options:
15#     -h        displays help
16#     ...
17#
18# Examples:
19#   Normal pipe:
20#
21#     : bar mypack.tar.bz2 | tar xjpf -
22#
23#   Individual pipe for each file:
24#
25#     : bar -c 'tar xjpf -' mypack1.tar.bz2 mypack2.tar.bz2
26#
27#   Individual pipe, using ${bar_file} variable:
28#
29#     : bar -c 'echo ${bar_file}: ; gzip -dc | tar tvf -' \
30#     :     -e .tar.gz \
31#     :     file1 file2 file3 file4 file5 \
32#     :         > package-list.txt
33
34#####################################################
35# Programs and shell commands:
36#
37# Required (otherwise this fails):
38#
39#    if, then, else, fi, expr, test, cat, eval, exec
40#    shell functions
41#
42#    test:
43#        a = b
44#        a -lt b
45#        a -gt b
46#        a -le b
47#        -f a
48#        -n a
49#        -z a
50#
51#    expr:
52#        a + b
53#        a - b
54#        a '*' b
55#        a / b
56#        a : b
57#
58# Optional (otherwise this does not show the bar):
59#
60#    grep, dd, echo, ls, sed, cut
61#
62#    ls:
63#        must output the file size at fifth position.
64#
65# The command line interface also uses:
66#
67#    awk
68#
69
70####>-SCHNIPP-<########################################################
71bar_cat()
72{
73   # Use this shell function in your own install scripts.
74
75   #####################################################
76   # Options:
77
78   # Width of the bar (in ten characters).  The default is 76 characters.
79   test -z "${BAR_WIDTH}" && test -n "${COLUMNS}" && BAR_WIDTH=${COLUMNS}
80
81   # Check syntax:
82   ( expr "${BAR_WIDTH}" + 0 >/dev/null 2>&1 ) || BAR_WIDTH=0
83   BAR_WIDTH=`expr ${BAR_WIDTH} + 0` || BAR_WIDTH=0
84   test "x${BAR_WIDTH}" = x0 && BAR_WIDTH=76
85
86   # Maximal block size to use for dd.
87   test -n "${BAR_BS}" || BAR_BS=1048567
88
89   # BEGIN PERC
90   # Whether to show a percentage.
91   test -n "${BAR_PERC}" || BAR_PERC=1
92   # END PERC
93
94   # BEGIN ETA
95   # Whether to show estimated time of arrival (ETA).
96   test -n "${BAR_ETA}" || BAR_ETA=1
97   # END ETA
98
99   # Width of the trace display:
100   # BEGIN TRACE
101   test -n "${BAR_TRACE_WIDTH}" || BAR_TRACE_WIDTH=10
102   # END TRACE
103
104   # The command to execute for every given file.  Each file
105   # is piped into this command individually.  By default, the
106   # files are simply dumped to stdout.
107   test -n "${BAR_CMD}" || BAR_CMD=cat
108
109   # The characters to be used in the bar
110   test -n "${BAR_L}"  || BAR_L='['
111   test -n "${BAR_R}"  || BAR_R=']'
112   test -n "${BAR_C0}" || BAR_C0='.'
113   test -n "${BAR_C1}" || BAR_C1='='
114
115   # Additional extension to add to each file:
116   #BAR_EXT=${BAR_EXT-}
117
118   # Whether to clear bar after termination.  Otherwise keep the full bar.
119   #BAR_CLEAR=${BAR_CLEAR-0}
120
121   # Unless switched off by user, use the bar by default:
122   test -n "${BAR_OK}" || BAR_OK=1
123
124   #####################################################
125   BAR_WIDTH=`expr ${BAR_WIDTH} - 3`
126
127   bar_trace=''
128   # BEGIN TRACE
129   if test "x${BAR_TRACE}" = x1
130   then
131       BAR_WIDTH=`expr ${BAR_WIDTH} - ${BAR_TRACE_WIDTH}`
132       bar_lauf=${BAR_TRACE_WIDTH}
133       bar_t_space=''
134       bar_t_dot=''
135       while test "${bar_lauf}" -gt 1
136       do
137           bar_t_space="${bar_t_space} "
138           bar_t_dot="${bar_t_dot}."
139           bar_lauf=`expr ${bar_lauf} - 1`
140       done
141       bar_trace="${bar_t_space} "
142   fi
143   # END TRACE
144
145   bar_eta=''
146   BAR_GET_TIME='echo'
147   # BEGIN ETA
148   ( expr 1 + ${SECONDS} >/dev/null 2>&1 ) || BAR_ETA=0
149   if test "x${BAR_ETA}" = x1
150   then
151       BAR_GET_TIME='( echo ${SECONDS} )'
152       BAR_WIDTH=`expr ${BAR_WIDTH} - 6`
153       bar_eta='--:-- '
154   fi
155   # END ETA
156
157   bar_perc=''
158   # BEGIN PERC
159   if test "x${BAR_PERC}" = x1
160   then
161       BAR_WIDTH=`expr ${BAR_WIDTH} - 5`
162       bar_perc='  0% '
163   fi
164   # END PERC
165
166   BAR_GET_SIZE='( ls -l "${BAR_DIR}${bar_file}${BAR_EXT}" | sed "s@  *@ @g" | cut -d " " -f 5 ) 2>/dev/null'
167       # portable?
168
169   # check features:
170   ( ( echo a                   ) >/dev/null 2>&1 ) || BAR_OK=0
171   ( ( echo a | dd bs=2 count=2 ) >/dev/null 2>&1 ) || BAR_OK=0
172   ( ( echo a | grep a          ) >/dev/null 2>&1 ) || BAR_OK=0
173   ( ( echo a | sed 's@  *@ @g' ) >/dev/null 2>&1 ) || BAR_OK=0
174   ( ( echo a | cut -d ' ' -f 1 ) >/dev/null 2>&1 ) || BAR_OK=0
175
176   # check ranges:
177   test "${BAR_WIDTH}" -ge 4 || BAR_OK=0
178
179   BAR_ECHO='echo'
180   BAR_E_C1=''
181   BAR_E_C2=''
182   BAR_E_NL='echo'
183
184   # Does echo accept -n without signalling an error?
185   if echo -n abc >/dev/null 2>&1
186   then
187       BAR_E_C1='-n'
188   fi
189
190   # Check how to print a line without newline:
191   if ( ( ${BAR_ECHO} "${BAR_E_C1}" abc ; echo 1,2,3 ) | grep n ) >/dev/null 2>&1
192   then
193       # Try echo \c:
194       if ( ( ${BAR_ECHO} 'xyz\c' ; echo 1,2,3 ) | grep c ) >/dev/null 2>&1
195       then
196           # Try printf:
197           if ( ( printf 'ab%s' c ; echo 1,2,3 ) | grep abc ) >/dev/null 2>&1
198           then
199              BAR_ECHO='printf'
200              BAR_E_C1='%s'
201           else
202              BAR_ECHO=':'
203              BAR_E_C1=''
204              BAR_E_NL=':'
205              BAR_OK=0
206           fi
207       else
208          BAR_E_C1=''
209          BAR_E_C2='\c'
210       fi
211   fi
212
213   # prepare initial bar:
214   bar_shown=0
215   if test "${BAR_OK}" = 1
216   then
217       bar_lauf=0
218       bar_graph=''
219       while test `expr ${bar_lauf} + 5` -le "${BAR_WIDTH}"
220       do
221           bar_graph="${bar_graph}${BAR_C0}${BAR_C0}${BAR_C0}${BAR_C0}${BAR_C0}"
222           bar_lauf=`expr ${bar_lauf} + 5`
223       done
224       while test "${bar_lauf}" -lt "${BAR_WIDTH}"
225       do
226           bar_graph="${bar_graph}${BAR_C0}"
227           bar_lauf=`expr ${bar_lauf} + 1`
228       done
229       ${BAR_ECHO} "${BAR_E_C1}" "
230${bar_trace}${bar_eta}${bar_perc}${BAR_L}${bar_graph}${BAR_R}
231${BAR_E_C2}" 1>&2
232       bar_shown=1
233   fi
234
235   # for shifting large numbers so that expr can handle them:
236   # Assume we can compute up to 2147483647, thus 9 arbitrary digits.
237   # We must be able to do + of two numbers of 9 digits length.  Ok.
238   # BEGIN LARGE
239   ( ( test 1999999998 = `expr 999999999 + 999999999` ) >/dev/null 2>&1 ) || BAR_OK=0
240   bar_large_num="........."
241   bar_div=""
242   # END LARGE
243   bar_numsuff=""
244
245   # find size:
246   bar_size=0
247   if test -n "${BAR_SIZE}"
248   then
249       bar_size=${BAR_SIZE}
250       # BEGIN LARGE
251       while expr "${bar_size}" : "${bar_large_num}" >/dev/null 2>&1
252       do
253           bar_div="${bar_div}."
254           bar_numsuff="${bar_numsuff}0"
255           bar_size=`expr "${bar_size}" : '\(.*\).$'`
256       done
257       # END LARGE
258       BAR_GET_SIZE="echo '${BAR_SIZE}'"
259   else
260       for bar_file
261       do
262           bar_size1=0
263           if test -f "${BAR_DIR}${bar_file}${BAR_EXT}"
264           then
265               bar_size1=`eval "${BAR_GET_SIZE}"`
266
267               # BEGIN LARGE
268               # divide and upround by pattern matching:
269               if test -n "${bar_div}"
270               then
271                   bar_size1=`expr "${bar_size1}" : '\(.*\)'${bar_div}'$'` || bar_size1=0
272               fi
273
274               # adjust if still too large:
275               while expr "${bar_size1}" : "${bar_large_num}" >/dev/null 2>&1
276               do
277                   bar_div="${bar_div}."
278                   bar_numsuff="${bar_numsuff}0"
279                   bar_size1=`expr "${bar_size1}" : '\(.*\).$'`
280                   bar_size=`expr "${bar_size}" : '\(.*\).$'` || bar_size=0
281               done
282
283               # upround if necessary:
284               if test -n "${bar_div}"
285               then
286                   bar_size1=`expr "${bar_size1}" + 1`
287               fi
288               # END LARGE
289
290               # add to total size:
291               bar_size=`expr ${bar_size} + ${bar_size1}`
292
293               # BEGIN LARGE
294               # adjust if still too large:
295               while expr "${bar_size}" : "${bar_large_num}" >/dev/null 2>&1
296               do
297                   bar_div="${bar_div}."
298                   bar_numsuff="${bar_numsuff}0"
299                   bar_size=`expr "${bar_size}" : '\(.*\).$'`
300               done
301               # END LARGE
302           else
303               BAR_OK=0
304           fi
305       done
306   fi
307
308   bar_quad=`expr ${BAR_WIDTH} '*' ${BAR_WIDTH}`
309   test "${bar_size}" -gt "${bar_quad}" || BAR_OK=0
310
311   if test "${BAR_OK}" = 0
312   then
313       # For some reason, we cannot display the bar.  Thus plain operation:
314       for bar_file
315       do
316           if test "${bar_file}" = "/dev/stdin"
317           then
318               eval "${BAR_CMD}"
319           else
320               eval "${BAR_CMD}" < "${BAR_DIR}${bar_file}${BAR_EXT}"
321           fi
322       done
323   else
324       # Compute wanted bytes per step:
325       bar_want_bps=`expr ${bar_size} + ${BAR_WIDTH}`
326       bar_want_bps=`expr ${bar_want_bps} - 1`
327       bar_want_bps=`expr ${bar_want_bps} / ${BAR_WIDTH}`
328
329       # Compute block count per step to keep within maximum block size:
330       bar_count=1
331       if test "${bar_want_bps}" -gt "${BAR_BS}"
332       then
333           bar_count=`expr ${bar_want_bps} + ${BAR_BS}`
334           bar_count=`expr ${bar_count} - 1`
335           bar_count=`expr ${bar_count} / ${BAR_BS}`
336       fi
337
338       # Compute block size for given count:
339       bar_wc=`expr ${BAR_WIDTH} '*' ${bar_count}`
340
341       bar_bs=`expr ${bar_size} + ${bar_wc}`
342       bar_bs=`expr ${bar_bs} - 1`
343       bar_bs=`expr ${bar_bs} / ${bar_wc}`
344
345       # Compute bs * count, the bytes per step:
346       bar_bps=`expr ${bar_bs} '*' ${bar_count}`
347
348       # Compute bytes per hundredth:
349       bar_bph=`expr ${bar_size} + 99`
350       bar_bph=`expr ${bar_bph} / 100`
351
352
353       # Run loop:
354       bar_pos=0
355       bar_graph="${BAR_L}"
356       bar_cur_char=0
357       bar_t0=`eval "${BAR_GET_TIME}" 2>/dev/null` || bar_t0=0
358       for bar_file
359       do
360           # BEGIN TRACE
361           if test "x${BAR_TRACE}" = x1
362           then
363               bar_trace=`expr "${bar_file}" : '.*/\([^/][^/]*\)$'` || bar_trace="${bar_file}"
364               bar_trace=`expr "${bar_trace}${bar_t_space}" : '\('${bar_t_dot}'\)'`
365               bar_trace="${bar_trace} "
366           fi
367           # END TRACE
368           # Initial character position in bar for file:
369           bar_char=`expr ${bar_pos} / ${bar_want_bps}` || bar_char=0
370           while test "${bar_char}" -gt `expr ${bar_cur_char} + 4`
371           do
372               bar_graph="${bar_graph}${BAR_C1}${BAR_C1}${BAR_C1}${BAR_C1}${BAR_C1}"
373               bar_cur_char=`expr ${bar_cur_char} + 5`
374           done
375           while test "${bar_char}" -gt "${bar_cur_char}"
376           do
377               bar_graph="${bar_graph}${BAR_C1}"
378               bar_cur_char=`expr ${bar_cur_char} + 1`
379           done
380
381           # Get file size.  This must work now (we checked with test -f before).
382           bar_size1=`eval "${BAR_GET_SIZE}" 2>/dev/null` || bar_size1=0
383
384           # BEGIN LARGE
385           # Divide and upround by pattern matching:
386           if test -n "${bar_div}"
387           then
388               bar_size1=`expr "${bar_size1}" : '\(.*\)'${bar_div}'$'` || bar_size1=0
389               bar_size1=`expr "${bar_size1}" + 1`
390           fi
391           # END LARGE
392
393           # loop:
394           bar_total=0
395           (
396               exec 6>&1
397               exec 5<"${BAR_DIR}${bar_file}${BAR_EXT}"
398               while test "${bar_total}" -lt "${bar_size1}"
399               do
400                   dd bs="${bar_bs}" count="${bar_count}${bar_numsuff}" <&5 >&6 2>/dev/null
401                   bar_total=`expr ${bar_total} + ${bar_bps}`
402                   if test "${bar_total}" -gt "${bar_size1}"
403                   then
404                       bar_total="${bar_size1}"
405                   fi
406                   bar_pos1=`expr ${bar_pos} + ${bar_total}`
407                   bar_proz=`expr ${bar_pos1} / ${bar_bph}` || bar_proz=0
408                   # BEGIN PERC
409                   if test "x${BAR_PERC}" = x1
410                   then
411                       bar_perc="  ${bar_proz}% "
412                       bar_perc=`expr "${bar_perc}" : '.*\(.....\)$'`
413                   fi
414                   # END PERC
415                   # BEGIN ETA
416                   if test "x${BAR_ETA}" = x1
417                   then
418                       bar_diff=`eval "${BAR_GET_TIME}" 2>/dev/null` || bar_diff=0
419                       bar_diff=`expr ${bar_diff} - ${bar_t0} 2>/dev/null` || bar_diff=0
420                       bar_100p=`expr 100 - ${bar_proz}` || bar_100p=0
421                       bar_diff=`expr ${bar_diff} '*' ${bar_100p}` || bar_diff=0
422                       bar_diff=`expr ${bar_diff} + ${bar_proz}` || bar_diff=0
423                       bar_diff=`expr ${bar_diff} - 1` || bar_diff=0
424                       bar_diff=`expr ${bar_diff} / ${bar_proz} 2>/dev/null` || bar_diff=0
425                       if test "${bar_diff}" -gt 0
426                       then
427                           bar_t_unit=":"
428                           if test "${bar_diff}" -gt 2700
429                           then
430                               bar_t_uni="h"
431                               bar_diff=`expr ${bar_diff} / 60`
432                           fi
433                           bar_diff_h=`expr ${bar_diff} / 60` || bar_diff_h=0
434                           if test "${bar_diff_h}" -gt 99
435                           then
436                               bar_eta="     ${bar_diff_h}${bar_t_unit} "
437                           else
438                               bar_diff_hi=`expr ${bar_diff_h} '*' 60` || bar_diff_hi=0
439                               bar_diff=`expr ${bar_diff} - ${bar_diff_hi}` || bar_diff=0
440                               bar_diff=`expr "00${bar_diff}" : '.*\(..\)$'`
441                               bar_eta="     ${bar_diff_h}${bar_t_unit}${bar_diff} "
442                           fi
443                           bar_eta=`expr "${bar_eta}" : '.*\(......\)$'`
444                       fi
445                   fi
446                   # END ETA
447
448                   bar_char=`expr ${bar_pos1} / ${bar_want_bps}` || bar_char=0
449                   while test "${bar_char}" -gt "${bar_cur_char}"
450                   do
451                       bar_graph="${bar_graph}${BAR_C1}"
452                       ${BAR_ECHO} "${BAR_E_C1}" "
453${bar_trace}${bar_eta}${bar_perc}${bar_graph}${BAR_E_C2}" 1>&2
454                       bar_cur_char=`expr ${bar_cur_char} + 1`
455                   done
456               done
457           ) | eval "${BAR_CMD}"
458           bar_pos=`expr ${bar_pos} + ${bar_size1}`
459       done
460       # ${BAR_ECHO} "${BAR_E_C1}" "${BAR_R}${BAR_E_C2}" 1>&2
461   fi
462
463   if test "${bar_shown}" = 1
464   then
465       # BEGIN TRACE
466       test "x${BAR_TRACE}" = x1 && bar_trace="${bar_t_space} "
467       # END TRACE
468       # BEGIN ETA
469       test "x${BAR_ETA}" = x1   && bar_eta='      '
470       # END ETA
471       if test "x${BAR_CLEAR}" = x1
472       then
473           # BEGIN PERC
474           test "x${BAR_PERC}" = x1 && bar_perc='     '
475           # END PERC
476          bar_lauf=0
477           bar_graph=''
478           while test `expr ${bar_lauf} + 5` -le "${BAR_WIDTH}"
479           do
480               bar_graph="${bar_graph}     "
481               bar_lauf=`expr ${bar_lauf} + 5`
482           done
483           while test "${bar_lauf}" -lt "${BAR_WIDTH}"
484           do
485               bar_graph="${bar_graph} "
486               bar_lauf=`expr ${bar_lauf} + 1`
487           done
488           ${BAR_ECHO} "${BAR_E_C1}" "
489${bar_trace}${bar_eta}${bar_perc} ${bar_graph}
490${BAR_E_C2}" 1>&2
491       else
492           # BEGIN PERC
493           test "x${BAR_PERC}" = x1 && bar_perc='100% '
494           # END PERC
495           bar_lauf=0
496           bar_graph=''
497           while test `expr ${bar_lauf} + 5` -le "${BAR_WIDTH}"
498           do
499               bar_graph="${bar_graph}${BAR_C1}${BAR_C1}${BAR_C1}${BAR_C1}${BAR_C1}"
500               bar_lauf=`expr ${bar_lauf} + 5`
501           done
502           while test "${bar_lauf}" -lt "${BAR_WIDTH}"
503           do
504               bar_graph="${bar_graph}${BAR_C1}"
505               bar_lauf=`expr ${bar_lauf} + 1`
506           done
507           ${BAR_ECHO} "${BAR_E_C1}" "
508${bar_trace}${bar_eta}${bar_perc}${BAR_L}${bar_graph}${BAR_R}${BAR_E_C2}" 1>&2
509           ${BAR_E_NL} 1>&2
510       fi
511   fi
512}
513####>-SCHNAPP-<########################################################
514
515
516BAR_AWK_0=''
517# Command line interface:
518while test -n "$1"
519do
520   case "$1" in
521       -o|-c|-w|-0|-1|-e|-d|-b|-s|-\[\]|-\[|-\]|-T)
522           if test -z "$2"
523           then
524               echo "$0: Error: A non-empty argument was expected after $1" 1>&2
525           fi
526           BAR_ARG="$1"
527           BAR_OPT="$2"
528           shift
529           shift
530       ;;
531       -o*|-c*|-w*|-0*|-1*|-e*|-d*|-b*|-T*)
532           BAR_ARG=`expr "$1" : '\(-.\)'`
533           BAR_OPT=`expr "$1" : '-.\(.*\)$'`
534           shift
535       ;;
536       -h|-n|-p|-D|-D-|-q|-V|-t|-E|-L)
537           BAR_ARG="$1"
538           BAR_OPT=""
539           shift
540       ;;
541       --) shift
542           break
543       ;;
544       -*) echo "$0: Error: Unrecognized option: $1" 1>&2
545           exit 1
546       ;;
547       *)
548           break
549       ;;
550   esac
551
552   case "${BAR_ARG}" in
553       -h)  echo 'Usage: bar [-n] [-p] [-q] [-o FILE] [-c CMD] [-s SIZE] [-b SIZE]'
554            echo '           [-w WIDTH] [-0/1/[/] CHAR] [-d DIR] [-e EXT] [Files]'
555            echo '       bar -V'
556            echo '       bar -D'
557            echo '       bar -D-'
558            echo 'Options:'
559            echo '     -h         displays help'
560            echo '     -o FILE    sets output file'
561            echo '     -c CMD     sets individual execution command'
562            echo '     -e EXT     append an extension to each file'
563            echo '     -d DIR     prepend this prefix to each file (a directory must end in /)'
564            echo '     -s SIZE    expected number of bytes.  Use for pipes.  This is a hint'
565            echo '                only that must be greater or equal to the amount actually'
566            echo '                processed.  Further, this only works for single files.'
567            echo '     -b SIZE    maximal block size (bytes) (default: 1048567)'
568            echo '     -w WIDTH   width in characters        (default: terminal width-3 or 76)'
569            echo '     -0 CHAR    character for empty bar    (default: .)'
570            echo '     -1 CHAR    character for full bar     (default: =)'
571            echo '     -[ CHAR    first character of bar     (default: [)'
572            echo '     -] CHAR    last  character of bar     (default: ])'
573            echo '     -n         clears bar after termination'
574            echo '     -t         traces (=displays) which file is processed'
575            echo '     -T WIDTH   no of characters reserved for the file display of -t'
576            echo '     -p         hides percentage'
577            echo '     -E         hides estimated time display'
578            echo '     -q         hides the whole bar, be quiet'
579            echo '     -D         tries to dump the bar_cat() shell function, then exit.'
580            echo '                Here, -t, -p, -E remove the corresponding feature completely.'
581            echo '                Further, -L removes large file support from the code.'
582            echo '     -D-        same as -D, but dumps the function body only'
583            echo '     -V         displays version number'
584            echo '     --         end of options: only file names follow'
585            exit 0
586       ;;
587       -n)  BAR_CLEAR=1
588       ;;
589       -L)  BAR_LARGE=0
590            BAR_AWK_0="${BAR_AWK_0} /END  *LARGE/ {x=1} ;"
591            BAR_AWK_0="${BAR_AWK_0} /BEGIN  *LARGE/ {x=0} ;"
592       ;;
593       -t)  BAR_TRACE=1
594            BAR_AWK_0="${BAR_AWK_0} /END  *TRACE/ {x=1} ;"
595            BAR_AWK_0="${BAR_AWK_0} /BEGIN  *TRACE/ {x=0} ;"
596       ;;
597       -T)  BAR_TRACE_WIDTH="${BAR_OPT}"
598       ;;
599       -q)  BAR_OK=0
600       ;;
601       -p)  BAR_PERC=0
602            BAR_AWK_0="${BAR_AWK_0} /END  *PERC/ {x=1} ;"
603            BAR_AWK_0="${BAR_AWK_0} /BEGIN  *PERC/ {x=0} ;"
604       ;;
605       -E)  BAR_ETA=0
606            BAR_AWK_0="${BAR_AWK_0} /END  *ETA/ {x=1} ;"
607            BAR_AWK_0="${BAR_AWK_0} /BEGIN  *ETA/ {x=0} ;"
608       ;;
609       -V)  echo "bar v${BAR_VERSION}"
610            exit 0
611       ;;
612       -D)  echo "BAR_VERSION=${BAR_VERSION}"
613            awk "${BAR_AWK_0}"'{sub(/ *#.*$/,"")} ; /^bar_cat/ {x=1} ; {sub(/^  */,"")} ; /./ {if(x)print} ; /^}/ {x=0}' "$0"
614            exit 0
615       ;;
616       -D-) echo "BAR_VERSION=${BAR_VERSION}"
617            awk "${BAR_AWK_0}"'{sub(/ *#.*$/,"")} ; /^}/ {x=0} ; {sub(/^  */,"")} ; /./ {if(x)print} ; /^{/ {x=1}' "$0"
618            exit 0
619       ;;
620       -o)  exec 1>"${BAR_OPT}"
621       ;;
622       -c)  BAR_CMD="${BAR_OPT}"
623       ;;
624       -b)  BAR_BS="${BAR_OPT}"
625            if BAR_RAW=`expr "${BAR_BS}" : '\(.*\)k$'`
626            then
627                BAR_BS=`expr ${BAR_RAW} '*' 1024`
628            elif BAR_RAW=`expr "${BAR_BS}" : '\(.*\)M$'`
629            then
630                BAR_BS=`expr ${BAR_RAW} '*' 1048567`
631            fi
632       ;;
633       -s)  BAR_SIZE="${BAR_OPT}"
634            if BAR_RAW=`expr "${BAR_SIZE}" : '\(.*\)k$'`
635            then
636                BAR_SIZE=`expr ${BAR_RAW} '*' 1024`
637            elif BAR_RAW=`expr "${BAR_SIZE}" : '\(.*\)M$'`
638            then
639                BAR_SIZE=`expr ${BAR_RAW} '*' 1048567`
640            fi
641            if test "$#" -gt 1
642            then
643                echo "Error: -s cannot be specified for multiple input files." 1>&2
644                exit 1
645            fi
646       ;;
647       -e)  BAR_EXT="${BAR_OPT}"
648       ;;
649       -d)  BAR_DIR="${BAR_OPT}"
650       ;;
651       -0)  BAR_C0="${BAR_OPT}"
652       ;;
653       -1)  BAR_C1="${BAR_OPT}"
654       ;;
655       -\[) BAR_L="${BAR_OPT}"
656       ;;
657       -\]) BAR_R="${BAR_OPT}"
658       ;;
659       -\[\])
660            BAR_L="${BAR_OPT}"
661            BAR_R="${BAR_OPT}"
662       ;;
663       -w)  BAR_WIDTH="${BAR_OPT}"
664       ;;
665    esac
666done
667
668# Invoke main function:
669if test "$#" = 0
670then
671    bar_cat /dev/stdin
672else
673    bar_cat "$@"
674fi
675