You can search for identifiers within the package.
in-package search v0.2.0
Source file pty.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213(* The child of a {!spawn} handle. [Running pid] is a child the handle has
not seen reaped: until it is reaped, its id names it and no other process.
[Exited status] is a child the handle reaped while checking on it, whose
status {!wait} returns. *)typechild=No_child|Runningofint|ExitedofUnix.process_statustypet={fd:Unix.file_descr;mutablechild:child;(* The slave of a {!spawn} PTY, held until [close] where [hold_slave]. *)slave:Unix.file_descroption;(* [close] must be a true no-op the second time: the kernel can reallocate
the fd number to anything else in the process, and a second [Unix.close]
would then close that unrelated descriptor. *)mutableclosed:bool;}typewinsize={rows:int;cols:int;xpixel:int;ypixel:int}(* External C functions *)externalopen_pty_raw:unit->Unix.file_descr*Unix.file_descr="ocaml_pty_open"externalget_winsize_raw:Unix.file_descr->winsize="ocaml_pty_get_winsize"externalset_winsize_raw:Unix.file_descr->winsize->unit="ocaml_pty_set_winsize_byte""ocaml_pty_set_winsize"externalfork_exec:Unix.file_descr->stringarray->stringarray->stringarray->stringoption->int="ocaml_pty_fork_exec"(* Why, and on which platforms, is in pty_stubs.c. *)externalhold_slave:unit->bool="ocaml_pty_hold_slave"lethold_slave=hold_slave()letfile_descrt=t.fdletin_fdt=t.fdletout_fdt=t.fdletpidt=matcht.childwithRunningpid->Somepid|_->None(* Every fd-consuming operation goes through this guard so a closed handle
raises EBADF instead of touching a possibly-reused fd number. *)letlive_fdtop=ift.closedthenraise(Unix.Unix_error(Unix.EBADF,op,""));t.fd(* Asks the system, without blocking, what became of a running child. One
that has exited is reaped here and its status kept. ECHILD means the
program reaped it with its own waitpid, and its id may now name another
process, so the handle forgets it. *)letreccheckt=matcht.childwith|No_child|Exited_->()|Runningpid->(matchUnix.waitpid[WNOHANG]pidwith|0,_->()|_,status->t.child<-Exitedstatus|exceptionUnix.Unix_error(EINTR,_,_)->checkt|exceptionUnix.Unix_error(ECHILD,_,_)->t.child<-No_child)(* Sends [signal] to the child if it is still running. *)letsignaltsignal=checkt;matcht.childwith|Runningpid->(tryUnix.killpidsignalwithUnix.Unix_error_->())|No_child|Exited_->()letterminatet=matcht.childwith|No_child->invalid_arg"Pty.terminate: no child process"|Running_|Exited_->signaltSys.sigtermletkillt=matcht.childwith|No_child->invalid_arg"Pty.kill: no child process"|Running_|Exited_->signaltSys.sigkillletrecwaitt=matcht.childwith|No_child->invalid_arg"Pty.wait: no child process"|Exitedstatus->t.child<-No_child;status|Runningpid->(matchUnix.waitpid[]pidwith|_,status->t.child<-No_child;status|exceptionUnix.Unix_error(EINTR,_,_)->waitt|exception(Unix.Unix_error(ECHILD,_,_)ase)->t.child<-No_child;raisee)(* After close's SIGTERM, the child has about 100 ms to exit, polled
without blocking, before SIGKILL. *)letrecgracetattempts=checkt;matcht.childwith|Runningpidwhenattempts<=0->(tryUnix.killpidSys.sigkillwithUnix.Unix_error_->())|Running_->Unix.sleepf0.01;gracet(attempts-1)|No_child|Exited_->()letclose_fdfd=tryUnix.closefdwithUnix.Unix_error_->()letclose?wait:(reap=true)t=ift.closedthen()else(t.closed<-true;(* Hang up first: a child whose exit waits for its output to be read can
then exit, so the reap below does not wait on it. *)close_fdt.fd;Option.iterclose_fdt.slave;signaltSys.sigterm;gracet10;ifreapthen((matcht.childwith|Running_->(tryignore(waitt:Unix.process_status)withUnix.Unix_error_->())|No_child|Exited_->());t.child<-No_child))letget_winsizet=get_winsize_raw(live_fdt"get_winsize")letset_winsizetws=set_winsize_raw(live_fdt"set_winsize")wsletresizet~rows~cols=letws={rows;cols;xpixel=0;ypixel=0}inset_winsizetwsletinherit_size~src~dst=letws=get_winsizesrcinset_winsizedstwsletopen_pty?winsize()=letmaster_fd,slave_fd=open_pty_raw()inletmaster={fd=master_fd;child=No_child;slave=None;closed=false}inletslave={fd=slave_fd;child=No_child;slave=None;closed=false}in(* Set initial window size if provided *)(matchwinsizewith|Somews->(tryset_winsizeslavewswithe->closemaster;closeslave;raisee)|None->());(master,slave)(* Where [execvp] looks for [prog], in order: [prog] itself when it names a
path, else [prog] in each directory of the parent's [PATH], where an empty
directory is the current one. *)letsearch_pathprog=ifString.containsprog'/'then[|prog|]elseletpath=Option.value(Sys.getenv_opt"PATH")~default:"/bin:/usr/bin"inString.split_on_char':'path|>List.map(fundir->ifdir=""thenprogelseFilename.concatdirprog)|>Array.of_listletspawn?env?cwd?winsize~prog~args()=letmaster,slave=open_pty?winsize()inletenv=matchenvwithSomeenv->env|None->Unix.environment()inletargv=Array.of_list(prog::args)inmatchfork_execslave.fd(search_pathprog)argvenvcwdwith|pid->letslave=ifhold_slavethenSomeslave.fdelse(closeslave;None)in{masterwithchild=Runningpid;slave}|exceptione->closemaster;closeslave;raiseeletwith_pty?winsizef=letmaster,slave=open_pty?winsize()inFun.protect~finally:(fun()->(tryclosemasterwithUnix.Unix_error_->());trycloseslavewithUnix.Unix_error_->())(fun()->fmasterslave)letwith_spawn?env?cwd?winsize~prog~argsf=letpty=spawn?env?cwd?winsize~prog~args()inFun.protect~finally:(fun()->trycloseptywithUnix.Unix_error_->())(fun()->fpty)(* I/O operations *)letreadtbufofslen=Unix.read(live_fdt"read")bufofslenletwritetbufofslen=Unix.write(live_fdt"write")bufofslenletwrite_stringtstrofslen=Unix.write_substring(live_fdt"write")strofslen(* Non-blocking mode *)letset_nonblockt=Unix.set_nonblock(live_fdt"set_nonblock")letclear_nonblockt=Unix.clear_nonblock(live_fdt"clear_nonblock")