U e5d@sdZddlmZddlZddlZddlZdddgZdZdZdZdZ d dZ d d Z d d Z ddZ ddZddZddZeefddZeefddZdS)zPseudo terminal utilities.)selectNopenptyforkspawnc Cs>z tWSttfk 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/lib64/python3.8/pty.pyrs  c CsLzt\}}Wnttfk r(YnXt|}t|||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 CsndD]\}dD]R}d||}zt|tj}Wntk rFYq YnX|d||fSqtddS)z1Open pty master and return (master_fd, tty_name).ZpqrstuvwxyzPQRSTZ0123456789abcdefz/dev/ptyz/dev/ttyzout of pty devicesN)ropenO_RDWRr )xyZpty_namefdrrrr 2s  r cCsrt|tj}zddlm}m}Wntk r:|YSXz|||d|||dWntk rlYnX|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 c Cszt\}}Wnttfk r(Yn4X|tkrTz tWntk rRYnX||fSt\}}t}|tkrtt|t |t t |t t |t |t krt|t tt tj}t|n t|||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_fdrrrrPs0         cCs"|rt||}||d}qdS)z#Write all the data to a descriptor.N)rwrite)rdatanrrr_writenxs r)cCs t|dS)zDefault read function.i)rread)rrrr_read~sr+cCsv|tg}t|gg\}}}||krF||}|s:||n tt|t|kr|t}|sf|tqt||qdS)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}td|t\}}|tkrHtj|df|ztt }t t d}Wntj k r~d}YnXzt |||Wn(t k r|rtt tj|YnXt|t|ddS)zCreate a spawned process.z pty.spawnrr)typesysauditrrrexeclpttyZ tcgetattrr"Zsetrawerrorr/r Z tcsetattrZ TCSAFLUSHrwaitpid)argvr-r.r%r modeZrestorerrrrs&      )__doc__rrr2r5__all__r"r#r$rrrr r rr)r+r/rrrrrs$    (