o ?Og]@sdZddlmZddlZddlZddlZddlmZmZddlmZmZm Z gdZ dZ dZ dZ dZd d Zd d Zd dZddZddZddZddZeefddZeefddZdS)zPseudo terminal utilities.)selectN)closewaitpid)setraw tcgetattr tcsetattr)openptyforkspawnc Cs<ztWSttfyYnwt\}}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*/opt/alt/python310/lib64/python3.10/pty.pyrs  rc CsHzt\}}Wn ttfyYtSwt|}t|||fS)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.)r rrrttynamerr)rrrrrr master_open&s  rc CsddD]+}dD]&}d||}z t|tj}Wn ty Yqw|d||fSqtd)z1Open pty master and return (master_fd, tty_name).ZpqrstuvwxyzPQRSTZ0123456789abcdefz/dev/ptyz/dev/ttyzout of pty devices)r openO_RDWRr)xyZpty_namefdrrrr6s  rcCsnt|tj}z ddlm}m}Wn ty|YSwz|||d|||dW|Sty6Y|Sw)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)r rrZfcntlrr ImportErrorr)Ztty_nameresultrrrrrrBs   rc Cszt\}}Wn ttfyYnw|tkr.z tW||fSty-Y||fSw||fSt\}}t}|tkrvtt|t |t t |t t |t |t krct|t tt tj}t|||fSt|||fS)zdfork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.)r forkptyrrCHILDsetsidrr rdup2 STDIN_FILENO STDOUT_FILENO STDERR_FILENOrrr)pidrrrZtmp_fdrrrr Ts:          r cCs(|rt||}||d}|sdSdS)z#Write all the data to a descriptor.N)r write)rdatanrrr_writen|s   r-cCs t|dS)zDefault read function.i)r read)rrrr_reads r/cCs|tg}|rHt|gg\}}}||vr/z||}Wn ty$d}Ynw|s)dStt|t|vrD|t}|s?|tnt|||sdSdS)zParent copy loop. Copies pty master -> standard output (master_read) standard input -> pty master (stdin_read)N)r&rrr r*r'remover-)r master_read stdin_readZfdsZrfdsZ_wfdsZ_xfdsr+rrr_copys$      r4c Cst|tdkr |f}td|t\}}|tkr%tj|dg|Rz tt}t td}Wn t j y=d}Ynwzt |||W|rOt tt j|n |rZt tt j|wwt|t|ddS)zCreate a spawned process.z pty.spawnrTFr )typesysauditr r#r execlprr&rttyerrorr4rZ TCSAFLUSHrr)argvr2r3r)rmodeZrestorerrrr s.  r )__doc__rr r7r:rrrrr__all__r&r'r(r#rrrrr r-r/r4r rrrrs*   (