esv: split utility functions into separate module
This commit is contained in:
parent
1ed36260f3
commit
d8e267d5ba
3 changed files with 95 additions and 76 deletions
4
configure
vendored
4
configure
vendored
|
@ -5,8 +5,8 @@
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
mkf=config.mk
|
mkf=config.mk
|
||||||
objs='esv.o esvapi.o initial.o'
|
objs='esv.o esvapi.o util.o initial.o'
|
||||||
srcs='esv.d esvapi.d initial.d'
|
srcs='esv.d esvapi.d util.d initial.d'
|
||||||
|
|
||||||
# utility functions
|
# utility functions
|
||||||
|
|
||||||
|
|
78
esv.d
78
esv.d
|
@ -27,11 +27,12 @@ import std.format : format;
|
||||||
import std.getopt : getopt, GetOptException;
|
import std.getopt : getopt, GetOptException;
|
||||||
import std.path : baseName, dirName, expandTilde, isValidPath;
|
import std.path : baseName, dirName, expandTilde, isValidPath;
|
||||||
import std.process : environment, executeShell;
|
import std.process : environment, executeShell;
|
||||||
import std.stdio : writef, writeln, writefln, File;
|
import std.stdio : writeln, writefln;
|
||||||
import std.string : splitLines;
|
import std.string : splitLines;
|
||||||
|
|
||||||
import esvapi;
|
import esvapi;
|
||||||
import initial;
|
import initial;
|
||||||
|
import util;
|
||||||
|
|
||||||
import cf = config;
|
import cf = config;
|
||||||
|
|
||||||
|
@ -48,13 +49,6 @@ bool rFlag, RFlag; /* passage references */
|
||||||
string sFlag; /* search passages */
|
string sFlag; /* search passages */
|
||||||
bool VFlag; /* show version */
|
bool VFlag; /* show version */
|
||||||
|
|
||||||
string[] mainArgs;
|
|
||||||
File stderr;
|
|
||||||
|
|
||||||
version (OpenBSD) {
|
|
||||||
immutable(char) *promises;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(string[] args)
|
main(string[] args)
|
||||||
{
|
{
|
||||||
|
@ -63,18 +57,7 @@ main(string[] args)
|
||||||
INIUnit ini;
|
INIUnit ini;
|
||||||
ESVApi esv;
|
ESVApi esv;
|
||||||
|
|
||||||
mainArgs = args;
|
sharedInit(args);
|
||||||
|
|
||||||
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);
|
|
||||||
}();
|
|
||||||
|
|
||||||
/* @safe way of opening stderr on Unix */
|
|
||||||
stderr = File("/dev/stderr", "w");
|
|
||||||
|
|
||||||
/* Parse command-line options */
|
/* Parse command-line options */
|
||||||
try {
|
try {
|
||||||
|
@ -93,7 +76,7 @@ main(string[] args)
|
||||||
"V", &VFlag,
|
"V", &VFlag,
|
||||||
);
|
);
|
||||||
} catch (GetOptException e) {
|
} catch (GetOptException e) {
|
||||||
string opt = extractOpt(e);
|
string opt = extractFlag(e);
|
||||||
|
|
||||||
enforceDie(!e.msg.startsWith("Unrecognized option"),
|
enforceDie(!e.msg.startsWith("Unrecognized option"),
|
||||||
"unknown option " ~ opt);
|
"unknown option " ~ opt);
|
||||||
|
@ -245,51 +228,6 @@ key = %s
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void
|
|
||||||
warn(string mesg)
|
|
||||||
{
|
|
||||||
stderr.writeln(baseName(mainArgs[0]) ~ ": " ~ mesg);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void
|
|
||||||
die(string mesg) @trusted
|
|
||||||
{
|
|
||||||
import core.runtime : Runtime;
|
|
||||||
import core.stdc.stdlib : exit;
|
|
||||||
|
|
||||||
warn(mesg);
|
|
||||||
Runtime.terminate();
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ushort
|
|
||||||
terminalColumns() @trusted
|
|
||||||
{
|
|
||||||
import core.sys.posix.sys.ioctl;
|
|
||||||
|
|
||||||
winsize w;
|
|
||||||
|
|
||||||
ioctl(1, TIOCGWINSZ, &w);
|
|
||||||
return w.ws_col > 72 ? 72 : w.ws_col;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void
|
|
||||||
enforceDie(A...)(bool cond, string fmt, A a)
|
|
||||||
{
|
|
||||||
import std.format : format;
|
|
||||||
|
|
||||||
if (!cond)
|
|
||||||
die(format(fmt, a));
|
|
||||||
}
|
|
||||||
|
|
||||||
private string
|
|
||||||
extractOpt(in GetOptException e)
|
|
||||||
{
|
|
||||||
import std.regex : matchFirst;
|
|
||||||
|
|
||||||
return e.msg.matchFirst("-.")[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
private void
|
private void
|
||||||
onLineLength(string flag, string value)
|
onLineLength(string flag, string value)
|
||||||
{
|
{
|
||||||
|
@ -299,11 +237,3 @@ onLineLength(string flag, string value)
|
||||||
catch (ConvException e)
|
catch (ConvException e)
|
||||||
die("illegal argument to -l option -- must be a positive integer");
|
die("illegal argument to -l option -- must be a positive integer");
|
||||||
}
|
}
|
||||||
|
|
||||||
private string
|
|
||||||
parseBook(in string book)
|
|
||||||
{
|
|
||||||
import std.string : tr;
|
|
||||||
|
|
||||||
return book.tr("-_", " ");
|
|
||||||
}
|
|
||||||
|
|
89
util.d
Normal file
89
util.d
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
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);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue