<html>
<head>
<?php
//辞書を引くPHPスクリプト programed by C.Yun
//利用はGPLに従います、改編再配布は自由です

    //define ( "NDTPD_TERMINATE_PATTERN" , "/^\\$(\\$|\\*|\\?|<|&|v[\\.\\d\\w\\s]*)$/" ); // NDTPD からの終了コードを認識している
    
    //define ( "DEBUG" , "on"); //デバッグモード

//サブルーチンを先に書いておく

/* ***** 辞書を引く
$dictonary    辞書番号
$word        検索単語
*/
function    consult$dictionary$word) {
    
$fp fsockopen "127.0.0.1"2010, &$errorno, &$errstr15 );
    if ( !
$fp ) {
        print 
"辞書サーバーとの接続でエラーが発生しました<br>";
    } else {
        
fwrite $fp,"L" $dictionary "\n" ); //辞書の選択
        
while ( ndtpd_end fgets $fp4096 ) ) == ) {} //読み捨て

        
$result_1[2] = "";
        
$result_1[4] = "";
        
fwrite $fp,"Pa" $word"\n" );
        for ( 
$i ; ( $i ) and !feof$fp ) ; $i++ ) {
            
$result_1[$i] = fgets $fp4095 );
            if ( 
ndtpd_end $result_1[$i] ) == -1) {
                
$i 10/* 最終行が来たフラグ */
                
break;
            }
        }

        
//読み捨て
        
if ( $i != 10 ) {
            while ( 
ndtpd_end fgets $fp4096 ) ) == ) {}
        }
    
        
/*  検索結果の表示  */
         
if ( $result_1[2] != "" ) {
             
print_s $fp$result_1[2] );
            if ( 
$result_1[4] != "" ) {
                 
print_s $fp$result_1[4] );
            }
            
fwrite $fp"Q\n" );
            return ( -
); //該当の単語あり
         
} else {
             
fwrite $fp"Q\n" );
             return ( 
); //該当の単語無し
         
}
    }
}


/* ***** ページ番号から表示するルーチン Sコマンド
$fp        ファイルポインタ
$page        ページ番号
*/
function    print_s $fp$page ) {
    
fwrite $fp"S"trim $page ) . "\n" ) ;
    
$line fgets $fp4095 ); //1行読み飛ばし
    
while ( ndtpd_end $line fgets $fp4095 ) ) == 0) {
        if ( 
substr $line02) == '→' ) { // リンクの作成
            
$new_consult substr $line2strpos $line"<" ) - );
            print ( 
"<a href=\"consult.php?consult=".$new_consult."\">$new_consult</a><br>");
        } else {
            print ( 
$line "<br>" );
        }
    }
}


/* ***** 入力が最終行かどうかの判定
$line        検索対象行
*/
function    ndtpd_end $line ) {
    switch ( 
trim $line ) ) {
        case 
'$*';
        case 
'$$';
        case 
'$?';
        case 
'$&';
        case 
'$<';
            return( -
); // last line

        
default;
            
/*if ( preg_match ( NDTPD_TERMINATE_PATTERN , $line ) ) {
                print ( "漏れです\"$line\"<br>" );
                return ( -1 );
            }*/
            
return ( );
        }
}
    
/* ***** メインルーチン開始
入力文字を渡す処理
入力文字の正統性のチェックが必要
*/

$consult $_GET['consult'];


// html ヘッダーの出力
print ("<title>".$consult."</title></head>");
print (
"<body style=\"color: rgb(0, 0, 0); background-color: rgb(204, 255, 255);\" link=\"#000099\" vlink=\"#990099\" alink=\"#000099\">");
print(
"<a href=\"/index.html\" target=\"_top\">こどもクリニック<br>医学英和辞書サービス<br></a><hr noshade>");


//該当する単語が無ければ一文字短くして検索をやり直す、最大3回繰り返す
$i 0;
while (  ( 
consult 1$consult ) + consult 2$consult ) == ) and ($i++ < ) ) {
    
$consult substr $consult0strlen $consult ) - 1);
}

?>
<br>
<form action="consult.php" method="GET">
<input type="text" name="consult"><input value="医学辞書" type="submit"></form>
<hr noshade>
このプログラムは英和辞書に
<a href="http://www.csse.monash.edu.au/%7Ejwb/edict.html" target="_blank">EDICT</a>、
<a href="http://lsd.pharm.kyoto-u.ac.jp/index-J.html" target="_blank">LSD(Life Science Dictionary)</a>
を使用しております。<br>
辞書を作成し公開いただいている作者の方々に深謝致します。<br>

</body>
</html>