ファイルのロック

                             <p>ソースコード</p>
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/perl

# print "Content-type: text/htmlnn";
# open(STDERR, ">&amp;STDOUT");
# $|=1;

# 初期設定
# require Jcode;
# $JcodeVer = "Jcode $Jcode::VERSION";
# *Jgetcode = &amp;Jcode::getcode;
# *Jconvert = &amp;Jcode::convert;

require "../../../cgi-bin/jcode.pl";
$JcodeVer = "jcode.pl $jcode::version";
*Jgetcode = &amp;jcode::getcode;
*Jconvert = sub { &amp;jcode::to($_[1], $_[0], $_[2]); };

@G_styles = ("../../ipp.css","../test.css");
$G_title = "テスト20";
# $G_myCode = &amp;Jgetcode("漢字");
$G_myCode = &amp;Jgetcode("漢字");
$G_code = "jis";
$G_charset = "iso-2022-jp";
%G_form=();

$G_scrName = $ENV{'SCRIPT_NAME'};
if($G_scrName =~ /ts[0-9]{4}/){ # _1,_uなどと共通で使用する項目
    $G_scrName = $&amp;;
    $G_dataFile = "dat/$G_scrName.dat";
    $G_lock = "lock/$G_scrName";
    $G_linkFile = "../$G_scrName.htm";
    $G_linkName = $G_title;
}

{
    &amp;sigInit;
    if($#ARGV == -1){
        &amp;selectCode;
    }elsif( $ENV{'HTTP_REFERER'} !~ /$ENV{'SCRIPT_NAME'}/ ||
            $ENV{'HTTP_REFERER'} !~ /$ENV{'HTTP_HOST'}/ ){
        exit;
#    }elsif($ARGV[0] eq "utf8"){
#        $G_code = "utf8";
#        $G_charset = "UTF-8";
#        &amp;printForm("post");
    }elsif($ARGV[0] eq "sjis"){
        $G_code = "sjis";
        $G_charset = "shift_jis";
        &amp;printForm("post");
    }elsif($ARGV[0] eq "euc"){
        $G_code = "euc";
        $G_charset = "euc-jp";
        &amp;printForm("post");
    }elsif($ARGV[0] eq "jis"){
        &amp;printForm("post");
    }elsif($ARGV[0] eq "output"){
        &amp;formRead;
        &amp;formWrite;
    }elsif($ARGV[0] eq "read"){
        &amp;dataOpen;
    }else{
        &amp;error("<h2>無効なコマンドが渡されました。</h2>");
    }

    exit;
}

sub sigInit{
    $SIG{'PIPE'} = "sigExit";
    $SIG{'INT'} = "sigExit";
    $SIG{'HUP'} = "sigExit";
    $SIG{'QUIT'} = "sigExit";
    $SIG{'TERM'} = "sigExit";
}

sub sigExit{
    &amp;dataUnlock;
    exit;
}

sub error{
    $G_title .= "(エラー)";
    &amp;printHeader;
    &amp;Jprint(@_);
    &amp;printFooter;
    exit;
}

sub selectCode{
    $G_title .= "(漢字コード選択)";
    &amp;printHeader;
    print "<div class=test>n";

    &amp;Jprint("<p>入力フォームで使用する漢字コードを選んでください。</p>n");
    print <<EOM;
<form method=post action="$ENV{'SCRIPT_NAME'}?jis"><input type=submit value="JIS(iso-2022-jp)"></form>
<form method=post action="$ENV{'SCRIPT_NAME'}?sjis"><input type=submit value="Shift_JIS"></form>
<form method=post action="$ENV{'SCRIPT_NAME'}?euc"><input type=submit value="EUC-JP"></form>
EOM
# 以下の一行を削除した
# <form method=post action="$ENV{'SCRIPT_NAME'}?utf8"><input type=submit value="Unicode(UTF-8)"></form>

    print qq(<form method=post action="$ENV{'SCRIPT_NAME'}?read">n);
    &amp;Jprint(qq(<input type=submit value="過去ログ(?)を見る">n));
    print "</form>n";
    print "</div>n";
    &amp;printFooter;
}

sub dataLock{
    local($i, $mtime);
    for($i=0;$i<2;$i++){
        if(mkdir($G_lock, 0755)){
            return;
        }else{
            unless($i){
                ($mtime) = (stat($G_lock))[9];
                if ($mtime < time() - 60) { # 1分以上前のロックは解除
                    rmdir($G_lock);
                }
            }
        }
    }
    &amp;error("<h2>現在、ロック中です。ごめんなさい。</h2>");
}

sub dataUnlock{
    if(-d $G_lock){
        rmdir($G_lock);
    }
}

sub dataOpen{
    $G_title .= "(過去ログ)";
    &amp;printHeader;
    print "<div class=test>n";

    &amp;dataLock;

    open(IN, "<$ G_dataFile")||die "Can not open $G_dataFile....";
    # データファイルはEUC記録なので時間短縮のつもり。
    # 標準出力は「jis」だけど・・・。
    if($G_code eq "euc"){
        print while (<IN>);
    }else{
        while (<IN>){
            print &amp;Jconvert($_, $G_code, "euc");
        }
    }
    close(IN);

    &amp;dataUnlock;

    print "</div>n";
    &amp;printFooter;
}

