89 lines
1.3 KiB
D
89 lines
1.3 KiB
D
module util;
|
|
|
|
import std.getopt : GetOptException;
|
|
import std.stdio : File;
|
|
|
|
public @safe:
|
|
|
|
File stderr;
|
|
|
|
private {
|
|
string[] mainArgs;
|
|
|
|
version (OpenBSD) {
|
|
immutable(char) *promises;
|
|
}
|
|
}
|
|
|
|
/++
|
|
+ Common initialisation function shared between esv and esvsearch
|
|
+/
|
|
void
|
|
sharedInit(string[] args)
|
|
{
|
|
mainArgs = args;
|
|
stderr = File("/dev/stderr", "w");
|
|
|
|
version (OpenBSD) () @trusted {
|
|
import core.sys.openbsd.unistd : pledge;
|
|
import std.string : toStringz;
|
|
|
|
promises = toStringz("stdio rpath wpath cpath inet dns tty proc exec prot_exec");
|
|
pledge(promises, null);
|
|
}();
|
|
}
|
|
|
|
void
|
|
enforceDie(A...)(bool cond, string fmt, A a)
|
|
{
|
|
import std.format : format;
|
|
|
|
if (!cond)
|
|
die(format(fmt, a));
|
|
}
|
|
|
|
string
|
|
extractFlag(in GetOptException e)
|
|
{
|
|
import std.regex : matchFirst;
|
|
|
|
return e.msg.matchFirst("-.")[0];
|
|
}
|
|
|
|
string
|
|
parseBook(in string book)
|
|
{
|
|
import std.string : tr;
|
|
|
|
return book.tr("-_", " ");
|
|
}
|
|
|
|
ushort
|
|
terminalColumns() @trusted
|
|
{
|
|
import core.sys.posix.sys.ioctl;
|
|
|
|
winsize w;
|
|
|
|
ioctl(1, TIOCGWINSZ, &w);
|
|
return w.ws_col > 72 ? 72 : w.ws_col;
|
|
}
|
|
|
|
void
|
|
warn(string mesg)
|
|
{
|
|
import std.path : baseName;
|
|
|
|
stderr.writeln(baseName(mainArgs[0]) ~ ": " ~ mesg);
|
|
}
|
|
|
|
void
|
|
die(string mesg) @trusted
|
|
{
|
|
import core.runtime : Runtime;
|
|
import core.stdc.stdlib : exit;
|
|
|
|
warn(mesg);
|
|
Runtime.terminate();
|
|
exit(1);
|
|
}
|