视二进制文件如文本

默认情况下,less只能查看文本,如果是二进制文件,就会显示乱码。

$ less hello.bc
"hello.bc" may be a binary file. See it anyway?

然而稍加配置,即可让less显示其对应的文本文件。

首先在~/.bashrc文件里增加一行

eval "$(lesspipe)"

打开新的终端窗口,即可查看各种压缩文件、PDF文件,甚至能够查看图片和ISO的metadata!(具体参加man lesspipe

通过~/.lessfilter文件可以扩展lesspipe,显示LLVM bc文件(以及spirv文件)。

$ cat ~/.lessfilter
#!/bin/sh

case "$1" in
    *.bc)
        llvm-dis "$1" -o -
        ;;
    *.spv)
        spirv-dis "$1" --raw-id --no-color
        ;;
    *)
        # We don't handle this format.
        exit 1
esac

# No further processing by lesspipe necessary
exit 0

使用Emacs,不仅可以直接打开二进制文件查看其文本形式,而且可以修改,并在保存时更新原二进制文件。配置如下:

(require 'jka-compr)
(require 'llvm-mode)

;;; bc
(add-to-list 'jka-compr-compression-info-list
 '["\\.bc\\'"
 "Assembling BC" "llvm-as" nil
 "Disassembling BC" "llvm-dis" ("-o" "-")
 t nil "BC"])

(add-to-list 'auto-mode-alist '("\\.bc\\'" . llvm-mode))

;;; spirv
(require 'asm-mode)

(add-to-list 'jka-compr-compression-info-list
 '["\\.spv\\'"
 "Assembling SPIRV" "spirv-as" ("-o" "-")
 "Disassembling SPIRV" "spirv-dis" ("--no-color" "--raw-id")
 t nil "\003\002\043\007"])

(add-to-list 'auto-mode-alist '("\\.spv\\'" . asm-mode))

(jka-compr-update)
Share on Google+Share on FacebookTweet about this on TwitterShare on LinkedInEmail this to someonePrint this page

发表评论

电子邮件地址不会被公开。 必填项已用*标注