![]() |
![]() |
Rit (Rc in text) はテキスト中に埋め込まれた Rc コマンドを処理するプログラムです。主要な用途は CGI プログラムの作成で HTML 文書にコマンドを埋め込みホスト側でダイナミックに HTML 文書を生成するのに使えます。
同じ趣旨のプログラムとして有名なのは PHP です。Plan 9 では PHP 自体はまだ移植されていませんが、Rit は Plan 9 の Web サーバである Pegasus によくなじみます。すなわち Rit の中では Rc の全ての機能を使用できて、コマンドのアクセス空間の制限は Pegasus がかけます。
筆者は Pegasus の CGI ツールとして、テキスト中に埋め込まれたコマンドを処理するプログラムとして Dx を添えていましたが、Dx には強い制限( I/O の切り替えや パイプの処理などができない)がありました。今回の Rit は、Dx で採用されたアプローチをやめて、コマンド部分は直接 Rc に処理させる事によって Rc と完全な互換性が保証され、遥かに強力になり、かつプログラム自体は遥かにシンプルになっています。
Rit は http://plan9.aichi-u.ac.jp/netlib/cmd/ から手に入ります。
Rit の中ではただ1つの記号 $ が特殊な意味を持っています。
Rit ver.1.0の仕様が修正されています。
Rit ver.1.1 のコメントの形式は Rc と完全互換になっています。即ち Rc スクリプト中の文字列の外にある # から行末までがコメントです。
Rc スクリプトの実行に伴うエラー処理ルーチンを加えました。
Rit ver.1.0 に発見されたバグを除去しています。
Rit ver1.1 のコマンドシンタックスは
rit [-Des] [file]
です。オブション e で Rc スクリプトが標準エラーに何か出力した場合には終了します。なおオプション無しの Rit の実行では標準エラーに exit が出力されると終了します。すなわち
${echo exit >[1=2]}
で処理が終了します。なお、exit を実行すると Rc は終了するので、その次にRc スクリプトが実行されると Rit はエラー終了します。すなわち
${exit} ${}
でも Rit が終了します。
オプション D はディバッグ用、オブション s は file の先頭一行の処理をスキップします。
以下にもつと詳しい文法解説を兼ねた例を挙げます。
Example 1 command execution
A dollar followed by '{':
You can write rc script in curly brace { }
Date: ${date}
Note that we have redundant empty line after this command;
this comes from two subsequent '\n's:
one from command and one from our this text line.
Avoiding this problem we have:
${date} continues nest line
${date}$ continues same line.
この文は Rit によって次のように変換されます。
Example 1 command execution
A dollar followed by '{':
You can write rc script in curly brace { }
Date: Mon Dec 20 20:23:03 JST 2004
Note that we have redundant empty line after this command;
this comes from two subsequent '\n's:
one from command and one from our this text line.
Avoiding this problem we have:
Mon Dec 20 20:23:03 JST 2004
continues nest line
Mon Dec 20 20:23:03 JST 2004 continues same line.
Example 2 variable
A dollar followed by alpha or numeric
User: $user
This is equivalent to
User: ${echo -n $user}
この文は Rit によって次のように変換されます。
Example 2 variable
A dollar followed by alpha or numeric
User: arisawa
This is equivalent to
User: arisawa
Example 3 NL escape
A dollar at the end of line is NL escape.
Example: This line has NL escape $
next line.
Many rc command produce NL at the end
We can avoid redundant NL by putting NL escape:
${pwd}$
is current working directory.
${pwd}$$
this line stayes in $${pwd}$$ line.
この文は Rit によって次のように変換されます。
Example 3 NL escape
A dollar at the end of line is NL escape.
Example: This line has NL escape next line.
Many rc command produce NL at the end
We can avoid redundant NL by putting NL escape:
/usr/arisawa/src/pegasus-2.1/rit
is current working directory.
/usr/arisawa/src/pegasus-2.1/rit this line stayes in ${pwd}$$ line.
Example 4 multi-lines
Rit has full functionality with rc.
For example Rit allows multi-line script:
${book='Alice in Wonder Land'}$
${echo -n 'echo test of multi-line:
line1: Carrol''s book:
line2: '$book'
line3: and we can use { and } in rc strings' }
Back slash new line escape in Rc command will work:
${echo -n one\
two }
この文は Rit によって次のように変換されます。
Example 4 multi-lines
Rit has full functionality with rc.
For example Rit allows multi-line script:
echo test of multi-line:
line1: Carrol's book:
line2: Alice in Wonder Land
line3: and we can use { and } in rc strings
Back slash new line escape in Rc command will work:
one two
Example 5 'if' statement
We have 'if' statement:
${
if(~ $user arisawa)
echo ARISAWA
if not
echo NOT
}$
この文は Rit によって次のように変換されます。
Example 5 'if' statement
We have 'if' statement:
ARISAWA
Example 6 braces in Rc
Swicth statement.
Note that { } is included in this example.
${switch($user){
case arisawa
echo ARISAWA
case *
echo NOT }}$
この文は Rit によって次のように変換されます。
Example 6 braces in Rc
Swicth statement.
Note that { } is included in this example.
ARISAWA
Example 7 comment in Rc command
Comment continues upto NL as Rc does.
${# This is a comment upto NL } this is also a part of comment
# this is also a comment
} # invisible
${# comment line1 terminated by Rc NL escape\
commment line2
} # invisible
$${# This isn't a commant but a part of text }
この文は Rit によって次のように変換されます。
Example 7 comment in Rc command
Comment continues upto NL as Rc does.
# invisible
# invisible
${# This isn't a commant but a part of text }
Example 8
$0 is special, this is a file name currently processed.
Dollar followed by other charactors is not special. i.e,
$, $#
are shown as it is.
この文は Rit によって次のように変換されます。
Example 8
readme is special, this is a file name currently processed.
Dollar followed by other charactors is not special. i.e,
$, $#
are shown as it is.
Example 9
Sequence of dollars are processed as follows:
If the sequence is followed by alpha or '{' or '\n'
one dollar is simply discarded:
$$$$home is equvalent to ${echo -n '$$$home'}
$$$${not a rc script}
$$$$
is not a NL escape.
If the sequence is followed by other charactors,
they are shown as it is:
$$$$ is shown as four dollars
この文は Rit によって次のように変換されます。
Example 9
Sequence of dollars are processed as follows:
If the sequence is followed by alpha or '{' or '\n'
one dollar is simply discarded:
$$$home is equvalent to $$$home
$$${not a rc script}
$$$
is not a NL escape.
If the sequence is followed by other charactors,
they are shown as it is:
$$$$ is shown as four dollars
Rit は UTF-8 または EUC コードを処理できます。しかし Rc は EUC コードは処理できません。Rc の中で日本語文字を使う可能性があるのは echo だけでしょう。しかし echo の部分は全てテキスト部分に回す事ができます。つまり Rit を使えば Rc の中で echo を使うニーズはないはずです。
UNIX には既に Rc は移植されているので UNIX への移植は難しくはないはずです。 Rit を UNIX で使用した場合の問題は CGI で使うには強力すぎることにあります。( PHP は制限をかける事によってセキュリティ上問題を回避しようとしていますが、UNIX 上の Rit は制限をかけるメカニズムを持っていません。)
Plan 9 のシェルである Rc はとても良くできていて、Rit を使わなくても HTML 文書を容易に作成できます。また UNIX 環境と違ってPlan 9 ではシェルを使って安全に CGI プログラムを書けます。すなわち HTML 形式で直接文書を作成するのであれば Rit のニーズはあまりありません。
しかしながら筆者は HTML 文書を直接書いているのではなく、もっと生産性の高い形式( tt 形式)で書いてそれを HTML 形式に自動変換しています。( tt 形式の例を見たいのであれば ここをクリックしてみてください。筆者のサーバにはその他多数の tt 形式の例を見ることができます。詳しくは http://ar.aichi-u.ac.jp/tt を見てください。) この場合には tt 形式の中に Rit によって Rc コマンドを埋め込み、それを Rit で処理可能な HTML 形式に変換できます。
tt 形式を HTML 形式に変換するプログラム( t2h )は近い将来公開するつもりでいます。