
    d                         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	 Zd
 Zd Zd Zd Zd ZeefdZeefdZdS )zPseudo terminal utilities.    )selectN)closewaitpid)setraw	tcgetattr	tcsetattr)openptyforkspawn      c                      	 t          j                    S # t          t          f$ r Y nw xY 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_fds        /croot/python-split_1694437901252/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac/lib/python3.11/pty.pyr	   r	      s`    z||G$   *,,Iz*%%Hhs    ))c                      	 t          j                    \  } }t          j        |          }t          j        |           | |fS # t          t
          f$ r Y nw xY wt                      S )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   r	   ttynamer   r   r   r   )r   r   r   s      r   master_openr   &   sx    
% jll	8 Z))

*$$ G$    s   A AAc                      dD ]M} dD ]H}d| z   |z   }	 t          j        |t           j                  }n# t          $ r Y 7w xY w|d| z   |z   fc c S Nt          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fds       r   r   r   6   s     , ,# 	, 	,A!A~)HWXry11   
Q*++++++	, &
'
''s   2
??c                     t          j        | t           j                  }	 ddlm}m} n# t          $ r |cY S w xY w	  |||d            |||d           n# t          $ r Y nw xY w|S )zslave_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&   s       r   r   r   B   s     WXry))F'''''''''   fff%%%ffh''''   Ms   * 99A 
A%$A%c                     	 t          j                    \  } }| t          k    r%	 t          j                     n# t          $ r Y nw xY w| |fS # t
          t          f$ r Y nw xY wt                      \  }}t          j                    } | t          k    rt          j                     t          j        |           t          j	        |t                     t          j	        |t                     t          j	        |t                     |t          k    rt          j        |           t          j        t          j        t                    t           j                  }t          j        |           nt          j        |           | |fS )zdfork() -> (pid, master_fd)
    Fork and make the child a session leader with a controlling terminal.)r   forkptyCHILDsetsidr   r   r	   r
   r   dup2STDIN_FILENOSTDOUT_FILENOSTDERR_FILENOr   r   r   )pidr#   r   r   tmp_fds        r   r
   r
   T   se   *,,R %<<	    Bw G$    "))Ix
'))C
e||
	
 	,'''
-(((
-(((m##HX M22BI>>

 	>s    A 8 
AAA A c                 ,    t          j        | d          S )zDefault read function.i   )r   read)r#   s    r   _readr9   |   s    72t    c                    t          j        |           rZt          j        | d           	 t          | ||           t          j        | d           n# t          j        | d           w xY wdS d}| t          k    }| t
          k    }d}d}	 g }g }	|r-t          |          |k     r|                    t                     |r(t          |          |k     r|                    |            |r-t          |          dk    r|	                    t
                     t          |          dk    r|	                    |            t          ||	g           \  }}	}
t
          |	v r8	 t          j	        t
          |          }||d         }n# t          $ r d}Y nw xY w| |v r(	  ||           }n# t          $ r d}Y nw xY w|sdS ||z  }| |	v rt          j	        | |          }||d         }|r#t          |v r |t                    }|sd}n||z  })	zParent copy loop.
    Copies
            pty master -> standard output   (master_read)
            standard input -> pty master    (stdin_read)F)master_read
stdin_readTNi   r:   r   r   )r   get_blockingset_blocking_copyr2   r3   lenappendr   writer   )r   r<   r=   high_waterlevelstdin_availstdout_availi_bufo_bufrfdswfds_xfdsndatas                r   r@   r@      sx   
 
y!! 
 		5)))	-)LLLL OIt,,,,BOIt,,,,O|+K-LEE* 	&3u::77KK%%% 	#CJJ88KK	""" 	'CJJNNKK&&&u::>>KK	""""4r22dED  %H]E22abb	 % % %$% "{9--    TMEE**A!""IE 	<4//:l++D #U*s/   A A*$F FFF' 'F65F6c                 d   t          |           t          d          k    r| f} t          j        d|            t                      \  }}|t          k    rt          j        | d         g| R   	 t          t                    }t          t                     d}n# t          j        $ r d}Y nw xY w	 t          |||           |r t          t          t          j        |           n(# |r!t          t          t          j        |           w w xY wt          |           t!          |d          d         S )zCreate a spawned process. z	pty.spawnr   TFr   )typesysauditr
   r/   r   execlpr   r2   r   ttyerrorr@   r   	TCSAFLUSHr   r   )argvr<   r=   r5   r   moderestores          r   r   r      s2   DzzT"XXwIk4   VVNC
e||
	$q'!D!!!!&&|9   9ij111 	9lCM4888  	9lCM48888	9 
)3??1s   .*B B-,B-1C% %%D
)__doc__r   r   rQ   rT   r   r   r   r   r   __all__r2   r3   r4   r/   r	   r   r   r   r
   r9   r@   r    r:   r   <module>r]      s-             				 



 



         , , , , , , , , , ,
&
&
&	
 
 
   
( 
( 
(  $& & &P   "'5 ? ? ? ?B "e      r:   