3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-25 21:39:27 +01:00

c99/c11 commands can now take -text option to filter output

This commit is contained in:
Pragmatic Software 2022-04-04 10:42:42 -07:00
parent 03fb901291
commit 8381fc7ad2
3 changed files with 101 additions and 19 deletions

54
applets/c11std.pl vendored
View File

@ -16,11 +16,11 @@ my $search = join ' ', @ARGV;
if (not length $search) {
print
"Usage: c11std [-list] [-n#] [-section <section>] [search text] -- 'section' must be in the form of X.YpZ where X and Y are section/chapter and, optionally, pZ is paragraph. If both 'section' and 'search text' are specified, then the search space will be within the specified section. You may use -n # to skip to the #th match. To list only the section numbers containing 'search text', add -list.\n";
"Usage: c11std [-list] [-n#] [-section <section>] [search text] [-text <regex>] -- `section` must be in the form of `X.Y[pZ]` where `X` and `Y` are section/chapter and, optionally, `pZ` is paragraph. If both `section` and `search text` are specified, then the search space will be within the specified section. Use `-n <n>` to skip to the nth match. To list only the section numbers containing 'search text', add -list. To display specific text, use `-text <regex>`.\n";
exit 0;
}
my ($section, $paragraph, $section_specified, $paragraph_specified, $match, $list_only, $list_titles);
my ($section, $paragraph, $section_specified, $paragraph_specified, $match, $list_only, $list_titles, $match_text);
$section_specified = 0;
$paragraph_specified = 0;
@ -40,8 +40,11 @@ if ($search =~ s/-section\s*([A-Z0-9\.p]+)//i or $search =~ s/\b([A-Z0-9]+\.[0-9
$section_specified = 1;
}
if ($search =~ s/-n\s*(\d+)//) { $match = $1; }
else { $match = 1; }
if ($search =~ s/-n\s*(\d+)//) {
$match = $1;
} else {
$match = 1;
}
if ($search =~ s/-list//i) {
$list_only = 1;
@ -53,6 +56,10 @@ if ($search =~ s/-titles//i) {
$list_titles = 1;
}
if ($search =~ s/-text ([^ ]+)//) {
$match_text = $1;
}
$search =~ s/^\s+//;
$search =~ s/\s+$//;
@ -214,10 +221,10 @@ if ($comma eq "") {
print "p" . $found_paragraph if $paragraph_specified;
=cut
print "http://www.iso-9899.info/n1570.html\#$found_section";
print "p" . $found_paragraph if $paragraph_specified;
print "\n\n";
print "[", $found_section_title, "]\n\n" if length $found_section_title;
print "http://www.iso-9899.info/n1570.html\#$found_section";
print "p" . $found_paragraph if $paragraph_specified;
print "\n\n";
print "[", $found_section_title, "]\n\n" if length $found_section_title;
}
$result =~ s/\s*Constraints\s*$//;
@ -227,4 +234,35 @@ $result =~ s/\s*Returns\s*$//;
$result =~ s/\s*Runtime-constraints\s*$//;
$result =~ s/\s*Recommended practice\s*$//;
if (length $match_text) {
my $match_result = $result;
$match_result =~ s/\s+/ /g;
my $match = eval {
$match_result =~ m/(.*)($match_text)(.*)/msi;
return [$1, $2, $3];
};
if ($@) {
print "Error in -text option: $@\n";
exit 1;
}
$result = '';
if (length $match->[0]) {
$result = '... ';
}
if (length $match->[1]) {
$result .= $match->[1];
} else {
$result = "No text found for `$match_text`.";
}
if (length $match->[2]) {
$result .= ' ...';
}
}
print "$result\n";

62
applets/c99std.pl vendored
View File

@ -16,11 +16,11 @@ my $search = join ' ', @ARGV;
if (not length $search) {
print
"Usage: c99std [-list] [-n#] [-section <section>] [search text] -- 'section' must be in the form of X.YpZ where X and Y are section/chapter and, optionally, pZ is paragraph. If both 'section' and 'search text' are specified, then the search space will be within the specified section. You may use -n # to skip to the #th match. To list only the section numbers containing 'search text', add -list.\n";
"Usage: c99std [-list] [-n#] [-section <section>] [search text] [-text <regex>] -- `section` must be in the form of `X.Y[pZ]` where `X` and `Y` are section/chapter and, optionally, `pZ` is paragraph. If both `section` and `search text` are specified, then the search space will be within the specified section. Use `-n <n>` to skip to the nth match. To list only the section numbers containing 'search text', add -list. To display specific text, use `-text <regex>`.\n";
exit 0;
}
my ($section, $paragraph, $section_specified, $paragraph_specified, $match, $list_only, $list_titles);
my ($section, $paragraph, $section_specified, $paragraph_specified, $match, $list_only, $list_titles, $match_text);
$section_specified = 0;
$paragraph_specified = 0;
@ -40,8 +40,11 @@ if ($search =~ s/-section\s*([A-Z0-9\.p]+)//i or $search =~ s/\b([A-Z0-9]+\.[0-9
$section_specified = 1;
}
if ($search =~ s/-n\s*(\d+)//) { $match = $1; }
else { $match = 1; }
if ($search =~ s/-n\s*(\d+)//) {
$match = $1;
} else {
$match = 1;
}
if ($search =~ s/-list//i) {
$list_only = 1;
@ -53,6 +56,10 @@ if ($search =~ s/-titles//i) {
$list_titles = 1;
}
if ($search =~ s/-text ([^ ]+)//) {
$match_text = $1;
}
$search =~ s/^\s+//;
$search =~ s/\s+$//;
@ -168,7 +175,7 @@ while ($text =~ m/^\s{0,4}([0-9A-Z]+\.[0-9\.]*)/msg) {
last if $found && $paragraph_specified == $USER_SPECIFIED;
if ($paragraph_specified == $USER_SPECIFIED) {
if (length $search) { print "No such text '$search' found within paragraph $paragraph of section $section of n1256.\n"; }
if (length $search) { print "No such text '$search' in paragraph $paragraph of section $section of n1256.\n"; }
else { print "No such paragraph $paragraph in section $section of n1256.\n"; }
exit 0;
}
@ -208,10 +215,16 @@ $result =~ s/[\n\r]/ /g;
if ($matches > 1 and not $list_only) { print "Displaying $match of $matches matches: "; }
if ($comma eq "") {
print "http://www.iso-9899.info/n1256.html\#$found_section";
print "p" . $found_paragraph if $paragraph_specified;
print "\n\n";
print "[", $found_section_title, "]\n\n" if length $found_section_title;
=cut
print $found_section;
print "p" . $found_paragraph if $paragraph_specified;
=cut
print "http://www.iso-9899.info/n1256.html\#$found_section";
print "p" . $found_paragraph if $paragraph_specified;
print "\n\n";
print "[", $found_section_title, "]\n\n" if length $found_section_title;
}
$result =~ s/\s*Constraints\s*$//;
@ -221,4 +234,35 @@ $result =~ s/\s*Returns\s*$//;
$result =~ s/\s*Runtime-constraints\s*$//;
$result =~ s/\s*Recommended practice\s*$//;
if (length $match_text) {
my $match_result = $result;
$match_result =~ s/\s+/ /g;
my $match = eval {
$match_result =~ m/(.*)($match_text)(.*)/msi;
return [$1, $2, $3];
};
if ($@) {
print "Error in -text option: $@\n";
exit 1;
}
$result = '';
if (length $match->[0]) {
$result = '... ';
}
if (length $match->[1]) {
$result .= $match->[1];
} else {
$result = "No text found for `$match_text`.";
}
if (length $match->[2]) {
$result .= ' ...';
}
}
print "$result\n";

View File

@ -25,8 +25,8 @@ use PBot::Imports;
# These are set by the /misc/update_version script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 4520,
BUILD_DATE => "2022-03-31",
BUILD_REVISION => 4521,
BUILD_DATE => "2022-04-04",
};
sub initialize {}