ManUtils: Move, use ExtUtils::MakeMaker and get rid of AnyEvent

I mean, I like AnyEvent, but this can be done with core modules as well
(albeit somewhat more verbose and error-prone...)
This commit is contained in:
Yorhel 2025-02-24 16:27:07 +01:00
parent 2f33e7f4b5
commit 682321d1be
9 changed files with 92 additions and 106 deletions

10
ManUtils/Makefile.PL Normal file
View file

@ -0,0 +1,10 @@
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'ManUtils',
VERSION_FROM => 'ManUtils.pm',
LICENSE => 'mit',
NO_MYMETA => 1,
MIN_PERL_VERSION => 'v5.36',
LDFROM => 'ManUtils.o ../web/target/release/libweb.a -lpthread',
);

68
ManUtils/ManUtils.pm Normal file
View file

@ -0,0 +1,68 @@
package ManUtils 0.2;
use v5.36;
use IPC::Open3;
use IO::Poll qw/POLLOUT POLLIN/;
use Symbol 'gensym';
use XSLoader;
XSLoader::load('ManUtils');
sub fmt($input) {
my @cmd = 'groff';
push @cmd, '-t' if $input =~ /^\.TS/m;
push @cmd, '-e' if $input =~ /^\.EQ/m;
# $MANWIDTH works by using the following groff options: -rLL=100n -rLT=100n
push @cmd, qw/-mandoc -Tutf8 -DUTF-8 -P-c -rLL=80n -rLT=80n -/;
$input =
# Disable hyphenation, since that screws up man page references. :-(
".hy 0\n.de hy\n..\n"
# Emulate man-db's --nj option
.".na\n.de ad\n..\n"
# Unbreak some quotes, there's plenty of man pages that ("incorrectly") use these for code samples.
.".char ` \\`\n"
.".char ' \\[aq]\n"
.$input;
utf8::encode($input);
my $pid = open3(my $in, my $out, my $err = gensym, @cmd);
my $inoff = 0;
my $output = '';
my $poll = IO::Poll->new;
$poll->mask($in => POLLOUT);
$poll->mask($out => POLLIN);
$poll->mask($err => POLLIN);
while (1) {
$poll->poll;
if ($poll->events($in)) {
my $r = syswrite $in, $input, 16*1024, $inoff;
die "Write error: $!\n" if $r <= 0;
$inoff += $r;
if ($inoff == length $input) {
$poll->remove($in);
close $in;
}
}
if ($poll->events($out)) {
my $r = sysread $out, $output, 16*1024, length $output;
die "Read error: $!\n" if $r < 0;
if ($r == 0) {
utf8::decode($output);
waitpid $pid, 0;
die "Groff exited with $?\n" if $?;
return $output =~ s/\s+$//r;
}
}
if ($poll->events($err)) {
my $r = sysread $err, my $buf, 16*1024;
$poll->remove($err) if $r == 0;
warn "GROFF: $buf\n" if $r > 0;
}
}
}
1;

28
ManUtils/ManUtils.xs Normal file
View file

@ -0,0 +1,28 @@
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
struct StringWrap {
char *buf;
unsigned long long len, cap;
};
struct StringWrap grotty2html_wrap(const char *, unsigned long long);
void grotty2html_free(struct StringWrap);
MODULE = ManUtils PACKAGE = ManUtils
SV *
html(str)
SV *str
CODE:
STRLEN len;
char *inbuf = SvPV(str, len);
struct StringWrap buf = grotty2html_wrap(inbuf, len);
SV *dest = buf.len ? newSVpv(buf.buf, buf.len) : newSVpv("", 0);
grotty2html_free(buf);
SvUTF8_on(dest);
RETVAL = dest;
OUTPUT:
RETVAL