diff --git a/configure b/configure index 3f9e07c..38055a6 100755 --- a/configure +++ b/configure @@ -5,8 +5,8 @@ set -e mkf=config.mk -objs='esv.o esvapi.o initial.o' -srcs='esv.d esvapi.d initial.d' +objs='esv.o esvapi.o util.o initial.o' +srcs='esv.d esvapi.d util.d initial.d' # utility functions diff --git a/esv.d b/esv.d index e3bbb64..ee67571 100644 --- a/esv.d +++ b/esv.d @@ -27,11 +27,12 @@ import std.format : format; import std.getopt : getopt, GetOptException; import std.path : baseName, dirName, expandTilde, isValidPath; import std.process : environment, executeShell; -import std.stdio : writef, writeln, writefln, File; +import std.stdio : writeln, writefln; import std.string : splitLines; import esvapi; import initial; +import util; import cf = config; @@ -48,13 +49,6 @@ bool rFlag, RFlag; /* passage references */ string sFlag; /* search passages */ bool VFlag; /* show version */ -string[] mainArgs; -File stderr; - -version (OpenBSD) { - immutable(char) *promises; -} - int main(string[] args) { @@ -63,18 +57,7 @@ main(string[] args) INIUnit ini; ESVApi esv; - mainArgs = 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"); + sharedInit(args); /* Parse command-line options */ try { @@ -93,7 +76,7 @@ main(string[] args) "V", &VFlag, ); } catch (GetOptException e) { - string opt = extractOpt(e); + string opt = extractFlag(e); enforceDie(!e.msg.startsWith("Unrecognized option"), "unknown option " ~ opt); @@ -245,51 +228,6 @@ key = %s 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 onLineLength(string flag, string value) { @@ -299,11 +237,3 @@ onLineLength(string flag, string value) catch (ConvException e) die("illegal argument to -l option -- must be a positive integer"); } - -private string -parseBook(in string book) -{ - import std.string : tr; - - return book.tr("-_", " "); -} diff --git a/util.d b/util.d new file mode 100644 index 0000000..826fff7 --- /dev/null +++ b/util.d @@ -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); +}