o
    éT•j]  ã                   @   s¼   d Z ddlmZ ddlZddlZddlZddlmZmZ ddlmZmZm	Z	 g d¢Z
dZdZdZdZd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zeefdd„Zeefdd„ZdS )zPseudo terminal utilities.é    )ÚselectN)ÚcloseÚwaitpid)ÚsetrawÚ	tcgetattrÚ	tcsetattr)ÚopenptyÚforkÚspawné   é   c               	   C   s<   zt  ¡ W S  ttfy   Y nw tƒ \} }t|ƒ}| |fS )zdopenpty() -> (master_fd, slave_fd)
    Open a pty master/slave pair, using os.openpty() if possible.)Úosr   ÚAttributeErrorÚOSErrorÚ_open_terminalÚ
slave_open)Ú	master_fdÚ
slave_nameÚslave_fd© r   ú/usr/lib/python3.10/pty.pyr      s   
ÿ
r   c               	   C   sH   zt  ¡ \} }W n ttfy   Y tƒ S w t  |¡}t  |¡ | |fS )z›master_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   r   r   r   Úttynamer   r   )r   r   r   r   r   r   Úmaster_open&   s   ù

r   c               
   C   sd   dD ]+} dD ]&}d|  | }z	t  |t j¡}W n	 ty    Y qw |d|  | f    S qtdƒ‚)z1Open pty master and return (master_fd, tty_name).ÚpqrstuvwxyzPQRSTÚ0123456789abcdefz/dev/ptyz/dev/ttyzout of pty devices)r   ÚopenÚO_RDWRr   )ÚxÚyÚpty_nameÚfdr   r   r   r   6   s   ÿúr   c                 C   sn   t  | t j¡}z
ddlm}m} W n ty   | Y S w z|||dƒ |||dƒ W |S  ty6   Y |S w )z©slave_open(tty_name) -> slave_fd
    Open the pty slave and acquire the controlling terminal, returning
    opened filedescriptor.
    Deprecated, use openpty() instead.r   )ÚioctlÚI_PUSHÚptemÚldterm)r   r   r   Úfcntlr!   r"   ÚImportErrorr   )Útty_nameÚresultr!   r"   r   r   r   r   B   s   ÿþþr   c               	   C   sþ   zt  ¡ \} }W n ttfy   Y nw | tkr.z	t  ¡  W | |fS  ty-   Y | |fS w | |fS tƒ \}}t  ¡ } | tkrvt  ¡  t  |¡ t  	|t
¡ t  	|t¡ t  	|t¡ |tkrct  |¡ t  t  t¡t j¡}t  |¡ | |fS t  |¡ | |fS )zdfork() -> (pid, master_fd)
    Fork and make the child a session leader with a controlling terminal.)r   Úforkptyr   r   ÚCHILDÚsetsidr   r	   r   Údup2ÚSTDIN_FILENOÚSTDOUT_FILENOÚSTDERR_FILENOr   r   r   )Úpidr    r   r   Útmp_fdr   r   r   r	   T   s:   ÿ
ýý




ýr	   c                 C   s(   |rt  | |¡}||d… }|sdS dS )z#Write all the data to a descriptor.N)r   Úwrite)r    ÚdataÚnr   r   r   Ú_writen|   s   þr5   c                 C   s   t  | d¡S )zDefault read function.i   )r   Úread)r    r   r   r   Ú_read‚   s   r7   c                 C   s”   | t g}|rHt|g g ƒ\}}}| |v r/z|| ƒ}W n ty$   d}Y nw |s)dS t t|¡ t |v rD|t ƒ}|s?| t ¡ nt| |ƒ |sdS dS )z�Parent copy loop.
    Copies
            pty master -> standard output   (master_read)
            standard input -> pty master    (stdin_read)ó    N)r-   r   r   r   r2   r.   Úremover5   )r   Úmaster_readÚ
stdin_readÚfdsÚrfdsÚ_wfdsÚ_xfdsr3   r   r   r   Ú_copy†   s$   ÿ
ër@   c              
   C   sÌ   t | ƒt dƒkr| f} t d| ¡ tƒ \}}|tkr%tj| d g| ¢R Ž  zttƒ}t	tƒ d}W n t
jy=   d}Y nw zt|||ƒ W |rOttt
j|ƒ n|rZttt
j|ƒ w w t|ƒ t|dƒd S )zCreate a spawned process.Ú z	pty.spawnr   TFr   )ÚtypeÚsysÚauditr	   r*   r   Úexeclpr   r-   r   ÚttyÚerrorr@   r   Ú	TCSAFLUSHr   r   )Úargvr:   r;   r0   r   ÚmodeÚrestorer   r   r   r
   £   s.   
ÿ€ÿÿr
   )Ú__doc__r   r   rC   rF   r   r   r   r   r   Ú__all__r-   r.   r/   r*   r   r   r   r   r	   r5   r7   r@   r
   r   r   r   r   Ú<module>   s*    (