Parse requests properly. Simpler, more correct and more robust.

Fix translation of escapes.  Still a hack.

Fix Empire client prompt in translation of EX and SY.
This commit is contained in:
Markus Armbruster 2004-02-15 16:14:46 +00:00
parent 1625567f97
commit eca121f709
2 changed files with 168 additions and 260 deletions

View file

@ -3,149 +3,122 @@
use strict; use strict;
use warnings; use warnings;
my (@Fld, $str, @a); my $esc="\\";
my @a;
line: while (<>) { line: while (<>) {
chop; # strip record separator chomp; # strip record separator
@Fld = split(' ', $_, 9999); s/([^\\](\\\\)*)\\\".*/$1/g;# strip comments
procline: @a = req($_);
if (/^\.TH/) {
$str=$Fld[2]; if (!@a) {
for (my $i=3; $i <= $#Fld; $i++) { print htmlify($_), "\n";
$str .= " " . $Fld[$i];
}
$str = &htmlify($str);
printf("<title>%s : %s</title><h1>%s : %s</h1>\n",
$Fld[1], $str, $Fld[1], $str);
next line; next line;
} }
if (/^\.SY../) { # requests
# $i = $_ =~ "\"" && ($RLENGTH = length($&), $RSTART = length($`)+1);
# $str = substr($_, $i + 1, length($_) - $i - 1); if ($a[1] eq "TH") {
$str = substr($_,4); @a = checkarg(2, @a);
$str = &htmlify($str); $a[3] = htmlify($a[3]);
printf "<samp>[##:##] </samp><KBD>%s</KBD><p>\n", $str; print "<title>$a[2] : $a[3]</title><h1>$a[2] : $a[3]</h1>\n";
next line; next line;
} }
if (/^\.EX../) { if ($a[1] eq "SY") {
$str = substr($_, 4); @a = checkarg(1, @a);
printf "<br><samp>[##:##] </samp><kbd>%s</kbd><p>\n", &htmlify($str); $a[2] = htmlify($a[2]);
print "<samp>[##:##] Command : </samp><KBD>$a[2]</KBD><p>\n";
next line; next line;
} }
if (/^\.L../) { if ($a[1] eq "EX") {
$str = substr($_, 3); my $str = htmlify(join(' ',@a[2..$#a]));
printf "<h2>%s</h2>\n", &htmlify($str); print "<br><samp>[##:##] Command : </samp><kbd>$str</kbd><p>\n";
next line; next line;
} }
if (/^\.(NF|nf)/) { printf (("<p><pre>\n")); next line; } if ($a[1] =~ /^LV?$/) {
if (/^\.(FI|fi)/) { printf (("</pre><p>\n")); next line; } @a = checkarg(1, @a);
$a[2] = htmlify($a[2]);
print "<h2>$a[2]</h2>\n";
next line;
}
if ($a[1] =~ "eo") { $esc = 0; next line; }
if ($a[1] =~ "ec") { $esc = $#a == 1 ? "\\" : $a[2]; next line; }
if (/^\.(NF|nf)/) { printf (("<p><pre>\n")); next line; }
if (/^\.(FI|fi)/) { printf (("</pre><p>\n")); next line; }
if (/^\.s3/) { printf (("<p>\n")); next line; } if (/^\.s3/) { printf (("<p>\n")); next line; }
if (/^\.s1/) {printf (("<hr> \n")); next line; } if (/^\.s1/) { printf (("<hr> \n")); next line; }
if (/^\.br/) { printf "<br>\n"; next line; } if (/^\.br/) { printf "<br>\n"; next line; }
if (/^\.SA/) {
@a = split('[: ",.]+');
printf("<p>See also : %s\n",&anchor($a[2]) ); if ($a[1] eq "SA") {
for (my $i = 3; $i <= $#a ; ($i)++) { @a = checkarg(1, @a);
printf(", %s\n",&anchor($a[$i])); @a = split(/[\: \"\,\.]+/, $a[2]);
for my $a (@a) {
$a = anchor($a);
} }
print "<p>See also : ", join("\n, ", @a), "\n";
while (<>) {
chop; # strip record separator
@a = split('[: ,.]+');
@Fld = split(' ', $_, 9999);
if (/^\./) { goto procline; }
for (my $i = 0; $i <= $#a ; ($i)++) {
printf(", %s\n",&anchor($a[$i]));
}
}
exit(0);
} }
if (/^\./) { next line; }
if (/^(See also|See Also|see also)/) { # ignore unknown request
@a = split('[: ,.]+');
printf("See also : %s\n",&anchor($a[2]) );
for (my $i = 3; $i <= $#a ; ($i)++) {
printf(", %s\n",&anchor($a[$i]));
}
while (<>) {
chop; # strip record separator
@a = split('[: ,.]+');
@Fld = split(' ', $_, 9999);
if (/^\./) { goto procline; }
for (my $i = 0; $i <= $#a ; ($i)++) {
printf(", %s\n",&anchor($a[$i]));
}
}
exit(0);
}
printf "%s\n", &htmlify($_);
} }
#sub anchor { sub req {
# local($_) = @_; local ($_) = @_;
# $file = $_ . ".t"; if (/^([\.\'])[ \t]*([^ ]+) *(.*)/) {
# if (-r $file) { my @a = ($1, $2);
# return ("<a href=" . $_ . ".html" . ">" . $_ . "</a>"); $_ = $3;
# } else { while (/(\"((\\.|[^\\\"])*)(\"|$))|(([^ ]|\\.)+) */g) {
# return ( " " . $_ . " "); push(@a, $2 || $5);
# } }
#} return @a;
}
return ();
}
sub checkarg {
my ($n, @a) = @_;
warn "extra arguments for $a[1] ignored" if $#a > $n+1;
warn "missing arguments for $a[1] supplied" if $#a < $n+1;
while ($#a < $n+1) {
push @a, "";
}
return @a;
}
sub anchor { sub anchor {
local ($_) = @_; local ($_) = @_;
my (@file,$file); # FIXME don't create dangling links here
$file = $_ . ".t"; return "<a href=\"$_.html\">$_</a>";
# if (-r $file) {
if (1) {
$file =~ s/.t$/.html/;
return ("<a href=\"$file\">$_</a>");
} else {
@file = <$_*t>;
if (@file) {
warn "Expanding $_ to $file[0]\n";
$file[0] =~ s/.t$/.html/;
return ("<a href=\"$file[0]\">$_</a>");
} else {
warn "Unable to link $_\n";
return ( "<em>$_</em>");
}
}
} }
# Translate HTML special characters into escape sequences # Translate HTML special characters into escape sequences
sub htmlify { sub htmlify {
local ($_) = @_; local ($_) = @_;
s/^\"(.*)\"$/$1/; die "funny escape character `$esc' not supported"
s/\\&//g; # a nothing character if $esc && $esc ne "\\";
# translate some troff escapes
s/\\&//g if $esc; # zero width space character
# escape HTML special characters
s/\&/&amp;/g; s/\&/&amp;/g;
s/\</&lt;/g; s/\</&lt;/g;
s/\>/&gt;/g; s/\>/&gt;/g;
while (@a = /(\\\*Q)([A-Za-z0-9\-\.]+)(\\\*U)/) { return $_ unless $esc;
/(\\\*Q)([A-Za-z\-]+)(\\\*U)/; # translate more troff escapes
$_ = $` . &anchor($a[1]) . $'; s/\\e/&\#92;/g; # escape character
# turn quoted strings that look like info names into links
# tacky...
while (/(\\\*Q)([A-Za-z0-9\-\.]+)(\\\*U)/) {
$_ = $` . anchor($2) . "$'";
} }
while (@a = /(\\\*Q)(\"info )([A-Za-z0-9\-\.]+)(\\\*U)/) { while (/(\"info )([A-Za-z0-9\-\.]+)/) {
/(\\\*Q)(\"info )([\w\-\.]+)(\\\*U)/; $_ = "$`\"info " . anchor($2) . "$'";
$_ = $` . "\"info " . &anchor($a[2]) . $';
}
while (@a = /(\"info )([A-Za-z0-9\-\.]+)/) {
/(\"info )([\w\-\.]+)/;
$_ = $` . "\"info " . &anchor($a[1]) . $';
} }
# tranlate more troff escapes and strings
s/\\\*Q/<em>/g; s/\\\*Q/<em>/g;
s/\\\*U/<\/em>/g; s/\\\*U/<\/em>/g;
s/\\fI/<em>/g; s/\\fI/<em>/g;
@ -157,13 +130,6 @@ sub htmlify {
s/\\\*\(nF/<\/strong><\/em>/g; # normal font s/\\\*\(nF/<\/strong><\/em>/g; # normal font
s/\\\*\(iF/<em>/g; # italic font s/\\\*\(iF/<em>/g; # italic font
s/\\\(mu/x/g; # multiply symbol s/\\\(mu/x/g; # multiply symbol
s/\\ / /g; # should be non breaking space s/\\ /&nbsp;/g; # non breaking space
return $_; return $_;
} }
#sub htmlify {
# local($str) = @_;
# $str;
#}

View file

@ -4,179 +4,128 @@ use strict;
use warnings; use warnings;
my $dome = 0; my $dome = 0;
my (@Fld, $str, @a); my $esc="\\";
my @a;
line: while (<>) { line: while (<>) {
chop; # strip record separator chomp; # strip record separator
@Fld = split(' ', $_, 9999); s/([^\\](\\\\)*)\\\".*/$1/g;# strip comments
procline: @a = req($_);
if (/^\.TH/) {
$str=$Fld[2]; if (!@a) {
for (my $i=3; $i <= $#Fld; $i++) { if ($dome) {
$str .= " " . $Fld[$i]; while ($_ =~ /[A-Za-z0-9\-\.]+/g) {
print htmlify("$`");
print anchor("$&");
$_="$'";
}
} }
$str = &htmlify($str); print htmlify($_), "\n";
printf("<title>%s : %s</title><h1>%s : %s</h1>\n",
$Fld[1], $str, $Fld[1], $str);
next line; next line;
} }
if (/^\.SY../) { # requests
# $i = $_ =~ "\"" && ($RLENGTH = length($&), $RSTART = length($`)+1);
# $str = substr($_, $i + 1, length($_) - $i - 1); if ($a[1] eq "TH") {
$str = substr($_,4); @a = checkarg(2, @a);
$str = &htmlify($str); $a[3] = htmlify($a[3]);
printf "<samp>[##:##] </samp><KBD>%s</KBD><p>\n", $str; print "<title>$a[2] : $a[3]</title><h1>$a[2] : $a[3]</h1>\n";
next line; next line;
} }
if (/^\.EX../) { if ($a[1] eq "SY") {
$str = substr($_, 4); @a = checkarg(1, @a);
printf "<br><samp>[##:##] </samp><kbd>%s</kbd><p>\n", &htmlify($str); $a[2] = htmlify($a[2]);
print "<samp>[##:##] Command : </samp><KBD>$a[2]</KBD><p>\n";
next line; next line;
} }
if (/^\.L/) { if ($a[1] eq "EX") {
@a = split('[: ",.]+'); my $str = htmlify(join(' ',@a[2..$#a]));
$str = &anchor($a[2]); print "<br><samp>[##:##] Command : </samp><kbd>$str</kbd><p>\n";
$str = ("$str $a[3]") if defined $a[3];
printf("<br>%s\n", $str);
next line; next line;
} }
if ($a[1] eq "L") {
$a[2] =~ / /;
print "<br>" . anchor("$`") . " $'\n";
next line;
}
if ($a[1] =~ "eo") { $esc = 0; next line; }
if ($a[1] =~ "ec") { $esc = $#a == 1 ? "\\" : $a[2]; next line; }
if (/^\.(NF|nf)/) { $dome = 1; printf (("<p><pre>\n")); next line; }
if (/^\.(FI|fi)/) { $dome = 0; printf (("</pre><p>\n")); next line; }
if (/^\.s3/) { printf (("<p>\n")); next line; } if (/^\.s3/) { printf (("<p>\n")); next line; }
if (/^\.s1/) {printf (("<hr> \n")); next line; } if (/^\.s1/) { printf (("<hr> \n")); next line; }
if (/^\.br/) { printf "<br>\n"; next line; } if (/^\.br/) { printf "<br>\n"; next line; }
if (/^\.(FI|fi)/) { printf (("</pre><p>\n")); $dome = 0; next line; } if ($a[1] eq "SA") {
if (/^\.(NF|nf)/) { printf (("<p><pre>\n")); $dome = 1; next line; } @a = checkarg(1, @a);
@a = split(/[\: \"\,\.]+/, $a[2]);
if ($dome == 1) { for my $a (@a) {
@a = split('[: ",.]+'); $a = anchor($a);
for (my $i = 1; $i <= $#a ; ($i)++) {
printf("%s",&anchor($a[$i]));
for (my $j = 0; $j < 20 - length($a[$i]); $j++) {
printf(" ");
}
} }
printf("\n"); print "<p>See also : ", join("\n, ", @a), "\n";
while (<>) {
chop; # strip record separator
@a = split('[: ,.]+');
@Fld = split(' ', $_, 9999);
if (/^\./) { goto procline; }
for (my $i = 1; $i <= $#a ; ($i)++) {
printf("%s",&anchor($a[$i]));
for (my $j = 0; $j < 20 - length($a[$i]); $j++) {
printf(" ");
}
}
printf("\n");
}
next line;
} }
if (/^\.SA/) { # ignore unknown request
@a = split('[: ",.]+');
printf("<p>See also : %s\n",&anchor($a[2]) );
for (my $i = 3; $i <= $#a ; ($i)++) {
printf(", %s\n",&anchor($a[$i]));
}
while (<>) {
chop; # strip record separator
@a = split('[: ,.]+');
@Fld = split(' ', $_, 9999);
if (/^\./) { goto procline; }
for (my $i = 0; $i <= $#a ; ($i)++) {
printf(", %s\n",&anchor($a[$i]));
}
}
exit(0);
}
if (/^\./) { next line; }
if (/^(See also|See Also|see also)/) {
@a = split('[: ,.]+');
printf("See also : %s\n",&anchor($a[2]) );
for (my $i = 3; $i <= $#a ; ($i)++) {
printf(", %s\n",&anchor($a[$i]));
}
while (<>) {
chop; # strip record separator
@a = split('[: ,.]+');
@Fld = split(' ', $_, 9999);
if (/^\./) { goto procline; }
for (my $i = 0; $i <= $#a ; ($i)++) {
printf(", %s\n",&anchor($a[$i]));
}
}
exit(0);
}
printf "%s\n", &htmlify($_);
} }
#sub anchor { sub req {
# local($_) = @_; local ($_) = @_;
# $file = $_ . ".t"; if (/^([\.\'])[ \t]*([^ ]+) *(.*)/) {
# if (-r $file) { my @a = ($1, $2);
# return ("<a href=" . $_ . ".html" . ">" . $_ . "</a>"); $_ = $3;
# } else { while (/(\"((\\.|[^\\\"])*)(\"|$))|(([^ ]|\\.)+) */g) {
# return ( " " . $_ . " "); push(@a, $2 || $5);
# } }
#} return @a;
}
return ();
}
sub checkarg {
my ($n, @a) = @_;
warn "extra arguments for $a[1] ignored" if $#a > $n+1;
warn "missing arguments for $a[1] supplied" if $#a < $n+1;
while ($#a < $n+1) {
push @a, "";
}
return @a;
}
sub anchor { sub anchor {
local ($_) = @_; local ($_) = @_;
my (@file, $file); # FIXME don't create dangling links here
$file = $_ . ".t"; return "<a href=\"$_.html\">$_</a>";
# if (-r $file) {
if (1) {
$file =~ s/.t$/.html/;
return ("<a href=\"$file\">$_</a>");
} else {
@file = <$_*t>;
if (@file) {
warn "Expanding $_ to $file[0]\n";
$file[0] =~ s/.t$/.html/;
return ("<a href=\"$file[0]\">$_</a>");
} else {
warn "Unable to link $_\n";
return ( "<em>$_</em>");
}
}
} }
# Translate HTML special characters into escape sequences # Translate HTML special characters into escape sequences
sub htmlify { sub htmlify {
local($_) = @_; local ($_) = @_;
s/^\"(.*)\"$/$1/; die "funny escape character `$esc' not supported"
s/\\&//g; # a nothing character if $esc && $esc ne "\\";
# translate some troff escapes
s/\\&//g if $esc; # zero width space character
# escape HTML special characters
s/\&/&amp;/g; s/\&/&amp;/g;
s/\</&lt;/g; s/\</&lt;/g;
s/\>/&gt;/g; s/\>/&gt;/g;
while (@a = /(\\\*Q)([A-Za-z0-9\-\.]+)(\\\*U)/) { return $_ unless $esc;
/(\\\*Q)([A-Za-z\-]+)(\\\*U)/; # translate more troff escapes
$_ = $` . &anchor($a[1]) . $'; s/\\e/&\#92;/g; # escape character
# turn quoted strings that look like info names into links
# tacky...
while (/(\\\*Q)([A-Za-z0-9\-\.]+)(\\\*U)/) {
$_ = $` . anchor($2) . "$'";
} }
while (@a = /(\\\*Q)(\"info )([A-Za-z0-9\-\.]+)(\\\*U)/) { while (/(\"info )([A-Za-z0-9\-\.]+)/) {
/(\\\*Q)(\"info )([\w\-\.]+)(\\\*U)/; $_ = "$`\"info " . anchor($2) . "$'";
$_ = $` . "\"info " . &anchor($a[2]) . $';
}
while (@a = /(\"info )([A-Za-z0-9\-\.]+)/) {
/(\"info )([\w\-\.]+)/;
$_ = $` . "\"info " . &anchor($a[1]) . $';
} }
# tranlate more troff escapes and strings
s/\\\*Q/<em>/g; s/\\\*Q/<em>/g;
s/\\\*U/<\/em>/g; s/\\\*U/<\/em>/g;
s/\\fI/<em>/g; s/\\fI/<em>/g;
@ -188,13 +137,6 @@ sub htmlify {
s/\\\*\(nF/<\/strong><\/em>/g; # normal font s/\\\*\(nF/<\/strong><\/em>/g; # normal font
s/\\\*\(iF/<em>/g; # italic font s/\\\*\(iF/<em>/g; # italic font
s/\\\(mu/x/g; # multiply symbol s/\\\(mu/x/g; # multiply symbol
s/\\ / /g; # should be non breaking space s/\\ /&nbsp;/g; # non breaking space
return $_; return $_;
} }
#sub htmlify {
# local($str) = @_;
# $str;
#}