| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | use locale; |
|---|
| 4 | use Getopt::Long; |
|---|
| 5 | |
|---|
| 6 | my $interactive=0; |
|---|
| 7 | my $help; |
|---|
| 8 | |
|---|
| 9 | my $configfile1=".../../conf/tok.conf"; |
|---|
| 10 | my $configfile2="../conf/tok.conf"; |
|---|
| 11 | |
|---|
| 12 | #read configuration files########################### |
|---|
| 13 | my $file; |
|---|
| 14 | foreach $file ($configfile1, $configfile2){ |
|---|
| 15 | if(open(CONFIG, $configfile1)){ |
|---|
| 16 | while (<CONFIG>) { |
|---|
| 17 | chomp; |
|---|
| 18 | s/#.*//; |
|---|
| 19 | s/^\s+//; |
|---|
| 20 | s/\s+$//; |
|---|
| 21 | next unless length; |
|---|
| 22 | my ($name, $value) = split(/\s*=\s*/, $_, 2); |
|---|
| 23 | if(($name eq "interactive")or($name eq "i")){ |
|---|
| 24 | $interactive=1; |
|---|
| 25 | } |
|---|
| 26 | elsif(($name eq "help")or($name eq "h")){ |
|---|
| 27 | $help=1; |
|---|
| 28 | } |
|---|
| 29 | } |
|---|
| 30 | close CONFIG; |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | #########################################################s |
|---|
| 34 | |
|---|
| 35 | GetOptions("interactive|i" => \$interactive, |
|---|
| 36 | "help|h" => \$help); |
|---|
| 37 | |
|---|
| 38 | if($help) |
|---|
| 39 | { |
|---|
| 40 | print <<'END' |
|---|
| 41 | Usage: tok [OPTIONS] |
|---|
| 42 | |
|---|
| 43 | Options: |
|---|
| 44 | --interactive Interactive (no output buffering). |
|---|
| 45 | --help -h Help. |
|---|
| 46 | END |
|---|
| 47 | ; |
|---|
| 48 | exit 0; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | $| = $interactive; |
|---|
| 53 | |
|---|
| 54 | my $offset = 0; |
|---|
| 55 | |
|---|
| 56 | while(<>) |
|---|
| 57 | { |
|---|
| 58 | 1 while |
|---|
| 59 | / [[:alpha:]]+ (?{seg('W',$&)}) |
|---|
| 60 | | \d+ (?{seg('N',$&)}) |
|---|
| 61 | | \s+ (?{seg('S',$&)}) |
|---|
| 62 | | [[:punct:]] (?{seg('P',$&)}) |
|---|
| 63 | | . (?{seg('B',$&)}) |
|---|
| 64 | /gxo; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | # | [^[:print:]] (?{seg("B",$&)}) |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | sub seg |
|---|
| 71 | { |
|---|
| 72 | my ($tag,$match) = @_; |
|---|
| 73 | my $len=length $match; |
|---|
| 74 | printf "%04d %02d %s ", $offset, $len, $tag; |
|---|
| 75 | if($tag eq 'S') |
|---|
| 76 | { |
|---|
| 77 | for(my $i=0; $i<$len; ++$i) |
|---|
| 78 | { |
|---|
| 79 | my $c = substr $match, $i, 1; |
|---|
| 80 | print '_' if $c eq ' '; |
|---|
| 81 | print '\n' if $c eq "\n"; |
|---|
| 82 | print '\t' if $c eq "\t"; |
|---|
| 83 | print '\r' if $c eq "\r"; |
|---|
| 84 | print '\f' if $c eq "\f"; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | elsif($tag eq 'B') |
|---|
| 88 | { |
|---|
| 89 | printf "\\x%02X", ord($match); |
|---|
| 90 | } |
|---|
| 91 | else |
|---|
| 92 | { |
|---|
| 93 | print $match; |
|---|
| 94 | } |
|---|
| 95 | print "\n"; |
|---|
| 96 | $offset += $len; |
|---|
| 97 | } |
|---|
| 98 | |
|---|