sub formWrite{
    local($name, $value);

    &amp;dataLock;

    open(OUT, ">> $G_dataFile")||die "Can not open $G_dataFile....";
    print(OUT "<table border=1>n");
    print(OUT "<tr><th>Name<th>Value</tr>n");
    while (($name, $value) = each(%G_form)){
        print(OUT "<tr><td>$name<td>$value</tr>n");
    }

    sleep 5;

    print(OUT "</table>n");

    close(OUT);

    &amp;dataUnlock;

    $G_title .= "(送信完了)";
    &amp;printHeader;
    print "<div class=test>n";

    &amp;Jprint("<p>たぶん、無事に送信されました。n");
    &amp;Jprint(qq(<p><a href="$ENV{'SCRIPT_NAME'}?read">心配だから、過去ログを見る。</a>n));
    print "</p>n";

    print "</div>n";
    &amp;printFooter;
}

sub formRead{
    local($query_string, @elements, $elm, $name, $value, $code, $method);
    $method = $ENV{'REQUEST_METHOD'};
    if($method eq "POST"){
        read(STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
    }elsif($method eq "GET"){
        $query_string = $ENV{'QUERY_STRING'};
    }else{
        &amp;error("<h2>「POST」か「GET」のデータしか受け取れません。</h2>");
    }
    @elements = split(/&amp;/, $query_string);  # 中身はpostもgetも同じ
    foreach $elm (@elements){
        ($name, $value) = split(/=/, $elm);
        $value =~ tr/+/ /;
        $value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg;
#        $code = &amp;Jgetcode($value);
        $code = &amp;Jgetcode($value);
        if($code ne "euc"){
            $value = &amp;Jconvert($value, "euc", $code); # とりあえずEUCに変換
        }
        $value =~ s/&amp;/&amp;amp;/g;  # メタ文字(?)を無効化
#        $value =~ s/<!--/&amp;lt;!--/g;   # SSI入力を無効化
#        $value =~ s/-->/--&amp;gt;/g;   # SSI入力を無効化
        $value =~ s/</&amp;lt;/g;   # タグを無効化
        $value =~ s/>/&amp;gt;/g;   # タグを無効化
        $value =~ s/rn*/n/g; # 改行を統一
        $value =~ s/nnn+/nn/g; # 長い改行(3回以上)を2回(1行空き)に
        $value =~ s/[ns]+$//g; # 最後の連続した空白、改行を削除
        $value =~ s/n/<br>/g;  # 改行を<br>に変換
        $G_form{$name} = $value ."<br>InputCode : $coden";
    }
}

sub printForm{
    $G_title .= "(入力画面)";
    &amp;printHeader;
    print "<div class=test>n";

    print qq(<form method=$_[0] action="$ENV{'SCRIPT_NAME'}?output">n);
    &amp;Jprint(qq(<input type=hidden name=tx0 value="画面表示:$G_codenブラウザ:$ENV{'HTTP_USER_AGENT'}nスクリプト:$G_myCode"><br>n));
    print qq(<input type=text name=tx1 value="tx1"><br>n);
    &amp;Jprint("<textarea name=ta1 cols=40 rows=4>ta1n何か適当にどうぞ。n</textarea><br>n");
    &amp;Jprint("<p>送信し、データを書き込んでいる最中に5秒間待ちます。</p>n");
    &amp;Jprint("<p>この間は、しばらく待つか、他のブラウザなどから「送信」を試してみてください。</p>n");
    &amp;Jprint(qq(<p><a href="$ENV{'SCRIPT_NAME'}">ブラウザをもう一枚開く</a></p>n));
    &amp;Jprint(qq(<input type=submit value="送信する">n));
    print "</form>n";
    print qq(<form method=$_[0] action="$ENV{'SCRIPT_NAME'}?read">n);
    &amp;Jprint(qq(<input type=submit value="過去ログ(?)を見る">n));
    print "</form>n";

    print "</div>n";
    &amp;printFooter;
}

sub printHeader{
    if($G_charset){
        print "Content-type: text/html; charset=$G_charsetnn";
    }else{
        print "Content-type: text/htmlnn";
    }
    print qq(<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">n);
    print "<html lang=ja>n<head>n";
    if($G_charset){
        print qq(<meta http-equiv="Content-Type" content="text/html; charset=$G_charset">n);
    }
    print qq(<meta http-equiv="Content-Script-Type" content="text/javascript">n);
    print qq(<meta http-equiv="Content-Style-Type" content="text/css">n);
    &amp;Jprint("<title>$G_title</title>n");
    foreach (@G_styles){
        print qq(<link rel="stylesheet" type="text/css" href="$_">n);
    }
    print "</head>n<body>n";
    print "<div class=head>n";
    &amp;Jprint("<h1>$G_title</h1><hr>n");
    &amp;printlinks;
    print "<hr></div>n";
#    print $ENV{'HTTP_REFERER'}."<br>".$ENV{'SCRIPT_NAME'}."<br>".$ENV{'HTTP_HOST'};
}

sub printFooter{
    print "<div class=foot><hr>n";
    &amp;printlinks;
    print "<hr>n";
    &amp;Jprint("漢字コード変換 : $JcodeVer<br>n");
    open(IN, "../../sig.txt");
    print while (<IN>);
    close(IN);
    print "</div>n";
    print "</body></html>n";
}

sub printlinks{
    print qq(<a href="../../../index.htm">Home</a>n);
    print qq(/n<a href="../../">Perl</a>n);
    print qq(/n<a href="../">TestCGI Index</a>n);
    if($G_linkFile){
        &amp;Jprint(qq(/n<a href="$G_linkFile">$G_linkNameの解説</a>n));
    }
}

sub Jprint{
    if($G_code eq $G_myCode){
        foreach (@_){ print; }
    }else{
        foreach (@_){ print &amp;Jconvert($_, $G_code, $G_myCode); }
    }
}

sub JconvPrint{
    foreach (@_){ print &amp;Jconvert($_, $G_code, &amp;Jgetcode($_)); }
}
                          </div>

Comments

comments powered by Disqus