[@sdZddlmZddlZddlZdddgZdZdZdZdZd dZ d d Z d d Z ddZ ddZ ddZddZeeddZeeddZdS)zPseudo terminal utilities.)selectNopenptyforkspawnc CsNytjSWnttfk r(YnXt\}}t|}||fS)zdopenpty() -> (master_fd, slave_fd) Open a pty master/slave pair, using os.openpty() if possible.)osrAttributeErrorOSError_open_terminal slave_open) master_fd slave_nameslave_fdr/usr/lib/python3.4/pty.pyrs c Cs^ytj\}}Wnttfk r0Yn'Xtj|}tj|||fStS)zmaster_open() -> (master_fd, slave_name) Open a pty master and return the fd, and the filename of the slave end. Deprecated, use openpty() instead.)rrr r ttynamecloser )r rrrrr master_open!s  rc CsxmdD]e}x\dD]T}d||}ytj|tj}Wntk rXwYnX|d||fSWqWtddS)z1Open pty master and return (master_fd, tty_name).ZpqrstuvwxyzPQRSTZ0123456789abcdefz/dev/ptyz/dev/ttyzout of pty devicesN)ropenO_RDWRr )xyZpty_namefdrrrr 1s   r cCstj|tj}yddlm}m}Wntk rG|SYnXy$|||d|||dWntk rYnX|S)zslave_open(tty_name) -> slave_fd Open the pty slave and acquire the controlling terminal, returning opened filedescriptor. Deprecated, use openpty() instead.r)ioctlI_PUSHZptemZldterm)rrrZfcntlrr ImportErrorr )Ztty_nameresultrrrrrr =s   r cCs<ytj\}}Wnttfk r0Yn=X|tkrcytjWqctk r_YqcXn||fSt\}}tj}|tkr%tjtj|tj |t tj |t tj |t |t krtj|ntj tjt tj}tj|n tj|||fS)zdfork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.)rforkptyr r CHILDsetsidrrrdup2 STDIN_FILENO STDOUT_FILENO STDERR_FILENOrrr)pidrr rZtmp_fdrrrrOs0         cCs3x,|r.tj||}||d}qWdS)z#Write all the data to a descriptor.N)rwrite)rdatanrrr_writenws r)cCstj|dS)zDefault read function.i)rread)rrrr_read}sr+cCs|tg}xt|gg\}}}||krk||}|sX|j|qktjt|nt|kr|t}|s|jtqt||qqWdS)zParent copy loop. Copies pty master -> standard output (master_read) standard input -> pty master (stdin_read)N)r"rremoverr&r#r))r master_read stdin_readZfdsZrfdsZwfdsZxfdsr'rrr_copys     r/cCst|tdkr$|f}nt\}}|tkrVtj|d|ny&tjt}tjtd}Wntj k rd}YnXyt |||Wn1t k r|rtj ttj |nYnXtj|tj|ddS)zCreate a spawned process.rr)typerrrexeclpttyZ tcgetattrr"Zsetrawerrorr/r Z tcsetattrZ TCSAFLUSHrwaitpid)argvr-r.r%r modeZrestorerrrrs$       )__doc__rrr3__all__r"r#r$rrrr r rr)r+r/rrrrrs"     (