B
    u9a›  ã               @   sŽ   d dl m Z  d dlZd dlZdddg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 )é    )ÚselectNÚopenptyÚforkÚspawné   é   c           	   C   s<   yt  ¡ S  ttfk
r    Y nX tƒ \} }t|ƒ}| |fS )N)Úosr   ÚAttributeErrorÚOSErrorÚ_open_terminalÚ
slave_open)Ú	master_fdÚ
slave_nameÚslave_fd© r   ú/usr/lib/python3.7/pty.pyr      s    
c           	   C   sL   yt  ¡ \} }W n ttfk
r(   Y nX t  |¡}t  |¡ | |fS tƒ S )N)r   r   r	   r
   ÚttynameÚcloser   )r   r   r   r   r   r   Úmaster_open!   s    

r   c           
   C   sl   x^dD ]V} xPdD ]H}d|  | }yt  |t j¡}W n tk
rH   wY nX |d|  | fS W qW tdƒ‚d S )NZpqrstuvwxyzPQRSTZ0123456789abcdefz/dev/ptyz/dev/ttyzout of pty devices)r   ÚopenÚO_RDWRr
   )ÚxÚyZpty_nameÚfdr   r   r   r   1   s    

r   c             C   sn   t  | t j¡}yddlm}m} W n tk
r6   |S X y|||dƒ |||dƒ W n tk
rh   Y nX |S )Nr   )ÚioctlÚI_PUSHZptemZldterm)r   r   r   Zfcntlr   r   ÚImportErrorr
   )Ztty_nameÚresultr   r   r   r   r   r   =   s    r   c           	   C   sð   yt  ¡ \} }W n ttfk
r(   Y n4X | tkrTyt  ¡  W n tk
rR   Y nX | |fS tƒ \}}t  ¡ } | tkrÞt  ¡  t  |¡ t  	|t
¡ t  	|t¡ t  	|t¡ |tkr¾t  |¡ t  t  t¡t j¡}t  |¡ n
t  |¡ | |fS )N)r   Úforkptyr	   r
   ÚCHILDÚsetsidr   r   r   Údup2ÚSTDIN_FILENOÚSTDOUT_FILENOÚSTDERR_FILENOr   r   r   )Úpidr   r   r   Ztmp_fdr   r   r   r   O   s0    



c             C   s&   x |r t  | |¡}||d … }qW d S )N)r   Úwrite)r   ÚdataÚnr   r   r   Ú_writenw   s    r)   c             C   s   t  | d¡S )Ni   )r   Úread)r   r   r   r   Ú_read}   s    r+   c             C   sz   | t g}xlt|g g ƒ\}}}| |krH|| ƒ}|s<| | ¡ nt t|¡ t |kr
|t ƒ}|sh| t ¡ q
t| |ƒ q
W d S )N)r"   r   Úremover   r&   r#   r)   )r   Úmaster_readÚ
stdin_readZfdsZrfdsZwfdsZxfdsr'   r   r   r   Ú_copy   s    r/   c             C   sÈ   t | ƒt dƒkr| f} tƒ \}}|tkr<tj| d f| žŽ  yt t¡}t t¡ d}W n tj	k
rr   d}Y nX yt
|||ƒ W n( tk
r¬   |r¨t ttj|¡ Y nX t |¡ t |d¡d S )NÚ r   r   )Útyper   r   r   ÚexeclpÚttyZ	tcgetattrr"   ZsetrawÚerrorr/   r
   Z	tcsetattrZ	TCSAFLUSHr   Úwaitpid)Úargvr-   r.   r%   r   ÚmodeZrestorer   r   r   r   –   s$    




)r   r   r3   Ú__all__r"   r#   r$   r   r   r   r   r   r   r)   r+   r/   r   r   r   r   r   Ú<module>	   s    
(