LoginSignup
0
0

More than 5 years have passed since last update.

コマンドライン文字列をリストに変換する まじめに実装版

Last updated at Posted at 2014-12-24

やりたいこと

(shell-split-string "abc  'def   dee'") => '("abc" "def   dee")

として、以前こんなのを書きました。

これは、強引に sh に文字列を解釈させてそれを emacs から受け取ってやろうというもので、単純な場合はうまく行きますが、それでも問題はあります。

その中でも大きなものは例えば

(shell-split-string "abc && rm -rf *")

などとするとたちまち rm -rf * が実行されてしまう、というようなことで、セキュリティ的にもあれだし、得られる結果も '("abc") となるところ実際にほしいのは '("abc" "&&" "rm" "-rf" "*") なわけです(多分:少なくとも Python による shlex はそうなっている)。

実装

そんなわけで、正面から Emacs lisp で実装してみよう、ということであたまから文字を読んで再帰してリストを作るような感じにやってみました。

これなら、 && を含むような場合でもうまく動きます。

(shell-split-string "abc")  ->  '("abc")
(shell-split-string "abc  def")  ->  '("abc" "def")
(shell-split-string "abc \"def ghi\"")  ->  '("abc" "def ghi")
(shell-split-string "abc && def")  -> '("abc" "&&" "def")

末尾再帰

最適化ほしいですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0