a �DOg��@s�dZddlmZddlZddlZddlZgd�ZdZdZdZdZ dd�Z d d �Z d d �Z d d�Z dd�Zdd�Zdd�Zeefdd�Zeefdd�ZdS)zPseudo terminal utilities.�)�selectN)�openpty�fork�spawn��c Cs<z t��WSttfy Yn0t�\}}t|�}||fS)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/lib64/python3.9/pty.pyrs  rc CsJzt��\}}Wnttfy&Yn0t�|�}t�|�||fSt�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.)rrr r �ttyname�closer )r rrrrr� master_open"s  rc CsldD]Z}dD]P}d||}zt�|tj�}WntyDYq Yn0|d||fSqtd��dS)z1Open pty master and return (master_fd, tty_name).ZpqrstuvwxyzPQRSTZ0123456789abcdefz/dev/ptyz/dev/ttyzout of pty devicesN)r�open�O_RDWRr )�x�yZpty_name�fdrrrr 2s   r cCsnt�|tj�}zddlm}m}Wnty8|YS0z|||d�|||d�WntyhYn0|S)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_PUSHZptemZldterm)rrrZfcntlrr� ImportErrorr )Ztty_name�resultrrrrrr >s    r c Cs�zt��\}}Wnttfy&Yn20|tkrPz t��WntyNYn0||fSt�\}}t��}|tkr�t��t�|�t� |t �t� |t �t� |t �|t kr�t�|�t� t�t �tj�}t�|�n t�|�||fS)zdfork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.)r�forkptyr r �CHILD�setsidrrr�dup2� STDIN_FILENO� STDOUT_FILENO� STDERR_FILENOrrr)�pidrr rZtmp_fdrrrrPs0          rcCs"|rt�||�}||d�}qdS)z#Write all the data to a descriptor.N)r�write)r�data�nrrr�_writenxs r)cCs t�|d�S)zDefault read function.i)r�read)rrrr�_read~sr+cCsv|tg}t|gg�\}}}||vrF||�}|s:|�|�n t�t|�t|vr|t�}|sf|�t�qt||�qdS)z�Parent copy loop. Copies pty master -> standard output (master_read) standard input -> pty master (stdin_read)N)r"r�removerr&r#r))r � master_read� stdin_read�fdsZrfdsZwfdsZxfdsr'rrr�_copy�s   r0cCs�t|�td�kr|f}t�d|�t�\}}|tkrJtj|dg|�R�zt�t �}t� t �d}Wntj y~d}Yn0zt |||�Wn&t y�|r�t�t tj|�Yn0t�|�t�|d�dS)zCreate a spawned process.�z pty.spawnrr)�type�sys�auditrrr�execlp�ttyZ tcgetattrr"Zsetraw�errorr0r Z tcsetattrZ TCSAFLUSHr�waitpid)�argvr-r.r%r �modeZrestorerrrr�s&       r)�__doc__rrr3r6�__all__r"r#r$rrrr r rr)r+r0rrrrr�s$   (