forked from wofr06/lesspipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesspipe.sh.in
executable file
·1297 lines (1263 loc) · 41 KB
/
lesspipe.sh.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# lesspipe.sh, a preprocessor for less (version 1.87)
#===============================================================================
### THIS FILE IS GENERATED FROM lesspipe.sh.in, PLEASE GET THE ZIP FILE
### from https://github.com/wofr06/lesspipe.sh/archive/lesspipe.zip
### AND RUN configure TO GENERATE A lesspipe.sh THAT WORKS IN YOUR ENVIRONMENT
#===============================================================================
#
# Usage: lesspipe.sh is called when the environment variable LESSOPEN is set:
# LESSOPEN="|lesspipe.sh %s"; export LESSOPEN (sh like shells)
# setenv LESSOPEN "|lesspipe.sh %s" (csh, tcsh)
# Use the fully qualified path if lesspipe.sh is not in the search path
# View files in multifile archives:
# less archive_file:contained_file
# This can be used to extract ASCII files from a multifile archive:
# less archive_file:contained_file>extracted_file
# As less is not good for extracting raw data use instead:
# lesspipe.sh archive_file:contained_file>extracted_file
# Even a file in a multifile archive that itself is contained in yet
# another archive can be viewed this way:
# less super_archive:archive_file:contained_file
# Display the last file in the file1:..:fileN chain in raw format:
# Suppress input filtering: less file1:..:fileN: (append a colon)
# Suppress decompression: less file1:..:fileN:: (append 2 colons)
#
# Required programs and supported formats: see the separate file README
# License: GPL (see file LICENSE)
# History: see the separate file ChangeLog
# Author: Wolfgang Friebel, DESY (Wolfgang.Friebel AT desy.de)
#
#===============================================================================
setopt KSH_ARRAYS SH_WORD_SPLIT 2>/dev/null
set +o noclobber
#ifdef tar
tarcmd='tar'
#endif
dir=${LESSOPEN#\|}
dir=${dir%%lesspipe.sh*\%s}
dir=${dir%%/}
# look for commands also where lesspipe.sh is stored
PATH=$PATH:$dir
cmd_exist () {
command -v "$1" > /dev/null 2>&1 && return 0 || return 1
}
if [[ "$LESS_ADVANCED_PREPROCESSOR" = '' ]]; then
NOL_A_P=_NO_L_A_P
fi
filecmd() {
file -L -s "$@"
file -L -s --mime "$@" 2> /dev/null | sed -n 's/.*charset=/;/p' | tr [:lower:] [:upper:]
}
TMPDIR=${TMPDIR:-/tmp}
sep=: # file name separator
altsep== # alternate separator character
# if the separator is part of the file name then altsep is used
if [[ -e "$1" && "$1" = *$sep* ]]; then
sep=$altsep
xxx="${1%=}"
set "$xxx"
fi
tmpdir="$TMPDIR"/lesspipe."$RANDOM"
mkdir "$tmpdir"
nexttmp () {
new="$tmpdir/lesspipe.$RANDOM"
[[ "$1" = -d ]] && mkdir "$new"
echo "$new"
}
[[ -d "$tmpdir" ]] || exit 1
trap "rm -rf '$tmpdir'" 0
trap - PIPE
unset iconv
iconv() {
if [[ -z "$iconv" ]]; then
arg=$(printf "%s$(command iconv --help 2>/dev/null | \
sed -n 's/.*\(--.*-subst=\)\(FORMATSTRING\).*/\1\\033[7m?\\033[m/p' | \
tr \\n ' ')")
if [[ -n "$arg" ]]; then
iconv="command iconv -c $arg -t //TRANSLIT"
else
iconv="command iconv -c"
fi
fi
if $iconv "$@" > /dev/null 2>&1; then
msg "append $sep to filename to view the $2 encoded data"
$iconv "$@"
fi
}
msg () {
if [[ -n "$LESSQUIET" ]]; then
return
fi
echo "==> $@"
}
filetype () {
# wrapper for 'file' command
typeset name
name="$1"
if [[ "$1" = - ]]; then
name="$filen"
fi
# ifdef brotli
if [[ ("$name" = *.br || "$name" = *.bro || "$name" = *.tbr) ]]; then
# In current format, brotli can only be detected by extension
echo " brotli compressed data"
return
fi
# endif
if [[ "$1" = - ]]; then
dd bs=40000 count=1 > "$tmpdir/file" 2>/dev/null
set "$tmpdir/file" "$2"
fi
typeset return
# file -b not supported by all versions of 'file'
type="$(filecmd "$1" | cut -d : -f 2- )"
if [[ "$type" = " empty" ]]; then
# exit if file returns "empty" (e.g., with "less archive:nonexisting_file")
exit 1
# Open Office
elif [[ "$type" = *OpenDocument\ Text* ]]; then
return=" OpenDocument Text"
elif [[ "$type" = *OpenDocument\ * || "$type" = *OpenOffice\.org\ 1\.x\ * ]]; then
return=" OpenDocument"
# Microsoft Office < 2007
elif [[ "$type" = *Microsoft\ Office\ Document* && ("$name" = *.do[st]) ]] ||
[[ "$type" = *Microsoft\ Office\ Word* ]]; then
return=" Microsoft Word Document"
elif [[ "$type" = *Microsoft\ Office\ Document* && ("$name" = *.pp[st]) ]] ||
[[ "$type" = *Microsoft\ Office\ PowerPoint* ]]; then
return=" Microsoft PowerPoint Document"
elif [[ "$type" = *Microsoft\ Office\ Document* && ("$name" = *.xl[mst]) ]] ||
[[ "$type" = *Microsoft\ Excel* ]]; then
return=" Microsoft Excel Document"
elif [[ "$type" = *Microsoft\ Office\ Document* || "$type" = *Composite\ Document\ File\ V2* ]]; then
return=" Microsoft Office Document"
# Microsoft Office >= 2007
elif [[ ("$type" = *Zip\ archive* || "$type" = Microsoft\ OOXML) && "$name" = *.do[ct][xm] ]] ||
[[ "$type" = *Microsoft\ Word\ 2007* ]]; then
return=" Microsoft Word 2007+"
elif [[ ("$type" = *Zip\ archive* || "$type" = Microsoft\ OOXML) && "$name" = *.pp[st][xm] ]] ||
[[ "$type" = *Microsoft\ PowerPoint\ 2007* ]]; then
return=" Microsoft PowerPoint 2007+"
elif [[ ("$type" = *Zip\ archive* || "$type" = Microsoft\ OOXML) && "$name" = *.xl[st][xmb] ]] ||
[[ "$type" = *Microsoft\ Excel\ 2007* ]]; then
return=" Microsoft Excel 2007+"
elif [[ "$type" = Microsoft\ OOXML ]] ||
[[ "$type" = *Microsoft\ *\ 2007* ]]; then
return=" Microsoft Office 2007"
# MP3
elif [[ "$type" = *MPEG\ *layer\ 3\ audio* || "$type" = *MPEG\ *layer\ III* || "$type" = *mp3\ file* || "$type" = *MP3* ]]; then
return="mp3"
# Compressed Archives
elif [[ "$type" != *lzip\ compressed* && ("$name" = *.lzma || "$name" = *.tlz) ]]; then
return=" LZMA compressed data"
elif [[ ("$type" = *Zip* || "$type" = *ZIP* || "$type" = *JAR*) && ("$name" = *.jar || "$name" = *.xpi) ]]; then
return=" Zip compressed Jar archive"
elif [[ "$type" = *Hierarchical\ Data\ Format* && ("$name" = *.nc4) ]]; then
return=" NetCDF Data Format data"
# Sometimes a BSD makefile is identified as "troff or preprocessor input
# text" probably due to its ".if" style directives.
elif [[ "$type" = *roff\ *,* && ("$name" = */[Mm]akefile || "$name" = */[Mm]akefile.* || "$name" = */BSDmakefile || "$name" = *.mk) ]]; then
return=" BSD makefile script,${type#*,}}"
# Correct HTML Detection
elif [[ ("$type" = *HTML* || "$type" = *ASCII*) && "$name" = *xml ]]; then
return=" XML document text"
elif [[ "$type" = *XML* && "$name" = *html ]]; then
return=" HTML document text"
fi
if [[ -n "$return" ]]; then
echo "$return"
return
fi
# file -b not supported by all versions of 'file'
mime="$(file --mime "$1" | cut -d : -f 2-)"
if [[ "$mime" = \ text/* ]]; then
return="text"
elif [[ "$mime" = \ image/* ]]; then
return="image"
elif [[ "$mime" = \ audio/* ]]; then
return="audio"
elif [[ "$mime" = \ video/* ]]; then
return="video"
fi
if [[ -n "$return" ]]; then
echo "$return"
return
fi
if [[ -n "$type" ]]; then
return="$type"
elif [[ -n "$mime" ]]; then
return="$mime"
else
return=""
fi
echo "$return"
}
show () {
file1="${1%%$sep*}"
rest1="${1#$file1}"
while [[ "$rest1" = ::* ]]; do
if [[ "$rest1" = "::" ]]; then
break
else
rest1="${rest1#$sep$sep}"
file1="${rest1%%$sep*}"
rest1="${rest1#$file1}"
file1="${1%$rest1}"
fi
done
if [[ ! -e $file1 && "$file1" != '-' ]]; then
return
fi
rest11="${rest1#$sep}"
file2="${rest11%%$sep*}"
rest2="${rest11#$file2}"
while [[ "$rest2" = ::* ]]; do
if [[ "$rest2" = "::" ]]; then
break
else
rest2="${rest2#$sep$sep}"
file2="${rest2%%$sep*}"
rest2="${rest2#$file2}"
file2="${rest11%$rest2}"
fi
done
if [[ "$file2" != "" ]]; then
in_file="-i$file2"
fi
rest2="${rest11#$file2}"
rest11="$rest1"
if cmd_exist html2text || cmd_exist elinks || cmd_exist links || cmd_exist lynx || cmd_exist w3m; then
PARSEHTML=yes
else
PARSEHTML=no
fi
if [[ "$cmd" = "" ]]; then
type=$(filetype "$file1") || exit 1
#ifdef lsbom
if cmd_exist lsbom; then
if [[ ! -f "$file1" ]]; then
if [[ "$type" = *directory* ]]; then
if [[ "$file1" = *.pkg ]]; then
if [[ -f "$file1/Contents/Archive.bom" ]]; then
type="bill of materials"
file1="$file1/Contents/Archive.bom"
msg "This is a Mac OS X archive directory, showing its contents (bom file)"
fi
fi
fi
fi
fi
#endif
get_cmd "$type" "$file1" "$rest1"
if [[ "$cmd" != "" ]]; then
show "-$rest1"
else
isfinal "$type" "$file1" "$rest11"
fi
elif [[ "$c1" = "" ]]; then
set -A c1 "${cmd[@]}"
type=$("${c1[@]}" | filetype -) || exit 1
get_cmd "$type" "$file1" "$rest1"
if [[ "$cmd" != "" ]]; then
show "-$rest1"
else
"${c1[@]}" | isfinal "$type" - "$rest11"
fi
elif [[ "$c2" = "" ]]; then
set -A c2 "${cmd[@]}"
type=$("${c1[@]}" | "${c2[@]}" | filetype -) || exit 1
get_cmd "$type" "$file1" "$rest1"
if [[ "$cmd" != "" ]]; then
show "-$rest1"
else
"${c1[@]}" | "${c2[@]}" | isfinal "$type" - "$rest11"
fi
elif [[ "$c3" = "" ]]; then
set -A c3 "${cmd[@]}"
type=$("${c1[@]}" | "${c2[@]}" | "${c3[@]}" | filetype -) || exit 1
get_cmd "$type" "$file1" "$rest1"
if [[ "$cmd" != "" ]]; then
show "-$rest1"
else
"${c1[@]}" | "${c2[@]}" | "${c3[@]}" | isfinal "$type" - "$rest11"
fi
elif [[ "$c4" = "" ]]; then
set -A c4 "${cmd[@]}"
type=$("${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | filetype -) || exit 1
get_cmd "$type" "$file1" "$rest1"
if [[ "$cmd" != "" ]]; then
show "-$rest1"
else
"${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | isfinal "$type" - "$rest11"
fi
elif [[ "$c5" = "" ]]; then
set -A c5 "${cmd[@]}"
type=$("${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | "${c5[@]}" | filetype -) || exit 1
get_cmd "$type" "$file1" "$rest1"
if [[ "$cmd" != "" ]]; then
echo "$0: Too many levels of encapsulation"
else
"${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | "${c5[@]}" | isfinal "$type" - "$rest11"
fi
fi
}
get_cmd () {
cmd=
typeset t
if [[ "$1" = *[bg]zip*compress* || "$1" = *compress\'d\ * || "$1" = *packed\ data* || "$1" = *LZMA\ compressed* || "$1" = *lzip\ compressed* || "$1" = *[Xx][Zz]\ compressed* || "$1" = *Zstandard\ compressed* || "$1" = *[Bb]rotli\ compressed* || "$1" = *LZ4\ compressed* ]]; then ## added '#..then' to fix vim's syntax parsing
if [[ "$3" = $sep$sep ]]; then
return
#ifdef bzip2
elif [[ "$1" = *bzip*compress* ]] && cmd_exist bzip2; then
set -A cmd bzip2 -cd "$2"
if [[ "$2" != - ]]; then filen="$2"; fi
case "$filen" in
*.bz2) filen="${filen%.bz2}";;
*.tbz) filen="${filen%.tbz}.tar";;
esac
return
#endif
#ifdef lzip
elif [[ "$1" = *lzip\ compressed* ]] && cmd_exist lzip; then
set -A cmd lzip -cd "$2"
if [[ "$2" != - ]]; then filen="$2"; fi
case "$filen" in
*.lz) filen="${filen%.lz}";;
*.tlz) filen="${filen%.tlz}.tar";;
esac
#endif
#ifdef lzma
# preliminary minimal support for lzma (not in magic file yet)
elif [[ "$1" = *LZMA\ compressed* ]] && cmd_exist lzma; then
set -A cmd lzma -cd "$2"
if [[ "$2" != - ]]; then filen="$2"; fi
case "$filen" in
*.lzma) filen="${filen%.lzma}";;
*.tlz) filen="${filen%.tlz}.tar";;
esac
#endif
elif [[ "$1" = *gzip\ compress* || "$1" = *compress\'d\ * || "$1" = *packed\ data* ]]; then ## added '#..then' to fix vim's syntax parsing
set -A cmd gzip -cd "$2"
if [[ "$2" != - ]]; then filen="$2"; fi
case "$filen" in
*.gz) filen="${filen%.gz}";;
*.tgz) filen="${filen%.tgz}.tar";;
esac
#ifdef xz
elif [[ "$1" = *[Xx][Zz]\ compressed* ]] && cmd_exist xz; then
set -A cmd xz -cd "$2"
if [[ "$2" != - ]]; then filen="$2"; fi
case "$filen" in
*.xz) filen="${filen%.xz}";;
*.txz) filen="${filen%.txz}.tar";;
esac
#endif
#ifdef zstd
elif [[ "$1" = *Zstandard\ compressed* ]] && cmd_exist zstd; then
set -A cmd zstd -cdqM1073741824 "$2"
if [[ "$2" != - ]]; then filen="$2"; fi
case "$filen" in
*.zst) filen="${filen%.zst}";;
*.tzst) filen="${filen%.tzst}.tar";;
esac
#endif
#ifdef brotli
elif [[ "$1" = *[Bb]rotli\ compressed* ]] && cmd_exist brotli; then
set -A cmd brotli -cd -- "$2"
if [[ "$2" != - ]]; then filen="$2"; fi
case "$filen" in
*.br|*.bro) filen="${filen%.*}";;
*.tbr) filen="${filen%.*}.tar";;
esac
#endif
#ifdef lz4
elif [[ "$1" = *LZ4\ compressed* ]] && cmd_exist lz4; then
set -A cmd lz4 -cdq "$2"
if [[ "$2" != - ]]; then filen="$2"; fi
case "$filen" in
*.lz4) filen="${filen%.*}";;
*.tl4|*.tz4|*.tlz4) filen="${filen%.*}.tar";;
esac
#endif
fi
return
fi
rsave="$rest1"
rest1="$rest2"
if [[ "$file2" != "" ]]; then
if [[ "$1" = *\ tar* || "$1" = *\ tar* ]]; then
set -A cmd istar "$2" "$file2"
elif [[ "$1" = *Debian* ]]; then
data="$(ar t "$2"|grep data.tar)"
set -A cmd2 "unpack_cmd" "$data"
t=$(nexttmp)
if [[ "$file2" = control/* ]]; then
istemp "ar p" "$2" control.tar.gz | gzip -dc - > "$t"
file2=".${file2:7}"
else
istemp "ar p" "$2" "$data" | $("${cmd2[@]}") > "$t"
fi
set -A cmd istar "$t" "$file2"
#ifdef rpm2cpio,cpio
elif [[ "$1" = *RPM* ]] && cmd_exist cpio && ( cmd_exist rpm2cpio || cmd_exist rpmunpack ); then
set -A cmd isrpm "$2" "$file2"
#elif rpmunpack,cpio
elif [[ "$1" = *RPM* ]] && cmd_exist cpio && cmd_exist rpmunpack; then
set -A cmd isrpm "$2" "$file2"
#endif
#ifdef fastjar
elif [[ "$1" = *Jar\ archive* || "$1" = *Java\ archive* ]] && cmd_exist fastjar; then
set -A cmd isjar "$2" "$file2"
#endif
#ifdef unzip
elif [[ "$1" = *Zip* || "$1" = *ZIP* || "$1" = *JAR* ]] && cmd_exist unzip; then
set -A cmd istemp "unzip -avp" "$2" "$file2"
#endif
#ifdef unrar
elif [[ "$1" = *RAR\ archive* ]]; then
if cmd_exist unrar; then
set -A cmd istemp "unrar p -inul" "$2" "$file2"
elif cmd_exist rar; then
set -A cmd istemp "rar p -inul" "$2" "$file2"
elif cmd_exist bsdtar; then
set -A cmd istemp "bsdtar Oxf" "$2" "$file2"
fi
#elif rar
elif [[ "$1" = *RAR\ archive* ]] && cmd_exist rar; then
set -A cmd istemp "rar p -inul" "$2" "$file2"
#elif bsdtar
elif [[ "$1" = *RAR\ archive* ]] && cmd_exist bsdtar; then
set -A cmd istemp "bsdtar Oxf" "$2" "$file2"
#endif
#ifdef 7za
elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7za; then
set -A cmd istemp "7za e -so" "$2" "$file2"
elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7zr; then
set -A cmd istemp "7zr e -so" "$2" "$file2"
#elif 7zr
elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7zr; then
set -A cmd istemp "7zr e -so" "$2" "$file2"
#endif
#ifdef cabextract
elif [[ "$1" = *[Cc]abinet* ]] && cmd_exist cabextract; then
set -A cmd iscab "$2" "$file2"
#endif
elif [[ "$1" = *\ ar\ archive* ]]; then
set -A cmd istemp "ar p" "$2" "$file2"
#ifdef isoinfo
elif [[ "$1" = *ISO\ 9660* ]] && cmd_exist isoinfo; then
set -A cmd isoinfo "-i$2" "-x$file2"
#endif
fi
if [[ "$cmd" != "" ]]; then
filen="$file2"
fi
fi
}
#ifdef cabextract
iscab () {
typeset t
if [[ "$1" = - ]]; then
t=$(nexttmp)
cat > "$t"
set "$t" "$2"
fi
cabextract -pF "$2" "$1"
}
#endif
istar () {
$tarcmd Oxf "$1" "$2" 2>/dev/null
}
#ifdef dvi2tty
isdvi () {
typeset t
if [[ "$1" != *.dvi ]]; then
t="$tmpdir/tmp.dvi"
cat "$1" > "$t"
set "$t"
fi
dvi2tty -q "$1"
}
#endif
# istemp wrapper for helpers that cannot read from stdin
istemp () {
typeset prog
typeset t
prog="$1"
t="$2"
shift
shift
if [[ "$t" = - ]]; then
t=$(nexttmp)
cat > "$t"
fi
if [[ $# -gt 0 ]]; then
$prog "$t" "$@" 2>/dev/null
else
$prog "$t" 2>/dev/null
fi
}
# nodash wrapper for helpers who don't understand '-' but can deal with stdin
nodash () {
typeset prog
prog="$1"
shift
if [[ "$1" = - ]]; then
shift
if [[ $# -gt 0 ]]; then
$prog "$@" 2>/dev/null
else
$prog 2>/dev/null
fi
else
$prog "$@" 2>/dev/null
fi
}
isrpm () {
if cmd_exist rpm2cpio && cmd_exist cpio; then
typeset t
if [[ "$1" = - ]]; then
t=$(nexttmp)
cat > "$t"
set "$t" "$2"
fi
# setup $b as a batch file containing "$b.out"
typeset b
# nasty hack to make rpm inspection work on MacOSX (and other BSD based systems)
#ifdef bsd_cpio
b="$(nexttmp -d)"
rpm2cpio "$1" 2>/dev/null|(cd "$b" || return; pax -r "$2" 2>/dev/null)
cat "$b/$2"
#else
b=$(nexttmp)
echo "$b.out" > "$b"
# to support older versions of cpio the --to-stdout option is not used here
rpm2cpio "$1" 2>/dev/null|cpio -i --quiet --rename-batch-file "$b" "$2"
cat "$b.out"
#endif
elif cmd_exist rpmunpack && cmd_exist cpio; then
#ifdef bsd_cpio
b=$(nexttmp -d)
cat "$1" | rpmunpack | gzip -cd |(cd "$b" || return; pax -r "$2" 2>/dev/null)
cat "$b/$2"
#else
# rpmunpack will write to stdout if it gets file from stdin
# extract file $2 from archive $1, assume that cpio is sufficiently new
# (option --to-stdout existing) if rpmunpack is installed
cat "$1" | rpmunpack | gzip -cd | cpio -i --quiet --to-stdout "$2"
#endif
fi
}
#ifdef fastjar
isjar () {
case "$2" in
/*) echo "lesspipe can't unjar files with absolute paths" >&2
exit 1
;;
../*) echo "lesspipe can't unjar files with ../ paths" >&2
exit 1
;;
esac
typeset d
d=$(nexttmp -d)
[[ -d "$d" ]] || exit 1
cat "$1" | (
cd "$d" || return
fastjar -x "$2"
if [[ -f "$2" ]]; then
cat "$2"
fi
)
}
#endif
# TODO
#parsexml () { nodash "elinks -dump -default-mime-type text/xml" "$1"; }
parsehtml () {
if [[ "$PARSEHTML" = no ]]; then
msg "No suitable tool for HTML parsing found, install one of html2text, elinks, links, lynx or w3m"
return
#ifdef html2text
elif cmd_exist html2text; then
if [[ "$1" = - ]]; then html2text -utf8 2>/dev/null ||
html2text -from_encoding utf-8
else html2text -utf8 "$1" 2>/dev/null ||
html2text -from_encoding utf-8 "$1"; fi
#ifdef lynx
elif cmd_exist lynx; then
if [[ "$1" = - ]]; then set - -stdin; fi
lynx -dump -force_html "$1" && return
#endif
#ifdef w3m
elif cmd_exist w3m; then
nodash "w3m -dump -T text/html" "$1"
#endif
#ifdef elinks
elif cmd_exist elinks; then
nodash "elinks -dump -force-html" "$1"
#endif
#ifdef links
elif cmd_exist links; then
if [[ "$1" = - ]]; then set - -stdin; fi
links -dump -force_html "$1"
#endif
fi
}
unpack_cmd() {
cmd_string="cat"
if [[ "$1" == *xz ]]; then
cmd_string="xz -dc -"
elif [[ "$1" == *gz ]]; then
cmd_string="gzip -dc -"
elif [[ "$1" == *bz2 ]]; then
cmd_string="bzip2 -dc -"
elif [[ "$1" == *lzma ]]; then
cmd_string="lzma -dc -"
elif [[ "$1" == *zst ]]; then
cmd_string="zstd -dcqM1073741824 -"
elif [[ ("$1" == *br || "$1" == *bro) ]]; then
cmd_string="brotli -dc -"
elif [[ "$1" == *lz4 ]]; then
cmd_string="lz4 -dcq -"
fi
echo "$cmd_string"
}
isfinal() {
# color requires -r or -R when calling less
typeset COLOR
if [[ $(tput colors) -ge 8 && ("$LESS" = *-*r* || "$LESS" = *-*R*) ]]; then
COLOR="--color=always"
else
COLOR="--color=auto"
fi
typeset t
if [[ $3 = $sep$sep ]]; then
cat "$2"
return
elif [[ $3 = $sep* ]]; then
# the next conditional is only there to conditionally remove syntax highlighting code
#ifdef perl
if [[ $3 = "$sep" ]]; then
msg "append :. or :<filetype> to activate syntax highlighting"
else
lang=${3#$sep}
lang="-l${lang#.}"
lang=${lang%%-l }
if cmd_exist bat; then
if [[ "$lang" = "-l" ]]; then
bat $COLOR "$2"
else
bat $COLOR "$lang" "$2"
fi
[[ $? = 0 ]] && return
elif cmd_exist batcat; then
if [[ "$lang" = "-l" ]]; then
batcat $COLOR "$2"
else
batcat $COLOR "$lang" "$2"
fi
[[ $? = 0 ]] && return
elif cmd_exist code2color; then
code2color $PPID ${in_file:+"$in_file"} "$lang" "$2"
[[ $? = 0 ]] && return
fi
fi
#endif
cat "$2"
return
fi
lang="$(echo $LANG | tr '[:upper:]' '[:lower:]')"
if [[ "$1" = *No\ such* ]]; then
exit 1
elif [[ "$1" = *directory* ]]; then
set -A cmd ls -lA $COLOR "$2"
if ! ls $COLOR > /dev/null 2>&1; then
set -A cmd ls -lA -G "$2"
if ! ls -lA -G > /dev/null 2>&1; then
set -A cmd ls -lA "$2"
fi
fi
msg "This is a directory, showing the output of ${cmd[@]}"
if [[ ${cmd[2]} = '-G' ]]; then
CLICOLOR_FORCE=1 "${cmd[@]}"
else
"${cmd[@]}"
fi
elif [[ "$1" = *\ tar* || "$1" = *\ tar* ]]; then
msg "use tar_file${sep}contained_file to view a file in the archive"
#ifdef tarcolor
if [[ -n $COLOR ]] && cmd_exist tarcolor; then
$tarcmd tvf "$2" | tarcolor
else
$tarcmd tvf "$2"
fi
#else
$tarcmd tvf "$2"
#endif
#ifdef rpm,rpm2cpio,cpio
elif [[ "$1" = *RPM* ]]; then
header="use RPM_file${sep}contained_file to view a file in the RPM"
if cmd_exist rpm; then
echo "$header"
istemp "rpm -qivp" "$2"
header="";
fi
if cmd_exist cpio && cmd_exist rpm2cpio; then
echo $header
echo "================================= Content ======================================"
istemp rpm2cpio "$2" 2>/dev/null|cpio -i -tv 2>/dev/null
elif cmd_exist cpio && cmd_exist rpmunpack; then
echo $header
echo "================================= Content ======================================"
cat "$2" | rpmunpack | gzip -cd | cpio -i -tv 2>/dev/null
else
msg "please install rpm2cpio or rpmunpack to see the contents of RPM files"
fi
#elif rpm2cpio,cpio
elif [[ "$1" = *RPM* ]]; then
if cmd_exist cpio && cmd_exist rpm2cpio; then
msg "use RPM_file${sep}contained_file to view a file in the RPM"
echo "================================= Content ======================================"
istemp rpm2cpio "$2" 2>/dev/null|cpio -i -tv --quiet
elif cmd_exist cpio && cmd_exist rpmunpack; then
msg "use RPM_file${sep}contained_file to view a file in the RPM"
echo "================================= Content ======================================"
cat "$2" | rpmunpack | gzip -cd | cpio -i -tv --quiet
else
msg "please install rpm2cpio or rpmunpack to see the contents of RPM files"
fi
#elif rpmunpack,cpio
elif [[ "$1" = *RPM* ]]; then
if cmd_exist cpio && cmd_exist rpmunpack; then
msg "use RPM_file${sep}contained_file to view a file in the RPM"
echo "================================= Content ======================================"
cat "$2" | rpmunpack | gzip -cd | cpio -i -tv --quiet
else
msg "please install rpm2cpio or rpmunpack to see the contents of RPM files"
fi
#endif
#ifdef groff
elif [[ "$1" = *roff* ]] && cmd_exist groff; then
DEV=utf8
if [[ $lang != *utf*8* ]]; then
if [[ "$lang" = ja* ]]; then
DEV=nippon
else
DEV=latin1
fi
fi
MACRO=andoc
if [[ "$2" = *.me ]]; then
MACRO=e
elif [[ "$2" = *.ms ]]; then
MACRO=s
fi
msg "append $sep to filename to view the nroff source"
groff -s -p -t -e -T$DEV -m$MACRO "$2"
#endif
elif [[ "$1" = *Debian* ]]; then
msg "use Deb_file${sep}contained_file to view a file in the Deb"
#ifdef dpkg
if cmd_exist dpkg; then
nodash "dpkg -I" "$2"
else
echo
istemp "ar p" "$2" control.tar.gz | gzip -dc - | $tarcmd tvf - | sed -r 's/(.{48})\./\1control/'
fi
#else
echo
istemp "ar p" "$2" control.tar.gz | gzip -dc - | $tarcmd tvf - | sed -r 's/(.{48})\./\1control/'
#endif
data=$(ar t "$2"|grep data.tar)
set -A cmd2 "unpack_cmd" "$data"
echo
istemp "ar p" "$2" "$data" | $("${cmd2[@]}") | $tarcmd tvf -
#ifdef perldoc
# do not display all perl text containing pod using perldoc
#elif [[ "$1" = *Perl\ POD\ document\ text* || "$1" = *Perl5\ module\ source\ text* ]]; then
elif [[ "$1" = *Perl\ POD\ document\ text$NOL_A_P* ]] && cmd_exist perldoc; then
msg "append $sep to filename to view the perl source"
istemp perldoc "$2"
#endif
elif [[ "$1" = *\ script* ]]; then
set "plain text" "$2"
elif [[ "$1" = *text\ executable* ]]; then
set "plain text" "$2"
elif [[ "$1" = *PostScript$NOL_A_P* ]]; then
#ifdef ps2ascii
if cmd_exist ps2ascii; then
msg "append $sep to filename to view the postscript file"
nodash ps2ascii "$2"
fi
#elif pstotext
if cmd_exist pstotext; then
msg "append $sep to filename to view the postscript file"
istemp pstotext "$2"
fi
#else
msg "install pstotext or ps2ascii to view a textual representation of the file contents"
#endif
elif [[ "$1" = *executable* ]]; then
msg "append $sep to filename to view the raw file"
nodash strings "$2"
elif [[ "$1" = *\ ar\ archive* ]]; then
msg "use library${sep}contained_file to view a file in the archive"
istemp "ar vt" "$2"
#ifdef nm
elif [[ "$1" = *shared* ]] && cmd_exist nm; then
msg "This is a dynamic library, showing the output of nm"
istemp nm "$2"
#endif
#ifdef fastjar
elif [[ "$1" = *Jar\ archive* ]] && cmd_exist fastjar; then
msg "use jar_file${sep}contained_file to view a file in the archive"
nodash "fastjar -tf" "$2"
#endif
#ifdef unzip
elif [[ "$1" = *Zip* || "$1" = *ZIP* || "$1" = *JAR* ]] && cmd_exist unzip; then
msg "use zip_file${sep}contained_file to view a file in the archive"
istemp "unzip -lv" "$2"
#endif
#ifdef unrar
elif [[ "$1" = *RAR\ archive* ]]; then
if cmd_exist unrar; then
msg "use rar_file${sep}contained_file to view a file in the archive"
istemp "unrar v" "$2"
elif cmd_exist rar; then
msg "use rar_file${sep}contained_file to view a file in the archive"
istemp "rar v" "$2"
elif cmd_exist bsdtar; then
msg "use rar_file${sep}contained_file to view a file in the archive"
istemp "bsdtar tvf" "$2"
fi
#elif rar
elif [[ "$1" = *RAR\ archive* ]] && cmd_exist rar; then
msg "use rar_file${sep}contained_file to view a file in the archive"
istemp "rar v" "$2"
#elif bsdtar
elif [[ "$1" = *RAR\ archive* ]] && cmd_exist bsdtar; then
msg "use rar_file${sep}contained_file to view a file in the archive"
istemp "bsdtar tvf" "$2"
#endif
#ifdef 7za
elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7za; then
typeset res
res=$(istemp "7za l" "$2")
if [[ "$res" = *\ 1\ file* ]]; then
msg "a 7za archive containing one file was silently unpacked"
if [[ "$2" != - ]]; then
7za e -so "$2" 2>/dev/null
else
# extract name of temporary file containing the 7za archive
t=${res#*Listing\ archive:\ }
t2="
"
t=${t%%$t2*}
7za e -so "$t" 2>/dev/null
fi
else
msg "use 7za_file${sep}contained_file to view a file in the archive"
echo "$res"
fi
elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7zr; then
typeset res
res=$(istemp "7zr l" "$2")
if [[ "$res" = *\ 1\ file* ]]; then
msg "a 7za archive containing one file was silently unpacked"
if [[ "$2" != - ]]; then
7zr e -so "$2" 2>/dev/null
else
# extract name of temporary file containing the 7za archive
t=${res#*Listing\ archive:\ }
t2="
"
t=${t%%$t2*}
7zr e -so "$t" 2>/dev/null
fi
else
msg "use 7za_file${sep}contained_file to view a file in the archive"
echo "$res"
fi
#elif 7zr
elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7zr; then
typeset res
res=$(istemp "7zr l" "$2")
if [[ "$res" = *\ 1\ file* ]]; then
msg "a 7za archive containing one file was silently unpacked"
if [[ "$2" != - ]]; then
7zr e -so "$2" 2>/dev/null
else
# extract name of temporary file containing the 7za archive
t=${res#*Listing\ archive:\ }
t2="
"
t=${t%%$t2*}
7zr e -so "$t" 2>/dev/null
fi
else
msg "use 7za_file${sep}contained_file to view a file in the archive"
echo "$res"
fi
#endif
#ifdef cabextract
elif [[ "$1" = *[Cc]abinet* ]] && cmd_exist cabextract; then
msg "use cab_file${sep}contained_file to view a file in the cabinet"
istemp "cabextract -l" "$2"
#endif
#ifdef dvi2tty
elif [[ "$1" = *\ DVI* ]] && cmd_exist dvi2tty; then
msg "append $sep to filename to view the raw DVI file"
isdvi "$2"
#endif
elif [[ "$PARSEHTML" = yes && "$1" = *HTML$NOL_A_P* ]]; then
msg "append $sep to filename to view the HTML source"
parsehtml "$2"
#ifdef pdftotext
elif [[ "$1" = *pdf* || "$1" = *PDF* ]] && cmd_exist pdftotext; then
if [[ "$PARSEHTML" = yes ]]; then
msg "append $sep to filename to view the PDF source"
istemp "pdftotext -htmlmeta -nopgbrk -q --" "$2" - | parsehtml -
else
msg "append $sep to filename to view the PDF source"
istemp pdftotext -nopgbrk -q -- "$2" -
fi
#endif
#ifdef pdftohtml
elif [[ "$PARSEHTML" = yes && "$1" = *pdf* ]] && cmd_exist pdftohtml; then
msg "append $sep to filename to view the PDF source"
t=$(nexttmp)
cat "$2" > "$t"; pdftohtml -i -noframes -nodrm -enc UTF-8 -stdout "$t" | parsehtml -
#endif
#ifdef pdfinfo
elif [[ "$1" = *pdf* ]] && cmd_exist pdfinfo; then
msg "append $sep to filename to view the PDF source"
istemp pdfinfo "$2"
#ifdef h5dump
elif [[ "$1" = *Hierarchical\ Data\ Format* ]] && cmd_exist h5dump; then
istemp h5dump "$2"
#endif
#ifdef ncdump
elif [[ "$1" = *NetCDF* || "$1" = *Hierarchical\ Data\ Format* ]] && cmd_exist ncdump; then
istemp ncdump "$2"
#endif
#ifdef matdump
elif [[ "$1" = *Matlab\ v[0-9.]*\ mat-file* ]] && cmd_exist matdump; then
msg "append $sep to filename to view the raw data"
matdump -d "$2"
#endif
#ifdef djvutxt
elif [[ "$1" = *DjVu* ]] && cmd_exist djvutxt; then
msg "append $sep to filename to view the DjVu source"
djvutxt "$2"
#endif
#ifdef docx2txt.pl
elif [[ "$1" = *Microsoft\ Word\ 2007* ]]; then
if cmd_exist docx2txt.pl; then
msg "append $sep to filename to view the raw word document"
docx2txt.pl "$2" -
else
msg "install docx2txt.pl to view human readable text"
cat "$2"
fi
#elif doc2txt.pl
#ifdef odt2txt
elif [[ "$1" = *OpenDocument\ Text* ]]; then
if cmd_exist odt2txt; then
msg "append $sep to filename to view the raw word document"
istemp odt2txt "$2"
else
msg "install odt2txt to view human readable text"