123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140(*---------------------------------------------------------------------------
Copyright (c) 2018 The b0 programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)openB0_stdopenB0_std.Result.SyntaxopenCmdliner(* Environment variables *)moduleEnv=structletpager="PAGER"letless="LESS"letterm="TERM"endletenvs=letvars=lazybeginCmd.Env.infoEnv.pager~doc:"The pager used to display content. This is a command invocation \
given to execvp(3)."::Cmd.Env.infoEnv.term~doc:"See options $(b,--color) and $(b,--no-pager)."::[]endinfun()->Lazy.forcevars(* Paging *)letfind?win_exe?search~don't()=ifdon'tthenOkNoneelsematchOs.Env.find~empty_is_none:trueEnv.termwith|Some"dumb"|None->OkNone|Some_->letcmds=B0_std.[Cmd.atom"less";Cmd.atom"more"]inlet*cmds=letempty_is_none=trueinmatchOs.Env.find'~empty_is_noneB0_std.Cmd.of_stringEnv.pagerwith|Error_ase->e|OkNone->Okcmds|Ok(Somecmd)->Ok(cmd::cmds)inletrecloop=function|[]->OkNone|cmd::cmds->matchOs.Cmd.find?win_exe?searchcmdwith|OkNone->loopcmds|v->vinloopcmdsletpager_env()=matchOs.Env.find~empty_is_none:falseEnv.lesswith|Some_->OkNone|None->Result.bind(Os.Env.current_assignments())@@funenv->Ok(Some("LESS=FRX"::env))letpage_stdout=function|None->Ok()|Somepager->letuerr=Unix.error_messageinleterrfmt=Fmt.error("page stdout: "^^fmt)inletrecdup2fd0fd1=matchUnix.dup2fd0fd1with|()->Ok()|exceptionUnix.Unix_error(Unix.EINTR,_,_)->dup2fd0fd1|exceptionUnix.Unix_error(e,_,_)->err"dup2: %s"(uerre)inmatchpager_env()with|Errore->err"%s"e|Okenv->matchUnix.pipe()with|exceptionUnix.Unix_error(e,_,_)->err"pipe: %s"(uerre)|(pager_read,parent_write)->letstdin=Os.Cmd.in_fd~close:truepager_readinUnix.set_close_on_execparent_write;Os.Fd.apply~close:Unix.closeparent_write@@funparent_write->Result.bind(Os.Cmd.spawn?env~stdinpager)@@funpid->Result.bind(dup2parent_writeUnix.stdout)@@fun()->letparent_pid=Unix.getpid()inleton_parent_exit()=(* We need to be careful here, forked processes will also
get execute to this. *)ifparent_pid=Unix.getpid()thenbegin(* Before closing Unix.stdout it's better to flush
formatter and channels. Otherwise it's done later
by OCaml's standard shutdown procedure and it
raises Sys_error as the fd is no longer valid. *)(tryFmt.flushFmt.stdout()withSys_error_->());(tryflushstdoutwithSys_error_->());(tryUnix.closeUnix.stdoutwithUnix.Unix_error_->());Log.if_error~use:()@@Result.mapignore@@Os.Cmd.spawn_wait_statuspidendinat_exiton_parent_exit;Ok()letpage_filespagerfiles=matchpagerwith|Somepagerwhenfiles=[]->Ok()|Somepager->Os.Cmd.runB0_std.Cmd.(pager%%pathsfiles)|None->letrecloop=function|[]->Ok()|f::fs->matchOs.File.readfwith|Error_ase->e|Okd->Printf.printf"%s"d;iffs<>[]thenPrintf.printf"\x1C"(* U+001C FS *);flushstdout;loopfsinloopfiles(* Cli interaction *)letdon't?(docs=Manpage.s_common_options)()=letdoc="Do not display the output in a pager. This automatically happens \
if the $(b,TERM) environment variable is $(b,dumb) or unset."inArg.(value&flag&info["no-pager"]~docs~doc)(*---------------------------------------------------------------------------
Copyright (c) 2018 The b0 programmers
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
---------------------------------------------------------------------------*)