/doc/rit/rit-1.1.tt

H1: Rit ver. 1.1
- 2004/12/22 追加 (知られたバグ)
- 2004/12/21
H3: はじめに
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/ から手に入ります。
H3: 規則
Rit の中ではただ1つの記号 $ が特殊な意味を持っています。
DL:
- $ に続く英数字(下線記号を含む)
Rc のシェル変数や環境変数と置き換えられます。
- $ に続く {
{ から } までが Rc スクリプトです。スクリプトは複数行に渡って書く事ができて、Rc の全ての機能を使えます。Rc スクリプトを終了する } は文字列の外、{  } ネストの外になければなりません。
- NL ('\n')
NL エスケープです。

H3: Rit ver.1.0 との違い
<a href="rit-1.0.html">Rit ver.1.0</a>の仕様が修正されています。
H4: コメントの扱い
Rit ver.1.1 のコメントの形式は Rc と完全互換になっています。即ち Rc スクリプト中の文字列の外にある # から行末までがコメントです。
H4: Rc スクリプトの実行によるエラー処理
Rc スクリプトの実行に伴うエラー処理ルーチンを加えました。
H4: バグ除去
Rit ver.1.0 に発見されたバグを除去しています。

H3: コマンドシンタックス
Rit ver1.1 のコマンドシンタックスは
rit [-Des] [file]
です。オブション e で Rc スクリプトが標準エラーに何か出力した場合には終了します。なおオプション無しの Rit の実行では標準エラーに exit が出力されると終了します。すなわち
${echo exit >[1=2]}
で処理が終了します。なお、exit を実行すると Rc は終了するので、その次にRc スクリプトが実行されると Rit はエラー終了します。すなわち
${exit} ${}
でも Rit が終了します。
オプション D はディバッグ用、オブション s は file の先頭一行の処理をスキップします。

H3: 例
以下にもつと詳しい文法解説を兼ねた例を挙げます。
H4: 例1
P:!
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 によって次のように変換されます。
P:!
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.
!
H4: 例2
P:!
Example 2 variable
A dollar followed by alpha or numeric
User: $user
This is equivalent to
User: ${echo -n $user}
!
この文は Rit によって次のように変換されます。
P:!
Example 2 variable
A dollar followed by alpha or numeric
User: arisawa
This is equivalent to
User: arisawa
!
H4: 例3
P:!
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 によって次のように変換されます。
P:!
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.
!
H4: 例4
P:!
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 によって次のように変換されます。
P:!
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
!
H4: 例5
P:!
Example 5 'if' statement
We have 'if' statement:
${
if(~ $user arisawa)
echo ARISAWA
if not
echo NOT
}$
!
この文は Rit によって次のように変換されます。
P:!
Example 5 'if' statement
We have 'if' statement:
ARISAWA
!
H4: 例6
P:!
Example 6 braces in Rc
Swicth statement.
Note that { } is included in this example.
${switch($user){
case arisawa
echo ARISAWA
case *
echo NOT }}$
!
この文は Rit によって次のように変換されます。
P:!
Example 6 braces in Rc
Swicth statement.
Note that { } is included in this example.
ARISAWA
!
H4: 例7
P:!
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 によって次のように変換されます。
P:!
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 }
!
H4: 例8
P:!
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 によって次のように変換されます。
P:!
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.
!
H4: 例9
P:!
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 によって次のように変換されます。
P:!
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
!
H3: 文字コード
Rit は UTF-8 または EUC コードを処理できます。しかし Rc は EUC コードは処理できません。Rc の中で日本語文字を使う可能性があるのは echo だけでしょう。しかし echo の部分は全てテキスト部分に回す事ができます。つまり Rit を使えば Rc の中で echo を使うニーズはないはずです。
H3: UNIX への移植
UNIX には既に Rc は移植されているので UNIX への移植は難しくはないはずです。 Rit を UNIX で使用した場合の問題は CGI で使うには強力すぎることにあります。( PHP は制限をかける事によってセキュリティ上問題を回避しようとしていますが、UNIX 上の Rit は制限をかけるメカニズムを持っていません。)
H3: 知られたバグ
- 2004/12/21
Rit ver.1.0, ver.1.1 共に read など標準入力からデータを読み取るコマンドが実行された場合に問題が発生します。この問題は ver.1.2 で解決される予定です。(この問題は Rit を UNIX に移植した場合に解決できないね...)
H3: おわりに
Plan 9 のシェルである Rc はとても良くできていて、Rit を使わなくても HTML 文書を容易に作成できます。また UNIX 環境と違ってPlan 9 ではシェルを使って安全に CGI プログラムを書けます。すなわち HTML 形式で直接文書を作成するのであれば Rit のニーズはあまりありません。
しかしながら筆者は HTML 文書を直接書いているのではなく、もっと生産性の高い形式( tt 形式)で書いてそれを HTML 形式に自動変換しています。( tt 形式の例を見たいのであれば <a href="rit-1.1.tt">ここ</a>をクリックしてみてください。筆者のサーバにはその他多数の tt 形式の例を見ることができます。詳しくは http://ar.aichi-u.ac.jp/tt を見てください。) この場合には tt 形式の中に Rit によって Rc コマンドを埋め込み、それを Rit で処理可能な HTML 形式に変換できます。
tt 形式を HTML 形式に変換するプログラム( t2h )は近い将来公開するつもりでいます。